Chris Friedt | bc5aff3 | 2024-12-26 05:17:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2024 Tenstorrent AI ULC |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
Chris Friedt | bc5aff3 | 2024-12-26 05:17:08 -0500 | [diff] [blame] | 7 | #include "fs_priv.h" |
| 8 | |
| 9 | #include <errno.h> |
Chris Friedt | bc5aff3 | 2024-12-26 05:17:08 -0500 | [diff] [blame] | 10 | #include <string.h> |
| 11 | |
| 12 | #include <zephyr/fs/fs.h> |
Chris Friedt | bc5aff3 | 2024-12-26 05:17:08 -0500 | [diff] [blame] | 13 | #include <zephyr/posix/dirent.h> |
Chris Friedt | 6b10383 | 2025-01-02 12:29:01 -0500 | [diff] [blame] | 14 | #include <zephyr/sys/util.h> |
Chris Friedt | bc5aff3 | 2024-12-26 05:17:08 -0500 | [diff] [blame] | 15 | |
| 16 | int 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 | } |