diff --git a/libc/Makefile b/libc/Makefile index 42b74016..b955dc41 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -173,6 +173,7 @@ mkdir.o \ mktemp.o \ on_exit.o \ open.o \ +openat.o \ pipe.o \ print.o \ putc.o \ diff --git a/libc/open.cpp b/libc/open.cpp index 5fed4ed3..31424240 100644 --- a/libc/open.cpp +++ b/libc/open.cpp @@ -17,8 +17,8 @@ You should have received a copy of the GNU Lesser General Public License along with the Sortix C Library. If not, see . - dup.cpp - Duplicates a file descriptor. + open.cpp + Open a file. *******************************************************************************/ @@ -26,11 +26,11 @@ #include #include -DEFN_SYSCALL3(int, SysOpen, SYSCALL_OPEN, const char*, int, mode_t); +DEFN_SYSCALL3(int, sys_open, SYSCALL_OPEN, const char*, int, mode_t); extern "C" int open(const char* path, int flags, ...) { - int mode = 0; + mode_t mode = 0; if ( flags & O_CREAT ) { va_list ap; @@ -38,5 +38,5 @@ extern "C" int open(const char* path, int flags, ...) mode = va_arg(ap, mode_t); va_end(ap); } - return SysOpen(path, flags, mode); + return sys_open(path, flags, mode); } diff --git a/libc/openat.cpp b/libc/openat.cpp new file mode 100644 index 00000000..58bdb88b --- /dev/null +++ b/libc/openat.cpp @@ -0,0 +1,42 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + 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 . + + openat.cpp + Open a file relative to directory. + +*******************************************************************************/ + +#include +#include +#include + +DEFN_SYSCALL4(int, sys_openat, SYSCALL_OPENAT, int, const char*, int, mode_t); + +extern "C" int openat(int dirfd, const char* path, int flags, ...) +{ + mode_t mode = 0; + if ( flags & O_CREAT ) + { + va_list ap; + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + } + return sys_openat(dirfd, path, flags, mode); +} diff --git a/sortix/filesystem.cpp b/sortix/filesystem.cpp index 22242175..96cd60a2 100644 --- a/sortix/filesystem.cpp +++ b/sortix/filesystem.cpp @@ -1,6 +1,6 @@ -/****************************************************************************** +/******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. This file is part of Sortix. @@ -14,13 +14,13 @@ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License along - with Sortix. If not, see . + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . filesystem.cpp Allows access to stored sequences of bytes in an orderly fashion. -******************************************************************************/ +*******************************************************************************/ #include #include @@ -86,6 +86,22 @@ namespace Sortix return fd; } + int SysOpenAt(int dirfd, const char* pathname, int flags, mode_t mode) + { + if ( pathname[0] == '/' ) + return SysOpen(pathname, flags, mode); + Process* process = CurrentProcess(); + Device* dir = process->descriptors.Get(dirfd); + if ( !dir ) { errno = EBADF; return -1; } + const char* path = process->descriptors.GetPath(dirfd); + char* fullpath = String::Combine(3, path, "/", pathname); + if ( !fullpath ) + return -1; + int ret = SysOpen(fullpath, flags, mode); + delete[] fullpath; + return ret; + } + int SysAccess(const char* pathname, int mode) { int oflags = 0; @@ -216,6 +232,7 @@ namespace Sortix void Init() { Syscall::Register(SYSCALL_OPEN, (void*) SysOpen); + Syscall::Register(SYSCALL_OPENAT, (void*) SysOpenAt); Syscall::Register(SYSCALL_UNLINK, (void*) SysUnlink); Syscall::Register(SYSCALL_MKDIR, (void*) SysMkDir); Syscall::Register(SYSCALL_RMDIR, (void*) SysRmDir); diff --git a/sortix/include/sortix/syscallnum.h b/sortix/include/sortix/syscallnum.h index 5ccd54fb..b7ac1bf1 100644 --- a/sortix/include/sortix/syscallnum.h +++ b/sortix/include/sortix/syscallnum.h @@ -1,6 +1,6 @@ /******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. This file is part of Sortix. @@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License along with Sortix. If not, see . + syscallnum.h Stores numerical constants for each available system call on this kernel. @@ -75,7 +76,7 @@ #define SYSCALL_TFORK 51 #define SYSCALL_TCGETWINSIZE 52 #define SYSCALL_RAISE 53 -#define SYSCALL_MAX_NUM 54 /* index of highest constant + 1 */ +#define SYSCALL_OPENAT 54 +#define SYSCALL_MAX_NUM 55 /* index of highest constant + 1 */ #endif -