1 /*
2  * Copyright 2011-2018 Brad Lanam, Walnut Creek, CA
3  */
4 
5 /*
6  * A new version of getopt()
7  *      Boolean short flags: -a -b  (a, b)
8  *      With values:         -c 123 -d=abcdef -ef
9  *                              (-c = 123, -d = abcdef, -e = f)
10  *      Long options:        --a --b --c 123 --d=abcdef
11  * LEGACY:
12  *      Boolean short flags: -ab    (a, b)
13  *      short flags:         -version  (-v = ersion)
14  * MODERN:
15  *      Boolean long name:   -ab    (ab)
16  *      long flags:          -version  (-version)
17  *
18  */
19 
20 #if defined(TEST_GETOPTN)
21 # include "gconfig.h"
22 #else
23 # include "config.h"
24 #endif
25 #include "getoptn.h"
26 
27 #if _hdr_stdio
28 # include <stdio.h>
29 #endif
30 #if _hdr_stdlib
31 # include <stdlib.h>
32 #endif
33 #if _hdr_string
34 # include <string.h>
35 #endif
36 #if _hdr_strings
37 # include <strings.h>
38 #endif
39 #if _sys_types \
40     && ! defined (DI_INC_SYS_TYPES_H) /* xenix */
41 # define DI_INC_SYS_TYPES_H
42 # include <sys/types.h>
43 #endif
44 #if _use_mcheck
45 # include <mcheck.h>
46 #endif
47 
48 typedef struct {
49     Size_t      optionlen;
50     int         idx;
51 } getoptn_optinfo_t;
52 
53 typedef struct {
54     int                 style;
55     int                 optidx;
56     Size_t              optcount;
57     getoptn_opt_t       *opts;
58     getoptn_optinfo_t   *optinfo;
59     int                 argc;
60     const char          * const *argv;
61     const char          *arg;       /* current arg we're processing         */
62     Size_t              arglen;     /* and the length of it                 */
63     int                 hasvalue;   /* does this arg have a value attached? */
64     Size_t              argidx;     /* index to the value                   */
65     Size_t              optionlen;  /* length of the option for this arg    */
66     Size_t              reprocess;  /* -ab legacy form? must be 0 or 1      */
67     Size_t              offset;     /* reprocessing offset                  */
68 } getoptn_info_t;
69 
70 typedef void (*getoptn_func_bool_t) _((const char *option, void *valptr));
71 typedef void (*getoptn_func_value_t) _((const char *option, void *valptr, const char *value));
72 
73 static int
74 #if _proto_stdc
find_option(getoptn_info_t * info,const char * arg,const char * oarg,Size_t * argidx)75 find_option (getoptn_info_t *info, const char *arg, const char *oarg, Size_t *argidx)
76 #else
77 find_option (info, arg, oarg, argidx)
78     getoptn_info_t  *info;
79     const char      *arg;
80     const char      *oarg;
81     Size_t          *argidx;
82 #endif
83 {
84   int       i;
85   Size_t    junk;
86 
87   for (i = 0; i < (int) info->optcount; ++i) {
88     if (info->optinfo[i].optionlen == 0) {
89       info->optinfo[i].optionlen = strlen (info->opts[i].option);
90       info->optinfo[i].idx = i;
91     }
92     if (strncmp (arg + info->offset, info->opts[i].option + info->reprocess,
93              info->optinfo[i].optionlen - info->reprocess) == 0) {
94       info->hasvalue = FALSE;
95         /* info->argidx == 0 indicates top level of recursion */
96       if (info->argidx == 0) {
97         info->optionlen = info->optinfo[i].optionlen;
98       }
99 
100       if (info->style == GETOPTN_LEGACY) {
101         if (info->arglen - info->offset > info->optionlen - info->reprocess) {
102           if (info->opts[i].option_type == GETOPTN_BOOL) {
103             info->reprocess = TRUE;
104             if (info->offset == 0) {
105               ++info->offset;
106             }
107             ++info->offset;
108             if (info->offset >= info->arglen) {
109               info->offset = 0;
110               info->reprocess = FALSE;
111             }
112           } else {
113             info->hasvalue = TRUE;
114           }
115         } else {
116           info->offset = 0;
117           info->reprocess = FALSE;
118         }
119       }
120 
121       if (info->style == GETOPTN_MODERN) {
122         if (info->arglen > info->optionlen) {
123           if (info->arg[info->optionlen] == '=') {
124             info->hasvalue = TRUE;
125           } else {
126             continue;  /* partial match */
127           }
128         }
129       }
130 
131       *argidx = info->optinfo[i].optionlen;
132       if (info->opts[i].option_type == GETOPTN_ALIAS) {
133         return find_option (info, (const char *) info->opts[i].valptr,
134             oarg, &junk);
135       }
136       return i;
137     }
138   }
139 
140   info->reprocess = FALSE;
141   info->offset = 0;
142   return GETOPTN_NOTFOUND;
143 }
144 
145 static const char *
146 #if _proto_stdc
getoption_value(getoptn_info_t * info,getoptn_opt_t * opt)147 getoption_value (getoptn_info_t *info, getoptn_opt_t *opt)
148 #else
149 getoption_value (info, opt)
150     getoptn_info_t      *info;
151     getoptn_opt_t       *opt;
152 #endif
153 {
154   const char    *ptr;
155 
156   ptr = (char *) NULL;
157   if (opt->option_type != GETOPTN_FUNC_VALUE &&
158       opt->value2 != (void *) NULL) {
159     ptr = (const char *) opt->value2;
160     return ptr;
161   }
162 
163   if (info->hasvalue && info->arg[info->argidx] == '=') {
164     ptr = &info->arg[info->argidx + 1];
165   } else if (info->hasvalue) {
166     ptr = &info->arg[info->argidx];
167   } else if (info->optidx + 1 < info->argc) {
168     ++info->optidx;
169     ptr = info->argv[info->optidx];
170   }
171 
172   return ptr;
173 }
174 
175 static int
176 #if _proto_stdc
dooptchecks(getoptn_info_t * info,getoptn_opt_t * opt,getoptn_optinfo_t * optinfo,const char * tag,Size_t sz)177 dooptchecks (getoptn_info_t *info, getoptn_opt_t *opt,
178      getoptn_optinfo_t *optinfo, const char *tag, Size_t sz)
179 #else
180 dooptchecks (info, opt, optinfo, tag, sz)
181     getoptn_info_t      *info;
182     getoptn_opt_t       *opt;
183     getoptn_optinfo_t   *optinfo;
184     const char          *tag;
185     Size_t              sz;
186 #endif
187 {
188   if (sz != 0 && opt->valsiz != sz) {
189     fprintf (stderr, "%s: %s: invalid size (line %d)\n", info->argv[0], tag, optinfo->idx);
190     return 1;
191   }
192   if (opt->valptr == NULL) {
193     fprintf (stderr, "%s: %s: invalid pointer (line %d)\n", info->argv[0], tag, optinfo->idx);
194     return 1;
195   }
196 
197   return 0;
198 }
199 
200 static int
201 #if _proto_stdc
process_opt(getoptn_info_t * info,getoptn_opt_t * opt,getoptn_optinfo_t * optinfo)202 process_opt (getoptn_info_t *info, getoptn_opt_t *opt, getoptn_optinfo_t *optinfo)
203 #else
204 process_opt (info, opt, optinfo)
205     getoptn_info_t  *info;
206     getoptn_opt_t   *opt;
207     getoptn_optinfo_t *optinfo;
208 #endif
209 {
210   const char    *ptr;
211 
212   ptr = (char *) NULL;
213   if (opt->option_type == GETOPTN_INT ||
214       opt->option_type == GETOPTN_LONG ||
215       opt->option_type == GETOPTN_SIZET ||
216       opt->option_type == GETOPTN_DOUBLE ||
217       opt->option_type == GETOPTN_STRING ||
218       opt->option_type == GETOPTN_STRPTR ||
219       opt->option_type == GETOPTN_FUNC_VALUE) {
220     ptr = getoption_value (info, opt);
221     if (ptr == (char *) NULL) {
222       fprintf (stderr, "%s: %s argument missing\n", info->argv[0], info->arg);
223       return 1;
224     }
225   }
226 
227   if (opt->option_type == GETOPTN_BOOL) {
228     int       *v;
229     if (dooptchecks (info, opt, optinfo, "bool", sizeof(int)) != 0) {
230       return 1;
231     }
232     v = (int *) opt->valptr;
233     *v = 1 - *v;  /* flip it */
234   } else if (opt->option_type == GETOPTN_INT) {
235     int       *v;
236     if (dooptchecks (info, opt, optinfo, "int", sizeof(int)) != 0) {
237       return 1;
238     }
239     v = (int *) opt->valptr;
240     *v = atoi (ptr);
241   } else if (opt->option_type == GETOPTN_LONG) {
242     long      *v;
243     if (dooptchecks (info, opt, optinfo, "long", sizeof(long)) != 0) {
244       return 1;
245     }
246     v = (long *) opt->valptr;
247     *v = atol (ptr);
248   } else if (opt->option_type == GETOPTN_SIZET) {
249     Size_t  *v;
250     if (dooptchecks (info, opt, optinfo, "Size_t", sizeof(Size_t)) != 0) {
251       return 1;
252     }
253     v = (Size_t *) opt->valptr;
254     *v = (Size_t) atol (ptr);
255   } else if (opt->option_type == GETOPTN_DOUBLE) {
256     double     *v;
257     if (dooptchecks (info, opt, optinfo, "double", sizeof(double)) != 0) {
258       return 1;
259     }
260     v = (double *) opt->valptr;
261     *v = atof (ptr);
262   } else if (opt->option_type == GETOPTN_STRING) {
263     char      *v;
264     if (dooptchecks (info, opt, optinfo, "string", 0) != 0) {
265       return 1;
266     }
267     v = (char *) opt->valptr;
268     strncpy (v, ptr, opt->valsiz - 1);
269   } else if (opt->option_type == GETOPTN_STRPTR) {
270     const char **v;
271     if (dooptchecks (info, opt, optinfo, "strptr", 0) != 0) {
272       return 1;
273     }
274     v = (const char **) opt->valptr;
275     *v = strdup (ptr); /* memory leak (one time) */
276   } else if (opt->option_type == GETOPTN_FUNC_BOOL) {
277     getoptn_func_bool_t f;
278     if (opt->value2 == (void *) NULL) {
279       fprintf (stderr, "%s: %s: invalid function ptr (line %d)\n", info->argv[0], "func_bool", optinfo->idx);
280       return 1;
281     }
282     f = (getoptn_func_bool_t) opt->value2;
283     (f)(opt->option, opt->valptr);
284   } else if (opt->option_type == GETOPTN_FUNC_VALUE) {
285     getoptn_func_value_t f;
286     if (opt->value2 == (void *) NULL) {
287       fprintf (stderr, "%s: %s: invalid function ptr (line %d)\n", info->argv[0], "func_val", optinfo->idx);
288       return 1;
289     }
290     f = (getoptn_func_value_t) opt->value2;
291     (f)(opt->option, opt->valptr, ptr);
292   } else {
293     info->reprocess = FALSE;
294     info->offset = 0;
295     fprintf (stderr, "%s: unknown option type %d\n",
296          info->argv[0], opt->option_type);
297     return 1;
298   }
299 
300   return 0;
301 }
302 
303 
304 int
305 #if _proto_stdc
getoptn(int style,int argc,const char * const argv[],Size_t optcount,getoptn_opt_t opts[],int * errorCount)306 getoptn (int style, int argc, const char * const argv [],
307          Size_t optcount, getoptn_opt_t opts [], int *errorCount)
308 #else
309 getoptn (style, argc, argv, optcount, opts, errorCount)
310     int         style;
311     int         argc;
312     const char * const argv [];
313     Size_t      optcount;
314     getoptn_opt_t opts [];
315     int         *errorCount;
316 #endif
317 {
318   int               i;
319   int               rc;
320   const char        *arg;
321   getoptn_opt_t     *opt;
322   getoptn_info_t    info;
323 
324   info.style = style;
325   info.argc = argc;
326   info.argv = argv;
327   info.optcount = optcount;
328   info.opts = opts;
329   info.optinfo = (getoptn_optinfo_t *) NULL;
330 
331   *errorCount = 0;
332 
333   if (optcount > 0) {
334     info.optinfo = (getoptn_optinfo_t *)
335           malloc (sizeof (getoptn_optinfo_t) * optcount);
336     for (i = 0; i < (int) info.optcount; ++i) {
337       info.optinfo[i].optionlen = 0;
338     }
339   } else {
340     return 1 < argc ? 1 : -1;
341   }
342 
343   for (info.optidx = 1; info.optidx < argc; info.optidx++) {
344     arg = argv[info.optidx];
345     if (*arg != '-') {
346       if (info.optinfo != (getoptn_optinfo_t *) NULL) {
347         free (info.optinfo);
348       }
349       return info.optidx;
350     }
351     if (strcmp (arg, "--") == 0) {
352       info.optidx++;
353       if (info.optinfo != (getoptn_optinfo_t *) NULL) {
354         free (info.optinfo);
355       }
356       return info.optidx;
357     }
358 
359     info.argidx = 0;
360     info.arg = arg;
361     info.arglen = strlen (arg);
362     info.reprocess = FALSE;
363     info.offset = 0;
364 
365     do {
366       i = find_option (&info, arg, arg, &info.argidx);
367       if (opts[i].option_type == GETOPTN_IGNORE) {
368         continue;
369       }
370       if (i == GETOPTN_NOTFOUND) {
371         if (info.reprocess == FALSE) {
372           fprintf (stderr, "%s: unknown option %s\n", argv[0], arg);
373           ++*errorCount;
374         }
375         continue;
376       }
377 
378       opt = &opts[i];
379       rc = process_opt (&info, opt, &info.optinfo[i]);
380       if (rc) {
381         ++*errorCount;
382       }
383     } while (info.reprocess);
384   }
385 
386   if (info.optinfo != (getoptn_optinfo_t *) NULL) {
387     free (info.optinfo);
388   }
389   return argc;
390 }
391 
392 #if defined(TEST_GETOPTN)
393 
394 #if _hdr_math
395 # include <math.h>
396 #endif
397 
398 static void
399 #if _proto_stdc
process_opts(const char * arg,char * valptr)400 process_opts (const char *arg, char *valptr)
401 #else
402 process_opts (arg, valptr)
403     const char  *arg;
404     char        *valptr;
405 #endif
406 {
407   if (strcmp (arg, "-h") == 0) {
408     double *v;
409     v = (double *) valptr;
410     *v = 9.9;
411   }
412   return;
413 }
414 
415 static void
416 #if _proto_stdc
process_opts_val(const char * arg,char * valptr,char * value)417 process_opts_val (const char *arg, char *valptr, char *value)
418 #else
419 process_opts_val (arg, valptr, value)
420     const char      *arg;
421     char            *valptr;
422     char            *value;
423 #endif
424 {
425   if (strcmp (arg, "-g") == 0) {
426     double *v;
427     v = (double *) valptr;
428     *v = atof (value);
429   }
430   return;
431 }
432 
433 int
434 #if _proto_stdc
main(int argc,const char * const argv[])435 main (int argc, const char * const argv[])
436 #else
437 main (argc, argv)
438     int                 argc;
439     const char * const  argv[];
440 #endif
441 {
442   char      tmp[40];
443   char      s[40];
444   char      s2[5];
445   char      *sp;
446   long      l;
447   double    d;
448   int       i;
449   int       j;
450   int       k;
451   int       ec;
452   int       optidx;
453   int       ac;
454   const char *av[10];
455 
456   int  grc = 0;
457   int  testno = 0;
458 
459   getoptn_opt_t opts[] = {
460     { "-D",  GETOPTN_STRING,     &s, sizeof(s), (void *) "abc123" },
461     { "-b",  GETOPTN_BOOL,       &i, sizeof(i), NULL },
462     { "--b", GETOPTN_BOOL,       &i, sizeof(i), NULL },
463     { "-c",  GETOPTN_BOOL,       &j, sizeof(j), NULL },
464     { "--c", GETOPTN_ALIAS,      (void *) "-c", 0, NULL },
465     { "-bc", GETOPTN_BOOL,       &k, sizeof(k), NULL },
466     { "-d",  GETOPTN_DOUBLE,     &d, sizeof(d), NULL },
467     { "-f1",  GETOPTN_INT,       &i, 8, NULL },
468     { "-f2",  GETOPTN_LONG,      &i, 2, NULL },
469     { "-f3",  GETOPTN_LONG,      &l, 12, NULL },
470     { "--i", GETOPTN_INT,        &i, sizeof(i), NULL },
471     { "-i",  GETOPTN_INT,        &i, sizeof(i), NULL },
472     { "-i15",GETOPTN_INT,        &j, sizeof(j), NULL },
473     { "-i17",GETOPTN_INT,        &j, sizeof(j), NULL },
474     { "-l",  GETOPTN_LONG,       &l, sizeof(l), NULL },
475     { "-s",  GETOPTN_STRING,     &s, sizeof(s), NULL },
476     { "-sabcd", GETOPTN_BOOL,    &i, sizeof(i), NULL },
477     { "-sp",  GETOPTN_STRPTR,    &sp, 0, NULL },
478     { "-p",  GETOPTN_STRPTR,     &sp, 0, NULL },
479     { "-S",  GETOPTN_STRPTR,     &sp, 0, (void *) "abc1234" },
480     { "-s2",  GETOPTN_STRING,    &s2, sizeof(s2), NULL },
481     { "-np1",  GETOPTN_STRING,   NULL, sizeof(s2), NULL },
482     { "-np2",  GETOPTN_FUNC_BOOL, NULL, sizeof(s2), NULL },
483     { "-np3",  GETOPTN_FUNC_VALUE, NULL, sizeof(s2), NULL },
484     { "-z1", GETOPTN_ALIAS,      (void *) "--c", 0, NULL },
485     { "-z2", GETOPTN_ALIAS,      (void *) "-z1", 0, NULL },
486     { "-z3", GETOPTN_ALIAS,      (void *) "-z2", 0, NULL }
487   };
488 
489   /* test 1 */
490   ++testno;
491   memset (s, '\0', sizeof (s));
492   ac = 2;
493   sprintf (tmp, "test: %d", testno);
494   av[0] = tmp;
495   av[1] = "-D";
496   av[2] = NULL;
497   ec = 0;
498   optidx = getoptn (GETOPTN_LEGACY, ac, av,
499        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
500   if (strcmp (s, "abc123") != 0 || optidx != 2 || ec != 0) {
501     fprintf (stderr, "fail test %d\n", testno);
502     grc = 1;
503   }
504 
505   /* test 2 */
506   ++testno;
507   i = 0;
508   ac = 2;
509   sprintf (tmp, "test %d", testno);
510   av[0] = tmp;
511   av[1] = "-b";
512   av[2] = NULL;
513   ec = 0;
514   optidx = getoptn (GETOPTN_LEGACY, ac, av,
515        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
516   if (i != 1 || optidx != 2 || ec != 0) {
517     fprintf (stderr, "fail test %d\n", testno);
518     grc = 1;
519   }
520 
521   /* test 3 */
522   ++testno;
523   i = 0;
524   ac = 2;
525   sprintf (tmp, "test %d", testno);
526   av[0] = tmp;
527   av[1] = "--b";
528   av[2] = NULL;
529   ec = 0;
530   optidx = getoptn (GETOPTN_LEGACY, ac, av,
531        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
532   if (i != 1 || optidx != 2 || ec != 0) {
533     fprintf (stderr, "fail test %d\n", testno);
534     grc = 1;
535   }
536 
537   /* test 4 */
538   ++testno;
539   i = 0;
540   ac = 2;
541   sprintf (tmp, "test %d", testno);
542   av[0] = tmp;
543   av[1] = "--i=13";
544   av[2] = NULL;
545   ec = 0;
546   optidx = getoptn (GETOPTN_LEGACY, ac, av,
547        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
548   if (i != 13 || optidx != 2 || ec != 0) {
549     fprintf (stderr, "fail test %d\n", testno);
550     grc = 1;
551   }
552 
553   /* test 5 */
554   ++testno;
555   i = 0;
556   ac = 3;
557   sprintf (tmp, "test %d", testno);
558   av[0] = tmp;
559   av[1] = "--i";
560   av[2] = "14";
561   av[3] = NULL;
562   ec = 0;
563   optidx = getoptn (GETOPTN_LEGACY, ac, av,
564        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
565   if (i != 14 || optidx != 3 || ec != 0) {
566     fprintf (stderr, "fail test %d\n", testno);
567     grc = 1;
568   }
569 
570   /* test 6 */
571   ++testno;
572   i = 0;
573   j = 0;
574   ac = 2;
575   sprintf (tmp, "test %d", testno);
576   av[0] = tmp;
577   av[1] = "-i15";
578   av[2] = NULL;
579   ec = 0;
580   optidx = getoptn (GETOPTN_LEGACY, ac, av,
581        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
582   if (i != 15 || j != 0 || optidx != 2 || ec != 0) {
583     fprintf (stderr, "fail test %d\n", testno);
584     grc = 1;
585   }
586 
587   /* test 7 */
588   ++testno;
589   i = 0;
590   ac = 3;
591   sprintf (tmp, "test %d", testno);
592   av[0] = tmp;
593   av[1] = "-i";
594   av[2] = "16";
595   av[3] = NULL;
596   ec = 0;
597   optidx = getoptn (GETOPTN_LEGACY, ac, av,
598        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
599   if (i != 16 || optidx != 3 || ec != 0) {
600     fprintf (stderr, "fail test %d\n", testno);
601     grc = 1;
602   }
603 
604   /* test 8 */
605   ++testno;
606   i = 0;
607   j = 0;
608   ac = 3;
609   sprintf (tmp, "test %d", testno);
610   av[0] = tmp;
611   av[1] = "-i17";
612   av[2] = "5";
613   av[3] = NULL;
614   ec = 0;
615   optidx = getoptn (GETOPTN_MODERN, ac, av,
616        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
617   if (i != 0 || j != 5 || optidx != 3 || ec != 0) {
618     fprintf (stderr, "fail test %d\n", testno);
619     grc = 1;
620   }
621 
622   /* test 9 */
623   ++testno;
624   i = 0;
625   ac = 2;
626   sprintf (tmp, "test %d", testno);
627   av[0] = tmp;
628   av[1] = "-i=17";
629   av[2] = NULL;
630   ec = 0;
631   optidx = getoptn (GETOPTN_MODERN, ac, av,
632        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
633   if (i != 17 || optidx != 2 || ec != 0) {
634     fprintf (stderr, "fail test %d\n", testno);
635     grc = 1;
636   }
637 
638   /* test 10 */
639   ++testno;
640   i = 0;
641   ac = 2;
642   sprintf (tmp, "test %d", testno);
643   av[0] = tmp;
644   av[1] = "-i7";
645   av[2] = NULL;
646   ec = 0;
647   optidx = getoptn (GETOPTN_LEGACY, ac, av,
648        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
649   if (i != 7 || optidx != 2 || ec != 0) {
650     fprintf (stderr, "fail test %d\n", testno);
651     grc = 1;
652   }
653 
654   /* test 11 */
655   ++testno;
656   l = 0L;
657   ac = 3;
658   sprintf (tmp, "test %d", testno);
659   av[0] = tmp;
660   av[1] = "-l";
661   av[2] = "19";
662   av[3] = NULL;
663   ec = 0;
664   optidx = getoptn (GETOPTN_LEGACY, ac, av,
665        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
666   if (l != 19 || optidx != 3 || ec != 0) {
667     fprintf (stderr, "fail test %d\n", testno);
668     grc = 1;
669   }
670 
671   /* test 12 */
672   ++testno;
673   i = 0;
674   j = 0;
675   ac = 3;
676   sprintf (tmp, "test %d", testno);
677   av[0] = tmp;
678   av[1] = "-b";
679   av[2] = "-c";
680   av[3] = NULL;
681   ec = 0;
682   optidx = getoptn (GETOPTN_LEGACY, ac, av,
683        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
684   if (i != 1 || j != 1 || optidx != 3 || ec != 0) {
685     fprintf (stderr, "fail test %d\n", testno);
686     grc = 1;
687   }
688 
689   /* test 13 */
690   ++testno;
691   i = 0;
692   j = 0;
693   k = 0;
694   ac = 2;
695   sprintf (tmp, "test %d", testno);
696   av[0] = tmp;
697   av[1] = "-bc";
698   av[2] = NULL;
699   ec = 0;
700   optidx = getoptn (GETOPTN_LEGACY, ac, av,
701        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
702   if (i != 1 || j != 1 || k != 0 || optidx != 2 || ec != 0) {
703     fprintf (stderr, "fail test %d\n", testno);
704     grc = 1;
705   }
706 
707   /* test 14 */
708   ++testno;
709   i = 0;
710   j = 0;
711   k = 0;
712   ac = 2;
713   sprintf (tmp, "test %d", testno);
714   av[0] = tmp;
715   av[1] = "-bc";
716   av[2] = NULL;
717   ec = 0;
718   optidx = getoptn (GETOPTN_MODERN, ac, av,
719        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
720   if (i != 0 || j != 0 || k != 1 || optidx != 2 || ec != 0) {
721     fprintf (stderr, "fail test %d\n", testno);
722     grc = 1;
723   }
724 
725   /* test 15 */
726   ++testno;
727   memset (s, '\0', sizeof (s));
728   ac = 2;
729   sprintf (tmp, "test %d", testno);
730   av[0] = tmp;
731   av[1] = "-s=abc";
732   av[2] = NULL;
733   ec = 0;
734   optidx = getoptn (GETOPTN_MODERN, ac, av,
735        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
736   if (strcmp (s, "abc") != 0 || optidx != 2 || ec != 0) {
737     fprintf (stderr, "fail test %d\n", testno);
738     grc = 1;
739   }
740 
741   /* test 16 */
742   ++testno;
743   d = 0.0;
744   ac = 2;
745   sprintf (tmp, "test %d", testno);
746   av[0] = tmp;
747   av[1] = "-d=1.2";
748   av[2] = NULL;
749   ec = 0;
750   optidx = getoptn (GETOPTN_MODERN, ac, av,
751        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
752   if (fabs(d - 1.2) > 0.00001 || optidx != 2 || ec != 0) {
753     fprintf (stderr, "fail test %d: %.2g %d\n", testno, d, optidx);
754     grc = 1;
755   }
756 
757   /* test 17 */
758   ++testno;
759   memset (s, '\0', sizeof (s));
760   i = 0;
761   ac = 2;
762   sprintf (tmp, "test %d", testno);
763   av[0] = tmp;
764   av[1] = "-sabcd";
765   av[2] = NULL;
766   ec = 0;
767   optidx = getoptn (GETOPTN_LEGACY, ac, av,
768        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
769   if (strcmp (s, "abcd") != 0 || i != 0 || optidx != 2 || ec != 0) {
770     fprintf (stderr, "fail test %d\n", testno);
771     grc = 1;
772   }
773 
774   /* test 18 */
775   ++testno;
776   memset (s, '\0', sizeof (s));
777   ac = 2;
778   sprintf (tmp, "test %d", testno);
779   av[0] = tmp;
780   av[1] = "-s=abcde";
781   av[2] = NULL;
782   ec = 0;
783   optidx = getoptn (GETOPTN_MODERN, ac, av,
784        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
785   if (strcmp (s, "abcde") != 0 || optidx != 2 || ec != 0) {
786     fprintf (stderr, "fail test %d\n", testno);
787     grc = 1;
788   }
789 
790   /* test 19 */
791   ++testno;
792   memset (s, '\0', sizeof (s));
793   ac = 3;
794   sprintf (tmp, "test %d", testno);
795   av[0] = tmp;
796   av[1] = "-s";
797   av[2] = "abcdef";
798   av[3] = NULL;
799   ec = 0;
800   optidx = getoptn (GETOPTN_MODERN, ac, av,
801        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
802   if (strcmp (s, "abcdef") != 0 || optidx != 3 || ec != 0) {
803     fprintf (stderr, "fail test %d\n", testno);
804     grc = 1;
805   }
806 
807   /* test 20 */
808   ++testno;
809   sp = "";
810   ac = 3;
811   sprintf (tmp, "test %d", testno);
812   av[0] = tmp;
813   av[1] = "-sp";
814   av[2] = "0123";
815   av[3] = NULL;
816   ec = 0;
817   optidx = getoptn (GETOPTN_MODERN, ac, av,
818        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
819   if (strcmp (sp, "0123") != 0 || optidx != 3 || ec != 0) {
820     fprintf (stderr, "fail test %d\n", testno);
821     grc = 1;
822   }
823 
824   /* test 21 */
825   ++testno;
826   sp = "";
827   ac = 2;
828   sprintf (tmp, "test %d", testno);
829   av[0] = tmp;
830   av[1] = "-sp=01234";
831   av[2] = NULL;
832   ec = 0;
833   optidx = getoptn (GETOPTN_MODERN, ac, av,
834        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
835   if (strcmp (sp, "01234") != 0 || optidx != 2 || ec != 0) {
836     fprintf (stderr, "fail test %d\n", testno);
837     grc = 1;
838   }
839 
840   /* test 22 */
841   ++testno;
842   sp = "";
843   ac = 2;
844   sprintf (tmp, "test %d", testno);
845   av[0] = tmp;
846   av[1] = "-p012345";
847   av[2] = NULL;
848   ec = 0;
849   optidx = getoptn (GETOPTN_LEGACY, ac, av,
850        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
851   if (strcmp (sp, "012345") != 0 || optidx != 2 || ec != 0) {
852     fprintf (stderr, "fail test %d\n", testno);
853     grc = 1;
854   }
855 
856   /* test 23 */
857   ++testno;
858   sp = "";
859   ac = 2;
860   sprintf (tmp, "test %d", testno);
861   av[0] = tmp;
862   av[1] = "-S";
863   av[2] = NULL;
864   ec = 0;
865   optidx = getoptn (GETOPTN_LEGACY, ac, av,
866        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
867   if (strcmp (sp, "abc1234") != 0 || optidx != 2 || ec != 0) {
868     fprintf (stderr, "fail test %d\n", testno);
869     grc = 1;
870   }
871 
872   /* test 24 */
873   ++testno;
874   sp = "";
875   ac = 2;
876   sprintf (tmp, "test %d", testno);
877   av[0] = tmp;
878   av[1] = "-p=0123456";
879   av[2] = NULL;
880   ec = 0;
881   optidx = getoptn (GETOPTN_LEGACY, ac, av,
882        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
883   if (strcmp (sp, "0123456") != 0 || optidx != 2 || ec != 0) {
884     fprintf (stderr, "fail test %d\n", testno);
885     grc = 1;
886   }
887 
888   /* test 25 */
889   ++testno;
890   memset (s, '\0', sizeof (s));
891   i = 0;
892   ac = 2;
893   sprintf (tmp, "test %d", testno);
894   av[0] = tmp;
895   av[1] = "-sabcd";
896   av[2] = NULL;
897   ec = 0;
898   optidx = getoptn (GETOPTN_MODERN, ac, av,
899        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
900   if (strcmp (s, "") != 0 || i != 1 || optidx != 2 || ec != 0) {
901     fprintf (stderr, "fail test %d\n", testno);
902     grc = 1;
903   }
904 
905   /* test 26 */
906   ++testno;
907   i = 1;
908   ac = 2;
909   sprintf (tmp, "test %d", testno);
910   av[0] = tmp;
911   av[1] = "-b";
912   av[2] = NULL;
913   ec = 0;
914   optidx = getoptn (GETOPTN_LEGACY, ac, av,
915        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
916   if (i != 0 || optidx != 2 || ec != 0) {
917     fprintf (stderr, "fail test %d\n", testno);
918     grc = 1;
919   }
920 
921   /* test 27 */
922   ++testno;
923   i = 1;
924   ac = 2;
925   sprintf (tmp, "test %d", testno);
926   av[0] = tmp;
927   av[1] = "--b";
928   av[2] = NULL;
929   ec = 0;
930   optidx = getoptn (GETOPTN_LEGACY, ac, av,
931        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
932   if (i != 0 || optidx != 2 || ec != 0) {
933     fprintf (stderr, "fail test %d\n", testno);
934     grc = 1;
935   }
936 
937   /* test 28 */
938   ++testno;
939   i = 1;
940   j = 1;
941   ac = 3;
942   sprintf (tmp, "test %d", testno);
943   av[0] = tmp;
944   av[1] = "-b";
945   av[2] = "-c";
946   av[3] = NULL;
947   ec = 0;
948   optidx = getoptn (GETOPTN_LEGACY, ac, av,
949        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
950   if (i != 0 || j != 0 || optidx != 3 || ec != 0) {
951     fprintf (stderr, "fail test %d\n", testno);
952     grc = 1;
953   }
954 
955   /* test 29 */
956   ++testno;
957   i = 1;
958   j = 1;
959   k = 1;
960   ac = 2;
961   sprintf (tmp, "test %d", testno);
962   av[0] = tmp;
963   av[1] = "-bc";
964   av[2] = NULL;
965   ec = 0;
966   optidx = getoptn (GETOPTN_LEGACY, ac, av,
967        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
968   if (i != 0 || j != 0 || k != 1 || optidx != 2 || ec != 0) {
969     fprintf (stderr, "fail test %d / i:%d j:%d k:%d optidx:%d ec:%d\n", testno, i, j, k, optidx, ec);
970     grc = 1;
971   }
972 
973   /* test 30 */
974   ++testno;
975   i = 1;
976   j = 1;
977   k = 1;
978   ac = 2;
979   sprintf (tmp, "test %d", testno);
980   av[0] = tmp;
981   av[1] = "-bc";
982   av[2] = NULL;
983   ec = 0;
984   optidx = getoptn (GETOPTN_MODERN, ac, av,
985        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
986   if (i != 1 || j != 1 || k != 0 || optidx != 2 || ec != 0) {
987     fprintf (stderr, "fail test %d / i:%d j:%d k:%d optidx:%d ec:%d\n", testno, i, j, k, optidx, ec);
988     grc = 1;
989   }
990 
991   /* test 31 - empty value  */
992   ++testno;
993   i = 0;
994   j = 0;
995   k = 0;
996   ac = 2;
997   sprintf (tmp, "test %d", testno);
998   av[0] = tmp;
999   av[1] = "-i=";
1000   av[2] = NULL;
1001   ec = 0;
1002   optidx = getoptn (GETOPTN_MODERN, ac, av,
1003        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1004   if (i != 0 || optidx != 2 || ec != 0) {
1005     fprintf (stderr, "fail test %d / i:%d optidx:%d ec:%d\n", testno, i, optidx, ec);
1006     grc = 1;
1007   }
1008 
1009   /* test 32 - no value; should print error */
1010   ++testno;
1011   i = 0;
1012   j = 0;
1013   k = 0;
1014   ac = 2;
1015   sprintf (tmp, "test %d", testno);
1016   av[0] = tmp;
1017   av[1] = "-i";
1018   av[2] = NULL;
1019   ec = 0;
1020   fprintf (stderr, "** expect argument missing\n");
1021   optidx = getoptn (GETOPTN_MODERN, ac, av,
1022        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1023   if (i != 0 || optidx != 2 || ec != 1) {
1024     fprintf (stderr, "fail test %d\n", testno);
1025     grc = 1;
1026   }
1027 
1028   /* test 33 - wrong size; should print error */
1029   ++testno;
1030   i = 0;
1031   j = 0;
1032   k = 0;
1033   l = 0L;
1034   ac = 2;
1035   sprintf (tmp, "test %d", testno);
1036   av[0] = tmp;
1037   av[1] = "-f1=7";
1038   av[2] = NULL;
1039   ec = 0;
1040   fprintf (stderr, "** expect invalid size\n");
1041   optidx = getoptn (GETOPTN_MODERN, ac, av,
1042        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1043   if (i != 0 || l != 0 || optidx != 2 || ec != 1) {
1044     fprintf (stderr, "fail test %d; %d %ld %d\n", testno, i, l, optidx);
1045     grc = 1;
1046   }
1047 
1048   /* test 34 - wrong size; should print error */
1049   ++testno;
1050   i = 0;
1051   j = 0;
1052   k = 0;
1053   l = 0L;
1054   ac = 2;
1055   sprintf (tmp, "test %d", testno);
1056   av[0] = tmp;
1057   av[1] = "-f2=7";
1058   av[2] = NULL;
1059   ec = 0;
1060   fprintf (stderr, "** expect invalid size\n");
1061   optidx = getoptn (GETOPTN_MODERN, ac, av,
1062        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1063   if (i != 0 || l != 0 || optidx != 2 || ec != 1) {
1064     fprintf (stderr, "fail test %d; %d %ld %d\n", testno, i, l, optidx);
1065     grc = 1;
1066   }
1067 
1068   /* test 35 - wrong size; should print error */
1069   ++testno;
1070   i = 0;
1071   j = 0;
1072   k = 0;
1073   l = 0L;
1074   ac = 2;
1075   sprintf (tmp, "test %d", testno);
1076   av[0] = tmp;
1077   av[1] = "-f3=7";
1078   av[2] = NULL;
1079   ec = 0;
1080   fprintf (stderr, "** expect invalid size\n");
1081   optidx = getoptn (GETOPTN_MODERN, ac, av,
1082        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1083   if (i != 0 || l != 0 || optidx != 2 || ec != 1) {
1084     fprintf (stderr, "fail test %d; %d %ld %d\n", testno, i, l, optidx);
1085     grc = 1;
1086   }
1087 
1088   /* test 36 - end of options */
1089   ++testno;
1090   i = 0;
1091   j = 0;
1092   k = 0;
1093   l = 0L;
1094   ac = 4;
1095   sprintf (tmp, "test %d", testno);
1096   av[0] = tmp;
1097   av[1] = "-i=7";
1098   av[2] = "--";
1099   av[3] = "abc";
1100   av[4] = NULL;
1101   ec = 0;
1102   optidx = getoptn (GETOPTN_MODERN, ac, av,
1103        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1104   if (i != 7 || optidx != 3 || ec != 0) {
1105     fprintf (stderr, "fail test %d\n", testno);
1106     grc = 1;
1107   }
1108 
1109   /* test 37 - no more options */
1110   ++testno;
1111   i = 0;
1112   j = 0;
1113   k = 0;
1114   l = 0L;
1115   ac = 3;
1116   sprintf (tmp, "test %d", testno);
1117   av[0] = tmp;
1118   av[1] = "-i=7";
1119   av[2] = "abc";
1120   av[3] = NULL;
1121   ec = 0;
1122   optidx = getoptn (GETOPTN_MODERN, ac, av,
1123        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1124   if (i != 7 || optidx != 2 || ec != 0) {
1125     fprintf (stderr, "fail test %d\n", testno);
1126     grc = 1;
1127   }
1128 
1129   /* test 38 - empty value followed by another option; returns the option */
1130   ++testno;
1131   i = 0;
1132   j = 0;
1133   k = 0;
1134   memset (s, '\0', sizeof (s));
1135   ac = 3;
1136   sprintf (tmp, "test %d", testno);
1137   av[0] = tmp;
1138   av[1] = "-s";
1139   av[2] = "-s";
1140   av[3] = NULL;
1141   ec = 0;
1142   optidx = getoptn (GETOPTN_MODERN, ac, av,
1143        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1144   if (strcmp (s, "-s") != 0 || optidx != 3 || ec != 0) {
1145     fprintf (stderr, "fail test %d\n", testno);
1146     grc = 1;
1147   }
1148 
1149   /* test 39 - unknown options */
1150   ++testno;
1151   i = 0;
1152   j = 0;
1153   k = 0;
1154   memset (s, '\0', sizeof (s));
1155   ac = 5;
1156   sprintf (tmp, "test %d", testno);
1157   av[0] = tmp;
1158   av[1] = "-b";
1159   av[2] = "-c";
1160   av[3] = "-s";
1161   av[4] = "abc";
1162   av[5] = NULL;
1163   ec = 0;
1164   optidx = getoptn (GETOPTN_LEGACY, ac, av,
1165        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1166   if (strcmp (s, "abc") != 0 || i != 1 || j != 1 || optidx != 5 || ec != 0) {
1167     fprintf (stderr, "fail test %d\n", testno);
1168     grc = 1;
1169   }
1170 
1171   /* test 40 - legacy: mixed boolean + arg */
1172   ++testno;
1173   i = 0;
1174   j = 0;
1175   k = 0;
1176   memset (s, '\0', sizeof (s));
1177   ac = 3;
1178   sprintf (tmp, "test %d", testno);
1179   av[0] = tmp;
1180   av[1] = "-bcs";
1181   av[2] = "abc";
1182   av[3] = NULL;
1183   ec = 0;
1184   optidx = getoptn (GETOPTN_LEGACY, ac, av,
1185        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1186   if (strcmp (s, "abc") != 0 || i != 1 || j != 1 || optidx != 3 || ec != 0) {
1187     fprintf (stderr, "fail test %d\n", testno);
1188     grc = 1;
1189   }
1190 
1191   /* test 41 */
1192   ++testno;
1193   memset (s2, '\0', sizeof (s2));
1194   ac = 3;
1195   sprintf (tmp, "test %d", testno);
1196   av[0] = tmp;
1197   av[1] = "-s2";
1198   av[2] = "abc";
1199   av[3] = NULL;
1200   ec = 0;
1201   optidx = getoptn (GETOPTN_MODERN, ac, av,
1202        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1203   if (strcmp (s2, "abc") != 0 || optidx != 3 || ec != 0) {
1204     fprintf (stderr, "fail test %d\n", testno);
1205     grc = 1;
1206   }
1207 
1208   /* test 42 - short string */
1209   ++testno;
1210   memset (s2, '\0', sizeof (s2));
1211   ac = 3;
1212   sprintf (tmp, "test %d", testno);
1213   av[0] = tmp;
1214   av[1] = "-s2";
1215   av[2] = "abcd";
1216   av[3] = NULL;
1217   ec = 0;
1218   optidx = getoptn (GETOPTN_MODERN, ac, av,
1219        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1220   if (strcmp (s2, "abcd") != 0 || optidx != 3 || ec != 0) {
1221     fprintf (stderr, "fail test %d\n", testno);
1222     grc = 1;
1223   }
1224 
1225   /* test 43 - short string */
1226   ++testno;
1227   memset (s2, '\0', sizeof (s2));
1228   ac = 3;
1229   sprintf (tmp, "test %d", testno);
1230   av[0] = tmp;
1231   av[1] = "-s2";
1232   av[2] = "abcde";
1233   av[3] = NULL;
1234   ec = 0;
1235   optidx = getoptn (GETOPTN_MODERN, ac, av,
1236        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1237   if (strcmp (s2, "abcd") != 0 || optidx != 3 || ec != 0) {
1238     fprintf (stderr, "fail test %d: %s\n", testno, s2);
1239     grc = 1;
1240   }
1241 
1242   /* test 44 - short string */
1243   ++testno;
1244   memset (s2, '\0', sizeof (s2));
1245   ac = 3;
1246   sprintf (tmp, "test %d", testno);
1247   av[0] = tmp;
1248   av[1] = "-s2";
1249   av[2] = "abcdef";
1250   av[3] = NULL;
1251   ec = 0;
1252   optidx = getoptn (GETOPTN_MODERN, ac, av,
1253        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1254   if (strcmp (s2, "abcd") != 0 || optidx != 3 || ec != 0) {
1255     fprintf (stderr, "fail test %d: %s\n", testno, s2);
1256     grc = 1;
1257   }
1258 
1259   /* test 45 - null ptr */
1260   ++testno;
1261   memset (s2, '\0', sizeof (s2));
1262   ac = 3;
1263   sprintf (tmp, "test %d", testno);
1264   av[0] = tmp;
1265   av[1] = "-np1";
1266   av[2] = "abcdef";
1267   av[3] = NULL;
1268   ec = 0;
1269   fprintf (stderr, "** expect invalid pointer\n");
1270   optidx = getoptn (GETOPTN_MODERN, ac, av,
1271        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1272   if (strcmp (s2, "") != 0 || ec != 1) {
1273     fprintf (stderr, "fail test %d: %s %d\n", testno, s2, optidx);
1274     grc = 1;
1275   }
1276 
1277   /* test 46 - null ptr */
1278   ++testno;
1279   memset (s2, '\0', sizeof (s2));
1280   ac = 3;
1281   sprintf (tmp, "test %d", testno);
1282   av[0] = tmp;
1283   av[1] = "-np2";
1284   av[2] = "abcdef";
1285   av[3] = NULL;
1286   ec = 0;
1287   fprintf (stderr, "** expect invalid pointer\n");
1288   optidx = getoptn (GETOPTN_MODERN, ac, av,
1289        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1290   if (strcmp (s2, "") != 0 || ec != 1) {
1291     fprintf (stderr, "fail test %d: %s %d\n", testno, s2, optidx);
1292     grc = 1;
1293   }
1294 
1295   /* test 47 - null ptr */
1296   ++testno;
1297   memset (s2, '\0', sizeof (s2));
1298   ac = 3;
1299   sprintf (tmp, "test %d", testno);
1300   av[0] = tmp;
1301   av[1] = "-np3";
1302   av[2] = "abcdef";
1303   av[3] = NULL;
1304   ec = 0;
1305   fprintf (stderr, "** expect invalid pointer\n");
1306   optidx = getoptn (GETOPTN_MODERN, ac, av,
1307        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1308   if (strcmp (s2, "") != 0 || ec != 1) {
1309     fprintf (stderr, "fail test %d: %s %d\n", testno, s2, optidx);
1310     grc = 1;
1311   }
1312 
1313   /* test 48 - alias chain */
1314   ++testno;
1315   i = 0;
1316   j = 0;
1317   k = 0;
1318   memset (s2, '\0', sizeof (s2));
1319   ac = 2;
1320   sprintf (tmp, "test %d", testno);
1321   av[0] = tmp;
1322   av[1] = "-z3";
1323   av[2] = NULL;
1324   ec = 0;
1325   optidx = getoptn (GETOPTN_LEGACY, ac, av,
1326        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1327   if (i != 0 || j != 1 || ec != 0) {
1328     fprintf (stderr, "fail test %d\n", testno);
1329     grc = 1;
1330   }
1331 
1332   /* test 49 - test boolean initial value */
1333   ++testno;
1334   i = 0;
1335   ac = 2;
1336   sprintf (tmp, "test %d", testno);
1337   av[0] = tmp;
1338   av[1] = "-c";
1339   av[2] = NULL;
1340   ec = 0;
1341   optidx = getoptn (GETOPTN_LEGACY, ac, av,
1342        sizeof (opts) / sizeof (getoptn_opt_t), opts, &ec);
1343   if (i != 0 || ec != 0) {
1344     fprintf (stderr, "fail test %d\n", testno);
1345     grc = 1;
1346   }
1347 
1348   return grc;
1349 }
1350 
1351 #endif /* TEST_GETOPTN */
1352