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