From bcbbcb24c2db6fbc2b71e6be5149ce43f0d899dd Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 30 Apr 2024 13:08:35 -0500 Subject: [PATCH] Switch most remaining toys.optflags&FLAG_x checks to FLAG(x) --- toys/lsb/mknod.c | 7 +++---- toys/lsb/umount.c | 22 ++++++++++------------ toys/net/ftpget.c | 30 ++++++++++++++---------------- toys/net/tunctl.c | 4 ++-- toys/other/blockdev.c | 13 ++++++------- toys/other/chcon.c | 6 +++--- toys/other/chrt.c | 9 ++++----- toys/other/mix.c | 8 ++++---- toys/other/setfattr.c | 9 ++++----- toys/other/swapon.c | 8 +++----- toys/other/sysctl.c | 26 ++++++++++---------------- toys/posix/cat.c | 2 +- toys/posix/du.c | 3 +-- toys/posix/getconf.c | 6 +++--- toys/posix/mkdir.c | 2 +- toys/posix/mkfifo.c | 7 +++---- toys/posix/paste.c | 8 ++++---- toys/posix/rm.c | 2 +- toys/posix/touch.c | 5 ++--- 19 files changed, 79 insertions(+), 98 deletions(-) diff --git a/toys/lsb/mknod.c b/toys/lsb/mknod.c index f8c33183..cbc33980 100644 --- a/toys/lsb/mknod.c +++ b/toys/lsb/mknod.c @@ -37,7 +37,7 @@ GLOBALS( void mknod_main(void) { mode_t modes[] = {S_IFIFO, S_IFCHR, S_IFCHR, S_IFBLK}; - int major=0, minor=0, type; + int major = 0, minor = 0, type; int mode = TT.m ? string_to_mode(TT.m, 0777) : 0660; type = stridx("pcub", *toys.optargs[1]); @@ -49,9 +49,8 @@ void mknod_main(void) minor = atoi(toys.optargs[3]); } - if (toys.optflags & FLAG_Z) - if (-1 == lsm_set_create(TT.Z)) - perror_exit("-Z '%s' failed", TT.Z); + if (FLAG(Z) && lsm_set_create(TT.Z)==-1) + perror_exit("-Z '%s' failed", TT.Z); if (mknod(*toys.optargs, mode|modes[type], dev_makedev(major, minor))) perror_exit_raw(*toys.optargs); } diff --git a/toys/lsb/umount.c b/toys/lsb/umount.c index 19fd5274..464b02b5 100644 --- a/toys/lsb/umount.c +++ b/toys/lsb/umount.c @@ -76,17 +76,16 @@ static void do_umount(char *dir, char *dev, int flags) } if (!umount2(dir, flags)) { - if (toys.optflags & FLAG_v) xprintf("%s unmounted\n", dir); + if (FLAG(v)) xprintf("%s unmounted\n", dir); // Attempt to disassociate loopback device. This ioctl should be ignored // for anything else, because lanana allocated ioctl range 'L' to loopback - if (dev && !(toys.optflags & FLAG_D)) { + if (dev && !FLAG(D)) { int lfd = open(dev, O_RDONLY); if (lfd != -1) { // This is LOOP_CLR_FD, fetching it from headers is awkward - if (!ioctl(lfd, 0x4C01) && (toys.optflags & FLAG_v)) - xprintf("%s cleared\n", dev); + if (!ioctl(lfd, 0x4C01) && FLAG(v)) xprintf("%s cleared\n", dev); close(lfd); } } @@ -94,9 +93,9 @@ static void do_umount(char *dir, char *dev, int flags) return; } - if (toys.optflags & FLAG_r) { + if (FLAG(r)) { if (!mount("", dir, "", MS_REMOUNT|MS_RDONLY, "")) { - if (toys.optflags & FLAG_v) xprintf("%s remounted ro\n", dir); + if (FLAG(v)) xprintf("%s remounted ro\n", dir); return; } } @@ -110,19 +109,18 @@ void umount_main(void) struct mtab_list *mlsave = 0, *mlrev = 0, *ml; int flags=0; - if (!toys.optc && !(toys.optflags & FLAG_a)) - error_exit("Need 1 arg or -a"); + if (!toys.optc && !FLAG(a)) error_exit("Need 1 arg or -a"); - if (toys.optflags & FLAG_f) flags |= MNT_FORCE; - if (toys.optflags & FLAG_l) flags |= MNT_DETACH; + if (FLAG(f)) flags |= MNT_FORCE; + if (FLAG(l)) flags |= MNT_DETACH; // Load /proc/mounts and get a reversed list (newest first) // We use the list both for -a, and to umount /dev/name or do losetup -d - if (!(toys.optflags & FLAG_n) && !access(pm, R_OK)) + if (!FLAG(n) && !access(pm, R_OK)) mlrev = dlist_terminate(mlsave = xgetmountlist(pm)); // Unmount all: loop through mounted filesystems, skip -t, unmount the rest - if (toys.optflags & FLAG_a) { + if (FLAG(a)) { char *typestr = 0; struct arg_list *tal; diff --git a/toys/net/ftpget.c b/toys/net/ftpget.c index 27ea6db6..8c72d97c 100644 --- a/toys/net/ftpget.c +++ b/toys/net/ftpget.c @@ -65,7 +65,7 @@ static int xread2line(int fd, char *buf, int len) while (total--) if (buf[total]=='\r' || buf[total]=='\n') buf[total] = 0; else break; - if (toys.optflags & FLAG_v) fprintf(stderr, "%s\n", toybuf); + if (FLAG(v)) fprintf(stderr, "%s\n", toybuf); return total+1; } @@ -76,7 +76,7 @@ static int ftp_line(char *cmd, char *arg, int must) if (cmd) { char *s = "%s %s\r\n"+3*(!arg); - if (toys.optflags & FLAG_v) fprintf(stderr, s, cmd, arg); + if (FLAG(v)) fprintf(stderr, s, cmd, arg); dprintf(TT.fd, s, cmd, arg); } if (must>=0) { @@ -115,15 +115,15 @@ void ftpget_main(void) if (rc == 331) rc = ftp_line("PASS", TT.P, 0); if (rc != 230) error_exit_raw(toybuf); - if (toys.optflags & FLAG_m) { + if (FLAG(m)) { if (toys.optc != 3) error_exit("-m FROM TO"); ftp_line("RNFR", toys.optargs[1], 350); ftp_line("RNTO", toys.optargs[2], 250); - } else if (toys.optflags & FLAG_M) ftp_line("MKD", toys.optargs[1], 257); - else if (toys.optflags & FLAG_d) ftp_line("DELE", toys.optargs[1], 250); - else if (toys.optflags & FLAG_D) ftp_line("RMD", toys.optargs[1], 250); + } else if (FLAG(M)) ftp_line("MKD", toys.optargs[1], 257); + else if (FLAG(d)) ftp_line("DELE", toys.optargs[1], 250); + else if (FLAG(D)) ftp_line("RMD", toys.optargs[1], 250); else { - int get = !(toys.optflags&FLAG_s), cnt = toys.optflags&FLAG_c; + int get = !FLAG(s), cnt = FLAG(c); char *cmd; // Only do passive binary transfers @@ -152,14 +152,12 @@ void ftpget_main(void) // RETR blocks until file data read from data port, so use SIZE to check // if file exists before creating local copy lenr = 0; - if (toys.optflags&(FLAG_s|FLAG_g)) { + if (FLAG(s)|FLAG(g)) { if (ftp_line("SIZE", remote, 0) == 213) sscanf(toybuf, "%*u %llu", &lenr); else if (get) error_exit("no %s", remote); - } - // Open file for reading or writing - if (toys.optflags & (FLAG_g|FLAG_s)) { + // Open file for reading or writing if (strcmp(toys.optargs[1], "-")) ii = xcreate(toys.optargs[1], get ? (cnt ? O_APPEND : O_TRUNC)|O_CREAT|O_WRONLY : O_RDONLY, 0666); @@ -167,8 +165,8 @@ void ftpget_main(void) } if (get) { cmd = "RETR"; - if (toys.optflags&FLAG_l) cmd = "LIST"; - if (toys.optflags&FLAG_L) cmd = "NLST"; + if (FLAG(l)) cmd = "LIST"; + if (FLAG(L)) cmd = "NLST"; if (cnt) { char buf[32]; @@ -179,8 +177,8 @@ void ftpget_main(void) ftp_line(cmd, remote, -1); lenl += xsendfile(port, ii); - ftp_line(0, 0, (toys.optflags&FLAG_g) ? 226 : 150); - } else if (toys.optflags & FLAG_s) { + ftp_line(0, 0, FLAG(g) ? 226 : 150); + } else if (FLAG(s)) { cmd = "STOR"; if (cnt && lenr) { cmd = "APPE"; @@ -190,7 +188,7 @@ void ftpget_main(void) lenr += xsendfile(ii, port); close(port); } - if (toys.optflags&(FLAG_g|FLAG_s)) + if (FLAG(s)|FLAG(g)) if (lenl != lenr) error_exit("short %lld/%lld", lenl, lenr); } ftp_line("QUIT", 0, 0); diff --git a/toys/net/tunctl.c b/toys/net/tunctl.c index b18306f0..21dcd0dd 100644 --- a/toys/net/tunctl.c +++ b/toys/net/tunctl.c @@ -42,11 +42,11 @@ void tunctl_main(void) int fd = xopen("/dev/net/tun", O_RDWR); // Associate filehandle with device - ifr->ifr_flags = ((toys.optflags&FLAG_T) ? IFF_TUN : IFF_TAP)|IFF_NO_PI; + ifr->ifr_flags = (FLAG(T) ? IFF_TUN : IFF_TAP)|IFF_NO_PI; strncpy(ifr->ifr_name, *toys.optargs, sizeof(ifr->ifr_name)); xioctl(fd, TUNSETIFF, toybuf); - if (toys.optflags&FLAG_t) { + if (FLAG(t)) { xioctl(fd, TUNSETPERSIST, (void *)1); xioctl(fd, TUNSETOWNER, (void *)(long)u); } else xioctl(fd, TUNSETPERSIST, (void *)0); diff --git a/toys/other/blockdev.c b/toys/other/blockdev.c index 9f169453..25553e99 100644 --- a/toys/other/blockdev.c +++ b/toys/other/blockdev.c @@ -34,7 +34,7 @@ config BLOCKDEV #include GLOBALS( - long setbsz, setra; + long setra, setbsz; ) void blockdev_main(void) @@ -50,20 +50,19 @@ void blockdev_main(void) int fd = xopenro(*ss), i; // Command line order discarded so perform multiple operations in flag order - for (i = 0; i < 32; i++) { + for (i = 0; i<32; i++) { long flag = toys.optflags & (1<> 9: val); + if (!flag) printf("%lld\n", val>>(9*FLAG(getsz))); } xclose(fd); } diff --git a/toys/other/chcon.c b/toys/other/chcon.c index cdbabf76..ff1c850d 100644 --- a/toys/other/chcon.c +++ b/toys/other/chcon.c @@ -28,12 +28,12 @@ static int do_chcon(struct dirtree *try) if (!dirtree_notdotdot(try)) return 0; path = dirtree_path(try, 0); - if (toys.optflags & FLAG_v) printf("chcon '%s' to %s\n", path, con); - if (-1 == ((toys.optflags & FLAG_h) ? lsetfilecon : setfilecon)(path, con)) + if (FLAG(v)) printf("chcon '%s' to %s\n", path, con); + if (-1 == (FLAG(h) ? lsetfilecon : setfilecon)(path, con)) perror_msg("'%s' to %s", path, con); free(path); - return (toys.optflags & FLAG_R)*DIRTREE_RECURSE; + return FLAG(R)*DIRTREE_RECURSE; } void chcon_main(void) diff --git a/toys/other/chrt.c b/toys/other/chrt.c index 1a8222fa..e2331c91 100644 --- a/toys/other/chrt.c +++ b/toys/other/chrt.c @@ -57,7 +57,7 @@ void chrt_main(void) int pol, pri; // Show min/maxes? - if (toys.optflags&FLAG_m) { + if (FLAG(m)) { for (pol = 0; pol 0xFF) xprintf("%s:%s = left:%d\t right:%d\n", @@ -57,7 +57,7 @@ void mix_main(void) else xprintf("%s:%s = %d\n", TT.d, channels[channel], level); } else { level = TT.l; - if (!(toys.optflags & FLAG_r)) level = TT.r | (level<<8); + if (!FLAG(r)) level = TT.r | (level<<8); xioctl(fd, MIXER_WRITE(channel), &level); } diff --git a/toys/other/setfattr.c b/toys/other/setfattr.c index 0aea225a..c6ff2ab5 100644 --- a/toys/other/setfattr.c +++ b/toys/other/setfattr.c @@ -29,13 +29,12 @@ GLOBALS( void setfattr_main(void) { - int h = toys.optflags & FLAG_h, rc; + int h = FLAG(h), rc; char **s; - for (s=toys.optargs; *s; s++) { - if (TT.x) rc = (h?lremovexattr:removexattr)(*s, TT.x); - else rc = (h?lsetxattr:setxattr)(*s, TT.n, TT.v, TT.v?strlen(TT.v):0, 0); - + for (s = toys.optargs; *s; s++) { + if (TT.x) rc = (h ? lremovexattr : removexattr)(*s, TT.x); + else rc = (h ? lsetxattr : setxattr)(*s, TT.n, TT.v, TT.v ? strlen(TT.v) : 0, 0); if (rc) perror_msg_raw(*s); } } diff --git a/toys/other/swapon.c b/toys/other/swapon.c index 2eb5b93c..e27517de 100644 --- a/toys/other/swapon.c +++ b/toys/other/swapon.c @@ -25,12 +25,10 @@ GLOBALS( void swapon_main(void) { - // 0x70000 = SWAP_FLAG_DISCARD|SWAP_FLAG_DISCARD_ONCE|SWAP_FLAG_DISCARD_PAGES - int flags = (toys.optflags&FLAG_d)*0x70000; - - if (toys.optflags) - flags |= SWAP_FLAG_PREFER | (TT.p << SWAP_FLAG_PRIO_SHIFT); + // SWAP_FLAG_DISCARD|SWAP_FLAG_DISCARD_ONCE|SWAP_FLAG_DISCARD_PAGES + int flags = FLAG(d)*0x70000; + if (FLAG(p)) flags |= SWAP_FLAG_PREFER | (TT.p << SWAP_FLAG_PRIO_SHIFT); if (swapon(*toys.optargs, flags)) perror_exit("Couldn't swapon '%s'", *toys.optargs); } diff --git a/toys/other/sysctl.c b/toys/other/sysctl.c index 2d79176f..4af3426a 100644 --- a/toys/other/sysctl.c +++ b/toys/other/sysctl.c @@ -43,9 +43,8 @@ static void replace_char(char *str, char old, char new) static void key_error(char *key) { - if (errno == ENOENT) { - if (!(toys.optflags & FLAG_e)) error_msg("unknown key '%s'", key); - } else perror_msg("key '%s'", key); + if (errno != ENOENT) perror_msg("key '%s'", key); + else if (!FLAG(e)) error_msg("unknown key '%s'", key); } static int write_key(char *path, char *key, char *value) @@ -77,11 +76,11 @@ static int do_show_keys(struct dirtree *dt) if (!data) key_error(key); else { // Print the parts that aren't switched off by flags. - if (!(toys.optflags & FLAG_n)) xprintf("%s", key); - if (!(toys.optflags & (FLAG_N|FLAG_n))) xprintf(" = "); + if (!FLAG(n)) xprintf("%s", key); + if (!FLAG(N) && !FLAG(n)) xprintf(" = "); for (key = data+strlen(data); key > data && isspace(*--key); *key = 0); - if (!(toys.optflags & FLAG_N)) xprintf("%s", data); - if ((toys.optflags & (FLAG_N|FLAG_n)) != (FLAG_N|FLAG_n)) xputc('\n'); + if (!FLAG(N)) xprintf("%s", data); + if (!FLAG(N) || !FLAG(n)) xputc('\n'); } free(data); @@ -96,16 +95,11 @@ static void process_key(char *key, char *value) char *path; if (!value) value = split_key(key); - if ((toys.optflags & FLAG_w) && !value) { - error_msg("'%s' not key=value", key); - - return; - } - + if (FLAG(w) && !value) return error_msg("'%s' not key=value", key); path = xmprintf("/proc/sys/%s", key); replace_char(path, '.', '/'); // Note: failure to assign to a non-leaf node suppresses the display. - if (!(value && (!write_key(path, key, value) || (toys.optflags & FLAG_q)))) { + if (!(value && (!write_key(path, key, value) || FLAG(q)))) { if (!access(path, R_OK)) dirtree_read(path, do_show_keys); else key_error(key); } @@ -117,10 +111,10 @@ void sysctl_main() char **args = 0; // Display all keys - if (toys.optflags & FLAG_a) dirtree_read("/proc/sys", do_show_keys); + if (FLAG(a)) dirtree_read("/proc/sys", do_show_keys); // read file - else if (toys.optflags & FLAG_p) { + else if (FLAG(p)) { FILE *fp = xfopen(*toys.optargs ? *toys.optargs : "/etc/sysctl.conf", "r"); size_t len; diff --git a/toys/posix/cat.c b/toys/posix/cat.c index 1eb15d49..1226651f 100644 --- a/toys/posix/cat.c +++ b/toys/posix/cat.c @@ -51,7 +51,7 @@ static void do_cat(int fd, char *name) if (c<32) { if (c == 10) { if (FLAG(e)) xputc('$'); - } else if (toys.optflags & (c==9 ? FLAG_t : FLAG_v)) { + } else if (c==9 ? FLAG(t) : FLAG(v)) { printf("^%c", c+'@'); continue; } diff --git a/toys/posix/du.c b/toys/posix/du.c index 0544aeee..a51dfc11 100644 --- a/toys/posix/du.c +++ b/toys/posix/du.c @@ -158,8 +158,7 @@ void du_main(void) // Loop over command line arguments, recursing through children for (args = toys.optc ? toys.optargs : noargs; *args; args++) - dirtree_flagread(*args, DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)), - do_du); + dirtree_flagread(*args, DIRTREE_SYMFOLLOW*(FLAG(H)|FLAG(L)), do_du); if (FLAG(c)) print(FLAG(b) ? TT.total : TT.total*512, 0); if (CFG_TOYBOX_FREE) seen_inode(TT.inodes, 0); diff --git a/toys/posix/getconf.c b/toys/posix/getconf.c index 7c2e588f..c553d258 100644 --- a/toys/posix/getconf.c +++ b/toys/posix/getconf.c @@ -200,14 +200,14 @@ static void show_conf(int i, struct config *c, const char *path) void getconf_main(void) { struct config *configs[] = {sysconfs, pathconfs, confstrs, limits, others}, - *c = NULL; + *c = 0; int i, j, lens[] = {ARRAY_LEN(sysconfs), ARRAY_LEN(pathconfs), ARRAY_LEN(confstrs), ARRAY_LEN(limits), ARRAY_LEN(others)}; char *name, *path = (toys.optc==2) ? toys.optargs[1] : "/", *config_names[] = {"sysconf(3)", "pathconf(3)", "confstr(3)", "", "Misc"}; - if (toys.optflags&FLAG_a) { + if (FLAG(a)) { for (i = 0; i<5; i++) { for (j = 0; jlsm_set_create(TT.Z)) perror_exit("-Z '%s' failed", TT.Z); diff --git a/toys/posix/mkfifo.c b/toys/posix/mkfifo.c index 8639ebd8..366ac436 100644 --- a/toys/posix/mkfifo.c +++ b/toys/posix/mkfifo.c @@ -28,8 +28,7 @@ config MKFIFO_Z #include "toys.h" GLOBALS( - char *m; - char *Z; + char *m, *Z; mode_t mode; ) @@ -39,9 +38,9 @@ void mkfifo_main(void) char **s; TT.mode = 0666; - if (toys.optflags & FLAG_m) TT.mode = string_to_mode(TT.m, 0); + if (FLAG(m)) TT.mode = string_to_mode(TT.m, 0); - if (CFG_MKFIFO_Z && (toys.optflags&FLAG_Z)) + if (CFG_MKFIFO_Z && FLAG(Z)) if (0>lsm_set_create(TT.Z)) perror_exit("-Z '%s' failed", TT.Z); diff --git a/toys/posix/paste.c b/toys/posix/paste.c index 5c1b234c..e685c506 100644 --- a/toys/posix/paste.c +++ b/toys/posix/paste.c @@ -35,7 +35,7 @@ static void paste_files(void) { FILE **fps = (void *)toybuf; char *dpos, *dstr, *buf, c; - int i, any, dcount, dlen, len, seq = toys.optflags&FLAG_s; + int i, any, dcount, dlen, len, seq = FLAG(s); // Loop through lines until no input left for (;;) { @@ -114,7 +114,7 @@ static void do_paste(int fd, char *name) if (!(fps[TT.files++] = (fd ? fdopen(fd, "r") : stdin))) perror_exit(0); if (TT.files >= sizeof(toybuf)/sizeof(FILE *)) perror_exit("tilt"); - if (toys.optflags&FLAG_s) { + if (FLAG(s)) { paste_files(); xputc('\n'); TT.files = 0; @@ -123,8 +123,8 @@ static void do_paste(int fd, char *name) void paste_main(void) { - if (!(toys.optflags&FLAG_d)) TT.d = "\t"; + if (!FLAG(d)) TT.d = "\t"; loopfiles_rw(toys.optargs, O_RDONLY, 0, do_paste); - if (!(toys.optflags&FLAG_s)) paste_files(); + if (!FLAG(s)) paste_files(); } diff --git a/toys/posix/rm.c b/toys/posix/rm.c index cfe282f7..27a461ff 100644 --- a/toys/posix/rm.c +++ b/toys/posix/rm.c @@ -32,7 +32,7 @@ static int do_rm(struct dirtree *try) // Intentionally fail non-recursive attempts to remove even an empty dir // (via wrong flags to unlinkat) because POSIX says to. - if (dir && !(toys.optflags & (FLAG_r|FLAG_R))) goto skip; + if (dir && !FLAG(r) && !FLAG(R)) goto skip; // This is either the posix section 2(b) prompt or the section 3 prompt. if (!FLAG(f) diff --git a/toys/posix/touch.c b/toys/posix/touch.c index e687ff81..0196b305 100644 --- a/toys/posix/touch.c +++ b/toys/posix/touch.c @@ -37,7 +37,7 @@ void touch_main(void) { struct timespec ts[2]; char **ss; - int fd, i; + int fd; // use current time if no -t or -d ts[0].tv_nsec = UTIME_NOW; @@ -61,8 +61,7 @@ void touch_main(void) } // Which time(s) should we actually change? - i = toys.optflags & (FLAG_a|FLAG_m); - if (i && i!=(FLAG_a|FLAG_m)) ts[i!=FLAG_m].tv_nsec = UTIME_OMIT; + if (FLAG(a)^FLAG(m)) ts[!FLAG(m)].tv_nsec = UTIME_OMIT; // Loop through files on command line for (ss = toys.optargs; *ss;) { -- 2.39.2