sbase

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

commit ad6776e9a1bdecf49208d4299a60cee07b083416
parent b2e73936bfc9cb6400825437cbf848393180f561
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sat,  7 Mar 2015 15:39:39 +0100

grep, kill, renice, sort: style: put main at bottom

Diffstat:
Mgrep.c | 220++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mkill.c | 52++++++++++++++++++++++++++--------------------------
Mrenice.c | 78+++++++++++++++++++++++++++++++++++++++---------------------------------------
Msort.c | 166++++++++++++++++++++++++++++++++++++++++----------------------------------------
4 files changed, 258 insertions(+), 258 deletions(-)

diff --git a/grep.c b/grep.c @@ -37,116 +37,6 @@ struct pattern { static SLIST_HEAD(phead, pattern) phead; static void -usage(void) -{ - enprintf(Error, "usage: %s [-EFHchilnqsvwx] [-e pattern] [-f file] [pattern] [file ...]\n", argv0); -} - -int -main(int argc, char *argv[]) -{ - struct pattern *pnode; - int i, m, flags = REG_NOSUB, match = NoMatch; - FILE *fp; - char *arg; - - SLIST_INIT(&phead); - - ARGBEGIN { - case 'E': - Eflag = 1; - flags |= REG_EXTENDED; - break; - case 'F': - Fflag = 1; - break; - case 'H': - Hflag = 1; - hflag = 0; - break; - case 'e': - arg = EARGF(usage()); - fp = fmemopen(arg, strlen(arg) + 1, "r"); - addpatternfile(fp); - fclose(fp); - eflag = 1; - break; - case 'f': - arg = EARGF(usage()); - fp = fopen(arg, "r"); - if (!fp) - enprintf(Error, "fopen %s:", arg); - addpatternfile(fp); - fclose(fp); - fflag = 1; - break; - case 'h': - hflag = 1; - Hflag = 0; - break; - case 'c': - case 'l': - case 'n': - case 'q': - mode = ARGC(); - break; - case 'i': - flags |= REG_ICASE; - iflag = 1; - break; - case 's': - sflag = 1; - break; - case 'v': - vflag = 1; - break; - case 'w': - wflag = 1; - break; - case 'x': - xflag = 1; - break; - default: - usage(); - } ARGEND; - - if (argc == 0 && !eflag && !fflag) - usage(); /* no pattern */ - - /* just add literal pattern to list */ - if (!eflag && !fflag) { - fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r"); - addpatternfile(fp); - fclose(fp); - argc--; - argv++; - } - - if (!Fflag) - /* Compile regex for all search patterns */ - SLIST_FOREACH(pnode, &phead, entry) - enregcomp(Error, &pnode->preg, pnode->pattern, flags); - many = (argc > 1); - if (argc == 0) { - match = grep(stdin, "<stdin>"); - } else { - for (i = 0; i < argc; i++) { - if (!(fp = fopen(argv[i], "r"))) { - if (!sflag) - weprintf("fopen %s:", argv[i]); - match = Error; - continue; - } - m = grep(fp, argv[i]); - if (m == Error || (match != Error && m == Match)) - match = m; - fclose(fp); - } - } - return match; -} - -static void addpattern(const char *pattern) { struct pattern *pnode; @@ -269,3 +159,113 @@ end: } return match; } + +static void +usage(void) +{ + enprintf(Error, "usage: %s [-EFHchilnqsvwx] [-e pattern] [-f file] [pattern] [file ...]\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + struct pattern *pnode; + int i, m, flags = REG_NOSUB, match = NoMatch; + FILE *fp; + char *arg; + + SLIST_INIT(&phead); + + ARGBEGIN { + case 'E': + Eflag = 1; + flags |= REG_EXTENDED; + break; + case 'F': + Fflag = 1; + break; + case 'H': + Hflag = 1; + hflag = 0; + break; + case 'e': + arg = EARGF(usage()); + fp = fmemopen(arg, strlen(arg) + 1, "r"); + addpatternfile(fp); + fclose(fp); + eflag = 1; + break; + case 'f': + arg = EARGF(usage()); + fp = fopen(arg, "r"); + if (!fp) + enprintf(Error, "fopen %s:", arg); + addpatternfile(fp); + fclose(fp); + fflag = 1; + break; + case 'h': + hflag = 1; + Hflag = 0; + break; + case 'c': + case 'l': + case 'n': + case 'q': + mode = ARGC(); + break; + case 'i': + flags |= REG_ICASE; + iflag = 1; + break; + case 's': + sflag = 1; + break; + case 'v': + vflag = 1; + break; + case 'w': + wflag = 1; + break; + case 'x': + xflag = 1; + break; + default: + usage(); + } ARGEND; + + if (argc == 0 && !eflag && !fflag) + usage(); /* no pattern */ + + /* just add literal pattern to list */ + if (!eflag && !fflag) { + fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r"); + addpatternfile(fp); + fclose(fp); + argc--; + argv++; + } + + if (!Fflag) + /* Compile regex for all search patterns */ + SLIST_FOREACH(pnode, &phead, entry) + enregcomp(Error, &pnode->preg, pnode->pattern, flags); + many = (argc > 1); + if (argc == 0) { + match = grep(stdin, "<stdin>"); + } else { + for (i = 0; i < argc; i++) { + if (!(fp = fopen(argv[i], "r"))) { + if (!sflag) + weprintf("fopen %s:", argv[i]); + match = Error; + continue; + } + m = grep(fp, argv[i]); + if (m == Error || (match != Error && m == Match)) + match = m; + fclose(fp); + } + } + return match; +} diff --git a/kill.c b/kill.c @@ -26,6 +26,32 @@ struct { const char *sig2name(int); int name2sig(const char *); +const char * +sig2name(int sig) +{ + size_t i; + + for (i = 0; i < LEN(sigs); i++) + if (sigs[i].sig == sig) + return sigs[i].name; + eprintf("%d: bad signal number\n", sig); + /* unreachable */ + return NULL; +} + +int +name2sig(const char *name) +{ + size_t i; + + for (i = 0; i < LEN(sigs); i++) + if (strcasecmp(sigs[i].name, name) == 0) + return sigs[i].sig; + eprintf("%s: bad signal name\n", name); + /* unreachable */ + return -1; +} + static void usage(void) { @@ -110,29 +136,3 @@ main(int argc, char *argv[]) exit(ret); } - -const char * -sig2name(int sig) -{ - size_t i; - - for (i = 0; i < LEN(sigs); i++) - if (sigs[i].sig == sig) - return sigs[i].name; - eprintf("%d: bad signal number\n", sig); - /* unreachable */ - return NULL; -} - -int -name2sig(const char *name) -{ - size_t i; - - for (i = 0; i < LEN(sigs); i++) - if (strcasecmp(sigs[i].name, name) == 0) - return sigs[i].sig; - eprintf("%s: bad signal name\n", name); - /* unreachable */ - return -1; -} diff --git a/renice.c b/renice.c @@ -10,6 +10,45 @@ static int strtop(const char *); static int renice(int, int, long); +static int +strtop(const char *s) +{ + char *end; + long n; + + errno = 0; + n = strtol(s, &end, 10); + if (*end != '\0') { + weprintf("%s: not an integer\n", s); + return -1; + } + if (errno != 0 || n <= 0 || n > INT_MAX) { + weprintf("%s: invalid value\n", s); + return -1; + } + + return (int)n; +} + +static int +renice(int which, int who, long adj) +{ + errno = 0; + adj += getpriority(which, who); + if (errno != 0) { + weprintf("getpriority %d:", who); + return 0; + } + + adj = MAX(PRIO_MIN, MIN(adj, PRIO_MAX)); + if (setpriority(which, who, (int)adj) < 0) { + weprintf("setpriority %d:", who); + return 0; + } + + return 1; +} + static void usage(void) { @@ -71,42 +110,3 @@ main(int argc, char *argv[]) return status; } - -static int -strtop(const char *s) -{ - char *end; - long n; - - errno = 0; - n = strtol(s, &end, 10); - if (*end != '\0') { - weprintf("%s: not an integer\n", s); - return -1; - } - if (errno != 0 || n <= 0 || n > INT_MAX) { - weprintf("%s: invalid value\n", s); - return -1; - } - - return (int)n; -} - -static int -renice(int which, int who, long adj) -{ - errno = 0; - adj += getpriority(which, who); - if (errno != 0) { - weprintf("getpriority %d:", who); - return 0; - } - - adj = MAX(PRIO_MIN, MIN(adj, PRIO_MAX)); - if (setpriority(which, who, (int)adj) < 0) { - weprintf("setpriority %d:", who); - return 0; - } - - return 1; -} diff --git a/sort.c b/sort.c @@ -43,89 +43,6 @@ static int Cflag = 0, cflag = 0, uflag = 0; static char *fieldsep = NULL; static void -usage(void) -{ - enprintf(2, "usage: %s [-Cbcnru] [-t delim] [-k def]... [file...]\n", argv0); -} - -int -main(int argc, char *argv[]) -{ - size_t i; - FILE *fp; - struct linebuf linebuf = EMPTY_LINEBUF; - int global_flags = 0; - - ARGBEGIN { - case 'C': - Cflag = 1; - break; - case 'b': - global_flags |= MOD_STARTB | MOD_ENDB; - break; - case 'c': - cflag = 1; - break; - case 'k': - addkeydef(EARGF(usage()), global_flags); - break; - case 'n': - global_flags |= MOD_N; - break; - case 'r': - global_flags |= MOD_R; - break; - case 't': - fieldsep = EARGF(usage()); - if (strlen(fieldsep) != 1) - usage(); - break; - case 'u': - uflag = 1; - break; - default: - usage(); - } ARGEND; - - if (!head && global_flags) - addkeydef("1", global_flags); - addkeydef("1", global_flags & MOD_R); - - if (argc == 0) { - if (Cflag || cflag) { - check(stdin); - } else { - getlines(stdin, &linebuf); - } - } else for (; argc > 0; argc--, argv++) { - if (!(fp = fopen(argv[0], "r"))) { - enprintf(2, "fopen %s:", argv[0]); - continue; - } - if (Cflag || cflag) { - check(fp); - } else { - getlines(fp, &linebuf); - } - fclose(fp); - } - - if (!Cflag && !cflag) { - qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines, - (int (*)(const void *, const void *))linecmp); - - for (i = 0; i < linebuf.nlines; i++) { - if (!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i], - (const char **)&linebuf.lines[i-1])) { - fputs(linebuf.lines[i], stdout); - } - } - } - - return 0; -} - -static void addkeydef(char *def, int flags) { struct kdlist *node; @@ -303,3 +220,86 @@ columns(char *line, const struct keydef *kd) return enstrndup(2, start, end - start); } + +static void +usage(void) +{ + enprintf(2, "usage: %s [-Cbcnru] [-t delim] [-k def]... [file...]\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + size_t i; + FILE *fp; + struct linebuf linebuf = EMPTY_LINEBUF; + int global_flags = 0; + + ARGBEGIN { + case 'C': + Cflag = 1; + break; + case 'b': + global_flags |= MOD_STARTB | MOD_ENDB; + break; + case 'c': + cflag = 1; + break; + case 'k': + addkeydef(EARGF(usage()), global_flags); + break; + case 'n': + global_flags |= MOD_N; + break; + case 'r': + global_flags |= MOD_R; + break; + case 't': + fieldsep = EARGF(usage()); + if (strlen(fieldsep) != 1) + usage(); + break; + case 'u': + uflag = 1; + break; + default: + usage(); + } ARGEND; + + if (!head && global_flags) + addkeydef("1", global_flags); + addkeydef("1", global_flags & MOD_R); + + if (argc == 0) { + if (Cflag || cflag) { + check(stdin); + } else { + getlines(stdin, &linebuf); + } + } else for (; argc > 0; argc--, argv++) { + if (!(fp = fopen(argv[0], "r"))) { + enprintf(2, "fopen %s:", argv[0]); + continue; + } + if (Cflag || cflag) { + check(fp); + } else { + getlines(fp, &linebuf); + } + fclose(fp); + } + + if (!Cflag && !cflag) { + qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines, + (int (*)(const void *, const void *))linecmp); + + for (i = 0; i < linebuf.nlines; i++) { + if (!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i], + (const char **)&linebuf.lines[i-1])) { + fputs(linebuf.lines[i], stdout); + } + } + } + + return 0; +}