]> git.ipfire.org Git - thirdparty/sarg.git/blob - documentation/util.txt
Store the period internaly and get rid of the sarg-period file.
[thirdparty/sarg.git] / documentation / util.txt
1 /*! \file util.c
2 \brief Various useful functions.
3 */
4
5
6
7
8
9 /*! \var static char mtab1[12][4];
10 The list of the months.
11 */
12
13
14
15
16 /*! \fn void getword_start(struct getwordstruct *gwarea, char *line)
17 Initialize the getword buffer with the given text line.
18
19 \param gwarea The getword buffer to initialize.
20 \param line The text line to use in the getword buffer.
21 */
22
23
24
25
26
27 /*! \fn void getword_restart(struct getwordstruct *gwarea)
28 Restart the getword buffer from the beginning.
29
30 \param gwarea The getword buffer to reset.
31 */
32
33
34
35
36
37 /*! \fn int getword(char *word, int limit, struct getwordstruct *gwarea, char stop)
38
39 Extract one "word" from the text line and remove it from the text line. The word's boundary is defined
40 by the \a stop character. If multiple stop characters are found after the word, only the first one is
41 removed. Therfore, passing the line buffer again to the function will remove the next word in a column
42 like manner.
43
44 \param word The buffer to store the extracted word.
45 \param limit The size of the buffer. If the stop character isn't found before that limit is reached,
46 the function displays an error message and returns an error code.
47 \param gwarea The getword buffer initialized by getword_start().
48 \param stop The character indicating the end of the word.
49
50 \retval 0 The word is extracted.
51 \retval -1 The stop character was not found before the limit is reached.
52
53 */
54
55
56
57
58
59 /*! \fn int getword_limit(char *word, int limit, struct getwordstruct *gwarea, char stop)
60 Extract one word with a maximum size and skip any supernumerary bytes until the stop bytes is
61 found.
62
63 \param word The buffer to store the extracted word.
64 \param limit The size of the buffer.
65 \param gwarea The getword buffer initialized by getword_start().
66 \param stop The character indicating the end of the word.
67
68 \retval 0 The word is extracted.
69 */
70
71
72
73
74
75 /*! \fn int getword_multisep(char *word, int limit, struct getwordstruct *gwarea, char stop)
76
77 Extract one "word" from the text line and remove it from the text line. The word's boundary is defined
78 by the \a stop character. All the stop characters following the word are removed too. Therefore, passing
79 the line buffer again to the function will remove words even if they are separated by multiple stop
80 characters.
81
82 \param word The buffer to store the extracted word.
83 \param limit The size of the buffer. If the stop character isn't found before that limit is reached,
84 the function displays an error message and returns an error code.
85 \param gwarea The getword buffer initialized by getword_start().
86 \param stop The character indicating the end of the word.
87
88 \retval 0 The word is extracted.
89 \retval -1 The stop character was not found before the limit is reached.
90
91 */
92
93
94
95
96 /*! \fn int getword_skip(int limit, struct getwordstruct *gwarea, char stop)
97 Skip one "word" from the text line and remove it from the text line. The word's boundary is defined
98 by the \a stop character.
99
100 \param limit The maximum number of characters to skip. If the stop character isn't found before that limit is reached,
101 the function displays an error message and returns an error code.
102 \param gwarea The getword buffer initialized by getword_start().
103 \param stop The character indicating the end of the word.
104
105 \retval 0 The word is skipped.
106 \retval -1 The stop character was not found before the limit is reached.
107 */
108
109
110
111
112
113 /*! \fn int getword_atoll(long long int *number, struct getwordstruct *gwarea, char stop)
114 Extract one number from the text line.
115
116 \param number Where the store the extracted number.
117 \param gwarea The getword buffer initialized by getword_start().
118 \param stop The character indicating the end of the word.
119
120 \retval 0 The number is extracted.
121 \retval -1 The stop character was not found after the number.
122 */
123
124
125
126
127 /*! \fn int getword_ptr(char *orig_line,char **word, struct getwordstruct *gwarea, char stop)
128 Return a pointer to a null terminated string starting at the current position and ending
129 and the stop character.
130
131 \param orig_line The line that is being parsed.
132 \param word A pointer to set to the beginning of the string.
133 \param gwarea The getword buffer initialized by getword_start().
134 \param stop The character indicating the end of the word.
135
136 \retval 0 The word is skipped.
137 \retval -1 Invalid \a orig_line passed to the function.
138 */
139
140
141
142
143 /*! \fn long long int my_atoll (const char *nptr)
144
145 Convert a string into a long long.
146
147 \param nptr The string containing the number to convert.
148
149 \return The number found in the string or zero if no number was found.
150
151 */
152
153
154
155
156
157 /*! \fn static int is_absolute(const char *path)
158
159 Tell if the path is absolute. On Unix, a path is absolute if it starts with a /.
160
161 On Windows, we also check if the path starts with "x:" where x can be any letter.
162
163 \param path The path to check.
164
165 \retval 1 The path is absolute.
166 \retval 0 The path is relative.
167 */
168
169
170
171
172
173 /*! \fn void my_mkdir(const char *name)
174
175 Create the directory and all the non existing parent directories.
176
177 \param name The absolute directory to create.
178
179 */
180
181
182
183
184
185 /*! \fn void my_lltoa(unsigned long long int n, char *s, int ssize, int len)
186
187 Format a long long into a string.
188
189 \param n The number to format.
190 \param s The buffer to write the number.
191 \param ssize The size of the output buffer.
192 \param len The minimum number of digits to format in the output. If the formatted
193 number is less than this length, it is padded with zeros.
194
195 */
196
197
198
199
200
201 /*! \fn int builddia(int day, int month, int year)
202
203 Return a numerical value made of the date.
204
205 \param day The day of the date.
206 \param month The number of the month starting from 1.
207 \param year The year.
208
209 \return The date in an integer format computed as year*10000+month*100+day.
210 */
211
212
213
214
215
216 /*! \fn void buildymd(const char *dia, const char *mes, const char *ano, char *wdata)
217 Convert the date into a machine format YYYYMMDD.
218
219 \param dia The day.
220 \param mes The name of the month as spelled in ::mtab1. If the month is invalid, the output date
221 is set to month 13.
222 \param ano The year.
223 \param wdata The buffer to format the date.
224 */
225
226
227
228
229
230 /*! \fn int conv_month(int char *month)
231 Convert the month's name into its two digits numerical equivalent.
232
233 \param month The name of the month as spelled in ::mtab1.
234
235 \return The month number on starting from one. If the month name is not in ::mtab1,
236 13 is returned.
237 */
238
239
240
241
242
243 /*! \fn const char *conv_month_name(int month)
244 Convert a month number into a name.
245
246 \param month The number of the month in the range 1 to 12.
247
248 \return The name of the month from ::mtab1 unless the month number is not between 1 and 12
249 in which case, the number is returned encoded on 3 characters. If the number is
250 invalid, the returned string is static and will be reused by any subsequent call to this
251 function with an invalid month number.
252 */
253
254
255
256
257
258 /*! \fn void name_month(char *month,int month_len)
259 Get the name of the month according to the language file selected by the user.
260
261 \param month The number of the month. It is replaced by the month's name if the number is between
262 1 and 12 or by the name of December if the number is invalid.
263 \param month_len The size of the \a month buffer.
264
265 */
266
267
268
269
270
271 /*! \fn void debuga(const char *msg,...)
272 Write a debug message to stderr. The message is prefixed by "SARG:" to identify its origin.
273
274 \param msg The printf like message to format.
275 \param ... The arguments to format in the message.
276
277 */
278
279
280
281
282
283 /*! \fn void debugaz(const char *head, const char *msg)
284 Write a debug message to stderr with the value of a parameter. The message is prefixed by "SARG: (util)".
285
286 \param head The name of the parameter.
287 \param msg The value of the parameter.
288 */
289
290
291
292
293
294 /*! \fn char *fixnum(long long int value, int n)
295 Rewrite a number to make it more readable. The number may be written
296 with the suffix K, M, G or T depending on its magnitude or the digits
297 are grouped by three and separated by a dot or a comma.
298
299 \param value The number to format.
300 \param n If the number is abreviated and this parameter is true then append
301 the suffix K, M, G or T if necessary. If it is zero, the number is shortened
302 but no suffix is written.
303
304 \return A static buffer containing the formatted number. It is overwritten on the next
305 call of this function.
306
307 */
308
309
310
311
312
313 /*! \def MAXLEN_FIXNUM
314 The size of the buffer to format a number in fixnum().
315 */
316
317
318
319
320
321 /*! \fn char *fixnum2(long long int value, int n)
322 Format a number by grouping the digits by three and separating the groups by
323 a dot or a comma.
324 */
325
326
327
328
329 /*! \def MAXLEN_FIXNUM2
330 The size of the buffer to format a number in fixnum2().
331 */
332
333
334
335
336
337 /*! \fn void buildhref(char * href)
338 Replace the path given as argument by the first part of a HTML tag to link to the given
339 directory (the A tag). More precisely, the argument is replaced by <a href=" followed by the given \a href.
340
341 \param href The directory to replace by a HTML A tag with the open HREF to it.
342
343 */
344
345
346
347
348
349 /*! \fn char *buildtime(long long int elap)
350 Write the elapsed time given in milliseconds as a string in the format HH:MM:SS.
351
352 \param elap The elapsed time in milliseconds.
353
354 \return A static buffer with the formatted time. It is valid until the function is called again.
355 */
356
357
358
359
360
361 /*! \fn void obtdate(const char *dirname, const char *name, char *data)
362 Get the date stored in the <tt>sarg-date</tt> file of a directory with the connection data.
363
364 \param dirname The directory to look for the connection directory.
365 \param name The name of the directory whose <tt>sarg-date</tt> file must be read.
366 \param data The buffer to store the content of the file. It must be more than 80
367 bytes long.
368 */
369
370
371
372
373
374 /*! \fn void formatdate(char *date,int date_size,int year,int month,int day,int hour,int minute,int second,int dst)
375 Format a date to display it in the report.
376
377 \param date The buffer to write the formatted date into.
378 \param date_size The size of the buffer.
379 \param year The absolute year to format. It must be greater than 1900.
380 \param month The month to format. It must be between 1 and 12.
381 \param day The day to format starting from 1.
382 \param hour The hour to format.
383 \param minute The minute to format.
384 \param second The second to format.
385 \param dst A positive number if the daylight saving is active, zero if it is not active and a negative number if it is unknown.
386 */
387
388
389
390
391
392 /*! \fn void computedate(int year,int month,int day,struct tm *t);
393 Fill a tm structure with the data of the date.
394
395 \param year The full year with century.
396 \param month The number of the month starting from one.
397 \param day The day of the date.
398 \param t The buffer to fill with the date.
399 */
400
401
402
403
404
405 /*! \fn int obtuser(const char *dirname, const char *name)
406 Get the number of entries stored in a report data directory. The number is read from
407 the <tt>sarg-users</tt> file of the report data's directory.
408
409 \param dirname The directory containing the reports.
410 \param name The name of the report directory whose <tt>sarg-users</tt> file must be read.
411
412 \return The number of entries in the report or zero if the file doesn't exists.
413 */
414
415
416
417
418
419 /*! \fn void obttotal(const char *dirname, const char *name, char *tbytes, int nuser, char *media)
420 Count the total size transfered in a report directory and compute the average number of bytes
421 per entry.
422
423 \param dirname The directory containing the reports.
424 \param name The name of the report directory whose <tt>sarg-general</tt> file must be read.
425 \param tbytes A buffer to store the total number of bytes from this report.
426 \param nuser The number of entries in the report directory.
427 \param media A buffer to store the average number of bytes per entry.
428 */
429
430
431
432
433 /*! \fn int getperiod_buildtext(struct periodstruct *period)
434 Build the text to display as the date range of the report.
435
436 \param period The object whose text must be contructed.
437
438 \retval 0 No error.
439 \retval -1 Resulting text too long for buffer.
440 */
441
442
443
444
445
446 /*! \fn static void copy_images(void)
447 Copy the images (in fact all the files) from the directory ::IMAGEDIR into the output directory
448 whose name is in ::outdir.
449 */
450
451
452
453
454
455 /*! \fn void vrfydir(const char *per1, const char *addr, const char *site, const char *us, const char *form)
456 Create a directory to generate a report for the specified connection data and populate it with the a <tt>sarg-date</tt> file
457 containing the current date.
458
459 The function also create an <tt>images</tt> directory in \a dir and copy all the files from the <tt>SYSCONFDIR/images</tt> into
460 that directory.
461
462 \param per1 The date range in the form: YYYYMMMDD-YYYYMMMDD or DDMMMYYYY-DDMMMYYYY depending on the value of
463 ::DateFormat.
464 \param addr The ip address or host name to which the report is limited. If the string is empty, all the addresses are accepted.
465 \param site The destination site to which the report is limited. If the string is empty, all the sites are accepted.
466 \param us The user to whom the report is limited. It is an empty string if all the users are accepted.
467 \param form The email address to which the report is sent. It is currently unused.
468
469 */
470
471
472
473
474
475 /*! \fn void strip_latin(char *line)
476 Remove any HTML entity from the line. A HTML entity starts with an ampersand and end at the next
477 semicolon.
478
479 \param line The text whose html entities are to be removed.
480 */
481
482
483
484
485
486 /*! \fn void zdate(char *ftime,int ftimesize, const char *DateFormat)
487 Format the current date and time according to the date format.
488
489 \param ftime The buffer to format the date.
490 \param ftimesize The size of the buffer to store the date
491 \param DateFormat The format of the date. It can be:
492 \arg u to format as mmm/dd/YYYY HH:MM
493 \arg e to format as dd/mmm/YYYY HH:MM
494 \arg w to format as WW-HH-MM where WW is the week number in the range 00 to 53.
495 */
496
497
498
499
500
501 /*! \fn char *fixtime(long int elap)
502 Format a "time" into a size or a time formatted as HH:MM:SS.
503
504 \param elap The "time" to format in milliseconds if it is a time and into bytes if it is a size.
505
506 \return The formatted time.
507
508 \bug If the elapsed time is less than 1000ms, the time is formated with the milliseconds as the seconds.
509
510 \todo Review this function and documentation based on the calls made to it and the arguments passed by the callers.
511 */
512
513
514
515
516
517 /*! \fn void date_from(char *date, char *dfrom, char *duntil)
518 Split a date range into a date from and a date until. If the date range
519 is not a range but just a single date, it is duplicated to make a range out
520 of it.
521
522 \param date The date range to split in the form <tt>from-until</tt>. If it is a single date,
523 it is transformed into a range like <tt>date-date</tt>. Each date is in the form DD/MM/YYYY.
524 \param dfrom A buffer to write the start date in the form YYYYMMDD.
525 \param duntil A buffer to write the end date in the form YYYYMMDD.
526 */
527
528
529
530
531
532 /*! \fn char *strlow(char *string)
533 Convert a string to all lowercases.
534
535 \param string The string to convert.
536
537 \return A pointer to the string passed as argument.
538 */
539
540
541
542
543
544 /*! \fn char *strup(char *string)
545 Convert a string to all uppercases.
546
547 \param string The string to convert.
548
549 \return A pointer to the string passed as argument.
550 */
551
552
553
554
555
556 /*! \fn void removetmp(const char *outdir)
557 Purge the file <tt>sarg-general</tt> from all the lines but the total.
558
559 \param outdir The output directory to purge.
560 */
561
562
563
564
565
566 /*! \fn void load_excludecodes(const char *ExcludeCodes)
567 Load the list of the HTTP codes to exclude from the report. There must be one code per line.
568 Any trailing space is removed and there is no provision for comments.
569
570 \param ExcludeCodes The name of the file to load.
571
572 This function allocate the memory to store the codes and it must be freed by a call to
573 free_excludecodes().
574 */
575
576
577
578
579
580 /*! \fn void free_excludecodes(void)
581 Free the memory allocated by load_excludecodes().
582 */
583
584
585
586
587 /*! \fn int vercode(const char *code)
588 Check if the code is contained in the exclusion list loaded by load_excludecodes().
589
590 \param code The HTTP code to test.
591
592 \retval 1 The code is excluded.
593 \retval 0 The code is not excluded.
594 */
595
596
597
598
599
600 /*! \fn void fixnone(char *str)
601 Find if the string is the word none and clear the string if it matches. The function
602 tolerates the trailing spaces and tabulations.
603
604 \param str The text to test for the word "none".
605 */
606
607
608
609
610
611 /*! \fn void fixendofline(char *str)
612 Remove the control codes and spaces at the end of the line. That is, it remove any ASCII
613 code less than or equal to 0x20.
614
615 \param str The string to truncate.
616 */
617
618
619
620
621 /*! \fn int testvaliduserchar(const char *user)
622 Tell if the user string contains any invalid character in a user name. The list
623 of the invalid characters is defined by ::UserInvalidChar.
624
625 \param user The user name to test.
626
627 \retval 1 The string contains at least one invalid character.
628 \retval 0 The string is valid.
629 */
630
631
632
633
634
635 /*! \fn int compar( const void *a, const void *b )
636 Compare two integers for bsearch.
637
638 \param a A pointer to the first integer.
639 \param b A pointer to the second integer.
640
641 \retval 1 If a > b.
642 \retval 0 If a == b.
643 \retval -1 If a < b.
644 */
645
646
647
648
649
650 /*! \fn int getnumlist( char *buf, numlist *list, const int len, const int maxvalue )
651 Get a comma separated list of numbers and split them into separate values taking into account
652 that no value may be greater than a maximum. If a value is in fact a range, it is expended.
653
654 Any duplicate value is removed.
655
656 \param buf The string with the list of numbers.
657 \param list Where to store the numbers.
658 \param len The size of the list.
659 \param maxvalue The maximum value allowed in the list.
660
661 \retval 0 No error.
662 \retval -1 Error detected.
663 */
664
665
666
667
668
669 /*! \fn void show_info(FILE *fp_ou)
670 Write the HTML formatted message to indicate the version of sarg that produced
671 the report and when the report was generated.
672
673 \param fp_ou The HTML file to which the identification block must be appended.
674 */
675
676
677
678
679
680 /*! \fn void show_sarg(FILE *fp_ou, int depth)
681 Write the header of the report to tell that it was generated by sarg.
682
683 \param fp_ou The handle of the HTML file.
684 \param depth How deep is the page in the directory tree. It is used to prepend the images directory name
685 with as many .. as necessary. If the page is at the same level as the image directory, the depth is zero.
686 */
687
688
689
690
691
692 /*! \fn char *get_size(const char *path, const char *file)
693 Get the size, in human readable form and kibibytes, of the content of a directory.
694
695 \param path The path containing the directory to scan.
696 \param file The last part of the path to the directory to scan.
697
698 \return The size of the path.
699 */
700
701
702
703
704
705 /*! \fn void write_html_header(FILE *fp_ou, int depth, const char *title)
706 Write the HTML header of a HTML report file. The DTD correspond to a
707 transitional HTML version 4.01. The title of the document is taken from
708 the global variable ::Title.
709
710 \param fp_ou The file to which the HTML header is written.
711 \param depth How deep is the page in the directory tree. The depth is passed to show_sarg().
712 \param title The title of the page.
713 */
714
715
716
717
718
719 /*! \fn void close_html_header(FILE *fp_ou)
720 Close the header opened by write_html_header().
721
722 \param fp_ou The file to which the HTML header is written.
723 */
724
725
726
727
728
729 /*! \fn void baddata(void)
730 Display an error message telling that sarg suspects an attempt to execute arbitrary code and terminate sarg.
731
732 Any temporary file created by sarg is deleted.
733 */
734
735
736
737
738
739 /*! \fn void url_hostname(const char *url,char *hostname,int hostsize)
740 Extract the host name from the URL.
741
742 \param url The url whose host name must be extracted.
743 \param hostname The buffer to store the host name.
744 \param hostsize The size of the host name buffer.
745
746 \note The function is stupid at this time. It just searches for the first slash
747 in the URL and truncates the URL there. It doesn't take the protocol into account
748 nor the port number nor any user or password information.
749 */
750
751
752
753
754
755 /*! \fn void url_module(const char *url, char *w2)
756 Copy at most 254 bytes from the end of the URL or stops at the first /.
757
758 \param url The URL to parse.
759 \param w2 A buffer to store the copied portion of the URL. The buffer must
760 be at least 255 characters long.
761 */
762
763
764
765
766
767 /*! \fn void url_to_file(const char *url,char *file,int filesize)
768 Mangle an URL to produce a part that can be included in a file.
769
770 \param url The URL to mangle.
771 \param file The buffer to write the mangled URL.
772 \param filesize The size of the buffer.
773 */
774
775
776
777
778
779 /*! \fn int write_html_trailer(FILE *fp_ou)
780 End the HTML file by closing the centered table that was opened by write_html_header(), writting
781 the informations of show_info() and closing the body and html tag. After this function returns, the
782 HTML file is complete and nothing should be written to it.
783
784 \param fp_ou The HTML file to close. The file handle is not closed but you should not write anything
785 to the file after this function returns.
786
787 \retval 0 No error.
788 \retval -1 Write error.
789 */
790
791
792
793
794
795 /*! \fn void version(void)
796 Display the current version of sarg and terminate the program.
797 */
798
799
800
801
802
803 /*! \fn char *get_param_value(const char *param,char *line)
804 Get the value of a parameter formatted in the string as "param value"
805 without the quotes.
806
807 If the parameter name matches \a param, then the value following the parameter
808 is returned. If it doesn't match, the function return NULL.
809
810 The function is suitable to parse configuration files because it will ignore
811 comments (anything but spaces and tabulations put before the parameter will make
812 it unrecognized by this function)
813
814 \param param The parameter name that must be found at the beginning of the line
815 with possible spaces or tabulations before.
816 \param line The text line to search for the parameter and it's value.
817
818 \return The beginning of the value after the equal sign and with the possible
819 spaces or tabulations removed. If the line doesn't start with the parameter name,
820 the function returns NULL.
821
822 */
823
824
825
826
827
828 /*! \fn void write_logo_image(FILE *fp_ou)
829 Write a link of the logo of the organisation that generate the report in the HTML file. The logo
830 is written in a centered table.
831
832 \param fp_ou The handle of the HTML file being written.
833 */
834
835
836
837
838
839 /*! \fn void output_html_string(FILE *fp_ou,const char *str,int maxlen)
840 Write a string in a file and replace the problematic ASCII characters by their equivalent HTML entities.
841
842 \param fp_ou The handle of the output file.
843 \param str The string to output.
844 \param maxlen The maximum number of bytes to write from the string. Set to zero to have no limit.
845
846 If the string is longer than the requested length, only the requested number of bytes are output and
847 the string is truncated and ended by &hellip;.
848 */
849
850
851
852
853
854 /*! \fn void output_html_url(FILE *fp_ou,const char *url)
855 Write an URL into the file and replace any & by &amp;.
856
857 \param fp_ou The handle of the output file.
858 \param url The URL to output.
859 */
860
861
862
863
864
865 /*! \fn void unlinkdir(const char *dir,int contentonly)
866 Delete a directory and its content.
867
868 \param dir The name of the directory to delete.
869 \param contentonly \c True to delete only the content of the directory and leave the directory
870 itself in place. If set to \c zero, the directory is removed too.
871 */
872