This commit is contained in:
nora 2023-10-08 11:52:35 +02:00
parent 8d1795ad1a
commit 4d4af78afe
10 changed files with 59 additions and 9 deletions

View file

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

View file

@ -47,6 +47,10 @@ pub unsafe fn sys_write(fd: Fd, buf: &[u8]) -> Result<usize, Error> {
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<i64, Error> {
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)?;

View file

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

View file

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

6
rawc/src/fcntl.rs Normal file
View file

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

View file

@ -4,6 +4,7 @@
#![deny(clippy::no_mangle_with_rust_abi)]
#![warn(rust_2018_idioms)]
mod fcntl;
mod rt;
mod stdio;
mod stdlib;

View file

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

View file

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

29
tests/c/fd.c Normal file
View file

@ -0,0 +1,29 @@
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
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");
}

1
tests/data/alphabet.txt Normal file
View file

@ -0,0 +1 @@
abcdefghijklmnopqrstuvwxyz0123456