sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit 27b770c02c3e936d12d3e0606e6ebf6c68b145e6
parent 5a20d0e9d76521830ae548225f846a4f199a1fed
Author: FRIGN <dev@frign.de>
Date:   Sun Feb  1 01:24:03 +0100

Adjust some limits to more flexibility for strtonum

Diffstat:
cols.c | 1+
date.c | 2+-
du.c | 140+++++++++++++++++++++++++++++++++++++++----------------------------------------
expand.c | 9+++++----
fold.c | 3++-
head.c | 5+++--
nl.c | 50++++++++++++++++++++++++--------------------------
seq.c | 2+-
split.c | 6+++---
touch.c | 2+-
10 files changed, 110 insertions(+), 110 deletions(-)
diff --git a/cols.c b/cols.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include <assert.h> #include <limits.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> diff --git a/date.c b/date.c @@ -26,7 +26,7 @@ main(int argc, char *argv[]) t = time(NULL); ARGBEGIN { case 'd': - t = estrtonum(EARGF(usage()), 0, LLONG_MAX); + t = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, (time_t)-1)); break; case 'u': tztime = gmtime; diff --git a/du.c b/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; +} diff --git a/expand.c b/expand.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <limits.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -7,9 +8,9 @@ #include "utf.h" #include "util.h" -static int iflag = 0; -static size_t *tablist = NULL; -static size_t tablistlen = 0; +static int iflag = 0; +static size_t *tablist = NULL; +static size_t tablistlen = 0; static size_t parselist(const char *s) @@ -22,7 +23,7 @@ parselist(const char *s) if (*p == '\0') eprintf("empty field in tablist\n"); 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]) eprintf("tablist must be ascending\n"); } diff --git a/fold.c b/fold.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include <ctype.h> #include <limits.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -84,7 +85,7 @@ main(int argc, char *argv[]) sflag = 1; break; case 'w': - width = estrtonum(EARGF(usage()), 1, LLONG_MAX); + width = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX)); break; ARGNUM: width = ARGNUMF(); diff --git a/head.c b/head.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <limits.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -34,14 +35,14 @@ usage(void) int main(int argc, char *argv[]) { - long n = 10; + size_t n = 10; FILE *fp; int ret = 0; int newline, many; ARGBEGIN { case 'n': - n = estrtonum(EARGF(usage()), 0, LONG_MAX); + n = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX)); break; ARGNUM: n = ARGNUMF(); diff --git a/nl.c b/nl.c @@ -2,6 +2,7 @@ #include <limits.h> #include <regex.h> #include <stdio.h> +#include <stdint.h> #include <stdlib.h> #include <string.h> #include <unistd.h> @@ -9,12 +10,29 @@ #include "text.h" #include "util.h" -static void nl(const char *, FILE *); - -static char mode = 't'; +static char mode = 't'; static const char *sep = "\t"; -static long incr = 1; -static regex_t preg; +static size_t incr = 1; +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 usage(void) @@ -38,7 +56,7 @@ main(int argc, char *argv[]) usage(); break; case 'i': - incr = estrtonum(EARGF(usage()), 0, LONG_MAX); + incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX)); break; case 's': sep = EARGF(usage()); @@ -60,23 +78,3 @@ main(int argc, char *argv[]) } 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); -} diff --git a/seq.c b/seq.c @@ -110,7 +110,7 @@ digitsright(const char *d) int shift, after; 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; return MAX(0, after - shift); diff --git a/split.c b/split.c @@ -28,7 +28,7 @@ main(int argc, char *argv[]) char *prefix = "x"; char *file = NULL; char *tmp, *end; - uint64_t size = 1000, scale = 1, n; + size_t size = 1000, scale = 1, n; int always = 0; FILE *in = stdin, *out = NULL; @@ -55,7 +55,7 @@ main(int argc, char *argv[]) default: usage(); } - if (size > (UINT64_MAX/scale)) + if (size > (SIZE_MAX/scale)) eprintf("'%s': out of range\n", tmp); size *= scale; break; @@ -63,7 +63,7 @@ main(int argc, char *argv[]) always = 0; tmp = ARGF(); if (tmp) - size = estrtonum(tmp, 0, LLONG_MAX); + size = estrtonum(tmp, 0, MIN(LLONG_MAX, SIZE_MAX)); break; case 'a': slen = estrtonum(EARGF(usage()), 0, INT_MAX); diff --git a/touch.c b/touch.c @@ -65,7 +65,7 @@ main(int argc, char *argv[]) mflag = 1; break; case 't': - t = estrtonum(EARGF(usage()), 0, LLONG_MAX); + t = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, (time_t)-1)); break; default: usage();