[hackers] [sbase] du: Don't print 0 entry if stat of named file fails || Michael Forney

From: <git_AT_suckless.org>
Date: Mon, 3 Jul 2017 21:06:19 +0200 (CEST)

commit 830ca05c25984586f3578f11a7a8038304d71719
Author: Michael Forney <mforney_AT_mforney.org>
AuthorDate: Wed Dec 14 19:40:03 2016 -0800
Commit: Anselm R Garbe <anselm_AT_garbe.us>
CommitDate: Mon Jul 3 21:03:04 2017 +0200

    du: Don't print 0 entry if stat of named file fails
    
    Previously:
    
            $ du doesntexist
            du: lstat doesntexist: No such file or directory
            0 doesntexist
    
    Now:
    
            $ du doesntexist
            du: lstat doesntexist: No such file or directory
    
    Also, just call nblks one time.

diff --git a/du.c b/du.c
index fb2c90b..4ceac66 100644
--- a/du.c
+++ b/du.c
_AT_@ -35,16 +35,19 @@ nblks(blkcnt_t blocks)
 }
 
 static void
-du(const char *path, struct stat *st, void *total, struct recursor *r)
+du(const char *path, struct stat *st, void *data, struct recursor *r)
 {
- off_t subtotal = 0;
+ off_t *total = data, subtotal;
 
+ subtotal = nblks(st->st_blocks);
         if (S_ISDIR(st->st_mode))
                 recurse(path, &subtotal, r);
- *((off_t *)total) += subtotal + nblks(st->st_blocks);
+ *total += subtotal;
 
- if (!sflag && r->depth <= maxdepth && r->depth && (S_ISDIR(st->st_mode) || aflag))
- printpath(subtotal + nblks(st->st_blocks), path);
+ if (!r->depth)
+ printpath(*total, path);
+ else if (!sflag && r->depth <= maxdepth && (S_ISDIR(st->st_mode) || aflag))
+ printpath(subtotal, path);
 }
 
 static void
_AT_@ -102,12 +105,10 @@ main(int argc, char *argv[])
 
         if (!argc) {
                 recurse(".", &n, &r);
- printpath(n, ".");
         } else {
                 for (; *argv; argc--, argv++) {
                         n = 0;
                         recurse(*argv, &n, &r);
- printpath(n, *argv);
                 }
         }
 
Received on Mon Jul 03 2017 - 21:06:19 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 03 2017 - 21:12:39 CEST