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