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