2013-10-16 11:58:52 -04:00
|
|
|
/* 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;
|
2013-10-17 09:06:17 -04:00
|
|
|
static char file[PATH_MAX];
|
2013-10-16 11:58:52 -04:00
|
|
|
|
|
|
|
static bool aflag = false;
|
2013-10-16 12:54:29 -04:00
|
|
|
static bool sflag = false;
|
2013-10-16 13:00:02 -04:00
|
|
|
static bool kflag = false;
|
2013-10-16 11:58:52 -04:00
|
|
|
|
|
|
|
static long du(const char *);
|
2013-10-16 12:54:29 -04:00
|
|
|
static void print(long n, char *path);
|
2013-10-16 11:58:52 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-10-17 06:21:15 -04:00
|
|
|
eprintf("usage: %s [-a | -s] [-k] [file...]\n", argv0);
|
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)
|
|
|
|
eprintf("realpath: %s:");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-10-16 11:58:52 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *bsize;
|
2013-10-16 12:54:29 -04:00
|
|
|
long n;
|
2013-10-16 11:58:52 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'a':
|
|
|
|
aflag = true;
|
|
|
|
break;
|
2013-10-16 12:54:29 -04:00
|
|
|
case 's':
|
|
|
|
sflag = true;
|
|
|
|
break;
|
2013-10-16 13:00:02 -04:00
|
|
|
case 'k':
|
|
|
|
kflag = true;
|
|
|
|
break;
|
2013-10-16 11:58:52 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2013-10-17 06:21:15 -04:00
|
|
|
if (aflag && sflag)
|
|
|
|
usage();
|
|
|
|
|
2013-10-16 11:58:52 -04:00
|
|
|
bsize = getenv("BLOCKSIZE");
|
|
|
|
if (bsize)
|
|
|
|
blksize = estrtol(bsize, 0);
|
|
|
|
|
2013-10-16 13:00:02 -04:00
|
|
|
if (kflag)
|
|
|
|
blksize = 1024;
|
|
|
|
|
2013-10-16 11:58:52 -04:00
|
|
|
if (argc < 1) {
|
2013-10-16 12:54:29 -04:00
|
|
|
n = du(".");
|
|
|
|
if (sflag)
|
2014-01-23 16:16:05 -05:00
|
|
|
print(n, xrealpath(".", file));
|
2013-10-16 11:58:52 -04:00
|
|
|
} else {
|
2013-10-16 12:54:29 -04:00
|
|
|
for (; argc > 0; argc--, argv++) {
|
|
|
|
n = du(argv[0]);
|
|
|
|
if (sflag)
|
2014-01-23 16:16:05 -05:00
|
|
|
print(n, xrealpath(argv[0], file));
|
2013-10-16 12:54:29 -04:00
|
|
|
}
|
2013-10-16 11:58:52 -04:00
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print(long n, char *path)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-10-18 11:40:31 -04:00
|
|
|
static long
|
|
|
|
nblks(struct stat *st)
|
|
|
|
{
|
|
|
|
return (512 * st->st_blocks + blksize - 1) / blksize;
|
|
|
|
}
|
|
|
|
|
2013-10-16 11:58:52 -04:00
|
|
|
static long
|
|
|
|
du(const char *path)
|
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
char *cwd;
|
|
|
|
struct dirent *dent;
|
|
|
|
struct stat st;
|
|
|
|
long n = 0, m;
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-10-17 13:03:01 -04:00
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
n += du(dent->d_name);
|
|
|
|
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)) {
|
|
|
|
if (snprintf(file, sizeof(file), "%s/%s",
|
|
|
|
cwd, dent->d_name) >= sizeof(file))
|
|
|
|
enprintf(EXIT_FAILURE, "path too long\n");
|
|
|
|
} else {
|
2014-01-23 16:16:05 -05:00
|
|
|
xrealpath(dent->d_name, file);
|
2014-01-23 15:05:01 -05:00
|
|
|
}
|
2013-10-17 13:03:01 -04:00
|
|
|
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:
|
2013-10-16 12:54:29 -04:00
|
|
|
if (!sflag)
|
2014-01-23 16:16:05 -05:00
|
|
|
print(n, xrealpath(path, file));
|
2013-10-16 11:58:52 -04:00
|
|
|
return n;
|
|
|
|
}
|