1 // Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
3 // SPDX-License-Identifier: BSD-2-Clause
5 #include <common/errno.h>
12 #include "dirent_impl.h"
14 DIR *fdopendir(int fd
) {
15 // Allocate new directory object and read buffer.
16 DIR *dirp
= malloc(sizeof(*dirp
));
19 dirp
->buffer
= malloc(DIRENT_DEFAULT_BUFFER_SIZE
);
20 if (dirp
->buffer
== NULL
) {
25 // Ensure that this is really a directory by already loading the first
27 __wasi_errno_t error
=
28 // TODO: Remove the cast on `dirp->buffer` once the witx is updated with char8 support.
29 __wasi_fd_readdir(fd
, (uint8_t *)dirp
->buffer
, DIRENT_DEFAULT_BUFFER_SIZE
,
30 __WASI_DIRCOOKIE_START
, &dirp
->buffer_used
);
34 errno
= errno_fixup_directory(fd
, error
);
38 // Initialize other members.
40 dirp
->cookie
= __WASI_DIRCOOKIE_START
;
41 dirp
->buffer_processed
= 0;
42 dirp
->buffer_size
= DIRENT_DEFAULT_BUFFER_SIZE
;
44 dirp
->dirent_size
= 1;