From 009b55edc4bad5b46ace88f3c255bbff8671d137 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 26 Aug 2018 14:16:49 -0500 Subject: Convert more commands to the new option/GLOBALS code style. --- toys/posix/head.c | 8 ++++---- toys/posix/iconv.c | 11 +++++------ toys/posix/kill.c | 17 ++++++++--------- toys/posix/logger.c | 15 +++++++-------- toys/posix/ls.c | 7 +++---- toys/posix/mkdir.c | 9 ++++----- toys/posix/mkfifo.c | 4 ++-- toys/posix/nice.c | 6 +++--- toys/posix/nl.c | 10 +++------- toys/posix/od.c | 42 ++++++++++++++++++++---------------------- 10 files changed, 59 insertions(+), 70 deletions(-) (limited to 'toys') diff --git a/toys/posix/head.c b/toys/posix/head.c index ea4bbbc6..0cec4033 100644 --- a/toys/posix/head.c +++ b/toys/posix/head.c @@ -27,14 +27,14 @@ config HEAD #include "toys.h" GLOBALS( - long bytes; - long lines; + long c, n; + int file_no; ) static void do_head(int fd, char *name) { - int i, len, lines=TT.lines, bytes=TT.bytes; + int i, len, lines=TT.n, bytes=TT.c; if ((toys.optc > 1 && !(toys.optflags & FLAG_q)) || toys.optflags & FLAG_v) { // Print an extra newline for all but the first file @@ -65,7 +65,7 @@ void head_main(void) // handle old "-42" style arguments if (arg && *arg == '-' && arg[1]) { - TT.lines = atolx(arg+1); + TT.n = atolx(arg+1); toys.optc--; } else arg = 0; loopfiles(toys.optargs+!!arg, do_head); diff --git a/toys/posix/iconv.c b/toys/posix/iconv.c index a31d713b..e3cf7bae 100644 --- a/toys/posix/iconv.c +++ b/toys/posix/iconv.c @@ -27,8 +27,7 @@ config ICONV #include GLOBALS( - char *from; - char *to; + char *f, *t; void *ic; ) @@ -64,11 +63,11 @@ static void do_iconv(int fd, char *name) void iconv_main(void) { - if (!TT.to) TT.to = "utf8"; - if (!TT.from) TT.from = "utf8"; + if (!TT.t) TT.t = "utf8"; + if (!TT.f) TT.f = "utf8"; - if ((iconv_t)-1 == (TT.ic = iconv_open(TT.to, TT.from))) - perror_exit("%s/%s", TT.to, TT.from); + if ((iconv_t)-1 == (TT.ic = iconv_open(TT.t, TT.f))) + perror_exit("%s/%s", TT.t, TT.f); loopfiles(toys.optargs, do_iconv); if (CFG_TOYBOX_FREE) iconv_close(TT.ic); } diff --git a/toys/posix/kill.c b/toys/posix/kill.c index 3fcd36a1..3ea86e09 100644 --- a/toys/posix/kill.c +++ b/toys/posix/kill.c @@ -44,8 +44,8 @@ config KILLALL5 #include "toys.h" GLOBALS( - char *signame; - struct arg_list *olist; + char *s; + struct arg_list *o; ) // But kill's flags are a subset of killall5's @@ -74,12 +74,12 @@ void kill_main(void) // signal must come before pids, so "kill -9 -1" isn't confusing. - if (!TT.signame && *args && **args=='-') TT.signame=*(args++)+1; - if (TT.signame) { + if (!TT.s && *args && **args=='-') TT.s = *(args++)+1; + if (TT.s) { char *arg; - int i = strtol(TT.signame, &arg, 10); + int i = strtol(TT.s, &arg, 10); if (!*arg) arg = num_to_sig(i); - else arg = TT.signame; + else arg = TT.s; if (!arg || -1 == (signum = sig_to_num(arg))) error_exit("Unknown signal '%s'", arg); @@ -96,11 +96,10 @@ void kill_main(void) if (toys.optflags & FLAG_o) { struct arg_list *ptr; - for (ptr = TT.olist; ptr; ptr = ptr->next) ocount++; + for (ptr = TT.o; ptr; ptr = ptr->next) ocount++; olist = xmalloc(ocount*sizeof(long)); ocount = 0; - for (ptr = TT.olist; ptr; ptr=ptr->next) - olist[ocount++] = atolx(ptr->arg); + for (ptr = TT.o; ptr; ptr=ptr->next) olist[ocount++] = atolx(ptr->arg); } sid = getsid(pid = getpid()); diff --git a/toys/posix/logger.c b/toys/posix/logger.c index 47562999..bae07b4d 100644 --- a/toys/posix/logger.c +++ b/toys/posix/logger.c @@ -25,8 +25,7 @@ config LOGGER #include "toys.h" GLOBALS( - char *priority; - char *ident; + char *p, *t; ) // find str in names[], accepting unambiguous short matches @@ -59,18 +58,18 @@ void logger_main(void) *facilities[] = {"kern", "user", "mail", "daemon", "auth", "syslog", "lpr", "news", "uucp", "cron", "authpriv", "ftp"}; - if (!TT.ident) TT.ident = xstrdup(xgetpwuid(geteuid())->pw_name); + if (!TT.t) TT.t = xstrdup(xgetpwuid(geteuid())->pw_name); if (toys.optflags & FLAG_p) { - if (!(s1 = strchr(TT.priority, '.'))) s1 = TT.priority; + if (!(s1 = strchr(TT.p, '.'))) s1 = TT.p; else { *s1++ = len = 0; - facility = arrayfind(TT.priority, facilities, ARRAY_LEN(facilities)); - if (facility == -1 && strncasecmp(TT.priority, "local", 5)) { + facility = arrayfind(TT.p, facilities, ARRAY_LEN(facilities)); + if (facility == -1 && strncasecmp(TT.p, "local", 5)) { facility = s1[5]-'0'; if (facility>7 || s1[6]) facility = -1; if (facility>=0) facility += 16; } - if (facility<0) error_exit("bad facility: %s", TT.priority); + if (facility<0) error_exit("bad facility: %s", TT.p); facility *= 8; } @@ -90,7 +89,7 @@ void logger_main(void) s1 = toybuf; } - openlog(TT.ident, LOG_PERROR*!!(toys.optflags&FLAG_s), facility); + openlog(TT.t, LOG_PERROR*!!(toys.optflags&FLAG_s), facility); syslog(priority, "%s", s1); closelog(); } diff --git a/toys/posix/ls.c b/toys/posix/ls.c index ec8c6c0d..976b3343 100644 --- a/toys/posix/ls.c +++ b/toys/posix/ls.c @@ -54,11 +54,10 @@ config LS // ls -lR starts .: then ./subdir: GLOBALS( - long ll; + long l; char *color; struct dirtree *files, *singledir; - unsigned screen_width; int nl_title; char *escmore; @@ -467,7 +466,7 @@ static void listfiles(int dirfd, struct dirtree *indir) // print time, always in --time-style=long-iso tm = localtime(&(st->st_mtime)); strftime(tmp, sizeof(tmp), "%F %H:%M", tm); - if (TT.ll>1) { + if (TT.l>1) { char *s = tmp+strlen(tmp); s += sprintf(s, ":%02d.%09d ", tm->tm_sec, (int)st->st_mtim.tv_nsec); @@ -533,7 +532,7 @@ void ls_main(void) if (toys.optflags&FLAG_full_time) { toys.optflags |= FLAG_l; - TT.ll = 2; + TT.l = 2; } // Do we have an implied -1 diff --git a/toys/posix/mkdir.c b/toys/posix/mkdir.c index e88a2988..05eac0ba 100644 --- a/toys/posix/mkdir.c +++ b/toys/posix/mkdir.c @@ -32,8 +32,7 @@ config MKDIR_Z #include "toys.h" GLOBALS( - char *arg_mode; - char *arg_context; + char *m, *Z; ) void mkdir_main(void) @@ -42,10 +41,10 @@ void mkdir_main(void) mode_t mode = (0777&~toys.old_umask); if (CFG_MKDIR_Z && (toys.optflags&FLAG_Z)) - if (0>lsm_set_create(TT.arg_context)) - perror_exit("-Z '%s' failed", TT.arg_context); + if (0>lsm_set_create(TT.Z)) + perror_exit("-Z '%s' failed", TT.Z); - if (TT.arg_mode) mode = string_to_mode(TT.arg_mode, 0777); + if (TT.m) mode = string_to_mode(TT.m, 0777); // Note, -p and -v flags line up with mkpathat() flags for (s=toys.optargs; *s; s++) { diff --git a/toys/posix/mkfifo.c b/toys/posix/mkfifo.c index 942dfdc5..8639ebd8 100644 --- a/toys/posix/mkfifo.c +++ b/toys/posix/mkfifo.c @@ -28,7 +28,7 @@ config MKFIFO_Z #include "toys.h" GLOBALS( - char *m_string; + char *m; char *Z; mode_t mode; @@ -39,7 +39,7 @@ void mkfifo_main(void) char **s; TT.mode = 0666; - if (toys.optflags & FLAG_m) TT.mode = string_to_mode(TT.m_string, 0); + if (toys.optflags & FLAG_m) TT.mode = string_to_mode(TT.m, 0); if (CFG_MKFIFO_Z && (toys.optflags&FLAG_Z)) if (0>lsm_set_create(TT.Z)) diff --git a/toys/posix/nice.c b/toys/posix/nice.c index 4b587ee6..57feffe1 100644 --- a/toys/posix/nice.c +++ b/toys/posix/nice.c @@ -24,15 +24,15 @@ config NICE #include "toys.h" GLOBALS( - long priority; + long n; ) void nice_main(void) { - if (!toys.optflags) TT.priority = 10; + if (!toys.optflags) TT.n = 10; errno = 0; - if (nice(TT.priority)==-1 && errno) perror_exit("Can't set priority"); + if (nice(TT.n)==-1 && errno) perror_exit("Can't set priority"); xexec(toys.optargs); } diff --git a/toys/posix/nl.c b/toys/posix/nl.c index 9b02bfaa..32848332 100644 --- a/toys/posix/nl.c +++ b/toys/posix/nl.c @@ -7,7 +7,7 @@ * This implements a subset: only one logical page (-ip), no sections (-dfh). * todo: -lv -USE_NL(NEWTOY(nl, "v#<1=1l#b:n:s:w#<0=6E", TOYFLAG_BIN)) +USE_NL(NEWTOY(nl, "v#<1=1l#w#<0=6Eb:n:s:", TOYFLAG_BIN)) config NL bool "nl" @@ -29,12 +29,8 @@ config NL #include "toys.h" GLOBALS( - long w; - char *s; - char *n; - char *b; - long l; - long v; + char *s, *n, *b; + long w, l, v; // Count of consecutive blank lines for -l has to persist between files long lcount; diff --git a/toys/posix/od.c b/toys/posix/od.c index ea7f6ac5..c9c5c486 100644 --- a/toys/posix/od.c +++ b/toys/posix/od.c @@ -27,11 +27,9 @@ config OD #include "toys.h" GLOBALS( - struct arg_list *output_base; - char *address_base; - long max_count; - long width; - long jump_bytes; + struct arg_list *t; + char *A; + long N, w, j; int address_idx; unsigned types, leftover, star; @@ -131,11 +129,11 @@ static void od_outline(void) struct odtype *types = (struct odtype *)toybuf; int i, j, len, pad; - if (TT.leftover TT.pos) { - off_t pos = TT.jump_bytes-TT.pos, off = lskip(fd, pos); + if (TT.j > TT.pos) { + off_t pos = TT.j-TT.pos, off = lskip(fd, pos); if (off >= 0) TT.pos += pos-off; - if (TT.jump_bytes > TT.pos) return; + if (TT.j > TT.pos) return; } for(;;) { char *buf = TT.buf + TT.leftover; - int len = TT.width - TT.leftover; + int len = TT.w - TT.leftover; if (toys.optflags & FLAG_N) { - if (!TT.max_count) break; - if (TT.max_count < len) len = TT.max_count; + if (!TT.N) break; + if (TT.N < len) len = TT.N; } len = readall(fd, buf, len); @@ -211,9 +209,9 @@ static void do_od(int fd, char *name) perror_msg_raw(name); break; } - if (TT.max_count) TT.max_count -= len; + if (TT.N) TT.N -= len; TT.leftover += len; - if (TT.leftover < TT.width) break; + if (TT.leftover < TT.w) break; od_outline(); } @@ -265,17 +263,17 @@ void od_main(void) { struct arg_list *arg; - TT.bufs[0] = xzalloc(TT.width); - TT.bufs[1] = xzalloc(TT.width); + TT.bufs[0] = xzalloc(TT.w); + TT.bufs[1] = xzalloc(TT.w); TT.buf = TT.bufs[0]; - if (!TT.address_base) TT.address_idx = 2; - else if (0>(TT.address_idx = stridx("ndox", *TT.address_base))) - error_exit("bad -A '%c'", *TT.address_base); + if (!TT.A) TT.address_idx = 2; + else if (0>(TT.address_idx = stridx("ndox", *TT.A))) + error_exit("bad -A '%c'", *TT.A); // Collect -t entries - for (arg = TT.output_base; arg; arg = arg->next) append_base(arg->arg); + for (arg = TT.t; arg; arg = arg->next) append_base(arg->arg); if (toys.optflags & FLAG_b) append_base("o1"); if (toys.optflags & FLAG_c) append_base("c"); if (toys.optflags & FLAG_d) append_base("u2"); -- cgit v1.2.3