Make glob work on mingw.
authorBruno Haible <bruno@clisp.org>
Sat, 13 Sep 2008 10:23:29 +0000 (12:23 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 13 Sep 2008 10:23:29 +0000 (12:23 +0200)
ChangeLog
lib/glob.c

index 74acd35..fa44475 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-09-13  Yoann Vandoorselaere  <yoann.v@prelude-ids.com>
+            Bruno Haible  <bruno@clisp.org>
+
+       * lib/glob.c: Don't include <pwd.h> on native Windows.
+       (WINDOWS32): New macro.
+       (glob) [WINDOW32]: Provide a reasonable replacement for getenv("HOME").
+
 2008-09-13  Bruno Haible  <bruno@clisp.org>
 
        * lib/glthread/cond.h [USE_SOLARIS_THREADS]
index 24aec87..c38ee4c 100644 (file)
 # define POSIX
 #endif
 
-#include <pwd.h>
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+# define WINDOWS32
+#endif
+
+#ifndef WINDOWS32
+# include <pwd.h>
+#endif
 
 #include <errno.h>
 #ifndef __set_errno
@@ -565,8 +571,26 @@ glob (pattern, flags, errfunc, pglob)
            home_dir = "SYS:";
 # else
 #  ifdef WINDOWS32
+         /* Windows NT defines HOMEDRIVE and HOMEPATH.  But give preference
+            to HOME, because the user can change HOME.  */
          if (home_dir == NULL || home_dir[0] == '\0')
-            home_dir = "c:/users/default"; /* poor default */
+           {
+             const char *home_drive = getenv ("HOMEDRIVE");
+             const char *home_path = getenv ("HOMEPATH");
+
+             if (home_drive != NULL && home_path != NULL)
+               {
+                 size_t home_drive_len = strlen (home_drive);
+                 size_t home_path_len = strlen (home_path);
+                 char *mem = alloca (home_drive_len + home_path_len + 1);
+
+                 memcpy (mem, home_drive, home_drive_len);
+                 memcpy (mem + home_drive_len, home_path, home_path_len + 1);
+                 home_dir = mem;
+               }
+             else
+               home_dir = "c:/users/default"; /* poor default */
+           }
 #  else
          if (home_dir == NULL || home_dir[0] == '\0')
            {