sbase

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

commit 37eaf92a7dc19a7148843411fada7021e6507caa
parent 0c17cd0fb39c47e2b9c8759dc23eef2d9e3bdfaa
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sat,  7 Mar 2015 13:36:40 +0100

seq, xargs: style: put main at bottom

Diffstat:
seq.c | 130++++++++++++++++++++++++++++++++++++++-----------------------------------------
xargs.c | 160++++++++++++++++++++++++++++++++++++++++----------------------------------------
2 files changed, 143 insertions(+), 147 deletions(-)

diff --git a/seq.c b/seq.c @@ -5,9 +5,69 @@ #include "util.h" -static int digitsleft(const char *); -static int digitsright(const char *); -static int validfmt(const char *); +static int +digitsleft(const char *d) +{ + char *exp; + int shift; + + if (*d == '+') + d++; + exp = strpbrk(d, "eE"); + shift = exp ? estrtonum(&exp[1], INT_MIN, INT_MAX) : 0; + + return MAX(0, strspn(d, "-0123456789") + shift); +} + +static int +digitsright(const char *d) +{ + char *exp; + int shift, after; + + exp = strpbrk(d, "eE"); + shift = exp ? estrtonum(&exp[1], INT_MIN, INT_MAX) : 0; + after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0; + + return MAX(0, after - shift); +} + +static int +validfmt(const char *fmt) +{ + int occur = 0; + +literal: + while (*fmt) + if (*fmt++ == '%') + goto format; + return occur == 1; + +format: + if (*fmt == '%') { + fmt++; + goto literal; + } + fmt += strspn(fmt, "-+#0 '"); + fmt += strspn(fmt, "0123456789"); + if (*fmt == '.') { + fmt++; + fmt += strspn(fmt, "0123456789"); + } + if (*fmt == 'L') + fmt++; + + switch (*fmt) { + case 'f': case 'F': + case 'g': case 'G': + case 'e': case 'E': + case 'a': case 'A': + occur++; + goto literal; + default: + return 0; + } +} static void usage(void) @@ -86,67 +146,3 @@ main(int argc, char *argv[]) return 0; } - -static int -digitsleft(const char *d) -{ - char *exp; - int shift; - - if (*d == '+') - d++; - exp = strpbrk(d, "eE"); - shift = exp ? estrtonum(&exp[1], INT_MIN, INT_MAX) : 0; - - return MAX(0, strspn(d, "-0123456789") + shift); -} - -static int -digitsright(const char *d) -{ - char *exp; - int shift, after; - - exp = strpbrk(d, "eE"); - shift = exp ? estrtonum(&exp[1], INT_MIN, INT_MAX) : 0; - after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0; - - return MAX(0, after - shift); -} - -static int -validfmt(const char *fmt) -{ - int occur = 0; - -literal: - while (*fmt) - if (*fmt++ == '%') - goto format; - return occur == 1; - -format: - if (*fmt == '%') { - fmt++; - goto literal; - } - fmt += strspn(fmt, "-+#0 '"); - fmt += strspn(fmt, "0123456789"); - if (*fmt == '.') { - fmt++; - fmt += strspn(fmt, "0123456789"); - } - if (*fmt == 'L') - fmt++; - - switch (*fmt) { - case 'f': case 'F': - case 'g': case 'G': - case 'e': case 'E': - case 'a': case 'A': - occur++; - goto literal; - default: - return 0; - } -} diff --git a/xargs.c b/xargs.c @@ -32,86 +32,6 @@ static int nerrors = 0; static char *eofstr; static int rflag = 0, nflag = 0; -static void -usage(void) -{ - eprintf("usage: %s [-n maxargs] [-r] [-E eofstr] [cmd [arg...]]\n", argv0); -} - -int -main(int argc, char *argv[]) -{ - int leftover = 0; - long argsz, argmaxsz; - char *arg = ""; - int i, a; - - ARGBEGIN { - case 'n': - nflag = 1; - if ((maxargs = strtol(EARGF(usage()), NULL, 10)) <= 0) - eprintf("%s: value for -n option should be >= 1\n", argv0); - break; - case 'r': - rflag = 1; - break; - case 'E': - eofstr = EARGF(usage()); - break; - default: - usage(); - } ARGEND; - - argmaxsz = sysconf(_SC_ARG_MAX); - if (argmaxsz < 0) - eprintf("sysconf:"); - /* Leave some room for environment variables */ - argmaxsz -= 4 * 1024; - - do { - argsz = 0; i = 0; a = 0; - if (argc > 0) { - for (; i < argc; i++) { - cmd[i] = estrdup(argv[i]); - argsz += strlen(cmd[i]) + 1; - } - } else { - cmd[i] = estrdup("/bin/echo"); - argsz += strlen(cmd[i]) + 1; - i++; - } - while (leftover == 1 || (arg = poparg())) { - if (argsz + strlen(arg) + 1 > argmaxsz || - i >= NARGS - 1) { - if (strlen(arg) + 1 > argmaxsz) - eprintf("insufficient argument space\n"); - leftover = 1; - break; - } - cmd[i] = estrdup(arg); - argsz += strlen(cmd[i]) + 1; - i++; - a++; - leftover = 0; - if (nflag == 1 && a >= maxargs) - break; - } - cmd[i] = NULL; - if (a >= maxargs && nflag == 1) - spawn(); - else if (!a || (i == 1 && rflag == 1)) - ; - else - spawn(); - for (; i >= 0; i--) - free(cmd[i]); - } while (arg); - - free(argb); - - return nerrors > 0 ? 123 : 0; -} - static int inputc(void) { @@ -258,3 +178,83 @@ spawn(void) } waitchld(); } + +static void +usage(void) +{ + eprintf("usage: %s [-n maxargs] [-r] [-E eofstr] [cmd [arg...]]\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + int leftover = 0; + long argsz, argmaxsz; + char *arg = ""; + int i, a; + + ARGBEGIN { + case 'n': + nflag = 1; + if ((maxargs = strtol(EARGF(usage()), NULL, 10)) <= 0) + eprintf("%s: value for -n option should be >= 1\n", argv0); + break; + case 'r': + rflag = 1; + break; + case 'E': + eofstr = EARGF(usage()); + break; + default: + usage(); + } ARGEND; + + argmaxsz = sysconf(_SC_ARG_MAX); + if (argmaxsz < 0) + eprintf("sysconf:"); + /* Leave some room for environment variables */ + argmaxsz -= 4 * 1024; + + do { + argsz = 0; i = 0; a = 0; + if (argc > 0) { + for (; i < argc; i++) { + cmd[i] = estrdup(argv[i]); + argsz += strlen(cmd[i]) + 1; + } + } else { + cmd[i] = estrdup("/bin/echo"); + argsz += strlen(cmd[i]) + 1; + i++; + } + while (leftover == 1 || (arg = poparg())) { + if (argsz + strlen(arg) + 1 > argmaxsz || + i >= NARGS - 1) { + if (strlen(arg) + 1 > argmaxsz) + eprintf("insufficient argument space\n"); + leftover = 1; + break; + } + cmd[i] = estrdup(arg); + argsz += strlen(cmd[i]) + 1; + i++; + a++; + leftover = 0; + if (nflag == 1 && a >= maxargs) + break; + } + cmd[i] = NULL; + if (a >= maxargs && nflag == 1) + spawn(); + else if (!a || (i == 1 && rflag == 1)) + ; + else + spawn(); + for (; i >= 0; i--) + free(cmd[i]); + } while (arg); + + free(argb); + + return nerrors > 0 ? 123 : 0; +}