From cde60c9f0eb6b41835cc89d2836e7323a6432c19 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 16 Apr 2023 10:58:46 +0100 Subject: [PATCH] stdlib: getsubopt implementation proposal. --- src/header/stdlib/mod.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 59d6de502b..a4037c7005 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -356,12 +356,38 @@ pub unsafe extern "C" fn getenv(name: *const c_char) -> *mut c_char { } // #[no_mangle] -pub extern "C" fn getsubopt( +pub unsafe extern "C" fn getsubopt( optionp: *mut *mut c_char, tokens: *const *mut c_char, valuep: *mut *mut c_char, ) -> c_int { - unimplemented!(); + let start = *optionp; + let max = strlen(start) as isize; + let mut i: usize = 0; + *valuep = ptr::null_mut(); + *optionp = strchr(start, b',' as i32); + if !(*optionp).is_null() { + *(*optionp).add(0) = 0; + *optionp = *optionp.add(1); + } else { + *(optionp) = start.add(max as usize); + } + while !tokens.offset(i as isize).is_null() { + let cur = tokens.offset(i as isize) as *const _; + let len = strlen(cur) as isize; + if strncmp(cur, start, len as usize) != 0 { + i = i + 1; + continue; + } + if (*start.offset(len)) == b'=' as i8 { + *valuep = start.offset(len + 1); + } else if !start.offset(len).is_null() { + i = i + 1; + continue; + } + return i as c_int; + } + -1 } // #[no_mangle]