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