PatchworkOS
19e446b
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
readdir.c
Go to the documentation of this file.
1
#include <
stdarg.h
>
2
#include <
stdio.h
>
3
#include <
stdlib.h
>
4
#include <
sys/fs.h
>
5
6
#include "
user/common/syscalls.h
"
7
8
size_t
readdir
(
fd_t
fd,
dirent_t
**
buffer
,
uint64_t
*
count
)
9
{
10
uint64_t
size = 1024 *
sizeof
(
dirent_t
);
11
dirent_t
* dirents =
malloc
(size);
12
if
(dirents ==
NULL
)
13
{
14
return
ERR
;
15
}
16
17
uint64_t
totalRead = 0;
18
while
(1)
19
{
20
uint64_t
bytesRead =
getdents
(fd, (
dirent_t
*)((
uint64_t
)dirents + totalRead), size - totalRead);
21
if
(bytesRead ==
ERR
)
22
{
23
free
(dirents);
24
return
ERR
;
25
}
26
27
if
(bytesRead == 0)
28
{
29
break
;
30
}
31
totalRead += bytesRead;
32
33
if
(size - totalRead <
sizeof
(
dirent_t
))
34
{
35
size *= 2;
36
dirent_t
* newDirents =
realloc
(dirents, size);
37
if
(newDirents ==
NULL
)
38
{
39
free
(dirents);
40
return
ERR
;
41
}
42
dirents = newDirents;
43
}
44
}
45
46
if
(totalRead > 0 && totalRead < size)
47
{
48
dirent_t
* newDirents =
realloc
(dirents, totalRead);
49
if
(newDirents !=
NULL
)
50
{
51
dirents = newDirents;
52
}
53
}
54
55
*
buffer
= dirents;
56
*
count
= totalRead /
sizeof
(
dirent_t
);
57
return
0;
58
}
buffer
EFI_PHYSICAL_ADDRESS buffer
Definition
main.c:237
getdents
size_t getdents(fd_t fd, dirent_t *buffer, uint64_t count)
System call for reading directory entires.
Definition
getdents.c:8
readdir
size_t readdir(fd_t fd, dirent_t **buffer, uint64_t *count)
Helper for reading all directory entries.
Definition
readdir.c:8
NULL
#define NULL
Pointer error value.
Definition
NULL.h:25
ERR
#define ERR
Integer error value.
Definition
ERR.h:17
fd_t
__UINT64_TYPE__ fd_t
File descriptor type.
Definition
fd_t.h:10
fs.h
count
static atomic_long count
Definition
main.c:11
stdarg.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
stdio.h
stdlib.h
realloc
_PUBLIC void * realloc(void *ptr, size_t size)
Definition
realloc.c:13
malloc
_PUBLIC void * malloc(size_t size)
Definition
malloc.c:5
free
_PUBLIC void free(void *ptr)
Definition
free.c:11
dirent_t
Directory entry struct.
Definition
fs.h:455
syscalls.h
src
libstd
user
functions
fs
readdir.c
Generated on Sat Jan 24 2026 10:59:25 for PatchworkOS by
1.9.8