From 6d1cf79ca62e18c959738d40dafc6a419e670c36 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 31 Oct 2023 12:53:32 +0300 Subject: [PATCH] fix --- Makefile | 4 ++-- coreutils/chown.c | 32 ++++++++++++-------------------- coreutils/cp.c | 34 +++++++--------------------------- coreutils/du.c | 16 ++++++---------- coreutils/ls.c | 34 ++++++---------------------------- coreutils/mv.c | 12 +++++------- coreutils/rm.c | 29 ++++------------------------- libmu/get_stat.h | 27 +++++++++++++++++++++++++++ libmu/make_path.h | 20 ++++++++++++++++++++ shell/sh.c | 3 +++ 10 files changed, 92 insertions(+), 119 deletions(-) create mode 100644 libmu/get_stat.h create mode 100644 libmu/make_path.h diff --git a/Makefile b/Makefile index e5514a2..4076df1 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -objects = coreutils console-tools networking procps +objects = coreutils console-tools networking procps shell CFLAGS?=-s -Os -flto -pedantic -Wall -Wextra CC?=cc @@ -7,7 +7,7 @@ all: $(objects) $(objects): echo MAKE $@ - cd $@ && $(MAKE) -B CC="$(CC)" CFLAGS="$(CFLAGS)" && cd .. + cd $@ && $(MAKE) -B CC="$(CC)" CFLAGS="$(CFLAGS) -I../libmu" && cd .. clean: rm bin/* diff --git a/coreutils/chown.c b/coreutils/chown.c index e7ab089..a3d48df 100644 --- a/coreutils/chown.c +++ b/coreutils/chown.c @@ -6,35 +6,28 @@ #include #include #include -#include +#include "make_path.h" +#include "get_stat.h" + unsigned int r_flag; +unsigned int stat_flag; int (*chown_func)(const char *pathname, uid_t owner, gid_t group); -int (*stat_func)(const char *restrict pathname, struct stat *restrict statbuf); long gid; long uid; int get_stat(const char *path, struct stat *stat_path) { - if (stat_func(path, stat_path)) { - fprintf(stderr, "chown: unable to stat %s: %s\n", path, strerror(errno)); - return 1; + if (stat_flag) { + if (mu_get_stat("chown", path, stat_path)) + return 1; } + else if (mu_get_lstat("chown", path, stat_path)) + return 1; + return 0; } -char *make_path(const char *src, const char *dst) { - size_t len = strlen(src) + strlen(dst) + 2; - char *full_path = malloc(len + 1); - if (full_path == NULL) { - fprintf(stderr, "chown: malloc() returned NULL\n"); - return NULL; - } - - snprintf(full_path, len, "%s/%s", src, dst); - return full_path; -} - int change(const char *file) { struct stat old_file; if (get_stat(file, &old_file)) @@ -79,7 +72,7 @@ int cntree(const char *dst) { if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..")) continue; - char *full_path = make_path(dst, ep->d_name); + char *full_path = mu_make_path("chown", dst, ep->d_name); if (full_path == NULL) continue; @@ -130,7 +123,6 @@ void get_owner(const char *arg) { int main(const int argc, char **argv) { chown_func = lchown; - stat_func = stat; int i; for (i = 1; i < argc; i++) { @@ -142,7 +134,7 @@ int main(const int argc, char **argv) { else if (!strcmp(argv[i], "-H")) { chown_func = chown; - stat_func = stat; + stat_flag = 1; } else if (!strcmp(argv[i], "--help")) { diff --git a/coreutils/cp.c b/coreutils/cp.c index ad964dc..95b7366 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c @@ -7,19 +7,8 @@ #include #include #include -#include - -char *make_path(const char *src, const char *dst) { - size_t len = strlen(src) + strlen(dst) + 2; - char *full_path = malloc(len + 1); - if (full_path == NULL) { - fprintf(stderr, "cp: malloc() returned NULL\n"); - return NULL; - } - - snprintf(full_path, len, "%s/%s", src, dst); - return full_path; -} +#include "make_path.h" +#include "get_stat.h" int write_buffer(int mode, int ifd, int ofd, const char *dst) { off_t len = lseek(ifd, 0, SEEK_END); @@ -77,7 +66,7 @@ int copy(int mode, const char *src, const char *dst) { flag = 1; new_path = NULL; - new_path = make_path(dst, basename(dup)); + new_path = mu_make_path("cp", dst, basename(dup)); if (new_path == NULL) goto CLOSE; @@ -110,18 +99,9 @@ CLOSE: return ret; } -int get_stat(const char *path, struct stat *stat_path) { - if (stat(path, stat_path)) { - fprintf(stderr, "cp: unable to stat %s: %s\n", path, strerror(errno)); - return 1; - } - - return 0; -} - int cptree(const char *src, const char *dst) { struct stat stat_path; - if (get_stat(src, &stat_path)) + if (mu_get_lstat("cp", src, &stat_path)) return 1; if (!S_ISDIR(stat_path.st_mode)) { @@ -147,11 +127,11 @@ int cptree(const char *src, const char *dst) { if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..") || !strcmp(dst, ep->d_name)) continue; - char *src_path = make_path(src, ep->d_name); + char *src_path = mu_make_path("cp", src, ep->d_name); if (src_path == NULL) continue; - char *dst_path = make_path(dst, ep->d_name); + char *dst_path = mu_make_path("cp", dst, ep->d_name); if (dst_path == NULL) { free(src_path); continue; @@ -186,7 +166,7 @@ int main(const int argc, char **argv) { else { for (; i < argc - 1; i++) { - char *new_path = make_path(argv[argc - 1], basename(argv[i])); + char *new_path = mu_make_path("cp", argv[argc - 1], basename(argv[i])); if (new_path == NULL) return 1; diff --git a/coreutils/du.c b/coreutils/du.c index 707fd42..8eda331 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -4,7 +4,8 @@ #include #include #include -#include +#include "get_stat.h" +#include "make_path.h" unsigned int h_flag; unsigned int s_flag; @@ -44,10 +45,8 @@ double du(const char *path, int recurs_flag) { double sum = 0; struct stat sb; - if (lstat(path, &sb) != 0) { - fprintf(stderr, "du: lstat() %s\n", strerror(errno)); - return 0; - } + if (mu_get_lstat("du", path, &sb)) + return 1; if (S_ISDIR(sb.st_mode)) { DIR *dp = opendir(path); @@ -61,12 +60,9 @@ double du(const char *path, int recurs_flag) { if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..")) continue; - size_t len = strlen(path) + strlen(ep->d_name) + 2; - char *new_path = malloc(len + 1); + char *new_path = mu_make_path("du", path, ep->d_name); if (new_path == NULL) - continue; - - snprintf(new_path, len, "%s/%s", path, ep->d_name); + return 1; sum += du(new_path, 1); diff --git a/coreutils/ls.c b/coreutils/ls.c index 6dd973b..2dd254c 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -8,33 +8,13 @@ #include #include #include -#include #include +#include "make_path.h" +#include "get_stat.h" unsigned int a_flag; unsigned int l_flag; -int get_stat(const char *path, struct stat *stat_path) { - if (stat(path, stat_path)) { - fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno)); - return 1; - } - - return 0; -} - -char *make_path(const char *src, const char *dst) { - size_t len = strlen(src) + strlen(dst) + 2; - char *full_path = malloc(len + 1); - if (full_path == NULL) { - fprintf(stderr, "cp: malloc() returned NULL\n"); - return NULL; - } - - snprintf(full_path, len, "%s/%s", src, dst); - return full_path; -} - void PrintPerm(struct stat sb) { printf("%c", (S_ISDIR(sb.st_mode)) ? 'd' : '-'); printf("%c%c%c", (sb.st_mode & S_IRUSR) ? 'r' : '-', (sb.st_mode & S_IWUSR) ? 'w' : '-', (sb.st_mode & S_IXUSR) ? 'x' : '-'); @@ -44,7 +24,7 @@ void PrintPerm(struct stat sb) { char *fileflag(const char *path) { struct stat sb; - if (get_stat(path, &sb)) + if (mu_get_lstat("ls", path, &sb)) return " "; if (S_ISDIR(sb.st_mode)) @@ -79,10 +59,8 @@ void PrintInfo(struct stat sb, const char *filename) { int list(const char *path, int label) { struct stat sb; - if (lstat(path, &sb)) { - fprintf(stderr, "ls: unable to lstat %s: %s\n", path, strerror(errno)); + if (mu_get_lstat("ls", path, &sb)) return 1; - } /* If its file */ if (!S_ISDIR(sb.st_mode)) { @@ -111,12 +89,12 @@ int list(const char *path, int label) { if (ep->d_name[0] == '.' && !a_flag) continue; - char *full_path = make_path(path, ep->d_name); + char *full_path = mu_make_path("ls", path, ep->d_name); if (full_path == NULL) return 1; if (l_flag) { - if (get_stat(full_path, &sb)) + if (mu_get_lstat("ls", full_path, &sb)) return 1; PrintInfo(sb, full_path); diff --git a/coreutils/mv.c b/coreutils/mv.c index 86ef165..4c592bd 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c @@ -5,20 +5,18 @@ #include #include #include +#include "make_path.h" int move(const char *src, const char *dst) { char *copy = strdup(src); if (!copy) return 1; - char *bname = basename(copy); - - size_t len = strlen(dst) + strlen(bname) + 2; - char *new_path = malloc(len + 1); - if (new_path == NULL) + char *new_path = mu_make_path("mv", dst, basename(copy)); + if (new_path == NULL) { + free(copy); return 1; - - snprintf(new_path, len, "%s/%s", dst, bname); + } int ret = 0; if (rename(src, new_path) < 0) diff --git a/coreutils/rm.c b/coreutils/rm.c index 7527368..c673f3e 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c @@ -3,35 +3,14 @@ #include #include #include -#include #include +#include "make_path.h" +#include "get_stat.h" unsigned int f_flag; -int get_stat(const char *path, struct stat *stat_path) { - if (lstat(path, stat_path)) { - if (!f_flag) - fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno)); - return 1; - } - - return 0; -} - -char *make_path(const char *src, const char *dst) { - size_t len = strlen(src) + strlen(dst) + 2; - char *full_path = malloc(len + 1); - if (full_path == NULL) { - fprintf(stderr, "rm: malloc() returned NULL\n"); - return NULL; - } - - snprintf(full_path, len, "%s/%s", src, dst); - return full_path; -} - int rmtree(const char *path) { struct stat stat_path; - if (get_stat(path, &stat_path)) + if (mu_get_lstat("rm", path, &stat_path)) return 1; if (!S_ISDIR(stat_path.st_mode) || S_ISLNK(stat_path.st_mode)) { @@ -56,7 +35,7 @@ int rmtree(const char *path) { if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..")) continue; - char *full_path = make_path(path, ep->d_name); + char *full_path = mu_make_path("rm", path, ep->d_name); if (full_path == NULL) continue; diff --git a/libmu/get_stat.h b/libmu/get_stat.h new file mode 100644 index 0000000..5972432 --- /dev/null +++ b/libmu/get_stat.h @@ -0,0 +1,27 @@ +#ifndef _GET_STAT_H +#define _GET_STAT_H + +#include +#include +#include +#include + +int mu_get_stat(const char *prog_name, const char *path, struct stat *stat_path) { + if (stat(path, stat_path)) { + fprintf(stderr, "%s: unable to stat %s: %s\n", prog_name, path, strerror(errno)); + return 1; + } + + return 0; +} + +int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path) { + if (lstat(path, stat_path)) { + fprintf(stderr, "%s: unable to stat %s: %s\n", prog_name, path, strerror(errno)); + return 1; + } + + return 0; +} + +#endif diff --git a/libmu/make_path.h b/libmu/make_path.h new file mode 100644 index 0000000..aba1eef --- /dev/null +++ b/libmu/make_path.h @@ -0,0 +1,20 @@ +#ifndef _MAKE_PATH_H +#define _MAKE_PATH_H + +#include +#include +#include + +char *mu_make_path(const char *prog_name, const char *src, const char *dst) { + size_t len = strlen(src) + strlen(dst) + 2; + char *full_path = malloc(len + 1); + if (full_path == NULL) { + fprintf(stderr, "%s: malloc() fail\n", prog_name); + return NULL; + } + + snprintf(full_path, len, "%s/%s", src, dst); + return full_path; +} + +#endif diff --git a/shell/sh.c b/shell/sh.c index 061ed7e..1dde422 100644 --- a/shell/sh.c +++ b/shell/sh.c @@ -1,3 +1,6 @@ +#include +#include + int main(void) { return 0; }