]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/ftw-compat11.c
zfs: merge openzfs/zfs@2e2a46e0a
[FreeBSD/FreeBSD.git] / lib / libc / gen / ftw-compat11.c
1 /*      $OpenBSD: ftw.c,v 1.5 2005/08/08 08:05:34 espie 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  * From: FreeBSD: head/lib/libc/gen/ftw.c 239151 2012-08-09 15:11:38Z jilles
23  */
24
25 #include <sys/cdefs.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <errno.h>
29 #include <fts.h>
30 #include <ftw.h>
31
32 #include "fts-compat11.h"
33
34 int freebsd11_ftw(const char *path, int (*fn)(const char *,
35     const struct freebsd11_stat *, int), int nfds);
36
37 int
38 freebsd11_ftw(const char *path,
39     int (*fn)(const char *, const struct freebsd11_stat *, int), int nfds)
40 {
41         char * const paths[2] = { (char *)path, NULL };
42         FTSENT11 *cur;
43         FTS11 *ftsp;
44         int error = 0, fnflag, sverrno;
45
46         /* XXX - nfds is currently unused */
47         if (nfds < 1) {
48                 errno = EINVAL;
49                 return (-1);
50         }
51
52         ftsp = freebsd11_fts_open(paths,
53             FTS_LOGICAL | FTS_COMFOLLOW | FTS_NOCHDIR, NULL);
54         if (ftsp == NULL)
55                 return (-1);
56         while ((cur = freebsd11_fts_read(ftsp)) != NULL) {
57                 switch (cur->fts_info) {
58                 case FTS_D:
59                         fnflag = FTW_D;
60                         break;
61                 case FTS_DNR:
62                         fnflag = FTW_DNR;
63                         break;
64                 case FTS_DP:
65                         /* we only visit in preorder */
66                         continue;
67                 case FTS_F:
68                 case FTS_DEFAULT:
69                         fnflag = FTW_F;
70                         break;
71                 case FTS_NS:
72                 case FTS_NSOK:
73                 case FTS_SLNONE:
74                         fnflag = FTW_NS;
75                         break;
76                 case FTS_SL:
77                         fnflag = FTW_SL;
78                         break;
79                 case FTS_DC:
80                         errno = ELOOP;
81                         /* FALLTHROUGH */
82                 default:
83                         error = -1;
84                         goto done;
85                 }
86                 error = fn(cur->fts_path, cur->fts_statp, fnflag);
87                 if (error != 0)
88                         break;
89         }
90 done:
91         sverrno = errno;
92         if (freebsd11_fts_close(ftsp) != 0 && error == 0)
93                 error = -1;
94         else
95                 errno = sverrno;
96         return (error);
97 }
98
99 __sym_compat(ftw, freebsd11_ftw, FBSD_1.0);