summaryrefslogtreecommitdiff
path: root/userland/libc/dirent/readdir.c
blob: a7f0d66bfb7879f223990beb38a373f0b77345d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <dirent.h>
#include <unistd.h>

int readdir_multi(DIR *dir, struct dirent *entries, int num_entries) {
  size_t offset = dir->dir_num * sizeof(struct dirent);
  int rc;
  if (-1 == (rc = pread(dir->fd, entries, num_entries * sizeof(struct dirent),
                        offset))) {
    return -1;
  }

  int num_read_entries = rc / sizeof(struct dirent);
  dir->dir_num += num_read_entries;
  return num_read_entries;
}

struct dirent *readdir(DIR *dir) {
  size_t offset = dir->dir_num * sizeof(struct dirent);
  int rc;
  if (-1 == (rc = pread(dir->fd, &dir->internal_direntry, sizeof(struct dirent),
                        offset))) {
    return NULL;
  }
  if (rc < (int)sizeof(struct dirent)) {
    return NULL;
  }

  dir->dir_num++;
  return &(dir->internal_direntry);
}