From b652da6c1c5eccd49cfe3ddf9e79a8c583c3cfd2 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Tue, 28 Jul 2026 15:34:14 +0200 Subject: Drop wordexp(3) in favor of glob(3) - more portable overall - fixes broken OpenBSD build - fixes SEGV with previous use of wordexp(3) --- colorize.c | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'colorize.c') diff --git a/colorize.c b/colorize.c index 6736200..a426d61 100644 --- a/colorize.c +++ b/colorize.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -37,7 +38,6 @@ #include #include #include -#include #ifndef DEBUG # define DEBUG 0 @@ -203,7 +203,8 @@ enum { FMT_CONF, FMT_CONF_FILE, FMT_CONF_INIT, - FMT_RAINBOW + FMT_RAINBOW, + FMT_GLOB }; static const char *formats[] = { "%s", /* generic */ @@ -217,7 +218,8 @@ static const char *formats[] = { "%s: option '%s' %s", /* conf */ "config file %s: %s", /* conf file */ "%s %s", /* conf init */ - "%s color '%s' %s %s" /* rainbow */ + "%s color '%s' %s %s", /* rainbow */ + "glob string: %s" /* glob */ }; enum { GENERIC, FOREGROUND = 0, BACKGROUND }; @@ -375,7 +377,7 @@ static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int); static void free_wrap (void **); static char *strdup_wrap (const char *, const char *, unsigned int); static char *str_concat_wrap (const char *, const char *, const char *, unsigned int); -static char *expand_string (const char *); +static char *glob_string (const char *); static bool get_bytes_size (unsigned long, struct bytes_size *); static char *get_file_type (mode_t); static bool has_color_name (const char *, const char *); @@ -430,7 +432,7 @@ main (int argc, char **argv) else { char *s; - if ((s = expand_string (conf_file))) + if ((s = glob_string (conf_file))) { free (conf_file); conf_file = s; @@ -1936,16 +1938,35 @@ str_concat_wrap (const char *str1, const char *str2, const char *file, unsigned return str; } +/* This code is largely untested. */ static char * -expand_string (const char *str) +glob_string (const char *str) { char *s = NULL; - wordexp_t p; + glob_t g; + int r; - wordexp (str, &p, 0); - if (p.we_wordc >= 1) - s = xstrdup (p.we_wordv[0]); - wordfree (&p); + r = glob (str, GLOB_ERR | GLOB_TILDE | GLOB_BRACE, NULL, &g); + + switch (r) + { + case 0: /* matches */ + if (g.gl_pathc >= 1) + s = xstrdup (g.gl_pathv[0]); + globfree (&g); + break; + case GLOB_NOMATCH: + break; + case GLOB_NOSPACE: + vfprintf_fail (formats[FMT_GLOB], "out of memory"); + break; + case GLOB_ABORTED: + vfprintf_fail (formats[FMT_GLOB], "read error"); + break; + default: + vfprintf_fail (formats[FMT_GLOB], "unknown error"); + break; + } return s; } -- cgit v1.2.3