3535#else
3636#include <zephyr/posix/unistd.h>
3737#endif
38- #include "getopt.h"
3938#include "getopt_common.h"
4039
4140#include <zephyr/logging/log.h>
@@ -45,59 +44,27 @@ LOG_MODULE_REGISTER(getopt);
4544#define BADARG ((int)':')
4645#define EMSG ""
4746
48- void getopt_init (void )
49- {
50- struct getopt_state * state ;
51-
52- state = getopt_state_get ();
53-
54- state -> opterr = 1 ;
55- state -> optind = 1 ;
56- state -> optopt = 0 ;
57- state -> optreset = 0 ;
58- state -> optarg = NULL ;
59-
60- state -> place = "" ; /* EMSG */
61-
62- #if CONFIG_GETOPT_LONG
63- state -> nonopt_start = -1 ; /* first non option argument (for permute) */
64- state -> nonopt_end = -1 ; /* first option after non options (for permute) */
65- #endif
66-
67- opterr = 1 ;
68- optind = 1 ;
69- optopt = 0 ;
70- optreset = 0 ;
71- optarg = NULL ;
72- }
73-
7447/*
7548 * getopt --
7649 * Parse argc/argv argument vector.
7750 */
78- int getopt (int nargc , char * const nargv [], const char * ostr )
51+ int __getopt_r (int nargc , char * const nargv [], const char * ostr , struct getopt_data * state )
7952{
80- struct getopt_state * state ;
8153 char * oli ; /* option letter list index */
8254
83- /* get getopt state of the current thread */
84- state = getopt_state_get ();
85-
8655 if (state -> optreset || * state -> place == 0 ) { /* update scanning pointer */
8756 state -> optreset = 0 ;
8857 state -> place = nargv [state -> optind ];
8958 if (state -> optind >= nargc || * state -> place ++ != '-' ) {
9059 /* Argument is absent or is not an option */
9160 state -> place = EMSG ;
92- z_getopt_global_state_update (state );
9361 return -1 ;
9462 }
9563 state -> optopt = * state -> place ++ ;
9664 if (state -> optopt == '-' && * state -> place == 0 ) {
9765 /* "--" => end of options */
9866 ++ state -> optind ;
9967 state -> place = EMSG ;
100- z_getopt_global_state_update (state );
10168 return -1 ;
10269 }
10370 if (state -> optopt == 0 ) {
@@ -106,7 +73,6 @@ int getopt(int nargc, char *const nargv[], const char *ostr)
10673 */
10774 state -> place = EMSG ;
10875 if (strchr (ostr , '-' ) == NULL ) {
109- z_getopt_global_state_update (state );
11076 return -1 ;
11177 }
11278 state -> optopt = '-' ;
@@ -124,7 +90,6 @@ int getopt(int nargc, char *const nargv[], const char *ostr)
12490 if (state -> opterr && * ostr != ':' ) {
12591 LOG_DBG ("illegal option -- %c" , state -> optopt );
12692 }
127- z_getopt_global_state_update (state );
12893 return BADCH ;
12994 }
13095
@@ -147,18 +112,30 @@ int getopt(int nargc, char *const nargv[], const char *ostr)
147112 /* option-argument absent */
148113 state -> place = EMSG ;
149114 if (* ostr == ':' ) {
150- z_getopt_global_state_update (state );
151115 return BADARG ;
152116 }
153117 if (state -> opterr ) {
154118 LOG_DBG ("option requires an argument -- %c" , state -> optopt );
155119 }
156- z_getopt_global_state_update (state );
157120 return BADCH ;
158121 }
159122 state -> place = EMSG ;
160123 ++ state -> optind ;
161124 }
162- z_getopt_global_state_update (state );
163125 return state -> optopt ; /* return option letter */
164126}
127+
128+ /*
129+ * getopt --
130+ * Parse argc/argv argument vector.
131+ */
132+ int getopt (int nargc , char * const nargv [], const char * ostr )
133+ {
134+ struct getopt_data state ;
135+ int ret ;
136+
137+ z_getopt_global_state_get (& state );
138+ ret = __getopt_r (nargc , nargv , ostr , & state );
139+ z_getopt_global_state_put (& state );
140+ return ret ;
141+ }
0 commit comments