If patterns has a trailing slash then glob has to match directories only.

If the pattern is only 2 characters long (e.g. */) glob already matches
directories only. If the pattern is longer e.g. hello*/ then glob matches only
directories or files and directoires depending on type in readdir_result.

regards, Dmitry

diff --git a/glob/glob.c b/glob/glob.c
index f3911bc..6f8c8a4 100644
--- a/glob/glob.c
+++ b/glob/glob.c
@@ -370,6 +375,7 @@ glob (pattern, flags, errfunc, pglob)
   size_t dirlen;
   int status;
   int oldcount;
+  int retval = 0;
 
   if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
     {
@@ -1028,24 +1034,43 @@ glob (pattern, flags, errfunc, pglob)
   if (flags & GLOB_MARK)
     {
       /* Append slashes to directory names.  */
-      int i;
+      int i, e;
       struct stat st;
-      for (i = oldcount; i < pglob->gl_pathc; ++i)
-       if (((flags & GLOB_ALTDIRFUNC)
-            ? (*pglob->gl_stat) (pglob->gl_pathv[i], &st)
-            : __stat (pglob->gl_pathv[i], &st)) == 0
-           && S_ISDIR (st.st_mode))
-         {
-           size_t len = strlen (pglob->gl_pathv[i]) + 2;
-           char *new = realloc (pglob->gl_pathv[i], len);
-           if (new == NULL)
-             {
-               globfree (pglob);
-               return GLOB_NOSPACE;
-             }
-           strcpy (&new[len - 2], "/");
-           pglob->gl_pathv[i] = new;
-         }
+      for (i = e = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
+        {
+         if (((flags & GLOB_ALTDIRFUNC)
+              ? (*pglob->gl_stat) (pglob->gl_pathv[i], &st)
+              : __stat (pglob->gl_pathv[i], &st)) == 0
+             && S_ISDIR (st.st_mode))
+           {
+             size_t len = strlen (pglob->gl_pathv[i]) + 2;
+             char *new = realloc (pglob->gl_pathv[i], len);
+             if (new == NULL)
+               {
+                 globfree (pglob);
+                 return GLOB_NOSPACE;
+               }
+             strcpy (&new[len - 2], "/");
+             if (pglob->gl_pathv[e] == NULL)
+               {
+                 pglob->gl_pathv[e++] = new;
+                 pglob->gl_pathv[i] = NULL;
+               }
+             else
+               pglob->gl_pathv[i] = new;
+           }
+         else if (flags & GLOB_ONLYDIR)
+           {
+             free (pglob->gl_pathv[i]);
+             pglob->gl_pathv[i] = NULL;
+             if (pglob->gl_pathv[e] != NULL)
+               e = i;
+           }
+       }
+       if (pglob->gl_pathv[e] == NULL)
+         pglob->gl_pathc = e - pglob->gl_offs;
+       if (pglob->gl_pathc == 0)
+         retval = GLOB_NOMATCH;
     }
 
   if (!(flags & GLOB_NOSORT))
@@ -1061,7 +1086,7 @@ glob (pattern, flags, errfunc, pglob)
             sizeof (char *), collated_compare);
     }
 
-  return 0;
+  return retval;
 }
 
 

_______________________________________________
Bug-make mailing list
Bug-make@gnu.org
https://lists.gnu.org/mailman/listinfo/bug-make

Reply via email to