Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2004:
[Freeciv-Dev] (PR#10744) PATH_MAX
Home

[Freeciv-Dev] (PR#10744) PATH_MAX

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#10744) PATH_MAX
From: "(Gaël Le Mignot)" <kilobug@xxxxxxxxxxx>
Date: Tue, 26 Oct 2004 10:51:35 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=10744 >


Hello,

Freeciv  uses PATH_MAX,  which  is not  mandatory  according to  POSIX
standard.  GNU/Hurd, which  supports unlimited  path length,  does not
define such a constant.

I wrote a small patch for  the 2.0beta2 version, to remove the uses of
PATH_MAX (except in the  WIN32 conditionals, relaying upon PATH_MAX in
win32 is not a problem).

I quickly tested  the resulting version (gtk2 client  and server), and
it worked fine on my GNU/Hurd system.

I had only  a problem: I couldn't start the server  from the client, I
had to run it manually. I'll have look at this issue later on.

I hope this patch is OK for you.


diff -ru freeciv-2.0.0-beta2/utility/shared.c 
freeciv-2.0.0-beta2-hurd/utility/shared.c
--- freeciv-2.0.0-beta2/utility/shared.c        2004-10-19 03:28:09.000000000 
+0200
+++ freeciv-2.0.0-beta2-hurd/utility/shared.c   2004-10-26 19:13:54.000000000 
+0200
@@ -1075,19 +1075,23 @@
 
   /* First assemble a full list of names. */
   for (dir_num = 0; dirs[dir_num]; dir_num++) {
-    char path[PATH_MAX];
+    char *path = NULL;
     DIR *dir;
     struct dirent *entry;
 
     if (subpath) {
-      my_snprintf(path, sizeof(path), "%s/%s", dirs[dir_num], subpath);
+      size_t len = strlen(dirs[dir_num]) + strlen(subpath) + 2;
+      path = fc_malloc(len);
+      my_snprintf(path, len, "%s/%s", dirs[dir_num], subpath);
     } else {
-      sz_strlcpy(path, dirs[dir_num]);
+      path = mystrdup(dirs[dir_num]);
     }
 
     /* Open the directory for reading. */
     dir = opendir(path);
     if (!dir) {
+      if (path)
+       free(path);
       continue;
     }
 
@@ -1128,6 +1132,8 @@
     }
 
     closedir(dir);
+    if (path)
+      free(path);
   }
 
   /* Sort the list by name. */
@@ -1454,6 +1460,26 @@
   }
 }
 
+/***************************************************************************
+  Interpret ~/ in filename as home dir
+  New path is returned in buf, as a newly allocated buffer
+***************************************************************************/
+void interpret_tilde_alloc(char** buf, const char* filename)
+{
+  if (filename[0] == '~' && filename[1] == '/') {
+    const char *home = user_home_dir();
+    size_t sz;
+    filename += 2;
+    sz = strlen(home) + strlen(filename) + 2;
+    *buf = fc_malloc(sz);
+    my_snprintf(*buf, sz, "%s/%s", home, filename);
+  } else if (filename[0] == '~' && filename[1] == '\0') {
+    *buf = mystrdup(user_home_dir());
+  } else  {
+    *buf = mystrdup(filename);
+  }
+}
+
 /**************************************************************************
   If the directory "pathname" does not exist, recursively create all
   directories until it does.
@@ -1461,31 +1487,30 @@
 bool make_dir(const char *pathname)
 {
   char *dir;
-  char file[PATH_MAX];
-  char path[PATH_MAX];
-
-  interpret_tilde(file, sizeof(file), pathname);
-  path[0] = '\0';
-
-#ifndef WIN32_NATIVE
-  /* Ensure we are starting from the root in absolute pathnames. */
-  if (file[0] == '/') {
-    sz_strlcat(path, "/");
-  }
-#endif
+  char *path = NULL;
 
-  for (dir = strtok(file, "/"); dir; dir = strtok(NULL, "/")) {
-    sz_strlcat(path, dir);
+  interpret_tilde_alloc(&path, pathname);
+  dir = path;
+  do {
+    dir = strchr(dir, '/');
+    /* We set the current / with 0, and restore it afterwards */
+    if (dir)
+      *dir = 0;
 
+    if (path)
 #ifdef WIN32_NATIVE
-    mkdir(path);
+      mkdir(path);
 #else
-    mkdir(path, 0755);
+      mkdir(path, 0755);
 #endif
+      
+    if (dir) {
+      *dir = '/';
+      dir++;
+    }
+  } while (dir);
 
-    sz_strlcat(path, "/");
-  }
-
+  free(path);
   return TRUE;
 }
 
diff -ru freeciv-2.0.0-beta2/utility/shared.h 
freeciv-2.0.0-beta2-hurd/utility/shared.h
--- freeciv-2.0.0-beta2/utility/shared.h        2004-10-21 22:11:04.000000000 
+0200
+++ freeciv-2.0.0-beta2-hurd/utility/shared.h   2004-10-26 18:43:58.000000000 
+0200
@@ -279,6 +279,7 @@
 
 char *get_multicast_group(void);
 void interpret_tilde(char* buf, size_t buf_size, const char* filename);
+void interpret_tilde_alloc(char** buf, const char* filename);
 
 bool make_dir(const char *pathname);
 bool path_is_absolute(const char *filename);

-- 
Gael Le Mignot "Kilobug" - kilobug@xxxxxxxxx - http://kilobug.free.fr
GSM         : 06.71.47.18.22 (in France)   ICQ UIN   : 7299959
Fingerprint : 1F2C 9804 7505 79DF 95E6 7323 B66B F67B 7103 C5DA

Member of HurdFr: http://hurdfr.org - The GNU Hurd: http://hurd.gnu.org

[Prev in Thread] Current Thread [Next in Thread]