autoupdate
[gnulib.git] / lib / glob.c
index 0229dbd..5aea0b3 100644 (file)
@@ -1,21 +1,19 @@
-/* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007
+/* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
    Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
 
-   The GNU C Library is distributed in the hope that it will be useful,
+   This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #ifndef _LIBC
 # include <config.h>
@@ -32,6 +30,8 @@
 /* #define NDEBUG 1 */
 #include <assert.h>
 
+#include <stdbool.h>
+
 #include <stdio.h>             /* Needed on stupid SunOS for assert.  */
 
 #if !defined _LIBC || !defined GLOB_ONLY_P
 # 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
 
 #include <fnmatch.h>
 
-#ifndef _LIBC
-# include "dirfd.h"
-#endif
-
 #ifdef _SC_GETPW_R_SIZE_MAX
 # define GETPW_R_SIZE_MAX()    sysconf (_SC_GETPW_R_SIZE_MAX)
 #else
@@ -182,7 +184,10 @@ static const char *next_brace_sub (const char *begin, int flags) __THROW;
 
 #ifndef _LIBC
 /* The results of opendir() in this file are not used with dirfd and fchdir,
-   therefore save some unnecessary work in fchdir.c.  */
+   and we do not leak fds to any single-threaded code that could use stdio,
+   therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
+   FIXME - if the kernel ever adds support for multi-thread safety for
+   avoiding standard fds, then we should use opendir_safer.  */
 # undef opendir
 # undef closedir
 
@@ -563,8 +568,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')
            {