Adjust some limits to more flexibility for strtonum
This commit is contained in:
140
du.c
140
du.c
@@ -1,6 +1,7 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#include <dirent.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -10,10 +11,10 @@
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static long blksize = 512;
|
||||
static char file[PATH_MAX];
|
||||
static long depth = -1;
|
||||
static long curdepth = 0;
|
||||
static size_t blksize = 512;
|
||||
static char file[PATH_MAX];
|
||||
static size_t depth = -1;
|
||||
static size_t curdepth = 0;
|
||||
|
||||
static int aflag = 0;
|
||||
static int dflag = 0;
|
||||
@@ -21,15 +22,6 @@ static int sflag = 0;
|
||||
static int kflag = 0;
|
||||
static int hflag = 0;
|
||||
|
||||
static long du(const char *);
|
||||
static void print(long n, char *path);
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
eprintf("usage: %s [-a | -s] [-d depth] [-h] [-k] [file...]\n", argv0);
|
||||
}
|
||||
|
||||
static char *
|
||||
xrealpath(const char *pathname, char *resolved)
|
||||
{
|
||||
@@ -41,60 +33,8 @@ xrealpath(const char *pathname, char *resolved)
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char *bsize;
|
||||
long n;
|
||||
|
||||
ARGBEGIN {
|
||||
case 'a':
|
||||
aflag = 1;
|
||||
break;
|
||||
case 'd':
|
||||
dflag = 1;
|
||||
depth = estrtonum(EARGF(usage()), 0, LONG_MAX);
|
||||
break;
|
||||
case 's':
|
||||
sflag = 1;
|
||||
break;
|
||||
case 'k':
|
||||
kflag = 1;
|
||||
break;
|
||||
case 'h':
|
||||
hflag = 1;
|
||||
break;
|
||||
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) {
|
||||
n = du(".");
|
||||
if (sflag)
|
||||
print(n, xrealpath(".", file));
|
||||
} else {
|
||||
for (; argc > 0; argc--, argv++) {
|
||||
curdepth = 0;
|
||||
n = du(argv[0]);
|
||||
if (sflag)
|
||||
print(n, xrealpath(argv[0], file));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
print(long n, char *path)
|
||||
print(size_t n, char *path)
|
||||
{
|
||||
if (hflag)
|
||||
printf("%s\t%s\n", humansize(n * blksize), path);
|
||||
@@ -121,21 +61,21 @@ pop(char *path)
|
||||
free(path);
|
||||
}
|
||||
|
||||
static long
|
||||
static size_t
|
||||
nblks(struct stat *st)
|
||||
{
|
||||
return (512 * st->st_blocks + blksize - 1) / blksize;
|
||||
}
|
||||
|
||||
static long
|
||||
static size_t
|
||||
du(const char *path)
|
||||
{
|
||||
DIR *dp;
|
||||
char *cwd;
|
||||
struct dirent *dent;
|
||||
struct stat st;
|
||||
long n = 0, m, t;
|
||||
DIR *dp;
|
||||
size_t n = 0, m, t;
|
||||
int r;
|
||||
char *cwd;
|
||||
|
||||
if (lstat(path, &st) < 0)
|
||||
eprintf("stat: %s:", path);
|
||||
@@ -187,3 +127,61 @@ done:
|
||||
print(n, xrealpath(path, file));
|
||||
return n;
|
||||
}
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
eprintf("usage: %s [-a | -s] [-d depth] [-h] [-k] [file ...]\n", argv0);
|
||||
}
|
||||
|
||||
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;
|
||||
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) {
|
||||
n = du(".");
|
||||
if (sflag)
|
||||
print(n, xrealpath(".", file));
|
||||
} else {
|
||||
for (; argc > 0; argc--, argv++) {
|
||||
curdepth = 0;
|
||||
n = du(argv[0]);
|
||||
if (sflag)
|
||||
print(n, xrealpath(argv[0], file));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user