Rename top(1) to mtop(1), import NetBSD top(1)
[minix3.git] / external / bsd / top / dist / getopt.c
blob9a4af2f4c1af020113d7d9e905c0fa0b1dbf63b2
1 /*
2 * Copyright (c) 1984 through 2008, William LeFebvre
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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
14 * distribution.
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();
52 #ifndef HAVE_GETOPT
54 /*LINTLIBRARY*/
56 #include "os.h"
57 #ifndef NULL
58 #define NULL 0
59 #endif
60 #ifndef EOF
61 #define EOF (-1)
62 #endif
63 #define ERR(s, c) if(opterr){\
64 extern int write();\
65 char errbuf[2];\
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);}
72 int opterr = 1;
73 int optind = 1;
74 int optopt;
75 char *optarg;
77 int
78 getopt(int argc, char **argv, char *opts)
81 static int sp = 1;
82 register int c;
83 register char *cp;
85 if(sp == 1)
86 if(optind >= argc ||
87 argv[optind][0] != '-' || argv[optind][1] == '\0')
88 return(EOF);
89 else if(strcmp(argv[optind], "--") == 0) {
90 optind++;
91 return(EOF);
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') {
97 optind++;
98 sp = 1;
100 return('?');
102 if(*++cp == ':') {
103 if(argv[optind][sp+1] != '\0')
104 optarg = &argv[optind++][sp+1];
105 else if(++optind >= argc) {
106 ERR(": argument missing for -", c);
107 sp = 1;
108 return('?');
109 } else
110 optarg = argv[optind++];
111 sp = 1;
112 } else {
113 if(argv[optind][++sp] == '\0') {
114 sp = 1;
115 optind++;
117 optarg = NULL;
119 return(c);
121 #endif /* HAVE_GETOPT */