/*! \file util.c \brief Various useful functions. */ /*! \var static char mtab1[12][4]; The list of the months. */ /*! \fn void getword_start(struct getwordstruct *gwarea, char *line) Initialize the getword buffer with the given text line. \param gwarea The getword buffer to initialize. \param line The text line to use in the getword buffer. */ /*! \fn void getword_restart(struct getwordstruct *gwarea) Restart the getword buffer from the beginning. \param gwarea The getword buffer to reset. */ /*! \fn int getword(char *word, int limit, struct getwordstruct *gwarea, char stop) Extract one "word" from the text line and remove it from the text line. The word's boundary is defined by the \a stop character. If multiple stop characters are found after the word, only the first one is removed. Therfore, passing the line buffer again to the function will remove the next word in a column like manner. \param word The buffer to store the extracted word. \param limit The size of the buffer. If the stop character isn't found before that limit is reached, the function displays an error message and returns an error code. \param gwarea The getword buffer initialized by getword_start(). \param stop The character indicating the end of the word. \retval 0 The word is extracted. \retval -1 The stop character was not found before the limit is reached. */ /*! \fn int getword_limit(char *word, int limit, struct getwordstruct *gwarea, char stop) Extract one word with a maximum size and skip any supernumerary bytes until the stop bytes is found. \param word The buffer to store the extracted word. \param limit The size of the buffer. \param gwarea The getword buffer initialized by getword_start(). \param stop The character indicating the end of the word. \retval 0 The word is extracted. */ /*! \fn int getword_multisep(char *word, int limit, struct getwordstruct *gwarea, char stop) Extract one "word" from the text line and remove it from the text line. The word's boundary is defined by the \a stop character. All the stop characters following the word are removed too. Therefore, passing the line buffer again to the function will remove words even if they are separated by multiple stop characters. \param word The buffer to store the extracted word. \param limit The size of the buffer. If the stop character isn't found before that limit is reached, the function displays an error message and returns an error code. \param gwarea The getword buffer initialized by getword_start(). \param stop The character indicating the end of the word. \retval 0 The word is extracted. \retval -1 The stop character was not found before the limit is reached. */ /*! \fn int getword_skip(int limit, struct getwordstruct *gwarea, char stop) Skip one "word" from the text line and remove it from the text line. The word's boundary is defined by the \a stop character. \param limit The maximum number of characters to skip. If the stop character isn't found before that limit is reached, the function displays an error message and returns an error code. \param gwarea The getword buffer initialized by getword_start(). \param stop The character indicating the end of the word. \retval 0 The word is skipped. \retval -1 The stop character was not found before the limit is reached. */ /*! \fn int getword_atoll(long long int *number, struct getwordstruct *gwarea, char stop) Extract one number from the text line. \param number Where the store the extracted number. \param gwarea The getword buffer initialized by getword_start(). \param stop The character indicating the end of the word. \retval 0 The number is extracted. \retval -1 The stop character was not found after the number. */ /*! \fn int getword_ptr(char *orig_line,char **word, struct getwordstruct *gwarea, char stop) Return a pointer to a null terminated string starting at the current position and ending and the stop character. \param orig_line The line that is being parsed. \param word A pointer to set to the beginning of the string. \param gwarea The getword buffer initialized by getword_start(). \param stop The character indicating the end of the word. \retval 0 The word is skipped. \retval -1 Invalid \a orig_line passed to the function. */ /*! \fn long long int my_atoll (const char *nptr) Convert a string into a long long. \param nptr The string containing the number to convert. \return The number found in the string or zero if no number was found. */ /*! \fn static int is_absolute(const char *path) Tell if the path is absolute. On Unix, a path is absolute if it starts with a /. On Windows, we also check if the path starts with "x:" where x can be any letter. \param path The path to check. \retval 1 The path is absolute. \retval 0 The path is relative. */ /*! \fn void my_mkdir(const char *name) Create the directory and all the non existing parent directories. \param name The absolute directory to create. */ /*! \fn void my_lltoa(unsigned long long int n, char *s, int ssize, int len) Format a long long into a string. \param n The number to format. \param s The buffer to write the number. \param ssize The size of the output buffer. \param len The minimum number of digits to format in the output. If the formatted number is less than this length, it is padded with zeros. */ /*! \fn int builddia(int day, int month, int year) Return a numerical value made of the date. \param day The day of the date. \param month The number of the month starting from 1. \param year The year. \return The date in an integer format computed as year*10000+month*100+day. */ /*! \fn void buildymd(const char *dia, const char *mes, const char *ano, char *wdata) Convert the date into a machine format YYYYMMDD. \param dia The day. \param mes The name of the month as spelled in ::mtab1. If the month is invalid, the output date is set to month 13. \param ano The year. \param wdata The buffer to format the date. */ /*! \fn int conv_month(int char *month) Convert the month's name into its two digits numerical equivalent. \param month The name of the month as spelled in ::mtab1. \return The month number on starting from one. If the month name is not in ::mtab1, 13 is returned. */ /*! \fn const char *conv_month_name(int month) Convert a month number into a name. \param month The number of the month in the range 1 to 12. \return The name of the month from ::mtab1 unless the month number is not between 1 and 12 in which case, the number is returned encoded on 3 characters. If the number is invalid, the returned string is static and will be reused by any subsequent call to this function with an invalid month number. */ /*! \fn void name_month(char *month,int month_len) Get the name of the month according to the language file selected by the user. \param month The number of the month. It is replaced by the month's name if the number is between 1 and 12 or by the name of December if the number is invalid. \param month_len The size of the \a month buffer. */ /*! \fn char *fixnum(long long int value, int n) Rewrite a number to make it more readable. The number may be written with the suffix K, M, G or T depending on its magnitude or the digits are grouped by three and separated by a dot or a comma. \param value The number to format. \param n If the number is abreviated and this parameter is true then append the suffix K, M, G or T if necessary. If it is zero, the number is shortened but no suffix is written. \return A static buffer containing the formatted number. It is overwritten on the next call of this function. */ /*! \def MAXLEN_FIXNUM The size of the buffer to format a number in fixnum(). */ /*! \fn char *fixnum2(long long int value, int n) Format a number by grouping the digits by three and separating the groups by a dot or a comma. */ /*! \def MAXLEN_FIXNUM2 The size of the buffer to format a number in fixnum2(). */ /*! \fn void buildhref(char * href) Replace the path given as argument by the first part of a HTML tag to link to the given directory (the A tag). More precisely, the argument is replaced by from-until. If it is a single date, it is transformed into a range like date-date. Each date is in the form DD/MM/YYYY. \param dfrom A variable to write the start date in the form YYYY*10000+MM*100+DD. \param duntil A variable to write the end date in the form YYYY*10000+MM*100+DD. */ /*! \fn char *strlow(char *string) Convert a string to all lowercases. \param string The string to convert. \return A pointer to the string passed as argument. */ /*! \fn char *strup(char *string) Convert a string to all uppercases. \param string The string to convert. \return A pointer to the string passed as argument. */ /*! \fn void removetmp(const char *outdir) Purge the file sarg-general from all the lines but the total. \param outdir The output directory to purge. */ /*! \fn void load_excludecodes(const char *ExcludeCodes) Load the list of the HTTP codes to exclude from the report. There must be one code per line. Any trailing space is removed and there is no provision for comments. \param ExcludeCodes The name of the file to load. This function allocate the memory to store the codes and it must be freed by a call to free_excludecodes(). */ /*! \fn void free_excludecodes(void) Free the memory allocated by load_excludecodes(). */ /*! \fn int vercode(const char *code) Check if the code is contained in the exclusion list loaded by load_excludecodes(). \param code The HTTP code to test. \retval 1 The code is excluded. \retval 0 The code is not excluded. */ /*! \fn void fixnone(char *str) Find if the string is the word none and clear the string if it matches. The function tolerates the trailing spaces and tabulations. \param str The text to test for the word "none". */ /*! \fn void fixendofline(char *str) Remove the control codes and spaces at the end of the line. That is, it remove any ASCII code less than or equal to 0x20. \param str The string to truncate. */ /*! \fn int testvaliduserchar(const char *user) Tell if the user string contains any invalid character in a user name. The list of the invalid characters is defined by ::UserInvalidChar. \param user The user name to test. \retval 1 The string contains at least one invalid character. \retval 0 The string is valid. */ /*! \fn int compar( const void *a, const void *b ) Compare two integers for bsearch. \param a A pointer to the first integer. \param b A pointer to the second integer. \retval 1 If a > b. \retval 0 If a == b. \retval -1 If a < b. */ /*! \fn int getnumlist( char *buf, numlist *list, const int len, const int maxvalue ) Get a comma separated list of numbers and split them into separate values taking into account that no value may be greater than a maximum. If a value is in fact a range, it is expended. Any duplicate value is removed. \param buf The string with the list of numbers. \param list Where to store the numbers. \param len The size of the list. \param maxvalue The maximum value allowed in the list. \retval 0 No error. \retval -1 Error detected. */ /*! \fn void show_info(FILE *fp_ou) Write the HTML formatted message to indicate the version of sarg that produced the report and when the report was generated. \param fp_ou The HTML file to which the identification block must be appended. */ /*! \fn void show_sarg(FILE *fp_ou, int depth) Write the header of the report to tell that it was generated by sarg. \param fp_ou The handle of the HTML file. \param depth How deep is the page in the directory tree. It is used to prepend the images directory name with as many .. as necessary. If the page is at the same level as the image directory, the depth is zero. */ /*! \fn char *get_size(const char *path, const char *file) Get the size, in human readable form and kibibytes, of the content of a directory. \param path The path containing the directory to scan. \param file The last part of the path to the directory to scan. \return The size of the path. */ /*! \fn void write_html_head(FILE *fp_ou,int depth, const char *page_title,int javascript) Write the header of the HTML document. The DTD corresponds to a transitional HTML version 4.01. The title of the document is taken from the global variable ::Title. \param fp_ou The file to which the HTML header is written. \param depth How deep is the page in the directory tree. The path of the relative javascripts is adjusted accordingly. \param title The title of the page. \param javascript Which javascript to include in the page. Is a combination of the following bits: \arg HTML_JS_SORTTABLE */ /*! \fn void write_html_header(FILE *fp_ou, int depth, const char *title) Write the HTML header of a HTML report file including the sarg logo and the beginning of the header of the report. The header of the report must be closed by a call to close_html_header(). \param fp_ou The file to which the HTML header is written. \param depth How deep is the page in the directory tree. The depth is passed to show_sarg(). \param title The title of the page. \param javascript Which javascript to include in the page. Is a combination of bits. See \see write_html_header() for the possible values. */ /*! \fn void close_html_header(FILE *fp_ou) Close the header opened by write_html_header(). \param fp_ou The file to which the HTML header is written. */ /*! \fn void url_module(const char *url, char *w2) Copy at most 254 bytes from the end of the URL or stops at the first /. \param url The URL to parse. \param w2 A buffer to store the copied portion of the URL. The buffer must be at least 255 characters long. */ /*! \fn int write_html_trailer(FILE *fp_ou) End the HTML file by closing the centered table that was opened by write_html_header(), writting the informations of show_info() and closing the body and html tag. After this function returns, the HTML file is complete and nothing should be written to it. \param fp_ou The HTML file to close. The file handle is not closed but you should not write anything to the file after this function returns. \retval 0 No error. \retval -1 Write error. */ /*! \fn void version(void) Display the current version of sarg and terminate the program. */ /*! \fn char *get_param_value(const char *param,char *line) Get the value of a parameter formatted in the string as "param value" without the quotes. If the parameter name matches \a param, then the value following the parameter is returned. If it doesn't match, the function return NULL. The function is suitable to parse configuration files because it will ignore comments (anything but spaces and tabulations put before the parameter will make it unrecognized by this function) \param param The parameter name that must be found at the beginning of the line with possible spaces or tabulations before. \param line The text line to search for the parameter and it's value. \return The beginning of the value after the equal sign and with the possible spaces or tabulations removed. If the line doesn't start with the parameter name, the function returns NULL. */ /*! \fn void write_logo_image(FILE *fp_ou) Write a link of the logo of the organisation that generate the report in the HTML file. The logo is written in a centered table. \param fp_ou The handle of the HTML file being written. */ /*! \fn void output_html_string(FILE *fp_ou,const char *str,int maxlen) Write a string in a file and replace the problematic ASCII characters by their equivalent HTML entities. \param fp_ou The handle of the output file. \param str The string to output. \param maxlen The maximum number of bytes to write from the string. Set to zero to have no limit. If the string is longer than the requested length, only the requested number of bytes are output and the string is truncated and ended by …. */ /*! \fn void output_html_url(FILE *fp_ou,const char *url) Write an URL into the file and replace any & by &. \param fp_ou The handle of the output file. \param url The URL to output. */ /*! \fn void unlinkdir(const char *dir,int contentonly) Delete a directory and its content. \param dir The name of the directory to delete. \param contentonly \c True to delete only the content of the directory and leave the directory itself in place. If set to \c zero, the directory is removed too. */