1 /* 2 * Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 * 16 * Sponsored in part by the Defense Advanced Research Projects 17 * Agency (DARPA) and Air Force Research Laboratory, Air Force 18 * Materiel Command, USAF, under agreement number F39502-99-1-0512. 19 * 20 * From: $OpenBSD: nftw.c,v 1.7 2006/03/31 19:41:44 millert Exp $ 21 * From: FreeBSD: head/lib/libc/gen/nftw.c 239160 2012-08-09 22:05:40Z jilles 22 */ 23 24 #include <sys/cdefs.h> 25 #include <sys/types.h> 26 #include <sys/stat.h> 27 #include <errno.h> 28 #include <fts.h> 29 #include <ftw.h> 30 31 #include "fts-compat11.h" 32 33 int 34 freebsd11_nftw(const char *path, 35 int (*fn)(const char *, const struct freebsd11_stat *, int, struct FTW *), 36 int nfds, int ftwflags) 37 { 38 char * const paths[2] = { (char *)path, NULL }; 39 struct FTW ftw; 40 FTSENT11 *cur; 41 FTS11 *ftsp; 42 int error = 0, ftsflags, fnflag, postorder, sverrno; 43 44 /* XXX - nfds is currently unused */ 45 if (nfds < 1) { 46 errno = EINVAL; 47 return (-1); 48 } 49 50 ftsflags = FTS_COMFOLLOW; 51 if (!(ftwflags & FTW_CHDIR)) 52 ftsflags |= FTS_NOCHDIR; 53 if (ftwflags & FTW_MOUNT) 54 ftsflags |= FTS_XDEV; 55 if (ftwflags & FTW_PHYS) 56 ftsflags |= FTS_PHYSICAL; 57 else 58 ftsflags |= FTS_LOGICAL; 59 postorder = (ftwflags & FTW_DEPTH) != 0; 60 ftsp = freebsd11_fts_open(paths, ftsflags, NULL); 61 if (ftsp == NULL) 62 return (-1); 63 while ((cur = freebsd11_fts_read(ftsp)) != NULL) { 64 switch (cur->fts_info) { 65 case FTS_D: 66 if (postorder) 67 continue; 68 fnflag = FTW_D; 69 break; 70 case FTS_DC: 71 continue; 72 case FTS_DNR: 73 fnflag = FTW_DNR; 74 break; 75 case FTS_DP: 76 if (!postorder) 77 continue; 78 fnflag = FTW_DP; 79 break; 80 case FTS_F: 81 case FTS_DEFAULT: 82 fnflag = FTW_F; 83 break; 84 case FTS_NS: 85 case FTS_NSOK: 86 fnflag = FTW_NS; 87 break; 88 case FTS_SL: 89 fnflag = FTW_SL; 90 break; 91 case FTS_SLNONE: 92 fnflag = FTW_SLN; 93 break; 94 default: 95 error = -1; 96 goto done; 97 } 98 ftw.base = cur->fts_pathlen - cur->fts_namelen; 99 ftw.level = cur->fts_level; 100 error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw); 101 if (error != 0) 102 break; 103 } 104 done: 105 sverrno = errno; 106 if (freebsd11_fts_close(ftsp) != 0 && error == 0) 107 error = -1; 108 else 109 errno = sverrno; 110 return (error); 111 } 112 113 __sym_compat(nftw, freebsd11_nftw, FBSD_1.0); 114