From d684299c777d8c191d400bbef942c7ec4b34456e Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 26 Jun 2026 23:27:28 -0500 Subject: [PATCH] Cleanup --- lib/commas.c | 2 +- main.c | 27 ++++++++------------------- toys/other/stat.c | 2 +- toys/posix/file.c | 2 +- 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/lib/commas.c b/lib/commas.c index 22676847..33d194ea 100644 --- a/lib/commas.c +++ b/lib/commas.c @@ -37,7 +37,7 @@ void comma_collate(char **old, char *new) if (atold[strlen(atold)-1] == ',') comma = ""; temp = xmprintf("%s%s%s", atold, comma, new); } else temp = xstrdup(new); - free (atold); + free(atold); *old = temp; } diff --git a/main.c b/main.c index aa6a52cb..1bd4b0d2 100644 --- a/main.c +++ b/main.c @@ -12,7 +12,6 @@ #define NEWTOY(name, opts, flags) {#name, name##_main, OPTSTR_##name, flags}, #define OLDTOY(name, oldname, flags) \ {#name, oldname##_main, OPTSTR_##oldname, flags}, - struct toy_list toy_list[] = { #include "generated/newtoys.h" }; @@ -97,7 +96,7 @@ static char *help_data = (void *)help_array; void show_help(int flags) { int i = toys.which-toy_list; - char *s, *ss; + char *s; if (!CFG_TOYBOX_HELP) return; @@ -111,8 +110,7 @@ void show_help(int flags) : " (see https://landley.net/toybox)"); for (;;) { - s = (void *)help_data; - while (i--) s += strlen(s) + 1; + for (s = (void *)help_data; i--; s += strlen(s)+1); // If it's an alias, restart search for real name if (*s != 255) break; i = toy_find(++s)-toy_list; @@ -127,8 +125,7 @@ void show_help(int flags) // Only "help -u" calls HELP_USAGE if (CFG_HELP && (flags&HELP_USAGE)) { strstart(&s, "usage: "); - for (ss = s; *ss && *ss!='\n'; ss++); - printf("%.*s\n", (int)(ss-s), s); + printf("%.*s\n", (int)strcspn(s, "\n"), s); } else if (!NEED_TRIMHELP || !(toys.which->flags&TOYFLAG_TRIMHELP)) puts(s); // TRIMHELP lines starting with ! are only displayed with BIGHELP, // and the starting ! is edited out either way. @@ -303,7 +300,7 @@ void toy_exec(char *argv[]) void toybox_main(void) { char *toy_paths[] = {"usr/", "bin/", "sbin/", 0}, *s = toys.argv[1]; - int i, len = 0; + int i, j, len = 0; unsigned width = 80; // fast path: try to exec immediately. @@ -328,11 +325,9 @@ void toybox_main(void) for (i = 1; i width-15) len = 0; xputc(len ? ' ' : '\n'); @@ -343,19 +338,13 @@ void toybox_main(void) int main(int argc, char *argv[]) { - // don't segfault if our environment is crazy - // TODO mooted by kernel commit dcd46d897adb7 5.17 kernel Jan 2022 - if (!*argv) return 127; - - // Snapshot stack location so we can detect recursion depth later. // Nommu has special reentry path, !stacktop = "vfork/exec self happened" if (!CFG_TOYBOX_FORK && (0x80 & **argv)) **argv &= 0x7f; - else { - int stack_start; // here so probe var won't permanently eat stack - toys.stacktop = &stack_start; - } + // Snapshot stack location so we can detect recursion depth later. + else toys.stacktop = &argc; + // This is a constant so dead code elimination will remove the unused branch if (CFG_TOYBOX) { // Call the multiplexer with argv[] as its arguments so it can toy_find() toys.argv = argv-1; diff --git a/toys/other/stat.c b/toys/other/stat.c index 463aa46a..eaf46bf4 100644 --- a/toys/other/stat.c +++ b/toys/other/stat.c @@ -5,7 +5,7 @@ USE_STAT(NEWTOY(stat, "<1c:(format)fLt", TOYFLAG_BIN)) config STAT - bool stat + bool "stat" default y help usage: stat [-tfL] [-c FORMAT] FILE... diff --git a/toys/posix/file.c b/toys/posix/file.c index 0f3af847..4b5242c5 100644 --- a/toys/posix/file.c +++ b/toys/posix/file.c @@ -320,7 +320,7 @@ static void do_regular_file(int fd, char *name) xputc('\n'); } else if (len>4 && strstart(&s, "BZh") && isdigit(*s)) xprintf("bzip2 compressed data, block size = %c00k\n", *s); - else if (len>31 && peek_be(s, 7) == 0xfd377a585a0000ULL) + else if (len>31 && !memcmp(s, "\xfd\x37\x7a\x58\x5a\x00", 7)) xputs("xz compressed data"); else if (len>10 && strstart(&s, "\x28\xb5\x2f\xfd")) xputs("zstd compressed data"); -- 2.39.5