1 /* $OpenBSD: ftw.c,v 1.4 2004/07/07 16:05:23 millert Exp $ */ 2 3 /* 4 * Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * 18 * Sponsored in part by the Defense Advanced Research Projects 19 * Agency (DARPA) and Air Force Research Laboratory, Air Force 20 * Materiel Command, USAF, under agreement number F39502-99-1-0512. 21 */ 22 23 #if 0 24 #if defined(LIBC_SCCS) && !defined(lint) 25 static const char rcsid[] = "$OpenBSD: ftw.c,v 1.4 2004/07/07 16:05:23 millert Exp $"; 26 #endif /* LIBC_SCCS and not lint */ 27 #endif 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/types.h> 33 #include <sys/stat.h> 34 #include <errno.h> 35 #include <fts.h> 36 #include <ftw.h> 37 #include <limits.h> 38 39 int 40 ftw(const char *path, int (*fn)(const char *, const struct stat *, int), 41 int nfds) 42 { 43 char * const paths[2] = { (char *)path, NULL }; 44 FTSENT *cur; 45 FTS *ftsp; 46 int error = 0, fnflag, sverrno; 47 48 /* XXX - nfds is currently unused */ 49 if (nfds < 1 || nfds > OPEN_MAX) { 50 errno = EINVAL; 51 return (-1); 52 } 53 54 ftsp = fts_open(paths, FTS_LOGICAL | FTS_COMFOLLOW | FTS_NOCHDIR, NULL); 55 if (ftsp == NULL) 56 return (-1); 57 while ((cur = fts_read(ftsp)) != NULL) { 58 switch (cur->fts_info) { 59 case FTS_D: 60 fnflag = FTW_D; 61 break; 62 case FTS_DNR: 63 fnflag = FTW_DNR; 64 break; 65 case FTS_DP: 66 /* we only visit in preorder */ 67 continue; 68 case FTS_F: 69 case FTS_DEFAULT: 70 fnflag = FTW_F; 71 break; 72 case FTS_NS: 73 case FTS_NSOK: 74 case FTS_SLNONE: 75 fnflag = FTW_NS; 76 break; 77 case FTS_SL: 78 fnflag = FTW_SL; 79 break; 80 case FTS_DC: 81 errno = ELOOP; 82 /* FALLTHROUGH */ 83 default: 84 error = -1; 85 goto done; 86 } 87 error = fn(cur->fts_path, cur->fts_statp, fnflag); 88 if (error != 0) 89 break; 90 } 91 done: 92 sverrno = errno; 93 if (fts_close(ftsp) != 0 && error == 0) 94 error = -1; 95 else 96 errno = sverrno; 97 return (error); 98 } 99