2013-10-16 15:58:52 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 20:02:41 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2015-03-11 22:21:52 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2015-02-01 00:24:03 +00:00
|
|
|
#include <stdint.h>
|
2013-10-16 15:58:52 +00:00
|
|
|
#include <stdlib.h>
|
2015-03-11 22:21:52 +00:00
|
|
|
#include <stdio.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-10-16 15:58:52 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-03-11 22:21:52 +00:00
|
|
|
static size_t maxdepth = SIZE_MAX;
|
|
|
|
static size_t blksize = 512;
|
2013-10-16 15:58:52 +00:00
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int aflag = 0;
|
|
|
|
static int sflag = 0;
|
|
|
|
static int hflag = 0;
|
2015-03-11 22:21:52 +00:00
|
|
|
static int ret = 0;
|
2014-01-23 21:16:05 +00:00
|
|
|
|
2013-10-16 15:58:52 +00:00
|
|
|
static void
|
2015-03-11 22:21:52 +00:00
|
|
|
printpath(size_t n, const char *path)
|
2013-10-16 15:58:52 +00:00
|
|
|
{
|
2014-10-16 09:06:27 +00:00
|
|
|
if (hflag)
|
2014-10-18 21:25:00 +00:00
|
|
|
printf("%s\t%s\n", humansize(n * blksize), path);
|
2014-10-16 09:06:27 +00:00
|
|
|
else
|
2015-03-11 22:21:52 +00:00
|
|
|
printf("%zu\t%s\n", n, path);
|
2013-10-16 15:58:52 +00:00
|
|
|
}
|
|
|
|
|
2015-02-01 00:24:03 +00:00
|
|
|
static size_t
|
2015-03-11 22:21:52 +00:00
|
|
|
nblks(blkcnt_t blocks)
|
2013-10-18 15:40:31 +00:00
|
|
|
{
|
2015-03-11 22:21:52 +00:00
|
|
|
return (512 * blocks + blksize - 1) / blksize;
|
2013-10-18 15:40:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 22:21:52 +00:00
|
|
|
void
|
|
|
|
du(const char *path, int depth, void *total)
|
2013-10-16 15:58:52 +00:00
|
|
|
{
|
2015-03-11 22:21:52 +00:00
|
|
|
struct stat st;
|
|
|
|
size_t subtotal = 0;
|
|
|
|
|
|
|
|
if (lstat(path, &st) < 0) {
|
|
|
|
if (!depth || errno != ENOENT)
|
|
|
|
weprintf("stat %s:", path);
|
|
|
|
if (!depth)
|
|
|
|
ret = 1;
|
|
|
|
return;
|
2013-10-17 17:03:01 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 22:21:52 +00:00
|
|
|
recurse(path, du, depth, &subtotal);
|
|
|
|
*((size_t *)total) += subtotal + nblks(st.st_blocks);
|
2013-10-16 15:58:52 +00:00
|
|
|
|
2015-03-11 22:21:52 +00:00
|
|
|
if (!sflag && depth <= maxdepth && (S_ISDIR(st.st_mode) || aflag))
|
|
|
|
printpath(subtotal + nblks(st.st_blocks), path);
|
2013-10-16 15:58:52 +00:00
|
|
|
}
|
2015-02-01 00:24:03 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-18 17:24:21 +00:00
|
|
|
eprintf("usage: %s [-a | -s] [-d depth] [-h] [-k] [-H | -L | -P] [-x] [file ...]\n", argv0);
|
2015-02-01 00:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-03-11 22:21:52 +00:00
|
|
|
int kflag = 0, dflag = 0;
|
|
|
|
size_t n = 0;
|
2015-02-01 00:24:03 +00:00
|
|
|
char *bsize;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
2015-03-11 22:21:52 +00:00
|
|
|
maxdepth = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
|
2015-02-01 00:24:03 +00:00
|
|
|
break;
|
2015-03-11 22:21:52 +00:00
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
2015-02-01 00:24:03 +00:00
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
kflag = 1;
|
|
|
|
break;
|
2015-03-11 22:21:52 +00:00
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
recurse_samedev = 1;
|
2015-02-01 00:24:03 +00:00
|
|
|
break;
|
2015-02-09 22:18:49 +00:00
|
|
|
case 'H':
|
|
|
|
case 'L':
|
2015-02-17 16:07:10 +00:00
|
|
|
case 'P':
|
2015-03-11 22:21:52 +00:00
|
|
|
recurse_follow = ARGC();
|
2015-02-18 17:24:21 +00:00
|
|
|
break;
|
2015-02-01 00:24:03 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if ((aflag && sflag) || (dflag && sflag))
|
|
|
|
usage();
|
|
|
|
|
|
|
|
bsize = getenv("BLOCKSIZE");
|
|
|
|
if (bsize)
|
2015-03-11 22:21:52 +00:00
|
|
|
blksize = estrtonum(bsize, 0, MIN(LLONG_MAX, SIZE_MAX));
|
2015-02-01 00:24:03 +00:00
|
|
|
if (kflag)
|
|
|
|
blksize = 1024;
|
|
|
|
|
2015-03-11 22:21:52 +00:00
|
|
|
if (!argc) {
|
|
|
|
du(".", 0, &n);
|
|
|
|
if (sflag && !ret)
|
|
|
|
printpath(nblks(n), ".");
|
2015-02-01 00:24:03 +00:00
|
|
|
} else {
|
2015-03-11 22:21:52 +00:00
|
|
|
for (; *argv; argc--, argv++) {
|
|
|
|
du(argv[0], 0, &n);
|
|
|
|
if (sflag && !ret)
|
|
|
|
printpath(n, argv[0]);
|
2015-02-01 00:24:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-11 22:21:52 +00:00
|
|
|
|
|
|
|
return ret;
|
2015-02-01 00:24:03 +00:00
|
|
|
}
|