2013-10-16 11:58:52 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 15:02:41 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2013-10-16 11:58:52 -04:00
|
|
|
#include <dirent.h>
|
2015-01-31 19:24:03 -05:00
|
|
|
#include <stdint.h>
|
2013-10-16 11:58:52 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-13 13:54:28 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2013-10-16 11:58:52 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-01-31 19:24:03 -05:00
|
|
|
static size_t blksize = 512;
|
|
|
|
static char file[PATH_MAX];
|
|
|
|
static size_t depth = -1;
|
|
|
|
static size_t curdepth = 0;
|
2013-10-16 11:58:52 -04:00
|
|
|
|
2014-11-13 15:24:47 -05:00
|
|
|
static int aflag = 0;
|
|
|
|
static int dflag = 0;
|
|
|
|
static int sflag = 0;
|
|
|
|
static int kflag = 0;
|
|
|
|
static int hflag = 0;
|
2015-02-17 11:07:10 -05:00
|
|
|
static int HLPflag = 'P';
|
2013-10-16 11:58:52 -04:00
|
|
|
|
2014-01-23 16:16:05 -05:00
|
|
|
static char *
|
|
|
|
xrealpath(const char *pathname, char *resolved)
|
|
|
|
{
|
|
|
|
char *r;
|
|
|
|
|
|
|
|
r = realpath(pathname, resolved);
|
|
|
|
if (!r)
|
2014-12-21 16:56:52 -05:00
|
|
|
eprintf("realpath: %s:", pathname);
|
2014-01-23 16:16:05 -05:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-10-16 11:58:52 -04:00
|
|
|
static void
|
2015-01-31 19:24:03 -05:00
|
|
|
print(size_t n, char *path)
|
2013-10-16 11:58:52 -04:00
|
|
|
{
|
2014-10-16 05:06:27 -04:00
|
|
|
if (hflag)
|
2014-10-18 17:25:00 -04:00
|
|
|
printf("%s\t%s\n", humansize(n * blksize), path);
|
2014-10-16 05:06:27 -04:00
|
|
|
else
|
|
|
|
printf("%lu\t%s\n", n, path);
|
2013-10-16 11:58:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
push(const char *path)
|
|
|
|
{
|
|
|
|
char *cwd;
|
|
|
|
|
|
|
|
cwd = agetcwd();
|
|
|
|
if (chdir(path) < 0)
|
|
|
|
eprintf("chdir: %s:", path);
|
|
|
|
return cwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pop(char *path)
|
|
|
|
{
|
|
|
|
if (chdir(path) < 0)
|
|
|
|
eprintf("chdir: %s:", path);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
2015-01-31 19:24:03 -05:00
|
|
|
static size_t
|
2013-10-18 11:40:31 -04:00
|
|
|
nblks(struct stat *st)
|
|
|
|
{
|
|
|
|
return (512 * st->st_blocks + blksize - 1) / blksize;
|
|
|
|
}
|
|
|
|
|
2015-01-31 19:24:03 -05:00
|
|
|
static size_t
|
2015-02-09 17:18:49 -05:00
|
|
|
du(const char *path, char follow)
|
2013-10-16 11:58:52 -04:00
|
|
|
{
|
|
|
|
struct dirent *dent;
|
|
|
|
struct stat st;
|
2015-01-31 19:24:03 -05:00
|
|
|
DIR *dp;
|
|
|
|
size_t n = 0, m, t;
|
2014-06-01 09:01:09 -04:00
|
|
|
int r;
|
2015-01-31 19:24:03 -05:00
|
|
|
char *cwd;
|
2013-10-16 11:58:52 -04:00
|
|
|
|
|
|
|
if (lstat(path, &st) < 0)
|
|
|
|
eprintf("stat: %s:", path);
|
2013-10-18 11:40:31 -04:00
|
|
|
n = nblks(&st);
|
2013-10-16 11:58:52 -04:00
|
|
|
|
2015-02-09 17:18:49 -05:00
|
|
|
if (!(S_ISDIR(st.st_mode) ||
|
|
|
|
(follow != 'P' && S_ISLNK(st.st_mode) &&
|
|
|
|
stat(path, &st) == 0 && S_ISDIR(st.st_mode))))
|
2013-10-17 13:03:01 -04:00
|
|
|
goto done;
|
|
|
|
|
2015-02-09 17:18:49 -05:00
|
|
|
follow = follow == 'H' ? 'P' : follow;
|
|
|
|
|
2013-10-17 13:03:01 -04:00
|
|
|
dp = opendir(path);
|
|
|
|
if (!dp) {
|
2013-11-13 06:39:24 -05:00
|
|
|
weprintf("opendir %s:", path);
|
2013-10-17 13:03:01 -04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
cwd = push(path);
|
|
|
|
while ((dent = readdir(dp))) {
|
|
|
|
if (strcmp(dent->d_name, ".") == 0 ||
|
|
|
|
strcmp(dent->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (lstat(dent->d_name, &st) < 0)
|
|
|
|
eprintf("stat: %s:", dent->d_name);
|
2015-02-09 17:18:49 -05:00
|
|
|
if (S_ISDIR(st.st_mode) ||
|
|
|
|
(follow != 'P' && S_ISLNK(st.st_mode) &&
|
|
|
|
stat(dent->d_name, &st) == 0 && S_ISDIR(st.st_mode))) {
|
2014-10-18 17:26:35 -04:00
|
|
|
t = curdepth;
|
|
|
|
curdepth++;
|
2015-02-09 17:18:49 -05:00
|
|
|
n += du(dent->d_name, follow);
|
2014-10-18 17:26:35 -04:00
|
|
|
curdepth = t;
|
2013-10-17 13:03:01 -04:00
|
|
|
continue;
|
|
|
|
}
|
2013-10-18 11:40:31 -04:00
|
|
|
m = nblks(&st);
|
2013-10-17 13:03:01 -04:00
|
|
|
n += m;
|
|
|
|
if (aflag && !sflag) {
|
2014-01-23 15:05:01 -05:00
|
|
|
if (S_ISLNK(st.st_mode)) {
|
2014-06-01 09:01:09 -04:00
|
|
|
r = snprintf(file, sizeof(file), "%s/%s",
|
|
|
|
cwd, dent->d_name);
|
2014-10-18 17:26:35 -04:00
|
|
|
if (r >= sizeof(file) || r < 0)
|
2014-01-30 08:54:16 -05:00
|
|
|
eprintf("path too long\n");
|
2014-01-23 15:05:01 -05:00
|
|
|
} else {
|
2014-01-23 16:16:05 -05:00
|
|
|
xrealpath(dent->d_name, file);
|
2014-01-23 15:05:01 -05:00
|
|
|
}
|
2014-10-18 17:26:35 -04:00
|
|
|
if (!dflag || (depth != -1 && curdepth < depth))
|
|
|
|
print(m, file);
|
2013-10-16 11:58:52 -04:00
|
|
|
}
|
|
|
|
}
|
2013-10-17 13:03:01 -04:00
|
|
|
pop(cwd);
|
|
|
|
closedir(dp);
|
2013-10-16 11:58:52 -04:00
|
|
|
|
2013-10-17 13:03:01 -04:00
|
|
|
done:
|
2014-10-18 17:26:35 -04:00
|
|
|
if (!sflag && (!dflag || (depth != -1 && curdepth <= depth)))
|
2014-01-23 16:16:05 -05:00
|
|
|
print(n, xrealpath(path, file));
|
2013-10-16 11:58:52 -04:00
|
|
|
return n;
|
|
|
|
}
|
2015-01-31 19:24:03 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-17 11:07:10 -05:00
|
|
|
eprintf("usage: %s [-a | -s] [-d depth] [-h] [-k] [-H | -L | -P] [file ...]\n", argv0);
|
2015-01-31 19:24:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
char *bsize;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
depth = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
kflag = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
|
|
|
break;
|
2015-02-09 17:18:49 -05:00
|
|
|
case 'H':
|
|
|
|
case 'L':
|
2015-02-17 11:07:10 -05:00
|
|
|
case 'P':
|
|
|
|
HLPflag = ARGC();
|
2015-02-09 17:18:49 -05:00
|
|
|
break;
|
2015-01-31 19:24:03 -05:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if ((aflag && sflag) || (dflag && sflag))
|
|
|
|
usage();
|
|
|
|
|
|
|
|
bsize = getenv("BLOCKSIZE");
|
|
|
|
if (bsize)
|
|
|
|
blksize = estrtonum(bsize, 0, LONG_MAX);
|
|
|
|
|
|
|
|
if (kflag)
|
|
|
|
blksize = 1024;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
2015-02-17 11:07:10 -05:00
|
|
|
n = du(".", HLPflag);
|
2015-01-31 19:24:03 -05:00
|
|
|
if (sflag)
|
|
|
|
print(n, xrealpath(".", file));
|
|
|
|
} else {
|
|
|
|
for (; argc > 0; argc--, argv++) {
|
|
|
|
curdepth = 0;
|
2015-02-17 11:07:10 -05:00
|
|
|
n = du(argv[0], HLPflag);
|
2015-01-31 19:24:03 -05:00
|
|
|
if (sflag)
|
|
|
|
print(n, xrealpath(argv[0], file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|