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,  ext3,  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

EXAMPLE

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

SEE ALSO

181       readdir(2), readdir(3)
182

COLOPHON

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