/*
- * Copyright (c) 1999, 2000, 2003, 2005, 2008 Apple Inc. All rights reserved.
+ * Copyright (c) 1999, 2000, 2003, 2005, 2008, 2012 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
#define F_STATDIRSYM (1 << F_SHIFT) /* only stat directories and symlinks (and unknowns) */
#define F_ALWAYSSTAT (2 << F_SHIFT) /* always stat */
#define F_STATDIR (3 << F_SHIFT) /* only stat directories (and unknowns) */
+#define F_D_TYPE (4 << F_SHIFT) /* only stat directories but use d_type */
+#define F_D_TYPESYM (5 << F_SHIFT) /* only stat directories and symlinks but use d_type */
static FTS *
__fts_open(argv, sp)
{
register FTSENT *p, *root;
register int nitems;
- FTSENT *parent, *tmp;
+ FTSENT *parent, *tmp = NULL;
int len;
/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
errno = EINVAL;
return (NULL);
}
+ if (options & FTS_NOSTAT_TYPE) options |= FTS_NOSTAT;
/* Allocate/initialize the stream */
if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
errno = EINVAL;
return (NULL);
}
+ if (options & FTS_NOSTAT_TYPE) options |= FTS_NOSTAT;
/* Allocate/initialize the stream */
if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
if (instr == FTS_FOLLOW &&
(p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
p->fts_info = fts_stat(sp, p, 1);
- if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
+ if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
p->fts_errno = errno;
p->fts_info = FTS_ERR;
- } else
+ } else {
p->fts_flags |= FTS_SYMFOLLOW;
+ }
+ }
return (p);
}
if (p->fts_info == FTS_D) {
/* If skipped or crossed mount point, do post-order visit. */
if (instr == FTS_SKIP ||
- ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) {
+ (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
if (p->fts_flags & FTS_SYMFOLLOW)
(void)close(p->fts_symfd);
if (sp->fts_child) {
}
p->fts_info = FTS_DP;
return (p);
- }
+ }
/* Rebuild if only read the names and now traversing. */
if (sp->fts_child && sp->fts_options & FTS_NAMEONLY) {
/* Move to the next node on this level. */
next: tmp = p;
- if (p = p->fts_link) {
- free(tmp);
-
+ if ((p = p->fts_link)) {
/*
* If reached the top, return to the original directory, and
* load the paths for the next root.
return (NULL);
}
fts_load(sp, p);
+ free(tmp);
return (sp->fts_cur = p);
}
* ignore. If followed, get a file descriptor so we can
* get back if necessary.
*/
- if (p->fts_instr == FTS_SKIP)
+ if (p->fts_instr == FTS_SKIP) {
+ free(tmp);
goto next;
+ }
if (p->fts_instr == FTS_FOLLOW) {
p->fts_info = fts_stat(sp, p, 1);
- if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR))
+ if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
if ((p->fts_symfd =
open(".", O_RDONLY, 0)) < 0) {
p->fts_errno = errno;
p->fts_info = FTS_ERR;
- } else
+ } else {
p->fts_flags |= FTS_SYMFOLLOW;
+ }
+ }
p->fts_instr = FTS_NOINSTR;
}
+ free(tmp);
name: t = sp->fts_path + NAPPEND(p->fts_parent);
*t++ = '/';
memmove(t, p->fts_name, p->fts_namelen + 1);
/* Move up to the parent node. */
p = tmp->fts_parent;
- free(tmp);
if (p->fts_level == FTS_ROOTPARENTLEVEL) {
/*
* Done; free everything up and set errno to 0 so the user
* can distinguish between error and EOF.
*/
+ free(tmp);
free(p);
errno = 0;
return (sp->fts_cur = NULL);
return (NULL);
}
}
+ free(tmp);
p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
return (sp->fts_cur = p);
}
DIR *dirp;
void *adjaddr;
int cderrno, descend, len, level, maxlen, dostat, oflag, saved_errno;
- char *cp;
+ char *cp = NULL;
/* Set current node pointer. */
cur = sp->fts_cur;
if (type == BNAMES)
dostat = F_NOSTAT;
+ else if (ISSET(FTS_NOSTAT_TYPE))
+ dostat = ISSET(FTS_PHYSICAL) ? F_D_TYPE : F_D_TYPESYM;
else if (ISSET(FTS_NOSTAT))
dostat = ISSET(FTS_PHYSICAL) ? F_STATDIR : F_STATDIRSYM;
else
* If not changing directories set a pointer so that can just append
* each new name into the path.
*/
- maxlen = sp->fts_pathlen - cur->fts_pathlen - 1;
len = NAPPEND(cur);
if (ISSET(FTS_NOCHDIR)) {
cp = sp->fts_path + len;
*cp++ = '/';
}
+ len++;
+ maxlen = sp->fts_pathlen - len;
level = cur->fts_level + 1;
/* Read the directory, attaching each entry to the `link' pointer. */
adjaddr = NULL;
- for (head = tail = NULL, nitems = 0; dp = readdir(dirp);) {
+ for (head = tail = NULL, nitems = 0; (dp = readdir(dirp)) ; ) {
if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
continue;
if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL)
goto mem1;
- if (dp->d_namlen > maxlen) {
+ if (dp->d_namlen >= maxlen) { /* include space for NUL */
if (fts_palloc(sp, (size_t)dp->d_namlen)) {
/*
* No more memory for path or structures. Save
free(p);
fts_lfree(head);
(void)closedir(dirp);
- errno = saved_errno;
cur->fts_info = FTS_ERR;
SET(FTS_STOP);
+ errno = saved_errno;
return (NULL);
}
adjaddr = sp->fts_path;
maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1;
}
- p->fts_pathlen = len + dp->d_namlen + 1;
- p->fts_parent = sp->fts_cur;
p->fts_level = level;
+ p->fts_parent = sp->fts_cur;
+ p->fts_pathlen = len + dp->d_namlen;
#ifdef FTS_WHITEOUT
if (dp->d_type == DT_WHT)
case (F_ALWAYSSTAT | DT_LNK):
case (F_ALWAYSSTAT | DT_SOCK):
case (F_ALWAYSSTAT | DT_WHT):
+ case (F_D_TYPE | DT_UNKNOWN):
+ case (F_D_TYPE | DT_DIR):
+ case (F_D_TYPESYM | DT_UNKNOWN):
+ case (F_D_TYPESYM | DT_DIR):
+ case (F_D_TYPESYM | DT_LNK):
/* Build a file name for fts_stat to stat. */
if (ISSET(FTS_NOCHDIR)) {
p->fts_accpath = p->fts_path;
/* Stat it. */
p->fts_info = fts_stat(sp, p, 0);
break;
+ case (F_D_TYPE | DT_FIFO):
+ case (F_D_TYPE | DT_CHR):
+ case (F_D_TYPE | DT_BLK):
+ case (F_D_TYPE | DT_SOCK):
+ case (F_D_TYPESYM | DT_FIFO):
+ case (F_D_TYPESYM | DT_CHR):
+ case (F_D_TYPESYM | DT_BLK):
+ case (F_D_TYPESYM | DT_SOCK):
+ p->fts_info = FTS_DEFAULT;
+ goto common_no_stat;
+ case (F_D_TYPE | DT_REG):
+ case (F_D_TYPESYM | DT_REG):
+ p->fts_info = FTS_F;
+ goto common_no_stat;
+ case (F_D_TYPE | DT_LNK):
+ p->fts_info = FTS_SL;
+ goto common_no_stat;
+ case (F_D_TYPE | DT_WHT):
+ case (F_D_TYPESYM | DT_WHT):
+ p->fts_info = FTS_W;
+ goto common_no_stat;
default:
/* No stat necessary */
+ p->fts_info = FTS_NSOK;
+common_no_stat:
p->fts_accpath =
ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
- p->fts_info = FTS_NSOK;
break;
}
}
register FTSENT *p;
/* Free a linked list of structures. */
- while (p = head) {
+ while ((p = head)) {
head = head->fts_link;
free(p);
}
* Allow essentially unlimited paths; find, rm, ls should all work on any tree.
* Most systems will allow creation of paths much longer than MAXPATHLEN, even
* though the kernel won't resolve them. Add the size (not just what's needed)
- * plus 256 bytes so don't realloc the path 2 bytes at a time.
+ * plus 256 bytes so don't realloc the path 2 bytes at a time.
*/
static int
fts_palloc(sp, more)
* already returned.
*/
static void
-fts_padjust(sp, addr)
- FTS *sp;
- void *addr;
+fts_padjust(FTS *sp, void *addr)
{
FTSENT *p;
-#define ADJUST(p) { \
- (p)->fts_accpath = \
- (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
+#define ADJUST(p) do { \
+ if ((p)->fts_accpath != (p)->fts_name) { \
+ (p)->fts_accpath = \
+ (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
+ } \
(p)->fts_path = addr; \
-}
+} while (0)
/* Adjust the current set of children. */
for (p = sp->fts_child; p; p = p->fts_link)
ADJUST(p);
for (max = 0; *argv; ++argv)
if ((len = strlen(*argv)) > max)
max = len;
- return (max);
+ return (max + 1);
}