]>
Commit | Line | Data |
---|---|---|
320054e8 DG |
1 | // Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/ |
2 | // | |
3 | // SPDX-License-Identifier: BSD-2-Clause | |
4 | ||
446cb3f1 | 5 | #include <wasi/api.h> |
320054e8 DG |
6 | #include <dirent.h> |
7 | #include <errno.h> | |
8 | #include <stdlib.h> | |
9 | ||
10 | #include "dirent_impl.h" | |
11 | ||
12 | DIR *fdopendir(int fd) { | |
13 | // Allocate new directory object and read buffer. | |
14 | DIR *dirp = malloc(sizeof(*dirp)); | |
15 | if (dirp == NULL) | |
16 | return NULL; | |
17 | dirp->buffer = malloc(DIRENT_DEFAULT_BUFFER_SIZE); | |
18 | if (dirp->buffer == NULL) { | |
19 | free(dirp); | |
20 | return NULL; | |
21 | } | |
22 | ||
23 | // Ensure that this is really a directory by already loading the first | |
24 | // chunk of data. | |
25 | __wasi_errno_t error = | |
446cb3f1 DG |
26 | // TODO: Remove the cast on `dirp->buffer` once the witx is updated with char8 support. |
27 | __wasi_fd_readdir(fd, (uint8_t *)dirp->buffer, DIRENT_DEFAULT_BUFFER_SIZE, | |
320054e8 DG |
28 | __WASI_DIRCOOKIE_START, &dirp->buffer_used); |
29 | if (error != 0) { | |
30 | free(dirp->buffer); | |
31 | free(dirp); | |
320054e8 DG |
32 | return NULL; |
33 | } | |
34 | ||
35 | // Initialize other members. | |
36 | dirp->fd = fd; | |
37 | dirp->cookie = __WASI_DIRCOOKIE_START; | |
38 | dirp->buffer_processed = 0; | |
39 | dirp->buffer_size = DIRENT_DEFAULT_BUFFER_SIZE; | |
40 | dirp->dirent = NULL; | |
41 | dirp->dirent_size = 1; | |
42 | return dirp; | |
43 | } |