fix getenv

This commit is contained in:
nora 2023-09-30 10:18:04 +02:00
parent 9464ea4829
commit de90913d46
6 changed files with 25 additions and 92 deletions

View file

@ -1,8 +1,4 @@
use core::{
ffi::{c_char, CStr},
iter,
ptr::NonNull,
};
use core::{ffi::CStr, ptr::NonNull};
use crate::{println, utils::SharedThinCstr};
@ -67,6 +63,7 @@ pub fn getenv(name: SharedThinCstr) -> Option<SharedThinCstr> {
fn getenv_inner(mut envp: EnvP, name: SharedThinCstr) -> Option<SharedThinCstr> {
let mut eq_idx = 0;
envp.find(|env| {
println!("trying {env:?}");
// Find ENV
// EN=x
// ENV=x <- this one
@ -84,6 +81,9 @@ fn getenv_inner(mut envp: EnvP, name: SharedThinCstr) -> Option<SharedThinCstr>
if name.is_none() || env == Some(b'=') {
return false;
}
if name != env {
return false;
}
eq_idx += 1;
}
})
@ -154,4 +154,11 @@ mod tests {
assert_eq!(super::getenv_inner(envp, cstr("LONG_NAME\0")), None);
})
}
#[test]
fn getenv_same_length() {
with_envp(&["OWO=a", "UWU=b"], |envp| {
assert_eq!(super::getenv_inner(envp, cstr("UWU\0")), Some(cstr("b\0")));
})
}
}

View file

@ -12,7 +12,7 @@ impl core::fmt::Write for Printer {
unsafe {
while s.len() > 0 {
let result =
syscall::syscall!(syscall::SYS_WRITE, STDIN, s.as_ptr(), s.len()) as i64;
syscall::syscall!(syscall::SYS_WRITE, STDOUT, s.as_ptr(), s.len()) as i64;
if result < 0 {
return Err(core::fmt::Error);
}

View file

@ -13,7 +13,7 @@ pub(crate) unsafe extern "C" fn start(argc: u64, argv: *const *const c_char, rsp
fn main(argc: c_int, argv: *const *const c_char) -> c_int;
}
// crate::env::debug_env();
crate::env::debug_env();
let result = main(argc as i32, argv);
@ -23,6 +23,7 @@ pub(crate) unsafe extern "C" fn start(argc: u64, argv: *const *const c_char, rsp
pub fn exit(code: u64) -> ! {
unsafe {
crate::sys::syscall::syscall!(crate::sys::syscall::SYS_EXIT, code);
crate::sys::helpers::trap!();
core::hint::unreachable_unchecked()
}
}