Commit | Line | Data |
---|---|---|
c4e1185d KB |
1 | .\" Copyright (c) 1989 The Regents of the University of California. |
2 | .\" All rights reserved. | |
3 | .\" | |
91cff1e1 | 4 | .\" %sccs.include.redist.man% |
c4e1185d | 5 | .\" |
91cff1e1 | 6 | .\" @(#)fts.3 5.6 (Berkeley) %G% |
c4e1185d KB |
7 | .\" |
8 | .TH FTS 3 "" | |
9 | .UC 7 | |
10 | .SH NAME | |
11 | fts \- traverse a file hierarchy | |
12 | .SH SYNOPSIS | |
13 | .nf | |
14 | .ft B | |
15 | #include <sys/types.h> | |
16 | #include <sys/stat.h> | |
17 | #include <fts.h> | |
18 | ||
19 | FTS * | |
9a0aa1ee | 20 | ftsopen(path_argv, options, compar) |
c4e1185d KB |
21 | char *path_argv[]; |
22 | int options, (*compar)(); | |
23 | ||
24 | FTSENT * | |
25 | ftsread(ftsp); | |
26 | FTS *ftsp; | |
27 | ||
28 | FTSENT * | |
29 | ftschildren(ftsp) | |
30 | FTS *ftsp; | |
31 | ||
32 | ftsset(ftsp, f, options) | |
33 | FTS *ftsp; | |
34 | FTSENT *f; | |
35 | int options; | |
36 | ||
37 | ftsclose(ftsp) | |
38 | FTS *ftsp; | |
39 | .ft R | |
40 | .fi | |
41 | .SH DESCRIPTION | |
42 | The | |
43 | .IR fts (3) | |
44 | utilities are provided for traversing UNIX file hierarchies. | |
45 | Two structures are defined (and typedef'd) in the include file <\fIfts.h\fP>. | |
46 | The first is FTS, the structure that defines the file hierarchy stream. | |
47 | The second is FTSENT, the structure that defines a file in the file | |
48 | hierarchy. | |
49 | .PP | |
50 | The | |
51 | .I ftsopen | |
9a0aa1ee KB |
52 | routine takes a pointer to an array of character pointers (``argv'') |
53 | naming the file hierarchies to be traversed. | |
54 | The array must be terminated by a pointer to a NULL string. | |
55 | .PP | |
c4e1185d KB |
56 | The |
57 | .I options | |
58 | specified are formed by | |
59 | .IR or 'ing | |
60 | one or more of the following values: | |
61 | .TP | |
62 | FTS_LOGICAL | |
63 | This option causes | |
64 | .I ftsread | |
65 | to use the function | |
66 | .IR stat (2), | |
67 | by default, to determine the status of each file. | |
68 | If this option is set, the only symbolic links returned to the application | |
69 | are those referencing non-existent files. | |
70 | Either FTS_LOGICAL or FTS_PHYSICAL | |
71 | .B must | |
72 | be provided to the | |
73 | .I ftsopen | |
74 | routine. | |
75 | .TP | |
c4e1185d KB |
76 | FTS_NOCHDIR |
77 | As a performance optimization, | |
78 | .I ftsread | |
79 | changes directories as it descends the hierarchy. | |
80 | This has the side-effect that applications cannot rely on being | |
81 | in any particular directory. | |
82 | The FTS_NOCHDIR option turns off this optimization. | |
83 | Note that applications should not change the current directory | |
84 | (even if FTS_NOCHDIR is specified) unless absolute pathnames were | |
85 | provided as arguments to | |
86 | .IR ftsopen . | |
87 | .TP | |
88 | FTS_NOSTAT | |
89 | By default, | |
90 | .I ftsread | |
91 | and | |
92 | .I ftschildren | |
93 | provide file characteristic information (the | |
94 | .I statb | |
95 | field) for each file they return. | |
96 | This option relaxes that requirement; the contents of the | |
97 | .I statb | |
98 | field may be undefined, and the | |
99 | .I info | |
100 | field may be set to FTS_NS. | |
101 | .TP | |
102 | FTS_PHYSICAL | |
103 | This option causes | |
104 | .I ftsread | |
105 | to use the function | |
106 | .IR lstat (2), | |
107 | by default, to determine the status of each file. | |
108 | If this option is set, all symbolic links are returned to the application | |
109 | program. | |
110 | Either FTS_LOGICAL or FTS_PHYSICAL | |
111 | .B must | |
112 | be provided to the | |
113 | .I ftsopen | |
114 | routine. | |
115 | .TP | |
116 | FTS_SEEDOT | |
117 | This option causes the routine | |
118 | .I ftsread | |
119 | to return structures for the directory entries ``.'' and ``..''. | |
120 | By default they are ignored unless specified as an argument to | |
121 | .IR ftsopen . | |
97bb157b KB |
122 | .TP |
123 | FTS_XDEV | |
124 | This option keeps | |
125 | .I fts | |
126 | from descending into directories that have a different device number | |
127 | than the file the descent began from. | |
c4e1185d KB |
128 | .PP |
129 | The argument | |
130 | .I compar | |
131 | specifies a user-defined routine which is used to order the traversal | |
132 | of directories. | |
133 | .I Compar | |
134 | takes two pointers to pointers to FTSENT structures as arguments and | |
135 | should return a negative value, zero, or a positive value to indicate | |
136 | if the file referenced by its first argument comes before, in any order | |
137 | with respect to, or after, the file referenced by its second argument. | |
9a0aa1ee KB |
138 | .PP |
139 | The | |
df589a0d | 140 | .I fts_accpath |
c4e1185d | 141 | and |
df589a0d | 142 | .I fts_path |
c4e1185d | 143 | fields of the FTSENT structures may not be used in this comparison. |
9a0aa1ee KB |
144 | If the option |
145 | .I FTS_NOSTAT | |
146 | is specified, the | |
147 | .I fts_stab | |
148 | field may not be used as well. | |
b42bc245 KB |
149 | If the |
150 | .I compar | |
9a0aa1ee KB |
151 | argument is NULL the directory traversal order is unspecified except |
152 | for the root paths which are traversed in the order listed in | |
153 | .IR path_argv . | |
c4e1185d KB |
154 | .PP |
155 | The | |
156 | .I ftsclose | |
157 | routine closes a file hierarchy stream and changes the current | |
158 | directory to the directory | |
159 | .I ftsopen | |
160 | was called from. | |
161 | .I Ftsclose | |
162 | returns 0 on success, and -1 if an error occurs. | |
163 | .PP | |
164 | Each call to the | |
165 | .I ftsread | |
166 | routine returns a pointer to an FTSENT structure describing a file in | |
167 | the hierarchy. | |
168 | Directories (that are readable, searchable and do not cause cycles) | |
169 | are visited at least twice, before any of their descendants have been | |
170 | visited (pre-order) and after all their descendants have been visited | |
171 | (post-order). | |
172 | All other files are visited at least once. | |
173 | .PP | |
174 | The FTSENT structure contains at least the following fields: | |
175 | .sp | |
176 | .RS | |
177 | .nf | |
178 | .ta .5i +.5i +\w'struct ftsent *parent;\0\0\0'u | |
179 | typedef struct ftsent { | |
df589a0d KB |
180 | struct ftsent *fts_parent; /* parent structure */ |
181 | struct ftsent *fts_link; /* cycle or file structure */ | |
c4e1185d KB |
182 | union { |
183 | long number; /* local numeric value */ | |
184 | void *pointer; /* local address value */ | |
df589a0d KB |
185 | } fts_local; |
186 | char *fts_accpath; /* path from current directory */ | |
187 | char *fts_path; /* path from starting directory */ | |
188 | char *fts_name; /* file name */ | |
189 | short fts_pathlen; /* strlen(path) */ | |
190 | short fts_namelen; /* strlen(name) */ | |
191 | short fts_level; /* depth (\-1 to N) */ | |
192 | unsigned short fts_info; /* flags for entry */ | |
193 | struct stat fts_statb; /* stat(2) information */ | |
c4e1185d KB |
194 | } FTSENT; |
195 | .fi | |
196 | .RE | |
197 | .PP | |
198 | The fields are as follows: | |
199 | .TP | |
df589a0d | 200 | fts_parent |
c4e1185d KB |
201 | A pointer to a structure referencing the file in the hierarchy |
202 | immediately above the current file/structure. | |
203 | A parent structure for the initial entry point is provided as well, | |
204 | however, only the | |
df589a0d | 205 | .I fts_local |
c4e1185d | 206 | and |
df589a0d | 207 | .I fts_level |
c4e1185d KB |
208 | fields are guaranteed to be initialized. |
209 | .TP | |
df589a0d | 210 | fts_link |
c4e1185d KB |
211 | If a directory causes a cycle in the hierarchy, either because of a |
212 | hard link between two directories, or a symbolic link pointing to a | |
213 | directory, the | |
df589a0d | 214 | .I fts_link |
c4e1185d KB |
215 | field of the structure will point to the structure in the hierarchy |
216 | that references the same file as the current structure. | |
217 | Upon return from the | |
218 | .I ftschildren | |
219 | routine, the | |
df589a0d | 220 | .I fts_link |
c4e1185d KB |
221 | field points to the next structure in the linked list of entries |
222 | from the directory. | |
df589a0d KB |
223 | Otherwise, the contents of the |
224 | .I fts_link | |
225 | field are undefined. | |
c4e1185d | 226 | .TP |
df589a0d | 227 | fts_local |
c4e1185d KB |
228 | This field is provided for the use of the application program. |
229 | It can be used to store either a long value or an address. | |
230 | .TP | |
df589a0d | 231 | fts_accpath |
c4e1185d KB |
232 | A path that can be used to access the file. |
233 | .TP | |
df589a0d | 234 | fts_path |
c4e1185d KB |
235 | The path for the file relative to the root of the traversal. |
236 | .TP | |
df589a0d | 237 | fts_name |
c4e1185d KB |
238 | The basename of the file. |
239 | .TP | |
df589a0d | 240 | fts_pathlen |
c4e1185d | 241 | The length of the string referenced by |
df589a0d | 242 | .IR fts_path . |
c4e1185d | 243 | .TP |
df589a0d | 244 | fts_namelen |
c4e1185d | 245 | The length of the string referenced by |
df589a0d | 246 | .IR fts_name . |
c4e1185d | 247 | .TP |
df589a0d | 248 | fts_level |
c4e1185d KB |
249 | The depth of the traversal, numbered from \-1 to N. |
250 | The parent structure of the root of the traversal is numbered \-1, and | |
251 | the structure for the root of the traversal is numbered 0. | |
252 | .TP | |
df589a0d | 253 | fts_info |
c4e1185d KB |
254 | Information describing the file. |
255 | With the exception of directories (FTS_D), all of these entries are | |
256 | terminal, i.e. they will not be revisited, nor will their descendants | |
257 | be visited (if they have not been visited already). | |
258 | .RS | |
259 | .TP | |
260 | FTS_D | |
261 | A directory being visited in pre-order. | |
262 | .TP | |
263 | FTS_DC | |
264 | A directory that causes a cycle. | |
265 | The | |
df589a0d | 266 | .I fts_link |
c4e1185d KB |
267 | field of the structure will be filled in as well. |
268 | .TP | |
269 | FTS_DEFAULT | |
270 | Anything that's not one of the others. | |
271 | .TP | |
272 | FTS_DNR | |
273 | A directory that cannot be read. | |
274 | .TP | |
275 | FTS_DNX | |
276 | A directory that cannot be searched. | |
277 | .TP | |
278 | FTS_DOT | |
279 | A file named ``.'' or ``..'' with a | |
df589a0d | 280 | .I fts_level |
c4e1185d KB |
281 | field not equal to zero, i.e. one not specified as an argument to |
282 | .IR ftsopen . | |
283 | (See FTS_SEEDOT.) | |
284 | .TP | |
285 | FTS_DP | |
286 | A directory that is being visited in post-order. | |
287 | The contents of the structure will be unchanged from when the | |
288 | directory was visited in pre-order. | |
289 | .TP | |
290 | FTS_ERR | |
291 | An error indicator; the external variable | |
292 | .I errno | |
293 | contains an error number indicating the reason for the error. | |
294 | .TP | |
295 | FTS_F | |
296 | A regular file. | |
297 | .TP | |
298 | FTS_NS | |
299 | No | |
300 | .IR stat (2) | |
301 | information is available at this time (see FTS_NOSTAT); the | |
302 | contents of the | |
df589a0d | 303 | .I fts_statb |
c4e1185d KB |
304 | field are undefined. |
305 | .TP | |
306 | FTS_SL | |
307 | A symbolic link. | |
308 | .TP | |
309 | FTS_SLNONE | |
310 | A symbolic link with a non-existent target. | |
311 | .RE | |
312 | .TP | |
df589a0d | 313 | fts_statb |
c4e1185d KB |
314 | .IR Stat (2) |
315 | information for the file. | |
316 | .PP | |
317 | The | |
df589a0d | 318 | .I fts_accpath |
c4e1185d | 319 | and |
df589a0d | 320 | .I fts_path |
c4e1185d KB |
321 | fields are guaranteed to be NULL-terminated |
322 | .B only | |
323 | for the file most recently returned by | |
324 | .IR ftsread . | |
325 | The | |
df589a0d | 326 | .I fts_name |
c4e1185d KB |
327 | field is always NULL-terminated. |
328 | To use these fields to reference any other active files may require | |
329 | that they be modified using the information contained in the | |
df589a0d | 330 | .I fts_pathlen |
c4e1185d KB |
331 | field. |
332 | Any such modifications should be undone before further calls to | |
333 | .I ftsread | |
334 | are attempted. | |
335 | .PP | |
336 | If all the members of the hierarchy have been returned, | |
337 | .I ftsread | |
338 | returns NULL and sets the external variable | |
339 | .I errno | |
340 | to 0. | |
341 | If an error unrelated to a file in the hierarchy occurs, | |
342 | .I ftsread | |
343 | returns NULL and sets errno appropriately. | |
344 | Otherwise, a pointer to an FTSENT structure is returned, and an | |
345 | error may or may not have occurred; see FTS_ERR above. | |
346 | .PP | |
347 | When the most recently returned file from | |
348 | .I ftsread | |
349 | is a directory being visited in pre-order, | |
350 | .I ftschildren | |
351 | returns a pointer to the first entry in a linked list (sorted by | |
352 | the comparison routine, if provided) of the directory entries | |
353 | it contains. | |
354 | The linked list is linked through the | |
df589a0d | 355 | .I fts_link |
c4e1185d KB |
356 | field of the FTSENT structure. |
357 | Each call to | |
358 | .I ftschildren | |
359 | recreates the list. | |
360 | Note, cycles are not detected until a directory is actually visited, | |
361 | therefore | |
362 | .I ftschildren | |
363 | will never return a structure with an | |
df589a0d | 364 | .I fts_info |
c4e1185d KB |
365 | field set to FTS_DC. |
366 | If the current file is not a directory being visited in pre-order, | |
367 | or if an error occurs, or the file does not contain any entries | |
368 | .I ftschildren | |
369 | returns NULL. | |
370 | If an error occurs, | |
371 | .I ftschildren | |
372 | sets errno appropriately, otherwise it sets errno to zero. | |
373 | .PP | |
374 | The pointers returned by | |
375 | .I ftsread | |
376 | and | |
377 | .I ftschildren | |
378 | point to structures which may be overwritten. | |
379 | Structures returned by | |
380 | .I ftschildren | |
381 | and | |
382 | .I ftsread | |
383 | may be overwritten after a call to | |
384 | .I ftsclose | |
385 | on the same file hierarchy. | |
386 | Otherwise, structures returned by | |
387 | .I ftschildren | |
388 | will not be overwritten until a subsequent call to either | |
389 | .I ftschildren | |
390 | or | |
391 | .I ftsread | |
392 | on the same file hierarchy. | |
393 | Structures returned by | |
394 | .I ftsread | |
395 | will not not be overwritten until a subsequent call to | |
396 | .I ftsread | |
397 | on the same file hierarchy stream, unless it represents a file of type | |
398 | directory, in which case it will not be overwritten until after a | |
399 | subsequent call to | |
400 | .I ftsread | |
401 | after it has been returned by the function | |
402 | .I ftsread | |
403 | in post-order. | |
404 | .PP | |
405 | The routine | |
406 | .I ftsset | |
407 | allows the user application to determine further processing for the | |
408 | file | |
409 | .I f | |
410 | of the stream | |
411 | .IR ftsp . | |
412 | .I Ftsset | |
413 | returns 0 on success, and -1 if an error occurs. | |
414 | .I Option | |
415 | may be set to any one of the following values. | |
416 | .TP | |
417 | FTS_AGAIN | |
418 | Re-visit the file; any file type may be re-visited. | |
419 | The next call to | |
420 | .I ftsread | |
421 | will return the referenced file. | |
422 | The | |
df589a0d | 423 | .I fts_stat |
c4e1185d | 424 | and |
df589a0d | 425 | .I fts_info |
c4e1185d KB |
426 | fields of the structure will be reinitialized at that time, |
427 | but no other fields will have been modified. | |
428 | This option is meaningful only for the most recently returned | |
429 | file from | |
430 | .IR ftsread . | |
431 | Normal use is for post-order directory visits, where it causes the | |
432 | directory to be re-visited (in both pre and post-order) as well as all | |
433 | of its descendants. | |
434 | .TP | |
435 | FTS_FOLLOW | |
436 | The referenced file must be a symbolic link. | |
437 | If the referenced file is the one most recently returned by | |
438 | .IR ftsread , | |
439 | the next call to | |
440 | .I ftsread | |
441 | returns the file with the | |
df589a0d | 442 | .I fts_info |
c4e1185d | 443 | and |
df589a0d | 444 | .I fts_statb |
c4e1185d KB |
445 | fields reinitialized to reflect the target of the symbolic link instead |
446 | of the symbolic link itself. | |
447 | If the file is one of those most recently returned by | |
448 | .IR ftschildren , | |
449 | the | |
df589a0d | 450 | .I fts_info |
c4e1185d | 451 | and |
df589a0d | 452 | .I fts_statb |
c4e1185d KB |
453 | fields of the structure, when returned by |
454 | .IR ftsread , | |
455 | will reflect the target of the symbolic link instead of the symbolic link | |
456 | itself. | |
457 | In either case, if the target of the link is a directory, the pre-order | |
458 | return, followed by the return of all of its descendants, followed by a | |
459 | post-order return, is done. | |
460 | .TP | |
461 | FTS_SKIP | |
462 | No descendants of this file are visited. | |
463 | .PP | |
464 | Hard links between directories that do not cause cycles or symbolic | |
465 | links to symbolic links may cause files to be visited more than once, | |
466 | or directories more than twice. | |
467 | .SH ERRORS | |
468 | .I Ftsopen | |
469 | may fail and set errno for any of the errors specified for the library | |
470 | routine | |
471 | .IR malloc (3). | |
472 | .PP | |
473 | .I Ftsclose | |
474 | may fail and set errno for any of the errors specified for the library | |
475 | routine | |
476 | .IR chdir (2). | |
477 | .PP | |
478 | .I Ftsread | |
479 | and | |
480 | .I ftschildren | |
481 | may fail and set errno for any of the errors specified for the library | |
482 | routines | |
483 | .IR chdir (2), | |
484 | .IR getgroups (2), | |
485 | .IR malloc (3), | |
486 | .IR opendir (3), | |
487 | .IR readdir (3) | |
488 | and | |
489 | .IR stat (2). | |
490 | .SH SEE ALSO | |
491 | find(1), chdir(2), stat(2), qsort(3) | |
492 | .SH STANDARDS | |
493 | The | |
494 | .I fts | |
df589a0d | 495 | utility is expected to be a superset of the POSIX 1003.1 specification. |