summaryrefslogtreecommitdiff
path: root/dviware/quicspool/man/getopt.l
blob: c1a50de8b014c4059f72887ad61779e7931276e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
.\" $Header: getopt.l,v 1.1 88/01/15 12:58:20 simpson Rel $
.\" $Log:	getopt.l,v $
.\" Revision 1.1  88/01/15  12:58:20  simpson
.\" initial release
.\" 
.\" Revision 0.1  87/12/11  17:56:51  simpson
.\" beta test
.\" 
.TH GETOPT 3
.SH NAME
getopt \- get option letter from argument vector
.SH SYNOPSIS
.B int getopt (argc, argv, optstring)
.br
.B int argc;
.br
.B char \(**\(**argv;
.br
.B char \(**optstring;
.PP
.B extern char \(**optarg;
.br
.B extern int optind;
.PP
cc ... -ltrw
.SH DESCRIPTION
.I Getopt\^
returns the next option letter in
.I argv\^
that matches
a letter in
.IR optstring .
.I Optstring\^
is a string of recognized option letters;
if a letter is followed by a colon, the option
is expected to have an argument that may or
may not be separated from it by white space.
.I Optarg\^
is set to point to the start of the option argument
on return from
.IR getopt .
.PP
.I Getopt\^
places in
.I optind\^
the
.I argv\^
index of the next argument to be processed.
Because
.I optind\^
is external, it is normally initialized to zero
automatically before the first call to
.IR getopt .
.PP
When all options have been processed
(i.e., up to the first non-option argument),
.I getopt\^
returns
.SM
.BR EOF .
The special option
.B \-\-
may be used to delimit the end of the options;
.SM
.B EOF
will be returned, and
.B \-\-
will be skipped.
.SH DIAGNOSTICS
.I Getopt\^
prints an error message on
.I stderr\^
and returns a
question mark
.RB ( ? )
when it encounters an option letter not included in
.IR optstring .
.SH WARNING
The above routine uses \fB<stdio.h>\fP, which causes 
it to increase the size of programs,
not otherwise using standard I/O, more
than might be expected.
.SH EXAMPLE
The following code fragment shows how one might process the arguments
for a command that can take the mutually exclusive options
.B a
and
.BR b ,
and the options
.B f
and
.BR o ,
both of which require arguments:
.PP
.RS
.nf
.ss 18
main (argc, argv)
int argc;
char \(**\(**argv;
{
	int c;
	extern int optind;
	extern char \(**optarg;
	\&\f3.\fP
	\&\f3.\fP
	\&\f3.\fP
	while ((c = getopt (argc, argv, "abf:o:")) != \s-1EOF\s+1)
		switch (c) {
		case \(fma\(fm:
			if (bflg)
				errflg++;
			else
				aflg++;
			break;
		case \(fmb\(fm:
			if (aflg)
				errflg++;
			else
				bproc( );
			break;
		case \(fmf\(fm:
			ifile = optarg;
			break;
		case \(fmo\(fm:
			ofile = optarg;
			bufsiza = 512;
			break;
		case \(fm?\(fm:
			errflg++;
		}
	if (errflg) {
		fprintf (stderr, "usage: . . . ");
		exit (2);
	}
	for ( ; optind < argc; optind++) {
		if (access (argv[optind], 4)) {
	\&\f3.\fP
	\&\f3.\fP
	\&\f3.\fP
}
.ss 12
.fi
.RE
.\"	@(#)getopt.3c	5.2 of 5/18/82