1.\" $NetBSD: getdents.2,v 1.20 2010/04/14 09:10:30 wiz Exp $ 2.\" 3.\" Copyright (c) 1989, 1991, 1993 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. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)getdirentries.2 8.1 (Berkeley) 6/9/93 31.\" 32.Dd April 14, 2010 33.Dt GETDENTS 2 34.Os 35.Sh NAME 36.Nm getdents 37.Nd "get directory entries in a filesystem independent format" 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In dirent.h 42.Ft int 43.Fn getdents "int fd" "char *buf" "size_t nbytes" 44.Sh DESCRIPTION 45.Fn getdents 46reads directory entries from the directory 47referenced by the file descriptor 48.Fa fd 49into the buffer pointed to by 50.Fa buf , 51in a filesystem independent format. 52Up to 53.Fa nbytes 54of data will be transferred. 55.Fa nbytes 56must be greater than or equal to the 57block size associated with the file, 58see 59.Xr stat 2 . 60Some filesystems may not support 61.Fn getdents 62with buffers smaller than this size. 63.Pp 64The data in the buffer is a series of 65.Em dirent 66structures each containing the following entries: 67.Bd -literal -offset indent 68ino_t d_fileno; 69uint16_t d_reclen; 70uint16_t d_namlen; 71uint8_t d_type; 72char d_name[MAXNAMLEN + 1]; 73.Ed 74.Pp 75These are: 76.Bl -enum -offset indent 77.It 78The 79.Fa d_fileno 80entry is a number which is unique for each 81distinct file in the filesystem. 82Files that are linked by hard links (see 83.Xr link 2 ) 84have the same 85.Fa d_fileno . 86If 87.Fa d_fileno 88is zero, the entry refers to a deleted file. 89.It 90The 91.Fa d_reclen 92entry is the length, in bytes, of the directory record. 93.It 94The 95.Fa d_namlen 96entry specifies the length of the file name excluding the NUL. 97Thus the actual size of 98.Fa d_name 99may vary from 1 to 100.Dv MAXNAMLEN 101\&+ 1. 102.It 103The 104.Fa d_type 105is the type of file, where the following are possible types: 106.Dv DT_UNKNOWN , 107.Dv DT_FIFO , 108.Dv DT_CHR , 109.Dv DT_DIR , 110.Dv DT_BLK , 111.Dv DT_REG , 112.Dv DT_LNK , 113.Dv DT_SOCK , 114and 115.Dv DT_WHT . 116.It 117The 118.Fa d_name 119entry contains a NUL-terminated file name. 120.El 121.Pp 122Entries may be separated by extra space. 123The 124.Fa d_reclen 125entry may be used as an offset from the start of a 126.Fa dirent 127structure to the next structure, if any. 128.Pp 129The actual number of bytes transferred is returned. 130The current position pointer associated with 131.Fa fd 132is set to point to the next block of entries. 133The pointer may not advance by the number of bytes returned by 134.Fn getdents . 135A value of zero is returned when 136the end of the directory has been reached. 137.Pp 138The current position pointer may be set and retrieved by 139.Xr lseek 2 . 140The current position pointer should only be set to a value returned by 141.Xr lseek 2 , 142or zero. 143.Sh RETURN VALUES 144If successful, the number of bytes actually transferred is returned. 145Otherwise, \-1 is returned and the global variable 146.Va errno 147is set to indicate the error. 148.Sh ERRORS 149.Fn getdents 150will fail if: 151.Bl -tag -width "[EINVAL]" 152.It Bq Er EBADF 153.Fa fd 154is not a valid file descriptor open for reading. 155.It Bq Er EFAULT 156Either 157.Fa buf 158points outside the allocated address space. 159.It Bq Er EINVAL 160A directory was being read on NFS, but it was modified on the server while 161it was being read. 162.It Bq Er EIO 163An 164.Tn I/O 165error occurred while reading from or writing to the file system. 166.El 167.Sh SEE ALSO 168.Xr lseek 2 , 169.Xr open 2 , 170.Xr dirent 5 171.Sh HISTORY 172The 173.Fn getdents 174function first appeared in 175.Nx 1.3 . 176