sbase

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

commit 3fe111e5d5e0b1d83a9803a76de6006660d674bc
parent c25e7897485c4805f5638875e4e5c832634bc8cb
Author: FRIGN <dev@frign.de>
Date:   Wed Sep 30 19:21:06 +0200

Convert split(1) to parseoffset()

This drops a lot of duplicate code. Also simplify it in other
parts of the main arg-handler.

Diffstat:
split.c | 33+++++----------------------------
1 file changed, 5 insertions(+), 28 deletions(-)
diff --git a/split.c b/split.c @@ -48,10 +48,9 @@ int main(int argc, char *argv[]) { FILE *in = stdin, *out = NULL; - size_t size = 1000, scale = 1, n; - long l; + size_t size = 1000, n; 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 { case 'a': @@ -59,29 +58,8 @@ main(int argc, char *argv[]) break; case 'b': always = 1; - tmp = EARGF(usage()); - l = strtol(tmp, &end, 10); - 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; + if ((size = parseoffset(EARGF(usage()))) < 0) + return 1; break; case 'd': base = 10; @@ -89,8 +67,7 @@ main(int argc, char *argv[]) break; case 'l': always = 0; - tmp = EARGF(usage()); - size = estrtonum(tmp, 1, MIN(LLONG_MAX, SIZE_MAX)); + size = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX)); break; default: usage();