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-10 09:26:01 -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
|
|
|
|
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
|
|
|
|
2013-07-11 12:32:54 -04:00
|
|
|
dirent/dir.cpp
|
2013-07-10 09:26:01 -04:00
|
|
|
DIR* is an interface allowing various directory backends.
|
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
|
|
|
|
|
|
|
#include <sys/types.h>
|
2013-07-11 12:32:54 -04:00
|
|
|
|
|
|
|
#include <dirent.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 <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-07-11 12:32:54 -04:00
|
|
|
static DIR* firstdir = 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
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-07-11 12:32:54 -04:00
|
|
|
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;
|
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->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 )
|
|
|
|
{
|
2013-07-11 12:32:54 -04:00
|
|
|
struct dirent* biggerdir = (struct dirent*) malloc(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
|
|
|
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);
|
2013-07-11 12:32:54 -04:00
|
|
|
if ( dir->free_func )
|
|
|
|
dir->free_func(dir);
|
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 result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewinddir(DIR* dir)
|
|
|
|
{
|
2013-07-11 12:32:54 -04:00
|
|
|
if ( dir->rewind_func )
|
|
|
|
dir->rewind_func(dir->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
|
|
|
dir->flags &= ~_DIR_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dirfd(DIR* dir)
|
|
|
|
{
|
2013-07-11 12:32:54 -04:00
|
|
|
if ( !dir->fd_func )
|
|
|
|
return errno = EBADF, 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
|
|
|
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);
|
2013-07-11 12:32:54 -04:00
|
|
|
if ( !dir )
|
|
|
|
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->flags = 0;
|
|
|
|
dir->free_func = dfreedir;
|
|
|
|
dregister(dir);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dcloseall(void)
|
|
|
|
{
|
|
|
|
int result = 0;
|
2013-07-11 12:32:54 -04:00
|
|
|
while ( firstdir )
|
|
|
|
result |= closedir(firstdir);
|
|
|
|
return result ? EOF : 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
|
|
|
}
|