-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetopt.c
executable file
·68 lines (57 loc) · 1.66 KB
/
getopt.c
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
/* Get Option Parser for Command Line Parsing
* Copyright (C) 1993-1996, 2005 Marko Kohtala
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <ctype.h>
#include "getopt.h"
/* Scan the given string to find options
str String to parse
switches Pointer to table of switches
lenght Lenght of switches table
Return: index into switches table telling which switch was parsed
*/
int get_opt(char **str, struct switch_s* switches, int length)
{
char *argp = *str;
char *end_ptr = argp;
int match = GETOPT_PARAM;
/* Skip white space */
while(isspace(*argp))
argp++;
*str = argp;
if (*argp == '/' || *argp == '-') {
int sw; /* Switch in turn */
int best_len = 0; /* How long the switch is on command line */
int ambiguous = 0; /* Is the best switch ambiguous */
argp++;
for (sw = 0; sw < length; sw++) {
char *s = switches[sw].sw;
char *arg = argp;
int m = 0;
while(*arg && *s && *s == toupper(*arg))
s++, arg++, m++;
if (m && !isalpha(*arg)) {
if (!*s || m > best_len) {
best_len = m; /* New best candidate (or exact match) */
match = sw;
end_ptr = arg;
ambiguous = 0;
if (!*s)
break; /* out of sw loop */
}
else if (m == best_len)
ambiguous = 1; /* As good choise as the previous */
}
}
if (!best_len)
match = GETOPT_NOMATCH;
else if (ambiguous)
match = GETOPT_AMBIGUOUS;
else /* If Match */ {
if (*end_ptr == ':')
end_ptr++;
*str = end_ptr;
}
}
return match;
}