about summary refs log tree commit diff
path: root/posix/glob.c
diff options
context:
space:
mode:
Diffstat (limited to 'posix/glob.c')
-rw-r--r--posix/glob.c157
1 files changed, 118 insertions, 39 deletions
diff --git a/posix/glob.c b/posix/glob.c
index 90dd9d90f9..ce17fe135f 100644
--- a/posix/glob.c
+++ b/posix/glob.c
@@ -37,8 +37,15 @@ Cambridge, MA 02139, USA.  */
    program understand `configure --with-gnu-libc' and omit the object files,
    it is simpler to just do this in the source for each such file.  */
 
-#if defined (_LIBC) || !defined (__GNU_LIBRARY__)
+#define GLOB_INTERFACE_VERSION 1
+#if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
+#include <gnu-versions.h>
+#if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
+#define ELIDE_CODE
+#endif
+#endif
 
+#ifndef ELIDE_CODE
 
 #ifdef	STDC_HEADERS
 #include <stddef.h>
@@ -53,6 +60,8 @@ Cambridge, MA 02139, USA.  */
 #endif
 #endif
 
+#include <pwd.h>
+
 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
 extern int errno;
 #endif
@@ -175,10 +184,7 @@ extern char *alloca ();
 #endif
 
 #ifndef __GNU_LIBRARY__
-#define __lstat lstat
-#ifndef HAVE_LSTAT
-#define lstat stat
-#endif
+#define __stat stat
 #ifdef STAT_MACROS_BROKEN
 #undef S_ISDIR
 #endif
@@ -211,10 +217,6 @@ extern char *alloca ();
 #undef	GLOB_PERIOD
 #include <glob.h>
 
-__ptr_t (*__glob_opendir_hook) __P ((const char *directory));
-const char *(*__glob_readdir_hook) __P ((__ptr_t stream));
-void (*__glob_closedir_hook) __P ((__ptr_t stream));
-
 static int glob_pattern_p __P ((const char *pattern, int quote));
 static int glob_in_dir __P ((const char *pattern, const char *directory,
 			     int flags,
@@ -250,6 +252,56 @@ glob (pattern, flags, errfunc, pglob)
       return -1;
     }
 
+  if (flags & GLOB_BRACE)
+    {
+      const char *begin = strchr (pattern, '{');
+      if (begin != NULL)
+	{
+	  const char *end = strchr (begin + 1, '}');
+	  if (end != NULL && end != begin + 1)
+	    {
+	      size_t restlen = strlen (end + 1) + 1;
+	      const char *p, *comma;
+	      char *buf;
+	      size_t bufsz = 0;
+	      int firstc;
+	      if (!(flags & GLOB_APPEND))
+		{
+		  pglob->gl_pathc = 0;
+		  pglob->gl_pathv = NULL;
+		}
+	      firstc = pglob->gl_pathc;
+	      for (p = begin + 1;; p = comma + 1)
+		{
+		  int result;
+		  comma = strchr (p, ',');
+		  if (comma == NULL)
+		    comma = strchr (p, '\0');
+		  if ((begin - pattern) + (comma - p) + 1 > bufsz)
+		    {
+		      if (bufsz * 2 < comma - p + 1)
+			bufsz *= 2;
+		      else
+			bufsz = comma - p + 1;
+		      buf = __alloca (bufsz);
+		    }
+		  memcpy (buf, pattern, begin - pattern);
+		  memcpy (buf + (begin - pattern), p, comma - p);
+		  memcpy (buf + (begin - pattern) + (comma - p), end, restlen);
+		  result = glob (buf, (flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC) |
+				       GLOB_APPEND), errfunc, pglob);
+		  if (result && result != GLOB_NOMATCH)
+		    return result;
+		  if (*comma == '\0')
+		    break;
+		}
+	      if (pglob->gl_pathc == firstc &&
+		  !(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
+		return GLOB_NOMATCH;
+	    }
+	}
+    }
+
   /* Find the filename.  */
   filename = strrchr (pattern, '/');
   if (filename == NULL)
@@ -291,6 +343,35 @@ glob (pattern, flags, errfunc, pglob)
 
   oldcount = pglob->gl_pathc;
 
+  if ((flags & GLOB_TILDE) && dirname[0] == '~')
+    {
+      if (dirname[1] == '\0')
+	{
+	  /* Look up home directory.  */
+	  dirname = getenv ("HOME");
+	  if (dirname == NULL || dirname[0] == '\0')
+	    {
+	      extern char *getlogin ();
+	      char *name = getlogin ();
+	      if (name != NULL)
+		{
+		  struct passwd *p = getpwnam (name);
+		  if (p != NULL)
+		    dirname = p->pw_dir;
+		}
+	    }
+	  if (dirname == NULL || dirname[0] == '\0')
+	    dirname = (char *) "~"; /* No luck.  */
+	}
+      else
+	{
+	  /* Look up specific user's home directory.  */
+	  struct passwd *p = getpwnam (dirname + 1);
+	  if (p != NULL)
+	    dirname = p->pw_dir;
+	}
+    }
+
   if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
     {
       /* The directory name contains metacharacters, so we
@@ -414,7 +495,8 @@ glob (pattern, flags, errfunc, pglob)
       int i;
       struct stat st;
       for (i = oldcount; i < pglob->gl_pathc; ++i)
-	if (__lstat (pglob->gl_pathv[i], &st) == 0 &&
+	if (((flags & GLOB_ALTDIRFUNC) ?
+	     *pglob->gl_stat : __stat) (pglob->gl_pathv[i], &st) == 0 &&
 	    S_ISDIR (st.st_mode))
 	  {
  	    size_t len = strlen (pglob->gl_pathv[i]) + 2;
@@ -581,8 +663,9 @@ glob_in_dir (pattern, directory, flags, errfunc, pglob)
     {
       flags |= GLOB_MAGCHAR;
 
-      stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory)
-		: (__ptr_t) opendir (directory));
+      stream = ((flags & GLOB_ALTDIRFUNC) ?
+		(*pglob->gl_opendir) (directory) :
+		opendir (directory));
       if (stream == NULL)
 	{
 	  if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
@@ -594,29 +677,20 @@ glob_in_dir (pattern, directory, flags, errfunc, pglob)
 	  {
 	    const char *name;
 	    size_t len;
-
-	    if (__glob_readdir_hook)
-	      {
-		name = (*__glob_readdir_hook) (stream);
-		if (name == NULL)
-		  break;
-		len = 0;
-	      }
-	    else
-	      {
-		struct dirent *d = readdir ((DIR *) stream);
-		if (d == NULL)
-		  break;
-		if (! REAL_DIR_ENTRY (d))
-		  continue;
-		name = d->d_name;
+	    struct dirent *d = ((flags & GLOB_ALTDIRFUNC) ?
+				(*pglob->gl_readdir) (stream) :
+				readdir (stream));
+	    if (d == NULL)
+	      break;
+	    if (! REAL_DIR_ENTRY (d))
+	      continue;
+	    name = d->d_name;
 #ifdef	HAVE_D_NAMLEN
-		len = d->d_namlen;
+	    len = d->d_namlen;
 #else
-		len = 0;
+	    len = 0;
 #endif
-	      }
-		
+
 	    if (fnmatch (pattern, name,
 			 (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
 			 ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
@@ -638,6 +712,10 @@ glob_in_dir (pattern, directory, flags, errfunc, pglob)
 	  }
     }
 
+  if (nfound == 0 && (flags & GLOB_NOMAGIC) &&
+      ! glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)))
+    flags |= GLOB_NOCHECK;
+
   if (nfound == 0 && (flags & GLOB_NOCHECK))
     {
       size_t len = strlen (pattern);
@@ -673,10 +751,10 @@ glob_in_dir (pattern, directory, flags, errfunc, pglob)
   if (stream != NULL)
     {
       int save = errno;
-      if (__glob_closedir_hook)
-	(*__glob_closedir_hook) (stream);
+      if (flags & GLOB_ALTDIRFUNC)
+	(*pglob->gl_closedir) (stream);
       else
-	(void) closedir ((DIR *) stream);
+	closedir (stream);
       errno = save;
     }
   return nfound == 0 ? GLOB_NOMATCH : 0;
@@ -684,10 +762,10 @@ glob_in_dir (pattern, directory, flags, errfunc, pglob)
  memory_error:
   {
     int save = errno;
-    if (__glob_closedir_hook)
-      (*__glob_closedir_hook) (stream);
+    if (flags & GLOB_ALTDIRFUNC)
+      (*pglob->gl_closedir) (stream);
     else
-      (void) closedir ((DIR *) stream);
+      closedir (stream);
     errno = save;
   }
   while (names != NULL)
@@ -699,4 +777,5 @@ glob_in_dir (pattern, directory, flags, errfunc, pglob)
   return GLOB_NOSPACE;
 }
 
-#endif	/* _LIBC or not __GNU_LIBRARY__.  */
+#endif	/* Not ELIDE_CODE.  */
+