source: trunk/minix/commands/cawf/getopt.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.6 KB
Line 
1/*
2Newsgroups: mod.std.unix
3Subject: public domain AT&T getopt source
4Date: 3 Nov 85 19:34:15 GMT
5
6Here's something you've all been waiting for: the AT&T public domain
7source for getopt(3). It is the code which was given out at the 1985
8UNIFORUM conference in Dallas. I obtained it by electronic mail
9directly from AT&T. The people there assure me that it is indeed
10in the public domain.
11*/
12
13
14/*LINTLIBRARY*/
15#define NULL 0
16#define EOF (-1)
17#define ERR(s, c) if(opterr){\
18 extern int strlen(), write();\
19 char errbuf[2];\
20 errbuf[0] = c; errbuf[1] = '\n';\
21 (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
22 (void) write(2, s, (unsigned)strlen(s));\
23 (void) write(2, errbuf, 2);}
24
25extern int strcmp();
26extern char *strchr();
27
28int opterr = 1;
29int optind = 1;
30int optopt;
31char *optarg;
32
33int
34getopt(argc, argv, opts)
35int argc;
36char **argv, *opts;
37{
38 static int sp = 1;
39 register int c;
40 register char *cp;
41
42 if(sp == 1)
43 if(optind >= argc ||
44 argv[optind][0] != '-' || argv[optind][1] == '\0')
45 return(EOF);
46 else if(strcmp(argv[optind], "--") == NULL) {
47 optind++;
48 return(EOF);
49 }
50 optopt = c = argv[optind][sp];
51 if(c == ':' || (cp=strchr(opts, c)) == NULL) {
52 ERR(": illegal option -- ", c);
53 if(argv[optind][++sp] == '\0') {
54 optind++;
55 sp = 1;
56 }
57 return('?');
58 }
59 if(*++cp == ':') {
60 if(argv[optind][sp+1] != '\0')
61 optarg = &argv[optind++][sp+1];
62 else if(++optind >= argc) {
63 ERR(": option requires an argument -- ", c);
64 sp = 1;
65 return('?');
66 } else
67 optarg = argv[optind++];
68 sp = 1;
69 } else {
70 if(argv[optind][++sp] == '\0') {
71 sp = 1;
72 optind++;
73 }
74 optarg = NULL;
75 }
76 return(c);
77}
Note: See TracBrowser for help on using the repository browser.