xref: /third_party/node/test/wasi/c/readdir.c (revision 1cb0ef41)
1#include <assert.h>
2#include <dirent.h>
3#include <errno.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8int main() {
9  DIR* dir;
10  struct dirent* entry;
11  char* platform;
12  int cnt;
13  int has_d_type;
14
15  platform = getenv("NODE_PLATFORM");
16  assert(platform != NULL);
17  has_d_type = (0 != strcmp(platform, "aix") && 0 != strcmp(platform, "sunos"));
18
19  dir = opendir("/sandbox");
20  assert(dir != NULL);
21
22  cnt = 0;
23  errno = 0;
24  while (NULL != (entry = readdir(dir))) {
25    if (strcmp(entry->d_name, "input.txt") == 0 ||
26        strcmp(entry->d_name, "input2.txt") == 0 ||
27        strcmp(entry->d_name, "notadir") == 0) {
28      if (has_d_type) {
29        assert(entry->d_type == DT_REG);
30      } else {
31        assert(entry->d_type == DT_UNKNOWN);
32      }
33    } else if (strcmp(entry->d_name, "subdir") == 0) {
34      if (has_d_type) {
35        assert(entry->d_type == DT_DIR);
36      } else {
37        assert(entry->d_type == DT_UNKNOWN);
38      }
39    } else {
40      assert("unexpected file");
41    }
42
43    cnt++;
44  }
45
46  assert(errno == 0);
47  assert(cnt == 4);
48  closedir(dir);
49  return 0;
50}
51