@@ -159,40 +159,32 @@ int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
159
159
return 0 ;
160
160
}
161
161
162
- struct option * parse_options_dup (const struct option * o )
162
+ static size_t parse_options_count (const struct option * opt )
163
163
{
164
- const struct option * orig = o ;
165
- struct option * opts ;
166
- int nr = 0 ;
164
+ size_t n = 0 ;
167
165
168
- while ( o && o -> type != OPTION_END ) {
169
- nr ++ ;
170
- o ++ ;
171
- }
166
+ for (; opt && opt -> type != OPTION_END ; opt ++ )
167
+ n ++ ;
168
+ return n ;
169
+ }
172
170
173
- ALLOC_ARRAY ( opts , nr + 1 );
174
- COPY_ARRAY ( opts , orig , nr );
175
- memset ( opts + nr , 0 , sizeof ( * opts )) ;
176
- opts [ nr ]. type = OPTION_END ;
177
- return opts ;
171
+ struct option * parse_options_dup ( const struct option * o )
172
+ {
173
+ struct option no_options [] = { OPT_END () } ;
174
+
175
+ return parse_options_concat ( o , no_options ) ;
178
176
}
179
177
180
- struct option * parse_options_concat (struct option * a , struct option * b )
178
+ struct option * parse_options_concat (const struct option * a ,
179
+ const struct option * b )
181
180
{
182
181
struct option * ret ;
183
- size_t i , a_len = 0 , b_len = 0 ;
184
-
185
- for (i = 0 ; a [i ].type != OPTION_END ; i ++ )
186
- a_len ++ ;
187
- for (i = 0 ; b [i ].type != OPTION_END ; i ++ )
188
- b_len ++ ;
182
+ size_t a_len = parse_options_count (a );
183
+ size_t b_len = parse_options_count (b );
189
184
190
185
ALLOC_ARRAY (ret , st_add3 (a_len , b_len , 1 ));
191
- for (i = 0 ; i < a_len ; i ++ )
192
- ret [i ] = a [i ];
193
- for (i = 0 ; i < b_len ; i ++ )
194
- ret [a_len + i ] = b [i ];
195
- ret [a_len + b_len ] = b [b_len ]; /* final OPTION_END */
186
+ COPY_ARRAY (ret , a , a_len );
187
+ COPY_ARRAY (ret + a_len , b , b_len + 1 ); /* + 1 for final OPTION_END */
196
188
197
189
return ret ;
198
190
}
0 commit comments