]> git.ipfire.org Git - thirdparty/sarg.git/blame - util.c
Imported sarg 2.0.9
[thirdparty/sarg.git] / util.c
CommitLineData
25697a35 1/*
94ff9470 2 * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br
25697a35 3 * 1998, 2005
94ff9470 4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
25697a35
GS
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26// #define LEGACY_MY_ATOLL
27// #define LEGACY_TESTVALIDUSERCHAR
28
29#include "include/conf.h"
30
31static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
32static char mtab2[12][3]={"01","02","03","04","05","06","07","08","09","10","11","12"};
33
34void getword(char *word, char *line, int stop)
35{
36 int x = 0,y;
37 int loop=0;
38 int limit=10000;
39 char wline[MAXLEN];
40
41 strcpy(wline,line);
42
43 if(strlen(line) < 3) {
44 word[0]='\0';
45 return;
46 }
47
48 for(x=0;((line[x]) && (line[x] != stop ));x++) {
49 loop++;
50 if(loop>=limit) {
51 printf("SARG: getword loop detected.\n");
52 printf("SARG: Record=\"%s\"\n",wline);
53 printf("SARG: searching for \'x%x\'\n",stop);
54 printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
55 word[0]='\0';
56 exit(1);
57 }
58 word[x] = line[x];
59 }
60
61 word[x] = '\0';
62 if(line[x]) ++x;
63 y=0;
64
65 while((line[y++] = line[x++]));
66}
67
68char * getword2(char *word, char *line, int stop)
69{
70 int x = 0 , y;
71 int limit=MAXLEN;
72
73 if(strlen(line) < 3) {
74 word[0]='\0';
75 return( line ) ;
76 }
77
78 // printf( "IN Buffer <%s>\n" , line ) ;
79 for(x=0;((line[x]) && (line[x] != stop && limit ));x++ , limit-- ) {
80 word[x] = line[x];
81 }
82 if( ! limit) {
83 printf("SARG: getword2 loop detected.\n");
84 printf("SARG: Buffer=\"%s\"\n",line);
85 printf("SARG: searching for \'x%x\'\n",stop);
86 printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
87 exit(1);
88 }
89
90 word[x] = '\0';
91 // printf( "Value <%s>\n" , word ) ;
92 // printf( "OUT Buffer <%s>\n" , line+x+1 ) ;
93 return( line + x +1) ;
94}
95
96void getword3(char *word, char *line, int stop)
97{
98 int x = 0,y;
99 int loop=0;
100
101 for(x=0;((line[x]) && (line[x] != stop ));x++)
102 word[x] = line[x];
103 word[x] = '\0';
104 if(line[x]) ++x;
105 y=0;
106 while((line[y++] = line[x++]));
107}
108
109
110#ifdef LEGACY_MY_ATOLL
111
112// BMG (bguillory@email.com)
113// 3 August 1999
114long long int my_atoll (const char *nptr)
115#define MAXLLL 30 //maximum number of digits in long long (a guess)
116{
117 int offset=0, x;
118 long long int returnval=0;
119 char one_digit[2];
120
121 one_digit[1]='\0';
122
123 // Soak up all the white space
124 while (isspace(nptr[offset])) {
125 offset++;
126 } //while
127
128 //For each character left to right
129 //change the character to a single digit
130 //multiply what we had before by 10 and add the new digit
131 for(x=offset; x<=MAXLLL+offset && isdigit(nptr[x]); x++) {
132 sprintf(one_digit, "%c", nptr[x]); //I don't know how else to do this
133 returnval = (returnval * 10) + atoi(one_digit);
134 } //for
135
136 return returnval;
137
138} //my_atoll
139
140#else
141
142#define MAXLLL 30 //maximum number of digits in long long (a guess)
143long long int my_atoll (const char *nptr)
144{
145 long long int returnval=0;
146 char * t = nptr ;
147 int max_digits = MAXLLL ;
148
149 // Soak up all the white space
150 while (isspace( *t )) {
151 t++;
152 } //while
153
154 //For each character left to right
155 //change the character to a single digit
156 //multiply what we had before by 10 and add the new digit
157
158 for( ; --max_digits && isdigit( *t ) ; t++ )
159 {
160 returnval = ( returnval * 10 ) + ( *t - '0' ) ;
161 }
162
163 return returnval;
164
165} //my_atoll
166
167#endif
168
169
170void my_mkdir(char *name)
171{
172 char w0[255];
173 char w1[255];
174 char w2[255];
175
176 if(strncmp(name,".",1) == 0 || strncmp(name,"/",1) != 0) {
177 fprintf(stderr,"SARG: Ivalid path (%s). Please, use absolute paths only.\n",name);
178 fprintf(stderr,"SARG: process aborted.\n");
179 exit(1);
180 }
181
182 strcpy(w0,name);
183 strcpy(w2,"/");
184 getword(w1,w0,'/');
185 while(strstr(w0,"/") != 0) {
186 getword(w1,w0,'/');
187 strcat(w2,w1);
188 if(access(w2, R_OK) != 0) {
189 if(mkdir(w2,0755)) {
190 fprintf(stderr,"SARG: mkdir %s %s\n",w2,strerror(errno));
191 fprintf(stderr,"SARG: process aborted.\n");
192 exit(1);
193 }
194 }
195 strcat(w2,"/");
196 }
197 strcat(w2,w0);
198 if(access(w2, R_OK) != 0) {
199 if(mkdir(w2,0755)) {
200 fprintf(stderr,"SARG: mkdir %s %s\n",w2,strerror(errno));
201 fprintf(stderr,"SARG: process aborted.\n");
202 exit(1);
203 }
204 }
205}
206
207
208void my_lltoa(unsigned long long int n, char s[], int len)
209{
210 int i = 0;
211 int x = 0;
212 char ww[50];
213 do {
214 s[i++] = (n % 10) + '0';
215 } while ((n /= 10) > 0);
216 s[i] = '\0';
217 {
218 int c,i,j;
219 for (i = 0, j = strlen(s)-1; i<j; i++, j--)
220 {
221 c = s[i];
222 s[i] = s[j];
223 s[j] = c;
224 }
225 }
226
227 if(len) {
228 bzero(ww,sizeof(ww));
229 i=len-strlen(s)-1;
230 for(x=0; x<=i; x++)
231 ww[x]='0';
232 i=strlen(s);
233 strncat(ww,s,i>sizeof(ww)?sizeof(ww):i);
234 strcpy(s,ww);
235 }
236
237}
238
239
240void builddia(char *dia, char *mes, char *ano, char *df, char *wdata)
241{
242 char ndia[11];
243 char nmes[3];
244 int x;
245
246 if(strlen(dia) < 1) return;
247
248 ndia[0]='\0';
249 nmes[0]='\0';
250
251 for(x=0; x<12; x++) {
252 if(strcmp(mtab1[x],mes) == 0) {
253 strncpy(nmes,mtab2[x],sizeof(nmes)-1);
254 nmes[sizeof(nmes)-1]=0;
255 break;
256 }
257 }
258
259 snprintf(wdata,9,"%s%s%s",ano,nmes,dia);
260
261 if(strncmp(df,"u",1) != 0)
262 snprintf(ndia,sizeof(ndia),"%s/%s/%s",dia,nmes,ano);
263 else
264 snprintf(ndia,sizeof(ndia),"%s/%s/%s",nmes,dia,ano);
265
266 strcpy(dia,ndia);
267
268}
269
270
271void buildymd(char *dia, char *mes, char *ano, char *wdata)
272{
273 char nmes[3];
274 int x;
275
276 nmes[0]='\0';
277
278 for(x=0; x<12; x++) {
279 if(strcmp(mtab1[x],mes) == 0)
280 strcpy(nmes,mtab2[x]);
281 }
282
283 sprintf(wdata,"%s%s%s",ano,nmes,dia);
284
285}
286
287
288void conv_month(char *month)
289{
290 int x;
291
292 for(x=0; x<12; x++) {
293 if(strcmp(mtab1[x],month) == 0)
294 strcpy(month,mtab2[x]);
295 }
296
297}
298
299
300void conv_month_name(char *month)
301{
302 int x;
303
304 for(x=0; x<12; x++) {
305 if(strcmp(mtab2[x],month) == 0)
306 strcpy(month,mtab1[x]);
307 }
308}
309
310
311void fixper(char *tbuf, char *periodo, char *duntil)
312{
313
314 char warea[50];
315 char dia[5], mes[5], ano[5];
316 int x;
317
318 warea[0]='\0';
319
320 strncpy(dia,duntil+6,2);
321 dia[2]='\0';
322 strncpy(mes,duntil+4,2);
323 mes[2]='\0';
324 strncpy(ano,duntil,4);
325 ano[4]='\0';
326
327 for(x=0; x<12; x++) {
328 if(strcmp(mtab2[x],mes) == 0)
329 strcpy(mes,mtab1[x]);
330 }
331
332 if(strcmp(df,"e") == 0)
333 sprintf(warea,"%s%s%s",dia,mes,ano);
334 if(strcmp(df,"u") == 0)
335 sprintf(warea,"%s%s%s",ano,mes,dia);
336
337 strcat(periodo,warea);
338}
339
340
341void debuga(char *msg)
342{
343 fprintf(stderr, "SARG: %s\n",msg);
344
345}
346
347
348void debugaz(char *head, char *msg)
349{
350 fprintf(stderr, "SARG: (util) %s=%s\n",head, msg);
351
352}
353
354
355void fixip(char *ip)
356{
357 char n1[MAXLEN], n2[MAXLEN], n3[MAXLEN];
358 char wip[MAXLEN];
359 char sep[2]=".";
360 int iflag=0;
361
362 strcpy(wip,ip);
363
364 if(strstr(ip,".") != 0) {
365 strcpy(sep,"_");
366 iflag++;
367 }
368
369 if(iflag) {
370 getword(n1,wip,'.');
371 getword(n2,wip,'.');
372 getword(n3,wip,'.');
373 } else {
374 getword(n1,wip,'_');
375 getword(n2,wip,'_');
376 getword(n3,wip,'_');
377 }
378 ip[0]='\0';
379 sprintf(ip,"%s%s%s%s%s%s%s",n1,sep,n2,sep,n3,sep,wip);
380
381}
382
383
384char *fixnum(long long int value, int n)
385#define MAXIMO 1024
386{
387 char num[MAXIMO];
388 char buf[MAXIMO * 2];
389 char *pbuf;
390 char ret[MAXIMO * 2];
391 char *pret;
392 float perc=0;
393 register int i, j, k;
394 static char abbrev[30];
395
396 my_lltoa(value, num, 0);
397
398 if(strcmp(DisplayedValues,"abbreviation") == 0) {
399 if(strlen(num) <= 3)
400 sprintf(abbrev,"%s",num);
401 if(strlen(num) == 4 || strlen(num) == 7 || strlen(num) == 10 || strlen(num) == 13) {
402 snprintf(abbrev,2,"%s",num);
403 strncat(abbrev,".",1);
404 strncat(abbrev,num+1,2);
405 if(!n) return(abbrev);
406 if(strlen(num) == 4)
407 strncat(abbrev,"K",1);
408 else if(strlen(num) == 7)
409 strncat(abbrev,"M",1);
410 else if(strlen(num) == 10)
411 strncat(abbrev,"G",1);
412 else if(strlen(num) == 13)
413 strncat(abbrev,"T",1);
414 }
415 if(strlen(num) == 5 || strlen(num) == 8 || strlen(num) == 11 || strlen(num) == 14) {
416 snprintf(abbrev,3,"%s",num);
417 strncat(abbrev,".",1);
418 strncat(abbrev,num+2,2);
419 if(!n) return(abbrev);
420 if(strlen(num) == 5)
421 strncat(abbrev,"K",1);
422 else if(strlen(num) == 8)
423 strncat(abbrev,"M",1);
424 else if(strlen(num) == 11)
425 strncat(abbrev,"G",1);
426 else if(strlen(num) == 14)
427 strncat(abbrev,"T",1);
428 }
429 if(strlen(num) == 6 || strlen(num) == 9 || strlen(num) == 12 || strlen(num) == 15) {
430 snprintf(abbrev,4,"%s",num);
431 strncat(abbrev,".",1);
432 strncat(abbrev,num+3,2);
433 if(!n) return(abbrev);
434 if(strlen(num) == 6)
435 strncat(abbrev,"K",1);
436 else if(strlen(num) == 9)
437 strncat(abbrev,"M",1);
438 else if(strlen(num) == 12)
439 strncat(abbrev,"G",1);
440 else if(strlen(num) == 15)
441 strncat(abbrev,"T",1);
442 }
443
444 return(abbrev);
445 }
446
447 bzero(buf, MAXIMO*2);
448
449 pbuf = buf;
450 pret = ret;
451 k = 0;
452
453 for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) {
454 if ( k == 2 && i != 0 ) {
455 k = 0;
456 pbuf[j++] = num[i];
457 if(strcmp(UseComma,"yes") == 0)
458 pbuf[j++] = ',';
459 else pbuf[j++] = '.';
460 continue;
461 }
462 pbuf[j] = num[i];
463 j++;
464 k++;
465 }
466
467 pret[0]='\0';
468
469 for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++)
470 pret[j] = pbuf[i];
471
472 pret[j] = '\0';
473
474 return pret;
475}
476
477
478void buildhref(char * href)
479{
480 char whref[MAXLEN];
481
482 if(strcmp(href,"./") == 0){
483 href[0]='\0';
484 strcat(href,"<a href='");
485 return;
486 }
487
488 href[strlen(href)-1]='\0';
489 sprintf(whref,"%s",strrchr(href,'/'));
490
491 strcpy(href,"<a href='");
492 strcat(href,whref);
493 strcat(href,"/");
494
495 return;
496
497}
498
499
500char *buildtime(long long int elap)
501{
502
503 int num = elap / 1000;
504 int hor = 0;
505 int min = 0;
506 int sec = 0;
507 static char buf[12];
508
509 buf[0]='\0';
510
511 hor=num / 3600;
512 min=(num % 3600) / 60;
513 sec=num % 60;
514 sprintf(buf,"%02d:%02d:%02d",hor,min,sec);
515
516 return(buf);
517
518}
519
520
521void obtdate(char *dirname, char *name, char *data)
522{
523
524 FILE *fp_in;
525 char wdir[MAXLEN];
526
527 sprintf(wdir,"%s%s/date",dirname,name);
528 if ((fp_in = fopen(wdir, "r")) == 0) {
529 data[0]='\0';
530 return;
531 }
532
533 fgets(data,80,fp_in);
534 fclose(fp_in);
535 data[strlen(data)-1]='\0';
536
537 return;
538
539}
540
541
542void obtuser(char *dirname, char *name, char *tuser)
543{
544
545 FILE *fp_in;
546 char wdir[MAXLEN];
547
548 sprintf(wdir,"%s%s/usuarios",dirname,name);
549 if((fp_in=fopen(wdir,"r"))==NULL){
550 tuser[0]='\0';
551 return;
552 }
553
554 fgets(tuser,20,fp_in);
555 tuser[strlen(tuser)-1]='\0';
556 fclose(fp_in);
557
558 return;
559
560}
561
562
563void obttotal(char *dirname, char *name, char *tbytes, char *tuser, char *media)
564{
565
566 FILE *fp_in;
567 char wdir[MAXLEN];
568 long long int med=0;
569 long long int wtuser=0;
570
571 sprintf(wdir,"%s%s/geral",dirname,name);
572
573 if ((fp_in = fopen(wdir, "r")) == 0) {
574 tbytes=0;
575 return;
576 }
577
578 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
579 getword(warea,buf,' ');
580 if(strcmp(warea,"TOTAL") != 0)
581 continue;
582 getword(warea,buf,' ');
583 getword(warea,buf,' ');
584 twork=my_atoll(warea);
585 sprintf(tbytes,"%s",fixnum(twork,1));
586 }
587 fclose(fp_in);
588
589 if(tuser[0] == '\0') {
590 wtuser=0;
591 sprintf(media,"%s","0");
592 return;
593 }
594
595 wtuser=my_atoll(tuser);
596 med=my_atoll(warea) / wtuser;
597 sprintf(media,"%s",fixnum(med,1));
598
599 return;
600
601}
602
603
604//void gperiodo(char *dirname, const char *periodo)
605void gperiodo()
606{
607
608 FILE *fp_ou;
609
610 char wdirname[MAXLEN];
611
612 strcpy(wdirname,dirname);
613 strcat(wdirname,"/");
614 strcat(wdirname,"periodo");
615
616 if((fp_ou=fopen(wdirname,"w"))==NULL){
617 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
618 exit(1);
619 }
620
621 fputs(periodo,fp_ou);
622 fclose(fp_ou);
623
624 if(debug)
625 debuga((char *)text[50]);
626
627 return;
628
629}
630
631void vrfydir(char *dir, char *per1, char *addr, char *site, char *us, char *form)
632{
633 FILE *img_in, *img_ou;
634 int num=1, count=0;
635 int c;
636 char wdir[MAXLEN];
637 char per2[MAXLEN];
638 char dirname2[MAXLEN];
639 char buffer[1024];
640 char images[512];
641 DIR *dirp;
642 struct dirent *direntp;
643
644 sprintf(wdir,"%s",dir);
645
646 if(strlen(us) > 0) {
647 strcat(wdir,"-");
648 strcat(wdir,us);
649 }
650 if(strlen(addr) > 0) {
651 strcat(wdir,"-");
652 strcat(wdir,addr);
653 }
654 if(strlen(site) > 0) {
655 strcat(wdir,"-");
656 strcat(wdir,site);
657 }
658
659 if(strcmp(dirname,wdir) != 0)
660 strcpy(dirname,wdir);
661
662 strcpy(dirname2,dirname);
663
664 if(strcmp(OverwriteReport,"no") == 0) {
665 while(num) {
666 if(access(wdir,R_OK) == 0) {
667 sprintf(wdir,"%s.%d",dirname,num);
668 sprintf(per2,"%s.%d",per1,num);
669 num++;
670 count++;
671 } else
672 break;
673 }
674
675 if(count > 0) {
676 if(debug)
677 fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname2,text[52],wdir);
678 rename(dirname2,wdir);
679 }
680 } else {
681 if(access(dir,R_OK) == 0) {
682 sprintf(csort,"rm -r %s",dir);
683 system(csort);
684 }
685 }
686
687 my_mkdir(dirname);
688
689 strcpy(dirname2,wdir);
690 sprintf(images,"%simages",outdir);
691 mkdir(images,0755);
692
693 sprintf(wdir,"date >%s/%s",dirname,"date");
694 system(wdir);
695
696 sprintf(per2,"%s/images",SYSCONFDIR);
697
698 dirp = opendir(per2);
699 if(dirp==NULL) {
700 fprintf(stderr, "SARG: (util) %s %s: %s\n","Can't open directory", per2,strerror(errno));
701 return;
702 }
703 while ((direntp = readdir( dirp )) != NULL ){
704 if(strncmp(direntp->d_name,".",1) == 0)
705 continue;
706 sprintf(val10,"%s/%s",per2,direntp->d_name);
707 sprintf(val11,"%s/%s",images,direntp->d_name);
708 img_in = fopen(val10, "rb");
709 if(img_in!=NULL) {
710 img_ou = fopen(val11, "wb");
711 if(img_ou!=NULL) {
712 while (c!=EOF) {
713 c = fgetc(img_in);
714 if(c==EOF) break;
715 fputc(c,img_ou);
716 }
717 c=0;
718 fclose(img_ou);
719 } else
720 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open/create file", val11, strerror(errno));
721 } else
722 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open file", val10, strerror(errno));
723
724 fclose(img_in);
725 }
726 (void) rewinddir(dirp);
727 (void) closedir(dirp);
728
729 return;
730
731
732}
733
734
735void strip_latin(char *line)
736{
737 char buf[255];
738 char warea[255];
739
740 while(strstr(line,"&") != 0){
741 getword(warea,line,'&');
742 strncat(warea,line,1);
743 getword(buf,line,';');
744 strcat(warea,line);
745 strcpy(line,warea);
746 }
747
748 return;
749
750}
751
752void zdate(char *ftime, char *DateFormat)
753{
754
755 time_t t;
756 struct tm *local;
757
758 t = time(NULL);
759 local = localtime(&t);
760 if(strcmp(DateFormat,"u") == 0)
761 strftime(ftime, 127, "%b/%d/%Y %H:%M", local);
762 if(strcmp(DateFormat,"e") == 0)
763 strftime(ftime, 127, "%d/%b/%Y-%H:%M", local);
764 if(strcmp(DateFormat,"w") == 0)
765 strftime(ftime, 127, "%V-%H-%M", local);
766
767 return;
768}
769
770
771char *fixtime(long int elap)
772{
773
774 int num = elap / 1000;
775 int hor = 0;
776 int min = 0;
777 int sec = 0;
778 static char buf[12];
779
780 if(strcmp(datetimeby,"bytes") == 0) {
781 sprintf(buf,"%s",fixnum(elap,1));
782 return buf;
783 }
784
785 buf[0]='\0';
786
787 if(num<1) {
788 sprintf(buf,"00:00:%02ld",elap);
789 return buf;
790 }
791
792 hor=num / 3600;
793 min=(num % 3600) / 60;
794 sec=num % 60;
795
796 sprintf(buf,"%01d:%02d:%02d",hor,min,sec);
797
798 if(strcmp(buf,"0:00:00") == 0)
799 strcpy(buf,"0");
800
801 return buf;
802
803}
804
805
806void date_from(char *date, char *dfrom, char *duntil)
807{
808
809 char diaf[10];
810 char mesf[10];
811 char anof[10];
812 char diau[10];
813 char mesu[10];
814 char anou[10];
815 static char wdate[50];
816
817
818 strcpy(wdate,date);
819 if(strstr(wdate,"-") == 0) {
820 strcat(wdate,"-");
821 strcat(wdate,date);
822 strcpy(date,wdate);
823 }
824
825 getword(diaf,wdate,'/');
826 getword(mesf,wdate,'/');
827 getword(anof,wdate,'-');
828 getword(diau,wdate,'/');
829 getword(mesu,wdate,'/');
830 strcpy(anou,wdate);
831
832 sprintf(dfrom,"%s%s%s",anof,mesf,diaf);
833 sprintf(duntil,"%s%s%s",anou,mesu,diau);
834 return;
835}
836
837
838char *strlow(char *string)
839{
840 char *s;
841
842 if (string)
843 {
844 for (s = string; *s; ++s)
845 *s = tolower(*s);
846 }
847
848 return string;
849}
850
851
852
853
854char *strup(char *string)
855{
856 char *s;
857
858 if (string)
859 {
860 for (s = string; *s; ++s)
861 *s = toupper(*s);
862 }
863
864 return string;
865}
866
867
868char *subs(char *str, char *from, char *to)
869{
870 char *tmp;
871 char *ret;
872 unsigned int ss, st;
873
874 if(strstr(str,from) == 0)
875 return (char *) str;
876
877 ss = strlen(str); st = strlen(to);
878 ret = (char *) malloc(ss + st);
879 bzero(ret,ss+st);
880
881 tmp = strstr(str, from);
882 if ( tmp == (char *) NULL )
883 return (char *) NULL;
884 strncpy(ret, str, ss - strlen(tmp));
885 strcat(ret, to);
886 strcat(ret, (tmp+strlen(from)));
887 return (char *) ret;
888}
889
890
891void removetmp(char *outdir)
892{
893
894 FILE *fp_in;
895 char warea[256];
896
897 if(strcmp(RemoveTempFiles,"yes") != 0) {
898 return;
899 } else {
900 if(debug) {
901 sprintf(msg,"%s: geral, periodo",text[82]);
902 debuga(msg);
903 }
904 sprintf(warea,"%s/geral",outdir);
905 if((fp_in=fopen(warea,"r"))==NULL){
906 fprintf(stderr, "SARG: (removetmp) %s: %s\n",text[45],warea);
907 exit(1);
908 }
909 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
910 if(strncmp(buf,"TOTAL",5) == 0)
911 break;
912 }
913 fclose(fp_in);
914 if((fp_in=fopen(warea,"w"))==NULL){
915 fprintf(stderr, "SARG: (removetmp) %s: %s\n",text[45],warea);
916 exit(1);
917 }
918 fputs(buf,fp_in);
919 fclose(fp_in);
920 sprintf(warea,"%s/periodo",outdir);
921 unlink(warea);
922 }
923
924 return;
925}
926
927void load_excludecodes()
928{
929
930 FILE *fp_in;
931 char data[80];
932
933 if((fp_in=fopen(ExcludeCodes,"r"))==NULL) {
934 fprintf(stderr, "SARG: (util) Cannot open file: %s (exclude_codes)\n",ExcludeCodes);
935 exit(1);
936 }
937
938 while(fgets(data,80,fp_in)!=NULL) {
939 data[strlen(data)-1]='\0';
940 strcat(excludecode,data);
941 strcat(excludecode,";");
942 excode++;
943 }
944
945 fclose(fp_in);
946 return;
947
948}
949
950int vercode(char *code)
951{
952 char warea[1024];
953 char cod[80];
954 int z;
955
956 strcpy(warea,excludecode);
957 for(z=0; z<=excode-1; z++) {
958 getword(cod,warea,';');
959 if(strcmp(code,cod) == 0)
960 return 1;
961 }
962 return 0;
963}
964
965void fixnone(char *str)
966{
967 if(strstr(str,"\n") != 0)
968 str[strlen(str)-1]='\0';
969 if(strcmp(str,"none") == 0)
970 str[0]='\0';
971
972 return;
973}
974
975#ifdef LEGACY_TESTVALIDUSERCHAR
976int testvaliduserchar(char *user)
977{
978
979 int x=0;
980 int y=0;
981
982 for (y=0; y<strlen(UserInvalidChar); y++) {
983 for (x=0; x<strlen(user); x++) {
984 if(user[x] == UserInvalidChar[y])
985 return 1;
986 }
987 }
988 return 0;
989}
990#else
991int testvaliduserchar(char *user)
992{
993
994 char * p_UserInvalidChar = UserInvalidChar ;
995 char * p_user ;
996
997 while( *p_UserInvalidChar ) {
998 p_user = user ;
999 while ( *p_user ) {
1000 if( *p_UserInvalidChar == *p_user )
1001 return 1;
1002 p_user++ ;
1003 }
1004 p_UserInvalidChar++ ;
1005 }
1006 return 0;
1007}
1008#endif
1009
1010int compar( const void *a, const void *b )
1011{ if( *(int *)a > *(int *)b ) return 1;
1012 if( *(int *)a < *(int *)b ) return -1;
1013 return 0;
1014}
1015
1016int getnumlist( char *buf, numlist *list, const int len, const int maxvalue )
1017{ int i, j, d, flag, r1, r2;
1018 char *pbuf, **bp, *strbufs[ 24 ];
1019
1020 bp = strbufs;
1021 strtok( buf, " \t" );
1022 for( *bp = strtok( NULL, "," ), list->len = 0; *bp; *bp = strtok( NULL, "," ) )
1023 { if( ++bp >= &strbufs[ 24 ] )
1024 break;
1025 list->len++;
1026 }
1027 if( ! list->len )
1028 return -1;
1029 d = 0;
1030 for( i = 0; i < list->len; i++ )
1031 { if( strstr( strbufs[ i ], "-" ) != 0 )
1032 { pbuf = strbufs[ i ];
1033 strtok( pbuf, "-" );
1034 pbuf = strtok( NULL, "\0" );
1035 r1 = atoi( strbufs[ i ] );
1036 if( ( r2 = atoi( pbuf ) ) >= maxvalue || r1 >= r2 )
1037 return -1;
1038 if( i + d + ( r2 - r1 ) + 1 <= len )
1039 { for( j = r1; j <= r2; j++ )
1040 list->list[ i + d++ ] = j;
1041 d--;
1042 }
1043 }
1044 else
1045 if( ( list->list[ i + d ] = atoi( strbufs[ i ] ) ) >= maxvalue )
1046 return 1;
1047 }
1048 list->len += d;
1049 qsort( list->list, list->len, sizeof( int ), compar );
1050 do
1051 { flag = 0;
1052 for( i = 0; i < list->len - 1; i++ )
1053 if( list->list[ i ] == list->list[ i + 1 ] )
1054 { for( j = i + 1; j < list->len; j++ )
1055 list->list[ j - 1 ] = list->list[ j ];
1056 list->len--;
1057 flag = 1;
1058 break;
1059 }
1060 } while( flag );
1061 return 0;
1062}
1063
1064
1065void show_info(FILE *fp_ou)
1066{
1067
1068 if(strcmp(ShowSargInfo,"yes") == 0) {
1069 zdate(ftime, DateFormat);
1070 sprintf(html,"<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></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
1071 fputs(html,fp_ou);
1072 }
1073}
1074
1075void show_sarg(FILE *fp_ou, char *ind)
1076{
1077 if(strcmp(ShowSargLogo,"yes") == 0) {
1078 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ou);
1079 sprintf(html,"<tr><th class=\"logo\"><a href=\"http://www.sarg-squid.org\"><img src=\"%s/images/sarg.png\" border=\"0\" align=\"absmiddle\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\"></a>&nbsp;<font class=\"logo\">Squid Analysis Report Generator</font></th></tr>\n",ind,Title);
1080 fputs(html,fp_ou);
1081 sprintf(html,"<tr><th class=\"title\">&nbsp</th></tr>\n",Title);
1082 fputs(html,fp_ou);
1083 fputs("<table>\n",fp_ou);
1084 }
1085}
1086
1087char url_module(char *url, char *w2)
1088{
1089 int x, y;
1090 char w[255];
1091
1092 bzero(w, 255);
1093 bzero(w2, 255);
1094 y=0;
1095 for(x=strlen(url)-1; x>=0; x--) {
1096 if(url[x] == '/' || y>255) break;
1097 w[y]=url[x];
1098 y++;
1099 }
1100
1101 y=0;
1102 for(x=strlen(w)-1; x>=0; x--) {
1103 w2[y]=w[x];
1104 y++;
1105 }
1106
1107 return;
1108}
1109
1110void version()
1111{
1112 printf("SARG Version: %s\n",VERSION);
1113 exit(0);
1114}