From 62eafb82caf3c855ffebf3543e62dbd2d7590664 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 21 Oct 2023 15:05:49 +0300 Subject: [PATCH] du1/2 --- coreutils/cat.c | 2 +- coreutils/cp.c | 10 +-- coreutils/du.c | 108 ++++++++++++++++++++++++++++ coreutils/du.c.save | 56 +++++++++++++++ coreutils/ln.c | 2 +- coreutils/ls.c | 4 +- coreutils/mirco-coreutils-version.c | 8 +++ coreutils/mkdir.c | 2 +- coreutils/printenv.c | 2 +- coreutils/reset.c | 5 ++ coreutils/rm.c | 6 +- coreutils/shred.c | 2 +- coreutils/uname.c | 2 +- coreutils/wc.c | 2 +- 14 files changed, 194 insertions(+), 17 deletions(-) create mode 100644 coreutils/du.c create mode 100644 coreutils/du.c.save create mode 100644 coreutils/mirco-coreutils-version.c create mode 100644 coreutils/reset.c diff --git a/coreutils/cat.c b/coreutils/cat.c index a0517dd..ac64efe 100644 --- a/coreutils/cat.c +++ b/coreutils/cat.c @@ -29,7 +29,7 @@ int main(const int argc, const char **argv) { else if (!strcmp(argv[i], "-n")) flag = 1; - else if (!strcmp(argv[i], "-h")) { + else if (!strcmp(argv[i], "--help")) { printf("cat [-n (numerate)] [file1 file2 ...]\n"); return 0; } diff --git a/coreutils/cp.c b/coreutils/cp.c index b4daa95..4004858 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c @@ -113,11 +113,13 @@ int cptree(const char *src, const char *dst) { char *src_path = make_path(src, ep->d_name); if (src_path == NULL) - return 1; + continue; char *dst_path = make_path(dst, ep->d_name); - if (dst_path == NULL) - return 1; + if (dst_path == NULL) { + free(src_path); + continue; + } cptree(src_path, dst_path); @@ -136,7 +138,7 @@ int main(const int argc, char **argv) { if (argv[i][0] != '-') break; - else if (!strcmp(argv[i], "-h")) { + else if (!strcmp(argv[i], "--help")) { printf("cp [Src] [Dst]\n"); return 0; } diff --git a/coreutils/du.c b/coreutils/du.c new file mode 100644 index 0000000..f5bc3d2 --- /dev/null +++ b/coreutils/du.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include + +unsigned int h_flag; +unsigned int s_flag; +unsigned int b_flag; +unsigned int m_flag; +unsigned int L_flag; + +void print(long size, const char *filename) { + /* Kb */ + if (!b_flag) + size = size / 1024; + + /* Mb */ + else if (m_flag) + size = size / 1048576; + + printf("%ld\t%s\n", size, filename); +} + +long du(const char *path) { + long sum; + + struct stat sb; + if (lstat(path, &sb) != 0) { + fprintf(stderr, "du: lstat() %s\n", strerror(errno)); + return 0; + } + + if (S_ISDIR(sb.st_mode)) { + DIR *dp = opendir(path); + if (!dp) { + fprintf(stderr, "du: %s\n", strerror(errno)); + return 0; + } + + struct dirent *ep; + while ((ep = readdir(dp)) != NULL) { + 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); + if (new_path == NULL) + continue; + + snprintf(new_path, len, "%s/%s", path, ep->d_name); + + sum += du(new_path); + + free(new_path); + } + + closedir(dp); + print(sum, path); + } + + else + sum += sb.st_size; + + return sum; +} + +int main(const int argc, const char **argv) { + + int i; + for (i = 1; i < argc; i++) { + if (argv[i][0] != '-') + break; + + else if (!strcmp(argv[i], "-h")) + h_flag = 1; + + else if (!strcmp(argv[i], "-s")) + s_flag = 1; + + else if (!strcmp(argv[i], "-b")) + b_flag = 1; + + else if (!strcmp(argv[i], "-m")) { + b_flag = 1; + m_flag = 1; + } + + else if (!strcmp(argv[i], "-L")) + L_flag = 1; + + else if (!strcmp(argv[i], "--help")) { + printf("du [-h (Sizes in human readable format (e.g., 1K 243M 2G))] [-s (Display only a total for each argument)] [-b (Apparent size)] [-L (Follow all symlinks)] [-m (Size in megabyte)] [file file2...]\n"); + return 0; + } + } + + if (argc - i == 0) + du("."); + + else + for (; i < argc; i++) + du(argv[i]); + + return 0; +} diff --git a/coreutils/du.c.save b/coreutils/du.c.save new file mode 100644 index 0000000..af68203 --- /dev/null +++ b/coreutils/du.c.save @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include + +unsigned int h_flag; +unsigned int s_flag; +unsigned int b_flag; +unsigned int m_flag; +unsigned int L_flag; + +int du(const char *path) { + struct stat sb; + if (lstat(path, &sb)) { + fprintf(stderr, "du: lstat() %s\n", strerror(errno)); + return 0; + } + + + return 0; +} + +int main(const int argc, const char **argv) { + + int i; + for (i = 1; i < argc; i++) { + if (argv[i][0] != '-') + break; + + else if (!strcmp(argv[i], "-h")) + h_flag = 1; + + else if (!strcmp(argv[i], "-s")) + s_flag = 1; + + else if (!strcmp(argv[i], "-b")) + b_flag = 1; + + else if (!strcmp(argv[i], "-m")) + m_flag = 1; + + else if (!strcmp(argv[i], "-L")) + L_flag = 1; + + else if (!strcmp(argv[i], "--help")) { + printf("du [-h (Sizes in human readable format (e.g., 1K 243M 2G))] [-s (Display only a total for each argument)] [-b (Apparent size)] [-L (Follow all symlinks)] [-m (Size in megabyte)] [file file2...]\n"); + return 0; + } + } + + for (; i < argc; i++) + du(argv[i]); + + return 0; +} diff --git a/coreutils/ln.c b/coreutils/ln.c index b7131b9..ee50212 100644 --- a/coreutils/ln.c +++ b/coreutils/ln.c @@ -24,7 +24,7 @@ int main(const int argc, const char **argv) { else if (!strcmp("-s", argv[i])) s_flag = 1; - else if (!strcmp("-h", argv[i])) { + else if (!strcmp("--help", argv[i])) { printf("ln [-s] [TARGET] [LINK/DIR]\n"); return 0; } diff --git a/coreutils/ls.c b/coreutils/ls.c index b04a312..9269a63 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -100,8 +100,8 @@ int main(const int argc, const char **argv) { else if (!strcmp(argv[i], "-l")) l_flag = 1; - else if (!strcmp(argv[i], "-h")) { - printf("ls [-a (Show hidden files)] [-l (use a long listing format (month / day / hour:min))] [Path]\n"); + else if (!strcmp(argv[i], "--help")) { + printf("ls [-a (show hidden files)] [-l (use a long listing format)] [Path]\n"); return 0; } } diff --git a/coreutils/mirco-coreutils-version.c b/coreutils/mirco-coreutils-version.c new file mode 100644 index 0000000..4adb9d4 --- /dev/null +++ b/coreutils/mirco-coreutils-version.c @@ -0,0 +1,8 @@ +#include + +#define MSG "Micro-coreutils: Version 0.1 LICENSE: wtfpl\n" + +int main(void) { + puts(MSG); + return 0; +} diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c index 7390a5b..2615d00 100644 --- a/coreutils/mkdir.c +++ b/coreutils/mkdir.c @@ -43,7 +43,7 @@ int main(const int argc, const char **argv) { else if (!strcmp(argv[i], "-p")) flag = 1; - else if (!strcmp(argv[i], "-h")) { + else if (!strcmp(argv[i], "--help")) { printf("mkdir [-p (make parent dir)] [dir1 dir2...]\n"); return 0; } diff --git a/coreutils/printenv.c b/coreutils/printenv.c index 081936d..63b20a2 100644 --- a/coreutils/printenv.c +++ b/coreutils/printenv.c @@ -28,7 +28,7 @@ int main(const int argc, const char **argv, const char **envp) { else if (!strcmp(argv[i], "-0")) nullline = 1; - else if (!strcmp(argv[i], "-h")) { + else if (!strcmp(argv[i], "--help")) { printf("printenv [-n (end each output line with NUL, not newline)]\n"); return 0; } diff --git a/coreutils/reset.c b/coreutils/reset.c new file mode 100644 index 0000000..012ae59 --- /dev/null +++ b/coreutils/reset.c @@ -0,0 +1,5 @@ +#include + +int main(void) { + return write(1, "\033c\033(K\033[J\033[0m\033[?25h", 18) != 18; +} diff --git a/coreutils/rm.c b/coreutils/rm.c index fba5a4e..35da044 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c @@ -42,10 +42,8 @@ int rmtree(const char *path) { size_t len = strlen(path) + strlen(ep->d_name) + 2; char *full_path = malloc(len + 1); - if (full_path == NULL) { - fprintf(stderr, "rm: malloc() returned NULL\n"); - return 1; - } + if (full_path == NULL) + continue; snprintf(full_path, len, "%s/%s", path, ep->d_name); diff --git a/coreutils/shred.c b/coreutils/shred.c index 421f8c0..70abe7a 100644 --- a/coreutils/shred.c +++ b/coreutils/shred.c @@ -68,7 +68,7 @@ int main(const int argc, const char **argv) { n_loops = atoi(val + 1); } - else if (!strcmp(argv[i], "-h")) { + else if (!strcmp(argv[i], "--help")) { printf("shred [-n=N (Overwrite N times (default 3))] [-z (Final overwrite with zeros)] [-u (Remove file)] [-f (Chmod to ensure writability)] [FILE...]\n"); return 1; } diff --git a/coreutils/uname.c b/coreutils/uname.c index dcc8d1b..44fd2e3 100644 --- a/coreutils/uname.c +++ b/coreutils/uname.c @@ -35,7 +35,7 @@ int main(const int argc, const char **argv) { else if (!strcmp("-m", argv[i])) flags[O_MACHINE] = 1; - else if (!strcmp("-h", argv[i])) { + else if (!strcmp("--help", argv[i])) { printf("uname [-a (All)] [-s (Sys)] [-n (Nodename)] [-r (Release)] [-v (Version)] [-m (Machine)]\n"); return 0; } diff --git a/coreutils/wc.c b/coreutils/wc.c index d129222..83b82a4 100644 --- a/coreutils/wc.c +++ b/coreutils/wc.c @@ -75,7 +75,7 @@ int main(const int argc, const char **argv) { else if (!strcmp(argv[i], "-w")) w_flag = 1; - else if (!strcmp(argv[i], "-h")) { + else if (!strcmp(argv[i], "--help")) { printf("wc [-l (lines)] [-c (bytes)] [-w (words)] [file1 file2...]\n"); return 0; }