blob: c5c30d0dd6806b1bec2fa8120a3df29aaa201393 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
/*
* Directory access for NOLIBC
* Copyright (C) 2025 Thomas Weißschuh <linux@weissschuh.net>
*/
#ifndef _NOLIBC_DIRENT_H
#define _NOLIBC_DIRENT_H
#include "stdint.h"
#include "types.h"
#include <linux/limits.h>
struct dirent {
ino_t d_ino;
char d_name[NAME_MAX + 1];
};
/* See comment of FILE in stdio.h */
typedef struct {
char dummy[1];
} DIR;
static __attribute__((unused))
DIR *fdopendir(int fd)
{
if (fd < 0) {
SET_ERRNO(EBADF);
return NULL;
}
return (DIR *)(intptr_t)~fd;
}
static __attribute__((unused))
DIR *opendir(const char *name)
{
int fd;
fd = open(name, O_RDONLY);
if (fd == -1)
return NULL;
return fdopendir(fd);
}
static __attribute__((unused))
int closedir(DIR *dirp)
{
intptr_t i = (intptr_t)dirp;
if (i >= 0) {
SET_ERRNO(EBADF);
return -1;
}
return close(~i);
}
static __attribute__((unused))
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
{
char buf[sizeof(struct linux_dirent64) + NAME_MAX + 1];
struct linux_dirent64 *ldir = (void *)buf;
intptr_t i = (intptr_t)dirp;
int fd, ret;
if (i >= 0)
return EBADF;
fd = ~i;
ret = sys_getdents64(fd, ldir, sizeof(buf));
if (ret < 0)
return -ret;
if (ret == 0) {
*result = NULL;
return 0;
}
/*
* getdents64() returns as many entries as fit the buffer.
* readdir() can only return one entry at a time.
* Make sure the non-returned ones are not skipped.
*/
ret = lseek(fd, ldir->d_off, SEEK_SET);
if (ret == -1)
return errno;
entry->d_ino = ldir->d_ino;
/* the destination should always be big enough */
strlcpy(entry->d_name, ldir->d_name, sizeof(entry->d_name));
*result = entry;
return 0;
}
/* make sure to include all global symbols */
#include "nolibc.h"
#endif /* _NOLIBC_DIRENT_H */
|