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
|
|
|
|
2012-09-28 18:36:46 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011.
|
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
|
|
|
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
|
|
|
|
2012-09-28 18:36:46 -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
|
|
|
|
2012-09-28 18:36:46 -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
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2012-09-28 18:36:46 -04:00
|
|
|
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
|
|
|
|
|
|
|
dir.c
|
|
|
|
DIR* is an interface allowing various directory backends.
|
|
|
|
|
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
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
DIR* firstdir = NULL;
|
|
|
|
|
|
|
|
void dregister(DIR* dir)
|
|
|
|
{
|
|
|
|
dir->flags |= _DIR_REGISTERED;
|
|
|
|
if ( !firstdir ) { firstdir = dir; return; }
|
|
|
|
dir->next = firstdir;
|
|
|
|
firstdir->prev = dir;
|
|
|
|
firstdir = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dunregister(DIR* dir)
|
|
|
|
{
|
|
|
|
if ( !(dir->flags & _DIR_REGISTERED) ) { return; }
|
|
|
|
if ( !dir->prev ) { firstdir = dir->next; }
|
|
|
|
if ( dir->prev ) { dir->prev->next = dir->next; }
|
|
|
|
if ( dir->next ) { dir->next->prev = dir->prev; }
|
|
|
|
dir->flags &= ~_DIR_REGISTERED;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent* readdir(DIR* dir)
|
|
|
|
{
|
|
|
|
if ( !dir->read_func )
|
|
|
|
{
|
|
|
|
dir->flags |= _DIR_ERROR;
|
|
|
|
errno = EBADF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size = dir->entrysize;
|
|
|
|
int status = dir->read_func(dir->user, dir->entry, &size);
|
|
|
|
if ( status < 0 )
|
|
|
|
{
|
|
|
|
dir->flags |= _DIR_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ( 0 < status )
|
|
|
|
{
|
|
|
|
struct dirent* biggerdir = malloc(size);
|
|
|
|
if ( !biggerdir )
|
|
|
|
{
|
|
|
|
dir->flags |= _DIR_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
free(dir->entry);
|
|
|
|
dir->entry = biggerdir;
|
|
|
|
dir->entrysize = size;
|
|
|
|
return readdir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
dir->flags &= ~_DIR_ERROR;
|
|
|
|
|
|
|
|
if ( !dir->entry->d_name[0] )
|
|
|
|
{
|
|
|
|
dir->flags |= _DIR_EOF;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-21 19:52:29 -04:00
|
|
|
return dir->entry;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int closedir(DIR* dir)
|
|
|
|
{
|
|
|
|
int result = (dir->close_func) ? dir->close_func(dir->user) : 0;
|
|
|
|
dunregister(dir);
|
|
|
|
free(dir->entry);
|
|
|
|
if ( dir->free_func ) { dir->free_func(dir); }
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewinddir(DIR* dir)
|
|
|
|
{
|
|
|
|
if ( dir->rewind_func ) { dir->rewind_func(dir->user); }
|
|
|
|
dir->flags &= ~_DIR_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dirfd(DIR* dir)
|
|
|
|
{
|
|
|
|
if ( !dir->fd_func ) { errno = EBADF; return 0; }
|
|
|
|
|
|
|
|
return dir->fd_func(dir->user);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dclearerr(DIR* dir)
|
|
|
|
{
|
|
|
|
dir->flags &= ~_DIR_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int derror(DIR* dir)
|
|
|
|
{
|
|
|
|
return dir->flags & _DIR_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int deof(DIR* dir)
|
|
|
|
{
|
|
|
|
return dir->flags & _DIR_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dfreedir(DIR* dir)
|
|
|
|
{
|
|
|
|
free(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR* dnewdir(void)
|
|
|
|
{
|
|
|
|
DIR* dir = (DIR*) calloc(sizeof(DIR), 1);
|
|
|
|
if ( !dir ) { return NULL; }
|
|
|
|
dir->flags = 0;
|
|
|
|
dir->free_func = dfreedir;
|
|
|
|
dregister(dir);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dcloseall(void)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
while ( firstdir ) { result |= closedir(firstdir); }
|
|
|
|
return (result) ? EOF : 0;
|
|
|
|
}
|
|
|
|
|