Add fchdir(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2012-10-24 13:06:12 +02:00
parent 7dd59dd2e0
commit 37a4c8f05e
5 changed files with 50 additions and 2 deletions

View File

@ -139,6 +139,7 @@ _exit.o \
_Exit.o \
exit.o \
faccessat.o \
fchdir.o \
fchmod.o \
fcloseall.o \
fcntl.o \

33
libc/fchdir.cpp Normal file
View File

@ -0,0 +1,33 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 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/>.
fchdir.cpp
Changes the current working directory.
*******************************************************************************/
#include <sys/syscall.h>
#include <unistd.h>
DEFN_SYSCALL1(int, sys_fchdir, SYSCALL_FCHDIR, int);
extern "C" int fchdir(int fd)
{
return sys_fchdir(fd);
}

View File

@ -92,7 +92,6 @@ size_t confstr(int, char*, size_t);
char* crypt(const char*, const char*);
char* ctermid(char*);
void encrypt(char [64], int);
int fchdir(int);
int fchown(int, uid_t, gid_t);
int fchownat(int, const char*, uid_t, gid_t, int);
int fdatasync(int);
@ -161,6 +160,7 @@ int execve(const char*, char* const [], char* const []);
int execvp(const char*, char* const []);
pid_t fork(void);
int faccessat(int, const char*, int, int);
int fchdir(int);
int ftruncate(int, off_t);
char* getcwd(char*, size_t);
char* get_current_dir_name(void);

View File

@ -85,6 +85,7 @@
#define SYSCALL_UNLINKAT 61
#define SYSCALL_FACCESSAT 62
#define SYSCALL_MKDIRAT 63
#define SYSCALL_MAX_NUM 64 /* index of highest constant + 1 */
#define SYSCALL_FCHDIR 64
#define SYSCALL_MAX_NUM 65 /* index of highest constant + 1 */
#endif

View File

@ -316,6 +316,18 @@ static ssize_t sys_readdirents(int fd, kernel_dirent* dirent, size_t size/*,
return desc->readdirents(&ctx, dirent, size, 1 /*maxcount*/);
}
static int sys_fchdir(int fd)
{
Process* process = CurrentProcess();
Ref<Descriptor> desc = process->GetDescriptor(fd);
if ( !desc )
return -1;
if ( !S_ISDIR(desc->type) )
return errno = ENOTDIR, -1;
process->SetCWD(desc);
return 0;
}
static int sys_chdir(const char* path)
{
char* pathcopy = GetStringFromUser(path);
@ -442,6 +454,7 @@ void Init()
Syscall::Register(SYSCALL_DUP, (void*) sys_dup);
Syscall::Register(SYSCALL_DUP2, (void*) sys_dup2);
Syscall::Register(SYSCALL_FACCESSAT, (void*) sys_faccessat);
Syscall::Register(SYSCALL_FCHDIR, (void*) sys_fchdir);
Syscall::Register(SYSCALL_FCNTL, (void*) sys_fcntl);
Syscall::Register(SYSCALL_FSTATAT, (void*) sys_fstatat);
Syscall::Register(SYSCALL_FSTAT, (void*) sys_fstat);