OSDN Git Service

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