From 4b2c22d48085c93869e6f72e1c5af5f804e7cbb2 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 5 Mar 2012 15:46:23 +0100 Subject: [PATCH] Added access(2). --- libmaxsi/include/unistd.h | 3 ++- libmaxsi/io.cpp | 6 ++++++ sortix/Makefile | 1 + sortix/filesystem.cpp | 22 +++++++++++++++++++++ sortix/syscallnum.h | 3 ++- sortix/unistd.h | 40 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 sortix/unistd.h diff --git a/libmaxsi/include/unistd.h b/libmaxsi/include/unistd.h index e9ee920d..c1252cf8 100644 --- a/libmaxsi/include/unistd.h +++ b/libmaxsi/include/unistd.h @@ -30,6 +30,7 @@ #include #include +#include #define _SORTIX_ALWAYS_SBRK @@ -79,7 +80,6 @@ __BEGIN_DECLS /* TODO: These are not implemented in libmaxsi/sortix yet. */ #if defined(__SORTIX_SHOW_UNIMPLEMENTED) -int access(const char*, int); unsigned alarm(unsigned); int chown(const char*, uid_t, gid_t); size_t confstr(int, char*, size_t); @@ -150,6 +150,7 @@ extern char* optarg; extern int opterr, optind, optopt; #endif +int access(const char*, int); int chdir(const char*); int close(int); int dup(int); diff --git a/libmaxsi/io.cpp b/libmaxsi/io.cpp index fa27b7b5..2980104e 100644 --- a/libmaxsi/io.cpp +++ b/libmaxsi/io.cpp @@ -58,6 +58,7 @@ namespace Maxsi DEFN_SYSCALL2(int, SysStat, SYSCALL_STAT, const char*, struct stat*); DEFN_SYSCALL2(int, SysFStat, SYSCALL_FSTAT, int, struct stat*); DEFN_SYSCALL3(int, SysFCntl, SYSCALL_FCNTL, int, int, unsigned long); + DEFN_SYSCALL2(int, SysAccess, SYSCALL_ACCESS, const char*, int); size_t Print(const char* string) { @@ -299,6 +300,11 @@ namespace Maxsi return SysFCntl(fd, cmd, arg); } + extern "C" int access(const char* pathname, int mode) + { + return SysAccess(pathname, mode); + } + // TODO: Implement these in the kernel. extern "C" int chmod(const char* path, mode_t mode) { diff --git a/sortix/Makefile b/sortix/Makefile index 02f5c1ca..28250c4c 100644 --- a/sortix/Makefile +++ b/sortix/Makefile @@ -90,6 +90,7 @@ termmode.h \ syscallnum.h \ stat.h \ timeval.h \ +unistd.h \ OBJS=$(CPUOBJS) \ kernel.o \ diff --git a/sortix/filesystem.cpp b/sortix/filesystem.cpp index e16b2d84..e46c0696 100644 --- a/sortix/filesystem.cpp +++ b/sortix/filesystem.cpp @@ -32,6 +32,7 @@ #include "directory.h" #include "mount.h" #include +#include using namespace Maxsi; @@ -83,6 +84,26 @@ namespace Sortix return fd; } + int SysAccess(const char* pathname, int mode) + { + int oflags = 0; + bool exec = mode & X_OK; + bool read = mode & R_OK; + bool write = mode & W_OK; + if ( mode == F_OK ) { oflags = O_RDONLY; } + if ( exec && !read && !write ) { oflags = O_EXEC; } + if ( exec && read && !write ) { oflags = O_EXEC; } + if ( exec && !read && write ) { oflags = O_EXEC; } + if ( exec && read && write ) { oflags = O_EXEC; } + if ( !exec && read && write ) { oflags = O_RDWR; } + if ( !exec && !read && write ) { oflags = O_WRONLY; } + if ( !exec && read && !write ) { oflags = O_RDONLY; } + Device* dev = Open(pathname, oflags, 0); + if ( !dev ) { return -1; } + dev->Unref(); + return 0; + } + int SysUnlink(const char* path) { return Unlink(path) ? 0 : -1; @@ -156,6 +177,7 @@ namespace Sortix Syscall::Register(SYSCALL_STAT, (void*) SysStat); Syscall::Register(SYSCALL_FSTAT, (void*) SysFStat); Syscall::Register(SYSCALL_FCNTL, (void*) SysFCntl); + Syscall::Register(SYSCALL_ACCESS, (void*) SysAccess); } } diff --git a/sortix/syscallnum.h b/sortix/syscallnum.h index a59b8dbf..c0472716 100644 --- a/sortix/syscallnum.h +++ b/sortix/syscallnum.h @@ -71,7 +71,8 @@ #define SYSCALL_STAT 44 #define SYSCALL_FSTAT 45 #define SYSCALL_FCNTL 46 -#define SYSCALL_MAX_NUM 47 /* index of highest constant + 1 */ +#define SYSCALL_ACCESS 47 +#define SYSCALL_MAX_NUM 48 /* index of highest constant + 1 */ #endif diff --git a/sortix/unistd.h b/sortix/unistd.h new file mode 100644 index 00000000..a163fcaf --- /dev/null +++ b/sortix/unistd.h @@ -0,0 +1,40 @@ +/******************************************************************************* + + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012. + + This file is part of Sortix. + + Sortix is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + Sortix 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 General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . + + unistd.h + Standard symbolic constants and types. + +*******************************************************************************/ + +#ifndef SORTIX_UNISTD_H +#define SORTIX_UNISTD_H + +#include + +__BEGIN_DECLS + +#define R_OK 4 /* Test for read permission. */ +#define W_OK 2 /* Test for write permission. */ +#define X_OK 1 /* Test for execute permission. */ +#define F_OK 0 /* Test for existence. */ + +__END_DECLS + +#endif +