[hackers] [sbase] seq, xargs: style: put main at bottom || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Sun, 8 Mar 2015 12:50:38 +0100 (CET)

commit b1036d8f1d68c5533561dbaa32e476ca228e3829
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sat Mar 7 13:36:40 2015 +0100

    seq, xargs: style: put main at bottom

diff --git a/seq.c b/seq.c
index 45bcae4..27fe7e1 100644
--- a/seq.c
+++ b/seq.c
_AT_@ -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)
_AT_@ -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
index 2d8f12b..9245627 100644
--- a/xargs.c
+++ b/xargs.c
_AT_@ -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)
 {
_AT_@ -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;
+}
Received on Sun Mar 08 2015 - 12:50:38 CET

This archive was generated by hypermail 2.3.0 : Sun Mar 08 2015 - 13:00:14 CET