1*13648Ssam #ifndef lint
2*13648Ssam static char sccsid[] = "@(#)getopt.c 5.1 (Berkeley) 07/02/83";
3*13648Ssam #endif
4*13648Ssam
5*13648Ssam #include <stdio.h>
6*13648Ssam #define ERR(s, c) if(opterr){\
7*13648Ssam fputs(argv[0], stderr);\
8*13648Ssam fputs(s, stderr);\
9*13648Ssam fputc(c, stderr);\
10*13648Ssam fputc('\n', stderr);} else
11*13648Ssam
12*13648Ssam int opterr = 1;
13*13648Ssam int optind = 1;
14*13648Ssam int optopt;
15*13648Ssam char *optarg;
16*13648Ssam
17*13648Ssam extern char *index();
18*13648Ssam
19*13648Ssam int
getopt(argc,argv,opts)20*13648Ssam getopt (argc, argv, opts)
21*13648Ssam char **argv, *opts;
22*13648Ssam {
23*13648Ssam static int sp = 1;
24*13648Ssam register c;
25*13648Ssam register char *cp;
26*13648Ssam
27*13648Ssam if (sp == 1)
28*13648Ssam if (optind >= argc ||
29*13648Ssam argv[optind][0] != '-' || argv[optind][1] == '\0')
30*13648Ssam return EOF;
31*13648Ssam else if (strcmp(argv[optind], "--") == NULL) {
32*13648Ssam optind++;
33*13648Ssam return EOF;
34*13648Ssam }
35*13648Ssam optopt = c = argv[optind][sp];
36*13648Ssam if (c == ':' || (cp=index(opts, c)) == NULL) {
37*13648Ssam ERR (": illegal option -- ", c);
38*13648Ssam if (argv[optind][++sp] == '\0') {
39*13648Ssam optind++;
40*13648Ssam sp = 1;
41*13648Ssam }
42*13648Ssam return '?';
43*13648Ssam }
44*13648Ssam if (*++cp == ':') {
45*13648Ssam if (argv[optind][sp+1] != '\0')
46*13648Ssam optarg = &argv[optind++][sp+1];
47*13648Ssam else if (++optind >= argc) {
48*13648Ssam ERR (": option requires an argument -- ", c);
49*13648Ssam sp = 1;
50*13648Ssam return '?';
51*13648Ssam } else
52*13648Ssam optarg = argv[optind++];
53*13648Ssam sp = 1;
54*13648Ssam }
55*13648Ssam else {
56*13648Ssam if (argv[optind][++sp] == '\0') {
57*13648Ssam sp = 1;
58*13648Ssam optind++;
59*13648Ssam }
60*13648Ssam optarg = NULL;
61*13648Ssam }
62*13648Ssam return c;
63*13648Ssam }
64