blob: 6cd14f9f81ad6054a93d25626fb08a3405977984 [file] [log] [blame]
Chris Friedtbc5aff32024-12-26 05:17:08 -05001/*
2 * Copyright (c) 2024 Tenstorrent AI ULC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
Chris Friedtbc5aff32024-12-26 05:17:08 -05007#include "fs_priv.h"
8
9#include <errno.h>
Chris Friedtbc5aff32024-12-26 05:17:08 -050010#include <string.h>
11
12#include <zephyr/fs/fs.h>
Chris Friedtbc5aff32024-12-26 05:17:08 -050013#include <zephyr/posix/dirent.h>
Chris Friedt6b103832025-01-02 12:29:01 -050014#include <zephyr/sys/util.h>
Chris Friedtbc5aff32024-12-26 05:17:08 -050015
16int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
17{
18 int rc;
19 struct fs_dirent de;
20 struct posix_fs_desc *const ptr = dirp;
21
22 if (result == NULL) {
23 return EINVAL;
24 }
25
26 if (entry == NULL) {
27 *result = NULL;
28 return EINVAL;
29 }
30
31 if (dirp == NULL) {
32 *result = NULL;
33 return EBADF;
34 }
35
36 rc = fs_readdir(&ptr->dir, &de);
37 if (rc < 0) {
38 *result = NULL;
39 return -rc;
40 }
41
42 strncpy(entry->d_name, de.name, MIN(sizeof(entry->d_name), sizeof(de.name)));
43 entry->d_name[sizeof(entry->d_name) - 1] = '\0';
44
45 if (entry->d_name[0] == '\0') {
46 *result = NULL;
47 return 0;
48 }
49
50 *result = entry;
51 return 0;
52}