From 11b3a7907af642349a3b514411572e2ecf03dc22 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 8 Nov 2023 19:47:09 +0300 Subject: [PATCH] fix --- TODO | 3 +- builder/builder.c | 9 ++- builder/config.h | 2 +- coreutils/ls.c | 160 ++++++++++++++++++++-------------------------- coreutils/mv.c | 28 ++++++-- coreutils/tee.c | 6 -- procps/uptime.c | 8 ++- 7 files changed, 108 insertions(+), 108 deletions(-) delete mode 100644 coreutils/tee.c diff --git a/TODO b/TODO index 88aa615..8aee5db 100644 --- a/TODO +++ b/TODO @@ -2,7 +2,8 @@ PlainOs With "micro-" prefix *Todo: -ls +**ls +**tee tail uniq head diff --git a/builder/builder.c b/builder/builder.c index 7f18ca5..0f85bc4 100644 --- a/builder/builder.c +++ b/builder/builder.c @@ -40,6 +40,9 @@ void Compile(const char *src, const char *output_dir) { pid_t pid; if ((pid = fork()) == 0) { + if (pid == 1) + exit(1); + execlp(CC, CC, CFLAGS, src, "-o", path, NULL); kill(getpid(), 9); @@ -47,21 +50,21 @@ void Compile(const char *src, const char *output_dir) { exit(1); } + free(path); + int status = 0; waitpid(pid, &status, 0); if (status) exit(1); - - free(path); } void ListAndCompile(const char *dir, const char *output_dir) { - printf("[CHDIR] %s\n", dir); if (chdir(dir) < 0) { fprintf(stderr, "builder: %s: %s\n", dir, strerror(errno)); exit(1); } + printf("[CHDIR] %s\n", dir); DIR *dp = opendir("."); struct dirent *ep; diff --git a/builder/config.h b/builder/config.h index e47e89d..3e9d9bc 100644 --- a/builder/config.h +++ b/builder/config.h @@ -14,6 +14,6 @@ const char *libs[] = { "readline" }; -#define CFLAGS "-Wall", "-Wextra", "-pedantic", "-Os", "-s", "-I", "../libmu" +#define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-Os", "-s", "-I", "../libmu" #define CC "cc" #endif diff --git a/coreutils/ls.c b/coreutils/ls.c index 2f47e29..1a5c9dd 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "make_path.h" #include "get_stat.h" @@ -16,92 +17,33 @@ unsigned int a_flag; unsigned int l_flag; unsigned int p_flag; -void PrintPerm(struct stat sb) { - if (S_ISDIR(sb.st_mode)) - printf("d"); +struct d_node { + char *name; + struct d_node *next; + struct stat stats; +}; - else if (S_ISLNK(sb.st_mode)) - printf("l"); +struct d_node *stat_file(char *filename) { + struct d_node *file = malloc(sizeof(struct d_node)); + if (file == NULL) + return NULL; - else if (S_ISCHR(sb.st_mode)) - printf("c"); + if (mu_get_lstat("ls", filename, &file->stats)) + return NULL; - else if (S_ISFIFO(sb.st_mode)) - printf("p"); - - else if (S_ISSOCK(sb.st_mode)) - printf("s"); - - else - printf("-"); - - printf("%c%c%c", (sb.st_mode & S_IRUSR) ? 'r' : '-', (sb.st_mode & S_IWUSR) ? 'w' : '-', (sb.st_mode & S_IXUSR) ? 'x' : '-'); - printf("%c%c%c", (sb.st_mode & S_IRGRP) ? 'r' : '-', (sb.st_mode & S_IWGRP) ? 'w' : '-', (sb.st_mode & S_IXGRP) ? 'x' : '-'); - printf("%c%c%c", (sb.st_mode & S_IROTH) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXOTH) ? 'x' : '-'); + return file; } -char *fileflag(const char *path) { - struct stat sb; - if (mu_get_lstat("ls", path, &sb)) - return " "; - - if (S_ISDIR(sb.st_mode)) - return "/"; - - else if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) || (sb.st_mode & S_IXOTH)) - return "*"; - - else - return " "; -} - -void PrintInfo(struct stat sb, const char *filename) { - /* Permissions */ - PrintPerm(sb); - - /* Date */ - struct tm *tm = localtime(&sb.st_mtime); - - char date[14]; - if (strftime(date, sizeof(date), "%b %d %H:%M", tm) == 0) { - fprintf(stderr, "ls: strftime()\n"); - return; - } - - /* Group and user name */ - struct passwd *pw = getpwuid(sb.st_uid); - struct group *gr = getgrgid(sb.st_gid); - - printf(" %s %s %jd %s %s%s\n", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)sb.st_size, date, filename, fileflag(filename)); -} - -int list(const char *path, int label) { - struct stat sb; - if (mu_get_lstat("ls", path, &sb)) - return 1; - - /* If its file */ - if (!S_ISDIR(sb.st_mode)) { - if (l_flag) - PrintInfo(sb, path); - - else - puts(path); - - return 0; - } - - /* Make label */ - if (label) - printf("\n%s: \n", path); - - /* Open and print dir */ +struct d_node **list(const char *path, size_t *nfiles) { DIR *dp = opendir(path); if (dp == NULL) { fprintf(stderr, "ls: %s: %s\n", path, strerror(errno)); - return 1; + return NULL; } + struct d_node **dir, *cur, *dr = NULL; + size_t files = 0; + struct dirent *ep; while ((ep = readdir(dp)) != NULL) { if (ep->d_name[0] == '.' && !a_flag) @@ -109,28 +51,58 @@ int list(const char *path, int label) { char *full_path = mu_make_path("ls", path, ep->d_name); if (full_path == NULL) - return 1; + continue; - if (l_flag) { - if (mu_get_lstat("ls", full_path, &sb)) - return 1; - - PrintInfo(sb, full_path); + cur = stat_file(full_path); + if (cur == NULL) { + free(full_path); + continue; } - else - printf("%s%s\n", ep->d_name, fileflag(full_path)); - free(full_path); + cur->name = ep->d_name; + cur->next = dr; + dr = cur; + + files++; + } + + if (dr == NULL) + return NULL; + + *nfiles = files; + + dir = malloc((files + 1) * sizeof(struct d_node *)); + if (dir == NULL) { + fprintf(stderr, "ls: malloc failed\n"); + exit(1); + } + + for (size_t i = 0; ; i++) { + dir[i] = dr; + dr = dr->next; + if (dr == NULL) + break; } closedir(dp); - printf("\n"); - return 0; + return dir; } +void dfree(struct d_node **dir) { + if (dir == NULL) + return; + struct d_node *cur = dir[0], *next; + while (cur != NULL) { + next = cur->next; + free(cur); + cur = next; + } + + free(dir); +} int main(int argc, char **argv) { int opt; @@ -153,8 +125,18 @@ int main(int argc, char **argv) { argv += optind; argc -= optind; - if (argc == 0) - list(".", 0); + struct d_node **dir = NULL; + size_t files = 0; + if (argc < 1) + dir = list(".", &files); + + if (argc == 1) + dir = list(argv[0], &files); + + for (size_t i = 0; i < files; i++) + puts(dir[i]->name); + + dfree(dir); return 0; } diff --git a/coreutils/mv.c b/coreutils/mv.c index cbf47e4..2bae2f9 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c @@ -28,16 +28,32 @@ int move(const char *src, const char *dst) { return ret; } -int main(const int argc, const char **argv) { - if (argc <= 2 || argv[argc - 1][0] == '-') { - printf("mv [src1 src2...] [dst]\n"); - return 0; +int main(int argc, char **argv) { + unsigned int f_flag = 0; + + int opt; + while ((opt = getopt(argc, argv, "f")) != -1) { + switch (opt) { + case 'f': + f_flag = 1; + break; + + default: + printf("mv [src1 src2...] [dst]\n\t[-f Force]\n"); + return 0; + } } - for (int i = 1; i < argc - 1; i++) { + argv += optind; + argc -= optind; + + /* Move code */ + for (int i = 0; i < argc - 1; i++) { if (rename(argv[i], argv[argc - 1]) < 0) { if (move(argv[i], argv[argc - 1])) { - fprintf(stderr, "mv: %s %s\n", argv[i], strerror(errno)); + if (!f_flag) + fprintf(stderr, "mv: %s %s\n", argv[i], strerror(errno)); + return 1; } } diff --git a/coreutils/tee.c b/coreutils/tee.c deleted file mode 100644 index fb11188..0000000 --- a/coreutils/tee.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main(const int argc, const char **argv) { - - return 0; -} diff --git a/procps/uptime.c b/procps/uptime.c index 1c0fc04..5756337 100644 --- a/procps/uptime.c +++ b/procps/uptime.c @@ -38,7 +38,7 @@ int main(void) { printf(" %d mins", mins); } - +#ifndef __ANDROID__ /* Print 1, 5 and 15 minute load averages */ double avg[3] = {0, 0, 0}; @@ -47,6 +47,10 @@ int main(void) { return 1; } - printf(" load average: %.2f %.2f %.2f\n", avg[0], avg[1], avg[2]); + printf(" load average: %.2f %.2f %.2f", avg[0], avg[1], avg[2]); + +#endif + + putchar('\n'); return 0; }