181b0715aSXin LI /* $OpenBSD: nftw.c,v 1.7 2006/03/31 19:41:44 millert Exp $ */
2e1bcce4fSTim J. Robbins
3e1bcce4fSTim J. Robbins /*
4e1bcce4fSTim J. Robbins * Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
5e1bcce4fSTim J. Robbins *
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.
9e1bcce4fSTim J. Robbins *
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.
17e1bcce4fSTim J. Robbins *
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.
21e1bcce4fSTim J. Robbins */
22e1bcce4fSTim J. Robbins
23e1bcce4fSTim J. Robbins #include <sys/types.h>
24e1bcce4fSTim J. Robbins #include <sys/stat.h>
25e1bcce4fSTim J. Robbins #include <errno.h>
26e1bcce4fSTim J. Robbins #include <fts.h>
27e1bcce4fSTim J. Robbins #include <ftw.h>
28e1bcce4fSTim J. Robbins
29e1bcce4fSTim J. Robbins int
nftw(const char * path,int (* fn)(const char *,const struct stat *,int,struct FTW *),int nfds,int ftwflags)30e1bcce4fSTim J. Robbins nftw(const char *path, int (*fn)(const char *, const struct stat *, int,
31e1bcce4fSTim J. Robbins struct FTW *), int nfds, int ftwflags)
32e1bcce4fSTim J. Robbins {
33e1bcce4fSTim J. Robbins char * const paths[2] = { (char *)path, NULL };
34e1bcce4fSTim J. Robbins struct FTW ftw;
35e1bcce4fSTim J. Robbins FTSENT *cur;
36e1bcce4fSTim J. Robbins FTS *ftsp;
37e1bcce4fSTim J. Robbins int error = 0, ftsflags, fnflag, postorder, sverrno;
38e1bcce4fSTim J. Robbins
39e1bcce4fSTim J. Robbins /* XXX - nfds is currently unused */
404d767dfeSJilles Tjoelker if (nfds < 1) {
41e1bcce4fSTim J. Robbins errno = EINVAL;
42e1bcce4fSTim J. Robbins return (-1);
43e1bcce4fSTim J. Robbins }
44e1bcce4fSTim J. Robbins
45e1bcce4fSTim J. Robbins ftsflags = FTS_COMFOLLOW;
46e1bcce4fSTim J. Robbins if (!(ftwflags & FTW_CHDIR))
47e1bcce4fSTim J. Robbins ftsflags |= FTS_NOCHDIR;
48e1bcce4fSTim J. Robbins if (ftwflags & FTW_MOUNT)
49e1bcce4fSTim J. Robbins ftsflags |= FTS_XDEV;
50e1bcce4fSTim J. Robbins if (ftwflags & FTW_PHYS)
51e1bcce4fSTim J. Robbins ftsflags |= FTS_PHYSICAL;
52e1bcce4fSTim J. Robbins else
53e1bcce4fSTim J. Robbins ftsflags |= FTS_LOGICAL;
54e1bcce4fSTim J. Robbins postorder = (ftwflags & FTW_DEPTH) != 0;
55e1bcce4fSTim J. Robbins ftsp = fts_open(paths, ftsflags, NULL);
56e1bcce4fSTim J. Robbins if (ftsp == NULL)
57e1bcce4fSTim J. Robbins return (-1);
58e1bcce4fSTim J. Robbins while ((cur = fts_read(ftsp)) != NULL) {
59e1bcce4fSTim J. Robbins switch (cur->fts_info) {
60e1bcce4fSTim J. Robbins case FTS_D:
61e1bcce4fSTim J. Robbins if (postorder)
62e1bcce4fSTim J. Robbins continue;
63e1bcce4fSTim J. Robbins fnflag = FTW_D;
64e1bcce4fSTim J. Robbins break;
65*9d8ca3b2SJilles Tjoelker case FTS_DC:
66*9d8ca3b2SJilles Tjoelker continue;
67e1bcce4fSTim J. Robbins case FTS_DNR:
68e1bcce4fSTim J. Robbins fnflag = FTW_DNR;
69e1bcce4fSTim J. Robbins break;
70e1bcce4fSTim J. Robbins case FTS_DP:
71e1bcce4fSTim J. Robbins if (!postorder)
72e1bcce4fSTim J. Robbins continue;
73e1bcce4fSTim J. Robbins fnflag = FTW_DP;
74e1bcce4fSTim J. Robbins break;
75e1bcce4fSTim J. Robbins case FTS_F:
76e1bcce4fSTim J. Robbins case FTS_DEFAULT:
77e1bcce4fSTim J. Robbins fnflag = FTW_F;
78e1bcce4fSTim J. Robbins break;
79e1bcce4fSTim J. Robbins case FTS_NS:
80e1bcce4fSTim J. Robbins case FTS_NSOK:
81e1bcce4fSTim J. Robbins fnflag = FTW_NS;
82e1bcce4fSTim J. Robbins break;
83e1bcce4fSTim J. Robbins case FTS_SL:
84e1bcce4fSTim J. Robbins fnflag = FTW_SL;
85e1bcce4fSTim J. Robbins break;
86e1bcce4fSTim J. Robbins case FTS_SLNONE:
87e1bcce4fSTim J. Robbins fnflag = FTW_SLN;
88e1bcce4fSTim J. Robbins break;
89e1bcce4fSTim J. Robbins default:
90e1bcce4fSTim J. Robbins error = -1;
91e1bcce4fSTim J. Robbins goto done;
92e1bcce4fSTim J. Robbins }
93e1bcce4fSTim J. Robbins ftw.base = cur->fts_pathlen - cur->fts_namelen;
94e1bcce4fSTim J. Robbins ftw.level = cur->fts_level;
95e1bcce4fSTim J. Robbins error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw);
96e1bcce4fSTim J. Robbins if (error != 0)
97e1bcce4fSTim J. Robbins break;
98e1bcce4fSTim J. Robbins }
99e1bcce4fSTim J. Robbins done:
100e1bcce4fSTim J. Robbins sverrno = errno;
101e1bcce4fSTim J. Robbins if (fts_close(ftsp) != 0 && error == 0)
102e1bcce4fSTim J. Robbins error = -1;
103e1bcce4fSTim J. Robbins else
104e1bcce4fSTim J. Robbins errno = sverrno;
105e1bcce4fSTim J. Robbins return (error);
106e1bcce4fSTim J. Robbins }
107