From aad798d9df7417009fa4438aac61ac8e0e4ded36 Mon Sep 17 00:00:00 2001 From: radiohotline Date: Sat, 14 Mar 2026 16:20:47 +0300 Subject: first commit --- src/tail.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/tail.c (limited to 'src/tail.c') diff --git a/src/tail.c b/src/tail.c new file mode 100644 index 0000000..e34b90f --- /dev/null +++ b/src/tail.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#define PROGRAM_NAME "tail" + +struct stat statbuf; + +int main(int argc, char* argv[]) { + FILE *fileptr = NULL; + char buf[8192]; + char ch; + int size = 0, lines = 0; + + if (argc < 2) { + while (fgets(buf, sizeof(buf), stdin) != NULL) { + printf("%s", buf); + } + + return 0; + } + + if (access(argv[1], F_OK) != 0) { + printf("%s: %s: No such file or directory.\n", PROGRAM_NAME, argv[1]); + return 1; + } + + if (stat(argv[1], &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) { + printf("%s: %s is a directory.\n", PROGRAM_NAME, argv[1]); + return 1; + } + + fileptr = fopen(argv[1], "r"); + + if (fileptr == NULL) { + printf("ermmmm can't open this\n"); + return 1; + } + + while ((ch = fgetc(fileptr)) != EOF) { + size++; + } + + size = size - 2; + rewind(fileptr); + + while (size >= 0 && lines < 10) { + fseek(fileptr, size, 0); + ch = fgetc(fileptr); + if (ch == '\n') { + lines++; + } + putchar(ch); + size--; + } + + fclose(fileptr); + return 0; +} -- cgit v1.3