libc: Implement dirfd, rewinddir, telldir and seekdir

This commit is contained in:
apio 2022-10-23 14:59:06 +02:00
parent e457b88b04
commit 865018e7f8
2 changed files with 35 additions and 3 deletions

View File

@ -36,6 +36,18 @@ extern "C"
* calls to readdir(). */
struct dirent* readdir(DIR* stream);
/* Returns the file descriptor associated with stream. */
int dirfd(DIR* stream);
/* Positions stream's offset at the start of the directory. */
void rewinddir(DIR* stream);
/* Returns the current offset position for stream. */
long telldir(DIR* stream);
/* Moves stream's read offset to offset, which should be the return value of a previous call to telldir(). */
void seekdir(DIR* stream, long offset);
#ifdef __cplusplus
}
#endif

View File

@ -10,9 +10,9 @@ extern "C"
{
DIR* opendir(const char* path)
{
int dirfd = open(path, O_RDONLY | O_DIRECTORY);
if (dirfd < 0) return NULL;
return fdopendir(dirfd);
int fd = open(path, O_RDONLY | O_DIRECTORY);
if (fd < 0) return NULL;
return fdopendir(fd);
}
DIR* fdopendir(int fd)
@ -50,4 +50,24 @@ extern "C"
strlcpy(result.d_name, ent.name, NAME_MAX);
return &result;
}
int dirfd(DIR* stream)
{
return stream->d_dirfd;
}
void rewinddir(DIR* stream)
{
lseek(stream->d_dirfd, 0, SEEK_SET);
}
long telldir(DIR* stream)
{
return lseek(stream->d_dirfd, 0, SEEK_CUR);
}
void seekdir(DIR* stream, long offset)
{
lseek(stream->d_dirfd, offset, SEEK_SET);
}
}