2 * Copyright (c) 1984 through 2008, William LeFebvre
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
16 * * Neither the name of William LeFebvre nor the names of other
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * "getopt" routine customized for top.
38 * Many modern-day Unix implementations already have this function
39 * in libc. The standard "getopt" is perfectly sufficient for top's
40 * needs. If such a function exists in libc then you certainly don't
41 * need to compile this one in. To prevent this function from being
42 * compiled, define "HAVE_GETOPT". This is usually done in the "CFLAGS"
43 * line of the corresponding machine module.
47 * This empty declaration exists solely to placate overexhuberant C
48 * compilers that like to warn you about content-free files.
50 static void __empty();
63 #define ERR(s, c) if(opterr){\
66 errbuf[0] = c; errbuf[1] = '\n';\
67 (void) write(2, argv[0], strlen(argv[0]));\
68 (void) write(2, s, strlen(s));\
69 (void) write(2, errbuf, 2);}
78 getopt(int argc
, char **argv
, char *opts
)
87 argv
[optind
][0] != '-' || argv
[optind
][1] == '\0')
89 else if(strcmp(argv
[optind
], "--") == 0) {
93 optopt
= c
= argv
[optind
][sp
];
94 if(c
== ':' || (cp
=strchr(opts
, c
)) == NULL
) {
95 ERR(": unknown option, -", c
);
96 if(argv
[optind
][++sp
] == '\0') {
103 if(argv
[optind
][sp
+1] != '\0')
104 optarg
= &argv
[optind
++][sp
+1];
105 else if(++optind
>= argc
) {
106 ERR(": argument missing for -", c
);
110 optarg
= argv
[optind
++];
113 if(argv
[optind
][++sp
] == '\0') {
121 #endif /* HAVE_GETOPT */