]> git.refcnt.org Git - colorize.git/commitdiff
Drop wordexp(3) in favor of glob(3)
authorSteven Schubiger <stsc@refcnt.org>
Tue, 28 Jul 2026 13:34:14 +0000 (15:34 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Tue, 28 Jul 2026 13:34:14 +0000 (15:34 +0200)
- more portable overall
- fixes broken OpenBSD build
- fixes SEGV with previous use of wordexp(3)

TODO
colorize.c

diff --git a/TODO b/TODO
index 73f8eca619154cd5f13ed7376fef1af5a4acedc8..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1 +0,0 @@
-- fix broken OpenBSD build (wordexp.h missing)
index 67362006e3f6b0483acb6b441aed63bb97171bfa..a426d614be1b76f5c93e295df1af0e5420416923 100644 (file)
@@ -27,6 +27,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <getopt.h>
+#include <glob.h>
 #include <pwd.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -37,7 +38,6 @@
 #include <sys/stat.h>
 #include <time.h>
 #include <unistd.h>
-#include <wordexp.h>
 
 #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;
 }