]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - report.c
Make b-tree cache functions static.
[thirdparty/sarg.git] / report.c
index 0509b63a598a093dd5afc5a76c2e215110b2aba2..a1ea40d43aba4b8f0dae5e7f3a768ca2726930ce 100644 (file)
--- a/report.c
+++ b/report.c
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2008
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ *     http://sourceforge.net/projects/sarg/forums/forum/363374
  * ---------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
 
 #include "include/conf.h"
 #include "include/defs.h"
+#include "include/filelist.h"
 
-static void maketmp(const char *user, const char *dirname, int debug, int indexonly);
-static void maketmp_hour(const char *user, const char *dirname, int indexonly);
-static void gravatmp_hora(char *dirname, char *user, char *data, char *hora, char *elap, char *accbytes, int indexonly);
-static void gravatmpf(char *oldaccuser, char *dirname, char *oldurl, long long int nacc, long long int nbytes, char *oldmsg, long long int nelap, int indexonly, long long int incache, long long int oucache);
-static void gravaporuser(char *user, char *dirname, char *url, char *ip, char *data, char *hora, char *tam, char *elap, int indexonly);
-static void gravager(char *dirname, char *user, long long int nacc, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long int nelap, long long int incache, long long int oucache);
-static void grava_SmartFilter(char *dirname, char *user, char *ip, char *data, char *hora, char *url, char *smart);
+//! The global statistics of the whole log read.
+struct globalstatstruct globstat;
+//! \c True to enable the smart filter.
+bool smartfilter=false;
 
-void gerarel(void)
-{
-
-   FILE *fp_in;
-
-   char accdia[11], acchora[9], accuser[MAXLEN], accip[MAXLEN], accurl[MAXLEN], accbytes[12], accelap[10];
-   char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN], wdir[MAXLEN], per1[MAXLEN];
-   char wdirname[MAXLEN], oldurl[MAXLEN], oldaccuser[MAXLEN];
-   char olduser[MAXLEN], oldmsg[50], acccode[MAXLEN/2 - 1], oldaccelap[10], oldacccode[MAXLEN/2 - 1], user[MAXLEN];
-   char ipantes[MAXLEN], nameantes[MAXLEN]; 
-   char accsmart[MAXLEN];
-   char wcrc[MAXLEN/2 -1];
-   char crc2[MAXLEN/2 -1];
-   long long int nbytes=0; 
-   long long int nelap=0; 
-   long long int nacc=0;
-   long long int rtotal=0;
-   long long int incache=0;
-   long long int oucache=0;
-   char *s;
-   DIR *dirp;
-   struct dirent *direntp;
-   const char logext[]=".log";
-   int dlen;
-
-   ipantes[0]='\0';
-   nameantes[0]='\0';
-   smartfilter=0;
-
-   sprintf(dirname, "%s%s", outdir, period);
-   sprintf(wdir, "%s%s", outdir, period);
-   strcpy(per1,period);
-   vrfydir(wdir, per1, addr, site, us, email);
-
-   if(debugz){
-      debugaz("dirname",dirname);
-      debugaz("wdir",wdir);
-   }
-   strcpy(wdirname,dirname);
-   gperiod();
-
-   if(strlen(UserAgentLog) > 0 && email[0] == '\0') useragent();
-
-   olduser[0]='\0';
-   strncat(tmp,"/sarg",5);
-
-   dirp = opendir(tmp);
-   while ((direntp = readdir( dirp )) != NULL ) {
-      dlen=strlen(direntp->d_name)-(sizeof(logext)-1);
-      if (dlen<0) continue;
-      if((strcmp(direntp->d_name+dlen,logext) != 0) ||
-         (strncmp(direntp->d_name,"download.log",12) == 0) ||
-         (strncmp(direntp->d_name,"denied.log",10) == 0) ||
-         (strncmp(direntp->d_name,"authfail.log.unsort",19) == 0))
-         continue;
-      if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) {
-         fprintf(stderr, "SARG: (report) directory entry too long: %s/%s\n",tmp,direntp->d_name);
-         exit(1);
-      }
-      if((fp_in=fopen(tmp3,"r"))==NULL){
-         fprintf(stderr, "SARG: (report) %s: %s\n",text[45],tmp);
-         exit(1);
-      }
-    
-      if (dlen>0) {
-         if (dlen>=sizeof(user)) continue;
-         strncpy(user,direntp->d_name,dlen);
-         user[dlen]=0;
-      } else {
-         user[0]='\0';
-      }
-      strcpy(wdirname,dirname);
-      maketmp(user,tmp,debug,indexonly);
-      maketmp_hour(user,tmp,indexonly);
-   
-      strcpy(u2,user);
-      if(strcmp(Ip2Name,"yes") == 0)
-         ip2name(u2,sizeof(u2));
-      get_usertab_name(u2,name,sizeof(name));
-
-      if(dotinuser && strstr(name,"_")) {
-         str2=(char *)subs(name,"_",".");
-         strcpy(name,str2);
-      }
-
-      ttopen=0;
-      bzero(html_old, MAXLEN);
-   
-      while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
-         if (getword(accdia,sizeof(accdia),buf,' ')<0 || getword(acchora,sizeof(acchora),buf,' ')<0 ||
-             getword(accuser,sizeof(accuser),buf,' ')<0 || getword(accip,sizeof(accip),buf,' ')<0 ||
-             getword(accurl,sizeof(accurl),buf,' ')<0 || getword(accbytes,sizeof(accbytes),buf,' ')<0 ||
-             getword(acccode,sizeof(acccode),buf,' ')<0) {
-            printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp3);
-            exit(1);
-         }
-         if(strncmp(acccode,"TCP_DENIED/407",14) == 0) continue;
-         if (getword(accelap,sizeof(accelap),buf,' ')<0 || getword(accsmart,sizeof(accsmart),buf,' ')<0 ||
-             getword(accsmart,sizeof(accsmart),buf,'"')<0) {
-            printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp3);
-            exit(1);
-         }
-   
-         if(strlen(accsmart) > 0) {
-            smartfilter++;
-            strcpy(wdirname,dirname);
-            grava_SmartFilter(wdirname,accuser,accip,accdia,acchora,accurl,accsmart);
-         }
-
-         if(strcmp(Ip2Name,"yes") == 0) {
-            if(strcmp(accip,ipantes) != 0) {
-               strcpy(ipantes,accip);
-               ip2name(accip,sizeof(accip));
-               strcpy(nameantes,accip);
-            } else strcpy(accip,nameantes);
-         }
-     
-         strcpy(wdirname,dirname);
-         gravatmp_hora(wdirname,accuser,accdia,acchora,accelap,accbytes,indexonly);
-
-         if(iprel){
-            strcpy(wdirname,dirname);
-            gravaporuser(accuser,wdirname,accurl,accip,accdia,acchora,accbytes,accelap,indexonly);
-         }
-
-         if(!rtotal){
-            strcpy(oldurl,accurl);
-            strcpy(oldacccode,acccode);
-            strcpy(oldaccelap,accelap);
-            strcpy(oldaccuser,accuser);
-            strcpy(oldaccip,accip);
-            strcpy(oldaccdia,accdia);
-            strcpy(oldacchora,acchora);
-            rtotal++;
-         }
-
-         if(site[0] != '\0') {
-            if(strcmp(oldaccuser,accuser) != 0){
-               strcpy(oldmsg,"OK");
-               if(strstr(oldacccode,"DENIED") != 0)
-                  sprintf(oldmsg,"%s",text[46]);
-               gravatmp(oldaccuser,wdirname,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
-               gravager(wdirname,oldaccuser,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,
-                               incache,oucache);
-               nacc=0;
-               nbytes=0;
-               nelap=0;
-               incache=0;
-               oucache=0;
-            }
-         } else {
-            if(strcmp(oldurl,accurl) != 0 || strcmp(oldaccuser,accuser) != 0){
-               strcpy(oldmsg,"OK");
-               if(strstr(oldacccode,"DENIED") != 0)
-                  sprintf(oldmsg,"%s",text[46]);
-               strcpy(wdirname,dirname);
-               gravatmp(oldaccuser,wdirname,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
-               strcpy(wdirname,dirname);
-               gravager(wdirname,oldaccuser,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
-               nacc=0;
-               nbytes=0;
-               nelap=0;
-               incache=0;
-               oucache=0;
-               if(strcmp(oldaccuser,accuser) != 0)
-                  ind2=0;
-            }
-         }
-         nacc++;
-         nbytes+=my_atoll(accbytes);
-         nelap+=my_atoll(accelap);
-
-         if(strstr(ReportType,"site_user_time_date") != 0) {
-            if(!ttopen) {
-               ind2++;
-               strcpy(siteind,accurl);
-               str=siteind;
-               for(z1=0; str[z1]; z1++) {
-                  if(str[z1]=='?' || str[z1]=='-' || str[z1]=='.' || str[z1]==':' || str[z1]=='/' || str[z1]=='\\' || str[z1]=='*' ||
-                     str[z1]=='\'' || str[z1]=='\"' || str[z1]=='$')
-                     str[z1]='_';
-               }
-               sprintf(arqtt,"%s/%s",dirname,accuser);
-               if(access(arqtt, R_OK) != 0)
-                  my_mkdir(arqtt);
-               sprintf(arqtt,"%s/%s/tt%s-%s.html",dirname,accuser,accuser,siteind);
-               if(strlen(arqtt) > 255) {
-                  strncpy(val7,arqtt,255);
-                  bzero(arqtt,MAXLEN);
-                  strcpy(arqtt,val7);
-               }
-               if ((fp_tt = fopen(arqtt, "w")) == 0) {
-                  fprintf(stderr, "SARG: (report) %s: %s\n",text[45],arqtt);
-                  exit(1);
-               }
-               ttopen=1;
-
-               if(strcmp(Privacy,"yes") == 0)
-                  sprintf(httplink,"<font size=%s color=%s><href=http://%s>%s",        \
-                              FontSize,PrivacyStringColor,PrivacyString,PrivacyString);
-               else
-                  sprintf(httplink,"<font size=%s><a href=http://%s>%s</A>",FontSize,accurl,accurl);
-
-               sprintf(ltext110,"%s",text[110]);
-               for(s=ltext110; *s; ++s)
-                  *s=tolower(*s);
-
-               fprintf(fp_tt, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
-               css(fp_tt);
-               fputs("</head>\n",fp_tt);
-               fprintf(fp_tt,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
-               if(strlen(LogoImage) > 0) fprintf(fp_tt, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText);
-
-               if(strcmp(IndexTree,"date") == 0)
-                  show_sarg(fp_tt, "../../../..");
-               else
-                  show_sarg(fp_tt, "../..");
-
-               fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_tt);
-               fprintf(fp_tt,"<tr><th class=\"title\" colspan=\"2\">%s</th></tr>\n",Title);
-
-               fprintf(fp_tt,"<tr><td class=\"header\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",text[89],period);
-               fprintf(fp_tt,"<tr><td class=\"header\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",text[90],name);
-               fprintf(fp_tt,"<tr><td class=\"header\" colspan=\"2\">%s:&nbsp;%s, %s</td></tr>\n",text[104],UserSortField,UserSortOrder);
-               fprintf(fp_tt,"<tr><th class=\"header3\" colspan=2>%s</th></tr>\n",text[32]);
-
-               fputs("</table></center>\n",fp_tt);
-               fputs("<center><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_tt);
-               fputs("<tr><td></td><td></td></tr>",fp_tt);
-               bzero(tmp4, MAXLEN);
-               strncpy(tmp4,text[110],4);
-               fprintf(fp_tt,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th></tr>\n",text[91],tmp4,text[110]+5);
-            }
-
-            sprintf(html,"<tr><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",accurl,accdia,acchora);
-
-            if(strcmp(html,html_old) != 0)
-               fputs(html,fp_tt);
-            strcpy(html_old, html);
-         } else bzero(ltext110, 50);
-
-         strcpy(wcrc,acccode);
-         if (getword(crc2,sizeof(crc2),wcrc,'/')<0) {
-            printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp3);
-            exit(1);
-         }
-
-         if(strstr(crc2,"MISS") != 0)
-            oucache+=my_atoll(accbytes);
-         else incache+=my_atoll(accbytes);
-
-         strcpy(oldurl,accurl);
-
-         if(strcmp(accuser,oldaccuser) != 0) {
-            strcpy(wdirname,dirname);
-            day_totalize(tmp,oldaccuser,indexonly);
-         }
-
-         strcpy(oldaccuser,accuser);
-         strcpy(oldacccode,acccode);
-         strcpy(oldaccelap,accelap);
-         strcpy(oldaccip,accip);
-         strcpy(oldaccdia,accdia);
-         strcpy(oldacchora,acchora);
-
-      }
-      bzero(user,MAXLEN);
-      fclose(fp_in);
-      unlink(tmp3);
-   }
-
-   strcpy(oldmsg,"OK");
-   if(strstr(oldacccode,"DENIED") != 0)
-      sprintf(oldmsg,"%s",text[46]);
-   strcpy(wdirname,dirname);
-   if(strlen(oldaccuser) == 0)
-      strcpy(oldaccuser,accuser);
-   gravatmpf(oldaccuser,wdirname,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
-   strcpy(wdirname,dirname);
-   gravager(wdirname,oldaccuser,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
-   strcpy(wdirname,dirname);
-   day_totalize(tmp,oldaccuser,indexonly);
-
-   tmpsort();
-
-   strcpy(wdirname,dirname);
-   totalger(wdirname, debug, outdir);
-
-   if(strlen(email) == 0) {
-      if(strstr(ReportType,"downloads") != 0) download_report();
-
-      if(strlen(DansGuardianConf) > 0) {
-         strcpy(wdirname,dirname);
-         dansguardian_log();
-      }
-
-      strcpy(wdirname,dirname);
-      squidguard_log();
-
-      strcpy(wdirname,dirname);
-      topuser();
-
-      if(strstr(ReportType,"topsites") != 0) topsites();
-
-      if(strstr(ReportType,"sites_users") != 0) siteuser();
-      gen_denied_report();
-
-      strcpy(wdirname,dirname);
-      authfail_report();
-
-      if(smartfilter) smartfilter_report();
-
-      if(strlen(DansGuardianConf) > 0) dansguardian_report();
+extern FileListObject UserAgentLog;
 
-      squidguard_report();
+//! The file to store the HTML page where the time of access is reported for one site and one user.
+static FILE *fp_tt=NULL;
+//! The name of the file containing the access time of the site/user.
+static char arqtt[4096]="";
 
-      if(strstr(ReportType,"users_sites") != 0) htmlrel();
+static FILE *maketmp(const char *user, const char *dirname, int debug);
+static void gravatmp(FILE *fp_ou, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, long long int incache, long long int oucache);
+static void closett(void);
+static void gravaporuser(const struct userinfostruct *uinfo, const char *dirname, const char *url, const char *ip, const char *data, const char *hora, long long int tam, long long int elap);
+static void gravager(FILE *fp_gen,const char *filename, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache);
+static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart);
 
-      make_index();
-
-      if(strncmp(SuccessfulMsg,"yes",3) == 0) fprintf(stderr, "SARG: %s %s\n",text[47],dirname);
-    } else {
-      strcpy(wdirname,dirname);
-      geramail(wdirname, debug, outdir, userip, email, TempDir);
-
-      if((strcmp(email,"stdout") != 0) && (strncmp(SuccessfulMsg,"yes",3) == 0))
-            fprintf(stderr, "SARG: %s %s\n",text[48],email);
-   }
-
-   if(indexonly) {
-      strcpy(wdirname,dirname);
-      index_only(wdirname, debug);
-   }
-
-   if(strlen(email) < 0)
-      removetmp(dirname);
-
-   return;
+void gerarel(void)
+{
+       FileObject *fp_in;
+       FILE *fp_gen;
+       FILE *fp_tmp=NULL;
+
+       char *buf;
+       char accdia[11], acchora[9], accip[256], *accurl;
+       char oldaccdia[11], oldacchora[9], oldaccip[256];
+       char oldacciptt[256];
+       char wdirname[MAXLEN];
+       char *oldurl=NULL;
+       char oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1];
+       char ipantes[256], nameantes[MAXLEN];
+       char accsmart[MAXLEN];
+       char crc2[MAXLEN/2 -1];
+       char siteind[MAX_TRUNCATED_URL];
+       char *oldurltt=NULL;
+       char oldaccdiatt[11],oldacchoratt[9];
+       char tmp3[MAXLEN];
+       char u2[MAX_USER_LEN];
+       char userlabel[MAX_USER_LEN];
+       long long int nbytes=0;
+       long long int nelap=0;
+       long long int nacc=0;
+       long long int incache=0;
+       long long int oucache=0;
+       long long int accbytes, accelap;
+       char *str;
+       userscan uscan;
+       const char *sort_field;
+       const char *sort_order;
+       const char *user;
+       int url_len;
+       int ourl_size=0;
+       int ourltt_size=0;
+       int same_url;
+       bool new_user;
+       struct getwordstruct gwarea;
+       longline line;
+       struct userinfostruct *uinfo;
+       DayObject daystat;
+
+       ipantes[0]='\0';
+       smartfilter=false;
+       memset(&globstat,0,sizeof(globstat));
+
+       if (email[0]=='\0') {
+               if (vrfydir(&period, addr, site, us)<0) {
+                       debuga(__FILE__,__LINE__,_("Cannot create the output directory name containing the period as part of the name\n"));
+                       exit(EXIT_FAILURE);
+               }
+       } else {
+               if (snprintf(outdirname,sizeof(outdirname),"%s/emailrep",tmp)>=sizeof(outdirname)) {
+                       debuga(__FILE__,__LINE__,_("Path too long: "));
+                       debuga_more("%s/emailrep\n",tmp);
+                       exit(EXIT_FAILURE);
+               }
+               my_mkdir(outdirname);
+       }
+
+       if(debugz>=LogLevel_Process){
+               debugaz(__FILE__,__LINE__,_("outdirname=%s\n"),outdirname);
+       }
+
+       if (email[0] == '\0' && !FileList_IsEmpty(UserAgentLog))
+               UserAgent_Readlog();
+
+       init_usertab(UserTabFile);
+       UserAgent();
+
+       snprintf(wdirname,sizeof(wdirname),"%s/sarg-general",outdirname);
+       if((fp_gen=MY_FOPEN(wdirname,"w"))==NULL){
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       fp_tt=NULL;
+       sort_labels(&sort_field,&sort_order);
+
+       if (indexonly || datetimeby==0)
+               daystat=NULL;
+       else
+               daystat=day_prepare();
+
+       uscan=userinfo_startscan();
+       if (uscan == NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot enumerate the user list\n"));
+               exit(EXIT_FAILURE);
+       }
+       while ((uinfo = userinfo_advancescan(uscan)) != NULL ) {
+               sort_users_log(tmp,debug,uinfo);
+               if (snprintf(tmp3,sizeof(tmp3),"%s/%s.user_log",tmp,uinfo->filename)>=sizeof(tmp3)) {
+                       debuga(__FILE__,__LINE__,_("Path too long: "));
+                       debuga_more("%s/%s.user_log\n",tmp,uinfo->filename);
+                       exit(EXIT_FAILURE);
+               }
+               if((fp_in=FileObject_Open(tmp3))==NULL){
+                       debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,FileObject_GetLastOpenError());
+                       exit(EXIT_FAILURE);
+               }
+               user=uinfo->filename;
+               day_newuser(daystat);
+
+               strcpy(u2,uinfo->id);
+               if(Ip2Name && uinfo->id_is_ip) {
+                       safe_strcpy(ipantes,u2,sizeof(ipantes));
+                       ip2name(u2,sizeof(u2));
+                       strcpy(nameantes,u2);
+               }
+               user_find(userlabel,MAX_USER_LEN, u2);
+               userinfo_label(uinfo,userlabel);
+
+               if (!indexonly) {
+                       fp_tmp=maketmp(user,tmp,debug);
+               }
+
+               ttopen=0;
+               oldurl=NULL;
+               oldurltt=NULL;
+               ourltt_size=0;
+               memset(oldaccdiatt,0,sizeof(oldaccdiatt));
+               memset(oldacchoratt,0,sizeof(oldacchoratt));
+               memset(oldacciptt,0,sizeof(oldacciptt));
+               new_user=true;
+               nacc=0;
+               nbytes=0;
+               nelap=0;
+               incache=0;
+               oucache=0;
+
+               if ((line=longline_create())==NULL) {
+                       debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),tmp3);
+                       exit(EXIT_FAILURE);
+               }
+
+               while((buf=longline_read(fp_in,line))!=NULL) {
+                       getword_start(&gwarea,buf);
+                       if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
+                           getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
+                           getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
+                           getword(acccode,sizeof(acccode),&gwarea,'\t')<0) {
+                               debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),tmp3);
+                               exit(EXIT_FAILURE);
+                       }
+                       if(strncmp(acccode,"TCP_DENIED/407",14) == 0) continue;
+                       if (getword_atoll(&accelap,&gwarea,'\t')<0) {
+                               debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),tmp3);
+                               exit(EXIT_FAILURE);
+                       }
+                       if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
+                               debuga(__FILE__,__LINE__,_("Invalid smart info in file \"%s\"\n"),tmp3);
+                               exit(EXIT_FAILURE);
+                       }
+
+                       if(accsmart[0] != '\0') {
+                               smartfilter=true;
+                               grava_SmartFilter(tmp,uinfo->id,accip,accdia,acchora,accurl,accsmart);
+                       }
+
+                       if(Ip2Name) {
+                               if(strcmp(accip,ipantes) != 0) {
+                                       strcpy(ipantes,accip);
+                                       ip2name(accip,sizeof(accip));
+                                       strcpy(nameantes,accip);
+                               } else safe_strcpy(accip,nameantes,sizeof(accip));
+                       }
+
+                       if (!indexonly) {
+                               day_addpoint(daystat,accdia,acchora,accelap,accbytes);
+                               if (iprel) gravaporuser(uinfo,outdirname,accurl,accip,accdia,acchora,accbytes,accelap);
+                       }
+
+                       if(new_user){
+                               url_len=strlen(accurl);
+                               if (!oldurl || url_len>=ourl_size) {
+                                       ourl_size=url_len+1;
+                                       oldurl=realloc(oldurl,ourl_size);
+                                       if (!oldurl) {
+                                               debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
+                                               exit(EXIT_FAILURE);
+                                       }
+                               }
+                               strcpy(oldurl,accurl);
+                               strcpy(oldacccode,acccode);
+                               strcpy(oldaccip,accip);
+                               strcpy(oldaccdia,accdia);
+                               strcpy(oldacchora,acchora);
+                               new_user=false;
+                       }
+                       same_url=(strcmp(oldurl,accurl) == 0);
+
+                       if(site[0] == '\0') {
+                               if(!same_url){
+                                       if(strstr(oldacccode,"DENIED") != 0)
+                                               strcpy(oldmsg,"DENIED");
+                                       else
+                                               strcpy(oldmsg,"OK");
+                                       if (fp_tmp) gravatmp(fp_tmp,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
+                                       gravager(fp_gen,wdirname,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
+                                       nacc=0;
+                                       nbytes=0;
+                                       nelap=0;
+                                       incache=0;
+                                       oucache=0;
+                               }
+                       }
+                       nacc++;
+                       nbytes+=accbytes;
+                       nelap+=accelap;
+
+                       if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0 && !indexonly &&
+                           (!oldurltt || strcmp(oldurltt,accurl) || strcmp(oldaccdiatt,accdia) || strcmp(oldacchoratt,acchora) ||
+                                               strcmp(oldacciptt,accip))) {
+
+                               if(!ttopen) {
+                                       snprintf(arqtt,sizeof(arqtt),"%s/%s",outdirname,uinfo->filename);
+                                       if(access(arqtt, R_OK) != 0)
+                                               my_mkdir(arqtt);
+                                       snprintf(arqtt,sizeof(arqtt),"%s/%s/tt.html",outdirname,uinfo->filename);
+                                       if ((fp_tt = fopen(arqtt, "w")) == 0) {
+                                               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqtt,strerror(errno));
+                                               exit(EXIT_FAILURE);
+                                       }
+                                       ttopen=1;
+
+                                       /*
+                                       if(Privacy)
+                                               sprintf(httplink,"<font size=%s color=%s><href=http://%s>%s",FontSize,PrivacyStringColor,PrivacyString,PrivacyString);
+                                       else
+                                               sprintf(httplink,"<font size=%s><a href=\"http://%s\">%s</a>",FontSize,accurl,accurl);
+                                       */
+
+                                       write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"),HTML_JS_NONE);
+                                       fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
+                                       fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("User"),uinfo->label);
+                                       fputs("<tr><td class=\"header_c\">",fp_tt);
+                                       fprintf(fp_tt,_("Sort:&nbsp;%s, %s"),sort_field,sort_order);
+                                       fputs("</td></tr>\n",fp_tt);
+                                       fprintf(fp_tt,"<tr><th class=\"header_c\">%s</th></tr>\n",_("User"));
+                                       close_html_header(fp_tt);
+
+                                       fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_tt);
+                               }
+                               if (!oldurltt || strcmp(oldurltt,accurl)) {
+                                       const char *url=accurl;
+                                       if (*url==ALIAS_PREFIX) url++;
+                                       url_to_anchor(accurl,siteind,sizeof(siteind));
+                                       fprintf(fp_tt,"<tr class=\"tt\"><td colspan=\"3\"><a name=\"%s\">",siteind);
+                                       fprintf(fp_tt,"<b>%s</b>",_("Accessed site: "));
+                                       output_html_string(fp_tt,url,100);
+                                       fputs("</a></td></tr>\n",fp_tt);
+                                       fprintf(fp_tt,"<tr><th class=\"header_l\">%s</th>",_("IP"));
+                                       fprintf(fp_tt,"<th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("DATE"),pgettext("wall clock","TIME"));
+                               }
+
+                               fprintf(fp_tt,"<tr><td class=\"data2\">%s</td>",accip);
+                               fprintf(fp_tt,"<td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",accdia,acchora);
+
+                               url_len=strlen(accurl);
+                               if (!oldurltt || url_len>=ourltt_size) {
+                                       ourltt_size=url_len+1;
+                                       oldurltt=realloc(oldurltt,ourltt_size);
+                                       if (!oldurltt) {
+                                               debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
+                                               exit(EXIT_FAILURE);
+                                       }
+                               }
+                               strcpy(oldurltt,accurl);
+                               strcpy(oldaccdiatt,accdia);
+                               strcpy(oldacchoratt,acchora);
+                               strcpy(oldacciptt,accip);
+                       }
+
+                       strcpy(crc2,acccode);
+                       str=strchr(crc2,'/');
+                       if (str) *str='\0';
+                       if(strstr(crc2,"MISS") != 0)
+                               oucache+=accbytes;
+                       else
+                               incache+=accbytes;
+
+                       strcpy(oldacccode,acccode);
+                       strcpy(oldaccip,accip);
+                       if (!same_url) {
+                               url_len=strlen(accurl);
+                               if (url_len>=ourl_size) {
+                                       ourl_size=url_len+1;
+                                       oldurl=realloc(oldurl,ourl_size);
+                                       if (!oldurl) {
+                                               debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
+                                               exit(EXIT_FAILURE);
+                                       }
+                               }
+                               strcpy(oldurl,accurl);
+                       }
+                       strcpy(oldaccdia,accdia);
+                       strcpy(oldacchora,acchora);
+               }
+               if (FileObject_Close(fp_in)) {
+                       debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,FileObject_GetLastCloseError());
+                       exit(EXIT_FAILURE);
+               }
+               longline_destroy(&line);
+               if (oldurltt) free(oldurltt);
+               if (oldurl) {
+                       if(strstr(oldacccode,"DENIED") != 0)
+                               strcpy(oldmsg,"DENIED");
+                       else
+                               strcpy(oldmsg,"OK");
+                       if (fp_tmp) gravatmp(fp_tmp,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
+                       closett();
+                       gravager(fp_gen,wdirname,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
+                       free(oldurl);
+                       oldurl=NULL;
+               }
+               if(!new_user) {
+                       day_totalize(daystat,tmp,uinfo);
+               }
+               if (fp_tmp) {
+                       if (fclose(fp_tmp)==EOF) {
+                               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),uinfo->filename,strerror(errno));
+                               exit(EXIT_FAILURE);
+                       }
+                       fp_tmp=NULL;
+               }
+               if (!KeepTempLog && unlink(tmp3)) {
+                       debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+       }
+       userinfo_stopscan(uscan);
+       day_cleanup(daystat);
+
+       totalger(fp_gen,wdirname);
+       if (fclose(fp_gen)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),wdirname,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if(email[0] == '\0') {
+               if (!indexonly) {
+                       if(DansGuardianConf[0] != '\0')
+                               dansguardian_log();
+                       else if (debugz>=LogLevel_Process)
+                               debugaz(__FILE__,__LINE__,_("Dansguardian report not produced because no dansguardian configuration file was provided\n"));
+
+                       redirector_log();
+               }
+
+               topuser();
+
+               if (!indexonly) {
+                       if((ReportType & REPORT_TYPE_DOWNLOADS) != 0)
+                               download_report();
+                       else if (debugz>=LogLevel_Process)
+                               debugaz(__FILE__,__LINE__,_("Downloaded files report not requested in report_type\n"));
+
+                       if((ReportType & REPORT_TYPE_TOPSITES) != 0)
+                               topsites();
+                       else if (debugz>=LogLevel_Process)
+                               debugaz(__FILE__,__LINE__,_("Top sites report not requested in report_type\n"));
+
+                       if((ReportType & REPORT_TYPE_SITES_USERS) != 0)
+                               siteuser();
+                       else if (debugz>=LogLevel_Process)
+                               debugaz(__FILE__,__LINE__,_("Sites & users report not requested in report_type\n"));
+
+                       if ((ReportType & REPORT_TYPE_DENIED) != 0)
+                               gen_denied_report();
+                       else if (debugz>=LogLevel_Process)
+                               debugaz(__FILE__,__LINE__,_("Denied accesses report not requested in report_type\n"));
+
+                       if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0)
+                               authfail_report();
+                       else if (debugz>=LogLevel_Process)
+                               debugaz(__FILE__,__LINE__,_("Authentication failures report not requested in report_type\n"));
+
+                       if(smartfilter) smartfilter_report();
+
+                       if(DansGuardianConf[0] != '\0')
+                               dansguardian_report();
+
+                       redirector_report();
+
+                       if((ReportType & REPORT_TYPE_USERS_SITES) != 0)
+                               htmlrel();
+                       else if (debugz>=LogLevel_Process)
+                               debugaz(__FILE__,__LINE__,_("User's detailed report not requested in report_type\n"));
+               }
+
+               make_index();
+
+               if(SuccessfulMsg) debuga(__FILE__,__LINE__,_("Successful report generated on %s\n"),outdirname);
+       } else {
+               topuser();
+
+               if((strcmp(email,"stdout") != 0) && SuccessfulMsg)
+                       debuga(__FILE__,__LINE__,_("Successful report generated and sent to %s\n"),email);
+       }
+
+       if(indexonly) index_only(outdirname, debug);
+
+       removetmp(outdirname);
+       return;
 }
 
-static void maketmp(const char *user, const char *dirname, int debug, int indexonly)
+static FILE *maketmp(const char *user, const char *dirname, int debug)
 {
+       FILE *fp_ou;
+       char wdirname[MAXLEN];
 
-   FILE *fp_ou;
-
-   char wdirname[MAXLEN];
-
-   if(indexonly) return;
-   if(strstr(ReportType,"users_sites") == 0) return;
+       if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return(NULL);
+       if((ReportType & REPORT_TYPE_TOPUSERS) == 0) return(NULL);
 
-   strcpy(wdirname,tmp);
-   strcat(wdirname,"/");
-   strcat(wdirname,user);
+       if(debug) debuga(__FILE__,__LINE__,_("Making file %s/%s\n"),tmp,user);
+       if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,user)>=sizeof(wdirname)) {
+               debuga(__FILE__,__LINE__,_("Path too long: "));
+               debuga_more("%s/%s.utmp\n",tmp,user);
+               exit(EXIT_FAILURE);
+       }
 
-   if(debug){
-      sprintf(msg,"%s: %s",text[49],wdirname);
-      debuga(msg);
-   }
+       if((fp_ou=fopen(wdirname,"w"))==NULL){
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
-   strcat(wdirname,".utmp");
-   if((fp_ou=fopen(wdirname,"w"))==NULL){
-      fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
-      exit(1);
-   }
-
-   fclose(fp_ou);
-   return;
+       return(fp_ou);
 }
 
 
-static void maketmp_hour(const char *user, const char *dirname, int indexonly)
+static void gravatmp(FILE *fp_ou, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, long long int incache, long long int oucache)
 {
+       /*
+       This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
+       to print a long long int unless it is exactly 64-bits long.
+       */
+       fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)nacc,(uint64_t)nbytes,oldurl,oldmsg,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache);
 
-   FILE *fp_ou;
-
-   char wdirname[MAXLEN];
-
-   if(indexonly) return;
-   if(strstr(ReportType,"users_sites") == 0) return;
-
-   strcpy(wdirname,tmp);
-   strcat(wdirname,"/");
-   strcat(wdirname,user);
-
-   strcat(wdirname,".htmp");
-   if((fp_ou=fopen(wdirname,"w"))==NULL){
-      fprintf(stderr, "SARG: (report-1) %s: %s - %s\n",text[45],wdirname,strerror(errno));
-      exit(1);
-   }
-
-   fclose(fp_ou);
-   return;
+       return;
 }
 
-
-void gravatmp(char *oldaccuser, char *dirname, char *oldurl, long long int nacc, long long int nbytes, char *oldmsg, long long int nelap, int indexonly, long long int incache, long long int oucache)
+static void closett(void)
 {
-
-   FILE *fp_ou;
-
-   char wdirname[MAXLEN];
-
-   if(indexonly) return;
-   if(strstr(ReportType,"users_sites") == 0) return;
-
-   strcpy(wdirname,tmp);
-   strcat(wdirname,"/");
-   strcat(wdirname,oldaccuser);
-   strcat(wdirname,".utmp");
-
-   if((fp_ou=fopen(wdirname,"a"))==NULL){
-      fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
-      exit(1);
-   } 
-
-   my_lltoa(nacc,val1,15);
-   my_lltoa(nbytes,val2,15);
-   my_lltoa(nelap,val3,15);
-   my_lltoa(incache,val4,15);
-   my_lltoa(oucache,val5,15);
-   fprintf(fp_ou,"%s %s %s %s %s %s %s\n",val1,val2,oldurl,oldmsg,val3,val4,val5);
-
-   fclose(fp_ou);
-   ttopen=0;
-
-   if(fp_tt) {
-      fputs("</table>\n",fp_tt);
-      fputs("</body>\n</html>\n",fp_tt);
-      fclose(fp_tt);
-   }
-
-   return;
-
+       ttopen=0;
+
+       if(fp_tt) {
+               fputs("</table>\n</div>\n",fp_tt);
+               fputs("</body>\n</html>\n",fp_tt);
+               if (fclose(fp_tt)==EOF) {
+                       debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),arqtt,strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+               fp_tt=NULL;
+       }
 }
 
-
-static void gravatmp_hora(char *dirname, char *user, char *data, char *hora, char *elap, char *bytes, int indexonly)
+static void gravaporuser(const struct userinfostruct *uinfo, const char *dirname, const char *url, const char *ip, const char *data, const char *hora, long long int tam, long long int elap)
 {
-
-   FILE *fp_ou;
-
-   char wdirname[MAXLEN];
-
-   if(indexonly || (strstr(ReportType,"users_sites") == 0)) return;
-
-   strcpy(wdirname,tmp);
-   strcat(wdirname,"/");
-   strcat(wdirname,user);
-   strcat(wdirname,".htmp");
-
-   if((fp_ou=fopen(wdirname,"a"))==NULL){
-      fprintf(stderr, "SARG: (report-2) %s: %s - %s\n",text[45],wdirname,strerror(errno));
-      exit(1);
-   }
-
-   if(strcmp(datetimeby,"bytes") == 0) fprintf(fp_ou,"%s %s %s\n",data,hora,bytes);
-   else fprintf(fp_ou,"%s %s %s\n",data,hora,elap);
-
-   fclose(fp_ou);
-
-   return;
+       FILE *fp_ou;
+       char wdirname[MAXLEN];
+
+       if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
+
+       if (snprintf(wdirname,sizeof(wdirname),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(wdirname)) {
+               debuga(__FILE__,__LINE__,_("Path too long: "));
+               debuga_more("%s/%s.ip\n",tmp,uinfo->filename);
+               exit(EXIT_FAILURE);
+       }
+
+       if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       /*
+       This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
+       to print a long long int unless it is exactly 64-bits long.
+       */
+       fprintf(fp_ou,"%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\n",ip,url,data,hora,(uint64_t)tam,(uint64_t)elap);
+
+       if (fclose(fp_ou)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),wdirname,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       return;
 }
 
 
-static void gravaporuser(char *user, char *dirname, char *url, char *ip, char *data, char *hora, char *tam, char *elap, int indexonly)
+static void gravager(FILE *fp_gen,const char *filename, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache)
 {
-
-   FILE *fp_ou;
-
-   char wdirname[MAXLEN];
-
-   if(indexonly || (strstr(ReportType,"users_sites") == 0)) return;
-
-   strcpy(wdirname,tmp);
-   strcat(wdirname,"/");
-   strcat(wdirname,user);
-   strcat(wdirname,".ip");
-
-   if((fp_ou=fopen(wdirname,"a"))==NULL){
-      fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
-      exit(1);
-   } 
-
-   fprintf(fp_ou,"%s %s %s %s %s %s\n",ip,url,data,hora,tam,elap);
-
-   fclose(fp_ou);
-
-   return;
-
+       /*
+       This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
+       to print a long long int unless it is exactly 64-bits long.
+       */
+       if (fprintf(fp_gen,"%s\t%"PRIu64"\t%"PRIu64"\t%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",uinfo->id,(uint64_t)nacc,(uint64_t)nbytes,url,ip,hora,dia,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache)<0) {
+               debuga(__FILE__,__LINE__,_("Write error in file \"%s\": %s\n"),filename,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       globstat.nacc+=nacc;
+       globstat.nbytes+=nbytes;
+       globstat.elap+=nelap;
+       globstat.incache+=incache;
+       globstat.oucache+=oucache;
+       return;
 }
 
-
-static void gravatmpf(char *oldaccuser, char *dirname, char *oldurl, long long int nacc, long long int nbytes, char *oldmsg, long long int nelap, int indexonly, long long int incache, long long int oucache)
+/*!
+Write the total line at the end of the general file.
+*/
+void totalger(FILE *fp_gen,const char *filename)
 {
-
-   FILE *fp_ou;
-
-   char wdirname[MAXLEN];
-
-   if(indexonly || (strstr(ReportType,"users_sites") == 0)) return;
-
-   strcpy(wdirname,tmp);
-   strcat(wdirname,"/");
-   strcat(wdirname,oldaccuser);
-   strcat(wdirname,".utmp");
-
-   if((fp_ou=fopen(wdirname,"a"))==NULL){
-      fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
-      exit(1);
-   }
-
-   my_lltoa(nacc,val1,15);
-   my_lltoa(nbytes,val2,15);
-   my_lltoa(nelap,val3,15);
-   my_lltoa(incache,val4,15);
-   my_lltoa(oucache,val5,15);
-   fprintf(fp_ou,"%s %s %s %s %s %s %s\n",val1,val2,oldurl,oldmsg,val3,val4,val5);
-
-   fclose(fp_ou);
-   ttopen=0;
-   ind2=0;
-
-   if(fp_tt) {
-      fputs("</table>\n",fp_tt);
-      fputs("</html>\n",fp_tt);
-      fclose(fp_tt);
-   }
-
-   return;
-     
+       /*
+       This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
+       to print a long long int unless it is exactly 64-bits long.
+       */
+       if (fprintf(fp_gen,"TOTAL\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)globstat.nacc,(uint64_t)globstat.nbytes,(uint64_t)globstat.elap,(uint64_t)globstat.incache,(uint64_t)globstat.oucache)<0) {
+               debuga(__FILE__,__LINE__,_("Failed to write the total line in %s\n"),filename);
+               exit(EXIT_FAILURE);
+       }
 }
 
-
-static void gravager(char *dirname, char *user, long long int nacc, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long int nelap, long long int incache, long long int oucache)
+int ger_read(char *buffer,struct generalitemstruct *item,const char *filename)
 {
-
-   FILE *fp_ou;
-
-   strcat(dirname,"/");
-   strcat(dirname,"sarg-general");
-   
-   if((fp_ou=fopen(dirname,"a"))==NULL){
-      fprintf(stderr, "SARG: (report) %s: %s\n",text[45],dirname);
-      exit(1);
-   }
-
-   my_lltoa(nacc,val1,15);
-   my_lltoa(nbytes,val2,15);
-   my_lltoa(nelap,val3,15);
-   my_lltoa(incache,val4,15);
-   my_lltoa(oucache,val5,15);
-   fprintf(fp_ou,"%s %s %s %s %s %s %s %s %s %s\n",user,val1,val2,url,ip,hora,dia,val3,val4,val5);
-
-   fclose(fp_ou);
-   return;
-
+       int i;
+       int sign;
+       long long int number;
+
+       if (strncmp(buffer,"TOTAL\t",6)==0) {
+               item->total=1;
+               buffer+=6;
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Invalid total number of accesses in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               buffer++;
+               item->nacc=number*sign;
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Invalid total size in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               buffer++;
+               item->nbytes=number*sign;
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Invalid total elapsed time in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               buffer++;
+               item->nelap=number*sign;
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Invalid total cache hit in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               buffer++;
+               item->incache=number*sign;
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\0') {
+                       debuga(__FILE__,__LINE__,_("Invalid total cache miss in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               item->oucache=number*sign;
+       } else {
+               item->total=0;
+
+               item->user=buffer;
+               for (i=0 ; i<MAX_USER_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("User name too long or invalid in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               *buffer++='\0';
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               buffer++;
+               item->nacc=number*sign;
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               buffer++;
+               item->nbytes=number*sign;
+
+               item->url=buffer;
+               while ((unsigned char)*buffer>=' ') buffer++;
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("URL too long or invalid in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               *buffer++='\0';
+
+               item->ip=buffer;
+               for (i=0 ; i<MAX_IP_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("IP address too long or invalid in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               *buffer++='\0';
+
+               item->time=buffer;
+               for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Time too long or invalid in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               *buffer++='\0';
+
+               item->date=buffer;
+               for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Date too long or invalid in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               *buffer++='\0';
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               buffer++;
+               item->nelap=number*sign;
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\t') {
+                       debuga(__FILE__,__LINE__,_("Invalid cache hit size in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               buffer++;
+               item->incache=number*sign;
+
+               sign=+1;
+               if (*buffer == '-') {
+                       buffer++;
+                       sign=-1;
+               } else if (*buffer == '+') {
+                       buffer++;
+               }
+               number=0LL;
+               while (isdigit(*buffer))
+                       number=(number * 10) + (*buffer++)-'0';
+               if (*buffer!='\0') {
+                       debuga(__FILE__,__LINE__,_("Invalid cache miss size in file \"%s\"\n"),filename);
+                       exit(EXIT_FAILURE);
+               }
+               item->oucache=number*sign;
+       }
+       return(0);
 }
 
-static void grava_SmartFilter(char *dirname, char *user, char *ip, char *data, char *hora, char *url, char *smart)
+static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart)
 {
+       FILE *fp_ou;
+       char wdirname[MAXLEN];
 
-   FILE *fp_ou;
-
-   char wdirname[MAXLEN];
-
-   sprintf(wdirname,"%s/smartfilter.unsort",dirname);
-
-   if((fp_ou=fopen(wdirname,"a"))==NULL){
-      fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
-      exit(1);
-   }
+       if (snprintf(wdirname,sizeof(wdirname),"%s/smartfilter.int_unsort",dirname)>=sizeof(wdirname)) {
+               debuga(__FILE__,__LINE__,_("Path too long: "));
+               debuga_more("%s/smartfilter.int_unsort\n",dirname);
+               exit(EXIT_FAILURE);
+       }
 
-   fprintf(fp_ou,"%s %s %s %s %s %s\n",user,data,hora,ip,url,smart);
-   fputs("</body>\n</html>\n",fp_tt);
+       if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
-   fclose(fp_ou);
+       fprintf(fp_ou,"%s\t%s\t%s\t%s\t%s\t%s\n",user,data,hora,ip,url,smart);
+       fputs("</body>\n</html>\n",fp_tt);
 
-   return;
+       if (fclose(fp_ou)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),wdirname,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
+       return;
 }