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