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 #ifdef __wasilibc_unmodified_upstream
29 __wasi_file_readdir(fd
, dirp
->buffer
, DIRENT_DEFAULT_BUFFER_SIZE
,
31 // TODO: Remove the cast on `dirp->buffer` once the witx is updated with char8 support.
32 __wasi_fd_readdir(fd
, (uint8_t *)dirp
->buffer
, DIRENT_DEFAULT_BUFFER_SIZE
,
34 __WASI_DIRCOOKIE_START
, &dirp
->buffer_used
);
38 errno
= errno_fixup_directory(fd
, error
);
42 // Initialize other members.
44 dirp
->cookie
= __WASI_DIRCOOKIE_START
;
45 dirp
->buffer_processed
= 0;
46 dirp
->buffer_size
= DIRENT_DEFAULT_BUFFER_SIZE
;
48 dirp
->dirent_size
= 1;