du.c: add symlink dereferencing flags -H and -L

This commit is contained in:
Tai Chi Minh Ralph Eastwood 2015-02-09 22:18:49 +00:00 committed by sin
parent bd89474b8a
commit 1d2d28a8e4
1 changed files with 17 additions and 6 deletions

23
du.c
View File

@ -21,6 +21,7 @@ static int dflag = 0;
static int sflag = 0; static int sflag = 0;
static int kflag = 0; static int kflag = 0;
static int hflag = 0; static int hflag = 0;
static char HLflag = 'P';
static char * static char *
xrealpath(const char *pathname, char *resolved) xrealpath(const char *pathname, char *resolved)
@ -68,7 +69,7 @@ nblks(struct stat *st)
} }
static size_t static size_t
du(const char *path) du(const char *path, char follow)
{ {
struct dirent *dent; struct dirent *dent;
struct stat st; struct stat st;
@ -81,9 +82,13 @@ du(const char *path)
eprintf("stat: %s:", path); eprintf("stat: %s:", path);
n = nblks(&st); n = nblks(&st);
if (!S_ISDIR(st.st_mode)) if (!(S_ISDIR(st.st_mode) ||
(follow != 'P' && S_ISLNK(st.st_mode) &&
stat(path, &st) == 0 && S_ISDIR(st.st_mode))))
goto done; goto done;
follow = follow == 'H' ? 'P' : follow;
dp = opendir(path); dp = opendir(path);
if (!dp) { if (!dp) {
weprintf("opendir %s:", path); weprintf("opendir %s:", path);
@ -97,10 +102,12 @@ du(const char *path)
continue; continue;
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) ||
(follow != 'P' && S_ISLNK(st.st_mode) &&
stat(dent->d_name, &st) == 0 && S_ISDIR(st.st_mode))) {
t = curdepth; t = curdepth;
curdepth++; curdepth++;
n += du(dent->d_name); n += du(dent->d_name, follow);
curdepth = t; curdepth = t;
continue; continue;
} }
@ -157,6 +164,10 @@ main(int argc, char *argv[])
case 'h': case 'h':
hflag = 1; hflag = 1;
break; break;
case 'H':
case 'L':
HLflag = ARGC();
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -172,13 +183,13 @@ main(int argc, char *argv[])
blksize = 1024; blksize = 1024;
if (argc < 1) { if (argc < 1) {
n = du("."); n = du(".", HLflag);
if (sflag) if (sflag)
print(n, xrealpath(".", file)); print(n, xrealpath(".", file));
} else { } else {
for (; argc > 0; argc--, argv++) { for (; argc > 0; argc--, argv++) {
curdepth = 0; curdepth = 0;
n = du(argv[0]); n = du(argv[0], HLflag);
if (sflag) if (sflag)
print(n, xrealpath(argv[0], file)); print(n, xrealpath(argv[0], file));
} }