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