[hackers] [sbase] du: add -d flag to specify the max depth to show files or directories || Hiltjo Posthuma
commit 2cf82f4c16c0dde6643e8482ed5d5409845016a7
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sat Oct 18 21:26:35 2014 +0000
du: add -d flag to specify the max depth to show files or directories
we don't allow to use it with -s (like GNU du). busybox allows it.
diff --git a/du.1 b/du.1
index 7f2451d..c1599c5 100644
--- a/du.1
+++ b/du.1
_AT_@ -5,8 +5,9 @@ du \- display disk usage statistics
.B du
.RB [ \-a
.RB |
-.B \-s
-.RB ]
+.BR \-s ]
+.RB [ \-d
+.IR depth ]
.RB [ \-k ]
.RB [ \-h ]
.RI [ file ...]
_AT_@ -25,6 +26,9 @@ Display an entry for each file in the file hierarchy.
.BI \-s
Display only the grand total for the specified files.
.TP
+.BI "\-d depth"
+Maximum directory depth to print files and directories.
+.TP
.BI \-k
By default all sizes are reported in 512-byte block counts.
The -k option causes the numbers to be reported in kilobyte counts.
diff --git a/du.c b/du.c
index ec1a911..207fcee 100644
--- a/du.c
+++ b/du.c
_AT_@ -12,8 +12,11 @@
static long blksize = 512;
static char file[PATH_MAX];
+static long depth = -1;
+static long curdepth = 0;
static bool aflag = false;
+static bool dflag = false;
static bool sflag = false;
static bool kflag = false;
static bool hflag = false;
_AT_@ -24,7 +27,7 @@ static void print(long n, char *path);
static void
usage(void)
{
- eprintf("usage: %s [-a | -s] [-k] [file...]\n", argv0);
+ eprintf("usage: %s [-a | -s] [-d depth] [-h] [-k] [file...]\n", argv0);
}
static char *
_AT_@ -48,6 +51,10 @@ main(int argc, char *argv[])
case 'a':
aflag = true;
break;
+ case 'd':
+ dflag = true;
+ depth = estrtol(EARGF(usage()), 0);
+ break;
case 's':
sflag = true;
break;
_AT_@ -61,7 +68,7 @@ main(int argc, char *argv[])
usage();
} ARGEND;
- if (aflag && sflag)
+ if ((aflag && sflag) || (dflag && sflag))
usage();
bsize = getenv("BLOCKSIZE");
_AT_@ -77,6 +84,7 @@ main(int argc, char *argv[])
print(n, xrealpath(".", file));
} else {
for (; argc > 0; argc--, argv++) {
+ curdepth = 0;
n = du(argv[0]);
if (sflag)
print(n, xrealpath(argv[0], file));
_AT_@ -126,7 +134,7 @@ du(const char *path)
char *cwd;
struct dirent *dent;
struct stat st;
- long n = 0, m;
+ long n = 0, m, t;
int r;
if (lstat(path, &st) < 0)
_AT_@ -150,7 +158,10 @@ du(const char *path)
if (lstat(dent->d_name, &st) < 0)
eprintf("stat: %s:", dent->d_name);
if (S_ISDIR(st.st_mode)) {
+ t = curdepth;
+ curdepth++;
n += du(dent->d_name);
+ curdepth = t;
continue;
}
m = nblks(&st);
_AT_@ -159,19 +170,20 @@ du(const char *path)
if (S_ISLNK(st.st_mode)) {
r = snprintf(file, sizeof(file), "%s/%s",
cwd, dent->d_name);
- if(r >= sizeof(file) || r < 0)
+ if (r >= sizeof(file) || r < 0)
eprintf("path too long\n");
} else {
xrealpath(dent->d_name, file);
}
- print(m, file);
+ if (!dflag || (depth != -1 && curdepth < depth))
+ print(m, file);
}
}
pop(cwd);
closedir(dp);
done:
- if (!sflag)
+ if (!sflag && (!dflag || (depth != -1 && curdepth <= depth)))
print(n, xrealpath(path, file));
return n;
}
Received on Sun Oct 19 2014 - 00:57:06 CEST
This archive was generated by hypermail 2.3.0
: Sun Oct 19 2014 - 01:00:13 CEST