kernel+libc: Add the write() system call, and fwrite()
This commit is contained in:
parent
92dbe58729
commit
292433dc39
@ -26,3 +26,26 @@ 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;
|
||||
}
|
||||
|
@ -35,7 +35,8 @@ extern "C"
|
||||
/* Read arbitrarily sized items from a stream. */
|
||||
size_t fread(void* buf, size_t size, size_t nmemb, FILE* stream);
|
||||
|
||||
size_t fwrite(const void*, size_t, size_t, FILE*);
|
||||
/* Write arbitrarily sized items to a stream. */
|
||||
size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream);
|
||||
|
||||
int fseek(FILE*, long, int);
|
||||
long ftell(FILE*);
|
||||
|
@ -38,6 +38,9 @@ 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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -66,6 +66,15 @@ 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 ferror(FILE* stream)
|
||||
{
|
||||
return stream->_err;
|
||||
|
@ -54,4 +54,10 @@ 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);
|
||||
}
|
||||
}
|
||||
|
@ -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(close) _e(read) _e(getpid) _e(write)
|
||||
|
||||
enum Syscalls
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user