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.
This commit is contained in:
parent
b6b8fe9591
commit
2cf82f4c16
8
du.1
8
du.1
|
@ -5,8 +5,9 @@ du \- display disk usage statistics
|
||||||
.B du
|
.B du
|
||||||
.RB [ \-a
|
.RB [ \-a
|
||||||
.RB |
|
.RB |
|
||||||
.B \-s
|
.BR \-s ]
|
||||||
.RB ]
|
.RB [ \-d
|
||||||
|
.IR depth ]
|
||||||
.RB [ \-k ]
|
.RB [ \-k ]
|
||||||
.RB [ \-h ]
|
.RB [ \-h ]
|
||||||
.RI [ file ...]
|
.RI [ file ...]
|
||||||
|
@ -25,6 +26,9 @@ Display an entry for each file in the file hierarchy.
|
||||||
.BI \-s
|
.BI \-s
|
||||||
Display only the grand total for the specified files.
|
Display only the grand total for the specified files.
|
||||||
.TP
|
.TP
|
||||||
|
.BI "\-d depth"
|
||||||
|
Maximum directory depth to print files and directories.
|
||||||
|
.TP
|
||||||
.BI \-k
|
.BI \-k
|
||||||
By default all sizes are reported in 512-byte block counts.
|
By default all sizes are reported in 512-byte block counts.
|
||||||
The -k option causes the numbers to be reported in kilobyte counts.
|
The -k option causes the numbers to be reported in kilobyte counts.
|
||||||
|
|
22
du.c
22
du.c
|
@ -12,8 +12,11 @@
|
||||||
|
|
||||||
static long blksize = 512;
|
static long blksize = 512;
|
||||||
static char file[PATH_MAX];
|
static char file[PATH_MAX];
|
||||||
|
static long depth = -1;
|
||||||
|
static long curdepth = 0;
|
||||||
|
|
||||||
static bool aflag = false;
|
static bool aflag = false;
|
||||||
|
static bool dflag = false;
|
||||||
static bool sflag = false;
|
static bool sflag = false;
|
||||||
static bool kflag = false;
|
static bool kflag = false;
|
||||||
static bool hflag = false;
|
static bool hflag = false;
|
||||||
|
@ -24,7 +27,7 @@ static void print(long n, char *path);
|
||||||
static void
|
static void
|
||||||
usage(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 *
|
static char *
|
||||||
|
@ -48,6 +51,10 @@ main(int argc, char *argv[])
|
||||||
case 'a':
|
case 'a':
|
||||||
aflag = true;
|
aflag = true;
|
||||||
break;
|
break;
|
||||||
|
case 'd':
|
||||||
|
dflag = true;
|
||||||
|
depth = estrtol(EARGF(usage()), 0);
|
||||||
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
sflag = true;
|
sflag = true;
|
||||||
break;
|
break;
|
||||||
|
@ -61,7 +68,7 @@ main(int argc, char *argv[])
|
||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
if (aflag && sflag)
|
if ((aflag && sflag) || (dflag && sflag))
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
bsize = getenv("BLOCKSIZE");
|
bsize = getenv("BLOCKSIZE");
|
||||||
|
@ -77,6 +84,7 @@ main(int argc, char *argv[])
|
||||||
print(n, xrealpath(".", file));
|
print(n, xrealpath(".", file));
|
||||||
} else {
|
} else {
|
||||||
for (; argc > 0; argc--, argv++) {
|
for (; argc > 0; argc--, argv++) {
|
||||||
|
curdepth = 0;
|
||||||
n = du(argv[0]);
|
n = du(argv[0]);
|
||||||
if (sflag)
|
if (sflag)
|
||||||
print(n, xrealpath(argv[0], file));
|
print(n, xrealpath(argv[0], file));
|
||||||
|
@ -126,7 +134,7 @@ du(const char *path)
|
||||||
char *cwd;
|
char *cwd;
|
||||||
struct dirent *dent;
|
struct dirent *dent;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
long n = 0, m;
|
long n = 0, m, t;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
if (lstat(path, &st) < 0)
|
if (lstat(path, &st) < 0)
|
||||||
|
@ -150,7 +158,10 @@ du(const char *path)
|
||||||
if (lstat(dent->d_name, &st) < 0)
|
if (lstat(dent->d_name, &st) < 0)
|
||||||
eprintf("stat: %s:", dent->d_name);
|
eprintf("stat: %s:", dent->d_name);
|
||||||
if (S_ISDIR(st.st_mode)) {
|
if (S_ISDIR(st.st_mode)) {
|
||||||
|
t = curdepth;
|
||||||
|
curdepth++;
|
||||||
n += du(dent->d_name);
|
n += du(dent->d_name);
|
||||||
|
curdepth = t;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
m = nblks(&st);
|
m = nblks(&st);
|
||||||
|
@ -159,11 +170,12 @@ du(const char *path)
|
||||||
if (S_ISLNK(st.st_mode)) {
|
if (S_ISLNK(st.st_mode)) {
|
||||||
r = snprintf(file, sizeof(file), "%s/%s",
|
r = snprintf(file, sizeof(file), "%s/%s",
|
||||||
cwd, dent->d_name);
|
cwd, dent->d_name);
|
||||||
if(r >= sizeof(file) || r < 0)
|
if (r >= sizeof(file) || r < 0)
|
||||||
eprintf("path too long\n");
|
eprintf("path too long\n");
|
||||||
} else {
|
} else {
|
||||||
xrealpath(dent->d_name, file);
|
xrealpath(dent->d_name, file);
|
||||||
}
|
}
|
||||||
|
if (!dflag || (depth != -1 && curdepth < depth))
|
||||||
print(m, file);
|
print(m, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,7 +183,7 @@ du(const char *path)
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
|
|
||||||
done:
|
done:
|
||||||
if (!sflag)
|
if (!sflag && (!dflag || (depth != -1 && curdepth <= depth)))
|
||||||
print(n, xrealpath(path, file));
|
print(n, xrealpath(path, file));
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user