142273Sbostic /*- 242273Sbostic * Copyright (c) 1990 The Regents of the University of California. 339800Sbostic * All rights reserved. 439800Sbostic * 542273Sbostic * %sccs.include.redist.c% 639800Sbostic */ 739800Sbostic 839800Sbostic #if defined(LIBC_SCCS) && !defined(lint) 9*52061Sbostic static char sccsid[] = "@(#)fts.c 5.20 (Berkeley) 12/23/91"; 1039800Sbostic #endif /* LIBC_SCCS and not lint */ 1139800Sbostic 1239800Sbostic #include <sys/param.h> 1339800Sbostic #include <sys/stat.h> 1442299Sbostic #include <fcntl.h> 1539800Sbostic #include <dirent.h> 1639800Sbostic #include <errno.h> 1747194Sbostic #include "fts.h" 1847194Sbostic #include <stdlib.h> 1942273Sbostic #include <string.h> 2047155Sbostic #include <unistd.h> 2139800Sbostic 22*52061Sbostic static FTSENT *fts_alloc __P((FTS *, char *, int)); 23*52061Sbostic static FTSENT *fts_build __P((FTS *, int)); 24*52061Sbostic static void fts_lfree __P((FTSENT *)); 25*52061Sbostic static void fts_load __P((FTS *, FTSENT *)); 26*52061Sbostic static char *fts_path __P((FTS *, int)); 27*52061Sbostic static FTSENT *fts_sort __P((FTS *, FTSENT *, int)); 28*52061Sbostic static u_short fts_stat __P((FTS *, FTSENT *, int)); 2939800Sbostic 3045600Sbostic #define ISSET(opt) (sp->fts_options & opt) 3145600Sbostic #define SET(opt) (sp->fts_options |= opt) 3239800Sbostic 3345600Sbostic #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) 3445600Sbostic #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) 3539800Sbostic 3642299Sbostic /* fts_build flags */ 3745600Sbostic #define BCHILD 1 /* from fts_children */ 3845600Sbostic #define BREAD 2 /* from fts_read */ 3942299Sbostic 4039800Sbostic FTS * 4145600Sbostic fts_open(argv, options, compar) 4247155Sbostic char * const *argv; 4339800Sbostic register int options; 4447194Sbostic int (*compar)(); 4539800Sbostic { 4639800Sbostic register FTS *sp; 4739800Sbostic register FTSENT *p, *root; 4839800Sbostic register int nitems, maxlen; 4939800Sbostic FTSENT *parent, *tmp; 5047194Sbostic int len; 5139800Sbostic 5245600Sbostic /* Allocate/initialize the stream */ 5345600Sbostic if (!(sp = (FTS *)malloc((u_int)sizeof(FTS)))) 5439800Sbostic return(NULL); 5539800Sbostic bzero(sp, sizeof(FTS)); 5642273Sbostic sp->fts_compar = compar; 5742273Sbostic sp->fts_options = options; 5839800Sbostic 5945600Sbostic /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ 6045600Sbostic if (ISSET(FTS_LOGICAL)) 6145600Sbostic SET(FTS_NOCHDIR); 6245600Sbostic 6345600Sbostic /* Allocate/initialize root's parent. */ 6445600Sbostic if (!(parent = fts_alloc(sp, "", 0))) 6539800Sbostic goto mem1; 6647212Sbostic parent->fts_level = FTS_ROOTPARENTLEVEL; 6739800Sbostic 6845600Sbostic /* Allocate/initialize root(s). */ 6943070Sbostic maxlen = -1; 7043070Sbostic for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) { 7147194Sbostic if (!(len = strlen(*argv))) { 7247194Sbostic errno = ENOENT; 7343070Sbostic goto mem2; 7447194Sbostic } 7547194Sbostic if (maxlen < len) 7647194Sbostic maxlen = len; 7747194Sbostic p = fts_alloc(sp, *argv, len); 7849519Sbostic p->fts_level = FTS_ROOTLEVEL; 7949519Sbostic p->fts_parent = parent; 8043070Sbostic /* 8145600Sbostic * If comparison routine supplied, traverse in sorted 8243070Sbostic * order; otherwise traverse in the order specified. 8343070Sbostic */ 8443070Sbostic if (compar) { 8543070Sbostic p->fts_link = root; 8643070Sbostic root = p; 8743070Sbostic p->fts_accpath = p->fts_name; 8843075Sbostic if (!(options & FTS_NOSTAT)) 8945600Sbostic p->fts_info = fts_stat(sp, p, 0); 9043070Sbostic } else { 9143070Sbostic p->fts_link = NULL; 9243070Sbostic if (!root) 9343070Sbostic tmp = root = p; 9443070Sbostic else { 9543070Sbostic tmp->fts_link = p; 9643070Sbostic tmp = p; 9739800Sbostic } 9839800Sbostic } 9939800Sbostic } 10043070Sbostic if (compar && nitems > 1) 10145600Sbostic root = fts_sort(sp, root, nitems); 10239800Sbostic 10342281Sbostic /* 10445600Sbostic * Allocate a dummy pointer and make fts_read think that we've just 10542281Sbostic * finished the node before the root(s); set p->fts_info to FTS_NS 10642281Sbostic * so that everything about the "current" node is ignored. 10742281Sbostic */ 10845600Sbostic if (!(sp->fts_cur = fts_alloc(sp, "", 0))) 10942281Sbostic goto mem2; 11042281Sbostic sp->fts_cur->fts_link = root; 11142281Sbostic sp->fts_cur->fts_info = FTS_NS; 11242281Sbostic 11345600Sbostic /* Start out with at least 1K+ of path space. */ 11445600Sbostic if (!fts_path(sp, MAX(maxlen, MAXPATHLEN))) 11542281Sbostic goto mem3; 11639800Sbostic 11739800Sbostic /* 11845600Sbostic * If using chdir(2), grab a file descriptor pointing to dot to insure 11942299Sbostic * that we can get back here; this could be avoided for some paths, 12042299Sbostic * but almost certainly not worth the effort. Slashes, symbolic links, 12142299Sbostic * and ".." are all fairly nasty problems. Note, if we can't get the 12242299Sbostic * descriptor we run anyway, just more slowly. 12339800Sbostic */ 12447194Sbostic if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0) 12545600Sbostic SET(FTS_NOCHDIR); 12639800Sbostic 12739800Sbostic return(sp); 12839800Sbostic 12947194Sbostic mem3: free(sp->fts_cur); 13039800Sbostic mem2: fts_lfree(root); 13147194Sbostic free(parent); 13247194Sbostic mem1: free(sp); 13339800Sbostic return(NULL); 13439800Sbostic } 13539800Sbostic 13647404Sbostic static void 13745600Sbostic fts_load(sp, p) 13845600Sbostic FTS *sp; 13939800Sbostic register FTSENT *p; 14039800Sbostic { 14139800Sbostic register int len; 14239800Sbostic register char *cp; 14339800Sbostic 14439800Sbostic /* 14547220Sbostic * Load the stream structure for the next traversal. Since we don't 14647220Sbostic * actually enter the directory until after the preorder visit, set 14747220Sbostic * the fts_accpath field specially so the chdir gets done to the right 14847220Sbostic * place and the user can access the first node. 14939800Sbostic */ 15042273Sbostic len = p->fts_pathlen = p->fts_namelen; 15145600Sbostic bcopy(p->fts_name, sp->fts_path, len + 1); 15242273Sbostic if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { 15339800Sbostic len = strlen(++cp); 15442273Sbostic bcopy(cp, p->fts_name, len + 1); 15542273Sbostic p->fts_namelen = len; 15639800Sbostic } 15745600Sbostic p->fts_accpath = p->fts_path = sp->fts_path; 15847194Sbostic 15947403Sbostic p->fts_info = fts_stat(sp, p, 0); 16047194Sbostic sp->rdev = p->fts_statb.st_dev; 16139800Sbostic } 16239800Sbostic 16345600Sbostic fts_close(sp) 16439800Sbostic FTS *sp; 16539800Sbostic { 16639800Sbostic register FTSENT *freep, *p; 16739800Sbostic int saved_errno; 16839800Sbostic 16942281Sbostic if (sp->fts_cur) { 17042281Sbostic /* 17145600Sbostic * This still works if we haven't read anything -- the dummy 17242281Sbostic * structure points to the root list, so we step through to 17342281Sbostic * the end of the root list which has a valid parent pointer. 17442281Sbostic */ 17547212Sbostic for (p = sp->fts_cur; p->fts_level > FTS_ROOTPARENTLEVEL;) { 17642281Sbostic freep = p; 17742281Sbostic p = p->fts_link ? p->fts_link : p->fts_parent; 17847194Sbostic free(freep); 17939800Sbostic } 18047194Sbostic free(p); 18142281Sbostic } 18239800Sbostic 18345600Sbostic /* Free up child linked list, sort array, path buffer. */ 18442273Sbostic if (sp->fts_child) 18542273Sbostic fts_lfree(sp->fts_child); 18642273Sbostic if (sp->fts_array) 18747194Sbostic free(sp->fts_array); 18847194Sbostic free(sp->fts_path); 18939800Sbostic 19047194Sbostic /* Return to original directory, save errno if necessary. */ 19145600Sbostic if (!ISSET(FTS_NOCHDIR)) { 19247194Sbostic saved_errno = fchdir(sp->fts_rfd) ? errno : 0; 19347194Sbostic (void)close(sp->fts_rfd); 19439800Sbostic } 19539800Sbostic 19645600Sbostic /* Free up the stream pointer. */ 19747194Sbostic free(sp); 19839800Sbostic 19945600Sbostic /* Set errno and return. */ 20045600Sbostic if (!ISSET(FTS_NOCHDIR) && saved_errno) { 20139800Sbostic errno = saved_errno; 20239800Sbostic return(-1); 20339800Sbostic } 20439800Sbostic return(0); 20539800Sbostic } 20639800Sbostic 20747194Sbostic /* 20847194Sbostic * Special case a root of "/" so that slashes aren't appended causing 20947194Sbostic * paths to be written as "//foo". 21047194Sbostic */ 21147194Sbostic #define NAPPEND(p) \ 21247212Sbostic (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \ 21347194Sbostic p->fts_path[0] == '/' ? 0 : p->fts_pathlen) 21447194Sbostic 21539800Sbostic FTSENT * 21645600Sbostic fts_read(sp) 21739800Sbostic register FTS *sp; 21839800Sbostic { 21942299Sbostic register FTSENT *p, *tmp; 22039800Sbostic register int instr; 22147194Sbostic register char *t; 22239800Sbostic 22345600Sbostic /* If finished or unrecoverable error, return NULL. */ 22447194Sbostic if (!sp->fts_cur || ISSET(FTS_STOP)) 22539800Sbostic return(NULL); 22639800Sbostic 22745600Sbostic /* Set current node pointer. */ 22842273Sbostic p = sp->fts_cur; 22939800Sbostic 23045600Sbostic /* Save and zero out user instructions. */ 23142273Sbostic instr = p->fts_instr; 23247194Sbostic p->fts_instr = FTS_NOINSTR; 23339800Sbostic 23445600Sbostic /* If used fts_link pointer for cycle detection, restore it. */ 23542273Sbostic if (sp->fts_savelink) { 23642273Sbostic p->fts_link = sp->fts_savelink; 23742273Sbostic sp->fts_savelink = NULL; 23839800Sbostic } 23939800Sbostic 24047194Sbostic /* Any type of file may be re-visited; re-stat and re-turn. */ 24139800Sbostic if (instr == FTS_AGAIN) { 24245600Sbostic p->fts_info = fts_stat(sp, p, 0); 24339800Sbostic return(p); 24439800Sbostic } 24539800Sbostic 24647194Sbostic /* 24747194Sbostic * Following a symlink -- SLNONE test allows application to see 24847194Sbostic * SLNONE and recover. 24947194Sbostic */ 25047194Sbostic if (instr == FTS_FOLLOW && 25147194Sbostic (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { 25245600Sbostic p->fts_info = fts_stat(sp, p, 1); 25342299Sbostic return(p); 25442299Sbostic } 25542299Sbostic 25645600Sbostic /* Directory in pre-order. */ 25742299Sbostic if (p->fts_info == FTS_D) { 25845600Sbostic /* If skipped or crossed mount point, do post-order visit. */ 25947194Sbostic if (instr == FTS_SKIP || 26047194Sbostic ISSET(FTS_XDEV) && p->fts_statb.st_dev != sp->rdev) { 26142273Sbostic if (sp->fts_child) { 26242273Sbostic fts_lfree(sp->fts_child); 26342273Sbostic sp->fts_child = NULL; 26439800Sbostic } 26542301Sbostic p->fts_info = FTS_DP; 26642301Sbostic return(p); 26742299Sbostic } 26842299Sbostic 26947194Sbostic /* 27047194Sbostic * Cd to the subdirectory, reading it if haven't already. If 27147194Sbostic * the read fails for any reason, or the directory is empty, 27247194Sbostic * the fts_info field of the current node is set by fts_build. 27347220Sbostic * If have already read and now fail to chdir, whack the list 27447220Sbostic * to make the names come out right, and set the parent state 27547220Sbostic * so the application will eventually get an error condition. 27647220Sbostic * If haven't read and fail to chdir, check to see if we're 27747220Sbostic * at the root node -- if so, we have to get back or the root 27847220Sbostic * node may be inaccessible. 27947194Sbostic */ 28047194Sbostic if (sp->fts_child) { 28142299Sbostic if (CHDIR(sp, p->fts_accpath)) { 28247194Sbostic p->fts_parent->fts_cderr = errno; 28347194Sbostic for (p = sp->fts_child; p; p = p->fts_link) 28447194Sbostic p->fts_accpath = 28547194Sbostic p->fts_parent->fts_accpath; 28642299Sbostic } 28747220Sbostic } else if (!(sp->fts_child = fts_build(sp, BREAD))) { 28847220Sbostic if ISSET(FTS_STOP) 28947220Sbostic return(NULL); 29047220Sbostic if (p->fts_level == FTS_ROOTLEVEL && 29147220Sbostic FCHDIR(sp, sp->fts_rfd)) { 29247220Sbostic SET(FTS_STOP); 29347220Sbostic return(NULL); 29447220Sbostic } 29547220Sbostic return(p); 29647220Sbostic } 29747194Sbostic p = sp->fts_child; 29842299Sbostic sp->fts_child = NULL; 29947194Sbostic goto name; 30039800Sbostic } 30139800Sbostic 30245600Sbostic /* Move to next node on this level. */ 30342299Sbostic next: tmp = p; 30442299Sbostic if (p = p->fts_link) { 30547194Sbostic free(tmp); 30647194Sbostic 30747194Sbostic /* If reached the top, load the paths for the next root. */ 30847212Sbostic if (p->fts_level == FTS_ROOTLEVEL) { 30947404Sbostic fts_load(sp, p); 31047194Sbostic return(sp->fts_cur = p); 31147194Sbostic } 31242299Sbostic 31347194Sbostic /* User may have called fts_set on the node. */ 31447194Sbostic if (p->fts_instr == FTS_SKIP) 31547194Sbostic goto next; 31647194Sbostic if (p->fts_instr == FTS_FOLLOW) { 31747194Sbostic p->fts_info = fts_stat(sp, p, 1); 31847194Sbostic p->fts_instr = FTS_NOINSTR; 31947194Sbostic } 32042299Sbostic 32147194Sbostic name: t = sp->fts_path + NAPPEND(p->fts_parent); 32247194Sbostic *t++ = '/'; 32347194Sbostic bcopy(p->fts_name, t, p->fts_namelen + 1); 32442273Sbostic return(sp->fts_cur = p); 32539800Sbostic } 32639800Sbostic 32747220Sbostic /* Move up to the parent node. */ 32842299Sbostic p = tmp->fts_parent; 32947194Sbostic free(tmp); 33042299Sbostic 33147212Sbostic if (p->fts_level == FTS_ROOTPARENTLEVEL) { 33239800Sbostic /* 33345600Sbostic * Done; free everything up and set errno to 0 so the user 33439800Sbostic * can distinguish between error and EOF. 33539800Sbostic */ 33647194Sbostic free(p); 33739800Sbostic errno = 0; 33842273Sbostic return(sp->fts_cur = NULL); 33939800Sbostic } 34039800Sbostic 34142273Sbostic sp->fts_path[p->fts_pathlen] = '\0'; 34247194Sbostic 34347194Sbostic /* 34447220Sbostic * Cd back up to the parent directory. If at a root node, have to cd 34547220Sbostic * back to the original place, otherwise may not be able to access the 34647220Sbostic * original node on post-order. 34747194Sbostic */ 34847220Sbostic if (p->fts_level == FTS_ROOTLEVEL) { 34947220Sbostic if (FCHDIR(sp, sp->fts_rfd)) { 35047220Sbostic SET(FTS_STOP); 35147220Sbostic return(NULL); 35247220Sbostic } 35347220Sbostic } 35447220Sbostic else if (CHDIR(sp, "..")) { 35547220Sbostic SET(FTS_STOP); 35647220Sbostic return(NULL); 35747220Sbostic } 35847220Sbostic 35947220Sbostic /* 36047220Sbostic * If had a chdir error when trying to get into the directory, set the 36147220Sbostic * info field to reflect this, and restore errno. The error indicator 36247220Sbostic * has to be reset to 0 so that if the user does an FTS_AGAIN, it all 36347220Sbostic * works. 36447220Sbostic */ 36547194Sbostic if (p->fts_cderr) { 36647194Sbostic errno = p->fts_cderr; 36747194Sbostic p->fts_cderr = 0; 36842273Sbostic p->fts_info = FTS_ERR; 36947220Sbostic } else 37042273Sbostic p->fts_info = FTS_DP; 37142273Sbostic return(sp->fts_cur = p); 37239800Sbostic } 37339800Sbostic 37439800Sbostic /* 37547220Sbostic * Fts_set takes the stream as an argument although it's not used in this 37639800Sbostic * implementation; it would be necessary if anyone wanted to add global 37745600Sbostic * semantics to fts using fts_set. An error return is allowed for similar 37845600Sbostic * reasons. 37939800Sbostic */ 38039800Sbostic /* ARGSUSED */ 38145600Sbostic fts_set(sp, p, instr) 38239800Sbostic FTS *sp; 38339800Sbostic FTSENT *p; 38439800Sbostic int instr; 38539800Sbostic { 38642273Sbostic p->fts_instr = instr; 38739800Sbostic return(0); 38839800Sbostic } 38939800Sbostic 39039800Sbostic FTSENT * 39145600Sbostic fts_children(sp) 39239800Sbostic register FTS *sp; 39339800Sbostic { 39442299Sbostic register FTSENT *p; 39542299Sbostic int fd; 39642299Sbostic 39745600Sbostic /* Set current node pointer. */ 39845600Sbostic p = sp->fts_cur; 39945600Sbostic 40039800Sbostic /* 40145600Sbostic * Set errno to 0 so that user can tell the difference between an 40247194Sbostic * error and a directory without entries. If not a directory being 40347194Sbostic * visited in *pre-order*, or we've already had fatal errors, return 40447194Sbostic * immediately. 40539800Sbostic */ 40639800Sbostic errno = 0; 40747194Sbostic if (ISSET(FTS_STOP) || p->fts_info != FTS_D && p->fts_info != FTS_DNR) 40839800Sbostic return(NULL); 40945600Sbostic 41045600Sbostic /* Free up any previous child list. */ 41142273Sbostic if (sp->fts_child) 41242273Sbostic fts_lfree(sp->fts_child); 41342299Sbostic 41442299Sbostic /* 41545600Sbostic * If using chdir on a relative path and called BEFORE fts_read does 41647194Sbostic * its chdir to the root of a traversal, we can lose -- we need to 41747194Sbostic * chdir into the subdirectory, and we don't know where the current 41847194Sbostic * directory is, so we can't get back so that the upcoming chdir by 41947194Sbostic * fts_read will work. 42042299Sbostic */ 42147212Sbostic if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || 42245600Sbostic ISSET(FTS_NOCHDIR)) 42342299Sbostic return(sp->fts_child = fts_build(sp, BCHILD)); 42442299Sbostic 42542299Sbostic if ((fd = open(".", O_RDONLY, 0)) < 0) 42642299Sbostic return(NULL); 42742299Sbostic sp->fts_child = fts_build(sp, BCHILD); 42842299Sbostic if (fchdir(fd)) 42942299Sbostic return(NULL); 43042299Sbostic (void)close(fd); 43142299Sbostic return(sp->fts_child); 43239800Sbostic } 43339800Sbostic 43447194Sbostic /* 43547194Sbostic * This is the tricky part -- do not casually change *anything* in here. The 43647194Sbostic * idea is to build the linked list of entries that are used by fts_children 43747194Sbostic * and fts_read. There are lots of special cases. 43847194Sbostic * 43947194Sbostic * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is 44047194Sbostic * set and it's a physical walk (so that symbolic links can't be directories), 44147194Sbostic * we assume that the number of subdirectories in a node is equal to the number 44247194Sbostic * of links to the parent. This allows stat calls to be skipped in any leaf 44347194Sbostic * directories and for any nodes after the directories in the parent node have 44447194Sbostic * been found. This empirically cuts the stat calls by about 2/3. 44547194Sbostic */ 44639800Sbostic #define ISDOT(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2])) 44739800Sbostic 44847155Sbostic static FTSENT * 44942299Sbostic fts_build(sp, type) 45039800Sbostic register FTS *sp; 45142299Sbostic int type; 45239800Sbostic { 45339800Sbostic register struct dirent *dp; 45439800Sbostic register FTSENT *p, *head; 45539800Sbostic register int nitems; 45647194Sbostic FTSENT *cur; 45739800Sbostic DIR *dirp; 45847194Sbostic int cderr, descend, len, level, maxlen, nlinks, saved_errno; 45939800Sbostic char *cp; 46039800Sbostic 46145600Sbostic /* Set current node pointer. */ 46247194Sbostic cur = sp->fts_cur; 46345600Sbostic 46447194Sbostic /* 46547194Sbostic * Open the directory for reading. If this fails, we're done. 46647194Sbostic * If being called from fts_read, set the fts_info field. 46747194Sbostic */ 46847194Sbostic if (!(dirp = opendir(cur->fts_accpath))) { 46947194Sbostic if (type == BREAD) 47047194Sbostic cur->fts_info = FTS_DNR; 47139800Sbostic return(NULL); 47239800Sbostic } 47339800Sbostic 47439800Sbostic /* 47547194Sbostic * Nlinks is the number of possible entries of type directory in the 47647194Sbostic * directory if we're cheating on stat calls, 0 if we're not doing 47747194Sbostic * any stat calls at all, -1 if we're doing stats on everything. 47839800Sbostic */ 47942273Sbostic nlinks = 48045600Sbostic ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ? 48147194Sbostic cur->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1; 48239800Sbostic 48347194Sbostic /* 48447194Sbostic * If we're going to need to stat anything or we want to descend 48547194Sbostic * and stay in the directory, chdir. If this fails we keep going. 48647194Sbostic * We won't be able to stat anything, but we can still return the 48747194Sbostic * names themselves. Note, that since fts_read won't be able to 48847194Sbostic * chdir into the directory, it will have to return different path 48947194Sbostic * names than before, i.e. "a/b" instead of "b". Since the node 49047194Sbostic * has already been visited in pre-order, have to wait until the 49147194Sbostic * post-order visit to return the error. This is all fairly nasty. 49247194Sbostic * If a program needed sorted entries or stat information, they had 49347194Sbostic * better be checking FTS_NS on the returned nodes. 49447194Sbostic */ 49545600Sbostic if (nlinks || type == BREAD) 49647194Sbostic if (FCHDIR(sp, dirfd(dirp))) { 49747194Sbostic if (type == BREAD) 49847194Sbostic cur->fts_cderr = errno; 49947194Sbostic descend = nlinks = 0; 50047194Sbostic cderr = 1; 50147194Sbostic } else { 50245600Sbostic descend = 1; 50347194Sbostic cderr = 0; 50439962Sbostic } 50547194Sbostic else 50647194Sbostic descend = 0; 50739962Sbostic 50847194Sbostic /* 50947194Sbostic * Figure out the max file name length that can be stored in the 51047194Sbostic * current path -- the inner loop allocates more path as necessary. 51147194Sbostic * We really wouldn't have to do the maxlen calculations here, we 51247194Sbostic * could do them in fts_read before returning the path, but it's a 51347194Sbostic * lot easier here since the length is part of the dirent structure. 51447194Sbostic * 51547194Sbostic * If not changing directories set a pointer so that we can just 51647194Sbostic * append each new name into the path. 51747194Sbostic */ 51847194Sbostic maxlen = sp->fts_pathlen - cur->fts_pathlen - 1; 51947194Sbostic len = NAPPEND(cur); 52047194Sbostic if (ISSET(FTS_NOCHDIR)) { 52147194Sbostic cp = sp->fts_path + len; 52247194Sbostic *cp++ = '/'; 52347194Sbostic } 52440939Sbostic 52547194Sbostic level = cur->fts_level + 1; 52640939Sbostic 52747194Sbostic /* Read the directory, attaching each entry to the `link' pointer. */ 52839800Sbostic for (head = NULL, nitems = 0; dp = readdir(dirp);) { 52947194Sbostic if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) 53039800Sbostic continue; 53139800Sbostic 53247194Sbostic if (!(p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen))) 53339800Sbostic goto mem1; 53439800Sbostic if (dp->d_namlen > maxlen) { 53545600Sbostic if (!fts_path(sp, (int)dp->d_namlen)) { 53647194Sbostic /* 53747194Sbostic * No more memory for path or structures. Save 53847194Sbostic * errno, free up the current structure and the 53947194Sbostic * structures already allocated. 54047194Sbostic */ 54147194Sbostic mem1: saved_errno = errno; 54247194Sbostic if (p) 54347194Sbostic free(p); 54447194Sbostic fts_lfree(head); 54547194Sbostic (void)closedir(dirp); 54639800Sbostic errno = saved_errno; 54747194Sbostic cur->fts_info = FTS_ERR; 54847194Sbostic SET(FTS_STOP); 54939800Sbostic return(NULL); 55039800Sbostic } 55142273Sbostic maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1; 55239800Sbostic } 55339800Sbostic 55442273Sbostic p->fts_pathlen = len + dp->d_namlen + 1; 55542273Sbostic p->fts_parent = sp->fts_cur; 55642273Sbostic p->fts_level = level; 55739800Sbostic 55839800Sbostic if (nlinks) { 55947194Sbostic /* Build a file name for fts_stat to stat. */ 56047194Sbostic if (ISSET(FTS_NOCHDIR)) { 56147194Sbostic p->fts_accpath = p->fts_path; 56242273Sbostic bcopy(p->fts_name, cp, p->fts_namelen + 1); 56347194Sbostic } else 56447194Sbostic p->fts_accpath = p->fts_name; 56545600Sbostic p->fts_info = fts_stat(sp, p, 0); 56647194Sbostic if (nlinks > 0 && p->fts_info == FTS_D) 56739800Sbostic --nlinks; 56847194Sbostic } else if (cderr) { 56947194Sbostic p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS; 57047194Sbostic p->fts_accpath = cur->fts_accpath; 57147194Sbostic } else { 57247194Sbostic p->fts_accpath = 57347194Sbostic ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; 57447194Sbostic p->fts_info = FTS_NSOK; 57547194Sbostic } 57639800Sbostic 57742273Sbostic p->fts_link = head; 57839800Sbostic head = p; 57939800Sbostic ++nitems; 58039800Sbostic } 58139800Sbostic (void)closedir(dirp); 58239800Sbostic 58347194Sbostic /* 58447194Sbostic * If not changing directories, reset the path back to original 58547194Sbostic * state. 58647194Sbostic */ 58747194Sbostic if (ISSET(FTS_NOCHDIR)) { 58847194Sbostic if (cp - 1 > sp->fts_path) 58947194Sbostic --cp; 59047194Sbostic *cp = '\0'; 59147194Sbostic } 59242299Sbostic 59342299Sbostic /* 59447194Sbostic * If descended after called from fts_children or called from 59547194Sbostic * fts_read and didn't find anything, get back. If can't get 59647194Sbostic * back, we're done. 59742299Sbostic */ 59842299Sbostic if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) { 59947194Sbostic cur->fts_info = FTS_ERR; 60047194Sbostic SET(FTS_STOP); 60139962Sbostic return(NULL); 60239962Sbostic } 60339962Sbostic 60447194Sbostic /* If we didn't find anything, just do the post-order visit */ 60539800Sbostic if (!nitems) { 60642299Sbostic if (type == BREAD) 60747194Sbostic cur->fts_info = FTS_DP; 60839800Sbostic return(NULL); 60939800Sbostic } 61039800Sbostic 61147194Sbostic /* Sort the entries. */ 61242273Sbostic if (sp->fts_compar && nitems > 1) 61345600Sbostic head = fts_sort(sp, head, nitems); 61439800Sbostic return(head); 61539800Sbostic } 61639800Sbostic 61745600Sbostic static u_short 61845600Sbostic fts_stat(sp, p, follow) 61945600Sbostic FTS *sp; 62039800Sbostic register FTSENT *p; 62145600Sbostic int follow; 62239800Sbostic { 62347194Sbostic int saved_errno; 62447194Sbostic 62539800Sbostic /* 62645600Sbostic * If doing a logical walk, or application requested FTS_FOLLOW, do 62747194Sbostic * a stat(2). If that fails, check for a non-existent symlink. If 62847194Sbostic * fail, return the errno from the stat call. 62939800Sbostic */ 63045600Sbostic if (ISSET(FTS_LOGICAL) || follow) { 63145600Sbostic if (stat(p->fts_accpath, &p->fts_statb)) { 63247194Sbostic saved_errno = errno; 63347194Sbostic if (!lstat(p->fts_accpath, &p->fts_statb)) { 63447194Sbostic errno = 0; 63545600Sbostic return(FTS_SLNONE); 63647194Sbostic } 63747194Sbostic errno = saved_errno; 63847194Sbostic bzero(&p->fts_statb, sizeof(struct stat)); 63947194Sbostic return(FTS_NS); 64045600Sbostic } 64145600Sbostic } else if (lstat(p->fts_accpath, &p->fts_statb)) { 64247194Sbostic bzero(&p->fts_statb, sizeof(struct stat)); 64345600Sbostic return(FTS_NS); 64445600Sbostic } 64539800Sbostic 64647194Sbostic /* 64747194Sbostic * Cycle detection is done as soon as we find a directory. Detection 64847194Sbostic * is by brute force; if the tree gets deep enough or the number of 64947194Sbostic * symbolic links to directories high enough something faster might 65047194Sbostic * be worthwhile. 65147194Sbostic */ 65247194Sbostic if (S_ISDIR(p->fts_statb.st_mode)) { 65347194Sbostic register FTSENT *t; 65447194Sbostic register dev_t dev; 65547194Sbostic register ino_t ino; 65647194Sbostic 65747194Sbostic dev = p->fts_statb.st_dev; 65847194Sbostic ino = p->fts_statb.st_ino; 65947212Sbostic for (t = p->fts_parent; t->fts_level > FTS_ROOTLEVEL; 66047194Sbostic t = t->fts_parent) 66147194Sbostic if (ino == t->fts_statb.st_ino && 66247194Sbostic dev == t->fts_statb.st_dev) { 66347194Sbostic sp->fts_savelink = p->fts_link; 66447194Sbostic p->fts_link = t; 66547194Sbostic return(FTS_DC); 66647194Sbostic } 66739800Sbostic return(FTS_D); 66847194Sbostic } 66945600Sbostic if (S_ISLNK(p->fts_statb.st_mode)) 67039800Sbostic return(FTS_SL); 67145600Sbostic if (S_ISREG(p->fts_statb.st_mode)) 67239800Sbostic return(FTS_F); 67340939Sbostic return(FTS_DEFAULT); 67439800Sbostic } 67539800Sbostic 67639800Sbostic #define R(type, nelem, ptr) \ 67742299Sbostic (type *)realloc((void *)ptr, (u_int)((nelem) * sizeof(type))) 67839800Sbostic 67939800Sbostic static FTSENT * 68045600Sbostic fts_sort(sp, head, nitems) 68145600Sbostic FTS *sp; 68239800Sbostic FTSENT *head; 68339800Sbostic register int nitems; 68439800Sbostic { 68539800Sbostic register FTSENT **ap, *p; 68639800Sbostic 68739800Sbostic /* 68845600Sbostic * Construct an array of pointers to the structures and call qsort(3). 68939800Sbostic * Reassemble the array in the order returned by qsort. If unable to 69039800Sbostic * sort for memory reasons, return the directory entries in their 69139800Sbostic * current order. Allocate enough space for the current needs plus 69239800Sbostic * 40 so we don't realloc one entry at a time. 69339800Sbostic */ 69445600Sbostic if (nitems > sp->fts_nitems) { 69545600Sbostic sp->fts_nitems = nitems + 40; 69645600Sbostic if (!(sp->fts_array = 69745600Sbostic R(FTSENT *, sp->fts_nitems, sp->fts_array))) { 69845600Sbostic sp->fts_nitems = 0; 69939800Sbostic return(head); 70039800Sbostic } 70139800Sbostic } 70245600Sbostic for (ap = sp->fts_array, p = head; p; p = p->fts_link) 70339800Sbostic *ap++ = p; 70445600Sbostic qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar); 70545600Sbostic for (head = *(ap = sp->fts_array); --nitems; ++ap) 70642273Sbostic ap[0]->fts_link = ap[1]; 70742273Sbostic ap[0]->fts_link = NULL; 70839800Sbostic return(head); 70939800Sbostic } 71039800Sbostic 71139800Sbostic static FTSENT * 71245600Sbostic fts_alloc(sp, name, len) 71345600Sbostic FTS *sp; 71439800Sbostic char *name; 71539800Sbostic register int len; 71639800Sbostic { 71739800Sbostic register FTSENT *p; 71839800Sbostic 71939800Sbostic /* 72045600Sbostic * Variable sized structures; the name is the last element so 72147194Sbostic * we allocate enough extra space after the structure to store 72247194Sbostic * it. 72339800Sbostic */ 72442299Sbostic if (!(p = (FTSENT *)malloc((size_t)(sizeof(FTSENT) + len)))) 72539800Sbostic return(NULL); 72642273Sbostic bcopy(name, p->fts_name, len + 1); 72742273Sbostic p->fts_namelen = len; 72845600Sbostic p->fts_path = sp->fts_path; 72947194Sbostic p->fts_instr = FTS_NOINSTR; 73047194Sbostic p->fts_cderr = 0; 73145600Sbostic p->fts_number = 0; 73245600Sbostic p->fts_pointer = NULL; 73339800Sbostic return(p); 73439800Sbostic } 73539800Sbostic 73645600Sbostic static void 73739800Sbostic fts_lfree(head) 73839800Sbostic register FTSENT *head; 73939800Sbostic { 74039800Sbostic register FTSENT *p; 74139800Sbostic 74245600Sbostic /* Free a linked list of structures. */ 74339800Sbostic while (p = head) { 74442273Sbostic head = head->fts_link; 74547194Sbostic free(p); 74639800Sbostic } 74739800Sbostic } 74839800Sbostic 74939800Sbostic /* 75045600Sbostic * Allow essentially unlimited paths; certain programs (find, rm, ls) need to 75145600Sbostic * work on any tree. Most systems will allow creation of paths much longer 75245600Sbostic * than MAXPATHLEN, even though the kernel won't resolve them. Add an extra 75345600Sbostic * 128 bytes to the requested size so that we don't realloc the path 2 bytes 75445600Sbostic * at a time. 75539800Sbostic */ 75642299Sbostic static char * 75745600Sbostic fts_path(sp, size) 75845600Sbostic FTS *sp; 75939800Sbostic int size; 76039800Sbostic { 76145600Sbostic sp->fts_pathlen += size + 128; 76247155Sbostic return(sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path)); 76347155Sbostic } 764