6d74b3997fb2a2b36f768f872d2b19c95fb2f328
[a56.git] / getopt.c
1 /*
2         I got this off net.sources from Henry Spencer.
3         It is a public domain getopt(3) like in System V.
4         I have made the following modifications:
5
6         index(s,c) was added because too many people could
7         not compile getopt without it.
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 #include <stdio.h>
16
17 #ifndef lint
18 static  char    sccsfid[] = "@(#) getopt.c 5.0 (UTZoo) 1985";
19 #endif
20
21 #define ARGCH    (int)':'
22 #define BADCH    (int)'?'
23 #define EMSG     ""
24 #define ENDARGS  "--"
25
26 /* this is included because index is not on some UNIX systems */
27 static
28 char *
29 index (s, c)
30 register        char    *s;
31 register        int     c;
32         {
33         while (*s)
34                 if (c == *s) return (s);
35                 else s++;
36         return (NULL);
37         }
38
39 /*
40  * get option letter from argument vector
41  */
42 int     opterr = 1,             /* useless, never set or used */
43         optind = 1,             /* index into parent argv vector */
44         optopt;                 /* character checked for validity */
45 char    *optarg;                /* argument associated with option */
46
47 #define tell(s) fputs(*nargv,stderr);fputs(s,stderr); \
48                 fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
49 \f
50
51 getopt(nargc,nargv,ostr)
52 int     nargc;
53 char    **nargv,
54         *ostr;
55 {
56         static char     *place = EMSG;  /* option letter processing */
57         register char   *oli;           /* option letter list index */
58         char    *index();
59
60         if(!*place) {                   /* update scanning pointer */
61                 if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
62                 if (*place == '-') {    /* found "--" */
63                         ++optind;
64                         return(EOF);
65                 }
66         }                               /* option letter okay? */
67         if ((optopt = (int)*place++) == ARGCH || !(oli = index(ostr,optopt))) {
68                 if(!*place) ++optind;
69                 tell(": illegal option -- ");
70         }
71         if (*++oli != ARGCH) {          /* don't need argument */
72                 optarg = NULL;
73                 if (!*place) ++optind;
74         }
75         else {                          /* need an argument */
76                 if (*place) optarg = place;     /* no white space */
77                 else if (nargc <= ++optind) {   /* no arg */
78                         place = EMSG;
79                         tell(": option requires an argument -- ");
80                 }
81                 else optarg = nargv[optind];    /* white space */
82                 place = EMSG;
83                 ++optind;
84         }
85         return(optopt);                 /* dump back option letter */
86 }
87
88 \f
89 #ifdef GETOPT
90
91 #ifndef lint
92 static  char    sccspid[] = "@(#) getopt.c 5.1 (WangInst) 6/15/85";
93 #endif
94
95 main (argc, argv) char **argv;
96         {
97         char    *optstring = argv[1];
98         char    *argv0 = argv[0];
99         extern  int     optind;
100         extern  char    *optarg;
101         int     opterr = 0;
102         int     C;
103         char    *opi;
104         if (argc == 1)
105                 {
106                 fprintf (stderr, "Usage: %s optstring args\n", argv0);
107                 exit (1);
108                 }
109         argv++;
110         argc--;
111         argv[0] = argv0;
112         while ((C = getopt (argc, argv, optstring)) != EOF)
113                 {
114                 if (C == BADCH) opterr++;
115                 printf ("-%c ", C);
116                 opi = index (optstring, C);
117                 if (opi && opi[1] == ARGCH)
118                         if (optarg)
119                                 printf ("\"%s\" ", optarg);
120                         else opterr++;
121                 }
122         printf ("%s", ENDARGS);
123         while (optind < argc)
124                 printf (" \"%s\"", argv[optind++]);
125         putchar ('\n');
126         exit (opterr);
127         }
128
129 #endif