OSDN Git Service

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