]> git.sur5r.net Git - cc65/blob - testcode/lib/getopt-test.c
Merge pull request #577 from polluks/master
[cc65] / testcode / lib / getopt-test.c
1 /*
2 ** This is part of a changed public domain getopt implementation that
3 ** had the following text on top:
4 **
5 **      I got this off net.sources from Henry Spencer.
6 **      It is a public domain getopt(3) like in System V.
7 **      I have made the following modifications:
8 **
9 **      A test main program was added, ifdeffed by GETOPT.
10 **      This main program is a public domain implementation
11 **      of the getopt(1) program like in System V.  The getopt
12 **      program can be used to standardize shell option handling.
13 **              e.g.  cc -DGETOPT getopt.c -o getopt
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20
21 #define ARGCH    ':'
22 #define BADCH    '?'
23 #define ENDARGS  "--"
24
25 int main (int argc, char **argv)
26 {
27     char *optstring = argv[1];
28
29     char *argv0 = argv[0];
30
31     int opterr = 0;
32
33     int C;
34
35     char *opi;
36
37     if (argc == 1) {
38         fprintf (stderr, "Usage: %s optstring args\n", argv0);
39         exit (1);
40     }
41     argv++;
42     argc--;
43     argv[0] = argv0;
44     while ((C = getopt (argc, argv, optstring)) != EOF) {
45         if (C == BADCH)
46             opterr++;
47         printf ("-%c ", C);
48         opi = strchr (optstring, C);
49         if (opi && opi[1] == ARGCH)
50             if (optarg)
51                 printf ("\"%s\" ", optarg);
52             else
53                 opterr++;
54     }
55     printf ("%s", ENDARGS);
56     while (optind < argc)
57         printf (" \"%s\"", argv[optind++]);
58     putchar ('\n');
59     return opterr;
60 }
61