- if (d == NULL)
- break;
- if (! REAL_DIR_ENTRY (d))
- continue;
-
- /* If we shall match only directories use the information
- provided by the dirent call if possible. */
- if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
- continue;
-
- name = d->d_name;
-
- if (fnmatch (pattern, name, fnm_flags) == 0)
- {
- /* ISDIR will often be incorrectly set to false
- when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
- don't care. It won't be used and we save the
- expensive call to stat. */
- int need_dir_test =
- (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
- ? GLOB_ONLYDIR : 0));
- bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
- || ((flags & need_dir_test)
- && is_dir_p (directory, dirlen, name,
- pglob, flags)));
-
- /* In GLOB_ONLYDIR mode, skip non-dirs. */
- if ((flags & GLOB_ONLYDIR) && !isdir)
- continue;
-
- {
- struct globlink *new =
- __alloca (sizeof (struct globlink));
- char *p;
- len = NAMLEN (d);
- new->name =
- malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
- if (new->name == NULL)
- goto memory_error;
- p = mempcpy (new->name, name, len);
- if ((flags & GLOB_MARK) && isdir)
- *p++ = '/';
- *p = '\0';
- new->next = names;
- names = new;
- ++nfound;
- }
- }
- }
- }
- }
+ if (d == NULL)
+ break;
+ if (! REAL_DIR_ENTRY (d))
+ continue;
+
+ /* If we shall match only directories use the information
+ provided by the dirent call if possible. */
+ if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
+ continue;
+
+ name = d->d_name;
+
+ if (fnmatch (pattern, name, fnm_flags) == 0)
+ {
+ /* If the file we found is a symlink we have to
+ make sure the target file exists. */
+ if (!DIRENT_MIGHT_BE_SYMLINK (d)
+ || link_exists_p (dfd, directory, dirlen, name, pglob,
+ flags))
+ {
+ if (cur == names->count)
+ {
+ struct globnames *newnames;
+ size_t count = names->count * 2;
+ size_t size = (sizeof (struct globnames)
+ + ((count - INITIAL_COUNT)
+ * sizeof (char *)));
+ allocasize += size;
+ if (__libc_use_alloca (allocasize))
+ newnames = names_alloca = __alloca (size);
+ else if ((newnames = malloc (size))
+ == NULL)
+ goto memory_error;
+ newnames->count = count;
+ newnames->next = names;
+ names = newnames;
+ cur = 0;
+ }
+ len = _D_EXACT_NAMLEN (d);
+ names->name[cur] = malloc (len + 1);
+ if (names->name[cur] == NULL)
+ goto memory_error;
+ *((char *) mempcpy (names->name[cur++], name, len))
+ = '\0';
+ ++nfound;
+ }
+ }
+ }
+ }