]> git.ipfire.org Git - thirdparty/sarg.git/blob - util.c
998e8dfa21764287f0b06ec3ce69d347808b4547
[thirdparty/sarg.git] / util.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27 // #define LEGACY_MY_ATOLL
28 // #define LEGACY_TESTVALIDUSERCHAR
29
30 #include "include/conf.h"
31 #include "include/defs.h"
32
33 #if defined(HAVE_BACKTRACE)
34 #define USE_GETWORD_BACKTRACE 1
35 #else
36 #define USE_GETWORD_BACKTRACE 0
37 #endif
38
39 static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
40
41 //! The list of the HTTP codes to exclude from the report.
42 static char *excludecode=NULL;
43
44 /*void fgetword(char *word, char *line, int stop)
45 {
46 //VARIANT N1
47 int x;
48
49 for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x];
50 word[x] = '\0';
51
52 //VARIANT N2
53 char *tchar;
54 int difflen;
55
56 tchar = strchr(line, stop);
57 if (tchar == NULL) strcpy(word, line);
58 else
59 {
60 difflen = tchar - line;
61 strncpy(word, line, difflen);
62 word[difflen] = '\0';
63 }
64 }*/
65
66 #if USE_GETWORD_BACKTRACE
67 static void getword_backtrace(void)
68 {
69 void *buffer[5];
70 int i, n;
71 char **calls;
72
73 n=backtrace(buffer,sizeof(buffer)/sizeof(buffer[0]));
74 if (n<=0) return;
75 calls=backtrace_symbols(buffer,n);
76 if (calls) {
77 fprintf(stderr,"SARG: getword backtrace:\n");
78 for (i=0 ; i<n ; i++) {
79 fprintf(stderr,"SARG: %d:%s\n",i+1,calls[i]);
80 }
81 free(calls);
82 }
83 }
84 #endif //USE_GETWORD_BACKTRACE
85
86 void getword_start(struct getwordstruct *gwarea, const char *line)
87 {
88 gwarea->beginning=line;
89 gwarea->current=line;
90 }
91
92 void getword_restart(struct getwordstruct *gwarea)
93 {
94 gwarea->current=gwarea->beginning;
95 }
96
97 int getword(char *word, int limit, struct getwordstruct *gwarea, int stop)
98 {
99 int x;
100
101 for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) {
102 if(x>=limit) {
103 printf("SARG: getword loop detected after %d bytes.\n",x);
104 printf("SARG: Line=\"%s\"\n",gwarea->beginning);
105 printf("SARG: Record=\"%s\"\n",gwarea->current);
106 printf("SARG: searching for \'x%x\'\n",stop);
107 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
108 word[(limit>0) ? limit-1 : 0]='\0';
109 #if USE_GETWORD_BACKTRACE
110 getword_backtrace();
111 #endif
112 return(-1);
113 }
114 word[x] = gwarea->current[x];
115 }
116
117 word[x] = '\0';
118 if (gwarea->current[x]) ++x;
119 gwarea->current+=x;
120 return(0);
121 }
122
123 int getword_multisep(char *word, int limit, struct getwordstruct *gwarea, int stop)
124 {
125 int x;
126
127 for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) {
128 if(x>=limit) {
129 printf("SARG: getword_multisep loop detected.\n");
130 printf("SARG: Line=\"%s\"\n",gwarea->beginning);
131 printf("SARG: Record=\"%s\"\n",gwarea->current);
132 printf("SARG: searching for \'x%x\'\n",stop);
133 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
134 if (limit>0) word[limit-1]='\0';
135 #if USE_GETWORD_BACKTRACE
136 getword_backtrace();
137 #endif
138 //exit(1);
139 return(-1);
140 }
141 word[x] = gwarea->current[x];
142 }
143
144 word[x] = '\0';
145 while (gwarea->current[x] && gwarea->current[x]==stop) ++x;
146 gwarea->current+=x;
147 return(0);
148 }
149
150 int getword_skip(int limit, struct getwordstruct *gwarea, int stop)
151 {
152 int x;
153
154 for(x=0;(gwarea->current[x] && (gwarea->current[x] != stop ));x++) {
155 if(x>=limit) {
156 printf("SARG: getword_skip loop detected after %d bytes.\n",x);
157 printf("SARG: Line=\"%s\"\n",gwarea->beginning);
158 printf("SARG: Record=\"%s\"\n",gwarea->current);
159 printf("SARG: searching for \'x%x\'\n",stop);
160 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
161 #if USE_GETWORD_BACKTRACE
162 getword_backtrace();
163 #endif
164 return(-1);
165 }
166 }
167
168 if (gwarea->current[x]) ++x;
169 gwarea->current+=x;
170 return(0);
171 }
172
173 int getword_atoll(long long int *number, struct getwordstruct *gwarea, int stop)
174 {
175 int x;
176 int sign=+1;
177
178 if (gwarea->current[0] == '-') {
179 gwarea->current++;
180 sign=-1;
181 } else if (gwarea->current[0] == '+') {
182 gwarea->current++;
183 }
184 *number=0LL;
185 for(x=0;isdigit(gwarea->current[x]);x++) {
186 *number=(*number * 10) + gwarea->current[x]-'0';
187 }
188 if(gwarea->current[x] && gwarea->current[x]!=stop) {
189 printf("SARG: getword_atoll loop detected after %d bytes.\n",x);
190 printf("SARG: Line=\"%s\"\n",gwarea->beginning);
191 printf("SARG: Record=\"%s\"\n",gwarea->current);
192 printf("SARG: searching for \'x%x\'\n",stop);
193 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
194 #if USE_GETWORD_BACKTRACE
195 getword_backtrace();
196 #endif
197 return(-1);
198 }
199 *number*=sign;
200
201 if (gwarea->current[x]) ++x;
202 gwarea->current+=x;
203 return(0);
204 }
205
206
207 #define MAXLLL 30 //!< Maximum number of digits in long long (a guess).
208 long long int my_atoll (const char *nptr)
209 {
210 long long int returnval=0LL;
211 int max_digits = MAXLLL ;
212
213 // Soak up all the white space
214 while (isspace( *nptr )) {
215 nptr++;
216 }
217
218 //For each character left to right
219 //change the character to a single digit
220 //multiply what we had before by 10 and add the new digit
221
222 while (--max_digits && isdigit( *nptr ))
223 {
224 returnval = ( returnval * 10 ) + ( *nptr++ - '0' ) ;
225 }
226
227 return returnval;
228 }
229
230 int is_absolute(const char *path)
231 {
232 if (*path=='/') return(1);
233 #ifdef WINDOWS
234 if (isalpha(path[0]) && path[1]==':') return(1);
235 #endif
236 return(0);
237 }
238
239 void my_mkdir(const char *name)
240 {
241 char w0[MAXLEN];
242 int i;
243 int chars;
244
245 if(!is_absolute(name)) {
246 fprintf(stderr,"SARG: Invalid path (%s). Please, use absolute paths only.\n",name);
247 fprintf(stderr,"SARG: process aborted.\n");
248 exit(1);
249 }
250
251 chars=0;
252 for (i=0 ; name[i] ; i++) {
253 if (i>=sizeof(w0)) {
254 fprintf(stderr,"SARG: directory name too long %s\n",name);
255 exit(1);
256 }
257 if (chars>0 && name[i] == '/') {
258 w0[i] = '\0';
259 if(access(w0, R_OK) != 0) {
260 if(mkdir(w0,0755)) {
261 fprintf(stderr,"SARG: mkdir %s %s\n",w0,strerror(errno));
262 fprintf(stderr,"SARG: process aborted.\n");
263 exit(1);
264 }
265 }
266 }
267 if (name[i] != '/') chars++;
268 w0[i] = name[i];
269 }
270
271 if(access(name, R_OK) != 0) {
272 if(mkdir(name,0755)) {
273 fprintf(stderr,"SARG: mkdir %s %s\n",name,strerror(errno));
274 fprintf(stderr,"SARG: process aborted.\n");
275 exit(1);
276 }
277 }
278 }
279
280
281 void my_lltoa(unsigned long long int n, char s[], int len)
282 {
283 int i;
284 int slen = 0;
285 int j;
286 char c;
287
288 do {
289 s[slen++] = (n % 10) + '0';
290 } while ((n /= 10) > 0);
291 s[slen] = '\0';
292
293 for (i = 0, j = slen-1; i<j; i++, j--) {
294 c = s[i];
295 s[i] = s[j];
296 s[j] = c;
297 }
298
299 if(len>slen) {
300 i=len-slen;
301 for(j=slen; j>=0; j--)
302 s[j+i]=s[j];
303 for(j=0 ; j<i ; j++)
304 s[j]='0';
305 }
306 }
307
308
309 void builddia(char *dia, const char *mes, const char *ano, const char *df, char *wdata)
310 {
311 char ndia[11];
312 int nmes;
313
314 if(strlen(dia) < 1) return;
315
316 for(nmes=0; nmes<12; nmes++) {
317 if(strcmp(mtab1[nmes],mes) == 0) {
318 break;
319 }
320 }
321 nmes++;
322
323 snprintf(wdata,9,"%s%02d%s",ano,nmes,dia);
324
325 if(df[0]!='u')
326 snprintf(ndia,sizeof(ndia),"%s/%02d/%s",dia,nmes,ano);
327 else
328 snprintf(ndia,sizeof(ndia),"%02d/%s/%s",nmes,dia,ano);
329
330 strcpy(dia,ndia);
331
332 }
333
334
335 void buildymd(const char *dia, const char *mes, const char *ano, char *wdata)
336 {
337 int nmes;
338
339 for(nmes=0; nmes<12; nmes++) {
340 if(strcmp(mtab1[nmes],mes) == 0)
341 break;
342 }
343
344 sprintf(wdata,"%04d%02d%02d",atoi(ano),nmes+1,atoi(dia));
345
346 }
347
348
349 void conv_month(char *month)
350 {
351 int x;
352
353 for(x=0; x<12 && strcmp(mtab1[x],month)!=0; x++);
354 sprintf(month,"%02d",x+1);
355 }
356
357
358 void conv_month_name(char *month)
359 {
360 int x;
361
362 x=atoi(month);
363 if (x>=1 && x<=12)
364 strcpy(month,mtab1[x-1]);
365 }
366
367
368 void name_month(char *month,int month_len)
369 {
370 int x, z=atoi(month)-1;
371 char m[255];
372 char w[20];
373 struct getwordstruct gwarea;
374
375 strcpy(m,text[133]);
376 getword_start(&gwarea,m);
377
378 for(x=0; x<z; x++)
379 if (getword_multisep(w,sizeof(w),&gwarea,',')<0) {
380 printf("SARG: Maybe you have a broken record or garbage in the names of the months.\n");
381 exit(1);
382 }
383 if (getword_multisep(month,month_len,&gwarea,',')<0) {
384 printf("SARG: Maybe you have a broken record or garbage in the name of the months.\n");
385 exit(1);
386 }
387 }
388
389
390 void fixper(char *tbuf, char *period, const char *duntil)
391 {
392 char warea[50];
393 char dia[5], mes[5], ano[5];
394 int x;
395
396 strncpy(dia,duntil+6,2);
397 dia[2]='\0';
398 strncpy(mes,duntil+4,2);
399 mes[2]='\0';
400 strncpy(ano,duntil,4);
401 ano[4]='\0';
402
403 x=atoi(mes);
404 if (x>=1 && x<=12)
405 strcpy(mes,mtab1[x-1]);
406
407 if(strcmp(df,"e") == 0)
408 sprintf(warea,"%s%s%s",dia,mes,ano);
409 else if(strcmp(df,"u") == 0)
410 sprintf(warea,"%s%s%s",ano,mes,dia);
411 else
412 warea[0]='\0';
413
414 strcat(period,warea);
415 }
416
417
418 void debuga(const char *msg,...)
419 {
420 va_list ap;
421
422 fputs("SARG: ",stderr);
423 va_start(ap,msg);
424 vfprintf(stderr,msg,ap);
425 va_end(ap);
426 fputc('\n',stderr);
427 }
428
429
430 void debugaz(const char *head, const char *msg)
431 {
432 fprintf(stderr, "SARG: (util) %s=%s\n",head, msg);
433
434 }
435
436
437 void fixip(char *ip)
438 {
439 int i;
440 char sep='_';
441 char search=0;
442 int nrepl=0;
443
444 for (i=0; ip[i]; i++) {
445 if (ip[i]=='.') {
446 search='.';
447 sep='_';
448 break;
449 }
450 if (ip[i]=='_') {
451 search='_';
452 sep='.';
453 break;
454 }
455 }
456 for (; ip[i]; i++) {
457 if (ip[i]==search) {
458 ip[i]=sep;
459 nrepl++;
460 if (nrepl>=3) break;
461 }
462 }
463 }
464
465
466 char *fixnum(long long int value, int n)
467 {
468 #define MAXLEN_FIXNUM 1024
469 char num[MAXLEN_FIXNUM]="";
470 char buf[MAXLEN_FIXNUM * 2];
471 char *pbuf;
472 static char ret[MAXLEN_FIXNUM * 2];
473 char *pret;
474 register int i, j, k;
475 int numlen;
476 static char abbrev[30];
477
478 my_lltoa(value, num, 0);
479
480 if(strcmp(DisplayedValues,"abbreviation") == 0) {
481 numlen = strlen(num);
482 if(numlen <= 3)
483 sprintf(abbrev,"%s",num);
484 if(numlen == 4 || numlen == 7 || numlen == 10 || numlen == 13) {
485 snprintf(abbrev,2,"%s",num);
486 strncat(abbrev,".",1);
487 strncat(abbrev,num+1,2);
488 if(!n) return(abbrev);
489 if(numlen == 4)
490 strncat(abbrev,"K",1);
491 else if(numlen == 7)
492 strncat(abbrev,"M",1);
493 else if(numlen == 10)
494 strncat(abbrev,"G",1);
495 else if(numlen == 13)
496 strncat(abbrev,"T",1);
497 }
498 if(numlen == 5 || numlen == 8 || numlen == 11 || numlen == 14) {
499 snprintf(abbrev,3,"%s",num);
500 strncat(abbrev,".",1);
501 strncat(abbrev,num+2,2);
502 if(!n) return(abbrev);
503 if(numlen == 5)
504 strncat(abbrev,"K",1);
505 else if(numlen == 8)
506 strncat(abbrev,"M",1);
507 else if(numlen == 11)
508 strncat(abbrev,"G",1);
509 else if(numlen == 14)
510 strncat(abbrev,"T",1);
511 }
512 if(numlen == 6 || numlen == 9 || numlen == 12 || numlen == 15) {
513 snprintf(abbrev,4,"%s",num);
514 strncat(abbrev,".",1);
515 strncat(abbrev,num+3,2);
516 if(!n) return(abbrev);
517 if(numlen == 6)
518 strncat(abbrev,"K",1);
519 else if(numlen == 9)
520 strncat(abbrev,"M",1);
521 else if(numlen == 12)
522 strncat(abbrev,"G",1);
523 else if(numlen == 15)
524 strncat(abbrev,"T",1);
525 }
526
527 return(abbrev);
528 }
529
530 bzero(buf, MAXLEN_FIXNUM*2);
531
532 pbuf = buf;
533 pret = ret;
534 k = 0;
535
536 for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) {
537 if ( k == 2 && i != 0 ) {
538 k = 0;
539 pbuf[j++] = num[i];
540 pbuf[j++] = (UseComma) ? ',' : '.';
541 continue;
542 }
543 pbuf[j] = num[i];
544 j++;
545 k++;
546 }
547
548 pret[0]='\0';
549
550 for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++)
551 pret[j] = pbuf[i];
552
553 pret[j] = '\0';
554
555 return pret;
556 }
557
558
559 char *fixnum2(long long int value, int n)
560 {
561 #define MAXLEN_FIXNUM2 1024
562 char num[MAXLEN_FIXNUM2];
563 char buf[MAXLEN_FIXNUM2 * 2];
564 char *pbuf;
565 static char ret[MAXLEN_FIXNUM2 * 2];
566 char *pret;
567 register int i, j, k;
568
569 my_lltoa(value, num, 0);
570 bzero(buf, MAXLEN_FIXNUM2*2);
571
572 pbuf = buf;
573 pret = ret;
574 k = 0;
575
576 for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) {
577 if ( k == 2 && i != 0 ) {
578 k = 0;
579 pbuf[j++] = num[i];
580 pbuf[j++] = (UseComma) ? ',' : '.';
581 continue;
582 }
583 pbuf[j] = num[i];
584 j++;
585 k++;
586 }
587
588 pret[0]='\0';
589
590 for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++)
591 pret[j] = pbuf[i];
592
593 pret[j] = '\0';
594
595 return pret;
596 }
597
598
599
600 void buildhref(char * href)
601 {
602 char whref[MAXLEN];
603
604 if(strcmp(href,"./") == 0){
605 href[0]='\0';
606 strcat(href,"<a href='");
607 return;
608 }
609
610 href[strlen(href)-1]='\0';
611 sprintf(whref,"%s",strrchr(href,'/'));
612
613 strcpy(href,"<a href='");
614 strcat(href,whref);
615 strcat(href,"/");
616
617 return;
618
619 }
620
621
622 char *buildtime(long long int elap)
623 {
624
625 int num = elap / 1000;
626 int hor = 0;
627 int min = 0;
628 int sec = 0;
629 static char buf[12];
630
631 buf[0]='\0';
632
633 hor=num / 3600;
634 min=(num % 3600) / 60;
635 sec=num % 60;
636 sprintf(buf,"%02d:%02d:%02d",hor,min,sec);
637
638 return(buf);
639
640 }
641
642
643 void obtdate(const char *dirname, const char *name, char *data)
644 {
645
646 FILE *fp_in;
647 char wdir[MAXLEN];
648
649 sprintf(wdir,"%s%s/sarg-date",dirname,name);
650 if ((fp_in = fopen(wdir, "rt")) == 0) {
651 sprintf(wdir,"%s%s/date",dirname,name);
652 if ((fp_in = fopen(wdir, "rt")) == 0) {
653 data[0]='\0';
654 return;
655 }
656 }
657
658 if (!fgets(data,80,fp_in)) {
659 fprintf(stderr,"Failed to read the date in %s\n",wdir);
660 exit(1);
661 }
662 fclose(fp_in);
663 fixendofline(data);
664
665 return;
666
667 }
668
669
670 void formatdate(char *date,int date_size,int year,int month,int day,int hour,int minute,int second,int dst)
671 {
672 struct tm ltm;
673 time_t unixtime;
674 struct tm *fulltm;
675
676 memset(&ltm,0,sizeof(ltm));
677 if (year>=1900) ltm.tm_year=year-1900;
678 if (month>=1 && month<=12) ltm.tm_mon=month-1;
679 if (day>=1 && day<=31) ltm.tm_mday=day;
680 if (hour>=0 && hour<24) ltm.tm_hour=hour;
681 if (minute>=0 && minute<60) ltm.tm_min=minute;
682 if (second>=0 && second<60) ltm.tm_sec=second;
683 ltm.tm_isdst=dst;
684 unixtime=mktime(&ltm); //fill the missing entries
685 fulltm=localtime(&unixtime);
686 //strftime(date,date_size,"%a %b %d %H:%M:%S %Z %Y",fulltm);
687 strftime(date,date_size,"%c",fulltm);
688 }
689
690
691 void obtuser(const char *dirname, const char *name, char *tuser)
692 {
693
694 FILE *fp_in;
695 char wdir[MAXLEN];
696
697 sprintf(wdir,"%s%s/sarg-users",dirname,name);
698 if((fp_in=fopen(wdir,"r"))==NULL) {
699 sprintf(wdir,"%s%s/users",dirname,name);
700 if((fp_in=fopen(wdir,"r"))==NULL) {
701 tuser[0]='\0';
702 return;
703 }
704 }
705
706 if (!fgets(tuser,20,fp_in)) {
707 fprintf(stderr,"Failed to read the user in %s\n",wdir);
708 exit(1);
709 }
710 fclose(fp_in);
711 fixendofline(tuser);
712
713 return;
714
715 }
716
717
718 void obttotal(const char *dirname, const char *name, char *tbytes, const char *tuser, char *media)
719 {
720 FILE *fp_in;
721 char buf[MAXLEN];
722 char wdir[MAXLEN];
723 char warea[MAXLEN];
724 char sep;
725 long long int med=0;
726 long long int wtuser=0;
727 long long int twork=0;
728 struct getwordstruct gwarea;
729
730 twork=0;
731 tbytes[0]='\0';
732 media[0]='\0';
733
734 sprintf(wdir,"%s%s/sarg-general",dirname,name);
735 if ((fp_in = fopen(wdir, "r")) == 0) {
736 sprintf(wdir,"%s%s/general",dirname,name);
737 if ((fp_in = fopen(wdir, "r")) == 0) {
738 return;
739 }
740 }
741
742 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
743 if (strncmp(buf,"TOTAL\t",6) == 0)
744 sep='\t'; //new file
745 else if (strncmp(buf,"TOTAL ",6) == 0)
746 sep=' '; //old file
747 else
748 continue;
749 getword_start(&gwarea,buf);
750 if (getword(warea,sizeof(warea),&gwarea,sep)<0) {
751 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir);
752 exit(1);
753 }
754 if(strcmp(warea,"TOTAL") != 0)
755 continue;
756 if (getword_skip(MAXLEN,&gwarea,sep)<0) {
757 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir);
758 exit(1);
759 }
760 if (getword_atoll(&twork,&gwarea,sep)<0) {
761 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir);
762 exit(1);
763 }
764 strcpy(tbytes,fixnum(twork,1));
765 break;
766 }
767 fclose(fp_in);
768
769 wtuser=my_atoll(tuser);
770 if(wtuser <= 0) {
771 strcpy(media,"0");
772 return;
773 }
774
775 med=twork / wtuser;
776 strcpy(media,fixnum(med,1));
777
778 return;
779 }
780
781
782 void gperiod(const char *dirname, const char *period)
783 {
784
785 FILE *fp_ou;
786 char wdirname[MAXLEN];
787
788 strcpy(wdirname,dirname);
789 strcat(wdirname,"/sarg-period");
790
791 if((fp_ou=fopen(wdirname,"w"))==NULL){
792 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
793 exit(1);
794 }
795
796 fputs(period,fp_ou);
797 fclose(fp_ou);
798
799 if(debug)
800 debuga("%s",(char *)text[50]);
801
802 return;
803
804 }
805
806 static void copy_images(void)
807 {
808 FILE *img_in, *img_ou;
809 char images[512];
810 char imgdir[MAXLEN];
811 char srcfile[MAXLEN];
812 char dstfile[MAXLEN];
813 DIR *dirp;
814 struct dirent *direntp;
815 char buffer[MAXLEN];
816 size_t nread;
817 struct stat info;
818
819 if (snprintf(images,sizeof(images),"%simages",outdir)>=sizeof(images)) {
820 fprintf(stderr,"SARG: Cannot copy images to target directory %simages\n",outdir);
821 exit(1);
822 }
823 if (access(images,R_OK)!=0) {
824 mkdir(images,0755);
825 }
826
827 strcpy(imgdir,IMAGEDIR);
828 dirp = opendir(imgdir);
829 if(dirp==NULL) {
830 fprintf(stderr, "SARG: (util) %s %s: %s\n","Can't open directory", imgdir,strerror(errno));
831 return;
832 }
833 while ((direntp = readdir( dirp )) != NULL ){
834 if(direntp->d_name[0]=='.')
835 continue;
836 sprintf(srcfile,"%s/%s",imgdir,direntp->d_name);
837 if (stat(srcfile,&info)) {
838 fprintf(stderr,"SARG: Cannot stat \"%s\" - %s\n",srcfile,strerror(errno));
839 continue;
840 }
841 if (S_ISREG(info.st_mode)) {
842 sprintf(dstfile,"%s/%s",images,direntp->d_name);
843 img_in = fopen(srcfile, "rb");
844 if(img_in!=NULL) {
845 img_ou = fopen(dstfile, "wb");
846 if(img_ou!=NULL) {
847 while ((nread = fread(buffer,1,sizeof(buffer),img_in))>0) {
848 if (fwrite(buffer,1,nread,img_ou)!=nread) {
849 fprintf(stderr,"SARG: Failed to copy image %s to %s\n",srcfile,dstfile);
850 break;
851 }
852 }
853 fclose(img_ou);
854 } else
855 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open/create file", dstfile, strerror(errno));
856 fclose(img_in);
857 } else
858 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open file", srcfile, strerror(errno));
859 }
860 }
861 (void) closedir(dirp);
862
863 return;
864 }
865
866 void vrfydir(const char *per1, const char *addr, const char *site, const char *us, const char *form)
867 {
868 FILE *fp_ou;
869 int num=1, count=0;
870 char wdir[MAXLEN];
871 char per2[MAXLEN];
872 char dirname2[MAXLEN];
873 char y1[5], y2[5];
874 char d1[3], d2[3];
875 char m1[8], m2[8];
876 time_t curtime;
877 struct tm *loctm;
878
879 if(IndexTree == INDEX_TREE_DATE) {
880 bzero(y1,sizeof(y1));
881 bzero(y2,sizeof(y2));
882 bzero(d1,sizeof(d1));
883 bzero(d2,sizeof(d2));
884 bzero(m1,sizeof(m1));
885 bzero(m2,sizeof(m2));
886 if(strncmp(df,"u",1) == 0) {
887 strncpy(y1,period,4);
888 strncpy(y2,period+10,4);
889 strncpy(m1,period+4,3);
890 strncpy(m2,period+14,3);
891 strncpy(d1,period+7,2);
892 strncpy(d2,period+17,2);
893 } else if(strncmp(df,"e",1) == 0) {
894 strncpy(d1,period+0,2);
895 strncpy(d2,period+10,2);
896 strncpy(m1,period+2,3);
897 strncpy(m2,period+12,3);
898 strncpy(y1,period+5,4);
899 strncpy(y2,period+15,4);
900 }
901 conv_month(m1);
902 conv_month(m2);
903
904 sprintf(wdir,"%s%s",outdir,y1);
905 if(strcmp(y1,y2) != 0) {
906 strcat(wdir,"-");
907 strcat(wdir,y2);
908 }
909 if(access(wdir, R_OK) != 0)
910 my_mkdir(wdir);
911
912 strcat(wdir,"/");
913 strcat(wdir,m1);
914 if(strcmp(m1,m2) != 0) {
915 strcat(wdir,"-");
916 strcat(wdir,m2);
917 }
918 if(access(wdir, R_OK) != 0)
919 my_mkdir(wdir);
920
921 strcat(wdir,"/");
922 strcat(wdir,d1);
923 if(strcmp(d1,d2) != 0) {
924 strcat(wdir,"-");
925 strcat(wdir,d2);
926 }
927 } else {
928 sprintf(wdir, "%s%s", outdir, per1);
929 }
930
931 if(us[0] != '\0') {
932 strcat(wdir,"-");
933 strcat(wdir,us);
934 }
935 if(addr[0] != '\0') {
936 strcat(wdir,"-");
937 strcat(wdir,addr);
938 }
939 if(site[0] != '\0') {
940 strcat(wdir,"-");
941 strcat(wdir,site);
942 }
943
944 strcpy(dirname,wdir);
945
946 if(IndexTree != INDEX_TREE_DATE) {
947 if(!OverwriteReport) {
948 while(num) {
949 if(access(wdir,R_OK) == 0) {
950 sprintf(wdir,"%s.%d",dirname,num);
951 sprintf(per2,"%s.%d",per1,num);
952 num++;
953 count++;
954 } else
955 break;
956 }
957
958 if(count > 0) {
959 if(debug)
960 fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname,text[52],wdir);
961 rename(dirname,wdir);
962 }
963 } else {
964 if(access(dirname,R_OK) == 0) {
965 unlinkdir(dirname,1);
966 }
967 }
968 my_mkdir(dirname);
969 } else {
970 strcpy(dirname2,wdir);
971 if(!OverwriteReport) {
972 while(num) {
973 if(access(wdir,R_OK) == 0) {
974 sprintf(wdir,"%s.%d",dirname2,num);
975 sprintf(per2,"%s.%d",per1,num);
976 num++;
977 count++;
978 } else
979 break;
980 }
981
982 if(count > 0) {
983 if(debug)
984 fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname2,text[52],wdir);
985 rename(dirname2,wdir);
986 strcpy(dirname2,wdir);
987 }
988 } else {
989 if(access(wdir,R_OK) == 0) {
990 unlinkdir(wdir,1);
991 }
992 }
993
994 if(access(wdir, R_OK) != 0)
995 my_mkdir(wdir);
996 }
997
998 strcpy(dirname2,wdir);
999
1000 sprintf(wdir,"%s/sarg-date",dirname);
1001 if ((fp_ou = fopen(wdir, "wt")) == 0) {
1002 fprintf(stderr, "SARG: cannot open %s for writing\n",wdir);
1003 perror("SARG:");
1004 exit(1);
1005 }
1006 time(&curtime);
1007 //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime));
1008 loctm=localtime(&curtime);
1009 strftime(wdir,sizeof(wdir),"%Y-%m-%d %H:%M:%S",loctm);
1010 fprintf(fp_ou,"%s %d\n",wdir,loctm->tm_isdst);
1011 fclose(fp_ou);
1012
1013 copy_images();
1014 }
1015
1016 void strip_latin(char *line)
1017 {
1018 int i,j;
1019 int skip;
1020
1021 j=0;
1022 skip=0;
1023 for (i=0;line[i];i++){
1024 if (skip){
1025 if (line[i]==';') skip=0;
1026 } else {
1027 if (line[i]=='&')
1028 skip=1;
1029 else
1030 line[j++]=line[i];
1031 }
1032 }
1033 line[j]='\0';
1034 return;
1035
1036 }
1037
1038 void zdate(char *ftime,int ftimesize, const char *DateFormat)
1039 {
1040
1041 time_t t;
1042 struct tm *local;
1043
1044 t = time(NULL);
1045 local = localtime(&t);
1046 if(strcmp(DateFormat,"u") == 0)
1047 strftime(ftime, ftimesize, "%b/%d/%Y %H:%M", local);
1048 if(strcmp(DateFormat,"e") == 0)
1049 strftime(ftime, ftimesize, "%d/%b/%Y-%H:%M", local);
1050 if(strcmp(DateFormat,"w") == 0)
1051 strftime(ftime, ftimesize, "%V-%H-%M", local);
1052 return;
1053 }
1054
1055
1056 char *fixtime(long int elap)
1057 {
1058
1059 int num = elap / 1000;
1060 int hor = 0;
1061 int min = 0;
1062 int sec = 0;
1063 static char buf[12];
1064
1065 if(strcmp(datetimeby,"bytes") == 0) {
1066 strcpy(buf,fixnum(elap,1));
1067 return buf;
1068 }
1069
1070 if(num<1) {
1071 sprintf(buf,"00:00:%02ld",elap);
1072 return buf;
1073 }
1074
1075 hor=num / 3600;
1076 min=(num % 3600) / 60;
1077 sec=num % 60;
1078
1079 if(hor==0 && min==0 && sec==0)
1080 strcpy(buf,"0");
1081 else
1082 sprintf(buf,"%01d:%02d:%02d",hor,min,sec);
1083
1084 return buf;
1085
1086 }
1087
1088
1089 void date_from(char *date, char *dfrom, char *duntil)
1090 {
1091
1092 int diaf;
1093 int mesf;
1094 int anof;
1095 int diau;
1096 int mesu;
1097 int anou;
1098 char wdate[50];
1099
1100 strncpy(wdate,date,sizeof(wdate)-1);
1101 wdate[sizeof(wdate)-1]='\0';
1102 if(strchr(wdate,'-') == NULL) {
1103 if (strlen(wdate)*2+1>=sizeof(wdate)) {
1104 fprintf(stderr,"SARG: Invalid date range passed as argument.\n");
1105 exit(1);
1106 }
1107 strcat(wdate,"-");
1108 strcat(wdate,date);
1109 strcpy(date,wdate);
1110 }
1111
1112 if (sscanf(wdate,"%d/%d/%d-%d/%d/%d",&diaf,&mesf,&anof,&diau,&mesu,&anou)!=6) {
1113 fprintf(stderr,"SARG: The date range passed as argument is not formated as dd/mm/yyyy-dd/mm/yyyy.\n");
1114 exit(1);
1115 }
1116
1117 sprintf(dfrom,"%04d%02d%02d",anof,mesf,diaf);
1118 sprintf(duntil,"%04d%02d%02d",anou,mesu,diau);
1119 return;
1120 }
1121
1122
1123 char *strlow(char *string)
1124 {
1125 char *s;
1126
1127 if (string)
1128 {
1129 for (s = string; *s; ++s)
1130 *s = tolower(*s);
1131 }
1132
1133 return string;
1134 }
1135
1136
1137
1138
1139 char *strup(char *string)
1140 {
1141 char *s;
1142
1143 if (string)
1144 {
1145 for (s = string; *s; ++s)
1146 *s = toupper(*s);
1147 }
1148
1149 return string;
1150 }
1151
1152
1153 void subs(char *str, int size, char *from, char *to)
1154 {
1155 char *tmp;
1156 int i;
1157 int ss, st, sf;
1158 int len;
1159 int shift;
1160
1161 tmp = strstr(str, from);
1162 if(tmp == NULL)
1163 return;
1164
1165 ss = strlen(str);
1166 sf = strlen(from);
1167 st = strlen(to);
1168 shift=st-sf;
1169
1170 if (shift>0) {
1171 if (ss+shift>=size){
1172 fprintf(stderr,"SARG: Cannot replace %s by %s in %s and fit within %d bytes\n",from,to,str,size);
1173 exit(1);
1174 }
1175 for (i=strlen(tmp) ; i>=sf ; i--)
1176 tmp[i+shift]=tmp[i];
1177 } else if (shift<0) {
1178 len=strlen(tmp);
1179 for (i=sf ; i<=len ; i++)
1180 tmp[i+shift]=tmp[i];
1181 }
1182 memcpy(tmp, to, st);
1183 return;
1184 }
1185
1186
1187 void removetmp(const char *outdir)
1188 {
1189
1190 FILE *fp_in;
1191 char warea[256];
1192
1193 if(!RemoveTempFiles)
1194 return;
1195
1196 if(debug) {
1197 debuga("%s: sarg-general, sarg-period",text[82]);
1198 }
1199 if (snprintf(warea,sizeof(warea),"%s/sarg-general",outdir)>=sizeof(warea)) {
1200 fprintf(stderr, "SARG: (removetmp) directory too long to remove: %s/sarg-period\n",outdir);
1201 exit(1);
1202 }
1203 if((fp_in=fopen(warea,"r"))==NULL){
1204 fprintf(stderr, "===SARG: (removetmp) %s: %s\n",text[45],warea);
1205 exit(1);
1206 }
1207 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
1208 if(strncmp(buf,"TOTAL",5) == 0)
1209 break;
1210 }
1211 fclose(fp_in);
1212 if((fp_in=fopen(warea,"w"))==NULL){
1213 fprintf(stderr, "SARG: (removetmp) %s: %s\n",text[45],warea);
1214 exit(1);
1215 }
1216 fputs(buf,fp_in);
1217 fclose(fp_in);
1218 if (snprintf(warea,sizeof(warea),"%s/sarg-period",outdir)>=sizeof(warea)) {
1219 fprintf(stderr, "SARG: (removetmp) directory too long to remove: %s/sarg-period\n",outdir);
1220 exit(1);
1221 }
1222 unlink(warea);
1223
1224 return;
1225 }
1226
1227 void load_excludecodes(const char *ExcludeCodes)
1228 {
1229
1230 FILE *fp_in;
1231 char data[80];
1232 int i;
1233
1234 if(ExcludeCodes[0] == '\0')
1235 return;
1236
1237 if((excludecode=(char *) malloc(1024))==NULL) {
1238 fprintf(stderr, "SARG: %s (1024):\n",text[59]);
1239 exit(1);
1240 }
1241 bzero(excludecode,1024);
1242
1243 if((fp_in=fopen(ExcludeCodes,"r"))==NULL) {
1244 fprintf(stderr, "SARG: (util) Cannot open file: %s (exclude_codes)\n",ExcludeCodes);
1245 exit(1);
1246 }
1247
1248 while(fgets(data,sizeof(data),fp_in)!=NULL) {
1249 for (i=strlen(data)-1 ; i>=0 && (unsigned char)data[i]<=' ' ; i--) data[i]=0;
1250 if (i<0) continue;
1251 strcat(excludecode,data);
1252 strcat(excludecode,";");
1253 }
1254
1255 fclose(fp_in);
1256 return;
1257
1258 }
1259
1260 void free_excludecodes(void)
1261 {
1262 if (excludecode) {
1263 free(excludecode);
1264 excludecode=NULL;
1265 }
1266 }
1267
1268 int vercode(const char *code)
1269 {
1270 char *cod;
1271 int clen;
1272
1273 if (excludecode && excludecode[0]!=0) {
1274 clen=strlen(code);
1275 for (cod=excludecode ; cod ; cod=strchr(cod+1,';')) {
1276 if (strncmp(code,cod,clen)==0 && cod[clen]==';')
1277 return 1;
1278 }
1279 }
1280 return 0;
1281 }
1282
1283 void fixnone(char *str)
1284 {
1285 int i;
1286
1287 for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--);
1288 if(i==3 && strncmp(str,"none",4) == 0)
1289 str[0]='\0';
1290
1291 return;
1292 }
1293
1294 void fixendofline(char *str)
1295 {
1296 int i;
1297
1298 for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--) str[i]=0;
1299 }
1300
1301 #ifdef LEGACY_TESTVALIDUSERCHAR
1302 int testvaliduserchar(const char *user)
1303 {
1304
1305 int x=0;
1306 int y=0;
1307
1308 for (y=0; y<strlen(UserInvalidChar); y++) {
1309 for (x=0; x<strlen(user); x++) {
1310 if(user[x] == UserInvalidChar[y])
1311 return 1;
1312 }
1313 }
1314 return 0;
1315 }
1316 #else
1317 int testvaliduserchar(const char *user)
1318 {
1319
1320 char * p_UserInvalidChar = UserInvalidChar ;
1321 const char * p_user ;
1322
1323 while( *p_UserInvalidChar ) {
1324 p_user = user ;
1325 while ( *p_user ) {
1326 if( *p_UserInvalidChar == *p_user )
1327 return 1;
1328 p_user++ ;
1329 }
1330 p_UserInvalidChar++ ;
1331 }
1332 return 0;
1333 }
1334 #endif
1335
1336 int compar( const void *a, const void *b )
1337 { if( *(int *)a > *(int *)b ) return 1;
1338 if( *(int *)a < *(int *)b ) return -1;
1339 return 0;
1340 }
1341
1342 int getnumlist( char *buf, numlist *list, const int len, const int maxvalue )
1343 {
1344 int i, j, d, flag, r1, r2;
1345 char *pbuf, **bp, *strbufs[ 24 ];
1346
1347 bp = strbufs;
1348 strtok( buf, " \t" );
1349 for( *bp = strtok( NULL, "," ), list->len = 0; *bp; *bp = strtok( NULL, "," ) ) {
1350 if( ++bp >= &strbufs[ 24 ] )
1351 break;
1352 list->len++;
1353 }
1354 if( ! list->len )
1355 return -1;
1356 d = 0;
1357 for( i = 0; i < list->len; i++ ) {
1358 if( strchr( strbufs[ i ], '-' ) != 0 ) {
1359 pbuf = strbufs[ i ];
1360 strtok( pbuf, "-" );
1361 pbuf = strtok( NULL, "\0" );
1362 r1 = atoi( strbufs[ i ] );
1363 if( ( r2 = atoi( pbuf ) ) >= maxvalue || r1 >= r2 )
1364 return -1;
1365 if( i + d + ( r2 - r1 ) + 1 <= len ) {
1366 for( j = r1; j <= r2; j++ )
1367 list->list[ i + d++ ] = j;
1368 d--;
1369 }
1370 }
1371 else
1372 if( ( list->list[ i + d ] = atoi( strbufs[ i ] ) ) >= maxvalue )
1373 return 1;
1374 }
1375 list->len += d;
1376 qsort( list->list, list->len, sizeof( int ), compar );
1377 do {
1378 flag = 0;
1379 for( i = 0; i < list->len - 1; i++ )
1380 if( list->list[ i ] == list->list[ i + 1 ] ) {
1381 for( j = i + 1; j < list->len; j++ )
1382 list->list[ j - 1 ] = list->list[ j ];
1383 list->len--;
1384 flag = 1;
1385 break;
1386 }
1387 } while( flag );
1388 return 0;
1389 }
1390
1391
1392 char *get_size(const char *path, const char *file)
1393 {
1394 FILE *fp;
1395 static char response[255];
1396 char cmd[255];
1397 char *ptr;
1398
1399 if (snprintf(cmd,sizeof(cmd),"du -skh %s%s",path,file)>=sizeof(cmd)) {
1400 printf("SARG: Cannot get disk space because the path %s%s is too long.\n",path,file);
1401 exit(1);
1402 }
1403 fp = popen(cmd, "r");
1404 if (!fgets(response, sizeof(response), fp)) {
1405 fprintf(stderr,"SARG: Cannot get disk size with command %s",cmd);
1406 exit(1);
1407 }
1408 ptr=strchr(response,'\t');
1409 if (ptr==NULL) {
1410 fprintf(stderr,"SARG: The command %s failed.\n",cmd);
1411 exit(1);
1412 }
1413 pclose(fp);
1414 *ptr='\0';
1415
1416 return (response);
1417 }
1418
1419 void show_info(FILE *fp_ou)
1420 {
1421 char ftime[127];
1422
1423 if(!ShowSargInfo) return;
1424 zdate(ftime, sizeof(ftime), DateFormat);
1425 fprintf(fp_ou,"<div align=\"center\"><table><tr><td><br><br></td><td class=\"info\">%s <a href='%s'><font class=\"info\">%s-%s</font></a> %s %s</td></tr></table></div>\n",text[108],URL,PGM,VERSION,text[109],ftime);
1426 }
1427
1428 void show_sarg(FILE *fp_ou, const char *ind)
1429 {
1430 if(ShowSargLogo) fprintf(fp_ou,"<div align=\"center\"><table class=\"logo\">\n<tr><th><a href=\"http://sarg.sourceforge.net\"><img src=\"%s/images/sarg.png\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\" alt=\"Sarg\"></a>&nbsp;Squid Analysis Report Generator</th></tr>\n<tr><th class=\"title\">&nbsp</th></tr>\n</table></div>\n",ind);
1431 }
1432
1433 void write_logo_image(FILE *fp_ou)
1434 {
1435 if(LogoImage[0]!='\0')
1436 fprintf(fp_ou, "<div align=\"center\"><table class=\"logo\">\n<tr><th><img src=\"%s\" width=\"%s\" height=\"%s\" alt=\"Logo\">&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n</div>\n",LogoImage,Width,Height,LogoText);
1437 }
1438
1439 void write_html_header(FILE *fp_ou, const char * ind)
1440 {
1441 fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
1442 css(fp_ou);
1443 fprintf(fp_ou,"</head>\n<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage);
1444 write_logo_image(fp_ou);
1445 show_sarg(fp_ou, ind);
1446 fprintf(fp_ou,"<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"title\">%s</th></tr>\n</table></div>\n<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",Title);
1447 }
1448
1449 void output_html_string(FILE *fp_ou,const char *str)
1450 {
1451
1452 while (*str) {
1453 switch (*str) {
1454 case '&':
1455 fputs("&amp;",fp_ou);
1456 break;
1457 case '<':
1458 fputs("&lt;",fp_ou);
1459 break;
1460 case '>':
1461 fputs("&gt;",fp_ou);
1462 break;
1463 case '"':
1464 fputs("&quot;",fp_ou);
1465 break;
1466 case '\'':
1467 fputs("&#39;",fp_ou);
1468 break;
1469 default:
1470 fputc(*str,fp_ou);
1471 }
1472 str++;
1473 }
1474 }
1475
1476 void baddata(void)
1477 {
1478 char dir[1024];
1479
1480 printf("SARG: ------------------------------------------------------------------------------\n");
1481 printf("SARG: MALICIUS CODE DETECTED.\n");
1482 printf("SARG: I think someone is trying to execute arbitrary code in your system using sarg.\n");
1483 printf("SARG: please review your access.log and/or your useragent.log file.\n");
1484 printf("SARG: process stoped. No actions taken.\n");
1485 printf("SARG: ------------------------------------------------------------------------------\n");
1486
1487 if (snprintf(dir,sizeof(dir),"%s/sarg",tmp)>=sizeof(dir)) {
1488 fprintf(stderr,"SARG: temporary directory too long: %s/sarg\n",tmp);
1489 exit(1);
1490 }
1491 unlinkdir(dir,0);
1492 unlinkdir(dirname,0);
1493
1494 exit(1);
1495 }
1496
1497
1498 void url_module(const char *url, char *w2)
1499 {
1500 int x, y;
1501 char w[255];
1502
1503 y=0;
1504 for(x=strlen(url)-1; x>=0; x--) {
1505 if(url[x] == '/' || y>=sizeof(w)-1) break;
1506 w[y++]=url[x];
1507 }
1508 if (x<0) {
1509 w2[0]='\0';
1510 return;
1511 }
1512
1513 x=0;
1514 for(y=y-1; y>=0; y--) {
1515 w2[x++]=w[y];
1516 }
1517 w2[x]='\0';
1518 }
1519
1520
1521 void write_html_trailer(FILE *fp_ou)
1522 {
1523 char ftime[128];
1524
1525 fputs("</table></div>\n",fp_ou);
1526 zdate(ftime, sizeof(ftime), DateFormat);
1527 show_info(fp_ou);
1528 fputs("</body>\n</html>\n",fp_ou);
1529 }
1530
1531 void version(void)
1532 {
1533 printf("SARG Version: %s\n",VERSION);
1534 exit(0);
1535 }
1536
1537 char *get_param_value(const char *param,char *line)
1538 {
1539 int plen;
1540
1541 while (*line==' ' || *line=='\t') line++;
1542 plen=strlen(param);
1543 if (strncasecmp(line,param,plen)) return(NULL);
1544 if (line[plen]!=' ' && line[plen]!='\t') return(NULL);
1545 line+=plen;
1546 while (*line==' ' || *line=='\t') line++;
1547 return(line);
1548 }
1549
1550 void unlinkdir(const char *dir,int contentonly)
1551 {
1552 struct stat st;
1553 DIR *dirp;
1554 struct dirent *direntp;
1555 char dname[MAXLEN];
1556 int err;
1557
1558 dirp=opendir(dir);
1559 if (!dirp) return;
1560 while ((direntp = readdir(dirp)) != NULL) {
1561 if (direntp->d_name[0] == '.' && (direntp->d_name[1] == '\0' ||
1562 (direntp->d_name[1] == '.' && direntp->d_name[2] == '\0')))
1563 continue;
1564 if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) {
1565 fprintf(stderr,"SARG: directory name to delete too long: %s/%s\n",dir,direntp->d_name);
1566 exit(1);
1567 }
1568 #ifdef HAVE_LSTAT
1569 err=lstat(dname,&st);
1570 #else
1571 err=stat(dname,&st);
1572 #endif
1573 if (err) {
1574 fprintf(stderr,"SARG: cannot stat %s\n",dname);
1575 exit(1);
1576 }
1577 if (S_ISREG(st.st_mode)) {
1578 if (unlink(dname)) {
1579 fprintf(stderr,"SARG: cannot delete %s - %s\n",dname,strerror(errno));
1580 exit(1);
1581 }
1582 } else if (S_ISDIR(st.st_mode)) {
1583 unlinkdir(dname,0);
1584 } else {
1585 fprintf(stderr,"SARG: unknown path type %s\n",dname);
1586 }
1587 }
1588 closedir(dirp);
1589
1590 if (!contentonly) {
1591 if (rmdir(dir)) {
1592 fprintf(stderr,"SARG: cannot delete %s - %s\n",dir,strerror(errno));
1593 exit(1);
1594 }
1595 }
1596 }