From 67c41c4c93d552e0a1648c5eb00b6e4fdf04c5a5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 17 Dec 2023 16:45:13 +0300 Subject: [PATCH] fix --- TODO | 1 - builder/config.h | 2 +- coreutils/nl.c | 11 +++++---- coreutils/truncate.c | 55 -------------------------------------------- coreutils/who.c | 35 ++++++++++++++++++++++++++++ findutils/xargs.c | 8 +++---- networking/nc.c | 49 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 66 deletions(-) delete mode 100644 coreutils/truncate.c create mode 100644 coreutils/who.c create mode 100644 networking/nc.c diff --git a/TODO b/TODO index 92b6c8b..5336dad 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,6 @@ With "micro-" prefix *Todo: -*nl tail expr uniq diff --git a/builder/config.h b/builder/config.h index 750f642..d578eb5 100644 --- a/builder/config.h +++ b/builder/config.h @@ -19,6 +19,6 @@ const char *objects[] = { /* Need by spark */ #define LDFLAGS "-lm" -#define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-flto", "-Os", "-s", "-I../libmu", "-I../", LDFLAGS +#define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-flto", "-Os", "-s", "-I../libmu", "-I../", "--std", "c99", LDFLAGS #define CC "cc" #endif diff --git a/coreutils/nl.c b/coreutils/nl.c index b84f4bf..1a1fb22 100644 --- a/coreutils/nl.c +++ b/coreutils/nl.c @@ -20,17 +20,18 @@ int nl(const char *path) { return 1; } - char buf[BUF_SIZE + 1]; - while (fgets(buf, sizeof(buf), fp)) { - size_t len = strlen(buf); - - if (len > 1) + char *buf = NULL; + size_t size = 0; + while (getline(&buf, &size, fp) != EOF) { + if (strlen(buf) > 1) fprintf(stdout, "%*u\t%s", w_flag, lines++, buf); else fprintf(stdout, "%*s\t%s", w_flag, " ", buf); } + free(buf); + if (strcmp(path, "-")) fclose(fp); diff --git a/coreutils/truncate.c b/coreutils/truncate.c deleted file mode 100644 index 9a3beb0..0000000 --- a/coreutils/truncate.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include -#include -#include -#include - -int main(int argc, char **argv) { - unsigned int c_flag = 0; - unsigned int s_size = 0; - - int opt; - while ((opt = getopt(argc, argv, "s:c")) != -1) { - switch (opt) { - case 's': - s_size = atoi(optarg); - break; - - case 'c': - c_flag = 1; - break; - - default: - printf("truncate [file]\n\t[-c Do not create files] [-s SIZE]\n"); - return 0; - } - } - - argv += optind; - argc -= optind; - - if (argc == 0) { - fprintf(stderr, "truncate: missing operand\n"); - return 1; - } - - int flags = O_WRONLY | O_NONBLOCK; - if (!c_flag) - flags |= O_CREAT; - - int fd = open(argv[0], flags, 0666); - if (fd < 0) { - fprintf(stderr, "truncate %s %s\n", argv[0], strerror(errno)); - return 1; - } - - if (ftruncate(fd, s_size) == -1) { - close(fd); - fprintf(stderr, "truncate %s %s\n", argv[0], strerror(errno)); - return 1; - } - - close(fd); - return 0; -} diff --git a/coreutils/who.c b/coreutils/who.c new file mode 100644 index 0000000..c8b1310 --- /dev/null +++ b/coreutils/who.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +int time_to_str(char *buf, size_t len, struct utmpx *usr) { + time_t t = usr->ut_tv.tv_sec; + struct tm *tmp = localtime(&t); + + if (tmp) + strftime(buf, len, "%F %H:%M", tmp); + + else + return 1; + + return 0; +} + +int main(void) { + setutxent(); + + struct utmpx *usr; + while ((usr = getutxent())) { + if (usr->ut_type == USER_PROCESS) { + char time_buf[20]; + if (time_to_str(time_buf, sizeof(time_buf), usr)) + continue; + + printf("%s\t%s\t%s\t(%s)\n", usr->ut_user, usr->ut_line, time_buf, usr->ut_host); + } + } + + endutxent(); +} diff --git a/findutils/xargs.c b/findutils/xargs.c index 44da7a4..6fd08ee 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c @@ -50,7 +50,7 @@ int stdin_read(void) { break; } - else if (isspace(c) && strlen(arg) > 0) { + else if ((isspace(c)) && strlen(arg) > 0) { *p = '\0'; p = arg; @@ -61,6 +61,9 @@ int stdin_read(void) { } else { + if (c == '\\') + c = getchar(); + *p = c; if (p + 1 == arg + sizeof(arg)) break; @@ -96,9 +99,6 @@ int run(void) { } int main(int argc, char **argv) { - for (int i = 0; i < argc; i++) - if (argv[i][0] == '-') - argc--; int opt; while ((opt = getopt(argc, argv, "tn:r")) != -1) { diff --git a/networking/nc.c b/networking/nc.c new file mode 100644 index 0000000..d48f944 --- /dev/null +++ b/networking/nc.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include "unused.h" + +int main(int argc, char **argv) { + int family = AF_UNSPEC; + int port = 33137; + unsigned int l_flag; + + UNUSED(port); + UNUSED(l_flag); + UNUSED(family); + + int opt; + while ((opt = getopt(argc, argv, "46vp:nl")) != 1) { + switch (opt) { + case '4': + family = AF_INET; + break; + + case '6': + family = AF_INET6; + break; + + case 'l': + l_flag = 1; + break; + + case 'p': + port = atoi(optarg); + break; + + default: + printf("nc\n\t[-4 Use ipv4] [-6 Use ipv6]\n\t[-v Verbose] [-p Port Listen port]\n\t[-u Udp mode] [-l Listen]\n\t[-n Don't do DNS resolution]\n"); + return 0; + } + } + + argv += optind; + argc -= optind; + + signal(SIGPIPE, SIG_IGN); + + return 0; +}