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: kilobug@xxxxxxxxxxx
Subject: [Freeciv-Dev] (PR#10744) PATH_MAX
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 27 Oct 2004 14:28:24 -0700
Reply-to: rt@xxxxxxxxxxx

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

> [kilobug@xxxxxxxxxxx - Tue Oct 26 17:51:32 2004]:
> 
> 
> 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).

Here is I think a slightly better patch.

- No need to alloc memory in datafilelist_infix.
- interpret_tilde_alloc returns the string (rather than having another
parameter).
- Slightly simpler mkdir (path is never NULL).

The only tricky part is in mkdir.  Vasco, will you take a look at this?

jason

? data/flags/armenia.svg
? data/flags/drawing.svg
? data/flags/svg
Index: utility/shared.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.c,v
retrieving revision 1.119
diff -u -r1.119 shared.c
--- utility/shared.c    18 Oct 2004 23:49:27 -0000      1.119
+++ utility/shared.c    27 Oct 2004 21:26:33 -0000
@@ -1075,7 +1075,8 @@
 
   /* First assemble a full list of names. */
   for (dir_num = 0; dirs[dir_num]; dir_num++) {
-    char path[PATH_MAX];
+    size_t len = (subpath ? strlen(subpath) : 0) + strlen(dirs[dir_num]) + 2;
+    char path[len];
     DIR *dir;
     struct dirent *entry;
 
@@ -1442,6 +1443,9 @@
 /***************************************************************************
   Interpret ~/ in filename as home dir
   New path is returned in buf of size buf_size
+
+  This may fail if the path is too long.  It is better to use
+  interpret_tilde_alloc.
 ***************************************************************************/
 void interpret_tilde(char* buf, size_t buf_size, const char* filename)
 {
@@ -1454,6 +1458,31 @@
   }
 }
 
+/***************************************************************************
+  Interpret ~/ in filename as home dir
+
+  The new path is returned in buf, as a newly allocated buffer.  The new
+  path will always be allocated and written, even if there is no ~ present.
+***************************************************************************/
+char *interpret_tilde_alloc(const char* filename)
+{
+  if (filename[0] == '~' && filename[1] == '/') {
+    const char *home = user_home_dir();
+    size_t sz;
+    char *buf;
+
+    filename += 2; /* Skip past "~/" */
+    sz = strlen(home) + strlen(filename) + 2;
+    buf = fc_malloc(sz);
+    my_snprintf(buf, sz, "%s/%s", home, filename);
+    return buf;
+  } else if (filename[0] == '~' && filename[1] == '\0') {
+    return mystrdup(user_home_dir());
+  } else  {
+    return mystrdup(filename);
+  }
+}
+
 /**************************************************************************
   If the directory "pathname" does not exist, recursively create all
   directories until it does.
@@ -1461,31 +1490,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);
+  path = interpret_tilde_alloc(pathname);
+  dir = path;
+  do {
+    dir = strchr(dir, '/');
+    /* We set the current / with 0, and restore it afterwards */
+    if (dir) {
+      *dir = 0;
+    }
 
 #ifdef WIN32_NATIVE
     mkdir(path);
 #else
     mkdir(path, 0755);
 #endif
+      
+    if (dir) {
+      *dir = '/';
+      dir++;
+    }
+  } while (dir);
 
-    sz_strlcat(path, "/");
-  }
-
+  free(path);
   return TRUE;
 }
 
Index: utility/shared.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.h,v
retrieving revision 1.137
diff -u -r1.137 shared.h
--- utility/shared.h    21 Oct 2004 20:27:29 -0000      1.137
+++ utility/shared.h    27 Oct 2004 21:26:33 -0000
@@ -279,6 +279,7 @@
 
 char *get_multicast_group(void);
 void interpret_tilde(char* buf, size_t buf_size, const char* filename);
+char *interpret_tilde_alloc(const char* filename);
 
 bool make_dir(const char *pathname);
 bool path_is_absolute(const char *filename);

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