From 648b3243859cafa6588ab95785ae25ac80790299 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 26 Dec 2011 23:12:12 +0100 Subject: [PATCH] Added lseek(2). --- libmaxsi/c/decl/SEEK_END.h | 4 ++-- libmaxsi/c/hsrc/unistd.h | 2 +- libmaxsi/io.cpp | 7 +++++++ sortix/io.cpp | 34 ++++++++++++++++++++++++++++++++++ sortix/syscallnum.h | 3 ++- 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/libmaxsi/c/decl/SEEK_END.h b/libmaxsi/c/decl/SEEK_END.h index dc64c8f0..13f1e0f7 100644 --- a/libmaxsi/c/decl/SEEK_END.h +++ b/libmaxsi/c/decl/SEEK_END.h @@ -1,3 +1,3 @@ -#ifndef SEEK_SET -#define SEEK_SET 2 /* Seek from end of file. */ +#ifndef SEEK_END +#define SEEK_END 2 /* Seek from end of file. */ #endif diff --git a/libmaxsi/c/hsrc/unistd.h b/libmaxsi/c/hsrc/unistd.h index 08c164ab..4c8a6edd 100644 --- a/libmaxsi/c/hsrc/unistd.h +++ b/libmaxsi/c/hsrc/unistd.h @@ -120,7 +120,6 @@ int lchown(const char*, uid_t, gid_t); int link(const char*, const char*); int linkat(int, const char*, int, const char*, int); int lockf(int, int, off_t); -off_t lseek(int, off_t, int); int nice(int); long pathconf(const char*, int); int pause(void); @@ -166,6 +165,7 @@ char* getcwd(char*, size_t); pid_t getpid(void); pid_t getppid(void); int isatty(int); +off_t lseek(int, off_t, int); int pipe(int [2]); ssize_t read(int, void*, size_t); unsigned sleep(unsigned); diff --git a/libmaxsi/io.cpp b/libmaxsi/io.cpp index b9e989dc..0dec9a8a 100644 --- a/libmaxsi/io.cpp +++ b/libmaxsi/io.cpp @@ -49,6 +49,7 @@ namespace Maxsi DEFN_SYSCALL2(char*, SysGetCWD, SYSCALL_GETCWD, char*, size_t); DEFN_SYSCALL1(int, SysUnlink, SYSCALL_UNLINK, const char*); DEFN_SYSCALL1(int, SysIsATTY, SYSCALL_ISATTY, int); + DEFN_SYSCALL3_VOID(SysSeek, SYSCALL_SEEK, int, off_t*, int); size_t Print(const char* string) { @@ -151,6 +152,12 @@ namespace Maxsi return 0; } + extern "C" off_t lseek(int fd, off_t offset, int whence) + { + SysSeek(fd, &offset, whence); + return offset; + } + extern "C" int pipe(int pipefd[2]) { return SysPipe(pipefd); diff --git a/sortix/io.cpp b/sortix/io.cpp index c42d9521..e7ee34e1 100644 --- a/sortix/io.cpp +++ b/sortix/io.cpp @@ -117,6 +117,39 @@ namespace Sortix return 0; } +// TODO: Get these from unistd.h or something. +#ifndef SEEK_SET +#define SEEK_SET 0 /* Seek from beginning of file. */ +#endif +#ifndef SEEK_CUR +#define SEEK_CUR 1 /* Seek from current position. */ +#endif +#ifndef SEEK_END +#define SEEK_END 2 /* Seek from end of file. */ +#endif + + void SysSeek(int fd, off_t* offset, int whence) + { + // TODO: Validate that offset is a legal user-space off_t! + Process* process = CurrentProcess(); + Device* dev = process->descriptors.Get(fd); + if ( !dev ) { Error::Set(EBADF); *offset = -1; return; } + if ( !dev->IsType(Device::BUFFER) ) { Error::Set(EBADF); *offset = -1; return; } + DevBuffer* buffer = (DevBuffer*) dev; + off_t origin; + switch ( whence ) + { + case SEEK_SET: origin = 0; break; + case SEEK_CUR: origin = buffer->Position(); break; + case SEEK_END: origin = buffer->Size(); break; + default: Error::Set(EINVAL); *offset = -1; break; + } + off_t newposition = origin + *offset; + if ( newposition < 0 ) { Error::Set(EINVAL); *offset = -1; return; } + if ( !buffer->Seek(newposition) ) { *offset = -1; return; } + *offset = buffer->Position(); + } + int SysClose(int fd) { Process* process = CurrentProcess(); @@ -150,6 +183,7 @@ namespace Sortix Syscall::Register(SYSCALL_CLOSE, (void*) SysClose); Syscall::Register(SYSCALL_DUP, (void*) SysDup); Syscall::Register(SYSCALL_ISATTY, (void*) SysIsATTY); + Syscall::Register(SYSCALL_SEEK, (void*) SysSeek); } } } diff --git a/sortix/syscallnum.h b/sortix/syscallnum.h index 0a7a5179..8e71af3d 100644 --- a/sortix/syscallnum.h +++ b/sortix/syscallnum.h @@ -61,7 +61,8 @@ #define SYSCALL_ISATTY 33 #define SYSCALL_UPTIME 34 #define SYSCALL_SBRK 35 -#define SYSCALL_MAX_NUM 36 /* index of highest constant + 1 */ +#define SYSCALL_SEEK 36 +#define SYSCALL_MAX_NUM 37 /* index of highest constant + 1 */ #endif