Hi Bruno,

Le vendredi 12 septembre 2008 à 02:17 +0200, Bruno Haible a écrit :
> > Attached are a few patch that fixes glob ...
> > compilation problem under MinGW.
> 
> A couple of details in this patch can be improved:
> 
>   - The predefine 'WIN32' is sometimes also defined for Cygwin, and is
>     not defined on some native Win32 platforms. The right test for platforms
>     that use the MSVCRT is
>        (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
> 
>   - You comment out the use of <pwd.h> and provide no replacement.
>     Nowadays, Windows systems have a home directory per user, but
>     getenv("HOME") is usually not set. The standard way to get the
>     home directory is getenv("HOMEDRIVE") and getenv("HOMEPATH") and
>     glue the two strings together (by simple string concatenation).

Thanks for the quick review, updated path attached!

-- 
Yoann Vandoorselaere <[EMAIL PROTECTED]>
diff --git a/lib/glob.c b/lib/glob.c
index 24aec87..a376749 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -43,7 +43,13 @@
 # define POSIX
 #endif
 
-#include <pwd.h>
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+# define USE_WIN32
+#endif
+
+#ifndef USE_WIN32
+# include <pwd.h>
+#endif
 
 #include <errno.h>
 #ifndef __set_errno
@@ -81,7 +87,7 @@
 
 /* If the system has the `struct dirent64' type we use it internally.  */
 #if defined _LIBC && !defined COMPILE_GLOB64
-# if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
+# if (defined POSIX || defined USE_WIN32) && !defined __GNU_LIBRARY__
 #  define CONVERT_D_INO(d64, d32)
 # else
 #  define CONVERT_D_INO(d64, d32) \
@@ -102,7 +108,7 @@
 #endif
 
 
-#if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
+#if (defined POSIX || defined USE_WIN32) && !defined __GNU_LIBRARY__
 /* Posix does not require that the d_ino field be present, and some
    systems do not provide it. */
 # define REAL_DIR_ENTRY(dp) 1
@@ -423,14 +429,14 @@ glob (pattern, flags, errfunc, pglob)
 
   /* Find the filename.  */
   filename = strrchr (pattern, '/');
-#if defined __MSDOS__ || defined WINDOWS32
+#if defined __MSDOS__ || defined USE_WIN32
   /* The case of "d:pattern".  Since `:' is not allowed in
      file names, we can safely assume that wherever it
      happens in pattern, it signals the filename part.  This
      is so we could some day support patterns like "[a-z]:foo".  */
   if (filename == NULL)
     filename = strchr (pattern, ':');
-#endif /* __MSDOS__ || WINDOWS32 */
+#endif /* __MSDOS__ || USE_WIN32 */
   dirname_modified = 0;
   if (filename == NULL)
     {
@@ -470,7 +476,7 @@ glob (pattern, flags, errfunc, pglob)
     {
       char *newp;
       dirlen = filename - pattern;
-#if defined __MSDOS__ || defined WINDOWS32
+#if defined __MSDOS__ || defined USE_WIN32
       if (*filename == ':'
 	  || (filename > pattern + 1 && filename[-1] == ':'))
 	{
@@ -494,7 +500,7 @@ glob (pattern, flags, errfunc, pglob)
       ++filename;
 
       if (filename[0] == '\0'
-#if defined __MSDOS__ || defined WINDOWS32
+#if defined __MSDOS__ || defined USE_WIN32
           && dirname[dirlen - 1] != ':'
 	  && (dirlen < 3 || dirname[dirlen - 2] != ':'
 	      || dirname[dirlen - 1] != '/')
@@ -560,13 +566,27 @@ glob (pattern, flags, errfunc, pglob)
 	{
 	  /* Look up home directory.  */
 	  const char *home_dir = getenv ("HOME");
+#ifdef USE_WIN32
+          char win_homedir[PATH_MAX];
+#endif
+
 # ifdef _AMIGA
 	  if (home_dir == NULL || home_dir[0] == '\0')
 	    home_dir = "SYS:";
 # else
-#  ifdef WINDOWS32
-	  if (home_dir == NULL || home_dir[0] == '\0')
-            home_dir = "c:/users/default"; /* poor default */
+#  ifdef USE_WIN32
+	  if (home_dir == NULL || home_dir[0] == '\0') {
+            char  *hdrv, *hpath;
+            hdrv = getenv("HOMEDRIVE");
+            hpath = getenv("HOMEPATH");
+            if ( hdrv && hpath ) { 
+              snprintf(win_homedir, sizeof(win_homedir), "%s%s", hdrv, hpath);
+              home_dir = win_homedir;
+            }
+                  
+            else
+              home_dir = "c:/users/default"; /* poor default */
+          }
 #  else
 	  if (home_dir == NULL || home_dir[0] == '\0')
 	    {
@@ -629,7 +649,7 @@ glob (pattern, flags, errfunc, pglob)
 	      else
 		home_dir = "~"; /* No luck.  */
 	    }
-#  endif /* WINDOWS32 */
+#  endif /* USE_WIN32 */
 # endif
 	  /* Now construct the full directory.  */
 	  if (dirname[1] == '\0')
@@ -649,7 +669,7 @@ glob (pattern, flags, errfunc, pglob)
 	    }
 	  dirname_modified = 1;
 	}
-# if !defined _AMIGA && !defined WINDOWS32
+# if !defined _AMIGA && !defined USE_WIN32
       else
 	{
 	  char *end_name = strchr (dirname, '/');
@@ -765,7 +785,7 @@ glob (pattern, flags, errfunc, pglob)
 		 home directory.  */
 	      return GLOB_NOMATCH;
 	}
-# endif	/* Not Amiga && not WINDOWS32.  */
+# endif	/* Not Amiga && not USE_WIN32.  */
     }
 
   /* Now test whether we looked for "~" or "~NAME".  In this case we
@@ -1113,7 +1133,7 @@ prefix_array (const char *dirname, char **array, size_t n)
 {
   register size_t i;
   size_t dirlen = strlen (dirname);
-#if defined __MSDOS__ || defined WINDOWS32
+#if defined __MSDOS__ || defined USE_WIN32
   int sep_char = '/';
 # define DIRSEP_CHAR sep_char
 #else
@@ -1124,7 +1144,7 @@ prefix_array (const char *dirname, char **array, size_t n)
     /* DIRNAME is just "/", so normal prepending would get us "//foo".
        We want "/foo" instead, so don't prepend any chars from DIRNAME.  */
     dirlen = 0;
-#if defined __MSDOS__ || defined WINDOWS32
+#if defined __MSDOS__ || defined USE_WIN32
   else if (dirlen > 1)
     {
       if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')

Reply via email to