]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - util.c
Add support to decompress xz files
[thirdparty/sarg.git] / util.c
diff --git a/util.c b/util.c
index 9955e62271f3bf987c8f89c8b6afb3e418980499..aba23b191b769db4af96cb708361bda67c3d6bdb 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2013
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
@@ -46,6 +46,9 @@ static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
 //! The list of the HTTP codes to exclude from the report.
 static char *excludecode=NULL;
 
+//! Directory where the images are stored.
+char ImageDir[MAXLEN]=IMAGEDIR;
+
 extern char *CurrentLocale;
 
 #if USE_GETWORD_BACKTRACE
@@ -59,7 +62,7 @@ static void getword_backtrace(void)
        if (n<=0) return;
        calls=backtrace_symbols(buffer,n);
        if (calls) {
-               debuga(_("getword backtrace:\n"));
+               debuga(__FILE__,__LINE__,_("getword backtrace:\n"));
                for (i=0 ; i<n ; i++) {
                        fprintf(stderr,"SARG: %d:%s\n",i+1,calls[i]);
                }
@@ -78,7 +81,7 @@ void getword_start(struct getwordstruct *gwarea, const char *line)
 void getword_restart(struct getwordstruct *gwarea)
 {
        if (gwarea->modified) {
-               debuga(_("Cannot parse again the line as it was modified\n"));
+               debuga(__FILE__,__LINE__,_("Cannot parse again the line as it was modified\n"));
                exit(EXIT_FAILURE);
        }
        gwarea->current=gwarea->beginning;
@@ -90,11 +93,14 @@ int getword(char *word, int limit, struct getwordstruct *gwarea, char stop)
 
        for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) {
                if(x>=limit) {
-                       debuga(_("End of word not found in getword after %d bytes.\n"),x);
-                       debuga(_("Line=\"%s\"\n"),gwarea->beginning);
-                       debuga(_("Record=\"%s\"\n"),gwarea->current);
-                       debuga(_("searching for \'x%x\'\n"),stop);
-                       //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
+                       /*
+                        TRANSLATORS: The %s is the name of the function reporting the
+                        error message.
+                        */
+                       debuga(__FILE__,__LINE__,_("End of word not found in %s after %d bytes.\n"),__func__,x);
+                       debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+                       debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+                       debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
                        word[(limit>0) ? limit-1 : 0]='\0';
 #if USE_GETWORD_BACKTRACE
                        getword_backtrace();
@@ -131,11 +137,10 @@ int getword_multisep(char *word, int limit, struct getwordstruct *gwarea, char s
 
        for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) {
                if(x>=limit) {
-                       debuga(_("End of word not found in getword_multisep after %d bytes.\n"),x);
-                       debuga(_("Line=\"%s\"\n"),gwarea->beginning);
-                       debuga(_("Record=\"%s\"\n"),gwarea->current);
-                       debuga(_("searching for \'x%x\'\n"),stop);
-                       //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
+                       debuga(__FILE__,__LINE__,_("End of word not found in %s after %d bytes.\n"),__func__,x);
+                       debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+                       debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+                       debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
                        if (limit>0) word[limit-1]='\0';
 #if USE_GETWORD_BACKTRACE
                        getword_backtrace();
@@ -158,11 +163,10 @@ int getword_skip(int limit, struct getwordstruct *gwarea, char stop)
 
        for(x=0;(gwarea->current[x] && (gwarea->current[x] != stop ));x++) {
                if(x>=limit) {
-                       debuga(_("End of word not found in getword_skip after %d bytes.\n"),x);
-                       debuga(_("Line=\"%s\"\n"),gwarea->beginning);
-                       debuga(_("Record=\"%s\"\n"),gwarea->current);
-                       debuga(_("searching for \'x%x\'\n"),stop);
-                       //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
+                       debuga(__FILE__,__LINE__,_("End of word not found in %s after %d bytes.\n"),__func__,x);
+                       debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+                       debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+                       debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
 #if USE_GETWORD_BACKTRACE
                        getword_backtrace();
 #endif
@@ -191,17 +195,23 @@ int getword_atoll(long long int *number, struct getwordstruct *gwarea, char stop
        for(x=0;isdigit(gwarea->current[x]);x++) {
                digit=gwarea->current[x]-'0';
                if (*number >= (LLONG_MAX-digit)/10) {
-                       debuga(_("Integer overflow detected in getword_atoll in line %s\n"),gwarea->beginning);
+                       /*
+                        TRANSLATORS: The first %s is the function name (in the source code) where the
+                        overflow is detected.
+                       */
+                       debuga(__FILE__,__LINE__,_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
                        return(-1);
                }
                *number=(*number * 10) + digit;
        }
        if(gwarea->current[x] && gwarea->current[x]!=stop) {
-               debuga(_("End of number not found in getword_atoll after %d bytes.\n"),x);
-               debuga(_("Line=\"%s\"\n"),gwarea->beginning);
-               debuga(_("Record=\"%s\"\n"),gwarea->current);
-               debuga(_("searching for \'x%x\'\n"),stop);
-               //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
+               /*
+                TRANSLATORS: The %s is the function name, in the source code, where the problem occured.
+               */
+               debuga(__FILE__,__LINE__,_("End of number not found in %s after %d bytes.\n"),__func__,x);
+               debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+               debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+               debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
 #if USE_GETWORD_BACKTRACE
                getword_backtrace();
 #endif
@@ -230,17 +240,16 @@ int getword_atoi(int *number, struct getwordstruct *gwarea, char stop)
        for(x=0;isdigit(gwarea->current[x]);x++) {
                digit=gwarea->current[x]-'0';
                if (*number > (INT_MAX-digit)/10) {
-                       debuga(_("Integer overflow detected in getword_atoi in line %s\n"),gwarea->beginning);
+                       debuga(__FILE__,__LINE__,_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
                        return(-1);
                }
                *number=(*number * 10) + digit;
        }
        if(gwarea->current[x] && gwarea->current[x]!=stop) {
-               debuga(_("End of number not found in getword_atoi after %d bytes.\n"),x);
-               debuga(_("Line=\"%s\"\n"),gwarea->beginning);
-               debuga(_("Record=\"%s\"\n"),gwarea->current);
-               debuga(_("searching for \'x%x\'\n"),stop);
-               //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
+               debuga(__FILE__,__LINE__,_("End of number not found in %s after %d bytes.\n"),__func__,x);
+               debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+               debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+               debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
 #if USE_GETWORD_BACKTRACE
                getword_backtrace();
 #endif
@@ -255,7 +264,7 @@ int getword_atoi(int *number, struct getwordstruct *gwarea, char stop)
 
 int getword_atol(long int *number, struct getwordstruct *gwarea, char stop)
 {
-       long int x;
+       int x;
        long int sign=+1;
        int digit;
 
@@ -269,17 +278,16 @@ int getword_atol(long int *number, struct getwordstruct *gwarea, char stop)
        for(x=0;isdigit(gwarea->current[x]);x++) {
                digit=gwarea->current[x]-'0';
                if (*number > (LONG_MAX-digit)/10) {
-                       debuga(_("Integer overflow detected in getword_atol in line %s\n"),gwarea->beginning);
+                       debuga(__FILE__,__LINE__,_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
                        return(-1);
                }
                *number=(*number * 10) + digit;
        }
        if(gwarea->current[x] && gwarea->current[x]!=stop) {
-               debuga(_("End of number not found in getword_atol after %ld bytes.\n"),x);
-               debuga(_("Line=\"%s\"\n"),gwarea->beginning);
-               debuga(_("Record=\"%s\"\n"),gwarea->current);
-               debuga(_("searching for \'x%x\'\n"),stop);
-               //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
+               debuga(__FILE__,__LINE__,_("End of number not found in %s after %d bytes.\n"),__func__,x);
+               debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+               debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+               debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
 #if USE_GETWORD_BACKTRACE
                getword_backtrace();
 #endif
@@ -294,13 +302,13 @@ int getword_atol(long int *number, struct getwordstruct *gwarea, char stop)
 
 int getword_atolu(unsigned long int *number, struct getwordstruct *gwarea, char stop)
 {
-       unsigned long int x;
+       int x;
        int digit;
 
        if (gwarea->current[0] == '-') {
-               debuga(_("getword_atolu got a negative number.\n"));
-               debuga(_("Line=\"%s\"\n"),gwarea->beginning);
-               debuga(_("Record=\"%s\"\n"),gwarea->current);
+               debuga(__FILE__,__LINE__,_("getword_atolu got a negative number.\n"));
+               debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+               debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
                return(-1);
        }
        if (gwarea->current[0] == '+') {
@@ -310,17 +318,16 @@ int getword_atolu(unsigned long int *number, struct getwordstruct *gwarea, char
        for(x=0;isdigit(gwarea->current[x]);x++) {
                digit=gwarea->current[x]-'0';
                if (*number > (ULONG_MAX-digit)/10) {
-                       debuga(_("Integer overflow detected in getword_atolu in line %s\n"),gwarea->beginning);
+                       debuga(__FILE__,__LINE__,_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
                        return(-1);
                }
                *number=(*number * 10) + digit;
        }
        if(gwarea->current[x] && gwarea->current[x]!=stop) {
-               debuga(_("End of number not found in getword_atolu after %ld bytes.\n"),x);
-               debuga(_("Line=\"%s\"\n"),gwarea->beginning);
-               debuga(_("Record=\"%s\"\n"),gwarea->current);
-               debuga(_("searching for \'x%x\'\n"),stop);
-               //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
+               debuga(__FILE__,__LINE__,_("End of number not found in %s after %d bytes.\n"),__func__,x);
+               debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+               debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+               debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
 #if USE_GETWORD_BACKTRACE
                getword_backtrace();
 #endif
@@ -346,7 +353,7 @@ int getword_ptr(char *orig_line,char **word, struct getwordstruct *gwarea, char
        int start;
 
        if (orig_line && orig_line!=gwarea->beginning) {
-               debuga(_("Invalid buffer passed to getword_ptr\n"));
+               debuga(__FILE__,__LINE__,_("Invalid buffer passed to getword_ptr\n"));
                return(-1);
        }
 
@@ -411,21 +418,22 @@ void my_mkdir(const char *name)
        int chars;
 
        if(!is_absolute(name)) {
-               debuga(_("Invalid path (%s). Please, use absolute paths only.\n"),name);
+               debuga(__FILE__,__LINE__,_("Invalid path (%s). Please, use absolute paths only.\n"),name);
                exit(EXIT_FAILURE);
        }
 
        chars=0;
        for (i=0 ; name[i] ; i++) {
                if (i>=sizeof(w0)) {
-                       debuga(_("directory name too long: %s\n"),name);
+                       debuga(__FILE__,__LINE__,_("Path too long: "));
+                       debuga_more("%s\n",name);
                        exit(EXIT_FAILURE);
                }
                if (chars>0 && name[i] == '/') {
                        w0[i] = '\0';
                        if (access(w0, R_OK) != 0) {
                                if (PortableMkDir(w0,0755)) {
-                                       debuga(_("Cannot create directory %s - %s\n"),w0,strerror(errno));
+                                       debuga(__FILE__,__LINE__,_("Cannot create directory \"%s\": %s\n"),w0,strerror(errno));
                                        exit(EXIT_FAILURE);
                                }
                        }
@@ -436,7 +444,7 @@ void my_mkdir(const char *name)
 
        if (access(name, R_OK) != 0) {
                if (PortableMkDir(name,0755)) {
-                       debuga(_("Cannot create directory %s - %s\n"),name,strerror(errno));
+                       debuga(__FILE__,__LINE__,_("Cannot create directory \"%s\": %s\n"),name,strerror(errno));
                        exit(EXIT_FAILURE);
                }
        }
@@ -452,7 +460,7 @@ void my_lltoa(unsigned long long int n, char *s, int ssize, int len)
 
        ssize--;
        if (len>ssize) {
-               debuga(_("The requested number of digits passed to my_lltoa (%d) is bigger than the output buffer size (%d)\n"),len,ssize);
+               debuga(__FILE__,__LINE__,_("The requested number of digits passed to my_lltoa (%d) is bigger than the output buffer size (%d)\n"),len,ssize);
                abort();
        }
 
@@ -499,7 +507,7 @@ Compare two dates.
 \retval 0 If date1==date2.
 \retval 1 if date1>date2.
 */
-int compare_date(struct tm *date1,struct tm *date2)
+int compare_date(const struct tm *date1,const struct tm *date2)
 {
        if (date1->tm_year<date2->tm_year) return(-1);
        if (date1->tm_year>date2->tm_year) return(1);
@@ -551,16 +559,44 @@ Write a debug message to stderr. The message is prefixed by "SARG:" to identify
 \param msg The printf like message to format.
 \param ... The arguments to format in the message.
 */
-void debuga(const char *msg,...)
+void debuga(const char *File,int Line,const char *msg,...)
 {
        va_list ap;
 
-       fputs(_("SARG: "),stderr);
+       if (debugz>=LogLevel_Source) {
+               /* The path is removed because every source file is in the same directory.
+                * There is no point in reporting the full path from the build directory.
+                */
+               const char *ptr=strrchr(File,'/');
+               if (!ptr) ptr=File;
+               /* TRANSLATORS: This is the prefix to stderr messages when the debug level is
+                set to display the source file (%s) and the line number (%d). */
+               fprintf(stderr,_("SARG(%s:%d): "),ptr,Line);
+       } else {
+               /* TRANSLATORS: This is the prefix to stderr messages when the debug level
+                is low. */
+               fputs(_("SARG: "),stderr);
+       }
        va_start(ap,msg);
        vfprintf(stderr,msg,ap);
        va_end(ap);
 }
 
+/*!
+Write a debug message to stderr. The message is supposed
+to be displayed after a message from debuga().
+
+\param msg The printf like message to format.
+\param ... The arguments to format in the message.
+*/
+void debuga_more(const char *msg,...)
+{
+       va_list ap;
+
+       va_start(ap,msg);
+       vfprintf(stderr,msg,ap);
+       va_end(ap);
+}
 
 /*!
 Write a debug message to stderr. The message is prefixed by "SARG: (info)".
@@ -568,11 +604,24 @@ Write a debug message to stderr. The message is prefixed by "SARG: (info)".
 \param msg The printf like message to format.
 \param ... The arguments to format in the message.
 */
-void debugaz(const char *msg,...)
+void debugaz(const char *File,int Line,const char *msg,...)
 {
        va_list ap;
 
-       fputs(_("SARG: (info) "),stderr);
+       if (debugz>=LogLevel_Source) {
+               /* The path is removed because every source file is in the same directory.
+                * There is no point in reporting the full path from the build directory.
+                */
+               const char *ptr=strrchr(File,'/');
+               if (!ptr) ptr=File;
+               /* TRANSLATORS: This is the prefix to information messages when the debug level is
+                set to display the source file (%s) and the line number (%d). */
+               fprintf(stderr,_("SARG(%s:%d): (info) "),ptr,Line);
+       } else {
+               /* TRANSLATORS: This is the prefix to information messages when the debug level
+                is low. */
+               fputs(_("SARG: (info) "),stderr);
+       }
        va_start(ap,msg);
        vfprintf(stderr,msg,ap);
        va_end(ap);
@@ -750,12 +799,14 @@ int obtdate(const char *dirname, const char *name, char *data)
        char wdir[MAXLEN];
 
        if (snprintf(wdir,sizeof(wdir),"%s%s/sarg-date",dirname,name)>=sizeof(wdir)) {
-               debuga(_("Buffer to small to store %s%s/sarg-date"),dirname,name);
+               debuga(__FILE__,__LINE__,_("Buffer too small to store "));
+               debuga_more("%s%s/sarg-date",dirname,name);
                exit(EXIT_FAILURE);
        }
        if ((fp_in = fopen(wdir, "rt")) == 0) {
                if (snprintf(wdir,sizeof(wdir),"%s%s/date",dirname,name)>=sizeof(wdir)) {
-                       debuga(_("Buffer to small to store %s%s/date"),dirname,name);
+                       debuga(__FILE__,__LINE__,_("Buffer too small to store "));
+                       debuga_more("%s%s/date",dirname,name);
                        exit(EXIT_FAILURE);
                }
                if ((fp_in = fopen(wdir, "rt")) == 0) {
@@ -765,10 +816,13 @@ int obtdate(const char *dirname, const char *name, char *data)
        }
 
        if (!fgets(data,80,fp_in)) {
-               debuga(_("Failed to read the date in %s\n"),wdir);
+               debuga(__FILE__,__LINE__,_("Failed to read the date in file \"%s\"\n"),wdir);
+               exit(EXIT_FAILURE);
+       }
+       if (fclose(fp_in)==EOF) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdir,strerror(errno));
                exit(EXIT_FAILURE);
        }
-       fclose(fp_in);
        fixendofline(data);
 
        return(0);
@@ -813,12 +867,14 @@ int obtuser(const char *dirname, const char *name)
        int nuser;
 
        if (snprintf(wdir,sizeof(wdir),"%s%s/sarg-users",dirname,name)>=sizeof(wdir)) {
-               debuga(_("Buffer too small to store %s%s/sarg-users"),dirname,name);
+               debuga(__FILE__,__LINE__,_("Buffer too small to store "));
+               debuga_more("%s%s/sarg-users",dirname,name);
                exit(EXIT_FAILURE);
        }
        if((fp_in=fopen(wdir,"r"))==NULL) {
                if (snprintf(wdir,sizeof(wdir),"%s%s/users",dirname,name)>=sizeof(wdir)) {
-                       debuga(_("Buffer too small to store %s%s/users"),dirname,name);
+                       debuga(__FILE__,__LINE__,_("Buffer too small to store "));
+                       debuga_more("%s%s/users",dirname,name);
                        exit(EXIT_FAILURE);
                }
                if((fp_in=fopen(wdir,"r"))==NULL) {
@@ -827,10 +883,13 @@ int obtuser(const char *dirname, const char *name)
        }
 
        if (!fgets(tuser,sizeof(tuser),fp_in)) {
-               debuga(_("Failed to read the number of users in %s\n"),wdir);
+               debuga(__FILE__,__LINE__,_("Failed to read the number of users in file \"%s\"\n"),wdir);
+               exit(EXIT_FAILURE);
+       }
+       if (fclose(fp_in)==EOF) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdir,strerror(errno));
                exit(EXIT_FAILURE);
        }
-       fclose(fp_in);
        nuser=atoi(tuser);
 
        return(nuser);
@@ -839,7 +898,7 @@ int obtuser(const char *dirname, const char *name)
 
 void obttotal(const char *dirname, const char *name, int nuser, long long int *tbytes, long long int *media)
 {
-       FILE *fp_in;
+       FileObject *fp_in;
        char *buf;
        char wdir[MAXLEN];
        char user[MAX_USER_LEN];
@@ -851,21 +910,23 @@ void obttotal(const char *dirname, const char *name, int nuser, long long int *t
        *media=0;
 
        if (snprintf(wdir,sizeof(wdir),"%s%s/sarg-general",dirname,name)>=sizeof(wdir)) {
-               debuga(_("Buffer too small to store %s%s/sarg-general"),dirname,name);
+               debuga(__FILE__,__LINE__,_("Buffer too small to store "));
+               debuga_more("%s%s/sarg-general",dirname,name);
                exit(EXIT_FAILURE);
        }
-       if ((fp_in = fopen(wdir, "r")) == 0) {
+       if ((fp_in = FileObject_Open(wdir)) == NULL) {
                if (snprintf(wdir,sizeof(wdir),"%s%s/general",dirname,name)>=sizeof(wdir)) {
-                       debuga(_("Buffer too small to store %s%s/general"),dirname,name);
+                       debuga(__FILE__,__LINE__,_("Buffer too small to store "));
+                       debuga_more("%s%s/general",dirname,name);
                        exit(EXIT_FAILURE);
                }
-               if ((fp_in = fopen(wdir, "r")) == 0) {
+               if ((fp_in = FileObject_Open(wdir)) == NULL) {
                        return;
                }
        }
 
        if ((line=longline_create())==NULL) {
-               debuga(_("Not enough memory to read the file %s\n"),wdir);
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),wdir);
                exit(EXIT_FAILURE);
        }
 
@@ -878,22 +939,25 @@ void obttotal(const char *dirname, const char *name, int nuser, long long int *t
                        continue;
                getword_start(&gwarea,buf);
                if (getword(user,sizeof(user),&gwarea,sep)<0) {
-                       debuga(_("There is a invalid user in file %s\n"),wdir);
+                       debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),wdir);
                        exit(EXIT_FAILURE);
                }
                if(strcmp(user,"TOTAL") != 0)
                        continue;
                if (getword_skip(MAXLEN,&gwarea,sep)<0) {
-                       debuga(_("There a broken total number of access in file %s\n"),wdir);
+                       debuga(__FILE__,__LINE__,_("Invalid total number of accesses in file \"%s\"\n"),wdir);
                        exit(EXIT_FAILURE);
                }
                if (getword_atoll(tbytes,&gwarea,sep)<0) {
-                       debuga(_("There is a broken number of bytes in file %s\n"),wdir);
+                       debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),wdir);
                        exit(EXIT_FAILURE);
                }
                break;
        }
-       fclose(fp_in);
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdir,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
+       }
        longline_destroy(&line);
 
        if (nuser <= 0)
@@ -924,7 +988,7 @@ int getperiod_fromsarglog(const char *arqtt,struct periodstruct *period)
                str+=2;
                year0=0;
                for (i=0 ; isdigit(str[i]) && i<4 ; i++) year0=year0*10+(str[i]-'0');
-               if (i!=4) continue;
+               if (i!=4 || year0<1900) continue;
                str+=4;
                if (str[0]!='_') continue;
                str++;
@@ -948,7 +1012,7 @@ int getperiod_fromsarglog(const char *arqtt,struct periodstruct *period)
                str+=2;
                year1=0;
                for (i=0 ; isdigit(str[i]) && i<4 ; i++) year1=year1*10+(str[i]-'0');
-               if (i!=4) continue;
+               if (i!=4 || year1<1900) continue;
                str+=4;
 
                if (str[0]!='_') continue;
@@ -976,6 +1040,13 @@ int getperiod_fromsarglog(const char *arqtt,struct periodstruct *period)
        return(-1);
 }
 
+/*!
+Fill the period with the specified range.
+
+\param period The period to change.
+\param dfrom The start date in the form year*10000+month*100+day.
+\param duntil The end date in the form year*10000+month*100+day.
+*/
 void getperiod_fromrange(struct periodstruct *period,int dfrom,int duntil)
 {
        memset(&period->start,0,sizeof(period->start));
@@ -989,6 +1060,21 @@ void getperiod_fromrange(struct periodstruct *period,int dfrom,int duntil)
        period->end.tm_year=(duntil/10000)-1900;
 }
 
+/*!
+Get the range from a period.
+
+\param period The period to convert to a range.
+\param dfrom The variable to store the range beginning. It can be NULL.
+\param duntil The variable to store the range end. It can be NULL.
+*/
+void getperiod_torange(const struct periodstruct *period,int *dfrom,int *duntil)
+{
+       if (dfrom)
+               *dfrom=(period->start.tm_year+1900)*10000+(period->start.tm_mon+1)*100+period->start.tm_mday;
+       if (duntil)
+               *duntil=(period->end.tm_year+1900)*10000+(period->end.tm_mon+1)*100+period->end.tm_mday;
+}
+
 /*!
 Update the \a main period to encompass the period in \a candidate.
 */
@@ -999,7 +1085,7 @@ void getperiod_merge(struct periodstruct *main,struct periodstruct *candidate)
 
        mdate=(main->start.tm_year)*10000+(main->start.tm_mon)*100+main->start.tm_mday;
        cdate=(candidate->start.tm_year)*10000+(candidate->start.tm_mon)*100+candidate->start.tm_mday;
-       if (cdate<mdate) memcpy(&main->start,&candidate->start,sizeof(struct tm));
+       if (mdate==0 || cdate<mdate) memcpy(&main->start,&candidate->start,sizeof(struct tm));
 
        mdate=(main->end.tm_year)*10000+(main->end.tm_mon)*100+main->end.tm_mday;
        cdate=(candidate->end.tm_year)*10000+(candidate->end.tm_mon)*100+candidate->end.tm_mday;
@@ -1050,7 +1136,6 @@ static void copy_images(void)
 {
        FILE *img_in, *img_ou;
        char images[512];
-       char imgdir[MAXLEN];
        char srcfile[MAXLEN];
        char dstfile[MAXLEN];
        DIR *dirp;
@@ -1060,36 +1145,37 @@ static void copy_images(void)
        struct stat info;
 
        if (snprintf(images,sizeof(images),"%simages",outdir)>=sizeof(images)) {
-               debuga(_("Cannot copy images to target directory %simages\n"),outdir);
+               debuga(__FILE__,__LINE__,_("Cannot copy images to target directory %simages\n"),outdir);
                exit(EXIT_FAILURE);
        }
        if (access(images,R_OK)!=0) {
                if (PortableMkDir(images,0755)) {
-                       debuga(_("Cannot create directory %s - %s\n"),images,strerror(errno));
+                       debuga(__FILE__,__LINE__,_("Cannot create directory \"%s\": %s\n"),images,strerror(errno));
                        exit(EXIT_FAILURE);
                }
        }
 
-       strcpy(imgdir,IMAGEDIR);
-       dirp = opendir(imgdir);
+       dirp = opendir(ImageDir);
        if(dirp==NULL) {
-               debuga(_("(util) Can't open directory %s: %s\n"),imgdir,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot open directory \"%s\": %s\n"),ImageDir,strerror(errno));
                return;
        }
        while ((direntp = readdir( dirp )) != NULL ){
                if(direntp->d_name[0]=='.')
                        continue;
-               if (snprintf(srcfile,sizeof(srcfile),"%s/%s",imgdir,direntp->d_name)>=sizeof(srcfile)) {
-                       debuga(_("Buffer too small to store %s/%s"),imgdir,direntp->d_name);
+               if (snprintf(srcfile,sizeof(srcfile),"%s/%s",ImageDir,direntp->d_name)>=sizeof(srcfile)) {
+                       debuga(__FILE__,__LINE__,_("Buffer too small to store "));
+                       debuga_more("%s/%s",ImageDir,direntp->d_name);
                        exit(EXIT_FAILURE);
                }
                if (stat(srcfile,&info)) {
-                       debuga(_("Cannot stat \"%s\" - %s\n"),srcfile,strerror(errno));
+                       debuga(__FILE__,__LINE__,_("Cannot stat \"%s\": %s\n"),srcfile,strerror(errno));
                        continue;
                }
                if (S_ISREG(info.st_mode)) {
                        if (snprintf(dstfile,sizeof(dstfile),"%s/%s",images,direntp->d_name)>=sizeof(dstfile)) {
-                               debuga(_("Buffer too small to store %s/%s"),images,direntp->d_name);
+                               debuga(__FILE__,__LINE__,_("Buffer too small to store "));
+                               debuga_more("%s/%s",images,direntp->d_name);
                                exit(EXIT_FAILURE);
                        }
                        img_in = fopen(srcfile, "rb");
@@ -1098,19 +1184,22 @@ static void copy_images(void)
                                if(img_ou!=NULL) {
                                        while ((nread = fread(buffer,1,sizeof(buffer),img_in))>0) {
                                                if (fwrite(buffer,1,nread,img_ou)!=nread) {
-                                                       debuga(_("Failed to copy image %s to %s\n"),srcfile,dstfile);
+                                                       debuga(__FILE__,__LINE__,_("Failed to copy image %s to %s\n"),srcfile,dstfile);
                                                        break;
                                                }
                                        }
                                        if (fclose(img_ou)==EOF) {
-                                               debuga(_("Error while copying image %s: %s\n"),dstfile,strerror(errno));
+                                               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),dstfile,strerror(errno));
                                                exit(EXIT_FAILURE);
                                        }
                                } else
-                                       fprintf(stderr,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open/create file", dstfile, strerror(errno));
-                               fclose(img_in);
+                                       debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"), dstfile, strerror(errno));
+                               if (fclose(img_in)==EOF) {
+                                       debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),srcfile,strerror(errno));
+                                       exit(EXIT_FAILURE);
+                               }
                        } else
-                               fprintf(stderr,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open file", srcfile, strerror(errno));
+                               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"), srcfile, strerror(errno));
                }
        }
        (void) closedir(dirp);
@@ -1280,12 +1369,24 @@ bool IsTreeDayFileName(const char *Name)
        return(true);
 }
 
-int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us, const char *form)
+/*!
+ * Create a directory to generate a report for the specified connection data
+ * and populate it with the a <tt>sarg-date</tt> file containing the current
+ * date.
+ *
+ * The function also create an <tt>images</tt> directory in \a dir and copy all
+ * the files from the <tt>SYSCONFDIR/images</tt> into that directory.
+ *
+ * \param per1 The date range in the form: YYYYMMMDD-YYYYMMMDD or DDMMMYYYY-DDMMMYYYY depending on the value of
+ * ::DateFormat.
+ * \param addr The ip address or host name to which the report is limited. If the string is empty, all the addresses are accepted.
+ * \param site The destination site to which the report is limited. If the string is empty, all the sites are accepted.
+ * \param us The user to whom the report is limited. It is an empty string if all the users are accepted.
+ */
+int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us)
 {
        FILE *fp_ou;
-       int  num=1, count=0;
        char wdir[MAXLEN];
-       char dirname2[MAXLEN];
        int y1, y2;
        int m1, m2;
        int d1, d2;
@@ -1346,64 +1447,35 @@ int vrfydir(const struct periodstruct *per1, const char *addr, const char *site,
 
        strcpy(outdirname,wdir);
 
-       if(IndexTree != INDEX_TREE_DATE) {
-               if(!OverwriteReport) {
-                       while(num) {
-                               if(access(wdir,R_OK) == 0) {
-                                       sprintf(wdir,"%s.%d",outdirname,num);
-                                       num++;
-                                       count++;
-                               } else
-                                       break;
-                       }
+       // manufacture a new unique name if configured to keep old reports or overwrite old report if configured to do so
+       if (!OverwriteReport) {
+               int num=1;
 
-                       if(count > 0) {
-                               if(debug)
-                                       debuga(_("File %s already exists, moved to %s\n"),outdirname,wdir);
-                               rename(outdirname,wdir);
-                       }
-               } else {
-                       if(access(outdirname,R_OK) == 0) {
-                               unlinkdir(outdirname,1);
-                       }
+               while (access(wdir,R_OK)==0 || errno==EACCES) //file exist or can't be read
+               {
+                       sprintf(wdir,"%s.%d",outdirname,num);
+                       num++;
+               }
+               if (num>1) {
+                       if(debug)
+                               debuga(__FILE__,__LINE__,_("File %s already exists, moved to %s\n"),outdirname,wdir);
+                       rename(outdirname,wdir);
                }
-               my_mkdir(outdirname);
        } else {
-               strcpy(dirname2,wdir);
-               if(!OverwriteReport) {
-                       while(num) {
-                               if(access(wdir,R_OK) == 0) {
-                                       sprintf(wdir,"%s.%d",dirname2,num);
-                                       num++;
-                                       count++;
-                               } else
-                                       break;
-                       }
-
-                       if(count > 0) {
-                               if(debug)
-                                       debuga(_("File %s already exists, moved to %s\n"),dirname2,wdir);
-                               rename(dirname2,wdir);
-                               strcpy(dirname2,wdir);
-                       }
-               } else {
-                       if(access(wdir,R_OK) == 0) {
-                               unlinkdir(wdir,1);
-                       }
+               if(access(outdirname,R_OK) == 0) {
+                       unlinkdir(outdirname,1);
                }
-
-               if(access(wdir, R_OK) != 0)
-                       my_mkdir(wdir);
        }
+       my_mkdir(outdirname);
 
-       strcpy(dirname2,wdir);
-
+       // create sarg-date to keep track of the report creation date
        if (snprintf(wdir,sizeof(wdir),"%s/sarg-date",outdirname)>=sizeof(wdir)) {
-               debuga(_("Buffer too small to store %s/sarg-date"),outdirname);
+               debuga(__FILE__,__LINE__,_("Buffer too small to store "));
+               debuga_more("%s/sarg-date",outdirname);
                exit(EXIT_FAILURE);
        }
        if ((fp_ou = fopen(wdir, "wt")) == 0) {
-               debuga(_("cannot open %s for writing: %s\n"),wdir,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdir,strerror(errno));
                perror("SARG:");
                exit(EXIT_FAILURE);
        }
@@ -1412,12 +1484,12 @@ int vrfydir(const struct periodstruct *per1, const char *addr, const char *site,
        loctm=localtime(&curtime);
        strftime(wdir,sizeof(wdir),"%Y-%m-%d %H:%M:%S",loctm);
        if (fprintf(fp_ou,"%s %d\n",wdir,loctm->tm_isdst)<0) {
-               debuga(_("Failed to write the date in %s\n"),wdir);
+               debuga(__FILE__,__LINE__,_("Failed to write the date in %s\n"),wdir);
                perror("SARG:");
                exit(EXIT_FAILURE);
        }
        if (fclose(fp_ou)==EOF) {
-               debuga(_("Failed to write the date in %s: %s\n"),wdir,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),wdir,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -1437,7 +1509,7 @@ int vrfydir(const struct periodstruct *per1, const char *addr, const char *site,
 void safe_strcpy(char *dest,const char *src,int length)
 {
        if (length<=0) {
-               debuga(_("Invalid buffer length passed to the function to safely copy a string\n"));
+               debuga(__FILE__,__LINE__,_("Invalid buffer length passed to the function to safely copy a string\n"));
                exit(EXIT_FAILURE);
        }
        strncpy(dest,src,length-1);
@@ -1516,16 +1588,16 @@ void date_from(char *date,int date_size, int *dfrom, int *duntil)
                int next=-1;
 
                if (sscanf(date,"%d/%d/%d%n",&d0,&m0,&y0,&next)!=3 || y0<100 || m0<1 || m0>12 || d0<1 || d0>31 || next<0) {
-                       debuga(_("The date passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
+                       debuga(__FILE__,__LINE__,_("The date passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
                        exit(EXIT_FAILURE);
                }
                if (date[next]=='-') {
                        if (sscanf(date+next+1,"%d/%d/%d",&d1,&m1,&y1)!=3 || y1<100 || m1<1 || m1>12 || d1<1 || d1>31) {
-                               debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
+                               debuga(__FILE__,__LINE__,_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
                                exit(EXIT_FAILURE);
                        }
                } else if (date[next]!='\0') {
-                       debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
+                       debuga(__FILE__,__LINE__,_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
                        exit(EXIT_FAILURE);
                } else {
                        d1=d0;
@@ -1538,18 +1610,18 @@ void date_from(char *date,int date_size, int *dfrom, int *duntil)
                struct tm *Date0,Date1;
 
                if (time(&Today)==(time_t)-1) {
-                       debuga(_("Failed to get the current time\n"));
+                       debuga(__FILE__,__LINE__,_("Failed to get the current time\n"));
                        exit(EXIT_FAILURE);
                }
                if (sscanf(date,"day-%d",&i)==1) {
                        if (i<0) {
-                               debuga(_("Invalid number of days in -d parameter\n"));
+                               debuga(__FILE__,__LINE__,_("Invalid number of days in -d parameter\n"));
                                exit(EXIT_FAILURE);
                        }
                        Today-=i*24*60*60;
                        Date0=localtime(&Today);
                        if (Date0==NULL) {
-                               debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+                               debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
                                exit(EXIT_FAILURE);
                        }
                        y0=y1=Date0->tm_year+1900;
@@ -1568,19 +1640,19 @@ void date_from(char *date,int date_size, int *dfrom, int *duntil)
                        time_t WeekBegin;
 
                        if (i<0) {
-                               debuga(_("Invalid number of weeks in -d parameter\n"));
+                               debuga(__FILE__,__LINE__,_("Invalid number of weeks in -d parameter\n"));
                                exit(EXIT_FAILURE);
                        }
                        Date0=localtime(&Today);
                        if (Date0==NULL) {
-                               debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+                               debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
                                exit(EXIT_FAILURE);
                        }
                        WeekBegin=Today-((Date0->tm_wday-FirstWeekDay+7)%7)*24*60*60;
                        WeekBegin-=i*7*24*60*60;
                        Date0=localtime(&WeekBegin);
                        if (Date0==NULL) {
-                               debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+                               debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
                                exit(EXIT_FAILURE);
                        }
                        y0=Date0->tm_year+1900;
@@ -1589,7 +1661,7 @@ void date_from(char *date,int date_size, int *dfrom, int *duntil)
                        WeekBegin+=6*24*60*60;
                        Date0=localtime(&WeekBegin);
                        if (Date0==NULL) {
-                               debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+                               debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
                                exit(EXIT_FAILURE);
                        }
                        y1=Date0->tm_year+1900;
@@ -1597,12 +1669,12 @@ void date_from(char *date,int date_size, int *dfrom, int *duntil)
                        d1=Date0->tm_mday;
                } else if (sscanf(date,"month-%d",&i)==1) {
                        if (i<0) {
-                               debuga(_("Invalid number of months in -d parameter\n"));
+                               debuga(__FILE__,__LINE__,_("Invalid number of months in -d parameter\n"));
                                exit(EXIT_FAILURE);
                        }
                        Date0=localtime(&Today);
                        if (Date0==NULL) {
-                               debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+                               debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
                                exit(EXIT_FAILURE);
                        }
                        if (Date0->tm_mon<i%12) {
@@ -1631,7 +1703,7 @@ void date_from(char *date,int date_size, int *dfrom, int *duntil)
                        m1=Date0->tm_mon+1;
                        d1=Date0->tm_mday;
                } else {
-                       debuga(_("Invalid date range passed on command line\n"));
+                       debuga(__FILE__,__LINE__,_("Invalid date range passed on command line\n"));
                        exit(EXIT_FAILURE);
                }
        }
@@ -1682,19 +1754,20 @@ void removetmp(const char *outdir)
                return;
 
        if(debug) {
-               debuga(_("Purging temporary file sarg-general\n"));
+               debuga(__FILE__,__LINE__,_("Purging temporary file sarg-general\n"));
        }
        if (snprintf(filename,sizeof(filename),"%s/sarg-general",outdir)>=sizeof(filename)) {
-               debuga(_("(removetmp) directory too long to remove %s/sarg-period\n"),outdir);
+               debuga(__FILE__,__LINE__,_("Path too long: "));
+               debuga_more("%s/sarg-period\n",outdir);
                exit(EXIT_FAILURE);
        }
        if((fp_gen=fopen(filename,"w"))==NULL){
-               debuga(_("(removetmp) Cannot open file %s: %s\n"),filename,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),filename,strerror(errno));
                exit(EXIT_FAILURE);
        }
        totalger(fp_gen,filename);
        if (fclose(fp_gen)==EOF) {
-               debuga(_("Failed to close %s after writing the total line: %s\n"),filename,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),filename,strerror(errno));
                exit(EXIT_FAILURE);
        }
 }
@@ -1711,27 +1784,27 @@ void load_excludecodes(const char *ExcludeCodes)
                return;
 
        if((fp_in=fopen(ExcludeCodes,"r"))==NULL) {
-               debuga(_("(util) Cannot open file %s (exclude_codes): %s\n"),ExcludeCodes,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ExcludeCodes,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        if (fseek(fp_in, 0, SEEK_END)==-1) {
-               debuga(_("Failed to move till the end of the excluded codes file %s: %s\n"),ExcludeCodes,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Failed to move till the end of file \"%s\": %s\n"),ExcludeCodes,strerror(errno));
                exit(EXIT_FAILURE);
        }
        MemSize = ftell(fp_in);
        if (MemSize<0) {
-               debuga(_("Cannot get the size of file %s\n"),ExcludeCodes);
+               debuga(__FILE__,__LINE__,_("Cannot get the size of file \"%s\"\n"),ExcludeCodes);
                exit(EXIT_FAILURE);
        }
        if (fseek(fp_in, 0, SEEK_SET)==-1) {
-               debuga(_("Failed to rewind the excluded codes file %s: %s\n"),ExcludeCodes,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Failed to rewind file \"%s\": %s\n"),ExcludeCodes,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        MemSize+=1;
        if((excludecode=(char *) malloc(MemSize))==NULL) {
-               debuga(_("malloc error (%ld)\n"),MemSize);
+               debuga(__FILE__,__LINE__,_("malloc error (%ld bytes required)\n"),MemSize);
                exit(EXIT_FAILURE);
        }
        memset(excludecode,0,MemSize);
@@ -1742,7 +1815,7 @@ void load_excludecodes(const char *ExcludeCodes)
                for (i=strlen(data)-1 ; i>=0 && (unsigned char)data[i]<=' ' ; i--) data[i]='\0';
                if (i<0) continue;
                if (Stored+i+2>=MemSize) {
-                       debuga(_("Too many codes to exclude in file %s\n"),ExcludeCodes);
+                       debuga(__FILE__,__LINE__,_("Too many codes to exclude in file \"%s\"\n"),ExcludeCodes);
                        break;
                }
                strcat(excludecode,data);
@@ -1750,7 +1823,10 @@ void load_excludecodes(const char *ExcludeCodes)
                Stored+=i+1;
        }
 
-       fclose(fp_in);
+       if (fclose(fp_in)==EOF) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),ExcludeCodes,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
        return;
 }
 
@@ -1894,7 +1970,7 @@ void show_info(FILE *fp_ou)
        if(!ShowSargInfo) return;
        zdate(ftime, sizeof(ftime), df);
        fputs("<div class=\"info\">",fp_ou);
-       fprintf(fp_ou,_("Generated by <a href='%s'>%s-%s</a> on %s"),URL,PGM,VERSION,ftime);
+       fprintf(fp_ou,_("Generated by <a href=\"%s\">%s-%s</a> on %s"),URL,PGM,VERSION,ftime);
        fputs("</div>\n",fp_ou);
 }
 
@@ -1947,11 +2023,10 @@ void close_html_header(FILE *fp_ou)
        fputs("</table></div>\n",fp_ou);
 }
 
-int write_html_trailer(FILE *fp_ou)
+void write_html_trailer(FILE *fp_ou)
 {
        show_info(fp_ou);
-       if (fputs("</body>\n</html>\n",fp_ou)==EOF) return(-1);
-       return(0);
+       fputs("</body>\n</html>\n",fp_ou);
 }
 
 void output_html_string(FILE *fp_ou,const char *str,int maxlen)
@@ -2096,7 +2171,7 @@ void version(void)
        printf(_("SARG Version: %s\n"),VERSION);
 #if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
        if (debug) {
-               printf(_("\nFor the translation to work, a valid message file should be installed as "
+               printf(_("\nFor the translation to work, a valid message file should be copied to "
                                 "\"%s/<Locale>/LC_MESSAGES/%s.mo\" where <Locale> is derived from the effective locale.\n"),LOCALEDIR,PACKAGE_NAME);
                if (CurrentLocale) {
                        printf(_("Currently effective locale is \"%s\".\n"),CurrentLocale);
@@ -2107,6 +2182,13 @@ void version(void)
                printf(_("If this message is in English, then your language is not supported or not correctly installed.\n"));
        }
 #endif
+       if (debug) {
+#ifdef HAVE_GLOB_H
+               printf(_("File globbing compiled in.\n"));
+#else
+               printf(_("File globbing NOT compiled in.\n"));
+#endif
+       }
        exit(EXIT_SUCCESS);
 }
 
@@ -2138,7 +2220,8 @@ void unlinkdir(const char *dir,bool contentonly)
                    (direntp->d_name[1] == '.' && direntp->d_name[2] == '\0')))
                        continue;
                if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) {
-                       debuga(_("directory name to delete too long: %s/%s\n"),dir,direntp->d_name);
+                       debuga(__FILE__,__LINE__,_("Path too long: "));
+                       debuga_more("%s/%s\n",dir,direntp->d_name);
                        exit(EXIT_FAILURE);
                }
 #ifdef HAVE_LSTAT
@@ -2147,25 +2230,25 @@ void unlinkdir(const char *dir,bool contentonly)
                err=stat(dname,&st);
 #endif
                if (err) {
-                       debuga(_("cannot stat %s\n"),dname);
+                       debuga(__FILE__,__LINE__,_("Cannot stat \"%s\": %s\n"),dname,strerror(errno));
                        exit(EXIT_FAILURE);
                }
                if (S_ISREG(st.st_mode)) {
                        if (unlink(dname)) {
-                               debuga(_("Cannot delete \"%s\": %s\n"),dname,strerror(errno));
+                               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),dname,strerror(errno));
                                exit(EXIT_FAILURE);
                        }
                } else if (S_ISDIR(st.st_mode)) {
                        unlinkdir(dname,0);
                } else {
-                       debuga(_("unknown path type %s\n"),dname);
+                       debuga(__FILE__,__LINE__,_("Don't know how to delete \"%s\" (not a regular file nor a directory)\n"),dname);
                }
        }
        closedir(dirp);
 
        if (!contentonly) {
                if (rmdir(dir)) {
-                       debuga(_("Cannot delete \"%s\": %s\n"),dir,strerror(errno));
+                       debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),dir,strerror(errno));
                        exit(EXIT_FAILURE);
                }
        }
@@ -2205,7 +2288,8 @@ void emptytmpdir(const char *dir)
                ".utmp",
                ".ip",
                "lastlog1",
-               "lastlog"
+               "lastlog",
+               "emailrep"
        };
 
        dirp=opendir(dir);
@@ -2224,14 +2308,15 @@ void emptytmpdir(const char *dir)
                        if (dlen>=elen && strcasecmp(direntp->d_name+dlen-elen,TmpExt[i])==0) break;
                }
                if (i<0) {
-                       debuga(_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
+                       debuga(__FILE__,__LINE__,_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
                        "Please check the temporary directory you gave to sarg. Adjust the path to a safe "
                        "directory or manually delete the content of \"%s\"\n"),direntp->d_name,dir,dir);
                        exit(EXIT_FAILURE);
                }
 
                if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) {
-                       debuga(_("directory name to delete too long: %s/%s\n"),dir,direntp->d_name);
+                       debuga(__FILE__,__LINE__,_("Path too long: "));
+                       debuga_more("%s/%s\n",dir,direntp->d_name);
                        exit(EXIT_FAILURE);
                }
 
@@ -2241,13 +2326,11 @@ void emptytmpdir(const char *dir)
                err=stat(dname,&st);
 #endif
                if (err) {
-                       debuga(_("cannot stat \"%s\"\n"),dname);
+                       debuga(__FILE__,__LINE__,_("Cannot stat \"%s\": %s\n"),dname,strerror(errno));
                        exit(EXIT_FAILURE);
                }
-               if (S_ISDIR(st.st_mode)) {
-                       unlinkdir(dname,0);
-               } else if (!S_ISREG(st.st_mode)) {
-                       debuga(_("Unknown path type for \"%s\". Check temporary directory\n"),dname);
+               if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) {
+                       debuga(__FILE__,__LINE__,_("Unknown path type for \"%s\". Check temporary directory\n"),dname);
                        exit(EXIT_FAILURE);
                }
        }
@@ -2266,14 +2349,15 @@ void emptytmpdir(const char *dir)
                        if (dlen>=elen && strcasecmp(direntp->d_name+dlen-elen,TmpExt[i])==0) break;
                }
                if (i<0) {
-                       debuga(_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
+                       debuga(__FILE__,__LINE__,_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
                        "Please check the temporary directory you gave to sarg. Adjust the path to a safe "
                        "directory or manually delete the content of \"%s\"\n"),direntp->d_name,dir,dir);
                        exit(EXIT_FAILURE);
                }
 
                if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) {
-                       debuga(_("directory name to delete too long: %s/%s\n"),dir,direntp->d_name);
+                       debuga(__FILE__,__LINE__,_("Path too long: "));
+                       debuga_more("%s/%s\n",dir,direntp->d_name);
                        exit(EXIT_FAILURE);
                }
 #ifdef HAVE_LSTAT
@@ -2282,16 +2366,18 @@ void emptytmpdir(const char *dir)
                err=stat(dname,&st);
 #endif
                if (err) {
-                       debuga(_("cannot stat \"%s\"\n"),dname);
+                       debuga(__FILE__,__LINE__,_("Cannot stat \"%s\": %s\n"),dname,strerror(errno));
                        exit(EXIT_FAILURE);
                }
-               if (S_ISREG(st.st_mode)) {
+               if (S_ISDIR(st.st_mode)) {
+                       unlinkdir(dname,0);
+               } else if (S_ISREG(st.st_mode)) {
                        if (unlink(dname)) {
-                               debuga(_("Cannot delete \"%s\": %s\n"),dname,strerror(errno));
+                               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),dname,strerror(errno));
                                exit(EXIT_FAILURE);
                        }
                } else {
-                       debuga(_("unknown path type %s\n"),dname);
+                       debuga(__FILE__,__LINE__,_("Don't know how to delete \"%s\" (not a regular file)\n"),dname);
                }
        }
        closedir(dirp);