1 /*
2 * ccsrch (c) 2007 Mike Beekey - zaphod2718@yahoo.com All rights reserved
3 *
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59
17 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <time.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31 #include "ccsrch.h"
32
33 char *logfilename = NULL;
34 char *currfilename = NULL;
35 FILE *logfilefd = NULL;
36 long total_count = 0;
37 long file_count = 0;
38 long currfile_atime=0;
39 long currfile_mtime=0;
40 long currfile_ctime=0;
41 int init_time = 0;
42 int cardbuf[CARDSIZE];
43 int print_byte_offset=0;
44 int print_epoch_time=0;
45 int print_julian_time=0;
46 int print_filename_only=0;
47 char ccsrch_buf[BSIZE];
48 int ccsrch_index = 0;
49 int tracksrch=0;
50 int tracktype1=0;
51 int tracktype2=0;
52 int trackdatacount=0;
53 char lastfilename[MAXPATH];
54 int filename_pan_count=0;
55
56 void
initialize_buffer()57 initialize_buffer()
58 {
59 int i=0;
60 for (i=0; i<CARDSIZE; i++)
61 cardbuf[i]=0;
62 }
63
64 void
print_result(char * cardname,int cardlen,long byte_offset)65 print_result(char *cardname, int cardlen, long byte_offset)
66 {
67 int i = 0;
68 char nbuf[20];
69 char buf[MAXPATH];
70 char basebuf[MDBUFSIZE];
71 char bytebuf[MDBUFSIZE];
72 char datebuf[MDBUFSIZE];
73 char mdatebuf[CARDTYPELEN];
74 char adatebuf[CARDTYPELEN];
75 char cdatebuf[CARDTYPELEN];
76 char trackbuf[MDBUFSIZE];
77
78 memset(&nbuf, '\0', 20);
79
80 for (i = 0; i < cardlen; i++)
81 nbuf[i] = cardbuf[i]+48;
82
83 memset(&buf,'\0',MAXPATH);
84 memset(&basebuf,'\0',MDBUFSIZE);
85
86 /* MB we need to figure out how to update the count and spit out the final filename with the count. ensure that it gets flushed out on the last match if you are doing a diff between previous filename and new filename */
87
88 if (print_filename_only)
89 snprintf(basebuf, MDBUFSIZE, "%s", currfilename);
90 else
91 snprintf(basebuf, MDBUFSIZE, "%s\t%s\t%s", currfilename, cardname, nbuf);
92
93 strncat(buf,basebuf,MAXPATH);
94
95 if (print_byte_offset)
96 {
97 memset(&bytebuf,'\0',MDBUFSIZE);
98 snprintf(bytebuf, MDBUFSIZE, "\t%ld", byte_offset);
99 strncat(buf,bytebuf,MAXPATH);
100 }
101
102 if (print_julian_time)
103 {
104 memset(&mdatebuf,'\0',CARDTYPELEN);
105 strncpy(mdatebuf,ctime((time_t *)&currfile_mtime),CARDTYPELEN);
106 mdatebuf[strlen(mdatebuf)-1]='\0';
107
108 memset(&adatebuf,'\0',CARDTYPELEN);
109 strncpy(adatebuf,ctime((time_t *)&currfile_atime),CARDTYPELEN);
110 adatebuf[strlen(adatebuf)-1]='\0';
111
112 memset(&cdatebuf,'\0',CARDTYPELEN);
113 strncpy(cdatebuf,ctime((time_t *)&currfile_ctime),CARDTYPELEN);
114 cdatebuf[strlen(cdatebuf)-1]='\0';
115
116 memset(&datebuf,'\0',MDBUFSIZE);
117 snprintf(datebuf, MDBUFSIZE, "\t%s\t%s\t%s", mdatebuf,adatebuf,cdatebuf);
118 strncat(buf,datebuf,MAXPATH);
119 }
120
121 if (print_epoch_time)
122 {
123 memset(&datebuf,'\0',MDBUFSIZE);
124 snprintf(datebuf, MDBUFSIZE, "\t%ld\t%ld\t%ld", currfile_mtime,currfile_atime,currfile_ctime);
125 strncat(buf,datebuf,MAXPATH);
126 }
127
128 if (tracksrch)
129 {
130 memset(&trackbuf,'\0',MDBUFSIZE);
131 if (tracktype1)
132 {
133 if (track1_srch(cardlen))
134 {
135 snprintf(trackbuf, MDBUFSIZE, "\tTRACK_1");
136 }
137 }
138 if (tracktype2)
139 {
140 if (track2_srch(cardlen))
141 {
142 snprintf(trackbuf, MDBUFSIZE, "\tTRACK_2");
143 }
144 }
145 strncat(buf,trackbuf,MAXPATH);
146 }
147 if (logfilefd != NULL)
148 fprintf(logfilefd, "%s\n", buf);
149 else
150 fprintf(stdout, "%s\n", buf);
151
152 total_count++;
153 }
154
track1_srch(int cardlen)155 int track1_srch(int cardlen)
156 {
157 /* [%:B:cardnum:^:name (first initial cap?, let's ignore the %)] */
158 if ((ccsrch_buf[ccsrch_index+1] == '^')
159 && (ccsrch_buf[ccsrch_index-cardlen] == 66)
160 && (ccsrch_buf[ccsrch_index+2] > 64)
161 && (ccsrch_buf[ccsrch_index+2] < 91))
162 {
163 trackdatacount++;
164 return(1);
165 }
166 else
167 return(0);
168 }
169
track2_srch(int cardlen)170 int track2_srch(int cardlen)
171 {
172 /* [;:cardnum:=:expir date(YYMM), we'll use the ; here] */
173 if (((ccsrch_buf[ccsrch_index+1] == '=') || (ccsrch_buf[ccsrch_index+1] == 68))
174 && ((ccsrch_buf[ccsrch_index-cardlen] == ';')||
175 ((ccsrch_buf[ccsrch_index-cardlen] > 57) || (ccsrch_buf[ccsrch_index-cardlen] < 91)) )
176 && ((ccsrch_buf[ccsrch_index+2] > 47)
177 && (ccsrch_buf[ccsrch_index+2] < 58))
178 && ((ccsrch_buf[ccsrch_index+3] > 47)
179 && (ccsrch_buf[ccsrch_index+3] < 58)))
180 {
181 trackdatacount++;
182 return(1);
183 }
184 else
185 return(0);
186 }
187
188
189 int
process_prefix(int len,long offset)190 process_prefix(int len, long offset)
191 {
192 switch (len)
193 {
194 case 16:
195 check_mastercard_16(offset);
196 check_visa_16(offset);
197 check_discover_16(offset);
198 check_jcb_16(offset);
199 break;
200 case 15:
201 check_amex_15(offset);
202 check_enroute_15(offset);
203 check_jcb_15(offset);
204 break;
205 case 14:
206 check_diners_club_cb_14(offset);
207 break;
208 /** It has been purported that these cards are no longer in use
209 case 13:
210 check_visa_13(offset);
211 break;
212 **/
213 }
214 return (0);
215 }
216
217
218 int
luhn_check(int len,long offset)219 luhn_check(int len, long offset)
220 {
221 int i = 0;
222 int tmp = 0;
223 int total = 0;
224 int nummod = 0;
225 int num[CARDSIZE];
226
227
228 if (cardbuf[i]<=0)
229 return(0);
230
231 for (i=0; i<CARDSIZE; i++)
232 num[i]=0;
233
234 for (i=0; i<len; i++)
235 num[i]=cardbuf[i];
236
237 #ifdef DEBUG
238 for (i=0; i<len; i++)
239 printf("%d ",num[i]);
240 printf("\n");
241 #endif
242
243 for (i=len-2; i>=0; i-=2)
244 {
245 tmp=2*num[i];
246 num[i]=tmp;
247 if (num[i]>9)
248 num[i]-=9;
249 }
250
251 for (i = 0; i < len; i++)
252 total += num[i];
253
254
255 nummod = total % 10;
256 if (nummod == 0)
257 {
258 #ifdef DEBUG
259 printf("Luhn Check passed ***********************************\n");
260 #endif
261 process_prefix(len, offset);
262 }
263 return (nummod);
264 }
265
266
267 int
ccsrch(char * filename)268 ccsrch(char *filename)
269 {
270 FILE *in = NULL;
271 int infd = 0;
272 int cnt = 0;
273 long byte_offset=1;
274 int k = 0;
275 int counter = 0;
276 int total = 0;
277 int check = 0;
278
279 memset(&lastfilename,'\0',MAXPATH);
280 ccsrch_index=0;
281 errno = 0;
282 in = fopen(filename, "rb");
283 if (in == NULL)
284 {
285 if (errno==13)
286 fprintf(stderr, "ccsrch: Unable to open file %s for reading; Permission Denied\n", filename);
287 else
288 fprintf(stderr, "ccsrch: Unable to open file %s for reading; errno=%d\n", filename, errno);
289 return (-1);
290 }
291 infd = fileno(in);
292 currfilename = filename;
293 byte_offset=1;
294
295 file_count++;
296
297 initialize_buffer();
298
299 while (1)
300 {
301 memset(&ccsrch_buf, '\0', BSIZE);
302 cnt = read(infd, &ccsrch_buf, BSIZE - 1);
303 if (cnt <= 0)
304 break;
305 ccsrch_index = 0;
306
307
308 while (ccsrch_index < cnt)
309 {
310 if ((ccsrch_buf[ccsrch_index] > 47) && (ccsrch_buf[ccsrch_index] < 58))
311 {
312 check = 1;
313 cardbuf[counter] = ((int)ccsrch_buf[ccsrch_index])-48;
314 counter++;
315 } else if ((ccsrch_buf[ccsrch_index] != 0) && (ccsrch_buf[ccsrch_index] != 10) && (ccsrch_buf[ccsrch_index] != 13))
316 {
317 /*
318 * we consider nulls, new lines, and carriage
319 * returns to be noise, so ingore those,
320 * otherwise, restart the count
321 */
322 check = 1;
323 counter = 0;
324 } else
325 {
326 check = 0;
327 initialize_buffer();
328 counter=0;
329 }
330
331 if (((counter > 12) && (counter < CARDSIZE)) && (check))
332 {
333 switch (counter)
334 {
335 case 16:
336 luhn_check(16,byte_offset-16);
337 break;
338 case 15:
339 luhn_check(15,byte_offset-15);
340 break;
341 case 14:
342 luhn_check(14,byte_offset-14);
343 break;
344 case 13:
345 luhn_check(13,byte_offset-13);
346 break;
347 }
348 } else if ((counter == CARDSIZE) && (check))
349 {
350 for (k = 0; k < counter - 1; k++)
351 {
352 cardbuf[k] = cardbuf[k + 1];
353 }
354 cardbuf[k] = (-1);
355 luhn_check(13,byte_offset-13);
356 luhn_check(14,byte_offset-14);
357 luhn_check(15,byte_offset-15);
358 luhn_check(16,byte_offset-16);
359 counter--;
360 }
361 byte_offset++;
362 ccsrch_index++;
363 }
364 }
365
366 fclose(in);
367
368 return (total);
369 }
370
371
372 int
escape_space(char * infile,char * outfile)373 escape_space(char *infile, char *outfile)
374 {
375 int i = 0;
376 int spc = 0;
377 char *tmpbuf = NULL;
378 int filelen = 0;
379 int newlen = 0;
380 int newpos = 0;
381
382 filelen = strlen(infile);
383 for (i = 0; i < filelen; i++)
384 if (infile[i] == ' ')
385 spc++;
386
387 newlen = filelen + spc + 1;
388 errno = 0;
389 tmpbuf = (char *)malloc(newlen);
390 if (tmpbuf == NULL)
391 {
392 fprintf(stderr, "escape_space: can't allocate memory; errno=%d\n", errno);
393 return (1);
394 }
395 memset(tmpbuf, '\0', newlen);
396
397 i = 0;
398 while (i < filelen)
399 {
400 if (infile[i] == ' ')
401 {
402 tmpbuf[newpos++] = '\\';
403 tmpbuf[newpos] = infile[i];
404 } else
405 tmpbuf[newpos] = infile[i];
406 newpos++;
407 i++;
408 }
409 strncpy(outfile, tmpbuf, newlen);
410 free(tmpbuf);
411 return (0);
412 }
413
414 int
get_file_stat(char * inputfile,struct stat * fileattr)415 get_file_stat(char *inputfile, struct stat * fileattr)
416 {
417 struct stat ffattr;
418 int err = 0;
419 char *tmp2buf = NULL;
420 int filelen = 0;
421
422
423 filelen = strlen(inputfile);
424 errno = 0;
425 tmp2buf = (char *) malloc(filelen+1);
426 if (tmp2buf == NULL)
427 {
428 fprintf(stderr, "get_file_stat: can't allocate memory; errno=%d\n", errno);
429 return (1);
430 }
431 memset(tmp2buf, '\0', filelen+1);
432 strncpy(tmp2buf, inputfile, filelen);
433
434 errno=0;
435 err = stat(tmp2buf, &ffattr);
436 if (err != 0)
437 {
438 if (errno == ENOENT)
439 fprintf(stderr, "get_file_stat: File %s not found, can't get stat info\n", inputfile);
440 else
441 fprintf(stderr, "get_file_stat: Cannot stat file %s; errno=%d\n", inputfile, errno);
442 free(tmp2buf);
443 return (-1);
444 }
445 memcpy(fileattr, &ffattr, sizeof(ffattr));
446 currfile_atime=ffattr.st_atime;
447 currfile_mtime=ffattr.st_mtime;
448 currfile_ctime=ffattr.st_ctime;
449 free(tmp2buf);
450 return (0);
451 }
452
453 int
proc_dir_list(char * instr)454 proc_dir_list(char *instr)
455 {
456 DIR *dirptr;
457 struct dirent *direntptr;
458 int dir_name_len = 0;
459 char *curr_path = NULL;
460 struct stat fstat;
461 int err = 0;
462 char tmpbuf[4096];
463
464 errno = 0;
465 dir_name_len = strlen(instr);
466 dirptr = opendir(instr);
467
468 #ifdef DEBUG
469 printf("Checking directory <%s>\n",instr);
470 #endif
471
472 if (dirptr == NULL)
473 {
474 fprintf(stderr, "proc_dir_list: Can't open dir %s; errno=%d\n", instr, errno);
475 return (1);
476 }
477 errno = 0;
478 curr_path = (char *)malloc(MAXPATH + 1);
479 if (curr_path == NULL)
480 {
481 fprintf(stderr, "proc_dir_list: Can't allocate enough space; errno=%d\n", errno);
482 closedir(dirptr);
483 return (1);
484 }
485 memset(curr_path, '\0', MAXPATH+1);
486 strncpy(curr_path, instr, MAXPATH);
487 errno = 0;
488 while ((direntptr = readdir(dirptr)) != NULL)
489 {
490 /* readdir give us everything and not necessarily in order. This
491 logic is just silly, but it works */
492 if (((direntptr->d_name[0] == '.') &&
493 (direntptr->d_name[1] == '\0')) ||
494 ((direntptr->d_name[0] == '.') &&
495 (direntptr->d_name[1] == '.') &&
496 (direntptr->d_name[2] == '\0')))
497 continue;
498
499 errno = 0;
500 strncat(curr_path, direntptr->d_name, MAXPATH);
501 err = get_file_stat(curr_path, &fstat);
502
503 if (err == -1)
504 {
505 if (errno == ENOENT)
506 fprintf(stderr, "proc_dir_list: file %s not found, can't stat\n", curr_path);
507 else
508 fprintf(stderr, "proc_dir_list: Cannot stat file %s; errno=%d\n", curr_path, errno);
509 closedir(dirptr);
510 return (1);
511 }
512 if ((fstat.st_mode & S_IFMT) == S_IFDIR)
513 {
514 strncat(curr_path, "/", MAXPATH);
515 proc_dir_list(curr_path);
516 } else if ((fstat.st_size > 0) && ((fstat.st_mode & S_IFMT) == S_IFREG))
517 {
518
519 memset(&tmpbuf, '\0', 4096);
520 if (escape_space(curr_path, tmpbuf) == 0)
521 {
522 /*
523 * kludge, need to clean this up
524 * later else any string matching in the path returns non NULL
525 */
526 if (logfilename != NULL)
527 if (strstr(curr_path, logfilename) != NULL)
528 fprintf(stderr, "We seem to be hitting our log file, so we'll leave this out of the search -> %s\n", curr_path);
529 else
530 {
531 #ifdef DEBUG
532 printf("Processing file %s\n",curr_path);
533 #endif
534 ccsrch(curr_path);
535 }
536 else
537 {
538 #ifdef DEBUG
539 printf("Processing file %s\n",curr_path);
540 #endif
541 ccsrch(curr_path);
542 }
543 }
544 }
545 #ifdef DEBUG
546 else
547 {
548 if (fstat.st_size > 0)
549 fprintf(stderr, "proc_dir_list: Unknown mode returned-> %x for file %s\n", fstat.st_mode,curr_path);
550 else
551 fprintf(stderr, "Not processing file of size 0 bytes: %s\n", curr_path);
552 }
553 #endif
554 curr_path[dir_name_len] = '\0';
555 }
556
557 free(curr_path);
558
559 closedir(dirptr);
560 return (0);
561 }
562
563 void
check_mastercard_16(long offset)564 check_mastercard_16(long offset)
565 {
566 char num2buf[3];
567 int vnum = 0;
568
569 memset(&num2buf, '\0', 3);
570 snprintf(num2buf, 3, "%d%d", cardbuf[0], cardbuf[1]);
571 vnum = atoi(num2buf);
572 if ((vnum > 50) && (vnum < 56))
573 print_result("MASTERCARD", 16, offset);
574 }
575
576 void
check_visa_16(long offset)577 check_visa_16(long offset)
578 {
579 char num2buf[2];
580 int vnum = 0;
581
582 memset(&num2buf, '\0', 2);
583 snprintf(num2buf, 2, "%d", cardbuf[0]);
584 vnum = atoi(num2buf);
585 if (vnum == 4)
586 print_result("VISA", 16, offset);
587 }
588
589 void
check_discover_16(long offset)590 check_discover_16(long offset)
591 {
592 char num2buf[5];
593 int vnum = 0;
594
595 memset(&num2buf, '\0', 5);
596 snprintf(num2buf, 5, "%d%d%d%d", cardbuf[0], cardbuf[1], cardbuf[2], cardbuf[3]);
597 vnum = atoi(num2buf);
598 if (vnum == 6011)
599 print_result("DISCOVER", 16, offset);
600 }
601
602 void
check_jcb_16(long offset)603 check_jcb_16(long offset)
604 {
605 char num2buf[5];
606 int vnum = 0;
607
608 memset(&num2buf, '\0', 5);
609 snprintf(num2buf, 5, "%d%d%d%d", cardbuf[0], cardbuf[1], cardbuf[2], cardbuf[3]);
610 vnum = atoi(num2buf);
611 if ((vnum == 3088) || (vnum == 3096) || (vnum == 3112) || (vnum == 3158) || (vnum == 3337) || (vnum == 3528) || (vnum == 3529))
612 print_result("JCB", 16, offset);
613 }
614
615 void
check_amex_15(long offset)616 check_amex_15(long offset)
617 {
618 char num2buf[3];
619 int vnum = 0;
620
621 memset(&num2buf, '\0', 3);
622 snprintf(num2buf, 3, "%d%d", cardbuf[0], cardbuf[1]);
623 vnum = atoi(num2buf);
624 if ((vnum == 34) || (vnum == 37))
625 print_result("AMEX", 15, offset);
626 }
627
628 void
check_enroute_15(long offset)629 check_enroute_15(long offset)
630 {
631 char num2buf[5];
632 int vnum = 0;
633
634 memset(&num2buf, '\0', 5);
635 snprintf(num2buf, 5, "%d%d%d%d", cardbuf[0], cardbuf[1], cardbuf[2], cardbuf[3]);
636 vnum = atoi(num2buf);
637 if ((vnum == 2014) || (vnum == 2149))
638 print_result("ENROUTE", 15, offset);
639 }
640
641 void
check_jcb_15(long offset)642 check_jcb_15(long offset)
643 {
644 char num2buf[5];
645 int vnum = 0;
646
647 memset(&num2buf, '\0', 5);
648 snprintf(num2buf, 5, "%d%d%d%d", cardbuf[0], cardbuf[1], cardbuf[2], cardbuf[3]);
649 vnum = atoi(num2buf);
650 if ((vnum == 2131) || (vnum == 1800) || (vnum == 3528) || (vnum == 3529))
651 print_result("JCB", 15, offset);
652 }
653
654 void
check_diners_club_cb_14(long offset)655 check_diners_club_cb_14(long offset)
656 {
657 char num2buf[4];
658 char num2buf2[3];
659 int vnum = 0;
660 int vnum2 = 0;
661
662 memset(&num2buf, '\0', 4);
663 memset(&num2buf2, '\0', 3);
664 snprintf(num2buf, 4, "%d%d%d", cardbuf[0], cardbuf[1], cardbuf[2]);
665 snprintf(num2buf2, 3, "%d%d", cardbuf[0], cardbuf[1]);
666 vnum = atoi(num2buf);
667 vnum2 = atoi(num2buf2);
668 if (((vnum > 299) && (vnum < 306)) || ((vnum > 379) && (vnum < 389)) || (vnum2 == 36))
669 print_result("DINERS_CLUB_CARTE_BLANCHE", 14, offset);
670 }
671
672 /** It has been purported that these cards are no longer in use
673 void
674 check_visa_13(long offset)
675 {
676 char num2buf[2];
677 int vnum = 0;
678
679 memset(&num2buf, '\0', 2);
680 snprintf(num2buf, 2, "%d\0", cardbuf[0]);
681 vnum = atoi(num2buf);
682 if (vnum == 4)
683 print_result("VISA", 13, offset);
684 }
685 **/
686
687 void
cleanup_shtuff()688 cleanup_shtuff()
689 {
690 int end_time=0;
691
692 end_time=time(NULL);
693 fprintf(stdout, "\n\nFiles searched ->\t\t%ld\n", file_count);
694 fprintf(stdout, "Search time (seconds) ->\t%d\n", ((int)time(NULL) - init_time));
695 fprintf(stdout, "Credit card matches->\t\t%ld\n", total_count);
696 if (tracksrch)
697 fprintf(stdout, "Track data pattern matches->\t%d\n\n", trackdatacount);
698 fprintf(stdout, "\nLocal end time: %s\n\n", ctime((time_t *)&end_time));
699 }
700
701 void
process_cleanup()702 process_cleanup()
703 {
704 if (logfilefd>0)
705 fclose(logfilefd);
706 cleanup_shtuff();
707 exit(0);
708 }
709
710 void
signal_proc()711 signal_proc()
712 {
713 signal(SIGHUP, process_cleanup);
714 signal(SIGTERM, process_cleanup);
715 signal(SIGINT, process_cleanup);
716 signal(SIGQUIT, process_cleanup);
717 }
718
719 void
usage(char * progname)720 usage(char *progname)
721 {
722 printf("%s\n", PROG_VER);
723 printf("Usage: %s <options> <start path>\n", progname);
724 printf(" where <options> are:\n");
725 printf(" -b\t\t Add the byte offset into the file of the number\n");
726 printf(" -e\t\t Include the Modify Access and Create times in terms \n\t\t of seconds since the epoch\n");
727 printf(" -f\t\t Only print the filename w/ potential PAN data\n");
728 printf(" -j\t\t Include the Modify Access and Create times in terms \n\t\t of normal date/time\n");
729 printf(" -o <filename> Output the data to the file <filename> vs. standard out\n");
730
731 printf(" -t <1 or 2>\t Check if the pattern follows either a Track 1 \n\t\t or 2 format\n");
732 printf(" -T\t\t Check for both Track 1 and Track 2 patterns\n");
733
734 printf(" -h\t\t This stuff\n\n");
735 printf("Please see the README file for a description of how this program works.\n\n");
736 exit(0);
737 }
738
739 int
open_logfile()740 open_logfile()
741 {
742 errno = 0;
743 if (logfilename!=NULL)
744 {
745 logfilefd = fopen(logfilename, "a+");
746 if (logfilefd == NULL)
747 {
748 fprintf(stderr, "Unable to open logfile %s for writing; errno=%d\n", logfilename, errno);
749 return (-1);
750 }
751 }
752 return (0);
753 }
754
check_dir(char * name)755 int check_dir (char *name)
756 {
757 DIR *dirptr;
758
759 dirptr = opendir(name);
760 if (dirptr!=NULL)
761 {
762 closedir(dirptr);
763 return(1);
764 }
765 else
766 return (0);
767 }
768
769 int
main(int argc,char * argv[])770 main(int argc, char *argv[])
771 {
772 struct stat ffstat;
773 char *inputstr = NULL;
774 char *inbuf = NULL;
775 char *tracktype_str=NULL;
776 char tmpbuf[4096];
777 int inlen = 0, err=0, c=0;
778
779 if (argc < 2)
780 usage(argv[0]);
781
782 while ((c = getopt(argc, argv,"befjt:To:")) != -1)
783 {
784 switch (c)
785 {
786 case 'b':
787 print_byte_offset=1;
788 break;
789 case 'e':
790 print_epoch_time=1;
791 break;
792 case 'f':
793 print_filename_only=1;
794 break;
795 case 'j':
796 print_julian_time=1;
797 break;
798 case 'o':
799 logfilename=optarg;
800 break;
801 case 't':
802 tracksrch=1;
803 tracktype_str=optarg;
804 if (atoi(tracktype_str)==1)
805 tracktype1=1;
806 else if (atoi(tracktype_str)==2)
807 tracktype2=1;
808 else
809 usage(argv[0]);
810 break;
811 case 'T':
812 tracksrch=1;
813 tracktype1=1;
814 tracktype2=1;
815 break;
816 case 'h':
817 default:
818 usage(argv[0]);
819 break;
820 }
821 }
822
823 inputstr = argv[optind];
824 if (inputstr == NULL)
825 usage(argv[0]);
826
827 inlen = strlen(inputstr) + 1;
828
829 if (open_logfile() < 0)
830 exit(-1);
831
832 inbuf = (char *)malloc(inlen + 1);
833
834 memset(inbuf, '\0', inlen+1);
835 strncpy(inbuf, inputstr, inlen);
836
837 signal_proc();
838
839 init_time = time(NULL);
840 printf("\n\t\t%s\n", PROG_VER);
841 printf("\nLocal start time: %s\n",ctime((time_t *)&init_time));
842 if (check_dir(inbuf))
843 {
844 #ifdef WINDOWS
845 if ((inbuf[strlen(inbuf) - 1]) != '\\')
846 inbuf[strlen(inbuf)] = '\\';
847 #else
848 if ((inbuf[strlen(inbuf) - 1]) != '/')
849 inbuf[strlen(inbuf)] = '/';
850 #endif
851 proc_dir_list(inbuf);
852 }
853 else
854 {
855 err = get_file_stat(inbuf, &ffstat);
856 if (err == -1)
857 {
858 if (errno == ENOENT)
859 fprintf(stderr, "File %s not found, can't stat\n", inbuf);
860 else
861 fprintf(stderr, "Cannot stat file %s; errno=%d\n", inbuf, errno);
862 exit (-1);
863 }
864
865 if ((ffstat.st_size > 0) && ((ffstat.st_mode & S_IFMT) == S_IFREG))
866 {
867
868 memset(&tmpbuf, '\0', 4096);
869 if (escape_space(inbuf, tmpbuf) == 0)
870 {
871 if (logfilename != NULL)
872 if (strstr(inbuf, logfilename) != NULL)
873 fprintf(stderr, "main: We seem to be hitting our log file, so we'll leave this out of the search -> %s\n", inbuf);
874 else
875 {
876 #ifdef DEBUG
877 printf("Processing file %s\n",inbuf);
878 #endif
879 ccsrch(inbuf);
880 }
881 else
882 {
883 #ifdef DEBUG
884 printf("Processing file %s\n",inbuf);
885 #endif
886 ccsrch(inbuf);
887 }
888 }
889 }
890 else if ((ffstat.st_mode & S_IFMT) == S_IFDIR)
891 {
892 #ifdef WINDOWS
893 if ((inbuf[strlen(inbuf) - 1]) != '\\')
894 inbuf[strlen(inbuf)] = '\\';
895 #else
896 if ((inbuf[strlen(inbuf) - 1]) != '/')
897 inbuf[strlen(inbuf)] = '/';
898 #endif
899 proc_dir_list(inbuf);
900
901 }
902 else
903 fprintf(stderr, "main: Unknown mode returned-> %x\n", ffstat.st_mode);
904 }
905
906 cleanup_shtuff();
907 free(inbuf);
908
909 return (0);
910 }
911