2023-03-28 23:06:57 +00:00
|
|
|
#include <bits/getdents.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
DIR* opendir(const char* path)
|
|
|
|
{
|
|
|
|
DIR* dp = (DIR*)malloc(sizeof(DIR));
|
|
|
|
if (!dp) { return nullptr; }
|
|
|
|
|
2023-03-29 20:23:52 +00:00
|
|
|
int fd = open(path, O_RDONLY | O_DIRECTORY);
|
2023-03-28 23:06:57 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
free(dp);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-07-31 08:58:06 +00:00
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
2023-03-28 23:06:57 +00:00
|
|
|
dp->_fd = fd;
|
|
|
|
|
|
|
|
return dp;
|
|
|
|
}
|
|
|
|
|
2023-04-28 19:15:41 +00:00
|
|
|
DIR* fdopendir(int fd)
|
|
|
|
{
|
|
|
|
DIR* dp = (DIR*)malloc(sizeof(DIR));
|
|
|
|
if (!dp) { return nullptr; }
|
|
|
|
|
|
|
|
dp->_fd = fd;
|
|
|
|
|
|
|
|
return dp;
|
|
|
|
}
|
|
|
|
|
2023-03-28 23:06:57 +00:00
|
|
|
struct dirent* readdir(DIR* stream)
|
|
|
|
{
|
|
|
|
// FIXME: Very hackish, right now luna_dirent and dirent have the same layout.
|
|
|
|
static luna_dirent ent;
|
|
|
|
|
|
|
|
long rc = syscall(SYS_getdents, stream->_fd, &ent, 1);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
2023-04-07 10:02:49 +00:00
|
|
|
errno = (int)-rc;
|
2023-03-28 23:06:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// End-of-file (no more directory entries)
|
|
|
|
if (rc < 1) return nullptr;
|
|
|
|
|
|
|
|
return (struct dirent*)&ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
int closedir(DIR* stream)
|
|
|
|
{
|
|
|
|
if (close(stream->_fd) < 0) return -1;
|
|
|
|
|
|
|
|
free(stream);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-04-28 19:15:41 +00:00
|
|
|
|
|
|
|
int dirfd(DIR* stream)
|
|
|
|
{
|
|
|
|
return stream->_fd;
|
|
|
|
}
|
2023-04-28 20:41:44 +00:00
|
|
|
|
|
|
|
void rewinddir(DIR* stream)
|
|
|
|
{
|
|
|
|
lseek(stream->_fd, 0, SEEK_SET);
|
|
|
|
}
|
2023-03-28 23:06:57 +00:00
|
|
|
}
|