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