blob: 8508f401a725329c5aeb35766efaa06392c43d79 (
plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
/*
* Minimum getopt, original version was:
*/
/*
getopt -- public domain version of standard System V routine
Strictly enforces the System V Command Syntax Standard;
provided by D A Gwyn of BRL for generic ANSI C implementations
*/
/* $Id: getopt.c,v 1.2 1992/12/07 11:12:52 nickc Exp $ */
#include "common.h" /* definition of FLASH */
#include <string.h>
int optind = 0; /* next argv[] index */
char *optarg; /* option parameter if any */
int
getopt( /* returns letter, '?', EOF */
int argc, /* argument count from main */
char *const argv[], /* argument vector from main */
const FLASH char *optstring ) /* allowed args, e.g. "ab:c" */
{
static int sp; /* position within argument */
int osp; /* saved `sp' for param test */
int c; /* option letter */
const FLASH char *cp; /* -> option in `optstring' */
optarg = NULL;
if (optind == 0) { /* start a new argument scan */
optind = 1;
sp = 1;
}
if ( sp == 1 ) /* fresh argument */
{
if ( optind >= argc /* no more arguments */
|| argv[optind][0] != '-' /* no more options */
|| argv[optind][1] == '\0' /* not option; stdin */
)
return -1;
}
c = argv[optind][sp]; /* option letter */
osp = sp++; /* get ready for next letter */
if ( argv[optind][sp] == '\0' ) /* end of argument */
{
++optind; /* get ready for next try */
sp = 1; /* beginning of next argument */
}
if ( c == ':' /* optstring syntax conflict */
|| (cp = strchr_P( optstring, c )) == NULL /* not found */
)
return '?';
if ( cp[1] == ':' ) /* option takes parameter */
{
if ( osp != 1 )
return '?';
if ( sp != 1 ) /* reset by end of argument */
return '?';
if ( optind >= argc )
return '?';
optarg = argv[optind]; /* make parameter available */
++optind; /* skip over parameter */
}
return c;
}
|