[hackers] [sbase] No need for realpath() to call malloc() in du(1) || sin

From: <git_AT_suckless.org>
Date: Thu, 17 Oct 2013 15:15:12 +0200

commit 9e321b69d2a4f2af1e067c1f58a0c8237e47a248
Author: sin <sin_AT_2f30.org>
Date: Thu Oct 17 14:06:17 2013 +0100

    No need for realpath() to call malloc() in du(1)
    
    Print links correctly as well.

diff --git a/du.c b/du.c
index edb2e50..b83aba9 100644
--- a/du.c
+++ b/du.c
_AT_@ -12,6 +12,7 @@
 #include "util.h"
 
 static long blksize = 512;
+static char file[PATH_MAX];
 
 static bool aflag = false;
 static bool sflag = false;
_AT_@ -59,12 +60,12 @@ main(int argc, char *argv[])
         if (argc < 1) {
                 n = du(".");
                 if (sflag)
- print(n, realpath(".", NULL));
+ print(n, realpath(".", file));
         } else {
                 for (; argc > 0; argc--, argv++) {
                         n = du(argv[0]);
                         if (sflag)
- print(n, realpath(argv[0], NULL));
+ print(n, realpath(argv[0], file));
                 }
         }
         return EXIT_SUCCESS;
_AT_@ -74,7 +75,6 @@ static void
 print(long n, char *path)
 {
         printf("%lu %s
", n, path);
- free(path);
 }
 
 static char *
_AT_@ -127,8 +127,13 @@ du(const char *path)
                                 } else {
                                         m = 512 * st.st_blocks / blksize;
                                         n += m;
- if (aflag && !sflag)
- print(m, realpath(dent->d_name, NULL));
+ if (aflag && !sflag) {
+ if (S_ISLNK(st.st_mode))
+ snprintf(file, sizeof(file), "%s/%s", cwd, dent->d_name);
+ else
+ realpath(dent->d_name, file);
+ print(m, file);
+ }
                                 }
                         }
                         pop(cwd);
_AT_@ -137,6 +142,6 @@ du(const char *path)
         }
 
         if (!sflag)
- print(n, realpath(path, NULL));
+ print(n, realpath(path, file));
         return n;
 }
Received on Thu Oct 17 2013 - 15:15:12 CEST

This archive was generated by hypermail 2.3.0 : Thu Oct 17 2013 - 15:24:18 CEST