From b7283d54fe42fe7d6ac3899c2e6ea1fea55552f6 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 4 Jun 2024 15:07:11 -0600 Subject: [PATCH] getopt.rs(3): formatting & organization --- src/getopt.rs | 55 +++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/getopt.rs b/src/getopt.rs index 79879df..0324f87 100644 --- a/src/getopt.rs +++ b/src/getopt.rs @@ -18,7 +18,6 @@ use std::ffi::{ c_int, c_char, CString, CStr }; - /* binding to getopt(3p) */ extern "C" { static mut optarg: *mut c_char; @@ -33,6 +32,27 @@ extern "C" { ) -> c_int; } +/* From getopt(3p): + * + * The variable optind is the index of the next element of the argv[] + * vector to be processed. It shall be initialized to 1 by the system, and + * getopt() shall update it when it finishes with each element of argv[]. + * If the application sets optind to zero before calling getopt(), the + * behavior is unspecified. When an element of argv[] contains multiple + * option characters, it is unspecified how getopt() determines which + * options have already been processed. + * + * This API can be utilized using unsafe blocks and dereferencing: + * + * use getopt::OPTIND; + * + * unsafe { while *OPTIND < 5 { + * println!("{}", *OPTIND); // 1..4 + * *OPTIND += 1; + * }} + */ +pub static mut OPTIND: *mut i32 = unsafe { std::ptr::addr_of_mut!(optind) }; + pub struct Opt { pub arg: Option, pub ind: i32, @@ -83,12 +103,12 @@ impl GetOpt for Vec { * * Otherwise, getopt() shall return -1 when all command line * options are parsed. */ - 58 => { /* numerical ASCII value for ':' */ - Some(Err(OptError::MissingArg(optopt.to_string()))) - }, - 63 => { /* numerical ASCII value for '?' */ - Some(Err(OptError::UnknownOpt(optopt.to_string()))) - }, + 58 => { /* numerical ASCII value for ':' */ + Some(Err(OptError::MissingArg(optopt.to_string()))) + }, + 63 => { /* numerical ASCII value for '?' */ + Some(Err(OptError::UnknownOpt(optopt.to_string()))) + }, /* From getopt(3p): * * If, when getopt() is called: @@ -118,24 +138,3 @@ impl GetOpt for Vec { } } } - -/* From getopt(3p): - * - * The variable optind is the index of the next element of the argv[] - * vector to be processed. It shall be initialized to 1 by the system, and - * getopt() shall update it when it finishes with each element of argv[]. - * If the application sets optind to zero before calling getopt(), the - * behavior is unspecified. When an element of argv[] contains multiple - * option characters, it is unspecified how getopt() determines which - * options have already been processed. - * - * This API can be utilized using unsafe blocks and dereferencing: - * - * use getopt::OPTIND; - * - * unsafe { while *OPTIND < 5 { - * println!("{}", *OPTIND); // 1..4 - * *OPTIND += 1; - * }} - */ -pub static mut OPTIND: *mut i32 = unsafe { std::ptr::addr_of_mut!(optind) };