From 0575aaaa0779812752427badbc0f80a09aac02a4 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 23 Apr 2023 09:00:04 +0100 Subject: win32: export xappendword() Export the function xappendword() from make. Use it in drop and watch. Saves 8-80 bytes, an unusually large disparity. --- include/mingw.h | 1 + miscutils/drop.c | 4 +--- miscutils/make.c | 13 ------------- procps/watch.c | 7 +++++++ win32/mingw.c | 12 ++++++++++++ 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index 3cf0526f2..c2c37ba48 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -594,3 +594,4 @@ const char *applet_to_exe(const char *name); char *get_user_name(void); char *quote_arg(const char *arg); char *find_first_executable(const char *name); +char *xappendword(const char *str, const char *word); diff --git a/miscutils/drop.c b/miscutils/drop.c index db3d709d1..342de41d6 100644 --- a/miscutils/drop.c +++ b/miscutils/drop.c @@ -165,10 +165,8 @@ int drop_main(int argc, char **argv) // Build the command line while (*a) { char *q = quote_arg(*a++); - char *newcmd = xasprintf("%s %s", cmd, q); + cmd = xappendword(cmd, q); free(q); - free(cmd); - cmd = newcmd; } ZeroMemory(&si, sizeof(STARTUPINFO)); diff --git a/miscutils/make.c b/miscutils/make.c index eddc07126..07d824752 100644 --- a/miscutils/make.c +++ b/miscutils/make.c @@ -333,19 +333,6 @@ auto_concat(const char *s1, const char *s2) return auto_string(xasprintf("%s%s", s1, s2)); } -/* - * Append a word to a space-separated string of words. The first - * call should use a NULL pointer for str, subsequent calls should - * pass an allocated string which will be freed. - */ -static char * -xappendword(const char *str, const char *word) -{ - char *newstr = str ? xasprintf("%s %s", str, word) : xstrdup(word); - free((void *)str); - return newstr; -} - static unsigned int getbucket(const char *name) { diff --git a/procps/watch.c b/procps/watch.c index 1190b29df..56b128df6 100644 --- a/procps/watch.c +++ b/procps/watch.c @@ -71,9 +71,16 @@ int watch_main(int argc UNUSED_PARAM, char **argv) // watch from both procps 2.x and 3.x does concatenation. Example: // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param +#if ENABLE_PLATFORM_MINGW32 + cmd = NULL; + do { + cmd = xappendword(cmd, *argv); + } while (*++argv); +#else cmd = *argv; while (*++argv) cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd +#endif period = parse_duration_str(period_str); width = (unsigned)-1; // make sure first time new_width != width diff --git a/win32/mingw.c b/win32/mingw.c index e81f17f11..9e1cf5eea 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -2389,3 +2389,15 @@ const char *applet_to_exe(const char *name) return name; } #endif + +/* + * Append a word to a space-separated string of words. The first + * call should use a NULL pointer for str, subsequent calls should + * pass an allocated string which will be freed. + */ +char *xappendword(const char *str, const char *word) +{ + char *newstr = str ? xasprintf("%s %s", str, word) : xstrdup(word); + free((void *)str); + return newstr; +} -- cgit v1.2.3-55-g6feb