diff --git a/src/userland/cli/head.c b/src/userland/cli/head.c new file mode 100644 index 0000000..ec67f99 --- /dev/null +++ b/src/userland/cli/head.c @@ -0,0 +1,54 @@ +// Copyright (c) 2026 janevers (https://github.com/janevers-sys) +// This software is released under the GNU General Public License v3.0. See LICENSE file for details. +// This header needs to maintain in any file it is present in, as per the GPL license terms. +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc == 2) { + if(strcmp(argv[1], "--help")) { + printf("-This command reads from the front of the file\n"); + printf("-Use: tail [-n] [-number of lines] [file]\n"); + return 0; + } + //print everything + FILE *fp = fopen(argv[1], "r"); + if (fp == NULL) { + printf("File not found\n"); + return 1; + } + char buf[1024]; + while (fgets(buf, sizeof(buf), fp)) { + printf("%s", buf); + } + fclose(fp); + return 0; + } + + if (argc == 4 && strcmp(argv[1], "-n") == 0) { + int lines = atoi(argv[2]); + if (lines <= 0) { + printf("Number of lines must be positive\n"); + return 1; + } + + FILE *fp = fopen(argv[3], "r"); + if (fp == NULL) { + printf("File not found\n"); + return 1; + } + + char buf[1024]; + while (lines > 0 && fgets(buf, sizeof(buf), fp)) { + printf("%s", buf); + lines--; + } + + fclose(fp); + return 0; + } + + printf("Wrong flag, --help for more info\n"); + return 1; +} diff --git a/src/userland/cli/help.c b/src/userland/cli/help.c index 8f651e3..2db5f0b 100644 --- a/src/userland/cli/help.c +++ b/src/userland/cli/help.c @@ -39,6 +39,10 @@ int main(int argc, char **argv) { printf("exit - Exit the terminal\n"); printf("net - Network tools\n"); printf("time - Measure command execution time\n"); + printf("find - find files or dol") + printf("rev - Reverse a string or file\n"); + printf("head - print lines from the top down\n"); + printf("tail - print lines from te bottom up\n"); printf("\nHint: Use Ctrl+C to force quit any running application.\n"); return 0; } diff --git a/src/userland/cli/rev.c b/src/userland/cli/rev.c new file mode 100644 index 0000000..e39b396 --- /dev/null +++ b/src/userland/cli/rev.c @@ -0,0 +1,48 @@ +// Copyright (c) 2026 janevers (https://github.com/janevers-sys) +// This software is released under the GNU General Public License v3.0. See LICENSE file for details. +// This header needs to maintain in any file it is present in, as per the GPL license terms. + +#include +#include +#include +#include + +// reverse function +void reverse(char *str) { + int len = strlen(str); + for (int i = 0; i < len / 2; i++) { + char tmp = str[i]; + str[i] = str[len - 1 - i]; + str[len - 1 - i] = tmp; + } +} + +int main(int argc, char *argv[]) { + // check if there are too much flags + if (argc != 2) { + printf("Wrong input, use --help for more info\n"); + return 1; + } + if (argc == 2 && strcmp(argv[1], "--help") == 0) { + printf("Reverses the contents of a file or text\n"); + printf("Usage: rev [file]\n"); + return 0; + } + + FILE *fp = fopen(argv[1], "r"); + // check if the argument is a file or not by opening it + if (fp == NULL) { + reverse(argv[1]); + printf("%s\n", argv[1]); + return 0; + } + // reverse if flag is a file + char line[1024]; + while (fgets(line, sizeof(line), fp)) { + line[strcspn(line, "\n")] = '\0'; + reverse(line); + printf("%s\n", line); + } + fclose(fp); + return 0; +} diff --git a/src/userland/cli/tail.c b/src/userland/cli/tail.c new file mode 100644 index 0000000..3fba348 --- /dev/null +++ b/src/userland/cli/tail.c @@ -0,0 +1,68 @@ +// Copyright (c) 2026 janevers (https://github.com/janevers-sys) +// This software is released under the GNU General Public License v3.0. See LICENSE file for details. +// This header needs to maintain in any file it is present in, as per the GPL license terms. +#include +#include +#include + +int main(int argc, char *argv[]) { + // print all lines + if (argc == 2) { + if (strcmp(argv[1], "--help") == 0) { + printf("-This command reads from the back of the file\n"); + printf("-Use: tail [-n] [-number of lines] [file]\n"); + return 0; + } + FILE *fp = fopen(argv[1], "r"); + if (fp == NULL) { + printf("File not found\n"); + return 1; + } + char buf[1024]; + while (fgets(buf, sizeof(buf), fp)) { + printf("%s", buf); + } + fclose(fp); + return 0; + } + // only print last lines + if (argc == 4 && strcmp(argv[1], "-n") == 0) { + int lines = atoi(argv[2]); + if (lines <= 0) { + printf("Number of lines must be positive\n"); + return 1; + } + + FILE *fp = fopen(argv[3], "r"); + if (fp == NULL) { + printf("File not found\n"); + return 1; + } + + // count total lines + int count = 0; + char buf[1024]; + while (fgets(buf, sizeof(buf), fp)) { + count++; + } + + // back to start of file + fseek(fp, 0, SEEK_SET); + + // check if lines are the end lines + int skip = count - lines; + int i = 0; + while (fgets(buf, sizeof(buf), fp)) { + if (i >= skip) { + printf("%s", buf); + } + i++; + } + + fclose(fp); + return 0; + } + + printf("Wrong flag, --help for more info\n"); + return 1; +}