Remove use of PATH_MAX to build on Hurd
authorPetter Reinholdtsen <pere@debian.org>
Mon, 14 Oct 2024 21:56:23 +0000 (23:56 +0200)
committerAntoine Jacquet <royale@zerezo.com>
Mon, 6 Jan 2025 11:23:34 +0000 (12:23 +0100)
This also ensure the program work with any file system on Linux,
as PATH_MAX is not really a relevant limit for all file systems.

fapg.c

diff --git a/fapg.c b/fapg.c
index e9d2b4d..d205eec 100644 (file)
--- a/fapg.c
+++ b/fapg.c
@@ -1484,7 +1484,7 @@ void parse_directory(unsigned char *path, unsigned char * original_path)
 {
     int i, n;
     struct dirent **namelist;
-    unsigned char newpath[PATH_MAX];
+    unsigned char *newpath = NULL;
     struct stat infos;
 
     if(debug)
@@ -1504,10 +1504,14 @@ void parse_directory(unsigned char *path, unsigned char * original_path)
         return;
     }
     for(i = 0; i < n; i++) {
-        snprintf(newpath, PATH_MAX, "%s/%s", path, namelist[i]->d_name);
+        int pathlen = strlen(path)+strlen(namelist[i]->d_name)+2;
+        newpath = malloc(pathlen);
+        snprintf(newpath, pathlen, "%s/%s", path, namelist[i]->d_name);
 
         if(stat(newpath, &infos) != 0) {
             fprintf(stderr, "Warning >> can't stat entry : %s\n", newpath);
+            free(newpath);
+            newpath = NULL;
             continue;
         }
         if(recursive && S_ISDIR(infos.st_mode)
@@ -1521,6 +1525,8 @@ void parse_directory(unsigned char *path, unsigned char * original_path)
         }
         free(namelist[i]);
     }
+    newpath = NULL;
+    free(newpath);
     free(namelist);
 }
 
@@ -1604,13 +1610,17 @@ int main(int argc, char **argv)
         sprintf(pwd, "%s/", pwd_source);
         
         if(fromstdin) {
-            unsigned char path[PATH_MAX];
+            char *path = NULL;
             int i;
-            while(fgets(path, PATH_MAX, stdin)) {
-                for(i = 0; i < PATH_MAX; i++)
+            size_t pathlen;
+
+            while(-1 != getline(&path, &pathlen, stdin)) {
+                for(i = 0; i < pathlen; i++)
                     if(path[i] == '\r' || path[i] == '\n')
                         path[i] = '\0';
                 if (i <= 0) {
+                    free(path);
+                    path = NULL;
                     continue;
                 }
 
@@ -1620,6 +1630,8 @@ int main(int argc, char **argv)
                 }
 
                 parse_directory(path, pwd);
+                free(path);
+                path = NULL;
             }
         } else
             for(; optind < argc; optind++) {