"Fossies" - the Fresh Open Source Software Archive 
Member "apg-2.2.3/apgbfm.c" (12 Sep 2003, 12113 Bytes) of package /linux/privat/old/apg-2.2.3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 ** Copyright (c) 2001, 2002, 2003
3 ** Adel I. Mirzazhanov. All rights reserved
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 **
9 ** 1.Redistributions of source code must retain the above copyright notice,
10 ** this list of conditions and the following disclaimer.
11 ** 2.Redistributions in binary form must reproduce the above copyright
12 ** notice, this list of conditions and the following disclaimer in the
13 ** documentation and/or other materials provided with the distribution.
14 ** 3.The name of the author may not be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 ** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include "bloom.h"
34 #include "errs.h"
35 #include "getopt.h"
36
37
38 #define VERSION "2.2.3"
39
40
41 #define FOUND "FOUND"
42 #define NOT_FOUND "NOT FOUND"
43 /*
44 #define FOUND "YES"
45 #define NOT_FOUND "NO"
46 */
47
48 int main (int argc, char *argv[]);
49 void print_help(void);
50 void checkopt(char *opt);
51 void print_filter_info(char * filter);
52
53 int
54 main (int argc, char *argv[])
55 {
56 int option = 0;
57
58 char *dictfile; /* dictionary filename */
59 FILE *f_dictfile; /* dictionary file descriptor */
60
61 char *filter; /* filter file name */
62 FILE *f_filter; /* filter file descriptor */
63
64 char *word; /* word to add or check */
65 char *tmp; /* just tmp char pointer */
66 h_val wc = 0L; /* amount of words to build dictionaty */
67 h_val filter_size =0L; /* filter size in bits */
68 int dummy_test = 0; /* variable to make dummy test for */
69 /* options correctness */
70 h_val i = 0L; /* counter */
71 f_mode flt_mode = 0x00; /* filter mode */
72
73 /* flags */
74 flag add_word_flag = FALSE; /* -a */
75 flag add_file_flag = FALSE; /* -A */
76 flag check_word_flag = FALSE; /* -c */
77 flag check_file_flag = FALSE; /* -C */
78 flag new_flag = FALSE; /* -n */
79 flag new_from_dict_flag = FALSE; /* -d */
80 flag filter_flag = FALSE; /* -f */
81 flag silent_flag = FALSE; /* -q */
82 flag case_insensitive_flag = FALSE; /* -q */
83 /* end of flags section */
84
85 /* Analize options */
86 if (argc < 2)
87 {
88 print_help();
89 exit(-1);
90 }
91
92 while ((option = apg_getopt (argc, argv, "a:A:c:C:n:d:f:i:hvqs")) != -1)
93 {
94 switch(option)
95 {
96 case 'a':
97 word = apg_optarg;
98 add_word_flag = TRUE;
99 dummy_test = dummy_test + 2;
100 break;
101 case 'A':
102 dictfile = apg_optarg;
103 add_file_flag = TRUE;
104 dummy_test = dummy_test + 2;
105 break;
106 case 'c':
107 word = apg_optarg;
108 check_word_flag = TRUE;
109 dummy_test = dummy_test + 2;
110 break;
111 case 'C':
112 dictfile = apg_optarg;
113 check_file_flag = TRUE;
114 dummy_test = dummy_test + 2;
115 break;
116 case 'n':
117 checkopt(apg_optarg);
118 wc = atoi(apg_optarg);
119 new_flag = TRUE;
120 dummy_test = dummy_test + 2;
121 break;
122 case 'd':
123 dictfile = apg_optarg;
124 new_from_dict_flag = TRUE;
125 dummy_test = dummy_test + 2;
126 break;
127 case 'f':
128 filter = apg_optarg;
129 filter_flag = TRUE;
130 dummy_test = dummy_test + 1;
131 break;
132 case 'h':
133 print_help();
134 return (0);
135 case 'v':
136 printf ("APG Bloom filter management programm");
137 printf ("\nversion %s", VERSION);
138 printf ("\nCopyright (c) 2001, 2002, 2003 Adel I. Mirzazhanov\n");
139 return (0);
140 case 'i':
141 print_filter_info(apg_optarg);
142 return (0);
143 case 'q':
144 silent_flag = TRUE;
145 break;
146 case 's':
147 flt_mode = flt_mode | BF_CASE_INSENSITIVE;
148 case_insensitive_flag = TRUE;
149 break;
150 default:
151 print_help();
152 exit(-1);
153 }
154 }
155 if (filter_flag != TRUE) err_app_fatal ("apg", "-f option is required");
156 if (dummy_test != 3) err_app_fatal ("apg", "too many options");
157 /* Main part */
158 /* At this point we can be sure that all options a correct */
159 if (add_word_flag == TRUE) /* -a word */
160 {
161 if ( (f_filter = open_filter(filter, "r+")) == NULL)
162 err_sys_fatal("open_filter");
163 filter_size = get_filtersize(f_filter);
164 flt_mode = get_filtermode(f_filter);
165 if (filter_size == 0) err_sys_fatal("get_filtersize");
166 if ( insert_word (word, f_filter, filter_size, flt_mode) == -1)
167 err_sys_fatal("insert_word");
168 if (silent_flag != TRUE)
169 printf ("Word %s added\n",word);
170 return (0);
171 }
172 if (add_file_flag == TRUE) /* -A dictfile */
173 {
174 word = (char *) calloc(1,MAX_DICT_STRLEN);
175 if ( (f_dictfile = fopen(dictfile,"r")) == NULL)
176 err_sys_fatal("fopen");
177 if( (f_filter = open_filter(filter,"r+")) == NULL)
178 err_sys_fatal("open_filter");
179 filter_size = get_filtersize(f_filter);
180 flt_mode = get_filtermode(f_filter);
181 if (filter_size == 0) err_sys_fatal("get_filtersize");
182 while ((fgets(word, MAX_DICT_STRLEN, f_dictfile) != NULL))
183 {
184 tmp = (char *)strtok (word," \t\n\0");
185 if( tmp != NULL)
186 word = tmp;
187 else continue;
188 if ( insert_word (word, f_filter, filter_size, flt_mode) == -1)
189 err_sys_fatal("insert_word");
190 i++;
191 if (silent_flag != TRUE)
192 {
193 if ( i % 100 == 0)
194 {
195 fprintf (stdout,".");
196 fflush (stdout);
197 }
198 }
199 (void)memset((void *)word, 0, MAX_DICT_STRLEN);
200 }
201 if (silent_flag != TRUE) printf ("\n");
202 free ( (void *)word);
203 fclose (f_dictfile);
204 close_filter (f_filter);
205 return (0);
206 }
207 if (check_word_flag == TRUE) /* -c word */
208 {
209 if ( (f_filter = open_filter(filter, "r")) == NULL)
210 err_sys_fatal("open_filter");
211 filter_size = get_filtersize(f_filter);
212 flt_mode = get_filtermode(f_filter);
213
214 if (filter_size == 0) err_sys_fatal("get_filtersize");
215 switch(check_word (word, f_filter, filter_size, flt_mode))
216 {
217 case -1:
218 err_sys_fatal("check_word");
219 break;
220 case 1:
221 printf ("%s: %s \n",word, FOUND);
222 break;
223 case 0:
224 printf ("%s: %s\n",word, NOT_FOUND);
225 break;
226 }
227 return (0);
228 }
229 if (check_file_flag == TRUE) /* -C dictfile */
230 {
231 word = (char *) calloc(1,MAX_DICT_STRLEN);
232 if ( (f_dictfile = fopen(dictfile,"r")) == NULL)
233 err_sys_fatal("fopen");
234 wc = count_words (f_dictfile);
235 if (wc == 0) err_sys_fatal("count_words");
236 if( (f_filter = open_filter(filter, "r")) == NULL)
237 err_sys_fatal("open_filter");
238 filter_size = get_filtersize(f_filter);
239 flt_mode = get_filtermode(f_filter);
240
241 if (filter_size == 0) err_sys_fatal("get_filtersize");
242 while ((fgets(word, MAX_DICT_STRLEN, f_dictfile) != NULL))
243 {
244 tmp = (char *)strtok (word," \t\n\0");
245 if( tmp != NULL)
246 word = tmp;
247 else continue;
248 switch(check_word (word, f_filter, filter_size, flt_mode))
249 {
250 case -1:
251 err_sys_fatal("check_word");
252 break;
253 case 1:
254 printf ("%s: %s\n",word, FOUND);
255 break;
256 case 0:
257 printf ("%s: %s\n",word, NOT_FOUND);
258 break;
259 }
260 (void)memset((void *)word, 0, MAX_DICT_STRLEN);
261 }
262 free ( (void *)word);
263 fclose (f_dictfile);
264 close_filter (f_filter);
265 return (0);
266 }
267 if (new_flag == TRUE) /* -n nwords */
268 {
269 if ((f_filter = create_filter(filter, wc, flt_mode)) == NULL)
270 err_sys_fatal("create_filter");
271 close_filter(f_filter);
272 return (0);
273 }
274 if (new_from_dict_flag == TRUE) /* -d dictfile */
275 {
276 word = (char *) calloc(1,MAX_DICT_STRLEN);
277 if ( (f_dictfile = fopen(dictfile,"r")) == NULL)
278 err_sys_fatal("fopen");
279 if (silent_flag != TRUE)
280 {
281 fprintf (stdout,"Counting words in dictionary. Please wait...\n");
282 fflush (stdout);
283 }
284 wc = count_words (f_dictfile);
285 if (wc == 0) err_sys_fatal("count_words");
286 if( (f_filter = create_filter(filter, wc, flt_mode)) == NULL)
287 err_sys_fatal("create_filter");
288 filter_size = get_filtersize(f_filter);
289 if (filter_size == 0) err_sys_fatal("get_filtersize");
290 while ((fgets(word, MAX_DICT_STRLEN, f_dictfile) != NULL))
291 {
292 tmp = (char *)strtok (word," \t\n\0");
293 if( tmp != NULL)
294 {
295 word = tmp;
296 }
297 else
298 {
299 continue;
300 }
301 if ( insert_word (word, f_filter, filter_size, flt_mode) == -1)
302 err_sys_fatal("insert_word");
303 i++;
304 if (silent_flag != TRUE)
305 {
306 if ( i % 100 == 0)
307 {
308 fprintf (stdout, ".");
309 fflush (stdout);
310 }
311 }
312 (void)memset((void *)word, 0, MAX_DICT_STRLEN);
313 }
314 if (silent_flag != TRUE) printf ("\n");
315 free ( (void *)word);
316 fclose (f_dictfile);
317 close_filter (f_filter);
318 return (0);
319 }
320 return (0);
321 }
322 /*
323 ** print_help() - prints short help info
324 ** INPUT:
325 ** none.
326 ** OUTPUT:
327 ** prints help info to the stdout.
328 ** NOTES:
329 ** none.
330 */
331 void
332 print_help(void)
333 {
334 printf ("\napgbfm APG Bloom filter management\n");
335 printf (" Copyright (c) 2001 Adel I. Mirzazhanov\n");
336 printf ("\napgbfm -f filter < [-a word] | [-A dictfile] | [-n numofwords] |\n");
337 printf (" [-c word] | [-C dictfile] | [-d dictfile] > [-s]\n");
338 printf ("apgbfm -i filter\n");
339 printf ("apgbfm [-v] [-h]\n\n");
340 printf ("-a word add word to filter\n");
341 printf ("-A dictfile add words from dictfile to filter\n");
342 printf ("-c word check word against filter\n");
343 printf ("-C dictfile check dictfile against filter\n");
344 printf ("-n numofwords create new empty filter\n");
345 printf ("-d dictfile create new filter and add all words from dictfile\n");
346 printf ("-f filtername use filtername as the name for filter\n");
347 printf ("-q quiet mode (do not print dots for -A and -d)\n");
348 printf ("-s create case insentive filter\n");
349 printf ("-i filter print filter information\n");
350 printf ("-v print version information\n");
351 printf ("-h print help (this screen)\n");
352 }
353
354 /*
355 ** checkopt() - check options
356 ** INPUT:
357 ** char * - option string.
358 ** OUTPUT:
359 ** none.
360 ** NOTES:
361 ** checks only is the option string numeral.
362 */
363 void
364 checkopt(char *opt)
365 {
366 int i;
367
368 for(i=0; i < strlen(opt);i++)
369 if(opt[i] != '0' && opt[i] != '1' && opt[i] != '2' && opt[i] != '3' &&
370 opt[i] != '4' && opt[i] != '5' && opt[i] != '6' && opt[i] != '7' &&
371 opt[i] != '8' && opt[i] != '9')
372 err_app_fatal ("checkopt", "wrong option format");
373 }
374
375 /*
376 ** print_filter_info(char * filter) - print filter information
377 ** INPUT:
378 ** char * - filter file name.
379 ** OUTPUT:
380 ** none.
381 ** NOTES:
382 ** none.
383 */
384 void
385 print_filter_info(char * filter)
386 {
387 FILE * f_filter;
388
389 if ( (f_filter = open_filter(filter, "r")) == NULL)
390 err_sys_fatal("open_filter");
391 if (( print_flt_info(f_filter)) == -1)
392 err_sys_fatal("print_flt_info");
393 close_filter(f_filter);
394 }