getopt: update optarg and optind correctly on missing argument
[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 struct dirent *readdir(DIR *dir)
12 {
13         struct dirent *de;
14         
15         if (dir->buf_pos >= dir->buf_end) {
16                 int len = __syscall(SYS_getdents, dir->fd, dir->buf, sizeof dir->buf);
17                 if (len <= 0) {
18                         if (len < 0 && len != -ENOENT) errno = -len;
19                         return 0;
20                 }
21                 dir->buf_end = len;
22                 dir->buf_pos = 0;
23         }
24         de = (void *)(dir->buf + dir->buf_pos);
25         dir->buf_pos += de->d_reclen;
26         dir->tell = de->d_off;
27         return de;
28 }
29
30 LFS64(readdir);