1GETDENTS(2)                Linux Programmer's Manual               GETDENTS(2)
2
3
4

NAME

6       getdents - get directory entries
7

SYNOPSIS

9       int getdents(unsigned int fd, struct linux_dirent *dirp,
10                    unsigned int count);
11

DESCRIPTION

13       This is not the function you are interested in.  Look at readdir(3) for
14       the POSIX conforming C library interface.  This page documents the bare
15       kernel system call interface.
16
17       The  system  call getdents() reads several linux_dirent structures from
18       the directory referred to by the open file descriptor fd into the  buf‐
19       fer  pointed to by dirp.  The argument count specifies the size of that
20       buffer.
21
22       The linux_dirent structure is declared as follows:
23
24           struct linux_dirent {
25               unsigned long  d_ino;     /* Inode number */
26               unsigned long  d_off;     /* Offset to next linux_dirent */
27               unsigned short d_reclen;  /* Length of this linux_dirent */
28               char           d_name[];  /* Filename (null-terminated) */
29                                   /* length is actually (d_reclen - 2 -
30                                      offsetof(struct linux_dirent, d_name) */
31               /*
32               char           pad;       // Zero padding byte */
33               char           d_type;    // File type (only since Linux 2.6.4;
34                                         // offset is (d_reclen - 1))
35               */
36
37           }
38
39       d_ino is an inode number.  d_off is the distance from the start of  the
40       directory  to the start of the next linux_dirent.  d_reclen is the size
41       of this entire linux_dirent.  d_name is a null-terminated filename.
42
43       d_type is a byte at the end of the structure that  indicates  the  file
44       type.  It contains one of the following values (defined in <dirent.h>):
45
46       DT_BLK      This is a block device.
47
48       DT_CHR      This is a character device.
49
50       DT_DIR      This is a directory.
51
52       DT_FIFO     This is a named pipe (FIFO).
53
54       DT_LNK      This is a symbolic link.
55
56       DT_REG      This is a regular file.
57
58       DT_SOCK     This is a Unix domain socket.
59
60       DT_UNKNOWN  The file type is unknown.
61
62       The d_type field is implemented since Linux 2.6.4.  It occupies a space
63       that was previously a zero-filled  padding  byte  in  the  linux_dirent
64       structure.   Thus,  on  kernels before 2.6.3, attempting to access this
65       field always provides the value 0 (DT_UNKNOWN).
66
67       Currently, only some file systems (among them: Btrfs, ext2,  etx3,  and
68       ext4)  have  full  support  for returning the file type in d_type.  All
69       applications must properly handle a return of DT_UNKNOWN.
70

RETURN VALUE

72       On success, the number of bytes read is returned.  On end of directory,
73       0  is  returned.   On error, -1 is returned, and errno is set appropri‐
74       ately.
75

ERRORS

77       EBADF  Invalid file descriptor fd.
78
79       EFAULT Argument points outside the calling process's address space.
80
81       EINVAL Result buffer is too small.
82
83       ENOENT No such directory.
84
85       ENOTDIR
86              File descriptor does not refer to a directory.
87

CONFORMING TO

89       SVr4.
90

NOTES

92       Glibc does not provide a wrapper for this system call;  call  it  using
93       syscall(2).   You  will need to define the linux_dirent structure your‐
94       self.
95
96       This call supersedes readdir(2).
97
98       Warning: Result of the getdents() system call in 32 bit application  on
99       64  bit  OS  doesn't  have  to  be always correct, potentially the call
100       itself can fail.
101

EXAMPLE

103       The program below demonstrates the use of  getdents().   The  following
104       output  shows an example of what we see when running this program on an
105       ext2 directory:
106
107           $ ./a.out /testfs/
108           --------------- nread=120 ---------------
109           i-node#  file type  d_reclen  d_off   d_name
110                  2  directory    16         12  .
111                  2  directory    16         24  ..
112                 11  directory    24         44  lost+found
113                 12  regular      16         56  a
114             228929  directory    16         68  sub
115              16353  directory    16         80  sub2
116             130817  directory    16       4096  sub3
117
118   Program source
119
120       #define _GNU_SOURCE
121       #include <dirent.h>     /* Defines DT_* constants */
122       #include <fcntl.h>
123       #include <stdio.h>
124       #include <unistd.h>
125       #include <stdlib.h>
126       #include <sys/stat.h>
127       #include <sys/syscall.h>
128
129       #define handle_error(msg) \
130               do { perror(msg); exit(EXIT_FAILURE); } while (0)
131
132       struct linux_dirent {
133           long           d_ino;
134           off_t          d_off;
135           unsigned short d_reclen;
136           char           d_name[];
137       };
138
139       #define BUF_SIZE 1024
140
141       int
142       main(int argc, char *argv[])
143       {
144           int fd, nread;
145           char buf[BUF_SIZE];
146           struct linux_dirent *d;
147           int bpos;
148           char d_type;
149
150           fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
151           if (fd == -1)
152               handle_error("open");
153
154           for ( ; ; ) {
155               nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
156               if (nread == -1)
157                   handle_error("getdents");
158
159               if (nread == 0)
160                   break;
161
162               printf("--------------- nread=%d ---------------\n", nread);
163               printf("i-node#  file type  d_reclen  d_off   d_name\n");
164               for (bpos = 0; bpos < nread;) {
165                   d = (struct linux_dirent *) (buf + bpos);
166                   printf("%8ld  ", d->d_ino);
167                   d_type = *(buf + bpos + d->d_reclen - 1);
168                   printf("%-10s ", (d_type == DT_REG) ?  "regular" :
169                                    (d_type == DT_DIR) ?  "directory" :
170                                    (d_type == DT_FIFO) ? "FIFO" :
171                                    (d_type == DT_SOCK) ? "socket" :
172                                    (d_type == DT_LNK) ?  "symlink" :
173                                    (d_type == DT_BLK) ?  "block dev" :
174                                    (d_type == DT_CHR) ?  "char dev" : "???");
175                   printf("%4d %10lld  %s\n", d->d_reclen,
176                           (long long) d->d_off, (char *) d->d_name);
177                   bpos += d->d_reclen;
178               }
179           }
180
181           exit(EXIT_SUCCESS);
182       }
183

SEE ALSO

185       readdir(2), readdir(3)
186

COLOPHON

188       This page is part of release 3.22 of the Linux  man-pages  project.   A
189       description  of  the project, and information about reporting bugs, can
190       be found at http://www.kernel.org/doc/man-pages/.
191
192
193
194Linux                             2009-07-04                       GETDENTS(2)
Impressum