[hackers] [sbase] Convert split(1) to parseoffset() || FRIGN

From: <git_AT_suckless.org>
Date: Wed, 30 Sep 2015 20:44:18 +0200 (CEST)

commit 8125e8829d87e83cb93528d5338b2179f3c76517
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Wed Sep 30 19:21:06 2015 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Wed Sep 30 19:44:10 2015 +0100

    Convert split(1) to parseoffset()
    
    This drops a lot of duplicate code. Also simplify it in other
    parts of the main arg-handler.

diff --git a/split.c b/split.c
index c5bd82d..69a0ee6 100644
--- a/split.c
+++ b/split.c
_AT_@ -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':
_AT_@ -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;
_AT_@ -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();
Received on Wed Sep 30 2015 - 20:44:18 CEST

This archive was generated by hypermail 2.3.0 : Wed Sep 30 2015 - 20:48:39 CEST