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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/*
* Copyright (C) 2001-2002 Tomasz Kojm <zolw@konarski.edu.pl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Sat Sep 14 22:18:20 CEST 2002: included getfirst*(), getnext*() functions
* from Alejandro Dubrovsky <s328940@student.uq.edu.au>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <clamav.h>
#define _GNU_SOURCE
#include "getopt.h"
#if defined(C_LINUX) && defined(CL_DEBUG)
#include <sys/resource.h>
#endif
#include "options.h"
//#include "others.h"
//#include "shared.h"
#include "memory.h"
void clamdscan(struct optstruct *opt);
int main(int argc, char **argv)
{
int ret, opt_index, i, len;
struct optstruct *opt;
const char *getopt_parameters = "hvfd:wriVql:";
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"quiet", 0, 0, 0},
{"verbose", 0, 0, 'v'},
{"version", 0, 0, 'V'},
{"config-file", 1, 0, 0},
{"tempdir", 1, 0, 0},
{"database", 1, 0, 'd'},
{"quick", 0, 0, 'q'},
{"whole-file", 0, 0, 'w'}, /* not used */
{"force", 0, 0, 0},
{"recursive", 0, 0, 'r'},
{"disable-summary", 0, 0, 0},
{"infected", 0, 0, 'i'},
{"log", 1, 0, 'l'},
{"log-verbose", 0, 0, 0},
{"threads", 1, 0, 0},
{"one-virus", 0, 0, 0}, /* not used */
{"remove", 0, 0, 0},
{"exclude", 1, 0, 0},
{"include", 1, 0, 0},
{"max-files", 1, 0, 0},
{"max-space", 1, 0, 0},
{"max-recursion", 1, 0, 0},
{"disable-archive", 0, 0, 0},
{"tag", 0, 0, 0},
{"foobar", 0, 0, 'f'},
{"admin", 1, 0, 0},
{"quar", 1, 0, 0},
{"mta", 1, 0, 0},
{"spam", 0, 0, 0},
{"unzip", 2, 0, 0},
{"unrar", 2, 0, 0},
{"unace", 2, 0, 0},
{"unarj", 2, 0, 0},
{"zoo", 2, 0, 0},
{"lha", 2, 0, 0},
{"jar", 2, 0, 0},
{"tar", 2, 0, 0},
{"tgz", 2, 0, 0},
{"deb", 2, 0, 0},
{0, 0, 0, 0}
};
#if defined(C_LINUX) && defined(CL_DEBUG)
/* njh@bandsman.co.uk: create a dump if needed */
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
if(setrlimit(RLIMIT_CORE, &rlim) < 0)
perror("setrlimit");
#endif
opt=(struct optstruct*) mmalloc(sizeof(struct optstruct));
opt->optlist = NULL;
while(1) {
opt_index=0;
ret=getopt_long(argc, argv, getopt_parameters, long_options, &opt_index);
if (ret == -1)
break;
switch (ret) {
case 0:
register_long_option(opt, long_options[opt_index].name);
break;
default:
if(strchr(getopt_parameters, ret))
register_char_option(opt, ret);
else {
mprintf("!Unknown option passed.\n");
free_opt(opt);
exit(40);
}
}
}
if (optind < argc) {
len=0;
/* count length of non-option arguments */
for(i=optind; i<argc; i++)
len+=strlen(argv[i]);
len=len+argc-optind-1; /* add spaces between arguments */
opt->filename=(char*)mcalloc(len + 256, sizeof(char));
for(i=optind; i<argc; i++) {
strncat(opt->filename, argv[i], strlen(argv[i]));
if(i != argc-1)
strncat(opt->filename, " ", 1);
}
} else
/* FIXME !!! Without this, we have segfault */
opt->filename=(char*)mcalloc(1, sizeof(char));
clamdscan(opt);
return(0);
}
void register_char_option(struct optstruct *opt, char ch)
{
struct optnode *newnode;
newnode = (struct optnode *) mmalloc(sizeof(struct optnode));
newnode->optchar = ch;
if(optarg != NULL) {
newnode->optarg = (char *) mcalloc(strlen(optarg) + 1, sizeof(char));
strcpy(newnode->optarg, optarg);
} else newnode->optarg = NULL;
newnode->optname = NULL;
newnode->next = opt->optlist;
opt->optlist = newnode;
}
void register_long_option(struct optstruct *opt, const char *optname)
{
struct optnode *newnode;
newnode = (struct optnode *) mmalloc(sizeof(struct optnode));
newnode->optchar = 0;
if(optarg != NULL) {
newnode->optarg = (char *) mcalloc(strlen(optarg) + 1, sizeof(char));
strcpy(newnode->optarg, optarg);
} else newnode->optarg = NULL;
newnode->optname = (char *) mcalloc(strlen(optname) + 1, sizeof(char));
strcpy(newnode->optname, optname);
newnode->next = opt->optlist;
opt->optlist = newnode;
}
int optc(const struct optstruct *opt, char ch)
{
struct optnode *handler;
handler = opt->optlist;
while(1) {
if(handler) {
if(handler->optchar == ch) return 1;
} else break;
handler = handler->next;
}
return(0);
}
int optl(const struct optstruct *opt, const char *optname)
{
struct optnode *handler;
handler = opt->optlist;
while(1) {
if(handler) {
if(handler->optname)
if(!strcmp(handler->optname, optname)) return 1;
} else break;
handler = handler->next;
}
return(0);
}
char *getargc(const struct optstruct *opt, char ch)
{
struct optnode *handler;
handler = opt->optlist;
while(1) {
if(handler) {
if(handler->optchar == ch) return handler->optarg;
} else break;
handler = handler->next;
}
return(NULL);
}
char *getfirstargc(const struct optstruct *opt, char ch, struct optnode **optnode)
{
struct optnode *handler;
handler = opt->optlist;
while(1) {
if(handler) {
if(handler->optchar == ch) {
*optnode = handler;
return handler->optarg;
}
} else break;
handler = handler->next;
}
*optnode = NULL;
return(NULL);
}
char *getnextargc(struct optnode **optnode, char ch)
{
struct optnode *handler;
handler = (*optnode)->next;
while(1) {
if(handler) {
if(handler->optchar == ch) {
*optnode = handler;
return handler->optarg;
}
} else break;
handler = handler->next;
}
*optnode = NULL;
return(NULL);
}
char *getargl(const struct optstruct *opt, const char *optname)
{
struct optnode *handler;
handler = opt->optlist;
while(1) {
if(handler) {
if(handler->optname)
if(!strcmp(handler->optname, optname)) return handler->optarg;
} else break;
handler = handler->next;
}
return(NULL);
}
char *getfirstargl(const struct optstruct *opt, const char *optname, struct optnode **optnode)
{
struct optnode *handler;
handler = opt->optlist;
while(1) {
if(handler) {
if(handler->optname)
if(!strcmp(handler->optname, optname)) {
*optnode = handler;
return handler->optarg;
}
} else break;
handler = handler->next;
}
*optnode = NULL;
return(NULL);
}
char *getnextargl(struct optnode **optnode, const char *optname)
{
struct optnode *handler;
handler = (*optnode)->next;
while(1) {
if(handler) {
if(handler->optname)
if(!strcmp(handler->optname, optname)) {
*optnode = handler;
return handler->optarg;
}
} else break;
handler = handler->next;
}
*optnode = NULL;
return(NULL);
}
void free_opt(struct optstruct *opt)
{
struct optnode *handler, *prev;
if(!opt || !opt->optlist)
return;
mprintf("*Freeing option list... ");
handler = opt->optlist;
while(handler != NULL) {
handler->optchar = 0;
if(handler->optarg) free(handler->optarg);
if(handler->optname) free(handler->optname);
prev = handler;
handler = handler->next;
free(prev);
}
free(opt->filename);
free(opt);
mprintf("*done\n");
}
|