From 6a9ab4f24492909bd0271fc756168ad6179fdcbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Sat, 26 Jul 2025 06:03:41 +0000 Subject: [PATCH 01/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=B7=AE=E5=BC=82?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/other/reboot.c | 24 +++++++----------------- toys/other/shred.c | 9 +++++---- toys/other/switch_root.c | 2 +- toys/other/uptime.c | 22 ++++++++++++---------- toys/other/watch.c | 12 ++++++------ toys/pending/bc.c | 2 +- toys/pending/diff.c | 2 +- toys/pending/ipcs.c | 3 --- toys/pending/lsof.c | 8 +++++--- toys/pending/traceroute.c | 3 ++- toys/posix/cksum.c | 2 +- toys/posix/false.c | 4 +--- toys/posix/iconv.c | 8 ++++---- toys/posix/kill.c | 19 ++++++++----------- toys/posix/pwd.c | 17 +++++++++-------- toys/posix/rm.c | 16 ++++------------ toys/posix/rmdir.c | 8 ++++---- toys/posix/sleep.c | 6 +++--- toys/posix/sort.c | 4 ++-- toys/posix/split.c | 17 +++++++---------- toys/posix/tee.c | 3 +-- toys/posix/true.c | 4 +--- toys/posix/uudecode.c | 2 +- 23 files changed, 86 insertions(+), 111 deletions(-) diff --git a/toys/other/reboot.c b/toys/other/reboot.c index 4770d1f..f31ab00 100644 --- a/toys/other/reboot.c +++ b/toys/other/reboot.c @@ -2,7 +2,7 @@ * * Copyright 2013 Elie De Brauwer -USE_REBOOT(NEWTOY(reboot, "d:fn", TOYFLAG_SBIN|TOYFLAG_NEEDROOT)) +USE_REBOOT(NEWTOY(reboot, "fn", TOYFLAG_SBIN|TOYFLAG_NEEDROOT)) USE_REBOOT(OLDTOY(halt, reboot, TOYFLAG_SBIN|TOYFLAG_NEEDROOT)) USE_REBOOT(OLDTOY(poweroff, reboot, TOYFLAG_SBIN|TOYFLAG_NEEDROOT)) @@ -10,37 +10,27 @@ config REBOOT bool "reboot" default y help - usage: reboot/halt/poweroff [-fn] [-d DELAY] + usage: reboot/halt/poweroff [-fn] - Restart, halt, or power off the system. + Restart, halt or powerdown the system. -d Wait DELAY before proceeding (in seconds or m/h/d suffix: -d 1.5m = 90s) - -f Force reboot (don't signal init, reboot directly) - -n Don't sync filesystems before reboot + -f Don't signal init + -n Don't sync before stopping the system */ #define FOR_reboot #include "toys.h" #include -GLOBALS( - char *d; -) - void reboot_main(void) { - struct timespec ts; int types[] = {RB_AUTOBOOT, RB_HALT_SYSTEM, RB_POWER_OFF}, sigs[] = {SIGTERM, SIGUSR1, SIGUSR2}, idx; - if (TT.d) { - xparsetimespec(TT.d, &ts); - nanosleep(&ts, NULL); - } - - if (!FLAG(n)) sync(); + if (!(toys.optflags & FLAG_n)) sync(); idx = stridx("hp", *toys.which->name)+1; - if (FLAG(f)) toys.exitval = reboot(types[idx]); + if (toys.optflags & FLAG_f) toys.exitval = reboot(types[idx]); else toys.exitval = kill(1, sigs[idx]); } diff --git a/toys/other/shred.c b/toys/other/shred.c index 529e2d2..4e56ce4 100644 --- a/toys/other/shred.c +++ b/toys/other/shred.c @@ -10,7 +10,7 @@ config SHRED bool "shred" default y help - usage: shred [-fuxz] [-n COUNT] [-o OFFSET] [-s SIZE] FILE... + usage: shred [-fuz] [-n COUNT] [-s SIZE] FILE... Securely delete a file by overwriting its contents with random data. @@ -37,7 +37,7 @@ void shred_main(void) { char **try; - if (!FLAG(n)) TT.n++; + if (!(toys.optflags & FLAG_n)) TT.n++; // We don't use loopfiles() here because "-" isn't stdin, and want to // respond to files we can't open via chmod. @@ -47,7 +47,7 @@ void shred_main(void) int fd = open(*try, O_RDWR), iter = 0, throw; // do -f chmod if necessary - if (fd == -1 && FLAG(f)) { + if (fd == -1 && (toys.optflags & FLAG_f)) { #ifdef TOYBOX_OH_ADAPT chmod(*try, 0660); #else @@ -98,6 +98,7 @@ void shred_main(void) if (throw != writeall(fd, toybuf, throw)) perror_msg_raw(*try); pos += throw; } - if (FLAG(u) && unlink(*try)) perror_msg("unlink '%s'", *try); + if (toys.optflags & FLAG_u) + if (unlink(*try) && unlink(*try)) perror_msg("unlink '%s'", *try); } } diff --git a/toys/other/switch_root.c b/toys/other/switch_root.c index 3df703d..9671a97 100644 --- a/toys/other/switch_root.c +++ b/toys/other/switch_root.c @@ -46,7 +46,7 @@ void switch_root_main(void) char *newroot = *toys.optargs, **cmdline = toys.optargs+1; struct stat st1, st2; struct statfs stfs; - int console QUIET; + int console = console;//gcc's "may be used" warnings are broken. if (getpid() != 1) error_exit("not pid 1"); diff --git a/toys/other/uptime.c b/toys/other/uptime.c index 6c0c805..d889c3e 100644 --- a/toys/other/uptime.c +++ b/toys/other/uptime.c @@ -62,17 +62,19 @@ void uptime_main(void) days, (days!=1)?"s":"", hours, (hours!=1)?"s":"", minutes, (minutes!=1)?"s":""); - } else { - xprintf(" %02d:%02d:%02d up ", tm->tm_hour, tm->tm_min, tm->tm_sec); - if (days) xprintf("%d day%s, ", days, (days!=1)?"s":""); - if (hours) xprintf("%2d:%02d, ", hours, minutes); - else printf("%d min, ", minutes); + return; + } + xprintf(" %02d:%02d:%02d up ", tm->tm_hour, tm->tm_min, tm->tm_sec); + if (days) xprintf("%d day%s, ", days, (days!=1)?"s":""); + if (hours) xprintf("%2d:%02d, ", hours, minutes); + else printf("%d min, ", minutes); + + // Obtain info about logged on users + setutxent(); + while ((entry = getutxent())) if (entry->ut_type == USER_PROCESS) users++; + endutxent(); - // Obtain info about logged on users - setutxent(); - while ((entry = getutxent())) if (entry->ut_type == USER_PROCESS) users++; - endutxent(); - printf(" %d user%s, ", users, (users!=1) ? "s" : ""); + printf(" %d user%s, ", users, (users!=1) ? "s" : ""); } printf(" load average: %.02f, %.02f, %.02f\n", info.loads[0]/65536.0, diff --git a/toys/other/watch.c b/toys/other/watch.c index 1c31394..06c65a0 100644 --- a/toys/other/watch.c +++ b/toys/other/watch.c @@ -33,7 +33,7 @@ GLOBALS( ) // When a child process exits, stop tracking them. Handle errors for -be -static void watch_child(int sig) +void watch_child(int sig) { int status; pid_t pid = wait(&status); @@ -41,8 +41,8 @@ static void watch_child(int sig) status = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+127; if (status) { // TODO should this be beep()? - if (FLAG(b)) putchar('\b'); - if (FLAG(e)) { + if (toys.optflags&FLAG_b) putchar('\b'); + if (toys.optflag&FLAG_e) { printf("Exit status %d\r\n", status); tty_reset(); _exit(status); @@ -55,7 +55,7 @@ static void watch_child(int sig) // Return early for low-ascii characters with special behavior, // discard remaining low ascii, escape other unprintable chars normally -static int watch_escape(FILE *out, int cols, int wc) +int watch_escape(FILE *out, int cols, int wc) { if (wc==27 || (wc>=7 && wc<=13)) return -1; if (wc < 32) return 0; @@ -67,7 +67,7 @@ void watch_main(void) { char *cmdv[] = {"/bin/sh", "-c", 0, 0}, *cmd, *ss; long long now, then = millitime(); - unsigned width, height, i, cmdlen, len, xx QUIET, yy QUIET, active QUIET; + unsigned width, height, i, cmdlen, len, xx = xx, yy = yy, active = active; struct pollfd pfd[2]; pid_t pid = 0; int fds[2], cc; @@ -99,7 +99,7 @@ void watch_main(void) start_redraw(&width, &height); // redraw the header - if (!FLAG(t)) { + if (!(toys.optflags&FLAG_t) { time_t t = time(0); int pad, ctimelen; diff --git a/toys/pending/bc.c b/toys/pending/bc.c index 2d92755..5d0e98e 100644 --- a/toys/pending/bc.c +++ b/toys/pending/bc.c @@ -1197,7 +1197,7 @@ ssize_t bc_num_cmp(BcNum *a, BcNum *b) { a_int -= b_int; a_max = (a->rdx > b->rdx); - if (a_int) return neg ? -((ssize_t) a_int) : (ssize_t) a_int; + if (a_int) return (ssize_t) a_int; if (a_max) { min = b->rdx; diff --git a/toys/pending/diff.c b/toys/pending/diff.c index e93c622..d7bb43c 100644 --- a/toys/pending/diff.c +++ b/toys/pending/diff.c @@ -253,7 +253,7 @@ int bcomp(const void *a, const void *b) * classes of lines in file[1], with e.last = true on the last element of each class. * The elements are ordered by serial within classes. * 4. Form the p vector stored in p_vector. p_vector[i], if non-zero, now points in e vector - * to the beginning of the equiv class of lines in file[1] equivalent to line + * to the begining of the equiv class of lines in file[1] equivalent to line * i in file[0]. * 5. Form the k-candidates as discribed in do_merge. * 6. Create a vector J[i] = j, such that i'th line in file[0] is j'th line of diff --git a/toys/pending/ipcs.c b/toys/pending/ipcs.c index 55f8d98..8ad7c73 100644 --- a/toys/pending/ipcs.c +++ b/toys/pending/ipcs.c @@ -432,9 +432,6 @@ void ipcs_main(void) { #ifdef TOYBOX_OH_ADAPT //delete 'ipcs -s' fail problem: kernel is not configured for semaphores - if (flag(s)) { - error_exit("Unknown option 's' (see \"ipcs --help\")"); - } if (flag(i)) { if (flag(m)) show_shm_id(); else if (flag(q)) show_msg_id(); diff --git a/toys/pending/lsof.c b/toys/pending/lsof.c index 9050ff5..0df6a79 100644 --- a/toys/pending/lsof.c +++ b/toys/pending/lsof.c @@ -120,7 +120,7 @@ static void scan_proc_net_file(char *path, int family, char type, if (!fp) return; - if (getline(&line, &line_length, fp) <= 0) return; // Skip header. + if (getline(&line, &line_length, fp)) return; // Skip header. while (getline(&line, &line_length, fp) > 0) { fn(line, family, type); @@ -336,7 +336,7 @@ static void visit_maps(struct proc_info *pi) FILE *fp; unsigned long long offset; long inode; - char *line = NULL, device[10]; // xxx:xxxxx\0 + char *line = NULL, device[10]; size_t line_length = 0; struct file_info *fi; @@ -347,8 +347,10 @@ static void visit_maps(struct proc_info *pi) while (getline(&line, &line_length, fp) > 0) { int name_pos; - if (sscanf(line, "%*x-%*x %*s %llx %9s %ld %n", + if (sscanf(line, "%*x-%*x %*s %llx %s %ld %n", &offset, device, &inode, &name_pos) >= 3) { + struct file_info *fi; + // Ignore non-file maps. if (inode == 0 || !strcmp(device, "00:00")) continue; // TODO: show unique maps even if they have a non-zero offset? diff --git a/toys/pending/traceroute.c b/toys/pending/traceroute.c index 559c077..4484580 100644 --- a/toys/pending/traceroute.c +++ b/toys/pending/traceroute.c @@ -477,7 +477,7 @@ static void do_trace() void traceroute_main(void) { - unsigned pack_size = 0, tyser = 0; + unsigned opt_len = 0, tyser = 0; int lsrr = 0, set = 1; if(!(toys.optflags & FLAG_4) && @@ -499,6 +499,7 @@ void traceroute_main(void) resolve_addr(node->arg, AF_INET, SOCK_STREAM, 0, &sin); TT.gw_list[lsrr] = sin.sin_addr.s_addr; } + opt_len = (lsrr + 1) * sizeof(TT.gw_list[0]); } else TT.first_ttl = 1; TT.msg_len = pack_size = ICMP_HD_SIZE4; //udp payload is also 8bytes diff --git a/toys/posix/cksum.c b/toys/posix/cksum.c index 6e5b915..ee7c6f5 100644 --- a/toys/posix/cksum.c +++ b/toys/posix/cksum.c @@ -11,7 +11,7 @@ config CKSUM bool "cksum" default y help - usage: cksum [-IPLN] [FILE...] + usage: cksum [-IPLN] [file...] For each file, output crc32 checksum value, length and name of file. If no files listed, copy from stdin. Filename "-" is a synonym for stdin. diff --git a/toys/posix/false.c b/toys/posix/false.c index e933799..0853b01 100644 --- a/toys/posix/false.c +++ b/toys/posix/false.c @@ -4,14 +4,12 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/false.html -USE_FALSE(NEWTOY(false, NULL, TOYFLAG_BIN|TOYFLAG_NOHELP|TOYFLAG_MAYFORK)) +USE_FALSE(NEWTOY(false, NULL, TOYFLAG_BIN|TOYFLAG_NOHELP)) config FALSE bool "false" default y help - usage: false - Return nonzero. */ diff --git a/toys/posix/iconv.c b/toys/posix/iconv.c index d272167..ce375b4 100644 --- a/toys/posix/iconv.c +++ b/toys/posix/iconv.c @@ -18,8 +18,8 @@ config ICONV Convert character encoding of files. -c Omit invalid chars - -f Convert from (default UTF-8) - -t Convert to (default UTF-8) + -f Convert from (default utf8) + -t Convert to (default utf8) */ #define FOR_iconv @@ -63,8 +63,8 @@ static void do_iconv(int fd, char *name) void iconv_main(void) { - if (!TT.t) TT.t = "UTF-8"; - if (!TT.f) TT.f = "UTF-8"; + if (!TT.t) TT.t = "utf8"; + if (!TT.f) TT.f = "utf8"; if ((iconv_t)-1 == (TT.ic = iconv_open(TT.t, TT.f))) perror_exit("%s/%s", TT.t, TT.f); diff --git a/toys/posix/kill.c b/toys/posix/kill.c index 2767659..c9de98e 100644 --- a/toys/posix/kill.c +++ b/toys/posix/kill.c @@ -11,14 +11,14 @@ * * No Standard -USE_KILL(NEWTOY(kill, "?ls: ", TOYFLAG_BIN|TOYFLAG_MAYFORK)) +USE_KILL(NEWTOY(kill, "?ls: ", TOYFLAG_BIN)) USE_KILLALL5(NEWTOY(killall5, "?o*ls: [!lo][!ls]", TOYFLAG_SBIN)) config KILL bool "kill" default y help - usage: kill [-l [SIGNAL] | -s SIGNAL | -SIGNAL] PID... + usage: kill [-l [SIGNAL] | -s SIGNAL | -SIGNAL] pid... Send signal to process(es). @@ -65,13 +65,12 @@ void kill_main(void) if (FLAG(l)) { if (*args) { int signum = sig_to_num(*args); - char *s = 0; + char *s = NULL; if (signum>=0) s = num_to_sig(signum&127); if (isdigit(**args)) puts(s ? s : "UNKNOWN"); else printf("%d\n", signum); } else list_signals(); - return; } @@ -81,7 +80,6 @@ void kill_main(void) if (TT.s) { char *arg; int i = strtol(TT.s, &arg, 10); - if (!*arg) arg = num_to_sig(i); else arg = TT.s; @@ -108,10 +106,7 @@ void kill_main(void) sid = getsid(pid = getpid()); - if (!(dp = opendir("/proc"))) { - free(olist); - perror_exit("/proc"); - } + if (!(dp = opendir("/proc"))) perror_exit("/proc"); while ((entry = readdir(dp))) { int count, procpid, procsid; @@ -133,8 +128,10 @@ void kill_main(void) kill(procpid, signum); } - closedir(dp); - free(olist); + if (CFG_TOYBOX_FREE) { + closedir(dp); + free(olist); + } // is it kill? } else { diff --git a/toys/posix/pwd.c b/toys/posix/pwd.c index c7dc78f..d0842bd 100644 --- a/toys/posix/pwd.c +++ b/toys/posix/pwd.c @@ -4,7 +4,7 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/pwd.html -USE_PWD(NEWTOY(pwd, ">0LP[-LP]", TOYFLAG_BIN|TOYFLAG_MAYFORK)) +USE_PWD(NEWTOY(pwd, ">0LP[-LP]", TOYFLAG_BIN)) config PWD bool "pwd" @@ -26,7 +26,7 @@ void pwd_main(void) char *s, *pwd = getcwd(0, 0), *PWD; // Only use $PWD if it's an absolute path alias for cwd with no "." or ".." - if (!FLAG(P) && (s = PWD = getenv("PWD"))) { + if (!(toys.optflags & FLAG_P) && (s = PWD = getenv("PWD"))) { struct stat st1, st2; while (*s == '/') { @@ -37,16 +37,17 @@ void pwd_main(void) while (*s && *s != '/') s++; } if (!*s && s != PWD) s = PWD; - else s = 0; + else s = NULL; // If current directory exists, make sure it matches. if (s && pwd) if (stat(pwd, &st1) || stat(PWD, &st2) || st1.st_ino != st2.st_ino || - st1.st_dev != st2.st_dev) s = 0; - } else s = 0; + st1.st_dev != st2.st_dev) s = NULL; + } else s = NULL; // If -L didn't give us a valid path, use cwd. - if (s || (s = pwd)) puts(s); - free(pwd); - if (!s) perror_exit("xgetcwd"); + if (!s && (s = pwd)) perror_exit("xgetcwd"); + xprintf("%s\n", s); + + if (CFG_TOYBOX_FREE) free(pwd); } diff --git a/toys/posix/rm.c b/toys/posix/rm.c index be611e6..b24b90b 100644 --- a/toys/posix/rm.c +++ b/toys/posix/rm.c @@ -37,10 +37,6 @@ static int do_rm(struct dirtree *try) // This is either the posix section 2(b) prompt or the section 3 prompt. if (!FLAG(f) && (!S_ISLNK(try->st.st_mode) && faccessat(fd, try->name, W_OK, 0))) or++; - - // Posix section 1(a), don't prompt for nonexistent. - if (or && errno == ENOENT) goto skip; - if (!(dir && try->again) && ((or && isatty(0)) || FLAG(i))) { char *s = dirtree_path(try, 0); @@ -99,19 +95,15 @@ void rm_main(void) error_msg("rm /. if you mean it"); continue; } - // "rm dir/.*" can expand to include .. which generally isn't what you want - if (!strcmp("..", basename(*s))) { - error_msg("bad path %s", *s); - continue; - } - // Files that already don't exist aren't errors for -f. Use lstat() instead - // of faccessat() because bionic doesn't support AT_SYMLINK_NOFOLLOW - if (FLAG(f) && lstat(*s, (void *)toybuf) && errno == ENOENT) continue; + // Files that already don't exist aren't errors for -f. so try a quick + // unlink now to see if it succeeds or reports that it didn't exist. + if (FLAG(f) && lstat(!unlink(*s) || errno == ENOENT) continue; // There's a race here where a file removed between the above check and // dirtree's stat would report the nonexistence as an error, but that's // not a normal "it didn't exist" so I'm ok with it. + dirtree_read(*s, do_rm); } } diff --git a/toys/posix/rmdir.c b/toys/posix/rmdir.c index 3714f33..26674db 100644 --- a/toys/posix/rmdir.c +++ b/toys/posix/rmdir.c @@ -4,13 +4,13 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/rmdir.html -USE_RMDIR(NEWTOY(rmdir, "<1(ignore-fail-on-non-empty)p(parents)", TOYFLAG_BIN)) +USE_RMDIR(NEWTOY(rmdir, "<1(ignore-fail-on-non-empty)p", TOYFLAG_BIN)) config RMDIR bool "rmdir" default y help - usage: rmdir [-p] [DIR...] + usage: rmdir [-p] [dirname...] Remove one or more directories. @@ -35,11 +35,11 @@ static void do_rmdir(char *name) // Each -p cycle back up one slash, ignoring trailing and repeated /. if (!toys.optflags) return; - do { + for (::) { if (!(temp = strrchr(name, '/'))) return; *temp = 0; } while (!temp[1]); - } while (*name); + } } void rmdir_main(void) diff --git a/toys/posix/sleep.c b/toys/posix/sleep.c index 73f03fb..846df80 100644 --- a/toys/posix/sleep.c +++ b/toys/posix/sleep.c @@ -23,8 +23,8 @@ config SLEEP void sleep_main(void) { - struct timespec ts; + struct timespec tv; - xparsetimespec(*toys.optargs, &ts); - toys.exitval = !!nanosleep(&ts, NULL); + tv.tv_sec = xparsetime(*toys.optargs, 9, &tv.tv_nsec); + toys.exitval = !!nanosleep(&tv, NULL); } diff --git a/toys/posix/sort.c b/toys/posix/sort.c index 9f38be8..2808142 100644 --- a/toys/posix/sort.c +++ b/toys/posix/sort.c @@ -283,7 +283,7 @@ static void sort_lines(char **pline, long len) if (!pline) return; line = *pline; if (!FLAG(z) && len && line[len-1]=='\n') line[--len] = 0; - *pline = 0; + *pline = NULL; // handle -c here so we don't allocate more memory than necessary. if (FLAG(c)) { @@ -292,7 +292,7 @@ static void sort_lines(char **pline, long len) if (TT.lines && compare_keys((void *)&TT.lines, &line)>j) error_exit("%s: Check line %d\n", TT.name, TT.linecount); free(TT.lines); - TT.lines = (void *)line; + TT.lines = (char **)line; } else { if (!(TT.linecount&63)) TT.lines = xrealloc(TT.lines, sizeof(char *)*(TT.linecount+64)); diff --git a/toys/posix/split.c b/toys/posix/split.c index 293343b..ea0fb7d 100644 --- a/toys/posix/split.c +++ b/toys/posix/split.c @@ -8,13 +8,13 @@ * - should splitting an empty file produce an empty outfile? (Went with "no".) * - permissions on output file -USE_SPLIT(NEWTOY(split, ">2a#<1=2>9b#<1l#<1n#<1[!bl][!bn][!ln]", TOYFLAG_USR|TOYFLAG_BIN)) +USE_SPLIT(NEWTOY(split, ">2a#<1=2>9b#<1l#<1[!b]", TOYFLAG_USR|TOYFLAG_BIN)) config SPLIT bool "split" default y help - usage: split [-a SUFFIX_LEN] [-b BYTES] [-l LINES] [-n PARTS] [INPUT [OUTPUT]] + usage: split [-a SUFFIX_LEN] [-b BYTES] [-l LINES] [INPUT [OUTPUT]] Copy INPUT (or stdin) data to a series of OUTPUT (or "x") files with alphabetically increasing suffix (aa, ab, ac... az, ba, bb...). @@ -22,14 +22,13 @@ config SPLIT -a Suffix length (default 2) -b BYTES/file (10, 10k, 10m, 10g...) -l LINES/file (default 1000) - -n PARTS many equal length files */ #define FOR_split #include "toys.h" GLOBALS( - long n, l, b, a; + long l, b, a; char *outfile; ) @@ -42,10 +41,8 @@ static void do_split(int infd, char *in) // posix doesn't cover permissions on output file, so copy input (or 0777) st.st_mode = 0777; - st.st_size = 0; fstat(infd, &st); - if (TT.n && (TT.b = st.st_size/TT.n)<1) return error_msg("%s: no size", in); len = pos = filenum = bytesleft = linesleft = 0; for (;;) { int i, j; @@ -66,9 +63,9 @@ static void do_split(int infd, char *in) j /= 26; } if (j) error_exit("bad suffix"); - bytesleft = TT.b + ((filenum == TT.n) ? st.st_size%TT.n : 0); + bytesleft = TT.b; linesleft = TT.l; - xclose(outfd); + if (outfd != -1) close(outfd); outfd = xcreate(TT.outfile, O_RDWR|O_CREAT|O_TRUNC, st.st_mode & 0777); } @@ -89,7 +86,7 @@ static void do_split(int infd, char *in) } if (CFG_TOYBOX_FREE) { - xclose(outfd); + if (outfd != -1) close(outfd); if (infd) close(infd); free(TT.outfile); } @@ -98,7 +95,7 @@ static void do_split(int infd, char *in) void split_main(void) { - if (!TT.b && !TT.l && !TT.n) TT.l = 1000; + if (!TT.b && !TT.l) TT.l = 1000; // Allocate template for output filenames TT.outfile = xmprintf("%s%*c", (toys.optc == 2) ? toys.optargs[1] : "x", diff --git a/toys/posix/tee.c b/toys/posix/tee.c index 88f7361..60e7a33 100644 --- a/toys/posix/tee.c +++ b/toys/posix/tee.c @@ -10,7 +10,7 @@ config TEE bool "tee" default y help - usage: tee [-ai] [FILE...] + usage: tee [-ai] [file...] Copy stdin to each listed file, and also to stdout. Filename "-" is a synonym for stdout. @@ -24,7 +24,6 @@ config TEE GLOBALS( void *outputs; - int out; ) struct fd_list { diff --git a/toys/posix/true.c b/toys/posix/true.c index 8e56269..2543b82 100644 --- a/toys/posix/true.c +++ b/toys/posix/true.c @@ -4,15 +4,13 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/true.html -USE_TRUE(NEWTOY(true, NULL, TOYFLAG_BIN|TOYFLAG_NOHELP|TOYFLAG_MAYFORK)) +USE_TRUE(NEWTOY(true, NULL, TOYFLAG_BIN|TOYFLAG_NOHELP)) USE_TRUE(OLDTOY(:, true, TOYFLAG_NOFORK|TOYFLAG_NOHELP)) config TRUE bool "true" default y help - usage: true - Return zero. */ diff --git a/toys/posix/uudecode.c b/toys/posix/uudecode.c index 43f8861..4d87ce1 100644 --- a/toys/posix/uudecode.c +++ b/toys/posix/uudecode.c @@ -27,7 +27,7 @@ GLOBALS( void uudecode_main(void) { FILE *ifp = stdin; - int ofd, idx = 0, m QUIET, n; + int ofd, idx = 0, m = m, n; size_t allocated_length; char *line = 0, mode[16], *class[] = {"begin%*[ ]%15s%*[ ]%n", "begin-base64%*[ ]%15s%*[ ]%n"}; -- Gitee From 446b709fecce5509ec7086574a82754119dee3bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Sat, 26 Jul 2025 06:42:55 +0000 Subject: [PATCH 02/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=B7=AE=E5=BC=82?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/other/uptime.c | 2 -- toys/other/watch.c | 2 +- toys/posix/rm.c | 2 +- toys/posix/rmdir.c | 4 ++-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/toys/other/uptime.c b/toys/other/uptime.c index d889c3e..d2f1ca7 100644 --- a/toys/other/uptime.c +++ b/toys/other/uptime.c @@ -75,8 +75,6 @@ void uptime_main(void) endutxent(); printf(" %d user%s, ", users, (users!=1) ? "s" : ""); - } - printf(" load average: %.02f, %.02f, %.02f\n", info.loads[0]/65536.0, info.loads[1]/65536.0, info.loads[2]/65536.0); } diff --git a/toys/other/watch.c b/toys/other/watch.c index 06c65a0..9d7c9fe 100644 --- a/toys/other/watch.c +++ b/toys/other/watch.c @@ -42,7 +42,7 @@ void watch_child(int sig) if (status) { // TODO should this be beep()? if (toys.optflags&FLAG_b) putchar('\b'); - if (toys.optflag&FLAG_e) { + if (toys.optflags&FLAG_e) { printf("Exit status %d\r\n", status); tty_reset(); _exit(status); diff --git a/toys/posix/rm.c b/toys/posix/rm.c index b24b90b..03cec94 100644 --- a/toys/posix/rm.c +++ b/toys/posix/rm.c @@ -98,7 +98,7 @@ void rm_main(void) // Files that already don't exist aren't errors for -f. so try a quick // unlink now to see if it succeeds or reports that it didn't exist. - if (FLAG(f) && lstat(!unlink(*s) || errno == ENOENT) continue; + if (FLAG(f) && (!unlink(*s) || errno == ENOENT)) continue; // There's a race here where a file removed between the above check and // dirtree's stat would report the nonexistence as an error, but that's diff --git a/toys/posix/rmdir.c b/toys/posix/rmdir.c index 26674db..d04cf0d 100644 --- a/toys/posix/rmdir.c +++ b/toys/posix/rmdir.c @@ -25,7 +25,7 @@ static void do_rmdir(char *name) { char *temp; - do { + for (;;) { if (rmdir(name)) { if (!FLAG(ignore_fail_on_non_empty) || errno != ENOTEMPTY) perror_msg_raw(name); @@ -35,7 +35,7 @@ static void do_rmdir(char *name) // Each -p cycle back up one slash, ignoring trailing and repeated /. if (!toys.optflags) return; - for (::) { + do { if (!(temp = strrchr(name, '/'))) return; *temp = 0; } while (!temp[1]); -- Gitee From 8bc739049eb299956bee7de7ecf569bbbdcd68c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Sat, 26 Jul 2025 06:45:09 +0000 Subject: [PATCH 03/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=B7=AE=E5=BC=82?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/other/watch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toys/other/watch.c b/toys/other/watch.c index 9d7c9fe..327d3d5 100644 --- a/toys/other/watch.c +++ b/toys/other/watch.c @@ -99,7 +99,7 @@ void watch_main(void) start_redraw(&width, &height); // redraw the header - if (!(toys.optflags&FLAG_t) { + if (!(toys.optflags&FLAG_t)) { time_t t = time(0); int pad, ctimelen; -- Gitee From 48fd72943f39908e7808939a1c607b3b7538833e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 03:25:30 +0000 Subject: [PATCH 04/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=B7=AE=E5=BC=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- tests/blkid.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/blkid.test b/tests/blkid.test index 5bfce43..5f0061e 100644 --- a/tests/blkid.test +++ b/tests/blkid.test @@ -15,7 +15,7 @@ function BLKID() } testing "cramfs" "BLKID cramfs" \ - 'temp.img: LABEL="mycramfs" TYPE="cramfs"\n' "" "" + 'temp.img: LABEL="mycramfs" TYPE="cramfs"\n' "" " testing "ext2" "BLKID ext2" \ 'temp.img: LABEL="myext2" UUID="e59093ba-4135-4fdb-bcc4-f20beae4dfaf" TYPE="ext2"\n' \ "" "" -- Gitee From 57980d56acf7bbd7dd52495de3748ee627d40f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 03:25:51 +0000 Subject: [PATCH 05/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=B7=AE=E5=BC=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- tests/blkid.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/blkid.test b/tests/blkid.test index 5f0061e..5bfce43 100644 --- a/tests/blkid.test +++ b/tests/blkid.test @@ -15,7 +15,7 @@ function BLKID() } testing "cramfs" "BLKID cramfs" \ - 'temp.img: LABEL="mycramfs" TYPE="cramfs"\n' "" " + 'temp.img: LABEL="mycramfs" TYPE="cramfs"\n' "" "" testing "ext2" "BLKID ext2" \ 'temp.img: LABEL="myext2" UUID="e59093ba-4135-4fdb-bcc4-f20beae4dfaf" TYPE="ext2"\n' \ "" "" -- Gitee From 9459283ef0175a0578f280348890099245fd95dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 06:06:33 +0000 Subject: [PATCH 06/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=B7=AE=E5=BC=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/other/base64.c | 50 ++++++++++----------------------------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/toys/other/base64.c b/toys/other/base64.c index 22652fd..05e3109 100644 --- a/toys/other/base64.c +++ b/toys/other/base64.c @@ -4,9 +4,7 @@ * * See https://tools.ietf.org/html/rfc4648 -// These optflags have to match. Todo: cleanup and collapse together? USE_BASE64(NEWTOY(base64, "diw#<0=76[!dw]", TOYFLAG_USR|TOYFLAG_BIN)) -USE_BASE32(NEWTOY(base32, "diw#<0=76[!dw]", TOYFLAG_USR|TOYFLAG_BIN)) config BASE64 bool "base64" @@ -16,32 +14,17 @@ config BASE64 Encode or decode in base64. - -d Decode - -i Ignore non-alphabetic characters - -w Wrap output at COLUMNS (default 76 or 0 for no wrap) - -config BASE32 - bool "base32" - default y - help - usage: base32 [-di] [-w COLUMNS] [FILE...] - - Encode or decode in base32. - -d Decode -i Ignore non-alphabetic characters -w Wrap output at COLUMNS (default 76 or 0 for no wrap) */ #define FOR_base64 -#define FORCE_FLAGS #include "toys.h" GLOBALS( long w; unsigned total; - unsigned n; // number of bits used in encoding. 5 for base32, 6 for base64 - unsigned align; // number of bits to align to ) static void wraputchar(int c, int *x) @@ -54,7 +37,7 @@ static void wraputchar(int c, int *x) }; } -static void do_base(int fd, char *name) +static void do_base64(int fd, char *name) { int out = 0, bits = 0, x = 0, i, len; char *buf = toybuf+128; @@ -64,9 +47,9 @@ static void do_base(int fd, char *name) for (;;) { // If no more data, flush buffer if (!(len = xread(fd, buf, sizeof(toybuf)-128))) { - if (!FLAG(d)) { - if (bits) wraputchar(toybuf[out<<(TT.n-bits)], &x); - while (TT.total&TT.align) wraputchar('=', &x); + if (!(toys.optflags & FLAG_d)) { + if (bits) wraputchar(toybuf[out<<(6-bits)], &x); + while (TT.total&3) wraputchar('=', &x); if (x) xputc('\n'); } @@ -74,12 +57,12 @@ static void do_base(int fd, char *name) } for (i=0; i= TT.n) { - wraputchar(toybuf[out >> (bits -= TT.n)], &x); + while (bits >= 6) { + wraputchar(toybuf[out >> (bits -= 6)], &x); out &= (1< Date: Tue, 29 Jul 2025 06:06:47 +0000 Subject: [PATCH 07/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/lsb/dmesg.c | 22 +++++++++++----------- toys/posix/du.c | 35 ++++++++++++++++------------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/toys/lsb/dmesg.c b/toys/lsb/dmesg.c index 8736a8e..a9e1022 100644 --- a/toys/lsb/dmesg.c +++ b/toys/lsb/dmesg.c @@ -69,15 +69,15 @@ static void format_message(char *msg, int new) subsystem = p ? (p-text) : 0; // To get "raw" output for /dev/kmsg we need to add priority to each line - if (FLAG(r)) { + if ((toys.optflags&FLAG_r)) { color(0); printf("<%d>", facpri); } // Format the time. - if (!FLAG(t)) { + if (!(toys.optflags&FLAG_t) { color(32); - if (FLAG(T)) { + if (toys.optflags&FLAG_T) { time_t t = TT.tea+time_s; char *ts = ctime(&t); @@ -115,23 +115,23 @@ void dmesg_main(void) if (TT.use_color) sigatexit(dmesg_cleanup); // If we're displaying output, is it klogctl or /dev/kmsg? - if (FLAG(C)||FLAG(n)) goto no_output; + if (toys.optflags & (FLAG_C|FLAG_n)) goto no_output; - if (FLAG(T)) { + if ((toys.optflags&FLAG_T)) { struct sysinfo info; sysinfo(&info); TT.tea = time(0)-info.uptime; } - if (!FLAG(S)) { + if (!(toys.optflags&FLAG_S)) { char msg[8193]; // CONSOLE_EXT_LOG_MAX+1 ssize_t len; int fd; // 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*!(toys.optflags&FLAG_w))); if (fd == -1) goto klogctl_mode; // SYSLOG_ACTION_CLEAR(5) doesn't actually remove anything from /dev/kmsg, @@ -141,7 +141,7 @@ void dmesg_main(void) for (;;) { // why does /dev/kmesg return EPIPE instead of EAGAIN if oldest message // expires as we read it? - if (-1==(len = read(fd, msg, sizeof(msg)-1)) && errno==EPIPE) continue; + if (-1==(len = read(fd, msg, sizeof(msg))) && errno==EPIPE) continue; // read() from kmsg always fails on a pre-3.5 kernel. if (len==-1 && errno==EINVAL) goto klogctl_mode; if (len<1) break; @@ -158,7 +158,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+toys.optflags&FLAG_c, data, size)] = 0; // Send each line to format_message. to = data + size; @@ -174,8 +174,8 @@ klogctl_mode: no_output: // Set the log level? - if (FLAG(n)) xklogctl(8, 0, TT.n); + if (toys.optflags & FLAG_n) xklogctl(8, 0, TT.n); // Clear the buffer? - if (FLAG(C)||FLAG(c)) xklogctl(5, 0, 0); + if (toys.optflags & (FLAG_C|FLAG_c)) xklogctl(5, 0, 0); } diff --git a/toys/posix/du.c b/toys/posix/du.c index 8611867..8ffc06b 100644 --- a/toys/posix/du.c +++ b/toys/posix/du.c @@ -4,22 +4,19 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/du.html * - * TODO: cleanup (should seen_inode be lib?) - * 32 bit du -b maxes out at 4 gigs (instead of 2 terabytes via *512 trick) - * because dirtree->extra is a long. + * TODO: cleanup -USE_DU(NEWTOY(du, "d#<0=-1hmlcaHkKLsxb[-HL][-kKmh]", TOYFLAG_USR|TOYFLAG_BIN)) +USE_DU(NEWTOY(du, "d#<0=-1hmlcaHkKLsx[-HL][-kKmh]", TOYFLAG_USR|TOYFLAG_BIN)) config DU bool "du" default y help - usage: du [-d N] [-askxHLlmc] [FILE...] + usage: du [-d N] [-askxHLlmc] [file...] Show disk usage, space consumed by files and directories. Size in: - -b Apparent bytes (directory listing size, not space used) -k 1024 byte blocks (default) -K 512 byte blocks (posix) -m Megabytes @@ -65,11 +62,10 @@ static void print(long long size, struct dirtree *node) } else { int bits = 10; - if (FLAG(K)) bits = 9; - else if (FLAG(m)) bits = 20; + if (toys.optflags & FLAG_K) bits = 9; + else if (toys.optflags & FLAG_m) bits = 20; - if (FLAG(b) && bits == 10 && !FLAG(k)) printf("%llu", size); - else printf("%llu", (size>>bits)+!!(size&((1<>bits)+!!(size&((1<st.st_dev)) + if ((toys.optflags & FLAG_x) && (TT.st_dev != node->st.st_dev)) return 0; // Don't loop endlessly on recursive directory symlink - if (FLAG(L)) { + if (toys.optflags & FLAG_L) { struct dirtree *try = node; while ((try = try->parent)) @@ -129,29 +125,30 @@ static int do_du(struct dirtree *node) } // Don't count hard links twice - if (!FLAG(l) && !node->again) + if (!(toys.optflags & FLAG_l) && !node->again) if (seen_inode(&TT.inodes, &node->st)) return 0; // Collect child info before printing directory size if (S_ISDIR(node->st.st_mode)) { if (!node->again) { TT.depth++; - return DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!FLAG(L)); + return DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L)); } else TT.depth--; } // Modern compilers' optimizers are insane and think signed overflow // behaves differently than unsigned overflow. Sigh. Big hammer. - blocks = FLAG(b) ? node->st.st_size : node->st.st_blocks; - blocks += (unsigned long)node->extra; + blocks = node->st.st_blocks + (unsigned long)node->extra; node->extra = blocks; if (node->parent) node->parent->extra = (unsigned long)node->parent->extra+blocks; else TT.total += node->extra; - if (FLAG(a) || !node->parent || (S_ISDIR(node->st.st_mode) && !FLAG(s))) { + if ((toys.optflags & FLAG_a) || !node->parent + || (S_ISDIR(node->st.st_mode) && !(toys.optflags & FLAG_s))) + { blocks = node->extra; - print(FLAG(b) ? blocks : blocks*512LL, node); + print(blocks*512LL, node); } return 0; @@ -165,7 +162,7 @@ void du_main(void) for (args = toys.optc ? toys.optargs : noargs; *args; args++) dirtree_flagread(*args, DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)), do_du); - if (FLAG(c)) print(FLAG(b) ? TT.total : TT.total*512, 0); + if (toys.optflags & FLAG_c) print(TT.total*512, 0); if (CFG_TOYBOX_FREE) seen_inode(TT.inodes, 0); } -- Gitee From e984e7c818fbf4491da79f1fe2b0928932329d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 06:07:01 +0000 Subject: [PATCH 08/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/lsb/gzip.c | 1 - toys/net/ftpget.c | 4 +-- toys/other/fallocate.c | 6 ++-- toys/other/free.c | 2 +- toys/other/i2ctools.c | 79 +++++++++++++++++++----------------------- toys/posix/find.c | 63 +++++++++++++-------------------- 6 files changed, 68 insertions(+), 87 deletions(-) diff --git a/toys/lsb/gzip.c b/toys/lsb/gzip.c index 049285a..3038495 100644 --- a/toys/lsb/gzip.c +++ b/toys/lsb/gzip.c @@ -78,7 +78,6 @@ static int do_deflate(int in_fd, int out_fd, int dd, int level) } if (!(gz = gzdopen(dd ? in_fd : out_fd, b))) perror_exit("gzdopen"); if (dd) { - if (gzdirect(gz)) error_exit("not gzip"); while ((len = gzread(gz, toybuf, sizeof(toybuf))) > 0) if (len != writeall(out_fd, toybuf, len)) break; } else { diff --git a/toys/net/ftpget.c b/toys/net/ftpget.c index 4e4c5ba..3b09f26 100644 --- a/toys/net/ftpget.c +++ b/toys/net/ftpget.c @@ -196,7 +196,7 @@ void ftpget_main(void) } if (!s || port<1 || port>65535) { error_msg("ftpget_main line %d, port %d toybox buf %s\r\n", __LINE__, port, toybuf); - ftp_line("QUIT", 0, 0); + ftp_line("QUIT", 0, -1); if (TT.fd >= 0) { xclose(TT.fd); } @@ -215,7 +215,7 @@ void ftpget_main(void) sscanf(toybuf, "%*u %llu", &lenr); } else if (get) { error_msg("ftpget_main line %d, port %d get %d toybox buf %s\r\n", __LINE__, port, get, toybuf); - ftp_line("QUIT", 0, 0); + ftp_line("QUIT", 0, -1); if (TT.fd >= 0) { xclose(TT.fd); } diff --git a/toys/other/fallocate.c b/toys/other/fallocate.c index dd009c9..a730897 100644 --- a/toys/other/fallocate.c +++ b/toys/other/fallocate.c @@ -20,12 +20,14 @@ config FALLOCATE #include "toys.h" GLOBALS( - long o, l; + long offset; + long size; ) void fallocate_main(void) { int fd = xcreate(*toys.optargs, O_RDWR | O_CREAT, 0644); - if ((errno = posix_fallocate(fd, TT.o, TT.l))) perror_exit("fallocate"); + if ((errno = posix_fallocate(fd, TT.offset, TT.size))) + perror_exit("fallocate"); if (CFG_TOYBOX_FREE) close(fd); } diff --git a/toys/other/free.c b/toys/other/free.c index d76e050..ce0df02 100644 --- a/toys/other/free.c +++ b/toys/other/free.c @@ -31,7 +31,7 @@ static char *convert(unsigned long d) long long ll = d*TT.units; char *s = TT.buf; - if (FLAG(h)) human_readable(s, ll, 0); + if (toys.optflags & FLAG_h) human_readable(s, ll, 0); else sprintf(s, "%llu",ll>>TT.bits); TT.buf += strlen(TT.buf)+1; diff --git a/toys/other/i2ctools.c b/toys/other/i2ctools.c index 1834159..9d42f58 100644 --- a/toys/other/i2ctools.c +++ b/toys/other/i2ctools.c @@ -118,16 +118,9 @@ static int i2c_read_byte(int fd, int addr, int *byte) return 0; } -static int i2c_quick_write(int fd, int addr) -{ - struct i2c_smbus_ioctl_data ioctl_data = { .read_write = I2C_SMBUS_QUICK, - .size = 0, .command = addr }; - - return ioctl(fd, I2C_SMBUS, &ioctl_data); -} - static void i2cdetect_dash_F(int bus) { + size_t i; struct { int mask; const char *name; } funcs[] = { {I2C_FUNC_I2C, "I2C"}, {I2C_FUNC_SMBUS_QUICK, "SMBus Quick Command"}, @@ -145,12 +138,13 @@ static void i2cdetect_dash_F(int bus) {I2C_FUNC_SMBUS_WRITE_I2C_BLOCK, "I2C Write Block"}, {I2C_FUNC_SMBUS_READ_I2C_BLOCK, "I2C Read Block"}, }; - unsigned long sup = i2c_get_funcs(bus); + unsigned long supported = i2c_get_funcs(bus); int i; printf("Functionalities implemented by %s:\n", toybuf); - for (i = 0; i < ARRAY_LEN(funcs); ++i) - printf("%-32s %s\n", funcs[i].name, (sup & funcs[i].mask) ? "yes" : "no"); + for (i = 0; i < ARRAY_LEN(funcs); ++i) { + printf("%-32s %s\n", funcs[i].name, + (supported & funcs[i].mask) ? "yes" : "no"); } static int i2cdetect_dash_l(struct dirtree *node) @@ -162,7 +156,7 @@ static int i2cdetect_dash_l(struct dirtree *node) if (!node->parent) return DIRTREE_RECURSE; // Skip the directory itself. - if (sscanf(node->name, "i2c-%d", &bus)!=1) return 0; + if (sscanf(node->name, "i2c-%d", &bus) != 1) return 0; funcs = i2c_get_funcs(bus); fname = dirtree_path(node, &suffix_len); @@ -173,7 +167,8 @@ static int i2cdetect_dash_l(struct dirtree *node) // "i2c-1 i2c Synopsys DesignWare I2C adapter I2C adapter" printf("%s\t%-10s\t%-32s\t%s\n", node->name, - (funcs & I2C_FUNC_I2C) ? "i2c" : "?", toybuf, + (funcs & I2C_FUNC_I2C) ? "i2c" : "?", + toybuf, (funcs & I2C_FUNC_I2C) ? "I2C Adapter" : "?"); return 0; @@ -181,25 +176,25 @@ static int i2cdetect_dash_l(struct dirtree *node) void i2cdetect_main(void) { - if (FLAG(l)) { + if (toys.optflags & FLAG_l) { if (toys.optc) error_exit("-l doesn't take arguments"); - dirtree_flagread("/sys/class/i2c-dev", DIRTREE_SHUTUP, i2cdetect_dash_l); - } else if (FLAG(F)) { + dirtree_read("/sys/class/i2c-dev", i2cdetect_dash_l); + } else if (toys.optflags & FLAG_F) { if (toys.optc != 1) error_exit("-F BUS"); i2cdetect_dash_F(atolx_range(*toys.optargs, 0, INT_MAX)); } else { int bus, first = 0x03, last = 0x77, fd, row, addr, byte; - if (FLAG(a)) { + if (toys.optflags & FLAG_a) { first = 0x00; last = 0x7f; } - if (toys.optc!=1 && toys.optc!=3) help_exit("Needs 1 or 3 arguments"); + if (toys.optc != 1 && toys.optc != 3) error_exit("bad args"); bus = atolx_range(*toys.optargs, 0, INT_MAX); - if (toys.optc==3) { + if (toys.optc == 3) { first = atolx_range(toys.optargs[1], 0, 0x7f); - last = atolx_range(toys.optargs[2], 0, 0x7f); + last = atolx_range(toys.optargs[1], 0, 0x7f); if (first > last) error_exit("first > last"); } @@ -209,8 +204,8 @@ void i2cdetect_main(void) printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); for (row = 0; row <= 0x70; row += 16) { xprintf("%02x:", row & 0xf0); - for (addr = row; addrlast) printf(" "); + for (addr = row; addr < row+16; ++addr) { + if (addr < first || addr > last) printf(" "); else { if (ioctl(fd, I2C_SLAVE, addr) == -1) { if (errno == EBUSY) { @@ -219,8 +214,7 @@ void i2cdetect_main(void) } perror_exit("ioctl(I2C_SLAVE)"); } - if ((FLAG(r) ? i2c_read_byte(fd, addr, &byte) - : i2c_quick_write(fd, addr)) == -1) xprintf(" --"); + if (i2c_quick_write(fd, addr, &byte) == -1) xprintf(" --"); else xprintf(" %02x", addr); } } @@ -242,16 +236,13 @@ void i2cdump_main(void) confirm("Dump chip 0x%02x on bus %d?", chip, bus); - fd = i2c_open(bus, FLAG(f) ? I2C_SLAVE_FORCE : I2C_SLAVE, chip); + fd = i2c_open(bus, (toys.optflags&FLAG_f) ? I2C_SLAVE_FORCE : I2C_SLAVE, chip); printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef\n"); - for (row = 0; row<=0xf0; row += 16) { + for (row = 0; row <= 0xf0; row += 16) { xprintf("%02x:", row & 0xf0); - for (addr = row; addr=5) { - if (toys.optc-4>I2C_SMBUS_BLOCK_MAX) error_exit("too much data"); + } else if (*mode == 'i' && toys.optc >= 5) { + if (toys.optc - 4 > I2C_SMBUS_BLOCK_MAX) error_exit("too much data"); ioctl_data.size = I2C_SMBUS_I2C_BLOCK_DATA; - for (i = 0; i FILE's Y=acm time (Y=t: FILE is literal time) - -quit exit immediately Numbers N may be prefixed by a - (less than) or + (greater than). Units for -Xtime are d (days, default), h (hours), m (minutes), or s (seconds). @@ -307,7 +305,7 @@ static int do_find(struct dirtree *new) if (new && check) test = !unlinkat(dirtree_parentfd(new), new->name, S_ISDIR(new->st.st_mode) ? AT_REMOVEDIR : 0); - } else if (!strcmp(s, "depth") || !strcmp(s, "d")) TT.depth = 1; + } else if (!strcmp(s, "depth")) TT.depth = 1; else if (!strcmp(s, "o") || !strcmp(s, "or")) { if (not) goto error; if (active) { @@ -347,33 +345,23 @@ static int do_find(struct dirtree *new) } else test = 0; } } else if (!strcmp(s, "nouser")) { - if (check && bufgetpwuid(new->st.st_uid)) test = 0; + if (check) if (bufgetpwuid(new->st.st_uid)) test = 0; } else if (!strcmp(s, "nogroup")) { - if (check && bufgetgrgid(new->st.st_gid)) test = 0; + if (check) if (bufgetgrgid(new->st.st_gid)) test = 0; } else if (!strcmp(s, "prune")) { if (check && S_ISDIR(new->st.st_mode) && !TT.depth) recurse = 0; - } else if (!strcmp(s, "executable")) { - if (check && faccessat(dirtree_parentfd(new), new->name,X_OK,0)) test = 0; - } else if (!strcmp(s, "quit")) { - if (check) { - execdir(0, 1); - xexit(); - } // Remaining filters take an argument } else { if (!strcmp(s, "name") || !strcmp(s, "iname") || !strcmp(s, "wholename") || !strcmp(s, "iwholename") - || !strcmp(s, "path") || !strcmp(s, "ipath") - || !strcmp(s, "lname") || !strcmp(s, "ilname")) + || !strcmp(s, "path") || !strcmp(s, "ipath")) { int i = (*s == 'i'), is_path = (s[i] != 'n'); char *arg = ss[1], *path = 0, *name = new ? new->name : arg; // Handle path expansion and case flattening - if (new && s[i] == 'l') - name = path = xreadlinkat(dirtree_parentfd(new), new->name); - else if (new && is_path) name = path = dirtree_path(new, 0); + if (new && is_path) name = path = dirtree_path(new, 0); if (i) { if ((check || !new) && name) name = strlower(name); if (!new) dlist_add(&TT.argdata, name); @@ -381,7 +369,7 @@ static int do_find(struct dirtree *new) } if (check) { - test = !fnmatch(arg, path ? name : basename(name), + test = !fnmatch(arg, is_path ? name : basename(name), FNM_PATHNAME*(!is_path)); if (i) free(name); } @@ -410,16 +398,9 @@ static int do_find(struct dirtree *new) } else if (!strcmp(s, "type")) { if (check) { int types[] = {S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFIFO, - S_IFREG, S_IFSOCK}, i; - char *t = ss[1]; - - for (; *t; t++) { - if (*t == ',') continue; - i = stridx("bcdlpfs", *t); - if (i<0) error_exit("bad -type '%c'", *t); - if ((new->st.st_mode & S_IFMT) == types[i]) break; - } - test = *t; + S_IFREG, S_IFSOCK}, i = stridx("bcdlpfs", *ss[1]); + if (i<0) error_exit("bad -type '%c'", *ss[1]); + if ((new->st.st_mode & S_IFMT) != types[i]) test = 0; } } else if (strchr("acm", *s) @@ -603,12 +584,16 @@ static int do_find(struct dirtree *new) if (check) for (fmt = ss[1]; *fmt; fmt++) { // Print the parts that aren't escapes if (*fmt == '\\') { - unsigned u; - - if (fmt[1] == 'c') break; - if ((u = unescape2(&fmt, 0))<128) putchar(u); - else printf("%.*s", (int)wcrtomb(buf, u, 0), buf); - fmt--; + int slash = *++fmt, n = unescape(slash); + + if (n) ch = n; + else if (slash=='c') break; + else if (slash=='0') { + ch = 0; + while (*fmt>='0' && *fmt<='7' && n++<3) ch=(ch*8)+*(fmt++)-'0'; + --fmt; + } else error_exit("bad \\%c", *fmt); + putchar(ch); } else if (*fmt != '%') putchar(*fmt); else if (*++fmt == '%') putchar('%'); else { @@ -634,7 +619,9 @@ static int do_find(struct dirtree *new) else if (ch == 'g') ll = (long)getgroupname(new->st.st_gid); else if (ch == 'u') ll = (long)getusername(new->st.st_uid); else if (ch == 'l') { - ll = (long)(ff = xreadlinkat(dirtree_parentfd(new), new->name)); + char *path = dirtree_path(new, 0); + ll = (long)(ff = xreadlinkat(path)); + free(path); if (!ll) ll = (long)""; } else if (ch == 'M') { mode_to_string(new->st.st_mode, buf); @@ -647,7 +634,7 @@ static int do_find(struct dirtree *new) } else if (ch == 'p') ll = (long)(ff = dirtree_path(new, 0)); else if (ch == 'T') { if (*++fmt!='@') error_exit("bad -printf %%T: %%T%c", *fmt); - sprintf(buf, "%lld.%ld", (long long)new->st.st_mtim.tv_sec, + sprintf(buf, "%ld.%ld", new->st.st_mtim.tv_sec, new->st.st_mtim.tv_nsec); ll = (long)buf; } else if (ch == 'Z') { @@ -699,7 +686,7 @@ void find_main(void) // Distinguish paths from filters for (len = 0; toys.optargs[len]; len++) - if (*toys.optargs[len] && strchr("-!(", *toys.optargs[len])) break; + if (strchr("-!(", *toys.optargs[len])) break; TT.filter = toys.optargs+len; // use "." if no paths -- Gitee From 2f3af40bc60ca773f46cb64633d904ef20e351aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 06:07:13 +0000 Subject: [PATCH 09/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/other/ionice.c | 31 ++++---- toys/other/losetup.c | 15 ++-- toys/other/lspci.c | 18 +++-- toys/other/makedevs.c | 13 ++-- toys/pending/modprobe.c | 166 +++++++++++++++++++++++++--------------- 5 files changed, 145 insertions(+), 98 deletions(-) diff --git a/toys/other/ionice.c b/toys/other/ionice.c index f356c5f..37d3545 100644 --- a/toys/other/ionice.c +++ b/toys/other/ionice.c @@ -40,24 +40,26 @@ config IORENICE #include GLOBALS( - long p, n, c; + long pid; + long level; + long class; ) static int ioprio_get(void) { - return syscall(__NR_ioprio_get, 1, (int)TT.p); + return syscall(__NR_ioprio_get, 1, (int)TT.pid); } static int ioprio_set(void) { - int prio = ((int)TT.c << 13) | (int)TT.n; + int prio = ((int)TT.class << 13) | (int)TT.level; - return syscall(__NR_ioprio_set, 1, (int)TT.p, prio); + return syscall(__NR_ioprio_set, 1, (int)TT.pid, prio); } void ionice_main(void) { - if (!TT.p && !toys.optc) error_exit("Need -p or COMMAND"); + if (!TT.pid && !toys.optc) error_exit("Need -p or COMMAND"); if (toys.optflags == FLAG_p) { int p = ioprio_get(); xprintf("%s: prio %d\n", @@ -65,7 +67,7 @@ void ionice_main(void) p&7); } else { if (-1 == ioprio_set() && !(toys.optflags&FLAG_t)) perror_exit("set"); - if (!TT.p) xexec(toys.optargs); + if (!TT.pid) xexec(toys.optargs); } } @@ -73,22 +75,23 @@ void iorenice_main(void) { char *classes[] = {"none", "rt", "be", "idle"}; - TT.p = atolx(*toys.optargs); + TT.pid = atolx(*toys.optargs); if (toys.optc == 1) { int p = ioprio_get(); if (p == -1) perror_exit("read priority"); - TT.c = (p>>13)&3; + TT.class = (p>>13)&3; p &= 7; - xprintf("Pid %ld, class %s (%ld), prio %d\n", TT.p, classes[TT.c], TT.c, p); + xprintf("Pid %ld, class %s (%ld), prio %d\n", + TT.pid, classes[TT.class], TT.class, p); return; } - for (TT.c = 0; TT.c<4; TT.c++) - if (!strcmp(toys.optargs[toys.optc-1], classes[TT.c])) break; - if (toys.optc == 3 || TT.c == 4) TT.n = atolx(toys.optargs[1]); - else TT.n = 4; - TT.c &= 3; + for (TT.class = 0; TT.class<4; TT.class++) + if (!strcmp(toys.optargs[toys.optc-1], classes[TT.class])) break; + if (toys.optc == 3 || TT.class == 4) TT.level = atolx(toys.optargs[1]); + else TT.level = 4; + TT.class &= 3; if (-1 == ioprio_set()) perror_exit("set"); } diff --git a/toys/other/losetup.c b/toys/other/losetup.c index ce84c1e..8e449cc 100644 --- a/toys/other/losetup.c +++ b/toys/other/losetup.c @@ -52,7 +52,7 @@ GLOBALS( static int loopback_setup(char *device, char *file) { struct loop_info64 *loop = (void *)(toybuf+32); - int lfd = -1, ffd = -1; + int lfd = -1, ffd = ffd; int racy = !device; // Open file (ffd) and loop device (lfd) @@ -103,20 +103,19 @@ static int loopback_setup(char *device, char *file) } // Associate file with this device? } else if (file) { - char *f_path = xabspath(file, ABS_PATH); + char *s = xabspath(file, 1); - if (!f_path) perror_exit("%s", file); // already opened but if deleted since + if (!s) perror_exit("file"); // already opened, but if deleted since... if (ioctl(lfd, LOOP_SET_FD, ffd)) { - free(f_path); if (racy && errno == EBUSY) return 1; perror_exit("%s=%s", device, file); } - xstrncpy((char *)loop->lo_file_name, f_path, LO_NAME_SIZE); - free(f_path); loop->lo_offset = TT.o; loop->lo_sizelimit = TT.S; + xstrncpy((char *)loop->lo_file_name, s, LO_NAME_SIZE); if (ioctl(lfd, LOOP_SET_STATUS64, loop)) perror_exit("%s=%s", device, file); if (FLAG(s)) puts(device); + free(s); } else { xprintf("%s: [%lld]:%llu (%s)", device, (long long)loop->lo_device, @@ -129,8 +128,8 @@ static int loopback_setup(char *device, char *file) } done: - xclose(ffd); - xclose(lfd); + if (file) close(ffd); + if (lfd != 1) close(lfd); return 0; } diff --git a/toys/other/lspci.c b/toys/other/lspci.c index 62df47c..b2d8870 100644 --- a/toys/other/lspci.c +++ b/toys/other/lspci.c @@ -13,7 +13,7 @@ config LSPCI -e Print all 6 digits in class -k Print kernel driver - -m Machine readable format + -m Machine parseable format config LSPCI_TEXT bool "lspci readable output" @@ -51,10 +51,11 @@ static int do_lspci(struct dirtree *new) return 0; *driver = 0; - if (FLAG(k)) readlinkat0(dirfd, "driver", driver, sizeof(driver)); + if (toys.optflags & FLAG_k) + readlinkat0(dirfd, "driver", driver, sizeof(driver)); for (fields = (char*[]){"class", "vendor", "device", 0}; *fields; fields++) { - int fd, size = 6 + 2*(FLAG(e) && p == toybuf); + int fd, size = 6 + 2*((toys.optflags & FLAG_e) && p == toybuf); *p = 0; if (-1 == (fd = openat(dirfd, *fields, O_RDONLY))) { @@ -94,7 +95,8 @@ static int do_lspci(struct dirtree *new) } if (TT.n > 1) { - printf(FLAG(m) ? "%s, \"%s\" \"%s [%s]\" \"%s [%s]\"" + printf((: "%s Class %s: %s:%s",) + ? "%s, \"%s\" \"%s [%s]\" \"%s [%s]\"" : "%s Class %s: %s [%s] %s [%s]", new->name+5, toybuf, vbig ? vbig : "", vendor, dbig ? dbig : "", device); @@ -103,11 +105,13 @@ static int do_lspci(struct dirtree *new) } } - printf(FLAG(m) ? "%s \"%s\" \"%s\" \"%s\"" : "%s Class %s: %s:%s", - new->name+5, toybuf, vbig ? vbig : vendor, dbig ? dbig : device); + printf((toys.optflags & FLAG_m) ? "%s \"%s\" \"%s\" \"%s\"" + : "%s Class %s: %s:%s", new->name+5, toybuf, + vbig ? vbig : vendor, dbig ? dbig : device); driver: - if (*driver) printf(FLAG(m) ? " \"%s\"" : " %s", basename(driver)); + if (*driver) + printf((toys.optflags & FLAG_m) ? " \"%s\"" : " %s", basename(driver)); xputc('\n'); return 0; diff --git a/toys/other/makedevs.c b/toys/other/makedevs.c index 4edd073..40b6bff 100644 --- a/toys/other/makedevs.c +++ b/toys/other/makedevs.c @@ -39,22 +39,20 @@ GLOBALS( char *d; ) -void makedevs_main(void) +void makedevs_main() { FILE *fp = stdin; char *line = NULL; - size_t allocated_length = 0; - int line_no = 0, i; // Open file and chdir, verbosely xprintf("rootdir = %s\n", *toys.optargs); - if (FLAG(d) && strcmp(TT.d, "-")) { - fp = xfopen(TT.d, "r"); + if (toys.optflags & FLAG_d && strcmp(TT.d, "-")) { + AT_FDCWD = xfopenro(TT.d, "r"); xprintf("table = %s\n", TT.d); } else xprintf("table = \n"); xchdir(*toys.optargs); - while (getline(&line, &allocated_length, fp) > 0) { + for (line_no = 0; (line = get_line(fd)); free(line)) { char type=0, user[64], group[64], *node, *ptr = line; unsigned int mode = 0755, major = 0, minor = 0, cnt = 0, incr = 0, st_val = 0; @@ -110,6 +108,5 @@ void makedevs_main(void) perror_msg("line %d: can't chown/chmod '%s'", line_no, ptr); } } - free(line); - if (fp != stdin) fclose(fp); + xclose(fd); } diff --git a/toys/pending/modprobe.c b/toys/pending/modprobe.c index fcd1cc3..8eae4d4 100644 --- a/toys/pending/modprobe.c +++ b/toys/pending/modprobe.c @@ -16,14 +16,14 @@ config MODPROBE modprobe utility - inserts modules and dependencies. -a Load multiple MODULEs - -b Apply blacklist to module names too - -D Show dependencies -d Load modules from DIR, option may be used multiple times -l List (MODULE is a pattern) - -q Quiet -r Remove MODULE (stacks) or do autoclean - -s Log to syslog + -q Quiet -v Verbose + -s Log to syslog + -D Show dependencies + -b Apply blacklist to module names too */ #define FOR_modprobe #include "toys.h" @@ -32,11 +32,17 @@ config MODPROBE GLOBALS( struct arg_list *dirs; - struct arg_list *probes, *dbase[256]; + struct arg_list *probes; + struct arg_list *dbase[256]; char *cmdopts; - int nudeps, symreq; + int nudeps' + uint8_t symreq; ) +/* Note: if "#define DBASE_SIZE" modified. + *Please update GLOBALS dbase[256] accordingly. + */ +#define DBASE_SIZE 256 #define MODNAME_LEN 256 // Modules flag definations @@ -142,7 +148,7 @@ static struct module_s *get_mod(char *mod, uint8_t add) path2mod(mod, name); for (i = 0; name[i]; i++) hash = ((hash*31) + hash) + name[i]; - hash %= ARRAY_LEN(TT.dbase); + hash %= DBASE_SIZE; for (temp = TT.dbase[hash]; temp; temp = temp->next) { modentry = (struct module_s *) temp->arg; if (!strcmp(modentry->name, name)) return modentry; @@ -155,7 +161,7 @@ static struct module_s *get_mod(char *mod, uint8_t add) } /* - * Read a line from file with \ continuation and skip commented lines. + * Read a line from file with \ continuation and escape commented line. * Return the line in allocated string (*li) */ static int read_line(FILE *fl, char **li) @@ -164,7 +170,7 @@ static int read_line(FILE *fl, char **li) ssize_t len, nxtlen; size_t linelen, nxtlinelen; - for (;;) { + while(1) { line = NULL; linelen = nxtlinelen = 0; len = getline(&line, &linelen, fl); @@ -220,7 +226,7 @@ static int config_action(struct dirtree *node) free(filename); return 0; } - for (line = linecp = NULL; read_line(fc, &line) >= 0; + for (line = linecp = NULL; read_line(fc, &line) > 0; free(line), free(linecp), line = linecp = NULL) { char *tk = NULL; @@ -234,18 +240,17 @@ static int config_action(struct dirtree *node) break; } } - // Every command requires at least one argument. - if (tcount < 2) continue; + if (!tk) continue; // process the tokens[0] contains first word of config line. if (!strcmp(tokens[0], "alias")) { struct arg_list *temp; - char alias[MODNAME_LEN], *realname; + char aliase[MODNAME_LEN], *realname; if (!tokens[2]) continue; - path2mod(tokens[1], alias); + path2mod(tokens[1], aliase); for (temp = TT.probes; temp; temp = temp->next) { modent = (struct module_s *) temp->arg; - if (fnmatch(alias, modent->name, 0)) continue; + if (fnmatch(aliase, modent->name, 0)) continue; realname = path2mod(tokens[2], NULL); llist_add(&modent->rnames, realname); if (modent->flags & MOD_NDDEPS) { @@ -268,7 +273,7 @@ static int config_action(struct dirtree *node) get_mod(tokens[1], 1)->flags |= MOD_BLACKLIST; else if (!strcmp(tokens[0], "install")) continue; else if (!strcmp(tokens[0], "remove")) continue; - else if (!FLAG(q)) + else if (toys.optflag&FLAG_q) error_msg("Invalid option %s found in file %s", tokens[0], filename); } fclose(fc); @@ -279,21 +284,21 @@ static int config_action(struct dirtree *node) // Show matched modules else return -1 on failure. static int depmode_read_entry(char *cmdname) { - char *line, *name; + char *line; int ret = -1; FILE *fe = xfopen("modules.dep", "r"); - while (read_line(fe, &line) >= 0) { + while (read_line(fe, &line) > 0) { char *tmp = strchr(line, ':'); if (tmp) { *tmp = '\0'; - name = basename(line); + char *name = basename(line); tmp = strchr(name, '.'); if (tmp) *tmp = '\0'; if (!cmdname || !fnmatch(cmdname, name, 0)) { if (tmp) *tmp = '.'; - if (FLAG(v)) puts(line); + if (toys.optflag&FLAG_v) puts(line); ret = 0; } } @@ -310,17 +315,18 @@ static void find_dep(void) struct module_s *mod; FILE *fe = xfopen("modules.dep", "r"); - for (; read_line(fe, &line) >= 0; free(line)) { + for (; read_line(fe, &line) > 0; free(line)) { char *tmp = strchr(line, ':'); if (tmp) { *tmp = '\0'; mod = get_mod(line, 0); if (!mod) continue; - if ((mod->flags & MOD_ALOADED) && !(FLAG(r)|FLAG(D))) continue; + if ((mod->flags & MOD_ALOADED) && + !(toys.optflag & (FLAG_r | FLAG_D))) continue; mod->flags |= MOD_FNDDEPMOD; - if ((mod->flags & MOD_NDDEPS) && !mod->dep) { + if ((mod->flags & MOD_NDDEPS) && (!mod->dep) { TT.nudeps--; llist_add(&mod->dep, xstrdup(line)); tmp++; @@ -341,20 +347,46 @@ static void find_dep(void) // Remove a module from the Linux Kernel. if !modules does auto remove. static int rm_mod(char *modules) { - char *s; + if (modules) { + int len = strlen(modukles); + + if (len > 3 && strcmp(modules+len-3, ".ko" )) modules[len-3] = 0; + } - if (modules && (s = strend(modules, ".ko"))) *s = 0; - return syscall(__NR_delete_module, modules, O_NONBLOCK); + errno = 0; + syscall(__NR_delete_module, flags ? flags : O_NONBLOCK|O_EXCL); + return errno; } -// Insert module; simpler than insmod(1) because we already flattened the array -// of flags, and don't need to support loading from stdin. +// Insert module same as insmod implementtation. static int ins_mod(char *modules, char *flags) { - int fd = xopenro(modules), rc = syscall(__NR_finit_module, fd, flags, 0); + char *buf = NULL; + int len, res; + int fd = xopenro(modules); + while (flags && strlen(toybuf) + strlen(flags) + 2 < sizeof(toybuf)) { + strcat(toybuf, flags); + strcat(toybuf, " "); + } + +#ifdef __NR_finit_module + res = syscall(__NR_finit_module, fd, toybuf, 0); + if(!res || ENOSYS) { + xclose(fd); + retur +#endif + + // TODD xreadfile() + + len =fdlength(fd); + buf = xmalloc(len); + xread(fd, buf, len); xclose(fd); - return rc; + + res = syscall(__NR_init_module, buf, len, toybuf); + if(CFG_TOYBOX_FREE && buf != toybuf) free(buf); + return res; } // Add module in probes list, if not loaded. @@ -362,11 +394,11 @@ static void add_mod(char *name) { struct module_s *mod = get_mod(name, 1); - if (!(FLAG(r)|FLAG(D)) && (mod->flags & MOD_ALOADED)) { - if (FLAG(v)) printf("%s already loaded\n", name); + if (!(toys.optflag & (FLAG_r |FLAG_D)) && (mod->flags & MOD_ALOADED)) { + if (toys.optflag&FLAG_v) printf("skipping %s, already loaded\n", name); return; } - if (FLAG(v)) printf("queuing %s\n", name); + if (toys.optflag&FLAG_v) printf("queuing %s\n", name); mod->cmdname = name; mod->flags |= MOD_NDDEPS; llist_add_tail(&TT.probes, mod); @@ -395,16 +427,17 @@ static char *add_cmdopt(char **argv) } // Probes a single module and loads all its dependencies. -static void go_probe(struct module_s *m) +static int go_probe(struct module_s *m) { int rc = 0, first = 1; if (!(m->flags & MOD_FNDDEPMOD)) { - if (!FLAG(q)) error_msg("module %s not found in modules.dep", m->name); - return; + if (!(toys.optflag&FLAG_q)) + error_msg("module %s not found in modules.dep", m->name); + return -ENOENT; } - if (FLAG(v)) printf("go_prob'ing %s\n", m->name); - if (!FLAG(r)) m->dep = llist_rev(m->dep); + if (toys.optflag&FLAG_v) printf("go_prob'ing %s\n", m->name); + if (!(toys.optflag&FLAG_r)) m->dep = llist_rev(m->dep); while (m->dep) { struct module_s *m2; @@ -414,9 +447,9 @@ static void go_probe(struct module_s *m) fn = llist_popme(&m->dep); m2 = get_mod(fn, 1); // are we removing ? - if (FLAG(r)) { + if (toys.optflag&FLAG_r) { if (m2->flags & MOD_ALOADED) { - if (rm_mod(m2->name)) { + if ((rc = rm_mod(m2->name, O_EXCL))) { if (first) { perror_msg("can't unload module %s", m2->name); break; @@ -426,19 +459,20 @@ static void go_probe(struct module_s *m) first = 0; continue; } -// TODO how does free work here without leaking? options = m2->opts; m2->opts = NULL; if (m == m2) options = add_opts(options, TT.cmdopts); // are we only checking dependencies ? - if (FLAG(D)) { - if (FLAG(v)) + if (toys.optflag&FLAG_D) { + if (toys.optflag&FLAG_v) printf(options ? "insmod %s %s\n" : "insmod %s\n", fn, options); if (options) free(options); continue; } if (m2->flags & MOD_ALOADED) { + if (toys.optflag&FLAG_v) + printf("%s is already loaded, skipping\n", fn); if (FLAG(v)) printf("%s already loaded\n", fn); if (options) free(options); continue; @@ -446,7 +480,7 @@ static void go_probe(struct module_s *m) // none of above is true insert the module. errno = 0; rc = ins_mod(fn, options); - if (FLAG(v)) + if (toys.optflag&FLAG_v) printf("loaded %s '%s': %s\n", fn, options, strerror(errno)); if (errno == EEXIST) rc = 0; free(options); @@ -456,35 +490,40 @@ static void go_probe(struct module_s *m) } m2->flags |= MOD_ALOADED; } + return rc; } void modprobe_main(void) { + struct utsname uts; char **argv = toys.optargs, *procline = NULL; FILE *fs; struct module_s *module; + unsigned flags = toys.optflags; struct arg_list *dirs; - if (toys.optc<1 && !FLAG(r) == !FLAG(l)) help_exit("bad syntax"); + if ((toys.optc < 1) && (((flags & FLAG_r) && (flags & FLAG_l)) + || (!((flags & FLAG_r)|| (flags & FLAG_l))))) + { + help_exit("bad syntax"); + } // Check for -r flag without arg if yes then do auto remove. - if (FLAG(r) && !toys.optc) { - if (rm_mod(0)) perror_exit("rmmod"); + if ((flags & FLAG_r) && !toys.optc) { + if (rm_mod(NULL, O_NONBLOCK | O_EXCL) perror_exit("rmmod"); return; } if (!TT.dirs) { - struct utsname uts; - uname(&uts); TT.dirs = xzalloc(sizeof(struct arg_list)); TT.dirs->arg = xmprintf("/lib/modules/%s", uts.release); } // modules.dep processing for dependency check. - if (FLAG(l)) { + if (flags & FLAG_l) { for (dirs = TT.dirs; dirs; dirs = dirs->next) { xchdir(dirs->arg); - if (!depmode_read_entry(*toys.optargs)) return; + if (!depmode_read_entry(toys.optargs[0])) return; } error_exit("no module found."); } @@ -493,19 +532,24 @@ void modprobe_main(void) fs = xfopen("/proc/modules", "r"); while (read_line(fs, &procline) > 0) { - *strchr(procline, ' ') = 0; + *(strchr(procline, ' ')) = '\0;'' get_mod(procline, 1)->flags = MOD_ALOADED; free(procline); procline = NULL; } fclose(fs); - if (FLAG(a) || FLAG(r)) for (; *argv; argv++) add_mod(*argv); - else { - add_mod(*argv); + if ((flags & FLAG_a) || (flags & FLAG_r)) { + do { + add_mod(*argv++); + } while (*argv); + } else { + add_mod(argv[0]); + } + TT.cmdopts = add_cmdopt(argv); } if (!TT.probes) { - if (FLAG(v)) puts("All modules loaded"); + if (toys.optflags&FLAG_v) puts("All modules loaded"); return; } dirtree_flagread("/etc/modprobe.conf", DIRTREE_SHUTUP, config_action); @@ -524,23 +568,23 @@ void modprobe_main(void) while ((module = llist_popme(&TT.probes))) { if (!module->rnames) { - if (FLAG(v)) puts("probing by module name"); + if (toys.optflags&FLAG_v) puts("probing by module name"); /* This is not an alias. Literal names are blacklisted * only if '-b' is given. */ - if (!FLAG(b) || !(module->flags & MOD_BLACKLIST)) + if (!(flags & FLAG_b) || !(module->flags & MOD_BLACKLIST)) go_probe(module); continue; } do { // Probe all real names for the alias. - char *real = ((struct arg_list *)llist_pop(&module->rnames))->arg; + char *real = ((struct arg_list*)llist_pop(&module->rnames))->arg; struct module_s *m2 = get_mod(real, 0); - if (FLAG(v)) + if (toys.optflags & FLAG_v) printf("probing alias %s by realname %s\n", module->name, real); if (!m2) continue; if (!(m2->flags & MOD_BLACKLIST) - && (!(m2->flags & MOD_ALOADED) || FLAG(r) || FLAG(D))) + && (!(m2->flags & MOD_ALOADED) || (flags & (flags & (FLAG_r | FLAG_D)))) go_probe(m2); free(real); } while (module->rnames); -- Gitee From ac4ec1e4b4ff44ae02240944883d9c678d160c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 06:07:29 +0000 Subject: [PATCH 10/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/lsb/mount.c | 6 +-- toys/lsb/pidof.c | 12 ++--- toys/other/nsenter.c | 4 +- toys/other/oneit.c | 120 +++++++++--------------------------------- toys/other/printenv.c | 2 +- toys/pending/tr.c | 26 +++++---- 6 files changed, 53 insertions(+), 117 deletions(-) diff --git a/toys/lsb/mount.c b/toys/lsb/mount.c index 22021ab..5bf300d 100644 --- a/toys/lsb/mount.c +++ b/toys/lsb/mount.c @@ -38,7 +38,7 @@ config MOUNT #config SMBMOUNT # bool "smbmount" -# default n +# deault n # helo # usage: smbmount SHARE DIR # @@ -185,7 +185,7 @@ static void mount_filesystem(char *dev, char *dir, char *type, if (strstart(&dev, "UUID=")) { char *s = tortoise(0, (char *[]){"blkid", "-U", dev, 0}); - if (!s) return error_msg("No uuid %s", dev); + if (!dev) return error_msg("No uuid %s", dev); dev = s; } @@ -215,7 +215,7 @@ static void mount_filesystem(char *dev, char *dir, char *type, if (fp && !buf) { size_t i; - if (getline(&buf, &i, fp)<1) { + if (getline(&buf, &i, fp)<0) { error_msg("%s: need -t", dev); break; } diff --git a/toys/lsb/pidof.c b/toys/lsb/pidof.c index 791eed0..5a09403 100644 --- a/toys/lsb/pidof.c +++ b/toys/lsb/pidof.c @@ -5,18 +5,18 @@ * * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html -USE_PIDOF(NEWTOY(pidof, "so:x", TOYFLAG_BIN)) +USE_PIDOF(NEWTOY(pidof, ". + * Copyright 2012 Andreas Heck + * Copyright 2012 Elie De Brauwer + * + * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html -USE_ONEIT(NEWTOY(oneit, "^<1nc:p3[!pn]", TOYFLAG_SBIN)) +USE_PIDOF(NEWTOY(pidof, " GLOBALS( - char *c; + char *omit; ) -// The minimum amount of work necessary to get ctrl-c and such to work is: -// -// - Fork a child (PID 1 is special: can't exit, has various signals blocked). -// - Do a setsid() (so we have our own session). -// - In the child, attach stdio to /dev/tty0 (/dev/console is special) -// - Exec the rest of the command line. -// -// PID 1 then reaps zombies until the child process it spawned exits, at which -// point it calls sync() and reboot(). I could stick a kill -1 in there. - -// Perform actions in response to signals. (Only root can send us signals.) -static void oneit_signaled(int signal) +static int print_pid(pid_t pid, char *name) { - int action = RB_AUTOBOOT; + sprintf(toybuf, "%d", (int)pid); + if (comma_scan(TT.omit, toybuf, 0)) return 0; + xprintf(" %s"+!!toys.exitval, toybuf); + toys.exitval = 0; - toys.signal = signal; - if (signal == SIGUSR1) action = RB_HALT_SYSTEM; - if (signal == SIGUSR2) action = RB_POWER_OFF; - - // PID 1 can't call reboot() because it kills the task that calls it, - // which causes the kernel to panic before the actual reboot happens. - sync(); - if (getpid()!=1) _exit(127+signal); - if (!vfork()) reboot(action); + return toys.optflags & FLAG_s; } -void oneit_main(void) +void pidof_main(void) { - int i, pid, pipes[] = {SIGUSR1, SIGUSR2, SIGTERM, SIGINT}; - - // Setup signal handlers for signals of interest - for (i = 0; i -USE_PRINTENV(NEWTOY(printenv, "(null)0", TOYFLAG_BIN)) +USE_PRINTENV(NEWTOY(printenv, "0(null)", TOYFLAG_BIN)) config PRINTENV bool "printenv" diff --git a/toys/pending/tr.c b/toys/pending/tr.c index e68ae46..29984bb 100644 --- a/toys/pending/tr.c +++ b/toys/pending/tr.c @@ -210,21 +210,27 @@ save: static void print_map(char *set1, char *set2) { - int n, src, dst, prev = -1; + int r = 0, i, prev_char = -1; - while ((n = read(0, toybuf, sizeof(toybuf)))) { - if (!FLAG(d) && !FLAG(s)) { + while (1) + { + i = 0; + r = read(STDIN_FILENO, (toybuf), sizeof(toybuf)); + if (!r) break; for (dst = 0; dst < n; dst++) toybuf[dst] = TT.map[toybuf[dst]]; } else { - for (src = dst = 0; src < n; src++) { - int ch = TT.map[toybuf[src]]; + for (;r > i;i++) { - if (FLAG(d) && (ch & 0x100)) continue; - if (FLAG(s) && ((ch & 0x200) && prev == ch)) continue; - toybuf[dst++] = prev = ch; - } + if((toys.optflags & FLAG_d) && (TT.map[(int)toybuf[i]] & 0x100)) continue; + if(toys.optflags & FLAG_s) { + if ((TT.map[(int)toysbuf[i]])) { + continue; + } + } + xputc(TT.map[(int)toybuf[i]] & 0xFF); + prev_char = TT.map[(int)toybuf[i]]; + fflush(stdout); } - xwrite(1, toybuf, dst); } } -- Gitee From 8044c79dd02c0d473663b2f4bfd4c38a9ae4e55f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 06:50:08 +0000 Subject: [PATCH 11/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/lsb/dmesg.c | 2 +- toys/lsb/pidof.c | 4 +-- toys/other/i2ctools.c | 1 - toys/other/nsenter.c | 2 +- toys/posix/find.c | 63 ++++++++++++++++++++++++++----------------- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/toys/lsb/dmesg.c b/toys/lsb/dmesg.c index a9e1022..5fdbf14 100644 --- a/toys/lsb/dmesg.c +++ b/toys/lsb/dmesg.c @@ -75,7 +75,7 @@ static void format_message(char *msg, int new) } // Format the time. - if (!(toys.optflags&FLAG_t) { + if (!(toys.optflags&FLAG_t)) { color(32); if (toys.optflags&FLAG_T) { time_t t = TT.tea+time_s; diff --git a/toys/lsb/pidof.c b/toys/lsb/pidof.c index 5a09403..700e16f 100644 --- a/toys/lsb/pidof.c +++ b/toys/lsb/pidof.c @@ -24,13 +24,13 @@ config PIDOF #include "toys.h" GLOBALS( - char *omit; + char *o; ) static int print_pid(pid_t pid, char *name) { sprintf(toybuf, "%d", (int)pid); - if (comma_scan(TT.omit, toybuf, 0)) return 0; + if (comma_scan(TT.o, toybuf, 0)) return 0; xprintf(" %s"+!!toys.exitval, toybuf); toys.exitval = 0; diff --git a/toys/other/i2ctools.c b/toys/other/i2ctools.c index 9d42f58..6eefaca 100644 --- a/toys/other/i2ctools.c +++ b/toys/other/i2ctools.c @@ -139,7 +139,6 @@ static void i2cdetect_dash_F(int bus) {I2C_FUNC_SMBUS_READ_I2C_BLOCK, "I2C Read Block"}, }; unsigned long supported = i2c_get_funcs(bus); - int i; printf("Functionalities implemented by %s:\n", toybuf); for (i = 0; i < ARRAY_LEN(funcs); ++i) { diff --git a/toys/other/nsenter.c b/toys/other/nsenter.c index 83fa932..dfcfbe8 100644 --- a/toys/other/nsenter.c +++ b/toys/other/nsenter.c @@ -82,7 +82,7 @@ GLOBALS( static void write_ugid_map(char *map, unsigned eugid) { - int bytes = sprintf(toybuf, "0 %u 1", eugid), fd = xopen(map, O_WRONLY; + int bytes = sprintf(toybuf, "0 %u 1", eugid), fd = xopen(map, O_WRONLY); xwrite(fd, toybuf, bytes); xclose(fd); diff --git a/toys/posix/find.c b/toys/posix/find.c index 2536add..f3d10f5 100644 --- a/toys/posix/find.c +++ b/toys/posix/find.c @@ -7,6 +7,7 @@ * Our "unspecified" behavior for no paths is to use "." * Parentheses can only stack 4096 deep * Not treating two {} as an error, but only using last + * TODO: -context USE_FIND(NEWTOY(find, "?^HL[-HL]", TOYFLAG_USR|TOYFLAG_BIN)) @@ -35,8 +36,9 @@ config FIND -inum N inode number N -empty empty files and dirs -type [bcdflps] type is (block, char, dir, file, symlink, pipe, socket) -true always true -false always false - -context PATTERN security context + -context PATTERN security context -executable access(X_OK) perm+ACL -newerXY FILE X=acm time > FILE's Y=acm time (Y=t: FILE is literal time) + -quit exit immediately Numbers N may be prefixed by a - (less than) or + (greater than). Units for -Xtime are d (days, default), h (hours), m (minutes), or s (seconds). @@ -305,7 +307,7 @@ static int do_find(struct dirtree *new) if (new && check) test = !unlinkat(dirtree_parentfd(new), new->name, S_ISDIR(new->st.st_mode) ? AT_REMOVEDIR : 0); - } else if (!strcmp(s, "depth")) TT.depth = 1; + } else if (!strcmp(s, "depth") || !strcmp(s, "d")) TT.depth = 1; else if (!strcmp(s, "o") || !strcmp(s, "or")) { if (not) goto error; if (active) { @@ -345,23 +347,33 @@ static int do_find(struct dirtree *new) } else test = 0; } } else if (!strcmp(s, "nouser")) { - if (check) if (bufgetpwuid(new->st.st_uid)) test = 0; + if (check && bufgetpwuid(new->st.st_uid)) test = 0; } else if (!strcmp(s, "nogroup")) { - if (check) if (bufgetgrgid(new->st.st_gid)) test = 0; + if (check && bufgetgrgid(new->st.st_gid)) test = 0; } else if (!strcmp(s, "prune")) { if (check && S_ISDIR(new->st.st_mode) && !TT.depth) recurse = 0; + } else if (!strcmp(s, "executable")) { + if (check && faccessat(dirtree_parentfd(new), new->name,X_OK,0)) test = 0; + } else if (!strcmp(s, "quit")) { + if (check) { + execdir(0, 1); + xexit(); + } // Remaining filters take an argument } else { if (!strcmp(s, "name") || !strcmp(s, "iname") || !strcmp(s, "wholename") || !strcmp(s, "iwholename") - || !strcmp(s, "path") || !strcmp(s, "ipath")) + || !strcmp(s, "path") || !strcmp(s, "ipath") + || !strcmp(s, "lname") || !strcmp(s, "ilname")) { int i = (*s == 'i'), is_path = (s[i] != 'n'); char *arg = ss[1], *path = 0, *name = new ? new->name : arg; // Handle path expansion and case flattening - if (new && is_path) name = path = dirtree_path(new, 0); + if (new && s[i] == 'l') + name = path = xreadlinkat(dirtree_parentfd(new), new->name); + else if (new && is_path) name = path = dirtree_path(new, 0); if (i) { if ((check || !new) && name) name = strlower(name); if (!new) dlist_add(&TT.argdata, name); @@ -369,7 +381,7 @@ static int do_find(struct dirtree *new) } if (check) { - test = !fnmatch(arg, is_path ? name : basename(name), + test = !fnmatch(arg, path ? name : basename(name), FNM_PATHNAME*(!is_path)); if (i) free(name); } @@ -398,9 +410,16 @@ static int do_find(struct dirtree *new) } else if (!strcmp(s, "type")) { if (check) { int types[] = {S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFIFO, - S_IFREG, S_IFSOCK}, i = stridx("bcdlpfs", *ss[1]); - if (i<0) error_exit("bad -type '%c'", *ss[1]); - if ((new->st.st_mode & S_IFMT) != types[i]) test = 0; + S_IFREG, S_IFSOCK}, i; + char *t = ss[1]; + + for (; *t; t++) { + if (*t == ',') continue; + i = stridx("bcdlpfs", *t); + if (i<0) error_exit("bad -type '%c'", *t); + if ((new->st.st_mode & S_IFMT) == types[i]) break; + } + test = *t; } } else if (strchr("acm", *s) @@ -584,16 +603,12 @@ static int do_find(struct dirtree *new) if (check) for (fmt = ss[1]; *fmt; fmt++) { // Print the parts that aren't escapes if (*fmt == '\\') { - int slash = *++fmt, n = unescape(slash); - - if (n) ch = n; - else if (slash=='c') break; - else if (slash=='0') { - ch = 0; - while (*fmt>='0' && *fmt<='7' && n++<3) ch=(ch*8)+*(fmt++)-'0'; - --fmt; - } else error_exit("bad \\%c", *fmt); - putchar(ch); + unsigned u; + + if (fmt[1] == 'c') break; + if ((u = unescape2(&fmt, 0))<128) putchar(u); + else printf("%.*s", (int)wcrtomb(buf, u, 0), buf); + fmt--; } else if (*fmt != '%') putchar(*fmt); else if (*++fmt == '%') putchar('%'); else { @@ -619,9 +634,7 @@ static int do_find(struct dirtree *new) else if (ch == 'g') ll = (long)getgroupname(new->st.st_gid); else if (ch == 'u') ll = (long)getusername(new->st.st_uid); else if (ch == 'l') { - char *path = dirtree_path(new, 0); - ll = (long)(ff = xreadlinkat(path)); - free(path); + ll = (long)(ff = xreadlinkat(dirtree_parentfd(new), new->name)); if (!ll) ll = (long)""; } else if (ch == 'M') { mode_to_string(new->st.st_mode, buf); @@ -634,7 +647,7 @@ static int do_find(struct dirtree *new) } else if (ch == 'p') ll = (long)(ff = dirtree_path(new, 0)); else if (ch == 'T') { if (*++fmt!='@') error_exit("bad -printf %%T: %%T%c", *fmt); - sprintf(buf, "%ld.%ld", new->st.st_mtim.tv_sec, + sprintf(buf, "%lld.%ld", (long long)new->st.st_mtim.tv_sec, new->st.st_mtim.tv_nsec); ll = (long)buf; } else if (ch == 'Z') { @@ -686,7 +699,7 @@ void find_main(void) // Distinguish paths from filters for (len = 0; toys.optargs[len]; len++) - if (strchr("-!(", *toys.optargs[len])) break; + if (*toys.optargs[len] && strchr("-!(", *toys.optargs[len])) break; TT.filter = toys.optargs+len; // use "." if no paths -- Gitee From 7411089d47e9f88038adf6ef336daa4c1c889317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 07:13:16 +0000 Subject: [PATCH 12/17] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/other/makedevs.c | 4 ++-- toys/posix/find.c | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/toys/other/makedevs.c b/toys/other/makedevs.c index 40b6bff..ecb632e 100644 --- a/toys/other/makedevs.c +++ b/toys/other/makedevs.c @@ -41,13 +41,13 @@ GLOBALS( void makedevs_main() { - FILE *fp = stdin; + int fd = 0, line_no, i; char *line = NULL; // Open file and chdir, verbosely xprintf("rootdir = %s\n", *toys.optargs); if (toys.optflags & FLAG_d && strcmp(TT.d, "-")) { - AT_FDCWD = xfopenro(TT.d, "r"); + AT_FDCWD = xfopenro(TT.d); xprintf("table = %s\n", TT.d); } else xprintf("table = \n"); xchdir(*toys.optargs); diff --git a/toys/posix/find.c b/toys/posix/find.c index f3d10f5..bfbf1a9 100644 --- a/toys/posix/find.c +++ b/toys/posix/find.c @@ -634,7 +634,10 @@ static int do_find(struct dirtree *new) else if (ch == 'g') ll = (long)getgroupname(new->st.st_gid); else if (ch == 'u') ll = (long)getusername(new->st.st_uid); else if (ch == 'l') { - ll = (long)(ff = xreadlinkat(dirtree_parentfd(new), new->name)); + char *path = dirtree_path(new, 0); + + ll = (long)(ff = xreadlink(path); + free(path); if (!ll) ll = (long)""; } else if (ch == 'M') { mode_to_string(new->st.st_mode, buf); -- Gitee From 421a8e885f018019473b23e5e853c9546f1aba8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 07:20:28 +0000 Subject: [PATCH 13/17] daimaxiugai1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/other/makedevs.c | 2 +- toys/posix/find.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/toys/other/makedevs.c b/toys/other/makedevs.c index ecb632e..f26c029 100644 --- a/toys/other/makedevs.c +++ b/toys/other/makedevs.c @@ -47,7 +47,7 @@ void makedevs_main() // Open file and chdir, verbosely xprintf("rootdir = %s\n", *toys.optargs); if (toys.optflags & FLAG_d && strcmp(TT.d, "-")) { - AT_FDCWD = xfopenro(TT.d); + fd = xfopenro(TT.d); xprintf("table = %s\n", TT.d); } else xprintf("table = \n"); xchdir(*toys.optargs); diff --git a/toys/posix/find.c b/toys/posix/find.c index bfbf1a9..e5487d4 100644 --- a/toys/posix/find.c +++ b/toys/posix/find.c @@ -636,7 +636,7 @@ static int do_find(struct dirtree *new) else if (ch == 'l') { char *path = dirtree_path(new, 0); - ll = (long)(ff = xreadlink(path); + ll = (long)(ff = xreadlink(path)); free(path); if (!ll) ll = (long)""; } else if (ch == 'M') { -- Gitee From c824bab5c49c21571d9c59f88b4cb06923ccd638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 07:30:27 +0000 Subject: [PATCH 14/17] daima1xiugai MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/other/lspci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toys/other/lspci.c b/toys/other/lspci.c index b2d8870..40ad3f2 100644 --- a/toys/other/lspci.c +++ b/toys/other/lspci.c @@ -95,7 +95,7 @@ static int do_lspci(struct dirtree *new) } if (TT.n > 1) { - printf((: "%s Class %s: %s:%s",) + printf((toys.optflags & FLAG_m) ? "%s, \"%s\" \"%s [%s]\" \"%s [%s]\"" : "%s Class %s: %s [%s] %s [%s]", new->name+5, toybuf, vbig ? vbig : "", vendor, -- Gitee From f1177fcc9f97f45f456db96d5c57449e67394d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Tue, 29 Jul 2025 08:36:55 +0000 Subject: [PATCH 15/17] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/lsb/pidof.c | 8 +-- toys/other/i2ctools.c | 80 +++++++++++++++------------- toys/other/ionice.c | 31 +++++------ toys/other/makedevs.c | 15 +++--- toys/other/oneit.c | 120 +++++++++++++++++++++++++++++++++--------- 5 files changed, 166 insertions(+), 88 deletions(-) diff --git a/toys/lsb/pidof.c b/toys/lsb/pidof.c index 700e16f..791eed0 100644 --- a/toys/lsb/pidof.c +++ b/toys/lsb/pidof.c @@ -5,18 +5,18 @@ * * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html -USE_PIDOF(NEWTOY(pidof, "parent) return DIRTREE_RECURSE; // Skip the directory itself. - if (sscanf(node->name, "i2c-%d", &bus) != 1) return 0; + if (sscanf(node->name, "i2c-%d", &bus)!=1) return 0; funcs = i2c_get_funcs(bus); fname = dirtree_path(node, &suffix_len); @@ -166,8 +173,7 @@ static int i2cdetect_dash_l(struct dirtree *node) // "i2c-1 i2c Synopsys DesignWare I2C adapter I2C adapter" printf("%s\t%-10s\t%-32s\t%s\n", node->name, - (funcs & I2C_FUNC_I2C) ? "i2c" : "?", - toybuf, + (funcs & I2C_FUNC_I2C) ? "i2c" : "?", toybuf, (funcs & I2C_FUNC_I2C) ? "I2C Adapter" : "?"); return 0; @@ -175,25 +181,25 @@ static int i2cdetect_dash_l(struct dirtree *node) void i2cdetect_main(void) { - if (toys.optflags & FLAG_l) { + if (FLAG(l)) { if (toys.optc) error_exit("-l doesn't take arguments"); - dirtree_read("/sys/class/i2c-dev", i2cdetect_dash_l); - } else if (toys.optflags & FLAG_F) { + dirtree_flagread("/sys/class/i2c-dev", DIRTREE_SHUTUP, i2cdetect_dash_l); + } else if (FLAG(F)) { if (toys.optc != 1) error_exit("-F BUS"); i2cdetect_dash_F(atolx_range(*toys.optargs, 0, INT_MAX)); } else { int bus, first = 0x03, last = 0x77, fd, row, addr, byte; - if (toys.optflags & FLAG_a) { + if (FLAG(a)) { first = 0x00; last = 0x7f; } - if (toys.optc != 1 && toys.optc != 3) error_exit("bad args"); + if (toys.optc!=1 && toys.optc!=3) help_exit("Needs 1 or 3 arguments"); bus = atolx_range(*toys.optargs, 0, INT_MAX); - if (toys.optc == 3) { + if (toys.optc==3) { first = atolx_range(toys.optargs[1], 0, 0x7f); - last = atolx_range(toys.optargs[1], 0, 0x7f); + last = atolx_range(toys.optargs[2], 0, 0x7f); if (first > last) error_exit("first > last"); } @@ -203,8 +209,8 @@ void i2cdetect_main(void) printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); for (row = 0; row <= 0x70; row += 16) { xprintf("%02x:", row & 0xf0); - for (addr = row; addr < row+16; ++addr) { - if (addr < first || addr > last) printf(" "); + for (addr = row; addrlast) printf(" "); else { if (ioctl(fd, I2C_SLAVE, addr) == -1) { if (errno == EBUSY) { @@ -213,7 +219,8 @@ void i2cdetect_main(void) } perror_exit("ioctl(I2C_SLAVE)"); } - if (i2c_quick_write(fd, addr, &byte) == -1) xprintf(" --"); + if ((FLAG(r) ? i2c_read_byte(fd, addr, &byte) + : i2c_quick_write(fd, addr)) == -1) xprintf(" --"); else xprintf(" %02x", addr); } } @@ -235,13 +242,16 @@ void i2cdump_main(void) confirm("Dump chip 0x%02x on bus %d?", chip, bus); - fd = i2c_open(bus, (toys.optflags&FLAG_f) ? I2C_SLAVE_FORCE : I2C_SLAVE, chip); + fd = i2c_open(bus, FLAG(f) ? I2C_SLAVE_FORCE : I2C_SLAVE, chip); printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef\n"); - for (row = 0; row <= 0xf0; row += 16) { + for (row = 0; row<=0xf0; row += 16) { xprintf("%02x:", row & 0xf0); - for (addr = row; addr < row + 16; ++addr) { - if (i2c_read_byte(fd, addr, &byte) == -1) perror_exit(" %02x", byte); - printf(" %02x", byte); + for (addr = row; addr= 5) { - if (toys.optc - 4 > I2C_SMBUS_BLOCK_MAX) error_exit("too much data"); + } else if (*mode=='i' && toys.optc>=5) { + if (toys.optc-4>I2C_SMBUS_BLOCK_MAX) error_exit("too much data"); ioctl_data.size = I2C_SMBUS_I2C_BLOCK_DATA; - for (i = 0; i < toys.optc - 4; ++i) + for (i = 0; i GLOBALS( - long pid; - long level; - long class; + long p, n, c; ) static int ioprio_get(void) { - return syscall(__NR_ioprio_get, 1, (int)TT.pid); + return syscall(__NR_ioprio_get, 1, (int)TT.p); } static int ioprio_set(void) { - int prio = ((int)TT.class << 13) | (int)TT.level; + int prio = ((int)TT.c << 13) | (int)TT.n; - return syscall(__NR_ioprio_set, 1, (int)TT.pid, prio); + return syscall(__NR_ioprio_set, 1, (int)TT.p, prio); } void ionice_main(void) { - if (!TT.pid && !toys.optc) error_exit("Need -p or COMMAND"); + if (!TT.p && !toys.optc) error_exit("Need -p or COMMAND"); if (toys.optflags == FLAG_p) { int p = ioprio_get(); xprintf("%s: prio %d\n", @@ -67,7 +65,7 @@ void ionice_main(void) p&7); } else { if (-1 == ioprio_set() && !(toys.optflags&FLAG_t)) perror_exit("set"); - if (!TT.pid) xexec(toys.optargs); + if (!TT.p) xexec(toys.optargs); } } @@ -75,23 +73,22 @@ void iorenice_main(void) { char *classes[] = {"none", "rt", "be", "idle"}; - TT.pid = atolx(*toys.optargs); + TT.p = atolx(*toys.optargs); if (toys.optc == 1) { int p = ioprio_get(); if (p == -1) perror_exit("read priority"); - TT.class = (p>>13)&3; + TT.c = (p>>13)&3; p &= 7; - xprintf("Pid %ld, class %s (%ld), prio %d\n", - TT.pid, classes[TT.class], TT.class, p); + xprintf("Pid %ld, class %s (%ld), prio %d\n", TT.p, classes[TT.c], TT.c, p); return; } - for (TT.class = 0; TT.class<4; TT.class++) - if (!strcmp(toys.optargs[toys.optc-1], classes[TT.class])) break; - if (toys.optc == 3 || TT.class == 4) TT.level = atolx(toys.optargs[1]); - else TT.level = 4; - TT.class &= 3; + for (TT.c = 0; TT.c<4; TT.c++) + if (!strcmp(toys.optargs[toys.optc-1], classes[TT.c])) break; + if (toys.optc == 3 || TT.c == 4) TT.n = atolx(toys.optargs[1]); + else TT.n = 4; + TT.c &= 3; if (-1 == ioprio_set()) perror_exit("set"); } diff --git a/toys/other/makedevs.c b/toys/other/makedevs.c index f26c029..4edd073 100644 --- a/toys/other/makedevs.c +++ b/toys/other/makedevs.c @@ -39,20 +39,22 @@ GLOBALS( char *d; ) -void makedevs_main() +void makedevs_main(void) { - int fd = 0, line_no, i; + FILE *fp = stdin; char *line = NULL; + size_t allocated_length = 0; + int line_no = 0, i; // Open file and chdir, verbosely xprintf("rootdir = %s\n", *toys.optargs); - if (toys.optflags & FLAG_d && strcmp(TT.d, "-")) { - fd = xfopenro(TT.d); + if (FLAG(d) && strcmp(TT.d, "-")) { + fp = xfopen(TT.d, "r"); xprintf("table = %s\n", TT.d); } else xprintf("table = \n"); xchdir(*toys.optargs); - for (line_no = 0; (line = get_line(fd)); free(line)) { + while (getline(&line, &allocated_length, fp) > 0) { char type=0, user[64], group[64], *node, *ptr = line; unsigned int mode = 0755, major = 0, minor = 0, cnt = 0, incr = 0, st_val = 0; @@ -108,5 +110,6 @@ void makedevs_main() perror_msg("line %d: can't chown/chmod '%s'", line_no, ptr); } } - xclose(fd); + free(line); + if (fp != stdin) fclose(fp); } diff --git a/toys/other/oneit.c b/toys/other/oneit.c index 5a09403..fca8bba 100644 --- a/toys/other/oneit.c +++ b/toys/other/oneit.c @@ -1,45 +1,115 @@ -/* pidof.c - Print the Process IDs of all processes with the given names. +/* oneit.c - tiny init replacement to launch a single child process. * - * Copyright 2012 Andreas Heck - * Copyright 2012 Elie De Brauwer - * - * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html + * Copyright 2005, 2007 by Rob Landley . -USE_PIDOF(NEWTOY(pidof, " GLOBALS( - char *omit; + char *c; ) -static int print_pid(pid_t pid, char *name) +// The minimum amount of work necessary to get ctrl-c and such to work is: +// +// - Fork a child (PID 1 is special: can't exit, has various signals blocked). +// - Do a setsid() (so we have our own session). +// - In the child, attach stdio to /dev/tty0 (/dev/console is special) +// - Exec the rest of the command line. +// +// PID 1 then reaps zombies until the child process it spawned exits, at which +// point it calls sync() and reboot(). I could stick a kill -1 in there. + +// Perform actions in response to signals. (Only root can send us signals.) +static void oneit_signaled(int signal) { - sprintf(toybuf, "%d", (int)pid); - if (comma_scan(TT.omit, toybuf, 0)) return 0; - xprintf(" %s"+!!toys.exitval, toybuf); - toys.exitval = 0; + int action = RB_AUTOBOOT; - return toys.optflags & FLAG_s; + toys.signal = signal; + if (signal == SIGUSR1) action = RB_HALT_SYSTEM; + if (signal == SIGUSR2) action = RB_POWER_OFF; + + // PID 1 can't call reboot() because it kills the task that calls it, + // which causes the kernel to panic before the actual reboot happens. + sync(); + if (getpid()!=1) _exit(127+signal); + if (!vfork()) reboot(action); } -void pidof_main(void) +void oneit_main(void) { - toys.exitval = 1; - names_to_pid(toys.optargs, print_pid, FLAG(x)); - if (!toys.exitval) xputc('\n'); + int i, pid, pipes[] = {SIGUSR1, SIGUSR2, SIGTERM, SIGINT}; + + // Setup signal handlers for signals of interest + for (i = 0; i Date: Wed, 30 Jul 2025 01:03:04 +0000 Subject: [PATCH 16/17] daimachayi MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/other/fallocate.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/toys/other/fallocate.c b/toys/other/fallocate.c index a730897..dd009c9 100644 --- a/toys/other/fallocate.c +++ b/toys/other/fallocate.c @@ -20,14 +20,12 @@ config FALLOCATE #include "toys.h" GLOBALS( - long offset; - long size; + long o, l; ) void fallocate_main(void) { int fd = xcreate(*toys.optargs, O_RDWR | O_CREAT, 0644); - if ((errno = posix_fallocate(fd, TT.offset, TT.size))) - perror_exit("fallocate"); + if ((errno = posix_fallocate(fd, TT.o, TT.l))) perror_exit("fallocate"); if (CFG_TOYBOX_FREE) close(fd); } -- Gitee From df32ec5f03f122aea00a5930805e45cc44d3097f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=95=8F=E6=9D=B0?= Date: Thu, 31 Jul 2025 07:59:37 +0000 Subject: [PATCH 17/17] daima MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴敏杰 --- toys/pending/traceroute.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/toys/pending/traceroute.c b/toys/pending/traceroute.c index 4484580..6d05bb6 100644 --- a/toys/pending/traceroute.c +++ b/toys/pending/traceroute.c @@ -218,7 +218,7 @@ static void do_trace() fflush(NULL); if (!TT.istraceroute6) - if (probe && (toys.optflags & FLAG_z)) msleep(TT.pause_time); + if (probe && (toys.optflags & FLAG_z)) usleep(TT.pause_time * 1000); if (!TT.istraceroute6) send_probe4(++seq, ttl); else send_probe6(++seq, ttl); @@ -477,7 +477,7 @@ static void do_trace() void traceroute_main(void) { - unsigned opt_len = 0, tyser = 0; + unsigned opt_len = 0, pack_size = 0,tyser = 0; int lsrr = 0, set = 1; if(!(toys.optflags & FLAG_4) && -- Gitee