@@ -56,64 +56,65 @@ impl Shell {
56
56
unsafe { ( self . 0 . set_cur_dir ) ( fs_ptr. cast ( ) , dir_ptr. cast ( ) ) } . to_result ( )
57
57
}
58
58
59
- /// Gets the environment variable or list of environment variables
59
+ /// Gets the value of the specified environment variable
60
60
///
61
61
/// # Arguments
62
62
///
63
63
/// * `name` - The environment variable name of which to retrieve the
64
- /// value
65
- /// If None, will return all defined shell environment
66
- /// variables
64
+ /// value.
67
65
///
68
66
/// # Returns
69
67
///
70
- /// * `Some(Vec <env_value>)` - Value of the environment variable
71
- /// * `Some(Vec<env_names>)` - Vector of environment variable names
72
- /// * `None` - Environment variable doesn't exist
68
+ /// * `Some(<env_value>)` - &CStr16 containing the value of the
69
+ /// environment variable
70
+ /// * `None` - If environment variable does not exist
73
71
#[ must_use]
74
- pub fn get_env < ' a > ( & ' a self , name : Option < & CStr16 > ) -> Option < Vec < & ' a CStr16 > > {
75
- let mut env_vec = Vec :: new ( ) ;
76
- match name {
77
- Some ( n) => {
78
- let name_ptr: * const Char16 = core:: ptr:: from_ref :: < CStr16 > ( n) . cast ( ) ;
79
- let var_val = unsafe { ( self . 0 . get_env ) ( name_ptr. cast ( ) ) } ;
80
- if var_val. is_null ( ) {
81
- return None ;
82
- } else {
83
- unsafe { env_vec. push ( CStr16 :: from_ptr ( var_val. cast ( ) ) ) } ;
84
- }
85
- }
86
- None => {
87
- let cur_env_ptr = unsafe { ( self . 0 . get_env ) ( ptr:: null ( ) ) } ;
72
+ pub fn get_env ( & self , name : & CStr16 ) -> Option < & CStr16 > {
73
+ let name_ptr: * const Char16 = core:: ptr:: from_ref :: < CStr16 > ( name) . cast ( ) ;
74
+ let var_val = unsafe { ( self . 0 . get_env ) ( name_ptr. cast ( ) ) } ;
75
+ if var_val. is_null ( ) {
76
+ None
77
+ } else {
78
+ unsafe { Some ( CStr16 :: from_ptr ( var_val. cast ( ) ) ) }
79
+ }
80
+ }
88
81
89
- let mut cur_start = cur_env_ptr;
90
- let mut cur_len = 0 ;
82
+ /// Gets the list of environment variables
83
+ ///
84
+ /// # Returns
85
+ ///
86
+ /// * `Vec<env_names>` - Vector of environment variable names
87
+ #[ must_use]
88
+ pub fn get_envs ( & self ) -> Vec < & CStr16 > {
89
+ let mut env_vec: Vec < & CStr16 > = Vec :: new ( ) ;
90
+ let cur_env_ptr = unsafe { ( self . 0 . get_env ) ( ptr:: null ( ) ) } ;
91
91
92
- let mut i = 0 ;
93
- let mut null_count = 0 ;
94
- unsafe {
95
- while null_count <= 1 {
96
- if ( * ( cur_env_ptr. add ( i) ) ) == Char16 :: from_u16_unchecked ( 0 ) . into ( ) {
97
- if cur_len > 0 {
98
- env_vec. push ( CStr16 :: from_char16_with_nul_unchecked (
99
- & ( * ptr:: slice_from_raw_parts ( cur_start. cast ( ) , cur_len + 1 ) ) ,
100
- ) ) ;
101
- }
102
- cur_len = 0 ;
103
- null_count += 1 ;
104
- } else {
105
- if null_count > 0 {
106
- cur_start = cur_env_ptr. add ( i) ;
107
- }
108
- null_count = 0 ;
109
- cur_len += 1 ;
110
- }
111
- i += 1 ;
92
+ let mut cur_start = cur_env_ptr;
93
+ let mut cur_len = 0 ;
94
+
95
+ let mut i = 0 ;
96
+ let mut null_count = 0 ;
97
+ unsafe {
98
+ while null_count <= 1 {
99
+ if ( * ( cur_env_ptr. add ( i) ) ) == Char16 :: from_u16_unchecked ( 0 ) . into ( ) {
100
+ if cur_len > 0 {
101
+ env_vec. push ( CStr16 :: from_char16_with_nul (
102
+ & ( * ptr:: slice_from_raw_parts ( cur_start. cast ( ) , cur_len + 1 ) ) ,
103
+ ) . unwrap ( ) ) ;
104
+ }
105
+ cur_len = 0 ;
106
+ null_count += 1 ;
107
+ } else {
108
+ if null_count > 0 {
109
+ cur_start = cur_env_ptr. add ( i) ;
112
110
}
111
+ null_count = 0 ;
112
+ cur_len += 1 ;
113
113
}
114
+ i += 1 ;
114
115
}
115
116
}
116
- Some ( env_vec)
117
+ env_vec
117
118
}
118
119
119
120
/// Sets the environment variable
@@ -122,12 +123,12 @@ impl Shell {
122
123
///
123
124
/// * `name` - The environment variable for which to set the value
124
125
/// * `value` - The new value of the environment variable
125
- /// * `volatile` - Indicates whether or not the variable is volatile or
126
+ /// * `volatile` - Indicates whether the variable is volatile or
126
127
/// not
127
128
///
128
129
/// # Returns
129
130
///
130
- /// * `Status::SUCCESS` The variable was successfully set
131
+ /// * `Status::SUCCESS` - The variable was successfully set
131
132
pub fn set_env ( & self , name : & CStr16 , value : & CStr16 , volatile : bool ) -> Status {
132
133
let name_ptr: * const Char16 = core:: ptr:: from_ref :: < CStr16 > ( name) . cast ( ) ;
133
134
let value_ptr: * const Char16 = core:: ptr:: from_ref :: < CStr16 > ( value) . cast ( ) ;
0 commit comments