]> git.saurik.com Git - apple/shell_cmds.git/blobdiff - find/function.c
shell_cmds-187.tar.gz
[apple/shell_cmds.git] / find / function.c
index 4e3950dbbaf5647cd0814694d612dc01ddb9d7f5..a673a025f1691d2518503c67ebfab6efd6e42d3b 100644 (file)
@@ -1145,11 +1145,24 @@ f_name(PLAN *plan, FTSENT *entry)
 {
        char fn[PATH_MAX];
        const char *name;
+       ssize_t len;
 
        if (plan->flags & F_LINK) {
-               name = fn;
-               if (readlink(entry->fts_accpath, fn, sizeof(fn)) == -1)
+               /*
+                * The below test both avoids obviously useless readlink()
+                * calls and ensures that symlinks with existent target do
+                * not match if symlinks are being followed.
+                * Assumption: fts will stat all symlinks that are to be
+                * followed and will return the stat information.
+                */
+               if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL &&
+                   entry->fts_info != FTS_SLNONE)
                        return 0;
+               len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1);
+               if (len == -1)
+                       return 0;
+               fn[len] = '\0';
+               name = fn;
        } else if (entry->fts_namelen == 0) {
                name = basename(entry->fts_path);
        } else