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