sbase/du.c

191 lines
3.1 KiB
C
Raw Normal View History

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