48 lines
902 B
C
48 lines
902 B
C
#ifdef HAVE_CONFIG_H
|
|
#include <config.h>
|
|
#endif
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "../internal/malloc.h"
|
|
#include "../internal/syscall.h"
|
|
#include "dirent_impl.h"
|
|
|
|
/*
|
|
* fdopendir (todo 24): validate the descriptor with SYS_fstat (POSIX
|
|
* requires the fd to name a directory), then allocate the stream state.
|
|
* On failure the descriptor is NOT closed — it stays owned by the caller,
|
|
* exactly as passed in.
|
|
*/
|
|
DIR *
|
|
fdopendir(int fd)
|
|
{
|
|
struct stat st;
|
|
DIR *d;
|
|
|
|
if (syscall_ret(__syscall2(SYS_fstat, fd, (long)&st)) < 0)
|
|
{
|
|
return NULL;
|
|
}
|
|
if (!S_ISDIR(st.st_mode))
|
|
{
|
|
errno = ENOTDIR;
|
|
return NULL;
|
|
}
|
|
d = __libc_malloc(sizeof *d);
|
|
if (d == NULL)
|
|
{
|
|
errno = ENOMEM;
|
|
return NULL;
|
|
}
|
|
d->fd = fd;
|
|
d->buf_pos = 0;
|
|
d->buf_end = 0;
|
|
d->de.d_off = 0;
|
|
return d;
|
|
}
|