From 9f12d068fc403e0fc4b0c696d819485a12ab9561 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 18 Mar 2024 00:46:58 +0300 Subject: [PATCH] fixed du ls chmod --- include/libmu/human.h | 1 + include/libmu/mode_to_str.h | 57 +++++++++++++++++++++++++++++++++++++ include/libmu/parse_mode.h | 19 +++++++++++-- src/coreutils/chmod/chmod.c | 3 +- src/coreutils/du/du.c | 21 ++++++-------- src/coreutils/ls/ls.c | 55 ++--------------------------------- src/init/init/init.c | 2 +- 7 files changed, 87 insertions(+), 71 deletions(-) create mode 100644 include/libmu/mode_to_str.h diff --git a/include/libmu/human.h b/include/libmu/human.h index 1248b42..c74f5b9 100644 --- a/include/libmu/human.h +++ b/include/libmu/human.h @@ -14,6 +14,7 @@ char *mu_humansize(off_t n, off_t block) { double size = n; size_t i; + for (i = 0; i < strlen(postfixes) && size >= block; i++) size /= block; diff --git a/include/libmu/mode_to_str.h b/include/libmu/mode_to_str.h new file mode 100644 index 0000000..de56f83 --- /dev/null +++ b/include/libmu/mode_to_str.h @@ -0,0 +1,57 @@ +#ifndef _MODE_TO_STR_H +#define _MODE_TO_STR_H + +#include +#include + +char *mu_mode_2_str(mode_t file_mode) { + static char mode[11]; + snprintf(mode, sizeof(mode), "----------"); + + if (file_mode & S_IRUSR) + mode[1] = 'r'; + + if (file_mode & S_IRGRP) + mode[4] = 'r'; + + if (file_mode & S_IROTH) + mode[7] = 'r'; + + if (file_mode & S_IWUSR) + mode[2] = 'w'; + + if (file_mode & S_IWGRP) + mode[5] = 'w'; + + if (file_mode & S_IWOTH) + mode[8] = 'w'; + + if (file_mode & S_IXUSR) + mode[3] = 'x'; + + if (file_mode & S_IXGRP) + mode[6] = 'x'; + + if (file_mode & S_IXOTH) + mode[9] = 'x'; + + if (file_mode & S_ISUID) { + if (file_mode & S_IXUSR) + mode[3] = 's'; + + else + mode[3] = 'S'; + } + + if (file_mode & S_ISGID) { + if (file_mode & S_IRGRP) + mode[6] = 's'; + + else + mode[6] = 'S'; + } + + return mode; +} + +#endif diff --git a/include/libmu/parse_mode.h b/include/libmu/parse_mode.h index 66877c8..884e9db 100644 --- a/include/libmu/parse_mode.h +++ b/include/libmu/parse_mode.h @@ -12,6 +12,7 @@ #define WR_PERM (S_IWUSR | S_IWGRP | S_IWOTH) #define EX_PERM (S_IXUSR | S_IXGRP | S_IXOTH) #define RD_PERM (S_IRUSR | S_IRGRP | S_IROTH) +#define SU_PERM (S_ISUID | S_ISGID | S_ISVTX) #define FULL_PERM (WR_PERM | EX_PERM | RD_PERM) mode_t mu_parse_mode(const char *s, mode_t cur_mode) { @@ -27,7 +28,7 @@ mode_t mu_parse_mode(const char *s, mode_t cur_mode) { mode = 0; /* Default + */ - int append = 1; + char append = 1; mode_t mask = 0; for (size_t i = 0; i < strlen(s); i++) { @@ -44,6 +45,10 @@ mode_t mu_parse_mode(const char *s, mode_t cur_mode) { mode |= EX_PERM; break; + case 's': + mode |= SU_PERM; + break; + case '+': append = 1; break; @@ -52,6 +57,10 @@ mode_t mu_parse_mode(const char *s, mode_t cur_mode) { append = 0; break; + case '=': + append = 2; + break; + case 'g': mask |= G; break; @@ -77,10 +86,14 @@ mode_t mu_parse_mode(const char *s, mode_t cur_mode) { mask = U; mask &= mode; - if (!append) + if (append == 0) mode = ~mode; - return (cur_mode & ~mask) | (mode & mask); + if (append == 2) + return mode & mask; + + else + return (cur_mode & ~mask) | (mode & mask); } #endif diff --git a/src/coreutils/chmod/chmod.c b/src/coreutils/chmod/chmod.c index 7bb0c1c..f22141e 100644 --- a/src/coreutils/chmod/chmod.c +++ b/src/coreutils/chmod/chmod.c @@ -4,6 +4,7 @@ #include #include #include +#include "mode_to_str.h" #include "make_path.h" #include "get_stat.h" #include "parse_mode.h" @@ -30,7 +31,7 @@ int change(const char *file, void *p) { } if (v_flag) - printf("chmod: %s: changed mode to %d\n", file, mode); + printf("chmod: %s: changed mode to %s\n", file, mu_mode_2_str(mode)); return 0; } diff --git a/src/coreutils/du/du.c b/src/coreutils/du/du.c index 187e006..b6c6fb8 100644 --- a/src/coreutils/du/du.c +++ b/src/coreutils/du/du.c @@ -16,19 +16,21 @@ off_t total; void print(off_t size, const char *filename) { if (h_flag) - printf("%s\t%s\n", mu_humansize(size, 1024), filename); + printf("%s\t%s\n", mu_humansize(size * 512, 1024), filename); else - printf("%jd\t%s\n", (intmax_t)size / 1024, filename); + printf("%jd\t%s\n", (intmax_t)size / 2, filename); } off_t du(const char *path, int recurs_flag) { - off_t sum = 0; - struct stat sb; if (mu_get_lstat("du", path, &sb)) return 0; + off_t sum = sb.st_blocks; + if (c_flag) + total += sum; + if (S_ISDIR(sb.st_mode)) { DIR *dp = opendir(path); if (!dp) { @@ -62,15 +64,8 @@ off_t du(const char *path, int recurs_flag) { print(sum, path); } - /* Get file size */ - else { - sum = sb.st_blocks * 512; - if (c_flag) - total += sum; - - if (!recurs_flag) - print(sum, path); - } + else if (!recurs_flag) + print(sum, path); return sum; } diff --git a/src/coreutils/ls/ls.c b/src/coreutils/ls/ls.c index e7781b6..eb16a72 100644 --- a/src/coreutils/ls/ls.c +++ b/src/coreutils/ls/ls.c @@ -11,6 +11,7 @@ #include #include #include +#include "mode_to_str.h" #include "make_path.h" #include "get_stat.h" #include "config.h" @@ -165,60 +166,8 @@ char *get_date(time_t mtime) { int print(const struct d_node *node) { char suf = ' '; char *color = ""; - char mode[] = "----------"; - - if (node->stats.st_mode & S_IRUSR) - mode[1] = 'r'; - - if (node->stats.st_mode & S_IRGRP) - mode[4] = 'r'; - - if (node->stats.st_mode & S_IROTH) - mode[7] = 'r'; - - if (node->stats.st_mode & S_IWUSR) - mode[2] = 'w'; - - if (node->stats.st_mode & S_IWGRP) - mode[5] = 'w'; - - if (node->stats.st_mode & S_IWOTH) - mode[8] = 'w'; - - if (node->stats.st_mode & S_IXUSR) - mode[3] = 'x'; - - if (node->stats.st_mode & S_IXGRP) - mode[6] = 'x'; - - if (node->stats.st_mode & S_IXOTH) - mode[9] = 'x'; - - if (node->stats.st_mode & S_ISUID) { - if (node->stats.st_mode & S_IXUSR) - mode[3] = 's'; - - else - mode[3] = 'S'; - } - - if (node->stats.st_mode & S_ISGID) { - if (node->stats.st_mode & S_IRGRP) - mode[6] = 's'; - - else - mode[6] = 'S'; - } - - if (node->stats.st_mode & S_ISVTX) { - if (node->stats.st_mode & S_IROTH) - mode[9] = 't'; - - else - mode[9] = 'T'; - } - + char *mode = mu_mode_2_str(node->stats.st_mode); if (S_ISDIR(node->stats.st_mode)) { if (node->name[strlen(node->name) - 1] != '/') suf = '/'; diff --git a/src/init/init/init.c b/src/init/init/init.c index c5cd8cb..0274f8e 100644 --- a/src/init/init/init.c +++ b/src/init/init/init.c @@ -29,7 +29,7 @@ void execute(char *argv[]) { execvp(argv[0], argv); fprintf(stderr, "init: Failed to run %s\nRebooting...\n", argv[0]); - poweroff(SIGKILL); + poweroff(SIGUSR2); } }