version 1.25, 2014/05/20 01:25:23 |
version 1.26, 2014/10/17 14:46:54 |
|
|
#include <err.h> |
#include <err.h> |
#include <errno.h> |
#include <errno.h> |
#include <fts.h> |
#include <fts.h> |
|
#include <limits.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
FTSENT *p; |
FTSENT *p; |
long blocksize; |
long blocksize; |
quad_t totalblocks; |
quad_t totalblocks; |
int ftsoptions, listdirs, listfiles; |
int ftsoptions, listfiles, maxdepth; |
int Hflag, Lflag, aflag, cflag, hflag, kflag, sflag; |
int Hflag, Lflag, cflag, hflag, kflag; |
int ch, notused, rval; |
int ch, notused, rval; |
char **save; |
char **save; |
|
const char *errstr; |
|
|
save = argv; |
save = argv; |
Hflag = Lflag = aflag = cflag = hflag = kflag = sflag = 0; |
Hflag = Lflag = cflag = hflag = kflag = listfiles = 0; |
totalblocks = 0; |
totalblocks = 0; |
ftsoptions = FTS_PHYSICAL; |
ftsoptions = FTS_PHYSICAL; |
while ((ch = getopt(argc, argv, "HLPachksxr")) != -1) |
maxdepth = -1; |
|
while ((ch = getopt(argc, argv, "HLPacd:hkrsx")) != -1) |
switch (ch) { |
switch (ch) { |
case 'H': |
case 'H': |
Hflag = 1; |
Hflag = 1; |
|
|
Hflag = Lflag = 0; |
Hflag = Lflag = 0; |
break; |
break; |
case 'a': |
case 'a': |
aflag = 1; |
listfiles = 1; |
break; |
break; |
case 'c': |
case 'c': |
cflag = 1; |
cflag = 1; |
break; |
break; |
|
case 'd': |
|
maxdepth = strtonum(optarg, 0, INT_MAX, &errstr); |
|
if (errstr) { |
|
warnx("max depth %s: %s", optarg, errstr); |
|
usage(); |
|
} |
|
break; |
case 'h': |
case 'h': |
hflag = 1; |
hflag = 1; |
kflag = 0; |
kflag = 0; |
|
|
hflag = 0; |
hflag = 0; |
break; |
break; |
case 's': |
case 's': |
sflag = 1; |
maxdepth = 0; |
break; |
break; |
case 'r': |
case 'r': |
break; |
break; |
|
|
ftsoptions |= FTS_LOGICAL; |
ftsoptions |= FTS_LOGICAL; |
} |
} |
|
|
if (aflag) { |
if (maxdepth == -1) |
if (sflag) |
maxdepth = INT_MAX; |
usage(); |
|
listdirs = listfiles = 1; |
|
} else if (sflag) |
|
listdirs = listfiles = 0; |
|
else { |
|
listfiles = 0; |
|
listdirs = 1; |
|
} |
|
|
|
if (!*argv) { |
if (!*argv) { |
argv = save; |
argv = save; |
|
|
* or directories and this is post-order of the |
* or directories and this is post-order of the |
* root of a traversal, display the total. |
* root of a traversal, display the total. |
*/ |
*/ |
if (listdirs || |
if (p->fts_level <= maxdepth) |
(!listfiles && p->fts_level == FTS_ROOTLEVEL)) { |
|
prtout((quad_t)howmany(p->fts_number, |
prtout((quad_t)howmany(p->fts_number, |
(unsigned long)blocksize), p->fts_path, |
(unsigned long)blocksize), p->fts_path, |
hflag); |
hflag); |
} |
|
break; |
break; |
case FTS_DC: /* Ignore. */ |
case FTS_DC: /* Ignore. */ |
break; |
break; |
|
|
* If listing each file, or a non-directory file was |
* If listing each file, or a non-directory file was |
* the root of a traversal, display the total. |
* the root of a traversal, display the total. |
*/ |
*/ |
if (listfiles || p->fts_level == FTS_ROOTLEVEL) |
if (listfiles && p->fts_level <= maxdepth) |
prtout(howmany(p->fts_statp->st_blocks, |
prtout(howmany(p->fts_statp->st_blocks, |
blocksize), p->fts_path, hflag); |
blocksize), p->fts_path, hflag); |
p->fts_parent->fts_number += p->fts_statp->st_blocks; |
p->fts_parent->fts_number += p->fts_statp->st_blocks; |
|
|
{ |
{ |
|
|
(void)fprintf(stderr, |
(void)fprintf(stderr, |
"usage: du [-a | -s] [-chkrx] [-H | -L | -P] [file ...]\n"); |
"usage: du [-d depth] [-achkrsx] [-H | -L | -P] [file ...]\n"); |
exit(1); |
exit(1); |
} |
} |