1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
27 * ftwalk on top of fts
35 int (*comparf
)(Ftw_t
*, Ftw_t
*);
39 * why does fts take FTSENT** instead of FTSENT*
43 ftscompare(Ftw_t
* const* pf1
, Ftw_t
* const* pf2
)
45 return (*state
.comparf
)(*pf1
, *pf2
);
49 * the real thing -- well it used to be
53 ftwalk(const char* path
, int (*userf
)(Ftw_t
*), int flags
, int (*comparf
)(Ftw_t
*, Ftw_t
*))
57 register int children
;
67 if (flags
& FTW_TWICE
)
68 flags
&= ~(FTS_NOPREORDER
|FTS_NOPOSTORDER
);
69 else if (flags
& FTW_POST
)
70 flags
|= FTS_NOPREORDER
;
72 flags
|= FTS_NOPOSTORDER
;
73 if (children
= flags
& FTW_CHILDREN
)
75 state
.comparf
= comparf
;
76 if (!(f
= fts_open((char* const*)path
, flags
, comparf
? ftscompare
: 0)))
78 if (!path
|| !(flags
& FTS_ONEPATH
) && !(path
= (const char*)(*((char**)path
))))
80 ns
= strlen(path
) + 1;
81 if (!(e
= newof(0, FTSENT
, 1, ns
)))
83 e
->fts_accpath
= e
->fts_name
= e
->fts_path
= strcpy((char*)(e
+ 1), path
);
84 e
->fts_namelen
= e
->fts_pathlen
= ns
;
88 rv
= (*userf
)((Ftw_t
*)e
);
93 if (children
&& (e
= fts_children(f
, 0)))
96 for (x
= e
; x
; x
= x
->link
)
99 x
->statb
= *x
->fts_statp
;
102 if (nd
>= elementsof(dd
))
106 rv
= (*userf
)((Ftw_t
*)e
->parent
);
109 dd
[--nd
]->info
|= FTS_DD
;
110 for (x
= e
; x
; x
= x
->link
)
111 if (!(x
->info
& FTS_D
))
112 x
->status
= FTS_SKIP
;
114 while (!rv
&& (e
= fts_read(f
)))
118 ns
= e
->status
= e
->path
== e
->fts_accpath
? FTW_PATH
: FTW_NAME
;
125 for (x
= fts_children(f
, 0); x
; x
= x
->link
)
126 if (x
->info
& FTS_DD
)
128 x
->statb
= *x
->fts_statp
;
131 if (nd
>= elementsof(dd
))
145 rv
= (*userf
)((Ftw_t
*)e
);
150 dd
[--nd
]->info
|= FTS_DD
;