@@ -41,52 +41,51 @@ const darwin = if (builtin.os.tag.isDarwin()) struct {
4141 ) kern_return_t ;
4242} else struct {};
4343
44- const global = struct {
45- // C ABI global: `extern char *optarg;`
46- export var optarg : [* :0 ]allowzero u8 = @ptrFromInt (0 );
47- export var opterr : c_int = 1 ;
48- export var optind : c_int = 1 ;
49- export var optopt : c_int = 0 ;
50- };
44+ // C ABI globals: `extern char *optarg; extern int opterr, optind, optopt;`
45+ export var optarg : [* c ]u8 = null ;
46+ export var opterr : c_int = 1 ;
47+ export var optind : c_int = 1 ;
48+ export var optopt : c_int = 0 ;
5149
5250/// Returns some information through these globals
5351/// extern char *optarg;
5452/// extern int opterr, optind, optopt;
5553export fn getopt (argc : c_int , argv : [* ][* :0 ]u8 , optstring : [* :0 ]const u8 ) callconv (.c ) c_int {
56- global .optarg = @ptrFromInt (0 );
54+ optarg = null ;
55+ if (optind < 1 ) optind = 1 ;
5756 trace .log ("getopt argc={} argv={*} opstring={f} (err={}, ind={}, opt={})" , .{
5857 argc ,
5958 argv ,
6059 trace .fmtStr (optstring ),
61- global . opterr ,
62- global . optind ,
63- global . optopt ,
60+ opterr ,
61+ optind ,
62+ optopt ,
6463 });
65- if (global . optind >= argc ) {
64+ if (optind >= argc ) {
6665 trace .log ("getopt return -1" , .{});
6766 return -1 ;
6867 }
69- const arg = argv [@as (usize , @intCast (global . optind ))];
68+ const arg = argv [@as (usize , @intCast (optind ))];
7069 if (arg [0 ] != '-' or arg [1 ] == 0 ) {
7170 // Stop option parsing when we reach a non-option argument.
7271 return -1 ;
7372 }
7473 if (arg [1 ] == '-' and arg [2 ] == 0 ) {
7574 // End-of-options marker.
76- global . optind += 1 ;
75+ optind += 1 ;
7776 return -1 ;
7877 }
7978 const result = c .strchr (optstring , arg [1 ]) orelse {
80- global . optind += 1 ;
81- global . optopt = @as (c_int , arg [1 ]);
79+ optind += 1 ;
80+ optopt = @as (c_int , arg [1 ]);
8281 return '?' ;
8382 };
84- global . optind += 1 ;
83+ optind += 1 ;
8584
8685 if (arg [2 ] != 0 ) {
8786 // Support compact required argument form: -ovalue
8887 if (result [1 ] == ':' ) {
89- global . optarg = arg + 2 ;
88+ optarg = @ptrCast ( arg + 2 ) ;
9089 return @as (c_int , arg [1 ]);
9190 }
9291 @panic ("multi-letter argument not implemented" );
@@ -96,12 +95,12 @@ export fn getopt(argc: c_int, argv: [*][*:0]u8, optstring: [*:0]const u8) callco
9695 if (takes_arg ) {
9796 const is_optional = result [2 ] == ':' ;
9897 if (is_optional ) @panic ("optional args not implemented" );
99- if (global . optind >= argc ) {
100- global . optopt = @as (c_int , arg [1 ]);
98+ if (optind >= argc ) {
99+ optopt = @as (c_int , arg [1 ]);
101100 return if (optstring [0 ] == ':' ) ':' else '?' ;
102101 }
103- global . optarg = @ptrCast (argv [@as (usize , @intCast (global . optind ))]);
104- global . optind += 1 ;
102+ optarg = @ptrCast (argv [@as (usize , @intCast (optind ))]);
103+ optind += 1 ;
105104 }
106105 return @as (c_int , arg [1 ]);
107106}
0 commit comments