xref: /freebsd/lib/libsys/getdirentries.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright (c) 1989, 1991, 1993
2*8269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
3*8269e767SBrooks Davis.\"
4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
6*8269e767SBrooks Davis.\" are met:
7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
8*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
10*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
11*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
13*8269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
14*8269e767SBrooks Davis.\"    without specific prior written permission.
15*8269e767SBrooks Davis.\"
16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8269e767SBrooks Davis.\" SUCH DAMAGE.
27*8269e767SBrooks Davis.\"
28*8269e767SBrooks Davis.Dd September 5, 2023
29*8269e767SBrooks Davis.Dt GETDIRENTRIES 2
30*8269e767SBrooks Davis.Os
31*8269e767SBrooks Davis.Sh NAME
32*8269e767SBrooks Davis.Nm getdirentries ,
33*8269e767SBrooks Davis.Nm getdents
34*8269e767SBrooks Davis.Nd "get directory entries in a file system independent format"
35*8269e767SBrooks Davis.Sh LIBRARY
36*8269e767SBrooks Davis.Lb libc
37*8269e767SBrooks Davis.Sh SYNOPSIS
38*8269e767SBrooks Davis.In sys/types.h
39*8269e767SBrooks Davis.In dirent.h
40*8269e767SBrooks Davis.Ft ssize_t
41*8269e767SBrooks Davis.Fn getdirentries "int fd" "char *buf" "size_t nbytes" "off_t *basep"
42*8269e767SBrooks Davis.Ft ssize_t
43*8269e767SBrooks Davis.Fn getdents "int fd" "char *buf" "size_t nbytes"
44*8269e767SBrooks Davis.Sh DESCRIPTION
45*8269e767SBrooks DavisThe
46*8269e767SBrooks Davis.Fn getdirentries
47*8269e767SBrooks Davisand
48*8269e767SBrooks Davis.Fn getdents
49*8269e767SBrooks Davissystem calls read directory entries from the directory
50*8269e767SBrooks Davisreferenced by the file descriptor
51*8269e767SBrooks Davis.Fa fd
52*8269e767SBrooks Davisinto the buffer pointed to by
53*8269e767SBrooks Davis.Fa buf ,
54*8269e767SBrooks Davisin a file system independent format.
55*8269e767SBrooks DavisUp to
56*8269e767SBrooks Davis.Fa nbytes
57*8269e767SBrooks Davisof data will be transferred.
58*8269e767SBrooks DavisThe
59*8269e767SBrooks Davis.Fa nbytes
60*8269e767SBrooks Davisargument must be greater than or equal to the
61*8269e767SBrooks Davisblock size associated with the file,
62*8269e767SBrooks Davissee
63*8269e767SBrooks Davis.Xr stat 2 .
64*8269e767SBrooks DavisSome file systems may not support these system calls
65*8269e767SBrooks Daviswith buffers smaller than this size.
66*8269e767SBrooks Davis.Pp
67*8269e767SBrooks DavisThe data in the buffer is a series of
68*8269e767SBrooks Davis.Vt dirent
69*8269e767SBrooks Davisstructures each containing the following entries:
70*8269e767SBrooks Davis.Bd -literal -offset indent
71*8269e767SBrooks Davisino_t     d_fileno;
72*8269e767SBrooks Davisoff_t     d_off;
73*8269e767SBrooks Davisuint16_t  d_reclen;
74*8269e767SBrooks Davisuint8_t   d_type;
75*8269e767SBrooks Davisuint16_t  d_namlen;
76*8269e767SBrooks Davischar      d_name[MAXNAMLEN + 1];	/* see below */
77*8269e767SBrooks Davis.Ed
78*8269e767SBrooks Davis.Pp
79*8269e767SBrooks DavisThe
80*8269e767SBrooks Davis.Fa d_fileno
81*8269e767SBrooks Davisentry is a number which is unique for each
82*8269e767SBrooks Davisdistinct file in the file system.
83*8269e767SBrooks DavisFiles that are linked by hard links (see
84*8269e767SBrooks Davis.Xr link 2 )
85*8269e767SBrooks Davishave the same
86*8269e767SBrooks Davis.Fa d_fileno .
87*8269e767SBrooks DavisThe
88*8269e767SBrooks Davis.Fa d_off
89*8269e767SBrooks Davisfield returns a cookie which, if non-zero, can be used with
90*8269e767SBrooks Davis.Xr lseek 2
91*8269e767SBrooks Davisto position the directory descriptor to the next entry.
92*8269e767SBrooks DavisThe
93*8269e767SBrooks Davis.Fa d_reclen
94*8269e767SBrooks Davisentry is the length, in bytes, of the directory record.
95*8269e767SBrooks DavisThe
96*8269e767SBrooks Davis.Fa d_type
97*8269e767SBrooks Davisentry is the type of the file pointed to by the directory record.
98*8269e767SBrooks DavisThe file type values are defined in
99*8269e767SBrooks Davis.Fa <sys/dirent.h> .
100*8269e767SBrooks DavisThe
101*8269e767SBrooks Davis.Fa d_name
102*8269e767SBrooks Davisentry contains a null terminated file name.
103*8269e767SBrooks DavisThe
104*8269e767SBrooks Davis.Fa d_namlen
105*8269e767SBrooks Davisentry specifies the length of the file name excluding the null byte.
106*8269e767SBrooks DavisThus the actual size of
107*8269e767SBrooks Davis.Fa d_name
108*8269e767SBrooks Davismay vary from 1 to
109*8269e767SBrooks Davis.Dv MAXNAMLEN
110*8269e767SBrooks Davis\&+ 1.
111*8269e767SBrooks Davis.Pp
112*8269e767SBrooks DavisEntries may be separated by extra space.
113*8269e767SBrooks DavisThe
114*8269e767SBrooks Davis.Fa d_reclen
115*8269e767SBrooks Davisentry may be used as an offset from the start of a
116*8269e767SBrooks Davis.Fa dirent
117*8269e767SBrooks Davisstructure to the next structure, if any.
118*8269e767SBrooks Davis.Pp
119*8269e767SBrooks DavisThe actual number of bytes transferred is returned.
120*8269e767SBrooks DavisThe current position pointer associated with
121*8269e767SBrooks Davis.Fa fd
122*8269e767SBrooks Davisis set to point to the next block of entries.
123*8269e767SBrooks DavisThe pointer may not advance by the number of bytes returned by
124*8269e767SBrooks Davis.Fn getdirentries
125*8269e767SBrooks Davisor
126*8269e767SBrooks Davis.Fn getdents .
127*8269e767SBrooks DavisA value of zero is returned when
128*8269e767SBrooks Davisthe end of the directory has been reached.
129*8269e767SBrooks Davis.Pp
130*8269e767SBrooks DavisIf the
131*8269e767SBrooks Davis.Fa basep
132*8269e767SBrooks Davispointer value is non-NULL,
133*8269e767SBrooks Davisthe
134*8269e767SBrooks Davis.Fn getdirentries
135*8269e767SBrooks Davissystem call writes the position of the block read into the location pointed to by
136*8269e767SBrooks Davis.Fa basep .
137*8269e767SBrooks DavisAlternatively, the current position pointer may be set and retrieved by
138*8269e767SBrooks Davis.Xr lseek 2 .
139*8269e767SBrooks DavisThe current position pointer should only be set to a value returned by
140*8269e767SBrooks Davis.Xr lseek 2 ,
141*8269e767SBrooks Davisa value returned in the location pointed to by
142*8269e767SBrooks Davis.Fa basep
143*8269e767SBrooks Davis.Po Fn getdirentries
144*8269e767SBrooks Davisonly
145*8269e767SBrooks Davis.Pc ,
146*8269e767SBrooks Davisa value returned in the
147*8269e767SBrooks Davis.Fa d_off
148*8269e767SBrooks Davisfield if it is non-zero,
149*8269e767SBrooks Davisor zero.
150*8269e767SBrooks Davis.Sh IMPLEMENTATION NOTES
151*8269e767SBrooks DavisThe
152*8269e767SBrooks Davis.Fa d_off
153*8269e767SBrooks Davisfield is currently set to 0 by the NFS client, since the
154*8269e767SBrooks Davisdirectory offset cookies returned by an NFS server cannot
155*8269e767SBrooks Davisbe used by
156*8269e767SBrooks Davis.Xr lseek 2
157*8269e767SBrooks Davisat this time.
158*8269e767SBrooks Davis.Sh RETURN VALUES
159*8269e767SBrooks DavisIf successful, the number of bytes actually transferred is returned.
160*8269e767SBrooks DavisOtherwise, -1 is returned and the global variable
161*8269e767SBrooks Davis.Va errno
162*8269e767SBrooks Davisis set to indicate the error.
163*8269e767SBrooks Davis.Sh ERRORS
164*8269e767SBrooks DavisThe
165*8269e767SBrooks Davis.Fn getdirentries
166*8269e767SBrooks Davissystem call
167*8269e767SBrooks Daviswill fail if:
168*8269e767SBrooks Davis.Bl -tag -width Er
169*8269e767SBrooks Davis.It Bq Er EBADF
170*8269e767SBrooks DavisThe
171*8269e767SBrooks Davis.Fa fd
172*8269e767SBrooks Davisargument
173*8269e767SBrooks Davisis not a valid file descriptor open for reading.
174*8269e767SBrooks Davis.It Bq Er EFAULT
175*8269e767SBrooks DavisEither
176*8269e767SBrooks Davis.Fa buf
177*8269e767SBrooks Davisor non-NULL
178*8269e767SBrooks Davis.Fa basep
179*8269e767SBrooks Davispoint outside the allocated address space.
180*8269e767SBrooks Davis.It Bq Er EINVAL
181*8269e767SBrooks DavisThe file referenced by
182*8269e767SBrooks Davis.Fa fd
183*8269e767SBrooks Davisis not a directory, or
184*8269e767SBrooks Davis.Fa nbytes
185*8269e767SBrooks Davisis too small for returning a directory entry or block of entries,
186*8269e767SBrooks Davisor the current position pointer is invalid.
187*8269e767SBrooks Davis.It Bq Er EIO
188*8269e767SBrooks DavisAn
189*8269e767SBrooks Davis.Tn I/O
190*8269e767SBrooks Daviserror occurred while reading from or writing to the file system.
191*8269e767SBrooks Davis.It Bq Er EINTEGRITY
192*8269e767SBrooks DavisCorrupted data was detected while reading from the file system.
193*8269e767SBrooks Davis.It Bq Er ENOENT
194*8269e767SBrooks DavisDirectory unlinked but still open.
195*8269e767SBrooks Davis.El
196*8269e767SBrooks Davis.Sh SEE ALSO
197*8269e767SBrooks Davis.Xr lseek 2 ,
198*8269e767SBrooks Davis.Xr open 2 ,
199*8269e767SBrooks Davis.Xr directory 3 ,
200*8269e767SBrooks Davis.Xr dir 5
201*8269e767SBrooks Davis.Sh HISTORY
202*8269e767SBrooks DavisThe
203*8269e767SBrooks Davis.Fn getdirentries
204*8269e767SBrooks Davissystem call first appeared in
205*8269e767SBrooks Davis.Bx 4.4 .
206*8269e767SBrooks DavisThe
207*8269e767SBrooks Davis.Fn getdents
208*8269e767SBrooks Davissystem call first appeared in
209*8269e767SBrooks Davis.Fx 3.0 .
210