124139Sjoerg/*
224139Sjoerg * "getopt" routine customized for top.
324139Sjoerg */
424139Sjoerg
524139Sjoerg/*
624139Sjoerg * Many modern-day Unix implementations already have this function
724139Sjoerg * in libc.  The standard "getopt" is perfectly sufficient for top's
824139Sjoerg * needs.  If such a function exists in libc then you certainly don't
924139Sjoerg * need to compile this one in.  To prevent this function from being
1024139Sjoerg * compiled, define "HAVE_GETOPT".  This is usually done in the "CFLAGS"
1124139Sjoerg * line of the corresponding machine module.
1224139Sjoerg */
1324139Sjoerg
1424139Sjoerg/*
1524139Sjoerg * This empty declaration exists solely to placate overexhuberant C
1624139Sjoerg * compilers that like to warn you about content-free files.
1724139Sjoerg */
1824139Sjoergstatic void __empty();
1924139Sjoerg
2024139Sjoerg#ifndef HAVE_GETOPT
2124139Sjoerg
2224139Sjoerg/*LINTLIBRARY*/
2324139Sjoerg
2424139Sjoerg#include "os.h"
2524139Sjoerg#ifndef NULL
2624139Sjoerg#define NULL	0
2724139Sjoerg#endif
2824139Sjoerg#ifndef EOF
2924139Sjoerg#define EOF	(-1)
3024139Sjoerg#endif
3124139Sjoerg#define ERR(s, c)	if(opterr){\
3224139Sjoerg	extern int write();\
3324139Sjoerg	char errbuf[2];\
3424139Sjoerg	errbuf[0] = c; errbuf[1] = '\n';\
3524139Sjoerg	(void) write(2, argv[0], strlen(argv[0]));\
3624139Sjoerg	(void) write(2, s, strlen(s));\
3724139Sjoerg	(void) write(2, errbuf, 2);}
3824139Sjoerg
3924139Sjoerg
4024139Sjoergint	opterr = 1;
4124139Sjoergint	optind = 1;
4224139Sjoergint	optopt;
4324139Sjoergchar	*optarg;
4424139Sjoerg
4524139Sjoergint
4624139Sjoerggetopt(argc, argv, opts)
4724139Sjoergint	argc;
4824139Sjoergchar	**argv, *opts;
4924139Sjoerg{
5024139Sjoerg	static int sp = 1;
5124139Sjoerg	register int c;
5224139Sjoerg	register char *cp;
5324139Sjoerg
5424139Sjoerg	if(sp == 1)
5524139Sjoerg		if(optind >= argc ||
5624139Sjoerg		   argv[optind][0] != '-' || argv[optind][1] == '\0')
5724139Sjoerg			return(EOF);
5824139Sjoerg		else if(strcmp(argv[optind], "--") == 0) {
5924139Sjoerg			optind++;
6024139Sjoerg			return(EOF);
6124139Sjoerg		}
6224139Sjoerg	optopt = c = argv[optind][sp];
6324139Sjoerg	if(c == ':' || (cp=strchr(opts, c)) == NULL) {
6424139Sjoerg		ERR(": unknown option, -", c);
6524139Sjoerg		if(argv[optind][++sp] == '\0') {
6624139Sjoerg			optind++;
6724139Sjoerg			sp = 1;
6824139Sjoerg		}
6924139Sjoerg		return('?');
7024139Sjoerg	}
7124139Sjoerg	if(*++cp == ':') {
7224139Sjoerg		if(argv[optind][sp+1] != '\0')
7324139Sjoerg			optarg = &argv[optind++][sp+1];
7424139Sjoerg		else if(++optind >= argc) {
7524139Sjoerg			ERR(": argument missing for -", c);
7624139Sjoerg			sp = 1;
7724139Sjoerg			return('?');
7824139Sjoerg		} else
7924139Sjoerg			optarg = argv[optind++];
8024139Sjoerg		sp = 1;
8124139Sjoerg	} else {
8224139Sjoerg		if(argv[optind][++sp] == '\0') {
8324139Sjoerg			sp = 1;
8424139Sjoerg			optind++;
8524139Sjoerg		}
8624139Sjoerg		optarg = NULL;
8724139Sjoerg	}
8824139Sjoerg	return(c);
8924139Sjoerg}
9024139Sjoerg#endif /* HAVE_GETOPT */
91