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