From 4d4af78afea9713942fb1f7dc7aa09a2a4d82b09 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 8 Oct 2023 11:52:35 +0200 Subject: [PATCH] fd --- libuwuc/src/io/fd.rs | 6 ++++++ libuwuc/src/io/mod.rs | 4 ++++ libuwuc/src/io/stream/mod.rs | 4 ++-- rawc/Cargo.toml | 2 +- rawc/src/fcntl.rs | 6 ++++++ rawc/src/lib.rs | 1 + rawc/src/stdio.rs | 10 ++++------ rawc/src/unistd.rs | 5 +++++ tests/c/fd.c | 29 +++++++++++++++++++++++++++++ tests/data/alphabet.txt | 1 + 10 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 rawc/src/fcntl.rs create mode 100644 tests/c/fd.c create mode 100644 tests/data/alphabet.txt diff --git a/libuwuc/src/io/fd.rs b/libuwuc/src/io/fd.rs index 34c696c..36c71e4 100644 --- a/libuwuc/src/io/fd.rs +++ b/libuwuc/src/io/fd.rs @@ -31,3 +31,9 @@ pub const O_NDELAY: i32 = O_NONBLOCK; pub const O_SYNC: i32 = 0o04010000; pub const O_FSYNC: i32 = O_SYNC; pub const O_ASYNC: i32 = 0o020000; + +pub const SEEK_SET: i32 = 0; +pub const SEEK_CUR: i32 = 1; +pub const SEEK_END: i32 = 2; +pub const SEEK_DATA: i32 = 3; +pub const SEEK_HOLE: i32 = 4; diff --git a/libuwuc/src/io/mod.rs b/libuwuc/src/io/mod.rs index ccb8b3b..fd2ab09 100644 --- a/libuwuc/src/io/mod.rs +++ b/libuwuc/src/io/mod.rs @@ -47,6 +47,10 @@ pub unsafe fn sys_write(fd: Fd, buf: &[u8]) -> Result { syscall::syscall!(syscall::SYS_WRITE, fd.0, buf.as_ptr(), buf.len()).syscall_resultify() } +pub unsafe fn sys_lseek(fd: Fd, offset: i64, whence: i32) -> Result { + syscall::syscall!(syscall::SYS_LSEEK, fd.0, offset, whence).syscall_resultify() +} + pub unsafe fn write_all(fd: Fd, mut buf: &[u8]) -> Result<(), Error> { while !buf.is_empty() { let result = sys_write(fd, buf)?; diff --git a/libuwuc/src/io/stream/mod.rs b/libuwuc/src/io/stream/mod.rs index 501cd3c..b0d1808 100644 --- a/libuwuc/src/io/stream/mod.rs +++ b/libuwuc/src/io/stream/mod.rs @@ -43,8 +43,8 @@ pub unsafe fn fopen<'a>( unsafe { Ok(&*alloc::boxed(FileStream { fd })) } } -pub fn fputc(c: u8, stream: &FileStream) -> i32 { - match stream.write_byte(c) { +pub fn fputc(c: i32, stream: &FileStream) -> i32 { + match stream.write_byte(c as u8) { Ok(_) => c as _, Err(_) => EOF, } diff --git a/rawc/Cargo.toml b/rawc/Cargo.toml index 5bbd979..c54d76a 100644 --- a/rawc/Cargo.toml +++ b/rawc/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] -crate-type = ["staticlib", "rlib", "cdylib"] +crate-type = ["staticlib", "rlib"] [dependencies] diff --git a/rawc/src/fcntl.rs b/rawc/src/fcntl.rs new file mode 100644 index 0000000..b35d15f --- /dev/null +++ b/rawc/src/fcntl.rs @@ -0,0 +1,6 @@ +use libuwuc::{error::IntoOkOrErrno, utils::SharedThinCstr, io::fd::Fd}; + +#[no_mangle] +pub unsafe extern "C" fn open(path: SharedThinCstr<'_>, flags: i32) -> Fd { + libuwuc::io::fd::open(path, flags).into_ok_or_errno() +} diff --git a/rawc/src/lib.rs b/rawc/src/lib.rs index eca015e..83a8253 100644 --- a/rawc/src/lib.rs +++ b/rawc/src/lib.rs @@ -4,6 +4,7 @@ #![deny(clippy::no_mangle_with_rust_abi)] #![warn(rust_2018_idioms)] +mod fcntl; mod rt; mod stdio; mod stdlib; diff --git a/rawc/src/stdio.rs b/rawc/src/stdio.rs index ab301b2..2adeba3 100644 --- a/rawc/src/stdio.rs +++ b/rawc/src/stdio.rs @@ -2,7 +2,7 @@ use core::ffi::{c_char, c_int}; use libuwuc::{ error::IntoOkOrErrno, - io::{fd::Fd, stream::FileStream, traits::WriteCounter, STDERR, STDIN, STDOUT}, + io::{stream::FileStream, traits::WriteCounter, STDERR, STDIN, STDOUT}, utils::SharedThinCstr, }; @@ -11,11 +11,9 @@ pub unsafe extern "C" fn puts(s: *const c_char) -> i32 { libuwuc::io::puts(s) } -// RAW FD: - #[no_mangle] -pub unsafe extern "C" fn open(path: SharedThinCstr<'_>, flags: i32) -> Fd { - libuwuc::io::fd::open(path, flags).into_ok_or_errno() +pub unsafe extern "C" fn putchar(char: i32) -> i32 { + libuwuc::io::stream::fputc(char, stdout) } // PRINTF: @@ -108,7 +106,7 @@ pub unsafe extern "C" fn ungetc(_c: c_int, _stream: *mut FileStream) -> c_int { #[no_mangle] pub unsafe extern "C" fn fputc(c: c_int, stream: *mut FileStream) -> c_int { - libuwuc::io::stream::fputc(c as u8, &*stream) + libuwuc::io::stream::fputc(c, &*stream) } #[no_mangle] diff --git a/rawc/src/unistd.rs b/rawc/src/unistd.rs index 4cbf673..33e457a 100644 --- a/rawc/src/unistd.rs +++ b/rawc/src/unistd.rs @@ -14,3 +14,8 @@ pub unsafe extern "C" fn write(fd: Fd, buf: *const u8, count: usize) -> isize { .map(|n| n as isize) .into_ok_or_errno() } + +#[no_mangle] +pub unsafe extern "C" fn lseek(fd: Fd, offset: i64, whence: i32) -> i64 { + libuwuc::io::sys_lseek(fd, offset, whence).into_ok_or_errno() +} diff --git a/tests/c/fd.c b/tests/c/fd.c new file mode 100644 index 0000000..633dee4 --- /dev/null +++ b/tests/c/fd.c @@ -0,0 +1,29 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int alphabet = open("data/alphabet.txt", O_RDONLY); + assert(alphabet != -1 && "failed to open file"); + + off_t off; + + off = lseek(alphabet, 10, SEEK_SET); + assert(off != -1 && "failed to seek file"); + + char buf[1]; + ssize_t amount; + + amount = read(alphabet, buf, 1); + assert(amount == 1 && "failed to read from alphabet"); + assert(buf[0] == 'k' && "character at offest 10 is not k"); + + + off = lseek(alphabet, 1, SEEK_CUR); + assert(off != -1 && "failed to seek file"); + + amount = read(alphabet, buf, 1); + assert(amount == 1 && "failed to read from alphabet"); + assert(buf[0] == 'm' && "character at offest 12 is not m"); +} diff --git a/tests/data/alphabet.txt b/tests/data/alphabet.txt new file mode 100644 index 0000000..0e1d09a --- /dev/null +++ b/tests/data/alphabet.txt @@ -0,0 +1 @@ +abcdefghijklmnopqrstuvwxyz0123456 \ No newline at end of file