2012-09-28 18:36:46 -04:00
|
|
|
/*******************************************************************************
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2014-01-03 13:33:12 -05:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2014.
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of the Sortix C Library.
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
The Sortix C Library is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
The Sortix C Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2014-01-03 13:33:12 -05:00
|
|
|
dirent/fdopendir.cpp
|
2013-07-10 09:26:01 -04:00
|
|
|
Handles the file descriptor backend for the DIR* API on Sortix.
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2012-09-28 18:36:46 -04:00
|
|
|
*******************************************************************************/
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2013-07-11 12:38:06 -04:00
|
|
|
#include <sys/readdirents.h>
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <assert.h>
|
2013-07-11 12:38:06 -04:00
|
|
|
#include <dirent.h>
|
2014-01-03 12:20:15 -05:00
|
|
|
#include <DIR.h>
|
2013-07-11 12:38:06 -04:00
|
|
|
#include <errno.h>
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2013-07-11 12:38:06 -04:00
|
|
|
#include <stdlib.h>
|
2012-02-12 06:18:06 -05:00
|
|
|
#include <string.h>
|
2013-07-11 12:38:06 -04:00
|
|
|
#include <unistd.h>
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
|
|
|
typedef struct fddir_sortix_struct
|
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
struct kernel_dirent* dirent;
|
|
|
|
struct kernel_dirent* current;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
size_t direntsize;
|
|
|
|
int fd;
|
|
|
|
} fddir_sortix_t;
|
|
|
|
|
2013-07-11 12:38:06 -04:00
|
|
|
static int fddir_sortix_readents(fddir_sortix_t* info)
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
{
|
|
|
|
if ( !info->dirent )
|
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
// Allocate a buffer of at least sizeof(kernel_dirent).
|
|
|
|
info->direntsize = sizeof(struct kernel_dirent) + 4UL;
|
2013-07-11 12:38:06 -04:00
|
|
|
info->dirent = (struct kernel_dirent*) malloc(info->direntsize);
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !info->dirent )
|
|
|
|
return -1;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( readdirents(info->fd, info->dirent, info->direntsize) < 0 )
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( errno != ERANGE )
|
|
|
|
return -1;
|
2014-01-03 16:09:36 -05:00
|
|
|
size_t newdirentsize = sizeof(struct kernel_dirent) + info->dirent->d_namlen + 1;
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( newdirentsize < info->direntsize )
|
|
|
|
newdirentsize *= 2;
|
2013-07-11 12:38:06 -04:00
|
|
|
struct kernel_dirent* newdirent = (struct kernel_dirent*) malloc(newdirentsize);
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !newdirent )
|
|
|
|
return -1;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
free(info->dirent);
|
|
|
|
info->dirent = newdirent;
|
|
|
|
info->direntsize = newdirentsize;
|
|
|
|
return fddir_sortix_readents(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-11 12:38:06 -04:00
|
|
|
static int fddir_sortix_read(void* user, struct dirent* dirent, size_t* size)
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
{
|
|
|
|
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
|
|
|
if ( !info->current )
|
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( fddir_sortix_readents(info) )
|
|
|
|
return -1;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
info->current = info->dirent;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t provided = (user) ? *size : 0;
|
2014-01-03 16:09:36 -05:00
|
|
|
size_t needed = sizeof(struct dirent) + info->current->d_namlen + 1;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
*size = needed;
|
2013-07-11 12:38:06 -04:00
|
|
|
if ( provided < needed )
|
|
|
|
return 1;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2012-09-06 12:08:06 -04:00
|
|
|
dirent->d_reclen = needed;
|
2014-01-03 16:20:52 -05:00
|
|
|
dirent->d_namlen = info->current->d_namlen;
|
|
|
|
dirent->d_ino = info->current->d_ino;
|
|
|
|
dirent->d_dev = info->current->d_dev;
|
|
|
|
dirent->d_type = info->current->d_type;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
strcpy(dirent->d_name, info->current->d_name);
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
info->current = kernel_dirent_next(info->current);
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-11 12:38:06 -04:00
|
|
|
static int fddir_sortix_rewind(void* user)
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
{
|
|
|
|
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
2012-08-07 18:19:44 -04:00
|
|
|
return lseek(info->fd, 0, SEEK_SET);
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
}
|
|
|
|
|
2013-07-11 12:38:06 -04:00
|
|
|
static int fddir_sortix_fd(void* user)
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
{
|
|
|
|
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
|
|
|
return info->fd;
|
|
|
|
}
|
|
|
|
|
2013-07-11 12:38:06 -04:00
|
|
|
static int fddir_sortix_close(void* user)
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
{
|
|
|
|
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
2012-02-12 06:18:06 -05:00
|
|
|
int result = close(info->fd);
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
free(info->dirent);
|
|
|
|
free(info);
|
2012-02-12 06:18:06 -05:00
|
|
|
return result;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
}
|
|
|
|
|
2013-07-11 12:38:06 -04:00
|
|
|
extern "C" DIR* fdopendir(int fd)
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
{
|
2013-07-11 12:38:06 -04:00
|
|
|
fddir_sortix_t* info = (fddir_sortix_t*) calloc(sizeof(fddir_sortix_t), 1);
|
|
|
|
if ( !info )
|
|
|
|
return NULL;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
|
|
|
DIR* dir = dnewdir();
|
2014-01-03 13:33:12 -05:00
|
|
|
if ( !dir )
|
|
|
|
return free(info), (DIR*) NULL;
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int old_dflags = fcntl(fd, F_GETFD);
|
|
|
|
if ( 0 <= old_dflags )
|
|
|
|
fcntl(fd, F_SETFD, old_dflags | O_CLOEXEC);
|
Added readdir(3), closedir(3), rewinddir(3), dirfd(3), fdopendir(3),
opendir(3).
Also added non-standard dregister(3), dunregister(3), dclearerr(3),
derror(3), deof(3), dnewdir(3), dcloseall(3).
2012-01-14 14:54:02 -05:00
|
|
|
|
|
|
|
info->fd = fd;
|
|
|
|
|
|
|
|
dir->read_func = fddir_sortix_read;
|
|
|
|
dir->rewind_func = fddir_sortix_rewind;
|
|
|
|
dir->fd_func = fddir_sortix_fd;
|
|
|
|
dir->close_func = fddir_sortix_close;
|
|
|
|
dir->user = (void*) info;
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|