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*52209Sbostic static char sccsid[] = "@(#)fts.c 5.26 (Berkeley) 01/15/92"; 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> 17*52209Sbostic #include <fts.h> 1847194Sbostic #include <stdlib.h> 1942273Sbostic #include <string.h> 2047155Sbostic #include <unistd.h> 2139800Sbostic 2252061Sbostic static FTSENT *fts_alloc __P((FTS *, char *, int)); 2352061Sbostic static FTSENT *fts_build __P((FTS *, int)); 2452061Sbostic static void fts_lfree __P((FTSENT *)); 2552061Sbostic static void fts_load __P((FTS *, FTSENT *)); 26*52209Sbostic static void fts_padjust __P((FTS *, void *)); 27*52209Sbostic static int fts_palloc __P((FTS *, int)); 2852061Sbostic static FTSENT *fts_sort __P((FTS *, FTSENT *, int)); 2952061Sbostic static u_short fts_stat __P((FTS *, FTSENT *, int)); 3039800Sbostic 31*52209Sbostic #define ISDOT(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2])) 32*52209Sbostic 3345600Sbostic #define ISSET(opt) (sp->fts_options & opt) 3445600Sbostic #define SET(opt) (sp->fts_options |= opt) 3539800Sbostic 3645600Sbostic #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) 3745600Sbostic #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) 3839800Sbostic 3942299Sbostic /* fts_build flags */ 4045600Sbostic #define BCHILD 1 /* from fts_children */ 4145600Sbostic #define BREAD 2 /* from fts_read */ 4242299Sbostic 4339800Sbostic FTS * 4445600Sbostic fts_open(argv, options, compar) 4547155Sbostic char * const *argv; 4639800Sbostic register int options; 4747194Sbostic int (*compar)(); 4839800Sbostic { 4939800Sbostic register FTS *sp; 5039800Sbostic register FTSENT *p, *root; 5139800Sbostic register int nitems, maxlen; 5239800Sbostic FTSENT *parent, *tmp; 5347194Sbostic int len; 5439800Sbostic 5545600Sbostic /* Allocate/initialize the stream */ 5652150Sbostic if ((sp = malloc((u_int)sizeof(FTS))) == NULL) 5752150Sbostic return (NULL); 5839800Sbostic bzero(sp, sizeof(FTS)); 5942273Sbostic sp->fts_compar = compar; 6042273Sbostic sp->fts_options = options; 6139800Sbostic 6245600Sbostic /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ 6345600Sbostic if (ISSET(FTS_LOGICAL)) 6445600Sbostic SET(FTS_NOCHDIR); 6545600Sbostic 6645600Sbostic /* Allocate/initialize root's parent. */ 6752155Sbostic if ((parent = fts_alloc(sp, "", 0)) == NULL) 6839800Sbostic goto mem1; 6947212Sbostic parent->fts_level = FTS_ROOTPARENTLEVEL; 7039800Sbostic 7145600Sbostic /* Allocate/initialize root(s). */ 7252160Sbostic for (root = NULL, maxlen = nitems = 0; *argv; ++argv, ++nitems) { 7352160Sbostic /* Don't allow zero-length paths. */ 74*52209Sbostic if ((len = strlen(*argv)) == 0) { 7547194Sbostic errno = ENOENT; 7643070Sbostic goto mem2; 7747194Sbostic } 7847194Sbostic if (maxlen < len) 7947194Sbostic maxlen = len; 80*52209Sbostic 8147194Sbostic p = fts_alloc(sp, *argv, len); 8249519Sbostic p->fts_level = FTS_ROOTLEVEL; 8349519Sbostic p->fts_parent = parent; 84*52209Sbostic p->fts_info = fts_stat(sp, p, 0); 85*52209Sbostic 86*52209Sbostic /* Command-line "." and ".." are real directories. */ 87*52209Sbostic p->fts_accpath = p->fts_name; 88*52209Sbostic if (p->fts_info == FTS_DOT) 89*52209Sbostic p->fts_info = FTS_D; 90*52209Sbostic 9143070Sbostic /* 9245600Sbostic * If comparison routine supplied, traverse in sorted 9343070Sbostic * order; otherwise traverse in the order specified. 9443070Sbostic */ 9543070Sbostic if (compar) { 9643070Sbostic p->fts_link = root; 9743070Sbostic root = p; 9843070Sbostic } else { 9943070Sbostic p->fts_link = NULL; 10052155Sbostic if (root == NULL) 10143070Sbostic tmp = root = p; 10243070Sbostic else { 10343070Sbostic tmp->fts_link = p; 10443070Sbostic tmp = p; 10539800Sbostic } 10639800Sbostic } 10739800Sbostic } 10843070Sbostic if (compar && nitems > 1) 10945600Sbostic root = fts_sort(sp, root, nitems); 11039800Sbostic 11142281Sbostic /* 11245600Sbostic * Allocate a dummy pointer and make fts_read think that we've just 11352160Sbostic * finished the node before the root(s); set p->fts_info to FTS_INIT 11442281Sbostic * so that everything about the "current" node is ignored. 11542281Sbostic */ 11652155Sbostic if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) 11742281Sbostic goto mem2; 11842281Sbostic sp->fts_cur->fts_link = root; 11952160Sbostic sp->fts_cur->fts_info = FTS_INIT; 12042281Sbostic 12152160Sbostic /* 122*52209Sbostic * Start out with more than 1K of path space, and enough, in any 12352160Sbostic * case, to hold the user's paths. 12452160Sbostic */ 125*52209Sbostic if (fts_palloc(sp, MAX(maxlen, MAXPATHLEN))) 12642281Sbostic goto mem3; 12739800Sbostic 12839800Sbostic /* 12945600Sbostic * If using chdir(2), grab a file descriptor pointing to dot to insure 13042299Sbostic * that we can get back here; this could be avoided for some paths, 13142299Sbostic * but almost certainly not worth the effort. Slashes, symbolic links, 13242299Sbostic * and ".." are all fairly nasty problems. Note, if we can't get the 13342299Sbostic * descriptor we run anyway, just more slowly. 13439800Sbostic */ 13547194Sbostic if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0) 13645600Sbostic SET(FTS_NOCHDIR); 13739800Sbostic 13852150Sbostic return (sp); 13939800Sbostic 14047194Sbostic mem3: free(sp->fts_cur); 14139800Sbostic mem2: fts_lfree(root); 14247194Sbostic free(parent); 14347194Sbostic mem1: free(sp); 14452150Sbostic return (NULL); 14539800Sbostic } 14639800Sbostic 14747404Sbostic static void 14845600Sbostic fts_load(sp, p) 14945600Sbostic FTS *sp; 15039800Sbostic register FTSENT *p; 15139800Sbostic { 15239800Sbostic register int len; 15339800Sbostic register char *cp; 15439800Sbostic 15539800Sbostic /* 15647220Sbostic * Load the stream structure for the next traversal. Since we don't 15747220Sbostic * actually enter the directory until after the preorder visit, set 15847220Sbostic * the fts_accpath field specially so the chdir gets done to the right 159*52209Sbostic * place and the user can access the first node. From fts_open it's 160*52209Sbostic * known that the path will fit. 16139800Sbostic */ 16242273Sbostic len = p->fts_pathlen = p->fts_namelen; 16345600Sbostic bcopy(p->fts_name, sp->fts_path, len + 1); 16442273Sbostic if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { 16539800Sbostic len = strlen(++cp); 16642273Sbostic bcopy(cp, p->fts_name, len + 1); 16742273Sbostic p->fts_namelen = len; 16839800Sbostic } 16945600Sbostic p->fts_accpath = p->fts_path = sp->fts_path; 170*52209Sbostic sp->fts_dev = p->fts_dev; 17139800Sbostic } 17239800Sbostic 173*52209Sbostic int 17445600Sbostic fts_close(sp) 17539800Sbostic FTS *sp; 17639800Sbostic { 17739800Sbostic register FTSENT *freep, *p; 17839800Sbostic int saved_errno; 17939800Sbostic 180*52209Sbostic /* 181*52209Sbostic * This still works if we haven't read anything -- the dummy structure 182*52209Sbostic * points to the root list, so we step through to the end of the root 183*52209Sbostic * list which has a valid parent pointer. 184*52209Sbostic */ 18542281Sbostic if (sp->fts_cur) { 186*52209Sbostic for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { 18742281Sbostic freep = p; 18842281Sbostic p = p->fts_link ? p->fts_link : p->fts_parent; 18947194Sbostic free(freep); 19039800Sbostic } 19147194Sbostic free(p); 19242281Sbostic } 19339800Sbostic 19445600Sbostic /* Free up child linked list, sort array, path buffer. */ 19542273Sbostic if (sp->fts_child) 19642273Sbostic fts_lfree(sp->fts_child); 19742273Sbostic if (sp->fts_array) 19847194Sbostic free(sp->fts_array); 19947194Sbostic free(sp->fts_path); 20039800Sbostic 20147194Sbostic /* Return to original directory, save errno if necessary. */ 20245600Sbostic if (!ISSET(FTS_NOCHDIR)) { 20347194Sbostic saved_errno = fchdir(sp->fts_rfd) ? errno : 0; 20447194Sbostic (void)close(sp->fts_rfd); 20539800Sbostic } 20639800Sbostic 20745600Sbostic /* Free up the stream pointer. */ 20847194Sbostic free(sp); 20939800Sbostic 21045600Sbostic /* Set errno and return. */ 21145600Sbostic if (!ISSET(FTS_NOCHDIR) && saved_errno) { 21239800Sbostic errno = saved_errno; 21352150Sbostic return (-1); 21439800Sbostic } 21552150Sbostic return (0); 21639800Sbostic } 21739800Sbostic 21847194Sbostic /* 21952160Sbostic * Special case a root of "/" so that slashes aren't appended which would 22052160Sbostic * cause paths to be written as "//foo". 22147194Sbostic */ 22247194Sbostic #define NAPPEND(p) \ 22347212Sbostic (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \ 22447194Sbostic p->fts_path[0] == '/' ? 0 : p->fts_pathlen) 22547194Sbostic 22639800Sbostic FTSENT * 22745600Sbostic fts_read(sp) 22839800Sbostic register FTS *sp; 22939800Sbostic { 23042299Sbostic register FTSENT *p, *tmp; 23139800Sbostic register int instr; 23247194Sbostic register char *t; 23339800Sbostic 23445600Sbostic /* If finished or unrecoverable error, return NULL. */ 23552155Sbostic if (sp->fts_cur == NULL || ISSET(FTS_STOP)) 23652150Sbostic return (NULL); 23739800Sbostic 23845600Sbostic /* Set current node pointer. */ 23942273Sbostic p = sp->fts_cur; 24039800Sbostic 24145600Sbostic /* Save and zero out user instructions. */ 24242273Sbostic instr = p->fts_instr; 24347194Sbostic p->fts_instr = FTS_NOINSTR; 24439800Sbostic 24547194Sbostic /* Any type of file may be re-visited; re-stat and re-turn. */ 24639800Sbostic if (instr == FTS_AGAIN) { 24745600Sbostic p->fts_info = fts_stat(sp, p, 0); 24852150Sbostic return (p); 24939800Sbostic } 25039800Sbostic 25147194Sbostic /* 25247194Sbostic * Following a symlink -- SLNONE test allows application to see 25347194Sbostic * SLNONE and recover. 25447194Sbostic */ 25547194Sbostic if (instr == FTS_FOLLOW && 25647194Sbostic (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { 25745600Sbostic p->fts_info = fts_stat(sp, p, 1); 25852150Sbostic return (p); 25942299Sbostic } 26042299Sbostic 26145600Sbostic /* Directory in pre-order. */ 26242299Sbostic if (p->fts_info == FTS_D) { 26345600Sbostic /* If skipped or crossed mount point, do post-order visit. */ 26447194Sbostic if (instr == FTS_SKIP || 265*52209Sbostic ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev) { 26642273Sbostic if (sp->fts_child) { 26742273Sbostic fts_lfree(sp->fts_child); 26842273Sbostic sp->fts_child = NULL; 26939800Sbostic } 27042301Sbostic p->fts_info = FTS_DP; 27152150Sbostic return (p); 27242299Sbostic } 27342299Sbostic 27447194Sbostic /* 27547194Sbostic * Cd to the subdirectory, reading it if haven't already. If 27647194Sbostic * the read fails for any reason, or the directory is empty, 27747194Sbostic * the fts_info field of the current node is set by fts_build. 27847220Sbostic * If have already read and now fail to chdir, whack the list 27947220Sbostic * to make the names come out right, and set the parent state 28047220Sbostic * so the application will eventually get an error condition. 28147220Sbostic * If haven't read and fail to chdir, check to see if we're 28247220Sbostic * at the root node -- if so, we have to get back or the root 28347220Sbostic * node may be inaccessible. 28447194Sbostic */ 28547194Sbostic if (sp->fts_child) { 28642299Sbostic if (CHDIR(sp, p->fts_accpath)) { 28752160Sbostic p->fts_parent->fts_errno = errno; 28847194Sbostic for (p = sp->fts_child; p; p = p->fts_link) 28947194Sbostic p->fts_accpath = 29047194Sbostic p->fts_parent->fts_accpath; 29142299Sbostic } 29252155Sbostic } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { 29347220Sbostic if ISSET(FTS_STOP) 29452150Sbostic return (NULL); 29547220Sbostic if (p->fts_level == FTS_ROOTLEVEL && 29647220Sbostic FCHDIR(sp, sp->fts_rfd)) { 29747220Sbostic SET(FTS_STOP); 29852150Sbostic return (NULL); 29947220Sbostic } 30052150Sbostic return (p); 30147220Sbostic } 30247194Sbostic p = sp->fts_child; 30342299Sbostic sp->fts_child = NULL; 30447194Sbostic goto name; 30539800Sbostic } 30639800Sbostic 30745600Sbostic /* Move to next node on this level. */ 30842299Sbostic next: tmp = p; 30942299Sbostic if (p = p->fts_link) { 31047194Sbostic free(tmp); 31147194Sbostic 31247194Sbostic /* If reached the top, load the paths for the next root. */ 31347212Sbostic if (p->fts_level == FTS_ROOTLEVEL) { 31447404Sbostic fts_load(sp, p); 31552150Sbostic return (sp->fts_cur = p); 31647194Sbostic } 31742299Sbostic 31847194Sbostic /* User may have called fts_set on the node. */ 31947194Sbostic if (p->fts_instr == FTS_SKIP) 32047194Sbostic goto next; 32147194Sbostic if (p->fts_instr == FTS_FOLLOW) { 32247194Sbostic p->fts_info = fts_stat(sp, p, 1); 32347194Sbostic p->fts_instr = FTS_NOINSTR; 32447194Sbostic } 32542299Sbostic 32647194Sbostic name: t = sp->fts_path + NAPPEND(p->fts_parent); 32747194Sbostic *t++ = '/'; 32847194Sbostic bcopy(p->fts_name, t, p->fts_namelen + 1); 32952150Sbostic return (sp->fts_cur = p); 33039800Sbostic } 33139800Sbostic 33247220Sbostic /* Move up to the parent node. */ 33342299Sbostic p = tmp->fts_parent; 33447194Sbostic free(tmp); 33542299Sbostic 33647212Sbostic if (p->fts_level == FTS_ROOTPARENTLEVEL) { 33739800Sbostic /* 33845600Sbostic * Done; free everything up and set errno to 0 so the user 33939800Sbostic * can distinguish between error and EOF. 34039800Sbostic */ 34147194Sbostic free(p); 34239800Sbostic errno = 0; 34352150Sbostic return (sp->fts_cur = NULL); 34439800Sbostic } 34539800Sbostic 34642273Sbostic sp->fts_path[p->fts_pathlen] = '\0'; 34747194Sbostic 34847194Sbostic /* 34947220Sbostic * Cd back up to the parent directory. If at a root node, have to cd 35047220Sbostic * back to the original place, otherwise may not be able to access the 35147220Sbostic * original node on post-order. 35247194Sbostic */ 35347220Sbostic if (p->fts_level == FTS_ROOTLEVEL) { 35447220Sbostic if (FCHDIR(sp, sp->fts_rfd)) { 35547220Sbostic SET(FTS_STOP); 35652150Sbostic return (NULL); 35747220Sbostic } 35847220Sbostic } 35947220Sbostic else if (CHDIR(sp, "..")) { 36047220Sbostic SET(FTS_STOP); 36152150Sbostic return (NULL); 36247220Sbostic } 36347220Sbostic 36447220Sbostic /* 36547220Sbostic * If had a chdir error when trying to get into the directory, set the 36647220Sbostic * info field to reflect this, and restore errno. The error indicator 36747220Sbostic * has to be reset to 0 so that if the user does an FTS_AGAIN, it all 36847220Sbostic * works. 36947220Sbostic */ 37052160Sbostic if (p->fts_errno) { 37152160Sbostic errno = p->fts_errno; 37252160Sbostic p->fts_errno = 0; 37342273Sbostic p->fts_info = FTS_ERR; 37447220Sbostic } else 37542273Sbostic p->fts_info = FTS_DP; 37652150Sbostic return (sp->fts_cur = p); 37739800Sbostic } 37839800Sbostic 37939800Sbostic /* 38047220Sbostic * Fts_set takes the stream as an argument although it's not used in this 38139800Sbostic * implementation; it would be necessary if anyone wanted to add global 38245600Sbostic * semantics to fts using fts_set. An error return is allowed for similar 38345600Sbostic * reasons. 38439800Sbostic */ 38539800Sbostic /* ARGSUSED */ 386*52209Sbostic int 38745600Sbostic fts_set(sp, p, instr) 38839800Sbostic FTS *sp; 38939800Sbostic FTSENT *p; 39039800Sbostic int instr; 39139800Sbostic { 39242273Sbostic p->fts_instr = instr; 39352150Sbostic return (0); 39439800Sbostic } 39539800Sbostic 39639800Sbostic FTSENT * 39745600Sbostic fts_children(sp) 39839800Sbostic register FTS *sp; 39939800Sbostic { 40042299Sbostic register FTSENT *p; 40142299Sbostic int fd; 40242299Sbostic 40345600Sbostic /* Set current node pointer. */ 40445600Sbostic p = sp->fts_cur; 40545600Sbostic 40639800Sbostic /* 40752160Sbostic * Errno set to 0 so user can distinguish empty directory from 40852160Sbostic * an error. 40939800Sbostic */ 41039800Sbostic errno = 0; 41152160Sbostic 41252160Sbostic /* Fatal errors stop here. */ 41352160Sbostic if (ISSET(FTS_STOP)) 41452150Sbostic return (NULL); 41545600Sbostic 41652160Sbostic /* Return logical hierarchy of user's arguments. */ 41752160Sbostic if (p->fts_info == FTS_INIT) 41852160Sbostic return (p->fts_link); 41952160Sbostic 42052160Sbostic /* If not a directory being visited in pre-order, stop here. */ 42152160Sbostic if (p->fts_info != FTS_D && p->fts_info != FTS_DNR) 42252160Sbostic return (NULL); 42352160Sbostic 42445600Sbostic /* Free up any previous child list. */ 42542273Sbostic if (sp->fts_child) 42642273Sbostic fts_lfree(sp->fts_child); 42742299Sbostic 42842299Sbostic /* 42945600Sbostic * If using chdir on a relative path and called BEFORE fts_read does 43047194Sbostic * its chdir to the root of a traversal, we can lose -- we need to 43147194Sbostic * chdir into the subdirectory, and we don't know where the current 43247194Sbostic * directory is, so we can't get back so that the upcoming chdir by 43347194Sbostic * fts_read will work. 43442299Sbostic */ 43547212Sbostic if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || 43645600Sbostic ISSET(FTS_NOCHDIR)) 43752150Sbostic return (sp->fts_child = fts_build(sp, BCHILD)); 43842299Sbostic 43942299Sbostic if ((fd = open(".", O_RDONLY, 0)) < 0) 44052150Sbostic return (NULL); 44142299Sbostic sp->fts_child = fts_build(sp, BCHILD); 44242299Sbostic if (fchdir(fd)) 44352150Sbostic return (NULL); 44442299Sbostic (void)close(fd); 44552150Sbostic return (sp->fts_child); 44639800Sbostic } 44739800Sbostic 44847194Sbostic /* 44947194Sbostic * This is the tricky part -- do not casually change *anything* in here. The 45047194Sbostic * idea is to build the linked list of entries that are used by fts_children 45147194Sbostic * and fts_read. There are lots of special cases. 45247194Sbostic * 45347194Sbostic * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is 45447194Sbostic * set and it's a physical walk (so that symbolic links can't be directories), 45547194Sbostic * we assume that the number of subdirectories in a node is equal to the number 45647194Sbostic * of links to the parent. This allows stat calls to be skipped in any leaf 45747194Sbostic * directories and for any nodes after the directories in the parent node have 45847194Sbostic * been found. This empirically cuts the stat calls by about 2/3. 45947194Sbostic */ 46047155Sbostic static FTSENT * 46142299Sbostic fts_build(sp, type) 46239800Sbostic register FTS *sp; 46342299Sbostic int type; 46439800Sbostic { 46539800Sbostic register struct dirent *dp; 46639800Sbostic register FTSENT *p, *head; 46739800Sbostic register int nitems; 46847194Sbostic FTSENT *cur; 46939800Sbostic DIR *dirp; 470*52209Sbostic void *adjaddr; 47147194Sbostic int cderr, descend, len, level, maxlen, nlinks, saved_errno; 47239800Sbostic char *cp; 47339800Sbostic 47445600Sbostic /* Set current node pointer. */ 47547194Sbostic cur = sp->fts_cur; 47645600Sbostic 47747194Sbostic /* 47847194Sbostic * Open the directory for reading. If this fails, we're done. 47947194Sbostic * If being called from fts_read, set the fts_info field. 48047194Sbostic */ 48152155Sbostic if ((dirp = opendir(cur->fts_accpath)) == NULL) { 48252206Sbostic if (type == BREAD) { 48347194Sbostic cur->fts_info = FTS_DNR; 48452206Sbostic cur->fts_errno = errno; 48552206Sbostic } 48652150Sbostic return (NULL); 48739800Sbostic } 48839800Sbostic 48939800Sbostic /* 49047194Sbostic * Nlinks is the number of possible entries of type directory in the 49147194Sbostic * directory if we're cheating on stat calls, 0 if we're not doing 49247194Sbostic * any stat calls at all, -1 if we're doing stats on everything. 49339800Sbostic */ 49442273Sbostic nlinks = 49545600Sbostic ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ? 496*52209Sbostic cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1; 49739800Sbostic 49847194Sbostic /* 49947194Sbostic * If we're going to need to stat anything or we want to descend 50047194Sbostic * and stay in the directory, chdir. If this fails we keep going. 50147194Sbostic * We won't be able to stat anything, but we can still return the 50247194Sbostic * names themselves. Note, that since fts_read won't be able to 50347194Sbostic * chdir into the directory, it will have to return different path 50447194Sbostic * names than before, i.e. "a/b" instead of "b". Since the node 50547194Sbostic * has already been visited in pre-order, have to wait until the 50647194Sbostic * post-order visit to return the error. This is all fairly nasty. 50747194Sbostic * If a program needed sorted entries or stat information, they had 50847194Sbostic * better be checking FTS_NS on the returned nodes. 50947194Sbostic */ 51045600Sbostic if (nlinks || type == BREAD) 51147194Sbostic if (FCHDIR(sp, dirfd(dirp))) { 51247194Sbostic if (type == BREAD) 51352160Sbostic cur->fts_errno = errno; 51447194Sbostic descend = nlinks = 0; 51547194Sbostic cderr = 1; 51647194Sbostic } else { 51745600Sbostic descend = 1; 51847194Sbostic cderr = 0; 51939962Sbostic } 52047194Sbostic else 52147194Sbostic descend = 0; 52239962Sbostic 52347194Sbostic /* 52447194Sbostic * Figure out the max file name length that can be stored in the 52547194Sbostic * current path -- the inner loop allocates more path as necessary. 52647194Sbostic * We really wouldn't have to do the maxlen calculations here, we 52747194Sbostic * could do them in fts_read before returning the path, but it's a 52847194Sbostic * lot easier here since the length is part of the dirent structure. 52947194Sbostic * 530*52209Sbostic * If not changing directories set a pointer so that can just append 531*52209Sbostic * each new name into the path. 53247194Sbostic */ 53347194Sbostic maxlen = sp->fts_pathlen - cur->fts_pathlen - 1; 53447194Sbostic len = NAPPEND(cur); 53547194Sbostic if (ISSET(FTS_NOCHDIR)) { 53647194Sbostic cp = sp->fts_path + len; 53747194Sbostic *cp++ = '/'; 53847194Sbostic } 53940939Sbostic 54047194Sbostic level = cur->fts_level + 1; 54140939Sbostic 54247194Sbostic /* Read the directory, attaching each entry to the `link' pointer. */ 543*52209Sbostic adjaddr = NULL; 54439800Sbostic for (head = NULL, nitems = 0; dp = readdir(dirp);) { 54547194Sbostic if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) 54639800Sbostic continue; 54739800Sbostic 54852155Sbostic if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL) 54939800Sbostic goto mem1; 55039800Sbostic if (dp->d_namlen > maxlen) { 551*52209Sbostic if (fts_palloc(sp, (int)dp->d_namlen)) { 55247194Sbostic /* 55347194Sbostic * No more memory for path or structures. Save 55447194Sbostic * errno, free up the current structure and the 55547194Sbostic * structures already allocated. 55647194Sbostic */ 55747194Sbostic mem1: saved_errno = errno; 55847194Sbostic if (p) 55947194Sbostic free(p); 56047194Sbostic fts_lfree(head); 56147194Sbostic (void)closedir(dirp); 56239800Sbostic errno = saved_errno; 56347194Sbostic cur->fts_info = FTS_ERR; 56447194Sbostic SET(FTS_STOP); 56552150Sbostic return (NULL); 56639800Sbostic } 567*52209Sbostic adjaddr = sp->fts_path; 56842273Sbostic maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1; 56939800Sbostic } 57039800Sbostic 57142273Sbostic p->fts_pathlen = len + dp->d_namlen + 1; 57242273Sbostic p->fts_parent = sp->fts_cur; 57342273Sbostic p->fts_level = level; 57439800Sbostic 57539800Sbostic if (nlinks) { 57647194Sbostic /* Build a file name for fts_stat to stat. */ 57747194Sbostic if (ISSET(FTS_NOCHDIR)) { 57847194Sbostic p->fts_accpath = p->fts_path; 57942273Sbostic bcopy(p->fts_name, cp, p->fts_namelen + 1); 58047194Sbostic } else 58147194Sbostic p->fts_accpath = p->fts_name; 58245600Sbostic p->fts_info = fts_stat(sp, p, 0); 583*52209Sbostic if (nlinks > 0 && (p->fts_info == FTS_D || 584*52209Sbostic p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) 58539800Sbostic --nlinks; 58647194Sbostic } else if (cderr) { 58747194Sbostic p->fts_info = ISSET(FTS_NOSTAT) ? FTS_NSOK : FTS_NS; 58847194Sbostic p->fts_accpath = cur->fts_accpath; 58947194Sbostic } else { 59047194Sbostic p->fts_accpath = 59147194Sbostic ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; 59247194Sbostic p->fts_info = FTS_NSOK; 59347194Sbostic } 59439800Sbostic 59542273Sbostic p->fts_link = head; 59639800Sbostic head = p; 59739800Sbostic ++nitems; 59839800Sbostic } 59939800Sbostic (void)closedir(dirp); 60039800Sbostic 60147194Sbostic /* 602*52209Sbostic * If had to realloc the path, adjust the addresses for the rest 603*52209Sbostic * of the tree. 604*52209Sbostic */ 605*52209Sbostic if (adjaddr) 606*52209Sbostic fts_padjust(sp, adjaddr); 607*52209Sbostic 608*52209Sbostic /* 60947194Sbostic * If not changing directories, reset the path back to original 61047194Sbostic * state. 61147194Sbostic */ 61247194Sbostic if (ISSET(FTS_NOCHDIR)) { 61347194Sbostic if (cp - 1 > sp->fts_path) 61447194Sbostic --cp; 61547194Sbostic *cp = '\0'; 61647194Sbostic } 61742299Sbostic 61842299Sbostic /* 61947194Sbostic * If descended after called from fts_children or called from 62047194Sbostic * fts_read and didn't find anything, get back. If can't get 621*52209Sbostic * back, done. 62242299Sbostic */ 62342299Sbostic if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) { 62447194Sbostic cur->fts_info = FTS_ERR; 62547194Sbostic SET(FTS_STOP); 62652150Sbostic return (NULL); 62739962Sbostic } 62839962Sbostic 629*52209Sbostic /* If didn't find anything, just do the post-order visit */ 63039800Sbostic if (!nitems) { 63142299Sbostic if (type == BREAD) 63247194Sbostic cur->fts_info = FTS_DP; 63352150Sbostic return (NULL); 63439800Sbostic } 63539800Sbostic 63647194Sbostic /* Sort the entries. */ 63742273Sbostic if (sp->fts_compar && nitems > 1) 63845600Sbostic head = fts_sort(sp, head, nitems); 63952150Sbostic return (head); 64039800Sbostic } 64139800Sbostic 64245600Sbostic static u_short 64345600Sbostic fts_stat(sp, p, follow) 64445600Sbostic FTS *sp; 64539800Sbostic register FTSENT *p; 64645600Sbostic int follow; 64739800Sbostic { 64852160Sbostic register FTSENT *t; 64952160Sbostic register dev_t dev; 65052160Sbostic register ino_t ino; 651*52209Sbostic struct stat *sbp, sb; 65247194Sbostic int saved_errno; 65347194Sbostic 654*52209Sbostic /* If user needs stat info, stat buffer already allocated. */ 655*52209Sbostic sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; 656*52209Sbostic 65739800Sbostic /* 65845600Sbostic * If doing a logical walk, or application requested FTS_FOLLOW, do 65947194Sbostic * a stat(2). If that fails, check for a non-existent symlink. If 66052160Sbostic * fail, set the errno from the stat call. 66139800Sbostic */ 66245600Sbostic if (ISSET(FTS_LOGICAL) || follow) { 663*52209Sbostic if (stat(p->fts_accpath, sbp)) { 66447194Sbostic saved_errno = errno; 665*52209Sbostic if (!lstat(p->fts_accpath, sbp)) { 66647194Sbostic errno = 0; 66752150Sbostic return (FTS_SLNONE); 66847194Sbostic } 66952160Sbostic p->fts_errno = saved_errno; 670*52209Sbostic goto err; 67145600Sbostic } 672*52209Sbostic } else if (lstat(p->fts_accpath, sbp)) { 67352160Sbostic p->fts_errno = errno; 674*52209Sbostic err: bzero(sbp, sizeof(struct stat)); 67552150Sbostic return (FTS_NS); 67645600Sbostic } 67739800Sbostic 678*52209Sbostic if (S_ISDIR(sbp->st_mode)) { 679*52209Sbostic if (ISDOT(p->fts_name)) 680*52209Sbostic return (FTS_DOT); 681*52209Sbostic /* 682*52209Sbostic * Set the device/inode. Used to find cycles and check for 683*52209Sbostic * crossing mount points. Also remember the link count, used 684*52209Sbostic * in fts_build to limit the number of stat calls. It is 685*52209Sbostic * understood that these fields are only referenced if fts_info 686*52209Sbostic * is set to FTS_D. 687*52209Sbostic */ 688*52209Sbostic dev = p->fts_dev = sbp->st_dev; 689*52209Sbostic ino = p->fts_ino = sbp->st_ino; 690*52209Sbostic p->fts_nlink = sbp->st_nlink; 691*52209Sbostic 692*52209Sbostic /* 693*52209Sbostic * Cycle detection is done by brute force when the directory 694*52209Sbostic * is first encountered. If the tree gets deep enough or the 695*52209Sbostic * number of symbolic links to directories is high enough, 696*52209Sbostic * something faster might be worthwhile. 697*52209Sbostic */ 698*52209Sbostic for (t = p->fts_parent; 699*52209Sbostic t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) 700*52209Sbostic if (ino == t->fts_ino && dev == t->fts_dev) { 70152073Sbostic p->fts_cycle = t; 70252150Sbostic return (FTS_DC); 70347194Sbostic } 70452150Sbostic return (FTS_D); 70547194Sbostic } 706*52209Sbostic if (S_ISLNK(sbp->st_mode)) 70752150Sbostic return (FTS_SL); 708*52209Sbostic if (S_ISREG(sbp->st_mode)) 70952150Sbostic return (FTS_F); 71052150Sbostic return (FTS_DEFAULT); 71139800Sbostic } 71239800Sbostic 71339800Sbostic static FTSENT * 71445600Sbostic fts_sort(sp, head, nitems) 71545600Sbostic FTS *sp; 71639800Sbostic FTSENT *head; 71739800Sbostic register int nitems; 71839800Sbostic { 71939800Sbostic register FTSENT **ap, *p; 72039800Sbostic 72139800Sbostic /* 72245600Sbostic * Construct an array of pointers to the structures and call qsort(3). 72339800Sbostic * Reassemble the array in the order returned by qsort. If unable to 72439800Sbostic * sort for memory reasons, return the directory entries in their 72539800Sbostic * current order. Allocate enough space for the current needs plus 726*52209Sbostic * 40 so don't realloc one entry at a time. 72739800Sbostic */ 72845600Sbostic if (nitems > sp->fts_nitems) { 72945600Sbostic sp->fts_nitems = nitems + 40; 730*52209Sbostic if ((sp->fts_array = realloc(sp->fts_array, 731*52209Sbostic (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) { 73245600Sbostic sp->fts_nitems = 0; 73352150Sbostic return (head); 73439800Sbostic } 73539800Sbostic } 73645600Sbostic for (ap = sp->fts_array, p = head; p; p = p->fts_link) 73739800Sbostic *ap++ = p; 73845600Sbostic qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar); 73945600Sbostic for (head = *(ap = sp->fts_array); --nitems; ++ap) 74042273Sbostic ap[0]->fts_link = ap[1]; 74142273Sbostic ap[0]->fts_link = NULL; 74252150Sbostic return (head); 74339800Sbostic } 74439800Sbostic 74539800Sbostic static FTSENT * 74645600Sbostic fts_alloc(sp, name, len) 74745600Sbostic FTS *sp; 74839800Sbostic char *name; 74939800Sbostic register int len; 75039800Sbostic { 75139800Sbostic register FTSENT *p; 752*52209Sbostic int needstat; 75339800Sbostic 75439800Sbostic /* 755*52209Sbostic * Variable sized structures. The stat structure isn't necessary 756*52209Sbostic * if the user doesn't need it, and the name is variable length. 757*52209Sbostic * Allocate enough extra space after the structure to store them. 75839800Sbostic */ 759*52209Sbostic needstat = ISSET(FTS_NOSTAT) ? 0 : sizeof(struct stat); 760*52209Sbostic if ((p = malloc((size_t)(sizeof(FTSENT) + len + 1 + needstat))) == NULL) 76152150Sbostic return (NULL); 76242273Sbostic bcopy(name, p->fts_name, len + 1); 763*52209Sbostic if (needstat) 764*52209Sbostic p->fts_statp = (struct stat *)(p->fts_name + len + 1); 76542273Sbostic p->fts_namelen = len; 76645600Sbostic p->fts_path = sp->fts_path; 76747194Sbostic p->fts_instr = FTS_NOINSTR; 76852160Sbostic p->fts_errno = 0; 76945600Sbostic p->fts_number = 0; 770*52209Sbostic #ifdef NOT_NECESSARY 77145600Sbostic p->fts_pointer = NULL; 772*52209Sbostic #endif 77352150Sbostic return (p); 77439800Sbostic } 77539800Sbostic 77645600Sbostic static void 77739800Sbostic fts_lfree(head) 77839800Sbostic register FTSENT *head; 77939800Sbostic { 78039800Sbostic register FTSENT *p; 78139800Sbostic 78245600Sbostic /* Free a linked list of structures. */ 78339800Sbostic while (p = head) { 78442273Sbostic head = head->fts_link; 78547194Sbostic free(p); 78639800Sbostic } 78739800Sbostic } 78839800Sbostic 78939800Sbostic /* 790*52209Sbostic * Allow essentially unlimited paths; find, rm, ls should all work on any tree. 791*52209Sbostic * Most systems will allow creation of paths much longer than MAXPATHLEN, even 792*52209Sbostic * though the kernel won't resolve them. Add the size (not just what's needed) 793*52209Sbostic * plus 256 bytes so don't realloc the path 2 bytes at a time. 79439800Sbostic */ 795*52209Sbostic static int 796*52209Sbostic fts_palloc(sp, more) 79745600Sbostic FTS *sp; 798*52209Sbostic int more; 79939800Sbostic { 800*52209Sbostic 801*52209Sbostic sp->fts_pathlen += more + 256; 802*52209Sbostic sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen); 803*52209Sbostic return (sp->fts_path == NULL); 80447155Sbostic } 805*52209Sbostic 806*52209Sbostic /* 807*52209Sbostic * When the path is realloc'd, have to fix all of the pointers in structures 808*52209Sbostic * already returned. 809*52209Sbostic */ 810*52209Sbostic static void 811*52209Sbostic fts_padjust(sp, addr) 812*52209Sbostic FTS *sp; 813*52209Sbostic void *addr; 814*52209Sbostic { 815*52209Sbostic FTSENT *p; 816*52209Sbostic 817*52209Sbostic #define ADJUST(p) { \ 818*52209Sbostic (p)->fts_accpath = addr + ((p)->fts_accpath - (p)->fts_path); \ 819*52209Sbostic (p)->fts_path = addr; \ 820*52209Sbostic } 821*52209Sbostic /* Adjust the current set of children. */ 822*52209Sbostic for (p = sp->fts_child; p; p = p->fts_link) 823*52209Sbostic ADJUST(p); 824*52209Sbostic 825*52209Sbostic /* Adjust the rest of the tree. */ 826*52209Sbostic for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { 827*52209Sbostic ADJUST(p); 828*52209Sbostic p = p->fts_link ? p->fts_link : p->fts_parent; 829*52209Sbostic } 830*52209Sbostic } 831