From b367482e9c102f49001a7e2d1ddb048bdce8b06e Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 3 Mar 2023 15:21:06 -0600 Subject: [PATCH] Change FLAG(x) macros to always return 0 or 1. Treewide audit of FLAG() users to make sure nobody NEEDED FLAG(x) to return the masked bit value, and to remove now-redundant !!, with a number of in-passing cleanups while I was there. I think I fixed an actual bug in patch.c (-R depended on the flag value but commit 6f6b7614e463 changed it, so "+-"[FLAG(R)] was comparing against the NUL terminator when measuring fuzz) but we didn't have a test for "fuzz autodetection". And there was another bug in tar.c where DIRTREE_BREADTH was assigned to a local variable... and then not used. But the test passed? (Not sure the flag's needed at the top level, added a comment to the test suite to remind me to revisit that.) While I was there, capitalize TODO comments so they're easier to grep for. --- lib/deflate.c | 2 +- tests/tar.test | 4 ++-- toys.h | 2 +- toys/lsb/dmesg.c | 4 ++-- toys/lsb/gzip.c | 4 ++-- toys/lsb/hostname.c | 2 +- toys/lsb/killall.c | 1 + toys/lsb/md5sum.c | 2 +- toys/lsb/mktemp.c | 2 +- toys/lsb/umount.c | 2 +- toys/net/netcat.c | 4 ++-- toys/net/tunctl.c | 2 +- toys/net/wget.c | 10 +++++----- toys/other/base64.c | 2 +- toys/other/gpiod.c | 1 + toys/other/help.c | 2 +- toys/other/hwclock.c | 6 +++--- toys/other/login.c | 6 +++--- toys/other/lsattr.c | 6 ++++-- toys/other/nbd_server.c | 2 +- toys/other/pmap.c | 6 +++--- toys/other/readelf.c | 6 ++++-- toys/other/sha3sum.c | 4 +--- toys/other/stat.c | 12 ++++++------ toys/posix/cat.c | 2 +- toys/posix/chgrp.c | 4 ++-- toys/posix/cksum.c | 2 +- toys/posix/cmp.c | 4 ++-- toys/posix/cp.c | 18 ++++++++---------- toys/posix/cpio.c | 3 +-- toys/posix/cut.c | 2 +- toys/posix/df.c | 2 +- toys/posix/du.c | 5 ++--- toys/posix/grep.c | 5 ++--- toys/posix/ls.c | 12 +++++------- toys/posix/patch.c | 20 ++++++++++---------- toys/posix/printf.c | 2 +- toys/posix/ps.c | 14 ++++++-------- toys/posix/sed.c | 4 ++-- toys/posix/sort.c | 2 +- toys/posix/tail.c | 2 +- toys/posix/tar.c | 14 +++++++------- toys/posix/time.c | 2 +- toys/posix/touch.c | 1 - toys/posix/uuencode.c | 13 +++++++------ 45 files changed, 111 insertions(+), 116 deletions(-) diff --git a/lib/deflate.c b/lib/deflate.c index b21a0e54..06d61e97 100644 --- a/lib/deflate.c +++ b/lib/deflate.c @@ -331,7 +331,7 @@ static void deflate(struct deflate *dd, struct bitbuf *bb) while (!final) { // Read next half-window of data if we haven't hit EOF yet. len = readall(dd->infd, data+(dd->len&32768), 32768); - if (len < 0) perror_exit("read"); // todo: add filename + if (len < 0) perror_exit("read"); // TODO: add filename if (len != 32768) final++; if (dd->crcfunc) dd->crcfunc(dd, data+(dd->len&32768), len); // dd->len += len; crcfunc advances len TODO diff --git a/tests/tar.test b/tests/tar.test index 3c70b47d..fb26991f 100755 --- a/tests/tar.test +++ b/tests/tar.test @@ -407,11 +407,11 @@ testing 'creation --wildcards --exclude'\ '$TAR -C sub --wildcards --exclude=d?bc abcd dabc | LL' \ 'abcd\n' '' '' +# TODO: do we need to set DIRTREE_BREADTH at top level? Come up with test if so. mkdir sub2 touch sub2/{ephebe,klatch,djelibeybi} testing 'tsort' '$TAR -c sub2 --sort=name | tar t' \ - 'sub2/\nsub2/djelibeybi\nsub2/ephebe\nsub2/klatch\n' \ - '' '' + 'sub2/\nsub2/djelibeybi\nsub2/ephebe\nsub2/klatch\n' '' '' if false then diff --git a/toys.h b/toys.h index 8e1ae9e8..d0ec2fd4 100644 --- a/toys.h +++ b/toys.h @@ -124,7 +124,7 @@ extern struct toy_context { extern char **environ, *toybox_version, toybuf[4096], libbuf[4096]; -#define FLAG(x) (toys.optflags&FLAG_##x) +#define FLAG(x) (!!(toys.optflags&FLAG_##x)) // Return 1 if flag set, 0 if not #define GLOBALS(...) #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array)) diff --git a/toys/lsb/dmesg.c b/toys/lsb/dmesg.c index a935f700..5ce590c8 100644 --- a/toys/lsb/dmesg.c +++ b/toys/lsb/dmesg.c @@ -136,7 +136,7 @@ void dmesg_main(void) // Each read returns one message. By default, we block when there are no // more messages (--follow); O_NONBLOCK is needed for for usual behavior. - fd = open("/dev/kmsg", O_RDONLY|(O_NONBLOCK*!FLAG(w))); + fd = open("/dev/kmsg", O_RDONLY|O_NONBLOCK*!FLAG(w)); if (fd == -1) goto klogctl_mode; // SYSLOG_ACTION_CLEAR(5) doesn't actually remove anything from /dev/kmsg, @@ -163,7 +163,7 @@ klogctl_mode: // Figure out how much data we need, and fetch it. if (!(size = TT.s)) size = xklogctl(10, 0, 0); data = from = xmalloc(size+1); - data[size = xklogctl(3+!!FLAG(c), data, size)] = 0; + data[size = xklogctl(3+FLAG(c), data, size)] = 0; // Send each line to format_message. to = data + size; diff --git a/toys/lsb/gzip.c b/toys/lsb/gzip.c index d519553a..6f6bfe5b 100644 --- a/toys/lsb/gzip.c +++ b/toys/lsb/gzip.c @@ -5,7 +5,7 @@ * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/gzip.html * GZIP RFC: http://www.ietf.org/rfc/rfc1952.txt * - * todo: qtv --rsyncable + * TODO: qtv --rsyncable // gzip.net version allows all options for all commands. USE_GZIP(NEWTOY(gzip, "n(no-name)cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN)) @@ -134,7 +134,7 @@ static void do_gzip(int ifd, char *in) else if ((out = strend(in, ".gz"))>in) out = xstrndup(in, out-in); else return error_msg("no .gz: %s", in); - ofd = xcreate(out, O_CREAT|O_WRONLY|WARN_ONLY|(O_EXCL*!FLAG(f)),sb.st_mode); + ofd = xcreate(out, O_CREAT|O_WRONLY|WARN_ONLY|O_EXCL*!FLAG(f), sb.st_mode); if (ofd == -1) return; } diff --git a/toys/lsb/hostname.c b/toys/lsb/hostname.c index daa60cc0..3c15b869 100644 --- a/toys/lsb/hostname.c +++ b/toys/lsb/hostname.c @@ -66,7 +66,7 @@ void hostname_main(void) snprintf(toybuf, sizeof(toybuf), "%s", h->h_name); } dot = toybuf+strcspn(toybuf, "."); - if (FLAG(s)) *dot = '\0'; + if (FLAG(s)) *dot = 0; xputs(FLAG(d) ? dot+1 : toybuf); } diff --git a/toys/lsb/killall.c b/toys/lsb/killall.c index d3546a05..d2a5dd20 100644 --- a/toys/lsb/killall.c +++ b/toys/lsb/killall.c @@ -50,6 +50,7 @@ static int kill_process(pid_t pid, char *name) kill(pid, TT.signum); if (FLAG(w)) { struct int_list *new = xmalloc(sizeof(*TT.pids)); + new->val = pid; new->next = TT.pids; TT.pids = new; diff --git a/toys/lsb/md5sum.c b/toys/lsb/md5sum.c index 060e1964..1f83a29d 100644 --- a/toys/lsb/md5sum.c +++ b/toys/lsb/md5sum.c @@ -455,7 +455,7 @@ static void do_hash(int fd, char *name) if (CFG_TOYBOX_LIBCRYPTO) do_lib_hash(fd, name); else do_builtin_hash(fd, name); - if (name) printf("%s %s\n"+4*!!FLAG(b), toybuf, name); + if (name) printf("%s %s\n"+4*FLAG(b), toybuf, name); } static void do_c_line(char *line) diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c index d449878a..530a4a26 100644 --- a/toys/lsb/mktemp.c +++ b/toys/lsb/mktemp.c @@ -48,7 +48,7 @@ void mktemp_main(void) if (!template) template = "tmp.XXXXXXXXXX"; else { if (*template == '/' && TT.p && *TT.p) perror_exit("-p + /template"); - if (!FLAG(p)&&!FLAG(t)) dir = 0; + if (!FLAG(p) && !FLAG(t)) dir = 0; } // TODO: coreutils cleans paths, so -p /t/// would result in /t/xxx... diff --git a/toys/lsb/umount.c b/toys/lsb/umount.c index 3c9e3ca1..fbaa8e42 100644 --- a/toys/lsb/umount.c +++ b/toys/lsb/umount.c @@ -37,7 +37,7 @@ GLOBALS( char *types; ) -// todo (done?) +// TODO (done?) // borrow df code to identify filesystem? // umount -a from fstab // umount when getpid() not 0, according to fstab diff --git a/toys/net/netcat.c b/toys/net/netcat.c index 6d709a9e..38348949 100644 --- a/toys/net/netcat.c +++ b/toys/net/netcat.c @@ -95,7 +95,7 @@ void netcat_main(void) // The argument parsing logic can't make "<2" conditional on other // arguments like -f and -l, so do it by hand here. - if (FLAG(f) ? toys.optc : (!FLAG(l) && !FLAG(L) && toys.optc!=(FLAG(U)?1:2))) + if (FLAG(f) ? toys.optc : (!FLAG(l) && !FLAG(L) && toys.optc!=2-FLAG(U))) help_exit("bad argument count"); if (FLAG(4)) family = AF_INET; @@ -108,7 +108,7 @@ void netcat_main(void) if (!FLAG(l) && !FLAG(L)) { if (FLAG(U)) sockfd = usock(toys.optargs[0], type, 1); else sockfd = xconnectany(xgetaddrinfo(toys.optargs[0], - toys.optargs[1], family, type, 0, AI_NUMERICHOST*!!FLAG(n))); + toys.optargs[1], family, type, 0, AI_NUMERICHOST*FLAG(n))); // We have a connection. Disarm timeout and start poll/send loop. alarm(0); diff --git a/toys/net/tunctl.c b/toys/net/tunctl.c index 6a2cf1ee..12855708 100644 --- a/toys/net/tunctl.c +++ b/toys/net/tunctl.c @@ -7,7 +7,7 @@ * This is useful for things like "kvm -netdev tap" and containers. * See https://landley.net/lxc/02-networking.html for example usage. * - * todo: bridge mode + * TODO: bridge mode * -b bridge daemon (forwards packets between NAME and NAME2 interfaces) diff --git a/toys/net/wget.c b/toys/net/wget.c index 1fbeaad9..4e17309a 100644 --- a/toys/net/wget.c +++ b/toys/net/wget.c @@ -21,10 +21,10 @@ * Transfer Encoding [gzip|deflate]: https://jigsaw.w3.org/HTTP/TE/bar.txt * * - * todo: Add support for configurable TLS versions - * todo: Add support for ftp - * todo: Add support for Transfer Encoding (gzip|deflate) - * todo: Add support for RFC5987 + * TODO: Add support for configurable TLS versions + * TODO: Add support for ftp + * TODO: Add support for Transfer Encoding (gzip|deflate) + * TODO: Add support for RFC5987 USE_WGET(NEWTOY(wget, "<1>1(max-redirect)#<0=20d(debug)O(output-document):p(post-data):", TOYFLAG_USR|TOYFLAG_BIN)) @@ -246,7 +246,7 @@ void wget_main(void) if (TT.p) sprintf(toybuf, "Content-Length: %ld\r\n", (long)strlen(TT.p)); ss = xmprintf("%s /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\n" "Connection: close\r\n%s\r\n%s", FLAG(p) ? "POST" : "GET", - path, host, agent, FLAG(p) ? toybuf : "", FLAG(p)?TT.p:""); + path, host, agent, TT.p ? toybuf : "", TT.p ? : ""); if (FLAG(d)) printf("--- Request\n%s", ss); wget_connect(host, port); wget_write(ss, strlen(ss)); diff --git a/toys/other/base64.c b/toys/other/base64.c index 1281e372..e6c936b6 100644 --- a/toys/other/base64.c +++ b/toys/other/base64.c @@ -4,7 +4,7 @@ * * See https://tools.ietf.org/html/rfc4648 -// These optflags have to match. Todo: cleanup and collapse together? +// These optflags have to match. TODO: cleanup and collapse together? USE_BASE64(NEWTOY(base64, "diw#<0=76[!dw]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LINEBUF)) USE_BASE32(NEWTOY(base32, "diw#<0=76[!dw]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LINEBUF)) diff --git a/toys/other/gpiod.c b/toys/other/gpiod.c index d667bf4c..14fab5da 100644 --- a/toys/other/gpiod.c +++ b/toys/other/gpiod.c @@ -210,6 +210,7 @@ void gpioinfo_main(void) #define FOR_gpioget #include "generated/flags.h" +// TODO: half the get/set plumbing same here, maybe collate? void gpioget_main(void) { struct gpiohandle_request req = { .flags = GPIOHANDLE_REQUEST_INPUT }; diff --git a/toys/other/help.c b/toys/other/help.c index 5a61f681..70885e83 100644 --- a/toys/other/help.c +++ b/toys/other/help.c @@ -30,7 +30,7 @@ static void do_help(struct toy_list *t) xprintf("

%s

\n", t->name, t->name);
 
   toys.which = t;
-  show_help(stdout, !FLAG(u)+(!!toys.argv[1]<<1)+(!!FLAG(h)<<2));
+  show_help(stdout, !FLAG(u)+(!!toys.argv[1]<<1)+(FLAG(h)<<2));
 
   if (FLAG(h)) xprintf("
\n"); } diff --git a/toys/other/hwclock.c b/toys/other/hwclock.c index 541c70a3..eaec842e 100644 --- a/toys/other/hwclock.c +++ b/toys/other/hwclock.c @@ -47,11 +47,11 @@ void hwclock_main() if (!TT.f) TT.f = "/dev/rtc0"; fd = xopen(TT.f, O_WRONLY*FLAG(w)); - // Get current time in seconds from rtc device. todo: get subsecond time + // Get current time in seconds from rtc device. TODO: get subsecond time if (!FLAG(w)) { xioctl(fd, RTC_RD_TIME, &tm); timeval.tv_sec = xmktime(&tm, utc); - timeval.tv_usec = 0; // todo: fixit + timeval.tv_usec = 0; // TODO: fixit } } @@ -64,7 +64,7 @@ void hwclock_main() if (FLAG(w)) { /* The value of tm_isdst is positive if daylight saving time is in effect, * zero if it is not and negative if the information is not available. - * todo: so why isn't this negative...? */ + * TODO: so why isn't this negative...? */ tm.tm_isdst = 0; xioctl(fd, RTC_SET_TIME, &tm); } else if (FLAG(s)) { diff --git a/toys/other/login.c b/toys/other/login.c index 486b7bce..62c1293e 100644 --- a/toys/other/login.c +++ b/toys/other/login.c @@ -37,7 +37,7 @@ static void login_timeout_handler(int sig __attribute__((unused))) void login_main(void) { - int hh = FLAG(h), count, tty = tty_fd(); + int count, tty = tty_fd(); char *username, *pass = 0, *ss; struct passwd *pwd = 0; @@ -97,7 +97,7 @@ void login_main(void) } syslog(LOG_WARNING, "invalid password for '%s' on %s %s%s", username, - ttyname(tty), hh ? "from " : "", hh ? TT.h : ""); + ttyname(tty), TT.h ? "from " : "", TT.h ? : ""); sleep(3); puts("Login incorrect"); @@ -129,7 +129,7 @@ void login_main(void) if ((ss = readfile("/etc/motd", 0, 0))) puts(ss); syslog(LOG_INFO, "%s logged in on %s %s %s", pwd->pw_name, - ttyname(tty), hh ? "from" : "", hh ? TT.h : ""); + ttyname(tty), TT.h ? "from" : "", TT.h ? : ""); // not using xexec(), login calls absolute path from filesystem so must exec() execl(pwd->pw_shell, xmprintf("-%s", pwd->pw_shell), (char *)0); diff --git a/toys/other/lsattr.c b/toys/other/lsattr.c index 31d7f43a..5be83013 100644 --- a/toys/other/lsattr.c +++ b/toys/other/lsattr.c @@ -198,10 +198,11 @@ error: // Get directory information. static int retell_dir(struct dirtree *root) { - char *fpath = NULL; + char *fpath = 0; if (root->again) { xputc('\n'); + return 0; } if (S_ISDIR(root->st.st_mode) && !root->parent) @@ -209,7 +210,7 @@ static int retell_dir(struct dirtree *root) fpath = dirtree_path(root, NULL); //Special case: with '-a' option and '.'/'..' also included in printing list. - if ((root->name[0] != '.') || FLAG(a)) { + if (*root->name != '.' || FLAG(a)) { print_file_attr(fpath); if (S_ISDIR(root->st.st_mode) && FLAG(R) && dirtree_notdotdot(root)) { xprintf("\n%s:\n", fpath); @@ -218,6 +219,7 @@ static int retell_dir(struct dirtree *root) } } free(fpath); + return 0; } diff --git a/toys/other/nbd_server.c b/toys/other/nbd_server.c index ddcc0d75..98c1937c 100644 --- a/toys/other/nbd_server.c +++ b/toys/other/nbd_server.c @@ -53,7 +53,7 @@ void nbd_server_main(void) // Send original recipe negotiation, with device length and flags memcpy(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16); ll[2] = SWAP_BE64(fdlength(fd)); - uu[6] = SWAP_BE32(5+2*!!FLAG(r)); // has flags, can flush, maybe read only + uu[6] = SWAP_BE32(5+2*FLAG(r)); // has flags, can flush, maybe read only xwrite(1, toybuf, 152); // Simple loop, handles one request at a time with "simple" reply. diff --git a/toys/other/pmap.c b/toys/other/pmap.c index 51c33285..91e28272 100644 --- a/toys/other/pmap.c +++ b/toys/other/pmap.c @@ -32,7 +32,7 @@ void pmap_main(void) for (optargs = toys.optargs; *optargs; optargs++) { long long start, end, pss, tpss=0, dirty, tdirty=0, swap, tswap=0, total=0; - char *name = 0, *k = FLAG(x) ? "" : "K", mode[5]; + char *name = 0, *k = "K"+FLAG(x), mode[5]; pid_t pid = atolx(*optargs); int extras = 0, off, count; FILE *fp; @@ -63,7 +63,7 @@ void pmap_main(void) name = line[off] ? line+off : " [anon]\n"; if (mode[3] == 'p') mode[3] = '-'; total += end = (end-start)/1024; - printf("%0*llx % *lld%s ", (int)(2*sizeof(long)), start, 6+!!FLAG(x), + printf("%0*llx % *lld%s ", (int)(2*sizeof(long)), start, 6+FLAG(x), end, k); if (FLAG(x)) { strcpy(toybuf, name); @@ -92,7 +92,7 @@ void pmap_main(void) xprintf("---------------- ------ ------ ------ ------\n" + ((sizeof(long)==4)?8:0)); } - printf("total% *lld%s", 2*(int)(sizeof(long)+1)+!!FLAG(x), total, k); + printf("total% *lld%s", 2*(int)(sizeof(long)+1)+FLAG(x), total, k); if (FLAG(x)) printf("% 8lld% 8lld% 8lld", tpss, tdirty, tswap); xputc('\n'); } diff --git a/toys/other/readelf.c b/toys/other/readelf.c index e93e6eb0..1db6b69a 100644 --- a/toys/other/readelf.c +++ b/toys/other/readelf.c @@ -124,6 +124,8 @@ static int find_section(char *spec, struct sh *s) char *end; unsigned i; + if (!spec) return 0; + // Valid section number? i = estrtol(spec, &end, 0); if (!errno && !*end && i512=224", TOYFLAG_USR|TOYFLAG_BIN)) config SHA3SUM @@ -89,8 +88,7 @@ static void do_sha3sum(int fd, char *name) } memset(buf, 0, sizeof(buf)); - // Depends on FLAG(b) being 4 - xprintf(" %s\n"+FLAG(b), name); + xprintf(" %s\n"+(FLAG(b)<<2), name); } // TODO test 224 256 384 512, and shake 128 256 diff --git a/toys/other/stat.c b/toys/other/stat.c index 98f27ed6..463aa46a 100644 --- a/toys/other/stat.c +++ b/toys/other/stat.c @@ -157,13 +157,13 @@ static void print_statfs(char type) { void stat_main(void) { - int flagf = FLAG(f), i; + int i; char *format, *f; - if (FLAG(t)) format = flagf + if (FLAG(t)) format = FLAG(f) ? "%n %i %l %t %s %S %b %f %a %c %d" : "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o"; - else format = flagf + else format = FLAG(f) ? " File: \"%n\"\n ID: %i Namelen: %l Type: %T\n" "Block Size: %s Fundamental block size: %S\n" "Blocks: Total: %b\tFree: %f\tAvailable: %a\n" @@ -180,8 +180,8 @@ void stat_main(void) // stat the file or filesystem TT.file = toys.optargs[i]; - if (flagf && !statfs(TT.file, (void *)&TT.stat)); - else if (flagf || (FLAG(L) ? stat : lstat)(TT.file, (void *)&TT.stat)) { + if (FLAG(f) && !statfs(TT.file, (void *)&TT.stat)); + else if (FLAG(f) || (FLAG(L) ? stat : lstat)(TT.file, (void *)&TT.stat)) { perror_msg("'%s'", TT.file); continue; } @@ -195,7 +195,7 @@ void stat_main(void) TT.patlen = f-TT.pattern; if (!*f || TT.patlen>99) error_exit("bad %s", TT.pattern); if (*f == 'n') strout(TT.file); - else if (flagf) print_statfs(*f); + else if (FLAG(f)) print_statfs(*f); else print_stat(*f); } } diff --git a/toys/posix/cat.c b/toys/posix/cat.c index e324c0d1..254f904d 100644 --- a/toys/posix/cat.c +++ b/toys/posix/cat.c @@ -28,7 +28,7 @@ config CAT static void do_cat(int fd, char *name) { - int i, len, size=FLAG(u) ? 1 : sizeof(toybuf); + int i, len, size = FLAG(u) ? : sizeof(toybuf); for(;;) { len = read(fd, toybuf, size); diff --git a/toys/posix/chgrp.c b/toys/posix/chgrp.c index 5c65ee58..73c4b223 100644 --- a/toys/posix/chgrp.c +++ b/toys/posix/chgrp.c @@ -49,7 +49,7 @@ static int do_chgrp(struct dirtree *node) // Depth first search if (!dirtree_notdotdot(node)) return 0; if (FLAG(R) && !node->again && S_ISDIR(node->st.st_mode)) - return DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!FLAG(L)); + return DIRTREE_COMEAGAIN|DIRTREE_SYMFOLLOW*FLAG(L); fd = dirtree_parentfd(node); ret = fchownat(fd, node->name, TT.owner, TT.group, @@ -94,7 +94,7 @@ void chgrp_main(void) TT.group = xgetgid(TT.group_name); for (s=toys.optargs+1; *s; s++) - dirtree_flagread(*s, DIRTREE_SYMFOLLOW*!!(FLAG(H)|FLAG(L)), do_chgrp); + dirtree_flagread(*s, DIRTREE_SYMFOLLOW*(FLAG(H)|FLAG(L)), do_chgrp); if (CFG_TOYBOX_FREE && ischown) free(own); } diff --git a/toys/posix/cksum.c b/toys/posix/cksum.c index 682dcf46..8bb6f815 100644 --- a/toys/posix/cksum.c +++ b/toys/posix/cksum.c @@ -64,7 +64,7 @@ static void do_cksum(int fd, char *name) if (len<1) break; llen += len; - for (i=0; ist.st_mode | 0200) || errno == EEXIST) if (-1 != (try->extra = openat(cfd, catch, O_NOFOLLOW))) if (!fstat(try->extra, &st2) && S_ISDIR(st2.st_mode)) - return DIRTREE_COMEAGAIN | (DIRTREE_SYMFOLLOW*!!FLAG(L)); + return DIRTREE_COMEAGAIN | DIRTREE_SYMFOLLOW*FLAG(L); // Hardlink @@ -430,9 +430,7 @@ void cp_main(void) // "mv across devices" triggers cp fallback path, so set that as default errno = EXDEV; if (CFG_MV && toys.which->name[0] == 'm') { - int force = FLAG(f), no_clobber = FLAG(n); - - if (!force || no_clobber) { + if (!FLAG(f) || FLAG(n)) { struct stat st; int exists = !stat(TT.destname, &st); @@ -445,7 +443,7 @@ void cp_main(void) else unlink(TT.destname); } // if -n and dest exists, don't try to rename() or copy - if (exists && no_clobber) send = 0; + if (exists && FLAG(n)) send = 0; } if (send) send = rename(src, TT.destname); if (trail) trail[1] = '/'; @@ -454,7 +452,7 @@ void cp_main(void) // Copy if we didn't mv or hit an error, skipping nonexistent sources if (send) { if (errno!=EXDEV || dirtree_flagread(src, DIRTREE_SHUTUP+ - DIRTREE_SYMFOLLOW*!!(FLAG(H)||FLAG(L)), TT.callback)) + DIRTREE_SYMFOLLOW*(FLAG(H)|FLAG(L)), TT.callback)) perror_msg("bad '%s'", src); } if (destdir) free(TT.destname); @@ -514,15 +512,15 @@ void install_main(void) } if (FLAG(D)) { - char *destname = FLAG(t) ? TT.i.t : (TT.destname = toys.optargs[toys.optc-1]); - if (mkpathat(AT_FDCWD, destname, 0777, MKPATHAT_MAKE | (FLAG(t) ? MKPATHAT_MKLAST : 0))) + char *destname = TT.i.t ? : (TT.destname = toys.optargs[toys.optc-1]); + if (mkpathat(AT_FDCWD, destname, 0777, MKPATHAT_MAKE|MKPATHAT_MKLAST*FLAG(t))) perror_exit("-D '%s'", destname); if (toys.optc == !FLAG(t)) return; } // Translate flags from install to cp - toys.optflags = cp_flag_F() + cp_flag_v()*!!FLAG(v) - + cp_flag_p()*!!(FLAG(p)|FLAG(o)|FLAG(g)); + toys.optflags = cp_flag_F() + cp_flag_v()*FLAG(v) + + cp_flag_p()*(FLAG(p)|FLAG(o)|FLAG(g)); TT.callback = install_node; cp_main(); diff --git a/toys/posix/cpio.c b/toys/posix/cpio.c index 282a39b1..71fb5022 100644 --- a/toys/posix/cpio.c +++ b/toys/posix/cpio.c @@ -14,7 +14,7 @@ * rdevmajor rdevminor namesize check * This is the equivalent of mode -H newc in other implementations. * - * todo: export/import linux file list text format ala gen_initramfs_list.sh + * TODO: export/import linux file list text format ala gen_initramfs_list.sh USE_CPIO(NEWTOY(cpio, "(ignore-devno)(renumber-inodes)(quiet)(no-preserve-owner)R(owner):md(make-directories)uH:p|i|t|F:v(verbose)o|[!pio][!pot][!pF]", TOYFLAG_BIN)) @@ -80,7 +80,6 @@ static unsigned x8u(char *hex) void cpio_main(void) { - // Subtle bit: FLAG_o is 1 so we can just use it to select stdin/stdout. int pipe, afd = FLAG(o), empty = 1; pid_t pid = 0; long Ruid = -1, Rgid = -1; diff --git a/toys/posix/cut.c b/toys/posix/cut.c index c4f34f95..d74dc4ea 100644 --- a/toys/posix/cut.c +++ b/toys/posix/cut.c @@ -8,7 +8,7 @@ * "-" counts as start to end. Using spaces to separate a comma-separated list * is silly and inconsistent with dd, ps, cp, and mount. * - * todo: -n, -s with -c + * TODO: -n, -s with -c USE_CUT(NEWTOY(cut, "b*|c*|f*|F*|C*|O(output-delimiter):d:sDn[!cbfF]", TOYFLAG_USR|TOYFLAG_BIN)) diff --git a/toys/posix/df.c b/toys/posix/df.c index 9e3adbab..e07b33bf 100644 --- a/toys/posix/df.c +++ b/toys/posix/df.c @@ -115,7 +115,7 @@ static void show_mt(struct mtab_list *mt, int measuring) suap[i] = (block*suap[i])/TT.units; if (FLAG(H)||FLAG(h)) - human_readable(dsuapm[i+1], suap[i], FLAG(H) ? HR_1000 : 0); + human_readable(dsuapm[i+1], suap[i], HR_1000*FLAG(H)); else sprintf(dsuapm[i+1], "%llu", suap[i]); dsuapm[i+2] = strchr(dsuapm[i+1], 0)+1; } diff --git a/toys/posix/du.c b/toys/posix/du.c index 05dea096..8bf9575d 100644 --- a/toys/posix/du.c +++ b/toys/posix/du.c @@ -114,8 +114,7 @@ static int do_du(struct dirtree *node) else if (!dirtree_notdotdot(node)) return 0; // detect swiching filesystems - if (FLAG(x) && (TT.st_dev != node->st.st_dev)) - return 0; + if (FLAG(x) && TT.st_dev != node->st.st_dev) return 0; // Don't loop endlessly on recursive directory symlink if (FLAG(L)) { @@ -132,7 +131,7 @@ static int do_du(struct dirtree *node) if (S_ISDIR(node->st.st_mode)) { if (!node->again) { TT.depth++; - return DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!FLAG(L)); + return DIRTREE_COMEAGAIN|DIRTREE_SYMFOLLOW*FLAG(L); } else TT.depth--; } diff --git a/toys/posix/grep.c b/toys/posix/grep.c index 5c6f5936..137c9c3c 100644 --- a/toys/posix/grep.c +++ b/toys/posix/grep.c @@ -429,8 +429,7 @@ static void parse_regex(void) struct reg *shoe; dlist_add_nomalloc(&TT.reg, (void *)(shoe = xmalloc(sizeof(struct reg)))); - xregcomp(&shoe->r, (*last)->arg, - (REG_EXTENDED*!!FLAG(E))|(REG_ICASE*!!FLAG(i))); + xregcomp(&shoe->r, (*last)->arg, REG_EXTENDED*FLAG(E)|REG_ICASE*FLAG(i)); al = *last; *last = (*last)->next; free(al); @@ -476,7 +475,7 @@ static int do_grep_r(struct dirtree *new) if (S_ISDIR(new->st.st_mode)) { for (al = TT.exclude_dir; al; al = al->next) if (!fnmatch(al->arg, new->name, 0)) return 0; - return DIRTREE_RECURSE|(FLAG(R)?DIRTREE_SYMFOLLOW:0); + return DIRTREE_RECURSE|DIRTREE_SYMFOLLOW*FLAG(R); } if (TT.S || TT.M) { for (al = TT.S; al; al = al->next) diff --git a/toys/posix/ls.c b/toys/posix/ls.c index aa722cbb..9d3e99a3 100644 --- a/toys/posix/ls.c +++ b/toys/posix/ls.c @@ -203,7 +203,7 @@ static int filter(struct dirtree *new) new->st.st_blocks >>= 1; // Use 1KiB blocks rather than 512B blocks. if (FLAG(a)||FLAG(f)) return DIRTREE_SAVE; - if (!FLAG(A) && new->name[0]=='.') return 0; + if (!FLAG(A) && *new->name=='.') return 0; return dirtree_notdotdot(new) & DIRTREE_SAVE; } @@ -291,7 +291,7 @@ static void listfiles(int dirfd, struct dirtree *indir) // Read directory contents. We dup() the fd because this will close it. // This reads/saves contents to display later, except for in "ls -1f" mode. } else dirtree_recurse(indir, filter, dirfd, - DIRTREE_STATLESS|DIRTREE_SYMFOLLOW*!!FLAG(L)); + DIRTREE_STATLESS|DIRTREE_SYMFOLLOW*FLAG(L)); // Copy linked list to array and sort it. Directories go in array because // we visit them in sorted order too. (The nested loops let us measure and @@ -379,9 +379,7 @@ static void listfiles(int dirfd, struct dirtree *indir) // Handle padding and wrapping for display purposes entrylen(dt, len); if (ul) { - int mm = !!FLAG(m); - - if (mm) xputc(','); + if (FLAG(m)) xputc(','); if (FLAG(C)||FLAG(x)) { if (!curcol) xputc('\n'); else { @@ -392,8 +390,8 @@ static void listfiles(int dirfd, struct dirtree *indir) xputc('\n'); width = 0; } else { - printf(" "+mm, 0); // shut up the stupid compiler - width += 2-mm; + printf(" "+FLAG(m), 0); // shut up the stupid compiler + width += 2-FLAG(m); } } width += *len; diff --git a/toys/posix/patch.c b/toys/posix/patch.c index 34a75e23..6bdd4615 100644 --- a/toys/posix/patch.c +++ b/toys/posix/patch.c @@ -143,7 +143,7 @@ static int loosecmp(char *aa, char *bb) static int apply_one_hunk(void) { struct double_list *plist, *buf = 0, *check; - int matcheof, trail = 0, reverse = FLAG(R), backwarn = 0, allfuzz, fuzz, i; + int matcheof, trail = 0, backwarn = 0, allfuzz, fuzz, i; int (*lcmp)(char *aa, char *bb) = FLAG(l) ? (void *)loosecmp : (void *)strcmp; // Match EOF if there aren't as many ending context lines as beginning @@ -157,7 +157,7 @@ static int apply_one_hunk(void) // Only allow fuzz if 2 context lines have multiple nonwhitespace chars. // avoids the "all context was blank or } lines" issue. Removed lines // count as context since they're matched. - if (c==' ' || c=="-+"[reverse]) { + if (c==' ' || c=="-+"[FLAG(R)]) { s = plist->data+1; while (isspace(*s)) s++; if (*s && s[1] && !isspace(s[1])) fuzz++; @@ -167,7 +167,7 @@ static int apply_one_hunk(void) } matcheof = !trail || trail < TT.context; if (fuzz<2) allfuzz = 0; - else allfuzz = FLAG(F) ? TT.F : (TT.context ? TT.context-1 : 0); + else allfuzz = TT.F ? : TT.context ? TT.context-1 : 0; if (FLAG(x)) fprintf(stderr,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N'); @@ -180,7 +180,7 @@ static int apply_one_hunk(void) // Figure out which line of hunk to compare with next. (Skip lines // of the hunk we'd be adding.) - while (plist && *plist->data == "+-"[reverse]) { + while (plist && *plist->data == "+-"[FLAG(R)]) { if (data && !lcmp(data, plist->data+1)) if (!backwarn) backwarn = TT.linenum; plist = plist->next; @@ -260,7 +260,7 @@ fuzzed: } out: // We have a match. Emit changed data. - TT.state = "-+"[reverse]; + TT.state = "-+"[FLAG(R)]; while ((plist = dlist_pop(&TT.current_hunk))) { if (TT.state == *plist->data || *plist->data == ' ') { if (*plist->data == ' ') dprintf(TT.fileout, "%s\n", buf->data); @@ -313,7 +313,7 @@ static char *unquote_file(char *filename) void patch_main(void) { - int reverse = FLAG(R), state = 0, patchlinenum = 0, strip = 0; + int state = 0, patchlinenum = 0, strip = 0; char *oldname = NULL, *newname = NULL; if (toys.optc == 2) TT.i = toys.optargs[1]; @@ -420,7 +420,7 @@ void patch_main(void) // If an original file was provided on the command line, it overrides // *all* files mentioned in the patch, not just the first. if (toys.optc) { - char **which = reverse ? &oldname : &newname; + char **which = FLAG(R) ? &oldname : &newname; free(*which); *which = strdup(toys.optargs[0]); @@ -429,12 +429,12 @@ void patch_main(void) TT.p = 0; } - name = reverse ? oldname : newname; + name = FLAG(R) ? oldname : newname; // We're deleting oldname if new file is /dev/null (before -p) // or if new hunk is empty (zero context) after patching - if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) { - name = reverse ? newname : oldname; + if (!strcmp(name, "/dev/null") || !(FLAG(R) ? oldsum : newsum)) { + name = FLAG(R) ? newname : oldname; del++; } diff --git a/toys/posix/printf.c b/toys/posix/printf.c index be06bd92..93374731 100644 --- a/toys/posix/printf.c +++ b/toys/posix/printf.c @@ -5,7 +5,7 @@ * * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html * - * todo: *m$ ala printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec); + * TODO: *m$ ala printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec); USE_PRINTF(NEWTOY(printf, "<1?^", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_MAYFORK)) diff --git a/toys/posix/ps.c b/toys/posix/ps.c index 4f3807a8..2ff3ba73 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -1634,8 +1634,8 @@ static void top_common( run[1+stridx("RTtZ", *string_field(mix.tb[i], &field))]++; sprintf(toybuf, "%ss: %d total, %3ld running, %3ld sleeping, %3ld stopped, " - "%3ld zombie", FLAG(H)?"Thread":"Task", mix.count, run[1], run[0], - run[2]+run[3], run[4]); + "%3ld zombie", FLAG(H) ? "Thread" : "Task", mix.count, run[1], + run[0], run[2]+run[3], run[4]); lines = header_line(lines, 0); if (readfile("/proc/meminfo", toybuf+256, sizeof(toybuf)-256)) { @@ -1843,12 +1843,12 @@ static int iotop_filter(long long *oslot, long long *nslot, int milis) if (!FLAG(a)) merge_deltas(oslot, nslot, milis); else oslot[SLOT_upticks] = ((millitime()-TT.time)*TT.ticks)/1000; - return !FLAG(O)||oslot[SLOT_iobytes+!FLAG(A)]; + return !FLAG(O) || oslot[SLOT_iobytes+!FLAG(A)]; } void iotop_main(void) { - char *s1 = 0, *s2 = 0, *d = "D"+!!FLAG(A); + char *s1 = 0, *s2 = 0, *d = "D"+FLAG(A); if (FLAG(K)) TT.forcek++; @@ -1883,9 +1883,7 @@ static void do_pgk(struct procpid *tb) } if (!FLAG(c) && (!TT.pgrep.signal || TT.tty)) { printf("%lld", *tb->slot); - if (FLAG(l)) - printf(" %s", tb->str+tb->offset[4]*!!FLAG(f)); - + if (FLAG(l)) printf(" %s", tb->str+tb->offset[4]*FLAG(f)); printf("%s", TT.pgrep.d ? TT.pgrep.d : "\n"); } } @@ -1895,7 +1893,7 @@ static void match_pgrep(void *p) struct procpid *tb = p; regmatch_t match; struct regex_list *reg; - char *name = tb->str+tb->offset[4]*!!FLAG(f); + char *name = tb->str+tb->offset[4]*FLAG(f); // Never match ourselves. if (TT.pgrep.self == *tb->slot) return; diff --git a/toys/posix/sed.c b/toys/posix/sed.c index d75b61a8..0de90633 100644 --- a/toys/posix/sed.c +++ b/toys/posix/sed.c @@ -837,7 +837,7 @@ static void parse_pattern(char **pline, long len) if (!(s = unescape_delimited_string(&line, 0))) goto error; if (!*s) command->rmatch[i] = 0; else { - xregcomp((void *)reg, s, REG_EXTENDED*!!FLAG(r)); + xregcomp((void *)reg, s, REG_EXTENDED*FLAG(r)); command->rmatch[i] = reg-toybuf; reg += sizeof(regex_t); } @@ -1124,5 +1124,5 @@ void sed_main(void) sed_line(0, 0); } - // todo: need to close fd when done for TOYBOX_FREE? + // TODO: need to close fd when done for TOYBOX_FREE? } diff --git a/toys/posix/sort.c b/toys/posix/sort.c index 443ca3e8..c0d312de 100644 --- a/toys/posix/sort.c +++ b/toys/posix/sort.c @@ -284,7 +284,7 @@ static void sort_lines(char **pline, long len) // handle -c here so we don't allocate more memory than necessary. if (FLAG(C)||FLAG(c)) { - if (TT.lines && compare_keys((void *)&TT.lines, &line)>-!!FLAG(u)) { + if (TT.lines && compare_keys((void *)&TT.lines, &line)>-FLAG(u)) { toys.exitval = 1; if (FLAG(C)) xexit(); error_exit("%s: Check line %u", TT.name, TT.linecount+1); diff --git a/toys/posix/tail.c b/toys/posix/tail.c index f9aabb32..514bd5b8 100644 --- a/toys/posix/tail.c +++ b/toys/posix/tail.c @@ -301,7 +301,7 @@ void tail_main(void) TT.ss = TT.s ? xparsemillitime(TT.s) : 1000; loopfiles_rw(args, - O_RDONLY|WARN_ONLY|LOOPFILES_ANYWAY|(O_CLOEXEC*!(FLAG(f) || FLAG(F))), + O_RDONLY|WARN_ONLY|LOOPFILES_ANYWAY|O_CLOEXEC*!(FLAG(f) || FLAG(F)), 0, do_tail); // Wait for more data when following files diff --git a/toys/posix/tar.c b/toys/posix/tar.c index 3e2a2153..977ccf0f 100644 --- a/toys/posix/tar.c +++ b/toys/posix/tar.c @@ -514,7 +514,7 @@ done: free(xfname); free(name); - return recurse*(DIRTREE_RECURSE|(FLAG(h)?DIRTREE_SYMFOLLOW:0)); + return recurse*(DIRTREE_RECURSE|DIRTREE_SYMFOLLOW*FLAG(h)); } static void wsettime(char *s, long long sec) @@ -923,7 +923,7 @@ static void unpack_tar(char *first) xsetenv(xmprintf("TAR_GID=%o", TT.hdr.gid), 0); pid = xpopen((char *[]){"sh", "-c", TT.to_command, NULL}, &fd, 0); - // todo: short write exits tar here, other skips data. + // TODO: short write exits tar here, other skips data. sendfile_sparse(fd); fd = xpclose_both(pid, 0); if (fd) error_msg("%d: Child returned %d", pid, fd); @@ -966,7 +966,7 @@ static void do_XT(char **pline, long len) static char *get_archiver() { - return FLAG(I) ? TT.I : FLAG(z) ? "gzip" : FLAG(j) ? "bzip2" : "xz"; + return TT.I ? : FLAG(z) ? "gzip" : FLAG(j) ? "bzip2" : "xz"; } void tar_main(void) @@ -1004,7 +1004,7 @@ void tar_main(void) for (args = toys.optargs; *args; args++) trim2list(&TT.incl, *args); // -T is always --verbatim-files-from: no quote removal or -arg handling for (;TT.T; TT.T = TT.T->next) - do_lines(xopenro(TT.T->arg), FLAG(null) ? '\0' : '\n', do_XT); + do_lines(xopenro(TT.T->arg), '\n'*!FLAG(null), do_XT); // If include file list empty, don't create empty archive if (FLAG(c)) { @@ -1155,9 +1155,9 @@ void tar_main(void) } do { TT.warn = 1; - ii = FLAG(h) ? DIRTREE_SYMFOLLOW : 0; - if (FLAG(sort)|FLAG(s)) ii |= DIRTREE_BREADTH; - dirtree_flagread(dl->data, FLAG(h) ? DIRTREE_SYMFOLLOW : 0, add_to_tar); + dirtree_flagread(dl->data, + DIRTREE_SYMFOLLOW*FLAG(h)|DIRTREE_BREADTH*(FLAG(sort)|FLAG(s)), + add_to_tar); } while (TT.incl != (dl = dl->next)); writeall(TT.fd, toybuf, 1024); diff --git a/toys/posix/time.c b/toys/posix/time.c index 1becad1b..078aca58 100644 --- a/toys/posix/time.c +++ b/toys/posix/time.c @@ -31,7 +31,7 @@ void time_main(void) long long sec[3]; int stat, ii, idx, nano[3]; pid_t pid; - char *labels[] = {"\nreal"+!!FLAG(p), "user", "sys"}, **label = labels, + char *labels[] = {"\nreal"+FLAG(p), "user", "sys"}, **label = labels, *vlabels[] ={"Real", "User", "System"}, tab = toys.optflags ? ' ' : '\t'; if (FLAG(v)) label = vlabels; diff --git a/toys/posix/touch.c b/toys/posix/touch.c index 5e33d7f7..e687ff81 100644 --- a/toys/posix/touch.c +++ b/toys/posix/touch.c @@ -71,7 +71,6 @@ void touch_main(void) if (!strcmp(s, "-")) { if (!futimens(1, ts)) continue; } else { - // cheat: FLAG_h is rightmost flag, so its value is 1 if (!utimensat(AT_FDCWD, s, ts, FLAG(h)*AT_SYMLINK_NOFOLLOW)) continue; if (FLAG(c)) continue; if (access(s, F_OK) && (-1!=(fd = open(s, O_CREAT, 0666)))) { diff --git a/toys/posix/uuencode.c b/toys/posix/uuencode.c index 160954d8..3bede303 100644 --- a/toys/posix/uuencode.c +++ b/toys/posix/uuencode.c @@ -24,19 +24,19 @@ void uuencode_main(void) { char *name = toys.optargs[toys.optc-1], buf[(76/4)*3]; - int i, m = FLAG(m), fd = 0; + int i, fd = 0; if (toys.optc > 1) fd = xopenro(toys.optargs[0]); base64_init(toybuf); - xprintf("begin%s 744 %s\n", m ? "-base64" : "", name); + xprintf("begin%s 744 %s\n", FLAG(m) ? "-base64" : "", name); for (;;) { char *in; - if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break; + if (!(i = xread(fd, buf, FLAG(m) ? sizeof(buf) : 45))) break; - if (!m) xputc(i+32); + if (!FLAG(m)) xputc(i+32); in = buf; for (in = buf; in-buf < i; ) { @@ -49,10 +49,11 @@ void uuencode_main(void) if (j < bytes) x |= (*(in++) & 0x0ff) << (8*(2-j)); out = (x>>((3-j)*6)) & 0x3f; - xputc(m ? (j > bytes ? '=' : toybuf[out]) : (out ? out + 0x20 : 0x60)); + xputc(FLAG(m) ? (j > bytes ? '=' : toybuf[out]) + : (out ? out + 0x20 : 0x60)); } } xputc('\n'); } - xputs(m ? "====" : "end"); + xputs(FLAG(m) ? "====" : "end"); } -- 2.39.2