1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Update libc/dirent/dir to current coding conventions.

This commit is contained in:
Jonas 'Sortie' Termansen 2013-07-11 18:32:54 +02:00
parent 8707a0d309
commit 290ee1a6a6

View file

@ -17,18 +17,19 @@
You should have received a copy of the GNU Lesser General Public License 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/>. along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/dir.c dirent/dir.cpp
DIR* is an interface allowing various directory backends. DIR* is an interface allowing various directory backends.
*******************************************************************************/ *******************************************************************************/
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <dirent.h>
DIR* firstdir = NULL; static DIR* firstdir = NULL;
void dregister(DIR* dir) void dregister(DIR* dir)
{ {
@ -41,10 +42,14 @@ void dregister(DIR* dir)
void dunregister(DIR* dir) void dunregister(DIR* dir)
{ {
if ( !(dir->flags & _DIR_REGISTERED) ) { return; } if ( !(dir->flags & _DIR_REGISTERED) )
if ( !dir->prev ) { firstdir = dir->next; } return;
if ( dir->prev ) { dir->prev->next = dir->next; } if ( !dir->prev )
if ( dir->next ) { dir->next->prev = 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; dir->flags &= ~_DIR_REGISTERED;
} }
@ -66,7 +71,7 @@ struct dirent* readdir(DIR* dir)
} }
if ( 0 < status ) if ( 0 < status )
{ {
struct dirent* biggerdir = malloc(size); struct dirent* biggerdir = (struct dirent*) malloc(size);
if ( !biggerdir ) if ( !biggerdir )
{ {
dir->flags |= _DIR_ERROR; dir->flags |= _DIR_ERROR;
@ -94,20 +99,22 @@ int closedir(DIR* dir)
int result = (dir->close_func) ? dir->close_func(dir->user) : 0; int result = (dir->close_func) ? dir->close_func(dir->user) : 0;
dunregister(dir); dunregister(dir);
free(dir->entry); free(dir->entry);
if ( dir->free_func ) { dir->free_func(dir); } if ( dir->free_func )
dir->free_func(dir);
return result; return result;
} }
void rewinddir(DIR* dir) void rewinddir(DIR* dir)
{ {
if ( dir->rewind_func ) { dir->rewind_func(dir->user); } if ( dir->rewind_func )
dir->rewind_func(dir->user);
dir->flags &= ~_DIR_EOF; dir->flags &= ~_DIR_EOF;
} }
int dirfd(DIR* dir) int dirfd(DIR* dir)
{ {
if ( !dir->fd_func ) { errno = EBADF; return 0; } if ( !dir->fd_func )
return errno = EBADF, 0;
return dir->fd_func(dir->user); return dir->fd_func(dir->user);
} }
@ -134,7 +141,8 @@ static void dfreedir(DIR* dir)
DIR* dnewdir(void) DIR* dnewdir(void)
{ {
DIR* dir = (DIR*) calloc(sizeof(DIR), 1); DIR* dir = (DIR*) calloc(sizeof(DIR), 1);
if ( !dir ) { return NULL; } if ( !dir )
return NULL;
dir->flags = 0; dir->flags = 0;
dir->free_func = dfreedir; dir->free_func = dfreedir;
dregister(dir); dregister(dir);
@ -144,6 +152,7 @@ DIR* dnewdir(void)
int dcloseall(void) int dcloseall(void)
{ {
int result = 0; int result = 0;
while ( firstdir ) { result |= closedir(firstdir); } while ( firstdir )
return (result) ? EOF : 0; result |= closedir(firstdir);
return result ? EOF : 0;
} }