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:20:51 -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:20:51 -05:00
|
|
|
dirent/readdir.cpp
|
|
|
|
Reads a directory entry from a directory stream into a DIR-specific buffer.
|
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:32:54 -04:00
|
|
|
#include <dirent.h>
|
2014-01-03 12:20:15 -05:00
|
|
|
#include <DIR.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>
|
2014-01-03 13:20:51 -05:00
|
|
|
#include <stddef.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 <stdlib.h>
|
|
|
|
|
2014-01-03 12:48:43 -05:00
|
|
|
extern "C" struct dirent* readdir(DIR* 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
|
|
|
{
|
2014-02-23 10:41:34 -05:00
|
|
|
int old_errno = errno;
|
|
|
|
|
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 ( !dir->read_func )
|
2014-02-23 10:41:34 -05:00
|
|
|
return dir->flags |= _DIR_ERROR, errno = EBADF, (struct dirent*) 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
|
|
|
|
|
|
|
size_t size = dir->entrysize;
|
2014-02-23 10:41:34 -05:00
|
|
|
int status;
|
|
|
|
while ( 0 < (status = dir->read_func(dir->user, dir->entry, &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
|
|
|
{
|
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 )
|
2014-02-23 10:41:34 -05:00
|
|
|
return dir->flags |= _DIR_ERROR, (struct dirent*) 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
|
|
|
free(dir->entry);
|
|
|
|
dir->entry = biggerdir;
|
|
|
|
dir->entrysize = size;
|
|
|
|
}
|
|
|
|
|
2014-02-23 10:41:34 -05:00
|
|
|
if ( status < 0 )
|
|
|
|
return dir->flags |= _DIR_ERROR, (struct dirent*) 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 &= ~_DIR_ERROR;
|
|
|
|
|
|
|
|
if ( !dir->entry->d_name[0] )
|
2014-02-23 10:41:34 -05:00
|
|
|
return dir->flags |= _DIR_EOF, errno = old_errno, (struct dirent*) 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-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
|
|
|
}
|