@@ -28,11 +28,9 @@ ZTEST(posix_c_lib_ext, test_getopt_basic)
2828 int c ;
2929 char * * argv ;
3030
31+ optind = 0 ; /* Reset state */
3132 argv = (char * * )nargv ;
3233
33- /* Get state of the current thread */
34- getopt_init ();
35-
3634 do {
3735 c = getopt (argc , argv , accepted_opt );
3836 if (cnt >= strlen (expected )) {
@@ -55,7 +53,6 @@ enum getopt_idx {
5553
5654ZTEST (posix_c_lib_ext , test_getopt )
5755{
58- struct getopt_state * state ;
5956 static const char * test_opts = "ac:" ;
6057 static const char * const nargv [] = {
6158 [GETOPT_IDX_CMD_NAME ] = "cmd_name" ,
@@ -67,9 +64,7 @@ ZTEST(posix_c_lib_ext, test_getopt)
6764 char * * argv ;
6865 int c ;
6966
70- /* Get state of the current thread */
71- getopt_init ();
72-
67+ optind = 0 ; /* Reset state */
7368 argv = (char * * )nargv ;
7469
7570 /* Test uknown option */
@@ -80,12 +75,7 @@ ZTEST(posix_c_lib_ext, test_getopt)
8075 zassert_equal (c , 'c' , "unexpected opt character" );
8176
8277 c = getopt (argc , argv , test_opts );
83- state = getopt_state_get ();
8478
85- /* Thread safe usge: */
86- zassert_equal (0 , strcmp (argv [GETOPT_IDX_OPTARG ], state -> optarg ),
87- "unexpected optarg result" );
88- /* Non thread safe usage: */
8979 zassert_equal (0 , strcmp (argv [GETOPT_IDX_OPTARG ], optarg ), "unexpected optarg result" );
9080}
9181
@@ -101,7 +91,6 @@ ZTEST(posix_c_lib_ext, test_getopt_long)
10191 /* Below test is based on example
10292 * https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html
10393 */
104- struct getopt_state * state ;
10594 int verbose_flag = 0 ;
10695 /* getopt_long stores the option index here. */
10796 int option_index = 0 ;
@@ -156,33 +145,31 @@ ZTEST(posix_c_lib_ext, test_getopt_long)
156145 int argc4 = ARRAY_SIZE (argv4 );
157146
158147 /* Test scenario 1 */
159- /* Get state of the current thread */
160- getopt_init ();
148+
149+ optind = 0 ; /* Reset state */
161150 argv = (char * * )argv1 ;
162151 c = getopt_long (argc1 , argv , accepted_opt , long_options , & option_index );
163152 zassert_equal (verbose_flag , 1 , "verbose flag expected" );
164153 c = getopt_long (argc1 , argv , accepted_opt , long_options , & option_index );
165- state = getopt_state_get ();
166154 zassert_equal ('c' , c , "unexpected option" );
167- zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
155+ zassert_equal (0 , strcmp (optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
168156 c = getopt_long (argc1 , argv , accepted_opt , long_options , & option_index );
169157 zassert_equal (-1 , c , "getopt_long shall return -1" );
170158
171159 /* Test scenario 2 */
160+ optind = 0 ; /* Reset state */
172161 argv = (char * * )argv2 ;
173- getopt_init ();
174162 c = getopt_long (argc2 , argv , accepted_opt , long_options , & option_index );
175163 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
176164 c = getopt_long (argc2 , argv , accepted_opt , long_options , & option_index );
177165 zassert_equal ('d' , c , "unexpected option" );
178- state = getopt_state_get ();
179- zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
166+ zassert_equal (0 , strcmp (optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
180167 c = getopt_long (argc2 , argv , accepted_opt , long_options , & option_index );
181168 zassert_equal (-1 , c , "getopt_long shall return -1" );
182169
183170 /* Test scenario 3 */
171+ optind = 0 ; /* Reset state */
184172 argv = (char * * )argv3 ;
185- getopt_init ();
186173 c = getopt_long (argc3 , argv , accepted_opt , long_options , & option_index );
187174 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
188175 c = getopt_long (argc3 , argv , accepted_opt , long_options , & option_index );
@@ -191,8 +178,8 @@ ZTEST(posix_c_lib_ext, test_getopt_long)
191178 zassert_equal (-1 , c , "getopt_long shall return -1" );
192179
193180 /* Test scenario 4 */
181+ optind = 0 ; /* Reset state */
194182 argv = (char * * )argv4 ;
195- getopt_init ();
196183 c = getopt_long (argc4 , argv , accepted_opt , long_options , & option_index );
197184 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
198185 c = getopt_long (argc4 , argv , accepted_opt , long_options , & option_index );
@@ -208,7 +195,6 @@ ZTEST(posix_c_lib_ext, test_getopt_long_only)
208195 /* Below test is based on example
209196 * https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html
210197 */
211- struct getopt_state * state ;
212198 int verbose_flag = 0 ;
213199 /* getopt_long stores the option index here. */
214200 int option_index = 0 ;
@@ -263,33 +249,30 @@ ZTEST(posix_c_lib_ext, test_getopt_long_only)
263249 int argc4 = ARRAY_SIZE (argv4 );
264250
265251 /* Test scenario 1 */
252+ optind = 0 ; /* Reset state */
266253 argv = (char * * )argv1 ;
267- getopt_init ();
268254 c = getopt_long_only (argc1 , argv , accepted_opt , long_options , & option_index );
269255 zassert_equal (verbose_flag , 1 , "verbose flag expected" );
270256 c = getopt_long_only (argc1 , argv , accepted_opt , long_options , & option_index );
271- state = getopt_state_get ();
272257 zassert_equal ('c' , c , "unexpected option" );
273- zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
258+ zassert_equal (0 , strcmp (optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
274259 c = getopt_long_only (argc1 , argv , accepted_opt , long_options , & option_index );
275260 zassert_equal (-1 , c , "getopt_long_only shall return -1" );
276261
277262 /* Test scenario 2 */
263+ optind = 0 ; /* Reset state */
278264 argv = (char * * )argv2 ;
279- getopt_init ();
280- state = getopt_state_get ();
281265 c = getopt_long_only (argc2 , argv , accepted_opt , long_options , & option_index );
282266 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
283267 c = getopt_long_only (argc2 , argv , accepted_opt , long_options , & option_index );
284- state = getopt_state_get ();
285268 zassert_equal ('d' , c , "unexpected option" );
286- zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
269+ zassert_equal (0 , strcmp (optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
287270 c = getopt_long_only (argc2 , argv , accepted_opt , long_options , & option_index );
288271 zassert_equal (-1 , c , "getopt_long_only shall return -1" );
289272
290273 /* Test scenario 3 */
274+ optind = 0 ; /* Reset state */
291275 argv = (char * * )argv3 ;
292- getopt_init ();
293276 c = getopt_long_only (argc3 , argv , accepted_opt , long_options , & option_index );
294277 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
295278 c = getopt_long_only (argc3 , argv , accepted_opt , long_options , & option_index );
@@ -298,8 +281,8 @@ ZTEST(posix_c_lib_ext, test_getopt_long_only)
298281 zassert_equal (-1 , c , "getopt_long_only shall return -1" );
299282
300283 /* Test scenario 4 */
284+ optind = 0 ; /* Reset state */
301285 argv = (char * * )argv4 ;
302- getopt_init ();
303286 c = getopt_long_only (argc4 , argv , accepted_opt , long_options , & option_index );
304287 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
305288 c = getopt_long_only (argc4 , argv , accepted_opt , long_options , & option_index );
0 commit comments