From 047beae5b5d932e096c163097b8ce8bd48680eaf Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 10 Nov 2023 06:45:25 +0300 Subject: [PATCH] Big update 8/N fix --- builder/builder.c | 7 +-- coreutils/echo.c | 123 +++++++++++++++++++--------------------------- 2 files changed, 55 insertions(+), 75 deletions(-) diff --git a/builder/builder.c b/builder/builder.c index 35a71ce..b239a3c 100644 --- a/builder/builder.c +++ b/builder/builder.c @@ -44,12 +44,13 @@ void Compile(const char *src, const char *output_dir) { exit(1); execlp(CC, CC, CFLAGS, src, "-o", path, NULL); - // kill(getppid(), 9); - // Probably doesn't need it as the child is resulted with exit(1) /* If compiler return 1 */ exit(1); - } else if (pid == -1) { + + } + + else if (pid == -1) { fprintf(stderr, "%s", "builder: fork failed"); exit(1); } diff --git a/coreutils/echo.c b/coreutils/echo.c index 3c590a8..1404933 100644 --- a/coreutils/echo.c +++ b/coreutils/echo.c @@ -1,97 +1,76 @@ #include -#include -#include #include -// I want to perform some sort of hack using errno for flag passing -static char should_stop = 0; - -char* ScreenString(const char* str) { - size_t len = strlen(str); - char* buf = malloc(len + 1); - size_t i, buf_idx = 0; - - for(i = 0; i < len - 1; i++) { - if (str[i] == '\\') { - switch(str[i+1]) { +void format(char *str) { + for (size_t i = 0; i < strlen(str); i++) { + unsigned int c = str[i]; + if (c == '\\') { + switch (str[i + 1]) { case 'a': - buf[buf_idx++] = '\a'; + c = '\a'; break; - case 'b': - buf[buf_idx++] = '\b'; - break; - case 'c': - buf[buf_idx] = '\0'; - should_stop = 1; - return buf; - case 'f': - buf[buf_idx++] = '\f'; - break; - case 'n': - buf[buf_idx++] = '\n'; - break; - case 'r': - buf[buf_idx++] = '\r'; - break; - case 't': - buf[buf_idx++] = '\t'; - break; - case 'v': - buf[buf_idx++] = '\v'; - break; - case '\\': - buf[buf_idx++] = '\\'; - break; - case '0': - unsigned int c = 0; - int offset = 0; - if (sscanf(str + i + 1, "%3o%n", &c, &offset)) { - buf[buf_idx++] = c; - i += offset; - } - break; - default: - buf[buf_idx++] = '\\'; - continue; - } - i++; - } else { - buf[buf_idx++] = str[i]; - } - } - if (i < len) buf[buf_idx] = str[i]; - errno = 0; - return buf; + case 'n': + c = '\n'; + break; + + case 't': + c = '\t'; + break; + + case 'c': + exit(0); + + case 'v': + c = '\v'; + break; + + case 'r': + c = '\r'; + break; + + case 'f': + c = '\f'; + break; + + case 'e': + c = '\033'; + break; + + case 'b': + c = '\b'; + break; + + default: + c = '\\'; + } + + i++; + } + + putchar(c); + } } -int main(int argc, const char **argv) { - char newline = 1; +int main(int argc, char **argv) { + unsigned int newline = 1; if (argc > 1) { - if (strcmp(argv[1], "-n") == 0) { + if (!strcmp(argv[1], "-n")) { newline = 0; argv++; argc--; } for (int i = 1; i < argc; i++) { - char* screened = ScreenString(argv[i]); - fputs(screened, stdout); - free(screened); - if (should_stop) return 0; + format(argv[i]); if (i < argc - 1) putchar(' '); } } - /* https://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html */ - /* This version does not support -n option and escape-sequences */ - /* https://gist.github.com/fogus/1094067/ */ - /* It seems like escape sequences are supported only by GNU's one */ - /* I'd implemented them anyway but you may like not to include them */ - /* Or to switch them with a flag */ if (newline) putchar('\n'); + return 0; }