Added lseek(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2011-12-26 23:12:12 +01:00
parent d6d404f3f0
commit 648b324385
5 changed files with 46 additions and 4 deletions

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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);
}
}
}

View File

@ -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