Compare commits

..

No commits in common. "7e655e320ac520b06e4e9076b6fdfb5a740a1e62" and "92dbe587293674e5fd99d29ae2625f39d8842ea8" have entirely different histories.

8 changed files with 5 additions and 154 deletions

View File

@ -3,9 +3,6 @@
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/seek.h>
#include <luna/SafeArithmetic.h>
#include <sys/types.h>
Result<u64> sys_read(Registers*, SyscallArgs args)
{
@ -29,57 +26,3 @@ Result<u64> sys_read(Registers*, SyscallArgs args)
return nread;
}
Result<u64> sys_write(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
const u8* buf = (const u8*)args[1];
usize size = (usize)args[2];
if (!MemoryManager::validate_user_read(buf, size)) return err(EFAULT);
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
Thread* current = Scheduler::current();
Option<FileDescriptor>& descriptor = current->fd_table->fds[fd];
if (!descriptor.has_value()) return err(EBADF);
usize nwritten = TRY(descriptor->inode->write(buf, descriptor->offset, size));
descriptor->offset += nwritten;
return nwritten;
}
Result<u64> sys_lseek(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
off_t offset = (long)args[1];
int whence = (int)args[2];
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
Thread* current = Scheduler::current();
Option<FileDescriptor>& descriptor = current->fd_table->fds[fd];
if (!descriptor.has_value()) return err(EBADF);
off_t new_offset;
switch (whence)
{
case SEEK_SET: new_offset = offset; break;
case SEEK_CUR: new_offset = TRY(safe_add((long)descriptor->offset, offset)); break;
case SEEK_END: todo();
default: return err(EINVAL);
}
if (new_offset < 0) return err(EINVAL);
descriptor->offset = (usize)new_offset;
return (u64)new_offset;
}

View File

@ -1,10 +0,0 @@
/* bits/seek.h: SEEK_* constants for lseek() and fseek(). */
#ifndef _BITS_SEEK_H
#define _BITS_SEEK_H
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#endif

View File

@ -3,7 +3,6 @@
#ifndef _STDIO_H
#define _STDIO_H
#include <bits/seek.h>
#include <stdarg.h>
#include <sys/types.h>
@ -14,6 +13,7 @@ typedef struct
int _eof;
} FILE;
#define SEEK_SET 0
#define EOF -1
extern FILE* stderr;
@ -35,23 +35,10 @@ extern "C"
/* Read arbitrarily sized items from a stream. */
size_t fread(void* buf, size_t size, size_t nmemb, FILE* stream);
/* Write arbitrarily sized items to a stream. */
size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream);
size_t fwrite(const void*, size_t, size_t, FILE*);
/* Move the file position. Clears the end-of-file indicator on success. */
int fseek(FILE* stream, long offset, int whence);
/* Return the current file position. */
long ftell(FILE* stream);
/* Rewind the file position and clear the error and end-of-file indicators. */
void rewind(FILE* stream);
/* Save the current file position. */
int fgetpos(FILE* stream, fpos_t* pos);
/* Restore a file position. */
int fsetpos(FILE* stream, const fpos_t* pos);
int fseek(FILE*, long, int);
long ftell(FILE*);
/* Return whether the error indicator was set in stream. */
int ferror(FILE* stream);

View File

@ -13,8 +13,5 @@ typedef __i64_t ssize_t;
typedef __i64_t time_t;
typedef __u16_t mode_t;
typedef __u64_t useconds_t;
typedef __i64_t off_t;
typedef off_t fpos_t;
#endif

View File

@ -6,7 +6,6 @@
#define __need_NULL
#include <stddef.h>
#include <bits/seek.h>
#include <stdint.h>
#include <sys/types.h>
@ -39,12 +38,6 @@ extern "C"
/* Read bytes from a file descriptor. */
ssize_t read(int fd, void* buf, size_t size);
/* Write bytes to a file descriptor. */
ssize_t write(int fd, const void* buf, size_t size);
/* Modify a file descriptor's offset. */
off_t lseek(int fd, off_t offset, int whence);
#ifdef __cplusplus
}
#endif

View File

@ -66,53 +66,6 @@ extern "C"
return (size_t)nread / size;
}
size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream)
{
if (size * nmemb == 0) return 0;
ssize_t nwrite = write(stream->_fd, buf, size * nmemb);
return (size_t)nwrite / size;
}
int fseek(FILE* stream, long offset, int whence)
{
long result = lseek(stream->_fd, offset, whence);
if (result < 0) return -1;
// man fseek(3): A successful call to the fseek() function clears the end-of-file indicator for the stream.
stream->_eof = 0;
return 0;
}
long ftell(FILE* stream)
{
return lseek(stream->_fd, 0, SEEK_CUR);
}
void rewind(FILE* stream)
{
lseek(stream->_fd, 0, SEEK_SET);
clearerr(stream);
}
int fgetpos(FILE* stream, fpos_t* pos)
{
long offset = ftell(stream);
if (offset < 0) return -1;
*pos = offset;
return 0;
}
int fsetpos(FILE* stream, const fpos_t* pos)
{
return fseek(stream, *pos, SEEK_SET);
}
int ferror(FILE* stream)
{
return stream->_err;

View File

@ -54,16 +54,4 @@ extern "C"
long rc = syscall(SYS_read, fd, buf, size);
__errno_return(rc, ssize_t);
}
ssize_t write(int fd, const void* buf, size_t size)
{
long rc = syscall(SYS_write, fd, buf, size);
__errno_return(rc, ssize_t);
}
off_t lseek(int fd, off_t offset, int whence)
{
long rc = syscall(SYS_lseek, fd, offset, whence);
__errno_return(rc, off_t);
}
}

View File

@ -2,7 +2,7 @@
#define enumerate_syscalls(_e) \
_e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep) _e(open) \
_e(close) _e(read) _e(getpid) _e(write) _e(lseek)
_e(close) _e(read) _e(getpid)
enum Syscalls
{