]>
Commit | Line | Data |
---|---|---|
5b2abdfb A |
1 | .\" Copyright (c) 1989, 1991, 1993, 1994 |
2 | .\" The Regents of the University of California. All rights reserved. | |
3 | .\" | |
4 | .\" Redistribution and use in source and binary forms, with or without | |
5 | .\" modification, are permitted provided that the following conditions | |
6 | .\" are met: | |
7 | .\" 1. Redistributions of source code must retain the above copyright | |
8 | .\" notice, this list of conditions and the following disclaimer. | |
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
10 | .\" notice, this list of conditions and the following disclaimer in the | |
11 | .\" documentation and/or other materials provided with the distribution. | |
12 | .\" 3. All advertising materials mentioning features or use of this software | |
13 | .\" must display the following acknowledgement: | |
14 | .\" This product includes software developed by the University of | |
15 | .\" California, Berkeley and its contributors. | |
16 | .\" 4. Neither the name of the University nor the names of its contributors | |
17 | .\" may be used to endorse or promote products derived from this software | |
18 | .\" without specific prior written permission. | |
19 | .\" | |
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | .\" SUCH DAMAGE. | |
31 | .\" | |
32 | .\" @(#)fts.3 8.5 (Berkeley) 4/16/94 | |
33 | .\" $FreeBSD: src/lib/libc/gen/fts.3,v 1.13 2001/09/20 12:32:45 ru Exp $ | |
34 | .\" | |
6465356a | 35 | .Dd Sept 24, 2012 |
5b2abdfb A |
36 | .Dt FTS 3 |
37 | .Os | |
38 | .Sh NAME | |
39 | .Nm fts | |
40 | .Nd traverse a file hierarchy | |
41 | .Sh LIBRARY | |
42 | .Lb libc | |
43 | .Sh SYNOPSIS | |
44 | .In sys/types.h | |
45 | .In sys/stat.h | |
46 | .In fts.h | |
47 | .Ft FTS * | |
48 | .Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT **, const FTSENT **)" | |
34e8f829 A |
49 | .Ft FTS * |
50 | .Fn fts_open_b "char * const *path_argv" "int options" "int (^compar)(const FTSENT **, const FTSENT **)" | |
5b2abdfb A |
51 | .Ft FTSENT * |
52 | .Fn fts_read "FTS *ftsp" | |
53 | .Ft FTSENT * | |
54 | .Fn fts_children "FTS *ftsp" "int options" | |
55 | .Ft int | |
56 | .Fn fts_set "FTS *ftsp" "FTSENT *f" "int options" | |
57 | .Ft int | |
58 | .Fn fts_close "FTS *ftsp" | |
59 | .Sh DESCRIPTION | |
60 | The | |
61 | .Nm | |
62 | functions are provided for traversing | |
63 | .Tn UNIX | |
64 | file hierarchies. | |
65 | A simple overview is that the | |
66 | .Fn fts_open | |
34e8f829 A |
67 | and |
68 | .Fn fts_open_b | |
69 | functions return a | |
5b2abdfb A |
70 | .Dq handle |
71 | on a file hierarchy, which is then supplied to | |
72 | the other | |
73 | .Nm | |
74 | functions. | |
75 | The function | |
76 | .Fn fts_read | |
77 | returns a pointer to a structure describing one of the files in the file | |
78 | hierarchy. | |
79 | The function | |
80 | .Fn fts_children | |
81 | returns a pointer to a linked list of structures, each of which describes | |
82 | one of the files contained in a directory in the hierarchy. | |
83 | In general, directories are visited two distinguishable times; in pre-order | |
84 | (before any of their descendants are visited) and in post-order (after all | |
85 | of their descendants have been visited). | |
86 | Files are visited once. | |
87 | It is possible to walk the hierarchy | |
88 | .Dq logically | |
89 | (ignoring symbolic links) | |
90 | or physically (visiting symbolic links), order the walk of the hierarchy or | |
91 | prune and/or re-visit portions of the hierarchy. | |
92 | .Pp | |
93 | Two structures are defined (and typedef'd) in the include file | |
94 | .Aq Pa fts.h . | |
95 | The first is | |
96 | .Fa FTS , | |
97 | the structure that represents the file hierarchy itself. | |
98 | The second is | |
99 | .Fa FTSENT , | |
100 | the structure that represents a file in the file | |
101 | hierarchy. | |
102 | Normally, an | |
103 | .Fa FTSENT | |
104 | structure is returned for every file in the file | |
105 | hierarchy. | |
106 | In this manual page, | |
107 | .Dq file | |
108 | and | |
109 | .Dq Fa FTSENT No structure | |
110 | are generally | |
111 | interchangeable. | |
112 | The | |
113 | .Fa FTSENT | |
114 | structure contains at least the following fields, which are | |
115 | described in greater detail below: | |
116 | .Bd -literal | |
117 | typedef struct _ftsent { | |
118 | u_short fts_info; /* flags for FTSENT structure */ | |
119 | char *fts_accpath; /* access path */ | |
120 | char *fts_path; /* root path */ | |
121 | u_short fts_pathlen; /* strlen(fts_path) */ | |
122 | char *fts_name; /* file name */ | |
123 | u_short fts_namelen; /* strlen(fts_name) */ | |
124 | short fts_level; /* depth (\-1 to N) */ | |
125 | int fts_errno; /* file errno */ | |
126 | long fts_number; /* local numeric value */ | |
127 | void *fts_pointer; /* local address value */ | |
128 | struct ftsent *fts_parent; /* parent directory */ | |
129 | struct ftsent *fts_link; /* next file structure */ | |
130 | struct ftsent *fts_cycle; /* cycle structure */ | |
131 | struct stat *fts_statp; /* stat(2) information */ | |
132 | } FTSENT; | |
133 | .Ed | |
134 | .Pp | |
135 | These fields are defined as follows: | |
136 | .Bl -tag -width "fts_namelen" | |
137 | .It Fa fts_info | |
138 | One of the following values describing the returned | |
139 | .Fa FTSENT | |
140 | structure and | |
141 | the file it represents. | |
142 | With the exception of directories without errors | |
143 | .Pq Dv FTS_D , | |
144 | all of these | |
145 | entries are terminal, that is, they will not be revisited, nor will any | |
146 | of their descendants be visited. | |
147 | .Bl -tag -width FTS_DEFAULT | |
148 | .It Dv FTS_D | |
149 | A directory being visited in pre-order. | |
150 | .It Dv FTS_DC | |
151 | A directory that causes a cycle in the tree. | |
152 | (The | |
153 | .Fa fts_cycle | |
154 | field of the | |
155 | .Fa FTSENT | |
156 | structure will be filled in as well.) | |
157 | .It Dv FTS_DEFAULT | |
158 | Any | |
159 | .Fa FTSENT | |
160 | structure that represents a file type not explicitly described | |
161 | by one of the other | |
162 | .Fa fts_info | |
163 | values. | |
164 | .It Dv FTS_DNR | |
165 | A directory which cannot be read. | |
166 | This is an error return, and the | |
167 | .Fa fts_errno | |
168 | field will be set to indicate what caused the error. | |
169 | .It Dv FTS_DOT | |
170 | A file named | |
171 | .Ql .\& | |
172 | or | |
173 | .Ql ..\& | |
174 | which was not specified as a file name to | |
175 | .Fn fts_open | |
34e8f829 A |
176 | or |
177 | .Fn fts_open_b | |
5b2abdfb A |
178 | (see |
179 | .Dv FTS_SEEDOT ) . | |
180 | .It Dv FTS_DP | |
181 | A directory being visited in post-order. | |
182 | The contents of the | |
183 | .Fa FTSENT | |
184 | structure will be unchanged from when | |
185 | it was returned in pre-order, i.e. with the | |
186 | .Fa fts_info | |
187 | field set to | |
188 | .Dv FTS_D . | |
189 | .It Dv FTS_ERR | |
190 | This is an error return, and the | |
191 | .Fa fts_errno | |
192 | field will be set to indicate what caused the error. | |
193 | .It Dv FTS_F | |
194 | A regular file. | |
195 | .It Dv FTS_NS | |
196 | A file for which no | |
197 | .Xr stat 2 | |
198 | information was available. | |
199 | The contents of the | |
200 | .Fa fts_statp | |
201 | field are undefined. | |
202 | This is an error return, and the | |
203 | .Fa fts_errno | |
204 | field will be set to indicate what caused the error. | |
205 | .It Dv FTS_NSOK | |
206 | A file for which no | |
207 | .Xr stat 2 | |
208 | information was requested. | |
209 | The contents of the | |
210 | .Fa fts_statp | |
211 | field are undefined. | |
212 | .It Dv FTS_SL | |
213 | A symbolic link. | |
214 | .It Dv FTS_SLNONE | |
215 | A symbolic link with a non-existent target. | |
216 | The contents of the | |
217 | .Fa fts_statp | |
218 | field reference the file characteristic information for the symbolic link | |
219 | itself. | |
220 | .El | |
221 | .It Fa fts_accpath | |
222 | A path for accessing the file from the current directory. | |
223 | .It Fa fts_path | |
224 | The path for the file relative to the root of the traversal. | |
225 | This path contains the path specified to | |
226 | .Fn fts_open | |
34e8f829 A |
227 | or |
228 | .Fn fts_open_b | |
5b2abdfb A |
229 | as a prefix. |
230 | .It Fa fts_pathlen | |
231 | The length of the string referenced by | |
232 | .Fa fts_path . | |
233 | .It Fa fts_name | |
234 | The name of the file. | |
235 | .It Fa fts_namelen | |
236 | The length of the string referenced by | |
237 | .Fa fts_name . | |
238 | .It Fa fts_level | |
239 | The depth of the traversal, numbered from \-1 to N, where this file | |
240 | was found. | |
241 | The | |
242 | .Fa FTSENT | |
243 | structure representing the parent of the starting point (or root) | |
244 | of the traversal is numbered | |
245 | .Dv FTS_ROOTPARENTLEVEL | |
246 | (\-1), and the | |
247 | .Fa FTSENT | |
248 | structure for the root | |
249 | itself is numbered | |
250 | .Dv FTS_ROOTLEVEL | |
251 | (0). | |
252 | .It Fa fts_errno | |
253 | Upon return of a | |
254 | .Fa FTSENT | |
255 | structure from the | |
256 | .Fn fts_children | |
257 | or | |
258 | .Fn fts_read | |
259 | functions, with its | |
260 | .Fa fts_info | |
261 | field set to | |
262 | .Dv FTS_DNR , | |
263 | .Dv FTS_ERR | |
264 | or | |
265 | .Dv FTS_NS , | |
266 | the | |
267 | .Fa fts_errno | |
268 | field contains the value of the external variable | |
269 | .Va errno | |
270 | specifying the cause of the error. | |
271 | Otherwise, the contents of the | |
272 | .Fa fts_errno | |
273 | field are undefined. | |
274 | .It Fa fts_number | |
275 | This field is provided for the use of the application program and is | |
276 | not modified by the | |
277 | .Nm | |
278 | functions. | |
279 | It is initialized to 0. | |
280 | .It Fa fts_pointer | |
281 | This field is provided for the use of the application program and is | |
282 | not modified by the | |
283 | .Nm | |
284 | functions. | |
285 | It is initialized to | |
286 | .Dv NULL . | |
287 | .It Fa fts_parent | |
288 | A pointer to the | |
289 | .Fa FTSENT | |
290 | structure referencing the file in the hierarchy | |
291 | immediately above the current file, i.e. the directory of which this | |
292 | file is a member. | |
293 | A parent structure for the initial entry point is provided as well, | |
294 | however, only the | |
295 | .Fa fts_level , | |
296 | .Fa fts_number | |
297 | and | |
298 | .Fa fts_pointer | |
299 | fields are guaranteed to be initialized. | |
300 | .It Fa fts_link | |
301 | Upon return from the | |
302 | .Fn fts_children | |
303 | function, the | |
304 | .Fa fts_link | |
305 | field points to the next structure in the NULL-terminated linked list of | |
306 | directory members. | |
307 | Otherwise, the contents of the | |
308 | .Fa fts_link | |
309 | field are undefined. | |
310 | .It Fa fts_cycle | |
311 | If a directory causes a cycle in the hierarchy (see | |
312 | .Dv FTS_DC ) , | |
313 | either because | |
314 | of a hard link between two directories, or a symbolic link pointing to a | |
315 | directory, the | |
316 | .Fa fts_cycle | |
317 | field of the structure will point to the | |
318 | .Fa FTSENT | |
319 | structure in the hierarchy that references the same file as the current | |
320 | .Fa FTSENT | |
321 | structure. | |
322 | Otherwise, the contents of the | |
323 | .Fa fts_cycle | |
324 | field are undefined. | |
325 | .It Fa fts_statp | |
326 | A pointer to | |
327 | .Xr stat 2 | |
328 | information for the file. | |
329 | .El | |
330 | .Pp | |
331 | A single buffer is used for all of the paths of all of the files in the | |
332 | file hierarchy. | |
333 | Therefore, the | |
334 | .Fa fts_path | |
335 | and | |
336 | .Fa fts_accpath | |
337 | fields are guaranteed to be | |
338 | .Dv NUL Ns -terminated | |
339 | .Em only | |
340 | for the file most recently returned by | |
341 | .Fn fts_read . | |
342 | To use these fields to reference any files represented by other | |
343 | .Fa FTSENT | |
344 | structures will require that the path buffer be modified using the | |
345 | information contained in that | |
346 | .Fa FTSENT | |
347 | structure's | |
348 | .Fa fts_pathlen | |
349 | field. | |
350 | Any such modifications should be undone before further calls to | |
351 | .Fn fts_read | |
352 | are attempted. | |
353 | The | |
354 | .Fa fts_name | |
355 | field is always | |
356 | .Dv NUL Ns -terminated . | |
357 | .Sh FTS_OPEN | |
358 | The | |
359 | .Fn fts_open | |
360 | function takes a pointer to an array of character pointers naming one | |
361 | or more paths which make up a logical file hierarchy to be traversed. | |
362 | The array must be terminated by a | |
363 | .Dv NULL | |
364 | pointer. | |
365 | .Pp | |
366 | There are | |
367 | a number of options, at least one of which (either | |
368 | .Dv FTS_LOGICAL | |
369 | or | |
370 | .Dv FTS_PHYSICAL ) | |
371 | must be specified. | |
372 | The options are selected by | |
373 | .Em or Ns 'ing | |
374 | the following values: | |
6465356a | 375 | .Bl -tag -width "FTS_NOSTAT_TYPE" |
5b2abdfb A |
376 | .It Dv FTS_COMFOLLOW |
377 | This option causes any symbolic link specified as a root path to be | |
378 | followed immediately whether or not | |
379 | .Dv FTS_LOGICAL | |
380 | is also specified. | |
381 | .It Dv FTS_LOGICAL | |
382 | This option causes the | |
383 | .Nm | |
384 | routines to return | |
385 | .Fa FTSENT | |
386 | structures for the targets of symbolic links | |
387 | instead of the symbolic links themselves. | |
388 | If this option is set, the only symbolic links for which | |
389 | .Fa FTSENT | |
390 | structures | |
391 | are returned to the application are those referencing non-existent files. | |
392 | Either | |
393 | .Dv FTS_LOGICAL | |
394 | or | |
395 | .Dv FTS_PHYSICAL | |
396 | .Em must | |
397 | be provided to the | |
398 | .Fn fts_open | |
399 | function. | |
400 | .It Dv FTS_NOCHDIR | |
401 | As a performance optimization, the | |
402 | .Nm | |
403 | functions change directories as they walk the file hierarchy. | |
404 | This has the side-effect that an application cannot rely on being | |
405 | in any particular directory during the traversal. | |
406 | The | |
407 | .Dv FTS_NOCHDIR | |
408 | option turns off this optimization, and the | |
409 | .Nm | |
410 | functions will not change the current directory. | |
411 | Note that applications should not themselves change their current directory | |
412 | and try to access files unless | |
413 | .Dv FTS_NOCHDIR | |
414 | is specified and absolute | |
415 | pathnames were provided as arguments to | |
416 | .Fn fts_open . | |
417 | .It Dv FTS_NOSTAT | |
418 | By default, returned | |
419 | .Fa FTSENT | |
420 | structures reference file characteristic information (the | |
421 | .Fa statp | |
422 | field) for each file visited. | |
423 | This option relaxes that requirement as a performance optimization, | |
6465356a A |
424 | not calling |
425 | .Xr stat 2 | |
426 | whenever possible. | |
427 | If | |
428 | .Xr stat 2 | |
429 | doesn't need to be called, the | |
5b2abdfb | 430 | .Nm |
6465356a | 431 | functions will set the |
5b2abdfb A |
432 | .Fa fts_info |
433 | field to | |
6465356a A |
434 | .Dv FTS_NSOK ; |
435 | otherwise | |
436 | .Fa fts_info | |
437 | will be set to the correct file information value corresponding to the | |
438 | .Xr stat 2 | |
439 | information. | |
440 | In any case, the | |
5b2abdfb | 441 | .Fa statp |
6465356a A |
442 | field will always be undefined. |
443 | Note that because | |
444 | .Nm | |
445 | detects directory cycles and dangling symbolic links, | |
446 | .Xr stat 2 | |
447 | is always called for directories and is called for symbolic links when | |
448 | .Dv FTS_LOGICAL | |
449 | is set. | |
450 | .It Dv FTS_NOSTAT_TYPE | |
451 | Like | |
452 | .Dv FTS_NOSTAT | |
453 | but if the file type is returned by | |
454 | .Xr readdir 3 , | |
455 | the corresponding file information value is returned in | |
456 | .Fa fts_info | |
457 | instead of | |
458 | .Dv FTS_NSOK . | |
5b2abdfb A |
459 | .It Dv FTS_PHYSICAL |
460 | This option causes the | |
461 | .Nm | |
462 | routines to return | |
463 | .Fa FTSENT | |
464 | structures for symbolic links themselves instead | |
465 | of the target files they point to. | |
466 | If this option is set, | |
467 | .Fa FTSENT | |
468 | structures for all symbolic links in the | |
469 | hierarchy are returned to the application. | |
470 | Either | |
471 | .Dv FTS_LOGICAL | |
472 | or | |
473 | .Dv FTS_PHYSICAL | |
474 | .Em must | |
475 | be provided to the | |
476 | .Fn fts_open | |
477 | function. | |
478 | .It Dv FTS_SEEDOT | |
479 | By default, unless they are specified as path arguments to | |
480 | .Fn fts_open , | |
481 | any files named | |
482 | .Ql .\& | |
483 | or | |
484 | .Ql ..\& | |
485 | encountered in the file hierarchy are ignored. | |
486 | This option causes the | |
487 | .Nm | |
488 | routines to return | |
489 | .Fa FTSENT | |
490 | structures for them. | |
491 | .It Dv FTS_XDEV | |
492 | This option prevents | |
493 | .Nm | |
494 | from descending into directories that have a different device number | |
495 | than the file from which the descent began. | |
496 | .El | |
497 | .Pp | |
498 | The argument | |
499 | .Fn compar | |
500 | specifies a user-defined function which may be used to order the traversal | |
501 | of the hierarchy. | |
502 | It | |
503 | takes two pointers to pointers to | |
504 | .Fa FTSENT | |
505 | structures as arguments and | |
506 | should return a negative value, zero, or a positive value to indicate | |
507 | if the file referenced by its first argument comes before, in any order | |
508 | with respect to, or after, the file referenced by its second argument. | |
509 | The | |
510 | .Fa fts_accpath , | |
511 | .Fa fts_path | |
512 | and | |
513 | .Fa fts_pathlen | |
514 | fields of the | |
515 | .Fa FTSENT | |
516 | structures may | |
517 | .Em never | |
518 | be used in this comparison. | |
519 | If the | |
520 | .Fa fts_info | |
521 | field is set to | |
522 | .Dv FTS_NS | |
523 | or | |
524 | .Dv FTS_NSOK , | |
525 | the | |
526 | .Fa fts_statp | |
527 | field may not either. | |
528 | If the | |
529 | .Fn compar | |
530 | argument is | |
531 | .Dv NULL , | |
532 | the directory traversal order is in the order listed in | |
533 | .Fa path_argv | |
534 | for the root paths, and in the order listed in the directory for | |
535 | everything else. | |
34e8f829 A |
536 | .Sh FTS_OPEN_B |
537 | The | |
538 | .Fn fts_open_b | |
539 | function is like | |
540 | .Fn fts_open | |
541 | except | |
542 | .Fa compar | |
543 | is a block pointer instead of a function pointer. | |
544 | This block is passed to | |
545 | .Xr qsort_b 3 | |
546 | (whereas | |
547 | .Fn fts_open | |
548 | passes its function pointer to | |
549 | .Xr qsort 3 ) . | |
550 | .Bd -ragged -offset indent | |
551 | Note: The | |
552 | .Fn Block_copy | |
553 | function (defined in | |
554 | .In Blocks.h ) | |
555 | is used by | |
556 | .Fn fts_open_b | |
557 | to make a copy of the block, especially for the case when a stack-based | |
558 | block might go out of scope when the subroutine returns. | |
559 | .Ed | |
5b2abdfb A |
560 | .Sh FTS_READ |
561 | The | |
562 | .Fn fts_read | |
563 | function returns a pointer to an | |
564 | .Fa FTSENT | |
565 | structure describing a file in | |
566 | the hierarchy. | |
567 | Directories (that are readable and do not cause cycles) are visited at | |
568 | least twice, once in pre-order and once in post-order. | |
569 | All other files are visited at least once. | |
570 | (Hard links between directories that do not cause cycles or symbolic | |
571 | links to symbolic links may cause files to be visited more than once, | |
572 | or directories more than twice.) | |
573 | .Pp | |
574 | If all the members of the hierarchy have been returned, | |
575 | .Fn fts_read | |
576 | returns | |
577 | .Dv NULL | |
578 | and sets the external variable | |
579 | .Va errno | |
580 | to 0. | |
581 | If an error unrelated to a file in the hierarchy occurs, | |
582 | .Fn fts_read | |
583 | returns | |
584 | .Dv NULL | |
585 | and sets | |
586 | .Va errno | |
587 | appropriately. | |
588 | If an error related to a returned file occurs, a pointer to an | |
589 | .Fa FTSENT | |
590 | structure is returned, and | |
591 | .Va errno | |
592 | may or may not have been set (see | |
593 | .Fa fts_info ) . | |
594 | .Pp | |
595 | The | |
596 | .Fa FTSENT | |
597 | structures returned by | |
598 | .Fn fts_read | |
599 | may be overwritten after a call to | |
600 | .Fn fts_close | |
601 | on the same file hierarchy stream, or, after a call to | |
602 | .Fn fts_read | |
603 | on the same file hierarchy stream unless they represent a file of type | |
604 | directory, in which case they will not be overwritten until after a call to | |
605 | .Fn fts_read | |
606 | after the | |
607 | .Fa FTSENT | |
608 | structure has been returned by the function | |
609 | .Fn fts_read | |
610 | in post-order. | |
611 | .Sh FTS_CHILDREN | |
612 | The | |
613 | .Fn fts_children | |
614 | function returns a pointer to an | |
615 | .Fa FTSENT | |
616 | structure describing the first entry in a NULL-terminated linked list of | |
617 | the files in the directory represented by the | |
618 | .Fa FTSENT | |
619 | structure most recently returned by | |
620 | .Fn fts_read . | |
621 | The list is linked through the | |
622 | .Fa fts_link | |
623 | field of the | |
624 | .Fa FTSENT | |
625 | structure, and is ordered by the user-specified comparison function, if any. | |
626 | Repeated calls to | |
627 | .Fn fts_children | |
628 | will recreate this linked list. | |
629 | .Pp | |
630 | As a special case, if | |
631 | .Fn fts_read | |
632 | has not yet been called for a hierarchy, | |
633 | .Fn fts_children | |
634 | will return a pointer to the files in the logical directory specified to | |
635 | .Fn fts_open , | |
636 | i.e. the arguments specified to | |
637 | .Fn fts_open . | |
638 | Otherwise, if the | |
639 | .Fa FTSENT | |
640 | structure most recently returned by | |
641 | .Fn fts_read | |
642 | is not a directory being visited in pre-order, | |
643 | or the directory does not contain any files, | |
644 | .Fn fts_children | |
645 | returns | |
646 | .Dv NULL | |
647 | and sets | |
648 | .Va errno | |
649 | to zero. | |
650 | If an error occurs, | |
651 | .Fn fts_children | |
652 | returns | |
653 | .Dv NULL | |
654 | and sets | |
655 | .Va errno | |
656 | appropriately. | |
657 | .Pp | |
658 | The | |
659 | .Fa FTSENT | |
660 | structures returned by | |
661 | .Fn fts_children | |
662 | may be overwritten after a call to | |
663 | .Fn fts_children , | |
664 | .Fn fts_close | |
665 | or | |
666 | .Fn fts_read | |
667 | on the same file hierarchy stream. | |
668 | .Pp | |
669 | .Em Option | |
670 | may be set to the following value: | |
671 | .Bl -tag -width FTS_NAMEONLY | |
672 | .It Dv FTS_NAMEONLY | |
673 | Only the names of the files are needed. | |
674 | The contents of all the fields in the returned linked list of structures | |
675 | are undefined with the exception of the | |
676 | .Fa fts_name | |
677 | and | |
678 | .Fa fts_namelen | |
679 | fields. | |
680 | .El | |
681 | .Sh FTS_SET | |
682 | The function | |
683 | .Fn fts_set | |
684 | allows the user application to determine further processing for the | |
685 | file | |
686 | .Fa f | |
687 | of the stream | |
688 | .Fa ftsp . | |
689 | The | |
690 | .Fn fts_set | |
691 | function | |
692 | returns 0 on success, and \-1 if an error occurs. | |
693 | .Em Option | |
694 | must be set to one of the following values: | |
695 | .Bl -tag -width FTS_PHYSICAL | |
696 | .It Dv FTS_AGAIN | |
697 | Re-visit the file; any file type may be re-visited. | |
698 | The next call to | |
699 | .Fn fts_read | |
700 | will return the referenced file. | |
701 | The | |
702 | .Fa fts_stat | |
703 | and | |
704 | .Fa fts_info | |
705 | fields of the structure will be reinitialized at that time, | |
706 | but no other fields will have been changed. | |
707 | This option is meaningful only for the most recently returned | |
708 | file from | |
709 | .Fn fts_read . | |
710 | Normal use is for post-order directory visits, where it causes the | |
711 | directory to be re-visited (in both pre and post-order) as well as all | |
712 | of its descendants. | |
713 | .It Dv FTS_FOLLOW | |
714 | The referenced file must be a symbolic link. | |
715 | If the referenced file is the one most recently returned by | |
716 | .Fn fts_read , | |
717 | the next call to | |
718 | .Fn fts_read | |
719 | returns the file with the | |
720 | .Fa fts_info | |
721 | and | |
722 | .Fa fts_statp | |
723 | fields reinitialized to reflect the target of the symbolic link instead | |
724 | of the symbolic link itself. | |
725 | If the file is one of those most recently returned by | |
726 | .Fn fts_children , | |
727 | the | |
728 | .Fa fts_info | |
729 | and | |
730 | .Fa fts_statp | |
731 | fields of the structure, when returned by | |
732 | .Fn fts_read , | |
733 | will reflect the target of the symbolic link instead of the symbolic link | |
734 | itself. | |
735 | In either case, if the target of the symbolic link does not exist the | |
736 | fields of the returned structure will be unchanged and the | |
737 | .Fa fts_info | |
738 | field will be set to | |
739 | .Dv FTS_SLNONE . | |
740 | .Pp | |
741 | If the target of the link is a directory, the pre-order return, followed | |
742 | by the return of all of its descendants, followed by a post-order return, | |
743 | is done. | |
744 | .It Dv FTS_SKIP | |
745 | No descendants of this file are visited. | |
746 | The file may be one of those most recently returned by either | |
747 | .Fn fts_children | |
748 | or | |
749 | .Fn fts_read . | |
750 | .El | |
751 | .Sh FTS_CLOSE | |
752 | The | |
753 | .Fn fts_close | |
754 | function closes a file hierarchy stream | |
755 | .Fa ftsp | |
756 | and restores the current directory to the directory from which | |
757 | .Fn fts_open | |
758 | was called to open | |
759 | .Fa ftsp . | |
760 | The | |
761 | .Fn fts_close | |
762 | function | |
763 | returns 0 on success, and \-1 if an error occurs. | |
764 | .Sh ERRORS | |
765 | The function | |
766 | .Fn fts_open | |
767 | may fail and set | |
768 | .Va errno | |
769 | for any of the errors specified for the library functions | |
770 | .Xr open 2 | |
771 | and | |
772 | .Xr malloc 3 . | |
773 | .Pp | |
774 | The function | |
775 | .Fn fts_close | |
776 | may fail and set | |
777 | .Va errno | |
778 | for any of the errors specified for the library functions | |
779 | .Xr chdir 2 | |
780 | and | |
781 | .Xr close 2 . | |
782 | .Pp | |
783 | The functions | |
784 | .Fn fts_read | |
785 | and | |
786 | .Fn fts_children | |
787 | may fail and set | |
788 | .Va errno | |
789 | for any of the errors specified for the library functions | |
790 | .Xr chdir 2 , | |
791 | .Xr malloc 3 , | |
792 | .Xr opendir 3 , | |
793 | .Xr readdir 3 | |
794 | and | |
795 | .Xr stat 2 . | |
796 | .Pp | |
797 | In addition, | |
798 | .Fn fts_children , | |
799 | .Fn fts_open | |
800 | and | |
801 | .Fn fts_set | |
802 | may fail and set | |
803 | .Va errno | |
804 | as follows: | |
805 | .Bl -tag -width Er | |
806 | .It Bq Er EINVAL | |
807 | The options were invalid. | |
808 | .El | |
809 | .Sh SEE ALSO | |
810 | .Xr find 1 , | |
811 | .Xr chdir 2 , | |
812 | .Xr stat 2 , | |
34e8f829 A |
813 | .Xr qsort 3 , |
814 | .Xr qsort_b 3 | |
5b2abdfb A |
815 | .Sh STANDARDS |
816 | The | |
817 | .Nm | |
818 | utility is expected to be included in a future | |
819 | .St -p1003.1-88 | |
820 | revision. |