xref: /freebsd-14.2/lib/libc/sys/getdirentries.2 (revision bb438c08)
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.\"	@(#)getdirentries.2	8.2 (Berkeley) 5/3/95
29.\"
30.Dd September 5, 2023
31.Dt GETDIRENTRIES 2
32.Os
33.Sh NAME
34.Nm getdirentries ,
35.Nm getdents
36.Nd "get directory entries in a file system independent format"
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In sys/types.h
41.In dirent.h
42.Ft ssize_t
43.Fn getdirentries "int fd" "char *buf" "size_t nbytes" "off_t *basep"
44.Ft ssize_t
45.Fn getdents "int fd" "char *buf" "size_t nbytes"
46.Sh DESCRIPTION
47The
48.Fn getdirentries
49and
50.Fn getdents
51system calls read directory entries from the directory
52referenced by the file descriptor
53.Fa fd
54into the buffer pointed to by
55.Fa buf ,
56in a file system independent format.
57Up to
58.Fa nbytes
59of data will be transferred.
60The
61.Fa nbytes
62argument must be greater than or equal to the
63block size associated with the file,
64see
65.Xr stat 2 .
66Some file systems may not support these system calls
67with buffers smaller than this size.
68.Pp
69The data in the buffer is a series of
70.Vt dirent
71structures each containing the following entries:
72.Bd -literal -offset indent
73ino_t     d_fileno;
74off_t     d_off;
75uint16_t  d_reclen;
76uint8_t   d_type;
77uint16_t  d_namlen;
78char      d_name[MAXNAMLEN + 1];	/* see below */
79.Ed
80.Pp
81The
82.Fa d_fileno
83entry is a number which is unique for each
84distinct file in the file system.
85Files that are linked by hard links (see
86.Xr link 2 )
87have the same
88.Fa d_fileno .
89The
90.Fa d_off
91field returns a cookie which, if non-zero, can be used with
92.Xr lseek 2
93to position the directory descriptor to the next entry.
94The
95.Fa d_reclen
96entry is the length, in bytes, of the directory record.
97The
98.Fa d_type
99entry is the type of the file pointed to by the directory record.
100The file type values are defined in
101.Fa <sys/dirent.h> .
102The
103.Fa d_name
104entry contains a null terminated file name.
105The
106.Fa d_namlen
107entry specifies the length of the file name excluding the null byte.
108Thus the actual size of
109.Fa d_name
110may vary from 1 to
111.Dv MAXNAMLEN
112\&+ 1.
113.Pp
114Entries may be separated by extra space.
115The
116.Fa d_reclen
117entry may be used as an offset from the start of a
118.Fa dirent
119structure to the next structure, if any.
120.Pp
121The actual number of bytes transferred is returned.
122The current position pointer associated with
123.Fa fd
124is set to point to the next block of entries.
125The pointer may not advance by the number of bytes returned by
126.Fn getdirentries
127or
128.Fn getdents .
129A value of zero is returned when
130the end of the directory has been reached.
131.Pp
132If the
133.Fa basep
134pointer value is non-NULL,
135the
136.Fn getdirentries
137system call writes the position of the block read into the location pointed to by
138.Fa basep .
139Alternatively, the current position pointer may be set and retrieved by
140.Xr lseek 2 .
141The current position pointer should only be set to a value returned by
142.Xr lseek 2 ,
143a value returned in the location pointed to by
144.Fa basep
145.Po Fn getdirentries
146only
147.Pc ,
148a value returned in the
149.Fa d_off
150field if it is non-zero,
151or zero.
152.Sh IMPLEMENTATION NOTES
153The
154.Fa d_off
155field is currently set to 0 by the NFS client, since the
156directory offset cookies returned by an NFS server cannot
157be used by
158.Xr lseek 2
159at this time.
160.Sh RETURN VALUES
161If successful, the number of bytes actually transferred is returned.
162Otherwise, -1 is returned and the global variable
163.Va errno
164is set to indicate the error.
165.Sh ERRORS
166The
167.Fn getdirentries
168system call
169will fail if:
170.Bl -tag -width Er
171.It Bq Er EBADF
172The
173.Fa fd
174argument
175is not a valid file descriptor open for reading.
176.It Bq Er EFAULT
177Either
178.Fa buf
179or non-NULL
180.Fa basep
181point outside the allocated address space.
182.It Bq Er EINVAL
183The file referenced by
184.Fa fd
185is not a directory, or
186.Fa nbytes
187is too small for returning a directory entry or block of entries,
188or the current position pointer is invalid.
189.It Bq Er EIO
190An
191.Tn I/O
192error occurred while reading from or writing to the file system.
193.It Bq Er EINTEGRITY
194Corrupted data was detected while reading from the file system.
195.It Bq Er ENOENT
196Directory unlinked but still open.
197.El
198.Sh SEE ALSO
199.Xr lseek 2 ,
200.Xr open 2 ,
201.Xr directory 3 ,
202.Xr dir 5
203.Sh HISTORY
204The
205.Fn getdirentries
206system call first appeared in
207.Bx 4.4 .
208The
209.Fn getdents
210system call first appeared in
211.Fx 3.0 .
212