git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@332 77e5149b-7576-45b1-b177-96237e5ba77b
Thomas Lamy authored on 2004/02/24 08:26:06... | ... |
@@ -29,6 +29,10 @@ |
29 | 29 |
|
30 | 30 |
int freshclam(struct optstruct *opt); |
31 | 31 |
|
32 |
+static void register_char_opt(struct optstruct *opt, char ch, struct option* longopts); |
|
33 |
+static void register_long_opt(struct optstruct *opt, const char *optname, struct option* longopts); |
|
34 |
+ |
|
35 |
+ |
|
32 | 36 |
int main(int argc, char **argv) |
33 | 37 |
{ |
34 | 38 |
int ret, opt_index, i, len; |
... | ... |
@@ -77,12 +81,12 @@ int main(int argc, char **argv) |
77 | 77 |
|
78 | 78 |
switch (ret) { |
79 | 79 |
case 0: |
80 |
- register_long_option(opt, long_options[opt_index].name); |
|
80 |
+ register_long_opt(opt, long_options[opt_index].name, long_options); |
|
81 | 81 |
break; |
82 | 82 |
|
83 | 83 |
default: |
84 | 84 |
if(strchr(getopt_parameters, ret)) |
85 |
- register_char_option(opt, ret); |
|
85 |
+ register_char_opt(opt, ret, long_options); |
|
86 | 86 |
else { |
87 | 87 |
mprintf("!Unknown option passed.\n"); |
88 | 88 |
free_opt(opt); |
... | ... |
@@ -118,28 +122,63 @@ int main(int argc, char **argv) |
118 | 118 |
return ret; |
119 | 119 |
} |
120 | 120 |
|
121 |
-void register_char_option(struct optstruct *opt, char ch) |
|
121 |
+static struct option* find_char_opt(char optchar, struct option* longopts) |
|
122 |
+{ |
|
123 |
+ int i; |
|
124 |
+ |
|
125 |
+ for (i=0; longopts[i].name; i++) { |
|
126 |
+ if ((char) longopts[i].val == optchar) { |
|
127 |
+ return (&longopts[i]); |
|
128 |
+ } |
|
129 |
+ } |
|
130 |
+ return NULL; |
|
131 |
+} |
|
132 |
+ |
|
133 |
+static void register_char_opt(struct optstruct *opt, char ch, struct option* longopts) |
|
122 | 134 |
{ |
123 | 135 |
struct optnode *newnode; |
136 |
+ struct option *longopt = find_char_opt(ch, longopts); |
|
124 | 137 |
|
125 | 138 |
newnode = (struct optnode *) mmalloc(sizeof(struct optnode)); |
139 |
+ |
|
126 | 140 |
newnode->optchar = ch; |
127 | 141 |
if(optarg != NULL) { |
128 | 142 |
newnode->optarg = (char *) mcalloc(strlen(optarg) + 1, sizeof(char)); |
129 | 143 |
strcpy(newnode->optarg, optarg); |
130 | 144 |
} else newnode->optarg = NULL; |
131 | 145 |
|
132 |
- newnode->optname = NULL; |
|
146 |
+ if (longopt) { |
|
147 |
+ newnode->optname = strdup(longopt->name); |
|
148 |
+ } else { |
|
149 |
+ newnode->optname = NULL; |
|
150 |
+ } |
|
133 | 151 |
newnode->next = opt->optlist; |
134 | 152 |
opt->optlist = newnode; |
135 | 153 |
} |
136 | 154 |
|
137 |
-void register_long_option(struct optstruct *opt, const char *optname) |
|
155 |
+static struct option* find_long_opt(const char *optname, struct option* longopts) |
|
156 |
+{ |
|
157 |
+ int i; |
|
158 |
+ |
|
159 |
+ for (i=0; longopts[i].name; i++) { |
|
160 |
+ if (strcmp(longopts[i].name, optname) == 0) { |
|
161 |
+ return (&longopts[i]); |
|
162 |
+ } |
|
163 |
+ } |
|
164 |
+ return NULL; |
|
165 |
+} |
|
166 |
+ |
|
167 |
+static void register_long_opt(struct optstruct *opt, const char *optname, struct option* longopts) |
|
138 | 168 |
{ |
139 | 169 |
struct optnode *newnode; |
170 |
+ struct option *longopt = find_long_opt(optname, longopts); |
|
140 | 171 |
|
141 | 172 |
newnode = (struct optnode *) mmalloc(sizeof(struct optnode)); |
142 |
- newnode->optchar = 0; |
|
173 |
+ if (longopt) { |
|
174 |
+ newnode->optchar = longopt->val; |
|
175 |
+ } else { |
|
176 |
+ newnode->optchar = 0; |
|
177 |
+ } |
|
143 | 178 |
if(optarg != NULL) { |
144 | 179 |
newnode->optarg = (char *) mcalloc(strlen(optarg) + 1, sizeof(char)); |
145 | 180 |
strcpy(newnode->optarg, optarg); |