getopt.rs(3): formatting

This commit is contained in:
Emma Tebibyte 2024-04-11 23:51:52 -06:00
parent ad92fe27d4
commit 95927ba8c5
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270

View File

@ -29,7 +29,7 @@ pub enum OptError {
UnknownOpt, UnknownOpt,
} }
// Function signature /* function signature */
pub trait GetOpt { pub trait GetOpt {
fn getopt(&self, optstring: &str) -> Option<Result<Opt, OptError>>; fn getopt(&self, optstring: &str) -> Option<Result<Opt, OptError>>;
} }
@ -43,10 +43,11 @@ impl GetOpt for Vec<String> {
.map(Result::unwrap) .map(Result::unwrap)
.collect(); .collect();
/* these operations must be separated out into separate operations so
* the CStrings can live long enough */
let argv: Vec<_> = c_strings.iter().map(|x| x.as_ptr()).collect(); let argv: Vec<_> = c_strings.iter().map(|x| x.as_ptr()).collect();
let argv_ptr = argv.as_ptr() as *const *mut c_char; let argv_ptr = argv.as_ptr() as *const *mut c_char;
let optstring_c = CString::new(optstring).unwrap(); let opts = CString::new(optstring).unwrap().into_raw();
let opts = optstring_c.into_raw();
let len = self.len() as c_int; let len = self.len() as c_int;
unsafe { unsafe {
@ -67,10 +68,10 @@ impl GetOpt for Vec<String> {
* *
* Otherwise, getopt() shall return -1 when all command line * Otherwise, getopt() shall return -1 when all command line
* options are parsed. */ * options are parsed. */
58 => { /* ASCII value for ':' */ 58 => { /* numerical ASCII value for ':' */
return Some(Err(OptError::MissingArg)); return Some(Err(OptError::MissingArg));
}, },
63 => { /* ASCII value for '?' */ 63 => { /* numerical ASCII value for '?' */
return Some(Err(OptError::UnknownOpt)) return Some(Err(OptError::UnknownOpt))
}, },
/* From getopt(3p): /* From getopt(3p):