GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/gen/nftw.c Lines: 0 38 0.0 %
Date: 2017-11-07 Branches: 0 31 0.0 %

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