commit 593effc7c82a8a49a00bec06185403bd5f6e33b8
parent 9d2b94dbb02d42ed7526c6effefc2a12e87e3eb1
Author: Quentin Rameau <quinq@quinq.eu.org>
Date: Wed, 18 Feb 2015 18:24:21 +0100
Add -x support for du(1)
Diffstat:
M | du.1 | | | 3 | +++ |
M | du.c | | | 20 | +++++++++++++------- |
2 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/du.1 b/du.1
@@ -11,6 +11,7 @@
.Op Fl h
.Op Fl k
.Op Fl H | L | P
+.Op Fl x
.Op Ar file ...
.Sh DESCRIPTION
.Nm
@@ -43,4 +44,6 @@ recursively traversing directories.
Always dereference symbolic links while recursively traversing directories.
.It Fl P
Don't dereference symbolic links. This is the default.
+.It Fl x
+Do not traverse file systems mount points.
.El
diff --git a/du.c b/du.c
@@ -21,6 +21,7 @@ static int dflag = 0;
static int sflag = 0;
static int kflag = 0;
static int hflag = 0;
+static int xflag = 0;
static int HLPflag = 'P';
static char *
@@ -72,19 +73,19 @@ static size_t
du(const char *path, char follow)
{
struct dirent *dent;
- struct stat st;
+ struct stat pst, st;
DIR *dp;
size_t n = 0, m, t;
int r;
char *cwd;
- if (lstat(path, &st) < 0)
+ if (lstat(path, &pst) < 0)
eprintf("stat: %s:", path);
- n = nblks(&st);
+ n = nblks(&pst);
- if (!(S_ISDIR(st.st_mode) ||
- (follow != 'P' && S_ISLNK(st.st_mode) &&
- stat(path, &st) == 0 && S_ISDIR(st.st_mode))))
+ if (!(S_ISDIR(pst.st_mode) ||
+ (follow != 'P' && S_ISLNK(pst.st_mode) &&
+ stat(path, &pst) == 0 && S_ISDIR(pst.st_mode))))
goto done;
follow = follow == 'H' ? 'P' : follow;
@@ -102,6 +103,8 @@ du(const char *path, char follow)
continue;
if (lstat(dent->d_name, &st) < 0)
eprintf("stat: %s:", dent->d_name);
+ if (xflag && st.st_dev != pst.st_dev)
+ continue;
if (S_ISDIR(st.st_mode) ||
(follow != 'P' && S_ISLNK(st.st_mode) &&
stat(dent->d_name, &st) == 0 && S_ISDIR(st.st_mode))) {
@@ -138,7 +141,7 @@ done:
static void
usage(void)
{
- eprintf("usage: %s [-a | -s] [-d depth] [-h] [-k] [-H | -L | -P] [file ...]\n", argv0);
+ eprintf("usage: %s [-a | -s] [-d depth] [-h] [-k] [-H | -L | -P] [-x] [file ...]\n", argv0);
}
int
@@ -169,6 +172,9 @@ main(int argc, char *argv[])
case 'P':
HLPflag = ARGC();
break;
+ case 'x':
+ xflag = 1;
+ break;
default:
usage();
} ARGEND;