Convert estrto{l, ul} to estrtonum

Enough with this insanity!
This commit is contained in:
FRIGN 2015-01-30 16:52:44 +01:00
parent 8c359daee3
commit fd562481f3
19 changed files with 30 additions and 81 deletions

View File

@ -39,8 +39,6 @@ LIBUTILSRC =\
libutil/eprintf.c\
libutil/eregcomp.c\
libutil/estrtod.c\
libutil/estrtol.c\
libutil/estrtoul.c\
libutil/fnck.c\
libutil/getlines.c\
libutil/human.c\

2
arg.h
View File

@ -46,7 +46,7 @@ extern char *argv0;
#define ARGC() argc_
#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base)))
#define ARGNUMF(base) (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
((x), abort(), (char *)0) :\

5
cols.c
View File

@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -35,9 +36,7 @@ main(int argc, char *argv[])
ARGBEGIN {
case 'c':
cflag = 1;
chars = estrtol(EARGF(usage()), 0);
if (chars < 3)
eprintf("%d: too few character columns");
chars = estrtonum(EARGF(usage()), 3, LONG_MAX);
break;
default:
usage();

3
date.c
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -25,7 +26,7 @@ main(int argc, char *argv[])
t = time(NULL);
ARGBEGIN {
case 'd':
t = estrtol(EARGF(usage()), 0);
t = estrtonum(EARGF(usage()), 0, LLONG_MAX);
break;
case 'u':
tztime = gmtime;

4
du.c
View File

@ -53,7 +53,7 @@ main(int argc, char *argv[])
break;
case 'd':
dflag = 1;
depth = estrtol(EARGF(usage()), 0);
depth = estrtonum(EARGF(usage()), 0, LONG_MAX);
break;
case 's':
sflag = 1;
@ -73,7 +73,7 @@ main(int argc, char *argv[])
bsize = getenv("BLOCKSIZE");
if (bsize)
blksize = estrtol(bsize, 0);
blksize = estrtonum(bsize, 0, LONG_MAX);
if (kflag)
blksize = 1024;

View File

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

5
fold.c
View File

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

3
head.c
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -40,7 +41,7 @@ main(int argc, char *argv[])
ARGBEGIN {
case 'n':
n = estrtol(EARGF(usage()), 0);
n = estrtonum(EARGF(usage()), 0, LONG_MAX);
break;
ARGNUM:
n = ARGNUMF(0);

View File

@ -1,27 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "../util.h"
long
estrtol(const char *s, int base)
{
char *end;
long n;
errno = 0;
n = strtol(s, &end, base);
if (*end != '\0') {
if (base == 0)
eprintf("%s: not an integer\n", s);
else
eprintf("%s: not a base %d integer\n", s, base);
}
if (errno != 0)
eprintf("%s:", s);
return n;
}

View File

@ -1,26 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "../util.h"
unsigned long
estrtoul(const char *s, int base)
{
char *end;
unsigned long n;
errno = 0;
n = strtoul(s, &end, base);
if (*end != '\0') {
if (base == 0)
eprintf("%s: not an integer\n", s);
else
eprintf("%s: not a base %d integer\n", s, base);
}
if (errno != 0)
eprintf("%s:", s);
return n;
}

4
nice.c
View File

@ -12,7 +12,7 @@
static void
usage(void)
{
eprintf("usage: nice [-n inc] cmd [arg ...]\n");
eprintf("usage: %s [-n inc] cmd [arg ...]\n", argv0);
}
int
@ -23,7 +23,7 @@ main(int argc, char *argv[])
ARGBEGIN {
case 'n':
val = estrtol(EARGF(usage()), 10);
val = estrtonum(EARGF(usage()), PRIO_MIN, PRIO_MAX);
break;
default:
usage();

3
nl.c
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
@ -37,7 +38,7 @@ main(int argc, char *argv[])
usage();
break;
case 'i':
incr = estrtol(EARGF(usage()), 0);
incr = estrtonum(EARGF(usage()), 0, LONG_MAX);
break;
case 's':
sep = EARGF(usage());

View File

@ -48,7 +48,7 @@ main(int argc, char *argv[])
if (argc == 0 || !adj)
usage();
val = estrtol(adj, 10);
val = estrtonum(adj, PRIO_MIN, PRIO_MAX);
for (i = 0; i < argc; i++) {
who = -1;
if (which == PRIO_USER) {

5
seq.c
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -97,7 +98,7 @@ digitsleft(const char *d)
if (*d == '+')
d++;
exp = strpbrk(d, "eE");
shift = exp ? estrtol(&exp[1], 10) : 0;
shift = exp ? estrtonum(&exp[1], -INT_MAX, INT_MAX) : 0;
return MAX(0, strspn(d, "-0123456789") + shift);
}
@ -109,7 +110,7 @@ digitsright(const char *d)
int shift, after;
exp = strpbrk(d, "eE");
shift = exp ? estrtol(&exp[1], 10) : 0;
shift = exp ? estrtonum(&exp[1], -INT_MAX, INT_MAX) : 0;
after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0;
return MAX(0, after - shift);

View File

@ -63,10 +63,10 @@ main(int argc, char *argv[])
always = 0;
tmp = ARGF();
if (tmp)
size = estrtol(tmp, 10);
size = estrtonum(tmp, 0, LLONG_MAX);
break;
case 'a':
slen = estrtol(EARGF(usage()), 10);
slen = estrtonum(EARGF(usage()), 0, INT_MAX);
break;
case 'd':
base = 10;

3
tail.c
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -29,7 +30,7 @@ main(int argc, char *argv[])
ARGBEGIN {
case 'n':
lines = EARGF(usage());
n = abs(estrtol(lines, 0));
n = estrtonum(lines, 0, LONG_MAX);
if (lines[0] == '+')
tail = dropinit;
break;

View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
@ -64,7 +65,7 @@ main(int argc, char *argv[])
mflag = 1;
break;
case 't':
t = estrtol(EARGF(usage()), 0);
t = estrtonum(EARGF(usage()), 0, LLONG_MAX);
break;
default:
usage();

View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
@ -25,7 +26,7 @@ main(int argc, char *argv[])
ARGBEGIN {
case 't':
tabsize = estrtol(EARGF(usage()), 0);
tabsize = estrtonum(EARGF(usage()), 0, INT_MAX);
if (tabsize <= 0)
eprintf("unexpand: invalid tabsize\n");
/* Fallthrough: -t implies -a */

2
util.h
View File

@ -31,8 +31,6 @@ void eprintf(const char *, ...);
void weprintf(const char *, ...);
double estrtod(const char *);
long estrtol(const char *, int);
unsigned long estrtoul(const char *, int);
#undef strcasestr
char *strcasestr(const char *, const char *);