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
12       Note: There is no glibc wrapper for this system call; see NOTES.
13

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

CONFORMING TO

91       SVr4.
92

NOTES

94       Glibc does not provide a wrapper for this system call;  call  it  using
95       syscall(2).   You  will need to define the linux_dirent structure your‐
96       self.  However, you probably want to use readdir(3) instead.
97
98       This call supersedes readdir(2).
99
100       The original Linux getdents() system call did  not  handle  large  file
101       systems  and  large  file  offsets.  Consequently, Linux 2.4 added get‐
102       dents64(), with wider types for the d_ino and d_off fields employed  in
103       the linux_dirent structure.
104

EXAMPLE

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

SEE ALSO

188       readdir(2), readdir(3)
189

COLOPHON

191       This  page  is  part of release 3.53 of the Linux man-pages project.  A
192       description of the project, and information about reporting  bugs,  can
193       be found at http://www.kernel.org/doc/man-pages/.
194
195
196
197Linux                             2012-08-03                       GETDENTS(2)
Impressum