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