fix regression in alignment of dirent structs produced by readdir
[musl] / src / dirent / readdir.c
1 #include <dirent.h>
2 #include <errno.h>
3 #include <stddef.h>
4 #include "__dirent.h"
5 #include "syscall.h"
6 #include "libc.h"
7
8 typedef char dirstream_buf_alignment_check[1-2*(int)(
9         offsetof(struct __dirstream, buf) % sizeof(off_t))];
10
11 int __getdents(int, struct dirent *, size_t);
12
13 struct dirent *readdir(DIR *dir)
14 {
15         struct dirent *de;
16         
17         if (dir->buf_pos >= dir->buf_end) {
18                 int len = __syscall(SYS_getdents, dir->fd, dir->buf, sizeof dir->buf);
19                 if (len <= 0) {
20                         if (len < 0 && len != -ENOENT) errno = -len;
21                         return 0;
22                 }
23                 dir->buf_end = len;
24                 dir->buf_pos = 0;
25         }
26         de = (void *)(dir->buf + dir->buf_pos);
27         dir->buf_pos += de->d_reclen;
28         dir->tell = de->d_off;
29         return de;
30 }
31
32 LFS64(readdir);