OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / 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
382 .IR or ing
383 the following values:
384 .\" .Bl -tag -width "FTS_PHYSICAL"
385 .TP 13
386 .BR FTS_COMFOLLOW
387 This option causes any symbolic link specified as a root path to be
388 followed immediately whether or not
389 .BR FTS_LOGICAL
390 is also specified.
391 .TP
392 .BR FTS_LOGICAL
393 This option causes the
394 fts routines to return
395 .I FTSENT
396 structures for the targets of symbolic links
397 instead of the symbolic links themselves.
398 If this option is set, the only symbolic links for which
399 .I FTSENT
400 structures
401 are returned to the application are those referencing nonexistent files.
402 Either
403 .BR FTS_LOGICAL
404 or
405 .BR FTS_PHYSICAL
406 .I must
407 be provided to the
408 .BR fts_open ()
409 function.
410 .TP
411 .BR FTS_NOCHDIR
412 As a performance optimization, the
413 fts functions change directories as they walk the file hierarchy.
414 This has the side-effect that an application cannot rely on being
415 in any particular directory during the traversal.
416 The
417 .BR FTS_NOCHDIR
418 option turns off this optimization, and the
419 fts functions will not change the current directory.
420 Note that applications should not themselves change their current directory
421 and try to access files unless
422 .BR FTS_NOCHDIR
423 is specified and absolute
424 pathnames were provided as arguments to
425 .BR fts_open ().
426 .TP
427 .BR FTS_NOSTAT
428 By default, returned
429 .I FTSENT
430 structures reference file characteristic information (the
431 .I statp
432 field) for each file visited.
433 This option relaxes that requirement as a performance optimization,
434 allowing the
435 fts functions to set the
436 .I fts_info
437 field to
438 .BR FTS_NSOK
439 and leave the contents of the
440 .I statp
441 field undefined.
442 .TP
443 .BR FTS_PHYSICAL
444 This option causes the
445 fts routines to return
446 .I FTSENT
447 structures for symbolic links themselves instead
448 of the target files they point to.
449 If this option is set,
450 .I FTSENT
451 structures for all symbolic links in the
452 hierarchy are returned to the application.
453 Either
454 .BR FTS_LOGICAL
455 or
456 .BR FTS_PHYSICAL
457 .I must
458 be provided to the
459 .BR fts_open ()
460 function.
461 .TP
462 .BR FTS_SEEDOT
463 By default, unless they are specified as path arguments to
464 .BR fts_open (),
465 any files named
466 "."
467 or
468 ".."
469 encountered in the file hierarchy are ignored.
470 This option causes the
471 fts routines to return
472 .I FTSENT
473 structures for them.
474 .TP
475 .BR FTS_XDEV
476 This option prevents
477 fts from descending into directories that have a different device number
478 than the file from which the descent began.
479 .\" .El
480 .PP
481 The argument
482 .BR compar ()
483 specifies a user-defined function which may be used to order the traversal
484 of the hierarchy.
485 It
486 takes two pointers to pointers to
487 .I FTSENT
488 structures as arguments and
489 should return a negative value, zero, or a positive value to indicate
490 if the file referenced by its first argument comes before, in any order
491 with respect to, or after, the file referenced by its second argument.
492 The
493 .IR fts_accpath ,
494 .I fts_path
495 and
496 .I fts_pathlen
497 fields of the
498 .I FTSENT
499 structures may
500 .I never
501 be used in this comparison.
502 If the
503 .I fts_info
504 field is set to
505 .BR FTS_NS
506 or
507 .BR FTS_NSOK ,
508 the
509 .I fts_statp
510 field may not either.
511 If the
512 .BR compar ()
513 argument is
514 NULL,
515 the directory traversal order is in the order listed in
516 .I path_argv
517 for the root paths, and in the order listed in the directory for
518 everything else.
519 .SS fts_read()
520 The
521 .BR fts_read ()
522 function returns a pointer to an
523 .I FTSENT
524 structure describing a file in
525 the hierarchy.
526 Directories (that are readable and do not cause cycles) are visited at
527 least twice, once in preorder and once in postorder.
528 All other files are visited at least once.
529 (Hard links between directories that do not cause cycles or symbolic
530 links to symbolic links may cause files to be visited more than once,
531 or directories more than twice.)
532 .PP
533 If all the members of the hierarchy have been returned,
534 .BR fts_read ()
535 returns
536 NULL
537 and sets the external variable
538 .I errno
539 to 0.
540 If an error unrelated to a file in the hierarchy occurs,
541 .BR fts_read ()
542 returns
543 NULL
544 and sets
545 .I errno
546 appropriately.
547 If an error related to a returned file occurs, a pointer to an
548 .I FTSENT
549 structure is returned, and
550 .I errno
551 may or may not have been set (see
552 .IR fts_info ).
553 .PP
554 The
555 .I FTSENT
556 structures returned by
557 .BR fts_read ()
558 may be overwritten after a call to
559 .BR fts_close ()
560 on the same file hierarchy stream, or, after a call to
561 .BR fts_read ()
562 on the same file hierarchy stream unless they represent a file of type
563 directory, in which case they will not be overwritten until after a call to
564 .BR fts_read ()
565 after the
566 .I FTSENT
567 structure has been returned by the function
568 .BR fts_read ()
569 in postorder.
570 .SS fts_children()
571 The
572 .BR fts_children ()
573 function returns a pointer to an
574 .I FTSENT
575 structure describing the first entry in a NULL-terminated linked list of
576 the files in the directory represented by the
577 .I FTSENT
578 structure most recently returned by
579 .BR fts_read ().
580 The list is linked through the
581 .I fts_link
582 field of the
583 .I FTSENT
584 structure, and is ordered by the user-specified comparison function, if any.
585 Repeated calls to
586 .BR fts_children ()
587 will recreate this linked list.
588 .PP
589 As a special case, if
590 .BR fts_read ()
591 has not yet been called for a hierarchy,
592 .BR fts_children ()
593 will return a pointer to the files in the logical directory specified to
594 .BR fts_open (),
595 that is, the arguments specified to
596 .BR fts_open ().
597 Otherwise, if the
598 .I FTSENT
599 structure most recently returned by
600 .BR fts_read ()
601 is not a directory being visited in preorder,
602 or the directory does not contain any files,
603 .BR fts_children ()
604 returns
605 NULL
606 and sets
607 .I errno
608 to zero.
609 If an error occurs,
610 .BR fts_children ()
611 returns
612 NULL
613 and sets
614 .I errno
615 appropriately.
616 .PP
617 The
618 .I FTSENT
619 structures returned by
620 .BR fts_children ()
621 may be overwritten after a call to
622 .BR fts_children (),
623 .BR fts_close ()
624 or
625 .BR fts_read ()
626 on the same file hierarchy stream.
627 .PP
628 .I Option
629 may be set to the following value:
630 .\" .Bl -tag -width FTS_NAMEONLY
631 .TP 13
632 .BR FTS_NAMEONLY
633 Only the names of the files are needed.
634 The contents of all the fields in the returned linked list of structures
635 are undefined with the exception of the
636 .I fts_name
637 and
638 .I fts_namelen
639 fields.
640 .\" .El
641 .SS fts_set()
642 The function
643 .BR fts_set ()
644 allows the user application to determine further processing for the
645 file
646 .I f
647 of the stream
648 .IR ftsp .
649 The
650 .BR fts_set ()
651 function
652 returns 0 on success, and \-1 if an error occurs.
653 .I Option
654 must be set to one of the following values:
655 .\" .Bl -tag -width FTS_PHYSICAL
656 .TP 13
657 .BR FTS_AGAIN
658 Re-visit the file; any file type may be revisited.
659 The next call to
660 .BR fts_read ()
661 will return the referenced file.
662 The
663 .I fts_stat
664 and
665 .I fts_info
666 fields of the structure will be reinitialized at that time,
667 but no other fields will have been changed.
668 This option is meaningful only for the most recently returned
669 file from
670 .BR fts_read ().
671 Normal use is for postorder directory visits, where it causes the
672 directory to be revisited (in both preorder and postorder) as well as all
673 of its descendants.
674 .TP
675 .BR FTS_FOLLOW
676 The referenced file must be a symbolic link.
677 If the referenced file is the one most recently returned by
678 .BR fts_read (),
679 the next call to
680 .BR fts_read ()
681 returns the file with the
682 .I fts_info
683 and
684 .I fts_statp
685 fields reinitialized to reflect the target of the symbolic link instead
686 of the symbolic link itself.
687 If the file is one of those most recently returned by
688 .BR fts_children (),
689 the
690 .I fts_info
691 and
692 .I fts_statp
693 fields of the structure, when returned by
694 .BR fts_read (),
695 will reflect the target of the symbolic link instead of the symbolic link
696 itself.
697 In either case, if the target of the symbolic link does not exist the
698 fields of the returned structure will be unchanged and the
699 .I fts_info
700 field will be set to
701 .BR FTS_SLNONE .
702 .IP
703 If the target of the link is a directory, the preorder return, followed
704 by the return of all of its descendants, followed by a postorder return,
705 is done.
706 .TP
707 .BR FTS_SKIP
708 No descendants of this file are visited.
709 The file may be one of those most recently returned by either
710 .BR fts_children ()
711 or
712 .BR fts_read ().
713 .\" .El
714 .SS fts_close()
715 The
716 .BR fts_close ()
717 function closes a file hierarchy stream
718 .I ftsp
719 and restores the current directory to the directory from which
720 .BR fts_open ()
721 was called to open
722 .IR ftsp .
723 The
724 .BR fts_close ()
725 function
726 returns 0 on success, and \-1 if an error occurs.
727 .SH ERRORS
728 The function
729 .BR fts_open ()
730 may fail and set
731 .I errno
732 for any of the errors specified for
733 .BR open (2)
734 and
735 .BR malloc (3).
736 .PP
737 The function
738 .BR fts_close ()
739 may fail and set
740 .I errno
741 for any of the errors specified for
742 .BR chdir (2)
743 and
744 .BR close (2).
745 .PP
746 The functions
747 .BR fts_read ()
748 and
749 .BR fts_children ()
750 may fail and set
751 .I errno
752 for any of the errors specified for
753 .BR chdir (2),
754 .BR malloc (3),
755 .BR opendir (3),
756 .BR readdir (3)
757 and
758 .BR stat (2).
759 .PP
760 In addition,
761 .BR fts_children (),
762 .BR fts_open ()
763 and
764 .BR fts_set ()
765 may fail and set
766 .I errno
767 as follows:
768 .TP
769 .B EINVAL
770 The options were invalid.
771 .SH VERSIONS
772 These functions are available in Linux since glibc2.
773 .SH "CONFORMING TO"
774 4.4BSD.
775 .\" The following statement is years old, and seems no closer to
776 .\" being true -- mtk
777 .\" The
778 .\" .I fts
779 .\" utility is expected to be included in a future
780 .\" POSIX.1
781 .\" revision.
782 .SH SEE ALSO
783 .BR find (1),
784 .BR chdir (2),
785 .BR stat (2),
786 .BR ftw (3),
787 .BR qsort (3)