Subversion Repositories planix.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include	<stdlib.h>
2
#include	<string.h>
3
#include	<stdio.h>
4
#define ERR(str, chr)       if(opterr){fprintf(stderr, "%s%s%c\n", argv[0], str, chr);}
5
int     opterr = 1;
6
int     optind = 1;
7
int	optopt;
8
char    *optarg;
9
 
10
int
11
getopt (int argc, char **argv, char *opts)
12
{
13
	static int sp = 1;
14
	register c;
15
	register char *cp;
16
 
17
	if (sp == 1)
18
		if (optind >= argc ||
19
		   argv[optind][0] != '-' || argv[optind][1] == '\0')
20
			return EOF;
21
		else if (strcmp(argv[optind], "--") == 0) {
22
			optind++;
23
			return EOF;
24
		}
25
	optopt = c = argv[optind][sp];
26
	if (c == ':' || (cp=strchr(opts, c)) == NULL) {
27
		ERR (": illegal option -- ", c);
28
		if (argv[optind][++sp] == '\0') {
29
			optind++;
30
			sp = 1;
31
		}
32
		return '?';
33
	}
34
	if (*++cp == ':') {
35
		if (argv[optind][sp+1] != '\0')
36
			optarg = &argv[optind++][sp+1];
37
		else if (++optind >= argc) {
38
			ERR (": option requires an argument -- ", c);
39
			sp = 1;
40
			return '?';
41
		} else
42
			optarg = argv[optind++];
43
		sp = 1;
44
	} else {
45
		if (argv[optind][++sp] == '\0') {
46
			sp = 1;
47
			optind++;
48
		}
49
		optarg = NULL;
50
	}
51
	return c;
52
}