File: test2.c

package info (click to toggle)
trueprint 5.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 1,744 kB
  • ctags: 728
  • sloc: ansic: 8,296; sh: 1,225; makefile: 138
file content (58 lines) | stat: -rw-r--r-- 1,462 bytes parent folder | download | duplicates (11)
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
/* @(#)getopt.c */

#define _POSIX_SOURCE

#include <stdio.h>
#include <string.h>

#include "trueprint.h"
#include "main.h"

/*
 * get option letter from argument vector
 */
int		optind = 1;		/* index into parent argv vector */
char		*optarg;		/* argument associated with option */

static int	optopt;			/* character checked for validity */

int
getopt(int nargc, char **nargv, char *ostr)
{
	register char	*oli;		/* option letter list index */
	static char	*place = "";	/* option letter processing */

	if(!*place) {			/* update scanning pointer */
		if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
		  place = "";
		  return(EOF);
		}
		if (*place == '-') {	/* found "--" */
		  ++optind;
		  place = "";
		  return EOF;
		}
	}				/* option letter okay? */
	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
		if(!*place) ++optind;
		(void)fprintf(stderr, "%s: illegal option -- %c\n", cmd_name, optopt);
		return '?';
	}
	if (*++oli != ':') {		/* don't need argument */
		optarg = NULL;
		if (!*place) ++optind;
	} else {				/* need an argument */
		if (*place) {			/* no white space */
			optarg = place;
		} else if (nargc <= ++optind) {	/* no arg */
			place = "";
			(void)fprintf(stderr, "%s: option requires an argument -- %c\n", cmd_name, optopt);
			optopt = '?';
		} else {
			optarg = nargv[optind];	/* white space */
		}
		place = "";
		++optind;
	}
	return optopt;			/* dump back option letter */
}