Convert split(1) to parseoffset()
This drops a lot of duplicate code. Also simplify it in other parts of the main arg-handler.
This commit is contained in:
parent
870a75076d
commit
8125e8829d
33
split.c
33
split.c
|
@ -48,10 +48,9 @@ int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
FILE *in = stdin, *out = NULL;
|
FILE *in = stdin, *out = NULL;
|
||||||
size_t size = 1000, scale = 1, n;
|
size_t size = 1000, n;
|
||||||
long l;
|
|
||||||
int ret = 0, ch, plen, slen = 2, always = 0;
|
int ret = 0, ch, plen, slen = 2, always = 0;
|
||||||
char name[NAME_MAX + 1], *prefix = "x", *file = NULL, *tmp, *end;
|
char name[NAME_MAX + 1], *prefix = "x", *file = NULL;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'a':
|
case 'a':
|
||||||
|
@ -59,29 +58,8 @@ main(int argc, char *argv[])
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
always = 1;
|
always = 1;
|
||||||
tmp = EARGF(usage());
|
if ((size = parseoffset(EARGF(usage()))) < 0)
|
||||||
l = strtol(tmp, &end, 10);
|
return 1;
|
||||||
if (l <= 0)
|
|
||||||
eprintf("invalid number of bytes: %s\n", tmp);
|
|
||||||
size = (size_t)l;
|
|
||||||
if (!*end)
|
|
||||||
break;
|
|
||||||
switch (toupper((int)*end)) {
|
|
||||||
case 'K':
|
|
||||||
scale = 1024;
|
|
||||||
break;
|
|
||||||
case 'M':
|
|
||||||
scale = 1024L * 1024L;
|
|
||||||
break;
|
|
||||||
case 'G':
|
|
||||||
scale = 1024L * 1024L * 1024L;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
usage();
|
|
||||||
}
|
|
||||||
if (size > (SIZE_MAX / scale))
|
|
||||||
eprintf("'%s': out of range\n", tmp);
|
|
||||||
size *= scale;
|
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
base = 10;
|
base = 10;
|
||||||
|
@ -89,8 +67,7 @@ main(int argc, char *argv[])
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
always = 0;
|
always = 0;
|
||||||
tmp = EARGF(usage());
|
size = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
|
||||||
size = estrtonum(tmp, 1, MIN(LLONG_MAX, SIZE_MAX));
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user