2 * Copyright (c) 2012 Jilles Tjoelker
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Limited test program for nftw() as specified by IEEE Std. 1003.1-2008.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
47 extern char **environ;
49 static char dir[] = "/tmp/testftw.XXXXXXXXXX";
54 cleanup(int ustatus __unused)
58 const char *myargs[5];
66 error = posix_spawnp(&pid, myargs[0], NULL, NULL,
67 __DECONST(char **, myargs), environ);
69 warn("posix_spawnp rm");
71 waitres = waitpid(pid, &status, 0);
73 warnx("waitpid rm failed");
80 cb(const char *path, const struct stat *st, int type, struct FTW *f)
85 if ((ftwflags & FTW_DEPTH) == 0)
89 if ((ftwflags & FTW_DEPTH) != 0)
93 if ((ftwflags & FTW_PHYS) != 0)
97 warnx("unexpected path=%s type=%d f.level=%d\n",
98 path, type, f->level);
104 main(int argc, char *argv[])
111 err_set_exit(cleanup);
113 fd = open(dir, O_DIRECTORY | O_RDONLY);
115 err(2, "open %s", dir);
117 if (mkdirat(fd, "d1", 0777) == -1)
118 err(2, "mkdirat d1");
120 if (symlinkat(dir, fd, "d1/looper") == -1)
121 err(2, "symlinkat looper");
124 if (nftw(dir, cb, 10, ftwflags) == -1)
125 err(2, "nftw FTW_PHYS");
126 ftwflags = FTW_PHYS | FTW_DEPTH;
127 if (nftw(dir, cb, 10, ftwflags) == -1)
128 err(2, "nftw FTW_PHYS | FTW_DEPTH");
130 if (nftw(dir, cb, 10, ftwflags) == -1)
132 ftwflags = FTW_DEPTH;
133 if (nftw(dir, cb, 10, ftwflags) == -1)
134 err(2, "nftw FTW_DEPTH");
138 printf("PASS nftw()\n");
140 cleanup(failures != 0);
142 return (failures != 0);