18e27b6e6SXin LI /* $OpenBSD: ftw.c,v 1.5 2005/08/08 08:05:34 espie 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 #include <sys/cdefs.h> 24e1bcce4fSTim J. Robbins __FBSDID("$FreeBSD$"); 25e1bcce4fSTim J. Robbins 26e1bcce4fSTim J. Robbins #include <sys/types.h> 27b03b864aSDavid Schultz #include <sys/stat.h> 28e1bcce4fSTim J. Robbins #include <errno.h> 29b03b864aSDavid Schultz #include <fts.h> 30e1bcce4fSTim J. Robbins #include <ftw.h> 31b03b864aSDavid Schultz 32e1bcce4fSTim J. Robbins int 33e1bcce4fSTim J. Robbins ftw(const char *path, int (*fn)(const char *, const struct stat *, int), 34e1bcce4fSTim J. Robbins int nfds) 35e1bcce4fSTim J. Robbins { 36e1bcce4fSTim J. Robbins char * const paths[2] = { (char *)path, NULL }; 37e1bcce4fSTim J. Robbins FTSENT *cur; 38e1bcce4fSTim J. Robbins FTS *ftsp; 39e1bcce4fSTim J. Robbins int error = 0, fnflag, sverrno; 40b03b864aSDavid Schultz 41e1bcce4fSTim J. Robbins /* XXX - nfds is currently unused */ 42*47875b0cSJilles Tjoelker if (nfds < 1) { 43b03b864aSDavid Schultz errno = EINVAL; 44e1bcce4fSTim J. Robbins return (-1); 45b03b864aSDavid Schultz } 46b03b864aSDavid Schultz 47e1bcce4fSTim J. Robbins ftsp = fts_open(paths, FTS_LOGICAL | FTS_COMFOLLOW | FTS_NOCHDIR, NULL); 48e1bcce4fSTim J. Robbins if (ftsp == NULL) 49e1bcce4fSTim J. Robbins return (-1); 50e1bcce4fSTim J. Robbins while ((cur = fts_read(ftsp)) != NULL) { 51e1bcce4fSTim J. Robbins switch (cur->fts_info) { 52b03b864aSDavid Schultz case FTS_D: 53e1bcce4fSTim J. Robbins fnflag = FTW_D; 54b03b864aSDavid Schultz break; 55b03b864aSDavid Schultz case FTS_DNR: 56e1bcce4fSTim J. Robbins fnflag = FTW_DNR; 57b03b864aSDavid Schultz break; 58b03b864aSDavid Schultz case FTS_DP: 59e1bcce4fSTim J. Robbins /* we only visit in preorder */ 60e1bcce4fSTim J. Robbins continue; 61e1bcce4fSTim J. Robbins case FTS_F: 62e1bcce4fSTim J. Robbins case FTS_DEFAULT: 63e1bcce4fSTim J. Robbins fnflag = FTW_F; 64b03b864aSDavid Schultz break; 65e1bcce4fSTim J. Robbins case FTS_NS: 66e1bcce4fSTim J. Robbins case FTS_NSOK: 67e1bcce4fSTim J. Robbins case FTS_SLNONE: 68e1bcce4fSTim J. Robbins fnflag = FTW_NS; 69e1bcce4fSTim J. Robbins break; 70e1bcce4fSTim J. Robbins case FTS_SL: 71e1bcce4fSTim J. Robbins fnflag = FTW_SL; 72e1bcce4fSTim J. Robbins break; 73e1bcce4fSTim J. Robbins case FTS_DC: 74e1bcce4fSTim J. Robbins errno = ELOOP; 75e1bcce4fSTim J. Robbins /* FALLTHROUGH */ 76b03b864aSDavid Schultz default: 77e1bcce4fSTim J. Robbins error = -1; 78e1bcce4fSTim J. Robbins goto done; 79e1bcce4fSTim J. Robbins } 80e1bcce4fSTim J. Robbins error = fn(cur->fts_path, cur->fts_statp, fnflag); 81e1bcce4fSTim J. Robbins if (error != 0) 829b5f0052SDavid Schultz break; 83b03b864aSDavid Schultz } 84e1bcce4fSTim J. Robbins done: 85e1bcce4fSTim J. Robbins sverrno = errno; 86e1bcce4fSTim J. Robbins if (fts_close(ftsp) != 0 && error == 0) 87e1bcce4fSTim J. Robbins error = -1; 88e1bcce4fSTim J. Robbins else 89e1bcce4fSTim J. Robbins errno = sverrno; 90e1bcce4fSTim J. Robbins return (error); 91b03b864aSDavid Schultz } 92