From 78ffcd1cfc13c1287e4946fab9ad7fc9a42c9eaf Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 21 Oct 2023 15:27:42 +0300 Subject: [PATCH] du1/2 --- coreutils/du.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/coreutils/du.c b/coreutils/du.c index f5bc3d2..84db165 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -24,7 +24,7 @@ void print(long size, const char *filename) { printf("%ld\t%s\n", size, filename); } -long du(const char *path) { +long du(const char *path, int recurs_flag) { long sum; struct stat sb; @@ -52,17 +52,27 @@ long du(const char *path) { snprintf(new_path, len, "%s/%s", path, ep->d_name); - sum += du(new_path); + sum += du(new_path, 1); free(new_path); } closedir(dp); - print(sum, path); + + if (s_flag) + if (!recurs_flag) { + print(sum, path); + } + + else + print(sum, path); } - else - sum += sb.st_size; + else { + sum = sb.st_size; + if (!recurs_flag) + print(sum, path); + } return sum; } @@ -98,11 +108,11 @@ int main(const int argc, const char **argv) { } if (argc - i == 0) - du("."); + du(".", 0); else for (; i < argc; i++) - du(argv[i]); + du(argv[i], 0); return 0; }