PatchworkOS  19e446b
A non-POSIX operating system.
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
7
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}
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
size_t getdents(fd_t fd, dirent_t *buffer, uint64_t count)
System call for reading directory entires.
Definition getdents.c:8
size_t readdir(fd_t fd, dirent_t **buffer, uint64_t *count)
Helper for reading all directory entries.
Definition readdir.c:8
#define NULL
Pointer error value.
Definition NULL.h:25
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ fd_t
File descriptor type.
Definition fd_t.h:10
static atomic_long count
Definition main.c:11
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC void * realloc(void *ptr, size_t size)
Definition realloc.c:13
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
Directory entry struct.
Definition fs.h:455