diff --git a/libc/Makefile b/libc/Makefile index 2b4a2489..689a0ab6 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -228,7 +228,8 @@ $(CPUDIR)/fork.o \ $(CPUDIR)/setjmp.o \ $(CPUDIR)/signal.o \ $(CPUDIR)/syscall.o \ -dirent/fddir-sortix.o \ +dirent/fdopendir.o \ +dirent/opendir.o \ dirent/scandir.o \ dlfcn/dlfcn.o \ error/gnu_error.o \ diff --git a/libc/dirent/fddir-sortix.cpp b/libc/dirent/fdopendir.cpp similarity index 91% rename from libc/dirent/fddir-sortix.cpp rename to libc/dirent/fdopendir.cpp index 62c57bf2..c5fc1fe4 100644 --- a/libc/dirent/fddir-sortix.cpp +++ b/libc/dirent/fdopendir.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2014. This file is part of the Sortix C Library. @@ -17,7 +17,7 @@ You should have received a copy of the GNU Lesser General Public License along with the Sortix C Library. If not, see . - dirent/fddir-sortix.cpp + dirent/fdopendir.cpp Handles the file descriptor backend for the DIR* API on Sortix. *******************************************************************************/ @@ -126,7 +126,8 @@ extern "C" DIR* fdopendir(int fd) return NULL; DIR* dir = dnewdir(); - if ( !dir ) { free(info); return NULL; } + if ( !dir ) + return free(info), (DIR*) NULL; int old_dflags = fcntl(fd, F_GETFD); if ( 0 <= old_dflags ) @@ -142,12 +143,3 @@ extern "C" DIR* fdopendir(int fd) return dir; } - -extern "C" DIR* opendir(const char* path) -{ - int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC); - if ( fd < 0 ) { return NULL; } - DIR* dir = fdopendir(fd); - if ( !dir ) { close(fd); return NULL; } - return dir; -} diff --git a/libc/dirent/opendir.cpp b/libc/dirent/opendir.cpp new file mode 100644 index 00000000..0758f53b --- /dev/null +++ b/libc/dirent/opendir.cpp @@ -0,0 +1,39 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2014. + + This file is part of the Sortix C Library. + + 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. + + 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. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + dirent/opendir.cpp + Opens a stream for the directory specified by the path. + +*******************************************************************************/ + +#include +#include +#include +#include + +extern "C" DIR* opendir(const char* path) +{ + int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if ( fd < 0 ) + return NULL; + DIR* dir = fdopendir(fd); + if ( !dir ) + return close(fd), (DIR*) NULL; + return dir; +}