Add openat(2).

This is a rather hacky implementation.
This commit is contained in:
Jonas 'Sortie' Termansen 2012-09-30 13:42:35 +02:00
parent 32f87f461d
commit c9b3002e43
5 changed files with 74 additions and 13 deletions

View File

@ -173,6 +173,7 @@ mkdir.o \
mktemp.o \
on_exit.o \
open.o \
openat.o \
pipe.o \
print.o \
putc.o \

View File

@ -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 <http://www.gnu.org/licenses/>.
dup.cpp
Duplicates a file descriptor.
open.cpp
Open a file.
*******************************************************************************/
@ -26,11 +26,11 @@
#include <fcntl.h>
#include <stdarg.h>
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);
}

42
libc/openat.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
openat.cpp
Open a file relative to directory.
*******************************************************************************/
#include <sys/syscall.h>
#include <fcntl.h>
#include <stdarg.h>
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);
}

View File

@ -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 <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>.
filesystem.cpp
Allows access to stored sequences of bytes in an orderly fashion.
******************************************************************************/
*******************************************************************************/
#include <sortix/kernel/platform.h>
#include <sortix/kernel/string.h>
@ -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);

View File

@ -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 <http://www.gnu.org/licenses/>.
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