Browse Source

Drop PATH_MAX usage

Strict POSIX systems (such as GNU Hurd) don't define PATH_MAX, which
results in a build failure:

https://buildd.debian.org/status/fetch.php?pkg=snac2&arch=hurd-amd64&ver=2.91-1&stamp=1774482136&raw=0

This commit replaces PATH_MAX with a dynamically-allocated buffer,
which is the usual way of fixing these failures.

Signed-off-by: Sergio Durigan Junior <sergiodj@sergiodj.net>
Sergio Durigan Junior 4 months ago
parent
commit
2c9f5956b5
1 changed files with 9 additions and 4 deletions
  1. 9 4
      snac.c

+ 9 - 4
snac.c

@@ -182,8 +182,9 @@ int check_password(const char *uid, const char *passwd, const char *hash)
 char* findprog(const char *prog)
 /* find a prog in PATH and return the first match */
 {
-    char *path_env, *path, *dir, filename[PATH_MAX];
+    char *path_env, *path, *dir, *filename;
     int len;
+    size_t filename_sz;
     struct stat sbuf;
 
     path_env = getenv("PATH");
@@ -195,6 +196,9 @@ char* findprog(const char *prog)
         return NULL;
     path = path_env;
 
+    filename_sz = strlen(path) + strlen(prog) + 2;
+    filename = xs_realloc(NULL, filename_sz);
+
     while ((dir = strsep(&path, ":")) != NULL) {
         /* empty entries as ./ instead of / */
         if (*dir == '\0')
@@ -205,16 +209,17 @@ char* findprog(const char *prog)
         while (len > 0 && dir[len-1] == '/')
             dir[--len] = '\0';
 
-        len = snprintf(filename, sizeof(filename), "%s/%s", dir, prog);
-        if (len > 0 && len < (int) sizeof(filename) &&
+        len = snprintf(filename, filename_sz, "%s/%s", dir, prog);
+        if (len > 0 && len < (int) filename_sz &&
             (stat(filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
             access(filename, X_OK) == 0) {
             free(path_env);
-            return strdup(filename);
+            return filename;
         }
     }
 
     free(path_env);
+    xs_free(filename);
     return NULL;
 }