1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2002, 2007 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: mygetopt.c 2544 2008-07-16 17:23:33Z sam $ |
---|
7 | * |
---|
8 | * This program is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * mygetopt.c: getopt_long reimplementation |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #if defined HAVE_STDINT_H |
---|
22 | # include <stdint.h> |
---|
23 | #elif defined HAVE_INTTYPES_H |
---|
24 | # include <inttypes.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #include <stdio.h> |
---|
28 | #include <string.h> |
---|
29 | |
---|
30 | #include "mygetopt.h" |
---|
31 | |
---|
32 | int myoptind = 1; |
---|
33 | char *myoptarg = NULL; |
---|
34 | |
---|
35 | /* XXX: this getopt_long implementation should not be trusted for other |
---|
36 | * applications without any serious peer reviewing. It “just works” with |
---|
37 | * zzuf but may fail miserably in other programs. */ |
---|
38 | int mygetopt(int argc, char * const _argv[], const char *optstring, |
---|
39 | const struct myoption *longopts, int *longindex) |
---|
40 | { |
---|
41 | char **argv = (char **)(uintptr_t)_argv; |
---|
42 | char *flag; |
---|
43 | int i; |
---|
44 | |
---|
45 | if(myoptind >= argc) |
---|
46 | return -1; |
---|
47 | |
---|
48 | flag = argv[myoptind]; |
---|
49 | |
---|
50 | if(flag[0] == '-' && flag[1] != '-') |
---|
51 | { |
---|
52 | char *tmp; |
---|
53 | int ret = flag[1]; |
---|
54 | |
---|
55 | if(ret == '\0') |
---|
56 | return -1; |
---|
57 | |
---|
58 | tmp = strchr(optstring, ret); |
---|
59 | if(!tmp || ret == ':') |
---|
60 | return '?'; |
---|
61 | |
---|
62 | myoptind++; |
---|
63 | if(tmp[1] == ':') |
---|
64 | { |
---|
65 | if(flag[2] != '\0') |
---|
66 | myoptarg = flag + 2; |
---|
67 | else |
---|
68 | myoptarg = argv[myoptind++]; |
---|
69 | return ret; |
---|
70 | } |
---|
71 | |
---|
72 | if(flag[2] != '\0') |
---|
73 | { |
---|
74 | flag[1] = '-'; |
---|
75 | myoptind--; |
---|
76 | argv[myoptind]++; |
---|
77 | } |
---|
78 | |
---|
79 | return ret; |
---|
80 | } |
---|
81 | |
---|
82 | if(flag[0] == '-' && flag[1] == '-') |
---|
83 | { |
---|
84 | if(flag[2] == '\0') |
---|
85 | return -1; |
---|
86 | |
---|
87 | for(i = 0; longopts[i].name; i++) |
---|
88 | { |
---|
89 | size_t l = strlen(longopts[i].name); |
---|
90 | |
---|
91 | if(strncmp(flag + 2, longopts[i].name, l)) |
---|
92 | continue; |
---|
93 | |
---|
94 | switch(flag[2 + l]) |
---|
95 | { |
---|
96 | case '=': |
---|
97 | if(!longopts[i].has_arg) |
---|
98 | goto bad_opt; |
---|
99 | if(longindex) |
---|
100 | *longindex = i; |
---|
101 | myoptind++; |
---|
102 | myoptarg = flag + 2 + l + 1; |
---|
103 | return longopts[i].val; |
---|
104 | case '\0': |
---|
105 | if(longindex) |
---|
106 | *longindex = i; |
---|
107 | myoptind++; |
---|
108 | if(longopts[i].has_arg) |
---|
109 | myoptarg = argv[myoptind++]; |
---|
110 | return longopts[i].val; |
---|
111 | default: |
---|
112 | break; |
---|
113 | } |
---|
114 | } |
---|
115 | bad_opt: |
---|
116 | fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag); |
---|
117 | return '?'; |
---|
118 | } |
---|
119 | |
---|
120 | return -1; |
---|
121 | } |
---|
122 | |
---|