Adjust some limits to more flexibility for strtonum

This commit is contained in:
FRIGN 2015-02-01 01:24:03 +01:00
parent 5a20d0e9d7
commit 27b770c02c
10 changed files with 110 additions and 110 deletions

1
cols.c
View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <assert.h> #include <assert.h>
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

2
date.c
View File

@ -26,7 +26,7 @@ main(int argc, char *argv[])
t = time(NULL); t = time(NULL);
ARGBEGIN { ARGBEGIN {
case 'd': case 'd':
t = estrtonum(EARGF(usage()), 0, LLONG_MAX); t = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, (time_t)-1));
break; break;
case 'u': case 'u':
tztime = gmtime; tztime = gmtime;

138
du.c
View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <dirent.h> #include <dirent.h>
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -10,10 +11,10 @@
#include "util.h" #include "util.h"
static long blksize = 512; static size_t blksize = 512;
static char file[PATH_MAX]; static char file[PATH_MAX];
static long depth = -1; static size_t depth = -1;
static long curdepth = 0; static size_t curdepth = 0;
static int aflag = 0; static int aflag = 0;
static int dflag = 0; static int dflag = 0;
@ -21,15 +22,6 @@ static int sflag = 0;
static int kflag = 0; static int kflag = 0;
static int hflag = 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 * static char *
xrealpath(const char *pathname, char *resolved) xrealpath(const char *pathname, char *resolved)
{ {
@ -41,60 +33,8 @@ xrealpath(const char *pathname, char *resolved)
return r; 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 static void
print(long n, char *path) print(size_t n, char *path)
{ {
if (hflag) if (hflag)
printf("%s\t%s\n", humansize(n * blksize), path); printf("%s\t%s\n", humansize(n * blksize), path);
@ -121,21 +61,21 @@ pop(char *path)
free(path); free(path);
} }
static long static size_t
nblks(struct stat *st) nblks(struct stat *st)
{ {
return (512 * st->st_blocks + blksize - 1) / blksize; return (512 * st->st_blocks + blksize - 1) / blksize;
} }
static long static size_t
du(const char *path) du(const char *path)
{ {
DIR *dp;
char *cwd;
struct dirent *dent; struct dirent *dent;
struct stat st; struct stat st;
long n = 0, m, t; DIR *dp;
size_t n = 0, m, t;
int r; int r;
char *cwd;
if (lstat(path, &st) < 0) if (lstat(path, &st) < 0)
eprintf("stat: %s:", path); eprintf("stat: %s:", path);
@ -187,3 +127,61 @@ done:
print(n, xrealpath(path, file)); print(n, xrealpath(path, file));
return n; 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;
}

View File

@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -22,7 +23,7 @@ parselist(const char *s)
if (*p == '\0') if (*p == '\0')
eprintf("empty field in tablist\n"); eprintf("empty field in tablist\n");
tablist = erealloc(tablist, (i + 1) * sizeof(*tablist)); tablist = erealloc(tablist, (i + 1) * sizeof(*tablist));
tablist[i] = estrtonum(p, 1, LLONG_MAX); tablist[i] = estrtonum(p, 1, MIN(LLONG_MAX, SIZE_MAX));
if (i > 0 && tablist[i - 1] >= tablist[i]) if (i > 0 && tablist[i - 1] >= tablist[i])
eprintf("tablist must be ascending\n"); eprintf("tablist must be ascending\n");
} }

3
fold.c
View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <ctype.h> #include <ctype.h>
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -84,7 +85,7 @@ main(int argc, char *argv[])
sflag = 1; sflag = 1;
break; break;
case 'w': case 'w':
width = estrtonum(EARGF(usage()), 1, LLONG_MAX); width = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
break; break;
ARGNUM: ARGNUM:
width = ARGNUMF(); width = ARGNUMF();

5
head.c
View File

@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -34,14 +35,14 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
long n = 10; size_t n = 10;
FILE *fp; FILE *fp;
int ret = 0; int ret = 0;
int newline, many; int newline, many;
ARGBEGIN { ARGBEGIN {
case 'n': case 'n':
n = estrtonum(EARGF(usage()), 0, LONG_MAX); n = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
break; break;
ARGNUM: ARGNUM:
n = ARGNUMF(); n = ARGNUMF();

46
nl.c
View File

@ -2,6 +2,7 @@
#include <limits.h> #include <limits.h>
#include <regex.h> #include <regex.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -9,13 +10,30 @@
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
static void nl(const char *, FILE *);
static char mode = 't'; static char mode = 't';
static const char *sep = "\t"; static const char *sep = "\t";
static long incr = 1; static size_t incr = 1;
static regex_t preg; static regex_t preg;
void
nl(const char *name, FILE *fp)
{
char *buf = NULL;
size_t n = 0, size = 0;
while (getline(&buf, &size, fp) != -1) {
if ((mode == 'a')
|| (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
|| (mode == 't' && buf[0] != '\n'))
printf("%6ld%s%s", n += incr, sep, buf);
else
printf(" %s", buf);
}
free(buf);
if (ferror(fp))
eprintf("%s: read error:", name);
}
static void static void
usage(void) usage(void)
{ {
@ -38,7 +56,7 @@ main(int argc, char *argv[])
usage(); usage();
break; break;
case 'i': case 'i':
incr = estrtonum(EARGF(usage()), 0, LONG_MAX); incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
break; break;
case 's': case 's':
sep = EARGF(usage()); sep = EARGF(usage());
@ -60,23 +78,3 @@ main(int argc, char *argv[])
} }
return 0; return 0;
} }
void
nl(const char *name, FILE *fp)
{
char *buf = NULL;
long n = 0;
size_t size = 0;
while (getline(&buf, &size, fp) != -1) {
if ((mode == 'a')
|| (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
|| (mode == 't' && buf[0] != '\n'))
printf("%6ld%s%s", n += incr, sep, buf);
else
printf(" %s", buf);
}
free(buf);
if (ferror(fp))
eprintf("%s: read error:", name);
}

2
seq.c
View File

@ -110,7 +110,7 @@ digitsright(const char *d)
int shift, after; int shift, after;
exp = strpbrk(d, "eE"); exp = strpbrk(d, "eE");
shift = exp ? estrtonum(&exp[1], -INT_MAX, INT_MAX) : 0; shift = exp ? estrtonum(&exp[1], INT_MIN, INT_MAX) : 0;
after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0; after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0;
return MAX(0, after - shift); return MAX(0, after - shift);

View File

@ -28,7 +28,7 @@ main(int argc, char *argv[])
char *prefix = "x"; char *prefix = "x";
char *file = NULL; char *file = NULL;
char *tmp, *end; char *tmp, *end;
uint64_t size = 1000, scale = 1, n; size_t size = 1000, scale = 1, n;
int always = 0; int always = 0;
FILE *in = stdin, *out = NULL; FILE *in = stdin, *out = NULL;
@ -55,7 +55,7 @@ main(int argc, char *argv[])
default: default:
usage(); usage();
} }
if (size > (UINT64_MAX/scale)) if (size > (SIZE_MAX/scale))
eprintf("'%s': out of range\n", tmp); eprintf("'%s': out of range\n", tmp);
size *= scale; size *= scale;
break; break;
@ -63,7 +63,7 @@ main(int argc, char *argv[])
always = 0; always = 0;
tmp = ARGF(); tmp = ARGF();
if (tmp) if (tmp)
size = estrtonum(tmp, 0, LLONG_MAX); size = estrtonum(tmp, 0, MIN(LLONG_MAX, SIZE_MAX));
break; break;
case 'a': case 'a':
slen = estrtonum(EARGF(usage()), 0, INT_MAX); slen = estrtonum(EARGF(usage()), 0, INT_MAX);

View File

@ -65,7 +65,7 @@ main(int argc, char *argv[])
mflag = 1; mflag = 1;
break; break;
case 't': case 't':
t = estrtonum(EARGF(usage()), 0, LLONG_MAX); t = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, (time_t)-1));
break; break;
default: default:
usage(); usage();