version 1.2, 1996/06/26 05:32:38 |
version 1.3, 1996/10/18 18:16:07 |
|
|
/* $OpenBSD$ */ |
/* $OpenBSD$ */ |
/* $NetBSD: du.c,v 1.10 1995/09/28 06:19:56 perry Exp $ */ |
/* $NetBSD: du.c,v 1.11 1996/10/18 07:20:35 thorpej Exp $ */ |
|
|
/* |
/* |
* Copyright (c) 1989, 1993, 1994 |
* Copyright (c) 1989, 1993, 1994 |
|
|
{ |
{ |
FTS *fts; |
FTS *fts; |
FTSENT *p; |
FTSENT *p; |
long blocksize; |
long blocksize, totalblocks; |
int ftsoptions, listdirs, listfiles; |
int ftsoptions, listdirs, listfiles; |
int Hflag, Lflag, Pflag, aflag, ch, kflag, notused, rval, sflag; |
int Hflag, Lflag, Pflag, aflag, ch, cflag, kflag, notused, rval, sflag; |
char **save; |
char **save; |
|
|
save = argv; |
save = argv; |
Hflag = Lflag = Pflag = aflag = kflag = sflag = 0; |
Hflag = Lflag = Pflag = aflag = cflag = kflag = sflag = 0; |
|
totalblocks = 0; |
ftsoptions = FTS_PHYSICAL; |
ftsoptions = FTS_PHYSICAL; |
while ((ch = getopt(argc, argv, "HLPaksx")) != EOF) |
while ((ch = getopt(argc, argv, "HLPacksx")) != EOF) |
switch (ch) { |
switch (ch) { |
case 'H': |
case 'H': |
Hflag = 1; |
Hflag = 1; |
|
|
case 'a': |
case 'a': |
aflag = 1; |
aflag = 1; |
break; |
break; |
|
case 'c': |
|
cflag = 1; |
|
break; |
case 'k': |
case 'k': |
blocksize = 1024; |
blocksize = 1024; |
kflag = 1; |
kflag = 1; |
|
|
blocksize /= 512; |
blocksize /= 512; |
|
|
if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL) |
if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL) |
err(1, NULL); |
err(1, "fts_open"); |
|
|
for (rval = 0; (p = fts_read(fts)) != NULL;) |
for (rval = 0; (p = fts_read(fts)) != NULL;) |
switch (p->fts_info) { |
switch (p->fts_info) { |
|
|
case FTS_DP: |
case FTS_DP: |
p->fts_parent->fts_number += |
p->fts_parent->fts_number += |
p->fts_number += p->fts_statp->st_blocks; |
p->fts_number += p->fts_statp->st_blocks; |
|
if (cflag) |
|
totalblocks += p->fts_statp->st_blocks; |
/* |
/* |
* If listing each directory, or not listing files |
* If listing each directory, or not listing files |
* 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 || !listfiles && !p->fts_level) |
if (listdirs || (!listfiles && !p->fts_level)) |
(void)printf("%ld\t%s\n", |
(void)printf("%ld\t%s\n", |
howmany(p->fts_number, blocksize), |
howmany(p->fts_number, blocksize), |
p->fts_path); |
p->fts_path); |
|
|
howmany(p->fts_statp->st_blocks, blocksize), |
howmany(p->fts_statp->st_blocks, blocksize), |
p->fts_path); |
p->fts_path); |
p->fts_parent->fts_number += p->fts_statp->st_blocks; |
p->fts_parent->fts_number += p->fts_statp->st_blocks; |
|
if (cflag) |
|
totalblocks += p->fts_statp->st_blocks; |
} |
} |
if (errno) |
if (errno) |
err(1, "fts_read"); |
err(1, "fts_read"); |
|
if (cflag) |
|
(void)printf("%ld\ttotal\n", |
|
howmany(totalblocks, blocksize)); |
exit(0); |
exit(0); |
} |
} |
|
|
|
|
|
|
if (nfiles == maxfiles && (files = realloc((char *)files, |
if (nfiles == maxfiles && (files = realloc((char *)files, |
(u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL) |
(u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL) |
err(1, ""); |
err(1, "can't allocate memory"); |
files[nfiles].inode = ino; |
files[nfiles].inode = ino; |
files[nfiles].dev = dev; |
files[nfiles].dev = dev; |
++nfiles; |
++nfiles; |
|
|
{ |
{ |
|
|
(void)fprintf(stderr, |
(void)fprintf(stderr, |
"usage: du [-H | -L | -P] [-a | -s] [-kx] [file ...]\n"); |
"usage: du [-H | -L | -P] [-a | -s] [-ckx] [file ...]\n"); |
exit(1); |
exit(1); |
} |
} |