Add -s support for du(1)

This commit is contained in:
sin 2013-10-16 17:54:29 +01:00
parent 09fcbfc338
commit 6e8b79ebd8
2 changed files with 20 additions and 5 deletions

3
du.1
View File

@ -16,3 +16,6 @@ directory is displayed.
.TP .TP
.BI \-a .BI \-a
Display an entry for each file in the file hierarchy. Display an entry for each file in the file hierarchy.
.TP
.BI \-s
Display only the grand total for the specified files.

22
du.c
View File

@ -14,8 +14,10 @@
static long blksize = 512; static long blksize = 512;
static bool aflag = false; static bool aflag = false;
static bool sflag = false;
static long du(const char *); static long du(const char *);
static void print(long n, char *path);
void void
usage(void) usage(void)
@ -27,11 +29,15 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *bsize; char *bsize;
long n;
ARGBEGIN { ARGBEGIN {
case 'a': case 'a':
aflag = true; aflag = true;
break; break;
case 's':
sflag = true;
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -41,10 +47,15 @@ main(int argc, char *argv[])
blksize = estrtol(bsize, 0); blksize = estrtol(bsize, 0);
if (argc < 1) { if (argc < 1) {
du("."); n = du(".");
if (sflag)
print(n, realpath(".", NULL));
} else { } else {
for (; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++) {
du(argv[0]); n = du(argv[0]);
if (sflag)
print(n, realpath(argv[0], NULL));
}
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -106,7 +117,7 @@ du(const char *path)
} else { } else {
m = 512 * st.st_blocks / blksize; m = 512 * st.st_blocks / blksize;
n += m; n += m;
if (aflag) if (aflag && !sflag)
print(m, realpath(dent->d_name, NULL)); print(m, realpath(dent->d_name, NULL));
} }
} }
@ -115,6 +126,7 @@ du(const char *path)
} }
} }
print(n, realpath(path, NULL)); if (!sflag)
print(n, realpath(path, NULL));
return n; return n;
} }