]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - log.c
Build the .htaccess of each report out of a template file.
[thirdparty/sarg.git] / log.c
diff --git a/log.c b/log.c
index 7c0b0c0ad2d5d84a47e341298d8fc9f8dde9cbf0..d9c2fc811331e76a6a61f38f7a13cbb4575ed1d9 100644 (file)
--- a/log.c
+++ b/log.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, 2010
  *
  * 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"
 
-#define LEGACY_WRITE_USER
-#define LEGACY_WRITE_DOWNLOAD
 #define REPORT_EVERY_X_LINES 5000
+#define MAX_OPEN_USER_FILES 10
 
+struct userfilestruct
+{
+   struct userfilestruct *next;
+   struct userinfostruct *user;
+   FILE *file;
+};
 
-char *userfile;
-char *excludefile;
-char *excludeuser;
-
-char sz_Last_User[ MAXLEN ] = { 0 } ;
-int bool_ShowReadStatistics ;
+static char *userfile=NULL;
 
 numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
 numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
              13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 };
 
-void getusers(char *pwdfile, int debug);
-void gethexclude(char *hexfile, int debug);
-void getuexclude(char *uexfile, int debug);
-void ttx(char *user);
-int compar( const void *, const void * );
+static void getusers(const char *pwdfile, int debug);
 
-#define _FILE_OFFSET_BITS 64
-
-int main(argc,argv)
-   int argc;
-   char *argv[];
+int main(int argc,char *argv[])
 {
-
-   FILE *fp_in = NULL, *fp_ou = NULL, *fp_denied=NULL, *fp_usr, *fp_authfail=NULL, *fp_log=NULL;
+   enum isa_col_id {
+      ISACOL_Ip,
+      ISACOL_UserName,
+      ISACOL_Date,
+      ISACOL_Time,
+      ISACOL_TimeTaken,
+      ISACOL_Bytes,
+      ISACOL_Uri,
+      ISACOL_Status,
+      ISACOL_Last //last entry of the list !
+   };
+   enum InputLogFormat {
+      ILF_Unknown,
+      ILF_Squid,
+      ILF_Common,
+      ILF_Sarg,
+      ILF_Isa,
+      ILF_Last //last entry of the list !
+   };
+
+   FILE *fp_in = NULL, *fp_denied=NULL, *fp_authfail=NULL, *fp_log=NULL;
 
    char sz_Download_Unsort[ 20000 ] ;
    FILE * fp_Download_Unsort = NULL ;
-   FILE * fp_Write_User = NULL ;
 
    extern int optind;
    extern int optopt;
    extern char *optarg;
 
-   char w[MAXLEN];
-   char data[255]; 
+   char data[255];
    char elap[255];
-   char none[255];
    char ip[MAXLEN];
-   char msg[MAXLEN];
    char tam[255];
-   char fun[255];
-   char wuser[MAXLEN]; 
+   char fun[MAXLEN];
+   char wuser[MAXLEN];
    char smartfilter[MAXLEN];
-   char dia[128]; 
+   char dia[128];
    char wdata[128];
    char mes[30];
    char ano[30];
@@ -84,8 +93,7 @@ int main(argc,argv)
    char date[255];
    char arq[255];
    char arq_log[255];
-   char warq[255][255];
-   char hm[15], hmf[15], hmr[15], whm[15];
+   char hm[15], hmf[15], hmr[15];
    int  chm=0;
    char uagent[MAXLEN];
    char hexclude[MAXLEN];
@@ -95,13 +103,22 @@ int main(argc,argv)
    char tbuf2[128];
    char zip[20];
    char *str;
-   char bufz[MAXLEN];
-   char bufy[MAXLEN];
-   int  common; 
-   int  common_log=0;
-   int  squid_log=0;
+   char tmp2[MAXLEN];
+   char tmp3[MAXLEN];
+   char denied_unsort[MAXLEN];
+   char denied_sort[MAXLEN];
+   char authfail_unsort[MAXLEN];
+   char start_hour[128];
+   char end_hour[128];
+   char *linebuf;
+   char hostname[512];
+   char *url;
+   char *urly;
+   char user[MAX_USER_LEN];
+   enum InputLogFormat ilf;
+   int ilf_count[ILF_Last];
    int  ch;
-   int  x, l;
+   int  x;
    int  errflg=0;
    int  puser=0;
    int  fhost=0;
@@ -109,45 +126,60 @@ int main(argc,argv)
    int  fuser=0;
    int  idata=0;
    int  mindate=0;
-   int  narq=0;
    int  iarq=0;
    int  exstring=0;
-   int  i0=0,i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0;
-   long totregsl=0; 
+   int isa_ncols=0,isa_cols[ISACOL_Last];
+   int from_stdin;
+   int blen;
+   int maxopenfiles;
+   int nopen;
+   int id_is_ip;
+   long totregsl=0;
    long totregsg=0;
    long totregsx=0;
    long totper=0;
-   long int  max_elapsed=0;
+   long int max_elapsed=0;
    time_t tt;
    struct tm *t;
-   unsigned long nreg=0;
-   off_t recs1=0;
-   unsigned long recs2=0;
-   struct rlimit rl;
+   unsigned long recs1=0UL;
+   unsigned long recs2=0UL;
    int OutputNonZero = REPORT_EVERY_X_LINES ;
+   int download_flag=0;
+   char *download_url=NULL;
+   struct getwordstruct gwarea;
+   struct longlinestruct line;
+   struct userinfostruct *uinfo;
+   struct userfilestruct *first_user_file, *ufile, *ufile1, *prev_ufile;
+
+#ifdef HAVE_LOCALE_H
+   setlocale(LC_TIME,"");
+#endif
+
+#if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
+   setlocale (LC_ALL, "");
+   bindtextdomain (PACKAGE_NAME, LOCALEDIR);
+   textdomain (PACKAGE_NAME);
+#endif //ENABLE_NLS
 
    BgImage[0]='\0';
    LogoImage[0]='\0';
    LogoText[0]='\0';
    PasswdFile[0]='\0';
    OutputEmail[0]='\0';
-   Ip2Name[0]='\0';
    UserAgentLog[0]='\0';
    ExcludeHosts[0]='\0';
    ExcludeUsers[0]='\0';
    ConfigFile[0]='\0';
    code[0]='\0';
-   LastLog[0]='\0';
-   UserIp[0]='\0';
-   ReportType[0]='\0';
+   LastLog=0;
+   ReportType=0UL;
    UserTabFile[0]='\0';
    BlockIt[0]='\0';
    ExternalCSSFile[0]='\0';
    SquidGuardLogFormat[0]='\0';
    SquidGuardLogAlternate[0]='\0';
-   arq[0]='\0';
+   for (ilf=0 ; ilf<ILF_Last ; ilf++) ilf_count[ilf]=0;
 
-   strcpy(AccessLog,"/usr/local/squid/var/logs/access.log");
    sprintf(ExcludeCodes,"%s/exclude_codes",SYSCONFDIR);
    strcpy(GraphDaysBytesBarColor,"orange");
    strcpy(BgColor,"#ffffff");
@@ -163,62 +195,78 @@ int main(argc,argv)
    strcpy(FontSize,"9px");
    strcpy(TempDir,"/tmp");
    strcpy(OutputDir,"/var/www/html/squid-reports");
-   strcpy(Ip2Name,"no");
+   Ip2Name=0;
    strcpy(DateFormat,"u");
-   strcpy(OverwriteReport,"no");
-   strcpy(RemoveTempFiles,"yes");
+   OverwriteReport=0;
+   RemoveTempFiles=1;
    strcpy(ReplaceIndex,"index.html");
-   strcpy(Index,"yes");
-   strcpy(RecordsWithoutUser,"ip");
-   strcpy(UseComma,"no");
+   Index=INDEX_YES;
+   RecordsWithoutUser=RECORDWITHOUTUSER_IP;
+   UseComma=0;
    strcpy(MailUtility,"mailx");
-   strcpy(TopSitesNum,"100");
-   strcpy(UserIp,"no");
+   TopSitesNum=100;
+   TopUsersNum=0;
+   UserIp=0;
    strcpy(TopuserSortField,"BYTES");
    strcpy(UserSortField,"BYTES");
    strcpy(TopuserSortOrder,"reverse");
    strcpy(UserSortOrder,"reverse");
    strcpy(TopsitesSortField,"CONNECT");
    strcpy(TopsitesSortType,"D");
-   strcpy(LongUrl,"no");
-   strcpy(language,"English");
+   LongUrl=0;
    strcpy(FontFace,"Verdana,Tahoma,Arial");
    strcpy(datetimeby,"elap");
    strcpy(CharSet,"ISO-8859-1");
-   strcpy(Privacy,"no");
+   Privacy=0;
    strcpy(PrivacyString,"***.***.***.***");
    strcpy(PrivacyStringColor,"blue");
-   strcpy(SuccessfulMsg,"yes");
-   strcpy(TopUserFields,"NUM DATE_TIME USERID CONNECT BYTES %BYTES IN-CACHE-OUT USED_TIME MILISEC %TIME TOTAL AVERAGE");
-   strcpy(UserReportFields,"CONNECT BYTES %BYTES IN-CACHE-OUT USED_TIME MILISEC %TIME TOTAL AVERAGE");
+   SuccessfulMsg=1;
+   TopUserFields=TOPUSERFIELDS_NUM | TOPUSERFIELDS_DATE_TIME | TOPUSERFIELDS_USERID | TOPUSERFIELDS_CONNECT |
+                 TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_OUT |
+                 TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTIME |
+                 TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE;
+   UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USERREPORTFIELDS_SETYB |
+                    USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERREPORTFIELDS_MILISEC |
+                    USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE;
    strcpy(DataFileDelimiter,";");
-   strcpy(DataFileFields,"user;date;time;url;connect;bytes;in_cache;out_cache;elapsed");
-   strcpy(SiteUserTimeDateType,"table");
-   strcpy(ShowReadStatistics,"yes");
+   DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA_FIELD_URL | DATA_FIELD_CONNECT |
+                  DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DATA_FIELD_ELAPSED;
+   ShowReadStatistics=1;
    strcpy(IndexSortOrder,"D");
-   strcpy(ShowSargInfo,"yes");
-   strcpy(ShowSargLogo,"yes");
+   ShowSargInfo=1;
+   ShowSargLogo=1;
    strcpy(ParsedOutputLog,"no");
-   strcpy(ParsedOutputLogCompress,"/bin/gzip");
+   strcpy(ParsedOutputLogCompress,"/bin/gzip -f");
    strcpy(DisplayedValues,"abbreviation");
    strcpy(HeaderFontSize,"9px");
    strcpy(TitleFontSize,"11px");
-   strcpy(AuthUserFile,"/usr/local/sarg/passwd");
-   strcpy(AuthName,"SARG, Restricted Access");
-   strcpy(AuthType,"basic");
-   strcpy(Require,"require user admin %u");
-   strcpy(DownloadSuffix,"7z,ace,arj,avi,bat,bin,bz2,bzip,cab,com,cpio,dll,doc,dot,exe,gz,iso,lha,lzh,mdb,mov,mp3,mpeg,mpg,mso,nrg,ogg,ppt,rar,rtf,shs,src,sys,tar,tgz,vcd,vob,wma,wmv,zip");
-   strcpy(Graphs,"yes");
+   strcpy(AuthUserTemplateFile,"sarg_htaccess");
+   set_download_suffix("7z,ace,arj,avi,bat,bin,bz2,bzip,cab,com,cpio,dll,doc,dot,exe,gz,iso,lha,lzh,mdb,mov,mp3,mpeg,mpg,mso,nrg,ogg,ppt,rar,rtf,shs,src,sys,tar,tgz,vcd,vob,wma,wmv,zip");
+   Graphs=1;
+#if defined(FONTDIR)
+   strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf");
+#else
+   GraphFont[0]='\0';
+#endif
    strcpy(Ulimit,"20000");
-   strcpy(NtlmUserFormat,"domainname+username");
-   strcpy(IndexTree,"file");
+   NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER;
+   IndexTree=INDEX_TREE_FILE;
    strcpy(RealtimeTypes,"GET,PUT,CONNECT");
    strcpy(RealtimeUnauthRec,"show");
-   strcpy(SquidguardIgnoreDate,"off");
-   strcpy(DansguardianIgnoreDate,"off");
-   strcpy(DataFileUrl,"ip");
+   SquidguardIgnoreDate=0;
+   DansguardianIgnoreDate=0;
+   DataFileUrl=DATAFILEURL_IP;
    strcpy(MaxElapsed,"28800000");
-   strcpy(BytesInSitesUsersReport,"no");
+   BytesInSitesUsersReport=0;
+   UserAuthentication=0;
+   strcpy(LDAPHost,"127.0.0.1");
+   LDAPPort=389;
+   LDAPProtocolVersion=3;
+   LDAPBindDN[0]='\0';
+   LDAPBindPW[0]='\0';
+   LDAPBaseSearch[0]='\0';
+   strcpy(LDAPFilterSearch, "uid=%s");
+   strcpy(LDAPTargetAttr, "cn");
 
    dia[0]='\0';
    mes[0]='\0';
@@ -246,13 +294,15 @@ int main(argc,argv)
    DataFile[0]='\0';
    SquidGuardConf[0]='\0';
    DansGuardianConf[0]='\0';
+   start_hour[0]='\0';
+   end_hour[0]='\0';
 
-   excode=0;
    denied_count=0;
    download_count=0;
    authfail_count=0;
    dansguardian_count=0;
    squidguard_count=0;
+   useragent_count=0;
    DeniedReportLimit=10;
    AuthfailReportLimit=10;
    DansGuardianReportLimit=10;
@@ -267,9 +317,6 @@ int main(argc,argv)
    color1=0;
    color2=0;
    color3=0;
-   sarglog=0;
-   isalog=0;
-   dotinuser=0;
    realt=0;
    realtime_refresh=3;
    realtime_access_log_lines=1000;
@@ -278,14 +325,16 @@ int main(argc,argv)
    ndownload=0;
    squid24=0;
 
-   bzero(IncludeUsers, MAXLEN);
-   bzero(ExcludeString, MAXLEN);
+   bzero(IncludeUsers, sizeof(IncludeUsers));
+   bzero(ExcludeString, sizeof(ExcludeString));
+   first_user_file=NULL;
 
-   for(x=0; x<=254; x++) 
-      warq[x][0]='\0';
+   NAccessLog=0;
+   for(x=0; x<MAXLOGS; x++)
+      AccessLog[x][0]='\0';
+   AccessLogFromCmdLine=0;
 
-   language_load(language);
-   strcpy(Title,text[88]);
+   strcpy(Title,_("Squid User Access Report"));
 
    while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:u:l:L:o:s:t:w:hijmnprvxyz")) != -1){
       switch(ch)
@@ -300,10 +349,11 @@ int main(argc,argv)
             strcpy(hexclude,optarg);
             break;
          case 'd':
-            strcpy(date,optarg);
-            strcpy(cduntil,optarg);
-            if (getword(cdfrom,sizeof(cdfrom),cduntil,'-')<0) {
-               printf("SARG: Maybe you have a broken record or garbage in your date range.\n");
+            strncpy(date,optarg,sizeof(date)-1);
+            date[sizeof(date)-1]='\0';
+            getword_start(&gwarea,optarg);
+            if (getword(cdfrom,sizeof(cdfrom),&gwarea,'-')<0 || getword(cduntil,sizeof(cduntil),&gwarea,0)<0) {
+               printf(_("SARG: The date range requested on the command line by option -d is invalid.\n"));
                exit(1);
             }
             date_from(date, cdfrom, cduntil);
@@ -319,16 +369,21 @@ int main(argc,argv)
          case 'g':
             strcpy(df,optarg);
             break;
-        case 'h':
-           usage(argv[0]);
+         case 'h':
+            usage(argv[0]);
             exit(0);
-           break;
+            break;
          case 'i':
             iprel++;
             break;
          case 'l':
-            strcpy(warq[narq],optarg);
-            narq++;
+            if (NAccessLog>=MAXLOGS) {
+               printf(_("SARG: Too many log files passed on command line with option -l.\n"));
+               exit(1);
+            }
+            strcpy(AccessLog[NAccessLog],optarg);
+            NAccessLog++;
+            AccessLogFromCmdLine++;
             break;
          case 'L':
             strcpy(SquidGuardLogAlternate,optarg);
@@ -352,39 +407,31 @@ int main(argc,argv)
             strcpy(site,optarg);
             break;
          case 't':
+         {
+            int h,m;
+
             if(strstr(optarg,"-") == 0) {
               strcpy(hm,optarg);
               strcpy(hmf,optarg);
             } else {
-               if (getword(hm,sizeof(hm),optarg,'-')<0) {
-                  printf("SARG: Maybe you have a broken record or garbage in your time range.\n");
+               getword_start(&gwarea,optarg);
+               if (getword(hm,sizeof(hm),&gwarea,'-')<0 || getword(hmf,sizeof(hmf),&gwarea,0)<0) {
+                  debuga(_("The time range passed on the command line with option -t is invalid\n"));
                   exit(1);
                }
-               strcpy(hmf,optarg);
             }
-            if(strlen(hm) > 5) {
-              printf("SARG: time period must be MM or MM:SS. Exit.\n");
-              exit(1);
-            }
-            bzero(whm,15);
-            if(strstr(hm,":") != 0) {
-               if (getword(warea,sizeof(warea),hm,':')<0) {
-                  printf("SARG: Maybe you have a broken record or garbage in your time range.\n");
-                  exit(1);
-               }
-               sprintf(whm,"%s%s",warea,hm);
-               strcpy(hm,whm);
+            if(sscanf(hm,"%d:%d",&h,&m)!=2) {
+               debuga(_("Time period must be MM or MM:SS. Exit\n"));
+               exit(1);
             }
-            bzero(whm,15);
-            if(strstr(hmf,":") != 0) {
-               if (getword(warea,sizeof(warea),hmf,':')<0) {
-                  printf("SARG: Maybe you have a broken record or garbage in your time range.\n");
-                  exit(1);
-               }
-               sprintf(whm,"%s%s",warea,hmf);
-               strcpy(hmf,whm);
+            sprintf(hm,"%02d%02d",h,m);
+            if(sscanf(hmf,"%d:%d",&h,&m)!=2) {
+               debuga(_("Time period must be MM or MM:SS. Exit\n"));
+               exit(1);
             }
+            sprintf(hmf,"%02d%02d",h,m);
             break;
+         }
          case 'u':
             strcpy(us,optarg);
             break;
@@ -404,9 +451,9 @@ int main(argc,argv)
             debugz++;
             break;
          case ':':
-           fprintf(stderr, "Option -%c require an argument\n",optopt);
-           errflg++;
-           break;
+            debuga(_("Option -%c require an argument\n"),optopt);
+            errflg++;
+            break;
          case '?':
             usage(argv[0]);
             exit(1);
@@ -420,61 +467,51 @@ int main(argc,argv)
       exit(2);
    }
 
-   if(debug) debuga("Init");
+   if(debug) debuga(_("Init\n"));
 
    if(ConfigFile[0] == '\0') sprintf(ConfigFile,"%s/sarg.conf",SYSCONFDIR);
-    else if(access(ConfigFile, R_OK) != 0) {
-            sprintf(msg,"Cannot open config file: %s - %s",ConfigFile,strerror(errno));
-            debuga(msg);
-            exit(1);
-         }
-    
+   if(access(ConfigFile, R_OK) != 0) {
+      debuga(_("Cannot open config file: %s - %s\n"),ConfigFile,strerror(errno));
+      exit(1);
+   }
+
    if(access(ConfigFile, R_OK) == 0)
       getconf();
-   
+
+   if(UserIp) userip++;
+
+   if(dns) Ip2Name=1;
+
    if(realt) {
       realtime();
       exit(0);
    }
 
-   if(strcmp(IndexTree,"file") == 0)
+   if(IndexTree == INDEX_TREE_FILE)
       strcpy(ImageFile,"../images");
    else
       strcpy(ImageFile,"../../../images");
 
    dataonly=0;
-   if(DataFile[0] != '\0');
+   if(DataFile[0] != '\0')
       dataonly++;
-   
-   str2=(char *)subs(TopUserFields,"%BYTES","SETYB");
-   strcpy(TopUserFields,str2);
-   
-   str2=(char *)subs(UserReportFields,"%BYTES","SETYB");
-   strcpy(UserReportFields,str2);
-
-   if(!narq) {
-      strcpy(warq[0],AccessLog);
-      narq++;
+
+   if(!NAccessLog) {
+      strcpy(AccessLog[0],"/var/log/squid/access.log");
+      NAccessLog++;
    }
 
    if(strcmp(hexclude,"onvert") == 0 && strcmp(site,"plit") != 0) {
-      convlog(warq[0], df, dfrom, duntil);
+      convlog(AccessLog[0], df, dfrom, duntil);
       exit(0);
    }
-   
+
    if(strcmp(site,"plit") == 0) {
-      splitlog(warq[0], df, dfrom, duntil, hexclude);
+      splitlog(AccessLog[0], df, dfrom, duntil, hexclude);
       exit(0);
    }
 
-   if(ExcludeCodes[0] != '\0') {
-      if((excludecode=(char *) malloc(1024))==NULL) {
-         fprintf(stderr, "SARG: %s (1024):\n",text[59]);
-         exit(1);
-      }
-      bzero(excludecode,1024);
-      load_excludecodes();
-   }
+   load_excludecodes(ExcludeCodes);
 
    if(access(PasswdFile, R_OK) == 0) {
       getusers(PasswdFile,debug);
@@ -483,19 +520,16 @@ int main(argc,argv)
 
    if(hexclude[0] == '\0')
       strcpy(hexclude,ExcludeHosts);
-   if(strlen(hexclude) > 0) {
-      if(access(hexclude, R_OK) != 0) {
-         sprintf(msg,"Cannot open exclude_hosts file: %s - %s",hexclude,strerror(errno));
-         debuga(msg);
-         exit(1);
-      } else {
-         gethexclude(hexclude,debug);
-         fhost++;
-      }
+   if(hexclude[0] != '\0') {
+      gethexclude(hexclude,debug);
+      fhost++;
    }
 
-   if(ReportType[0] == '\0')
-      strcpy(ReportType,"topusers topsites users_sites sites_users date_time denied auth_failures site_user_time_date downloads");
+   if(ReportType == 0) {
+      ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES |
+                 REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPORT_TYPE_DENIED |
+                 REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | REPORT_TYPE_DOWNLOADS;
+   }
 
    if(access(ExcludeUsers, R_OK) == 0) {
       getuexclude(ExcludeUsers,debug);
@@ -504,24 +538,17 @@ int main(argc,argv)
 
    indexonly=0;
    if(fuser) {
-      if(strstr(excludeuser,"indexonly") != 0)
+      if(is_indexonly())
          indexonly++;
    }
    if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly++;
-   if(strcmp(Index,"only") == 0) indexonly++;
-
-   if(dns) strcpy(Ip2Name,"yes");
-
-   if(strcmp(UserIp,"yes") == 0) userip++;
+   if(Index == INDEX_ONLY) indexonly++;
 
-   if(strlen(MaxElapsed)>1) max_elapsed=atol(MaxElapsed);
+   if(MaxElapsed[0] != '\0') max_elapsed=atol(MaxElapsed);
 
-   if(strlen(outdir)<1) strcpy(outdir,OutputDir);
+   if(outdir[0] == '\0') strcpy(outdir,OutputDir);
    strcat(outdir,"/");
 
-
-   if(arq[0] == '\0') strcpy(arq,AccessLog);
-
    if(uagent[0] == '\0') strcpy(uagent,UserAgentLog);
 
    if(tmp[0] == '\0') strcpy(tmp,TempDir);
@@ -535,11 +562,11 @@ int main(argc,argv)
       strcpy(DateFormat,"u");
    }
 
-   if(strlen(email)<1 && strlen(OutputEmail)>0) strcpy(email,OutputEmail);
+   if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail);
 
    strcpy(tmp2,tmp);
 
-   if(strlen(email) > 0) {
+   if(email[0] != '\0') {
       sprintf(wtemp2,"%s/sarg",tmp2);
       my_mkdir(wtemp2);
       strcat(tmp2,"/sarg");
@@ -549,999 +576,980 @@ int main(argc,argv)
 
    strcat(tmp2,"/sarg.log");
 
-   sprintf(warea,"%s/sarg",tmp);
-   if(access(warea, R_OK) == 0) {
-      sprintf(tmp3,"rm -rf %s",warea);
-      system(tmp3);
-   }
-   
    sprintf(tmp3,"%s/sarg",tmp);
+   if(access(tmp3, R_OK) == 0) {
+      unlinkdir(tmp3,1);
+   }
    my_mkdir(tmp3);
-   strcpy(tmp4,tmp3);
-   strcpy(tmp5,tmp3);
-   strcpy(tmp6,tmp3);
-   strcat(tmp4,"/denied.log.unsort");
-   strcat(tmp5,"/denied.log");
-   strcat(tmp6,"/authfail.log.unsort");
+   strcpy(denied_unsort,tmp3);
+   strcpy(denied_sort,tmp3);
+   strcpy(authfail_unsort,tmp3);
+   strcat(denied_unsort,"/denied.log.unsort");
+   strcat(denied_sort,"/denied.log");
+   strcat(authfail_unsort,"/authfail.log.unsort");
 
    if(debug) {
-      fprintf(stderr, "SARG: %s:\nSARG:\n",text[22]);
-      fprintf(stderr, "SARG: %35s (-a) = %s\n",text[23],addr);
-      fprintf(stderr, "SARG: %35s (-b) = %s\n",text[71],uagent);
-      fprintf(stderr, "SARG: %35s (-c) = %s\n",text[69],hexclude);
-      fprintf(stderr, "SARG: %35s (-d) = %s\n",text[24],date);
-      fprintf(stderr, "SARG: %35s (-e) = %s\n",text[41],email);
-      fprintf(stderr, "SARG: %35s (-f) = %s\n",text[70],ConfigFile);
+      debuga(_("Parameters:\n"));
+      debuga(_("          Hostname or IP address (-a) = %s\n"),addr);
+      debuga(_("                   Useragent log (-b) = %s\n"),uagent);
+      debuga(_("                    Exclude file (-c) = %s\n"),hexclude);
+      debuga(_("                 Date from-until (-d) = %s\n"),date);
+      debuga(_("   Email address to send reports (-e) = %s\n"),email);
+      debuga(_("                     Config file (-f) = %s\n"),ConfigFile);
       if(strcmp(df,"e") == 0)
-         fprintf(stderr, "SARG: %35s (-g) = %s (dd/mm/yyyy)\n",text[25],text[26]);
+         debuga(_("                     Date format (-g) = Europe (dd/mm/yyyy)\n"));
       if(strcmp(df,"u") == 0)
-         fprintf(stderr, "SARG: %35s (-g) = %s (mm/dd/yyyy)\n",text[25],text[27]);
+         debuga(_("                     Date format (-g) = USA (mm/dd/yyyy)\n"));
       if(strcmp(df,"w") == 0)
-         fprintf(stderr, "SARG: %35s (-g) = %s (yyyy/ww)\n",text[25],text[85]);
-      if(iprel)
-         fprintf(stderr, "SARG: %35s (-i) = %s\n",text[28],text[1]);
-       else
-         fprintf(stderr, "SARG: %35s (-i) = %s\n",text[28],text[2]);
-      fprintf(stderr, "SARG: %35s (-l) = %s\n",text[37],arq);
-      if(strcmp(Ip2Name,"yes") == 0)
-         fprintf(stderr, "SARG: %35s (-n) = %s\n",text[65],text[1]);
-       else
-         fprintf(stderr, "SARG: %35s (-n) = %s\n",text[65],text[2]);
-      fprintf(stderr, "SARG: %35s (-o) = %s\n",text[38],outdir);
-      if(strcmp(UserIp,"yes") == 0)
-         fprintf(stderr, "SARG: %35s (-p) = %s\n",text[29],text[1]);
-       else
-         fprintf(stderr, "SARG: %35s (-p) = %s\n",text[29],text[2]);
-      fprintf(stderr, "SARG: %35s (-s) = %s\n",text[30],site);
-      fprintf(stderr, "SARG: %35s (-t) = %s\n",text[31],hm);
-      fprintf(stderr, "SARG: %35s (-u) = %s\n",text[32],us);
-      fprintf(stderr, "SARG: %35s (-w) = %s\n",text[34],tmp);
-      if(debug)
-         fprintf(stderr, "SARG: %35s (-x) = %s\n",text[35],text[1]);
-       else
-         fprintf(stderr, "SARG: %35s (-x) = %s\n",text[35],text[2]);
-      if(debugz)
-         fprintf(stderr, "SARG: %35s (-z) = %s\n",text[36],text[1]);
-       else
-         fprintf(stderr, "SARG: %35s (-z) = %s\n",text[36],text[2]);
-      fprintf(stderr, "SARG:\n");
+         debuga(_("                     Date format (-g) = Sites & Users (yyyy/ww)\n"));
+      debuga(_("                       IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
+      for (iarq=0 ; iarq<NAccessLog ; iarq++)
+         debuga(_("                       Input log (-l) = %s\n"),AccessLog[iarq]);
+      debuga(_("              Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
+      debuga(_("                      Output dir (-o) = %s\n"),outdir);
+      debuga(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
+      debuga(_("                   Accessed site (-s) = %s\n"),site);
+      debuga(_("                            Time (-t) = %s\n"),hm);
+      debuga(_("                            User (-u) = %s\n"),us);
+      debuga(_("                   Temporary dir (-w) = %s\n"),tmp);
+      debuga(_("                  Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
+      debuga(_("                Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
+      debuga("\n");
    }
 
    if(debugm) {
-      printf("%s:\nSARG:\n",text[22]);
-      printf("%35s (-a) = %s\n",text[23],addr);
-      printf("%35s (-b) = %s\n",text[71],uagent);
-      printf("%35s (-c) = %s\n",text[69],hexclude);
-      printf("%35s (-d) = %s\n",text[24],date);
-      printf("%35s (-e) = %s\n",text[41],email);
-      printf("%35s (-f) = %s\n",text[70],ConfigFile);
+      printf(_("Parameters:\n"));
+      printf(_("          Hostname or IP address (-a) = %s\n"),addr);
+      printf(_("                   Useragent log (-b) = %s\n"),uagent);
+      printf(_("                    Exclude file (-c) = %s\n"),hexclude);
+      printf(_("                 Date from-until (-d) = %s\n"),date);
+      printf(_("   Email address to send reports (-e) = %s\n"),email);
+      printf(_("                     Config file (-f) = %s\n"),ConfigFile);
       if(strcmp(df,"e") == 0)
-         printf("%35s (-g) = %s (dd/mm/yyyy)\n",text[25],text[26]);
+         printf(_("                     Date format (-g) = Europe (dd/mm/yyyy)\n"));
       if(strcmp(df,"u") == 0)
-         printf("%35s (-g) = %s (mm/dd/yyyy)\n",text[25],text[27]);
+         printf(_("                     Date format (-g) = USA (mm/dd/yyyy)\n"));
       if(strcmp(df,"w") == 0)
-         printf("%35s (-g) = %s (yyyy/ww)\n",text[25],text[85]);
-      if(iprel)
-         printf("%35s (-i) = %s\n",text[28],text[1]);
-       else
-         printf("%35s (-i) = %s\n",text[28],text[2]);
-      printf("%35s (-l) = %s\n",text[37],arq);
-      if(strcmp(Ip2Name,"yes") == 0)
-         printf("%35s (-n) = %s\n",text[65],text[1]);
-       else
-         printf("%35s (-n) = %s\n",text[65],text[2]);
-      printf("%35s (-o) = %s\n",text[38],outdir);
-      if(strcmp(UserIp,"yes") == 0)
-         printf("%35s (-p) = %s\n",text[29],text[1]);
-       else
-         printf("%35s (-p) = %s\n",text[29],text[2]);
-      printf("%35s (-s) = %s\n",text[30],site);
-      printf("%35s (-t) = %s\n",text[31],hm);
-      printf("%35s (-u) = %s\n",text[32],us);
-      printf("%35s (-w) = %s\n",text[34],tmp);
-      if(debug)
-         printf("%35s (-x) = %s\n",text[35],text[1]);
-       else
-         printf("%35s (-x) = %s\n",text[35],text[2]);
-      if(debugz)
-         printf("%35s (-z) = %s\n",text[36],text[1]);
-       else
-         printf("%35s (-z) = %s\n",text[36],text[2]);
-      printf("sarg %s: %s\n",text[73],VERSION);
-      printf("Language=%s\n\n",text[3]);
+         printf(_("                     Date format (-g) = Sites & Users (yyyy/ww)\n"));
+      printf(_("                       IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
+      for (iarq=0 ; iarq<NAccessLog ; iarq++)
+         printf(_("                       Input log (-l) = %s\n"),AccessLog[iarq]);
+      printf(_("              Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
+      printf(_("                      Output dir (-o) = %s\n"),outdir);
+      printf(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
+      printf(_("                   Accessed site (-s) = %s\n"),site);
+      printf(_("                            Time (-t) = %s\n"),hm);
+      printf(_("                            User (-u) = %s\n"),us);
+      printf(_("                   Temporary dir (-w) = %s\n"),tmp);
+      printf(_("                  Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
+      printf(_("                Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
+      printf(_("sarg version: %s\n"),VERSION);
    }
 
-   if(debug){
-      sprintf(msg,"sarg %s: %s",text[73],VERSION);
-      debuga(msg);
-   }
+   if(debug)
+      debuga(_("sarg version: %s\n"),VERSION);
+
+   maxopenfiles=MAX_OPEN_USER_FILES;
+#ifdef HAVE_RLIM_T
+   if (Ulimit[0] != '\0') {
+      struct rlimit rl;
+      long l1, l2;
+      int rc=0;
 
-   if (strlen(Ulimit) > 0) {
 #if defined(RLIMIT_NOFILE)
       getrlimit (RLIMIT_NOFILE, &rl);
 #elif defined(RLIMIT_OFILE)
       getrlimit (RLIMIT_OFILE, &rl);
+#else
+#warning "No rlimit resource for the number of open files"
 #endif
       l1 = rl.rlim_cur;
       l2 = rl.rlim_max;
 
-      if(Ulimit[0] != '\0') {
-         rl.rlim_cur = atol(Ulimit);
-         rl.rlim_max = atol(Ulimit);
- #if defined(RLIMIT_NOFILE)
-         if(setrlimit (RLIMIT_NOFILE, &rl) == -1) {
- #elif defined(RLIMIT_OFILE)
-         if(setrlimit (RLIMIT_OFILE, &rl) == -1) {
- #endif
-            sprintf(msg,"setrlimit error - %s\n",strerror(errno));
-            debuga(msg);
-         }
+      rl.rlim_cur = atol(Ulimit);
+      rl.rlim_max = atol(Ulimit);
+#if defined(RLIMIT_NOFILE)
+      rc=setrlimit (RLIMIT_NOFILE, &rl);
+#elif defined(RLIMIT_OFILE)
+      rc=setrlimit (RLIMIT_OFILE, &rl);
+#else
+#warning "No rlimit resource for the number of open files"
+#endif
+      if(rc == -1) {
+            debuga(_("setrlimit error - %s\n"),strerror(errno));
       }
 
-      if(debug) {
-         sprintf(msg,"Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING,l1,l2,rl.rlim_cur,rl.rlim_max);
-         debuga(msg);
-      }
+      if(debug)
+         debuga("Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING,l1,l2,rl.rlim_cur,rl.rlim_max);
    }
+#endif
 
-   if (UserTabFile[0] != '\0') {
-      if(debug) {
-         sprintf(msg,"%s: %s",text[86],UserTabFile);
-         debuga(msg);
-      }
-      if((fp_usr=fopen(UserTabFile,"r"))==NULL) {
-        fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[45],UserTabFile,strerror(errno));
-        exit(1);
-      }
-      nreg = lseek(fileno(fp_usr), 0, SEEK_END);
-      lseek(fileno(fp_usr), 0, 0);
-      if((userfile=(char *) malloc(nreg+100))==NULL){
-         fprintf(stderr, "%s ERROR: %s",argv[0],text[87]);
-         exit(1);
-      }
-      bzero(userfile,nreg+100);
-      strncat(userfile,":",1);
-      z1=0;
-      z2=1;
-      while(fgets(buf,MAXLEN,fp_usr)!=NULL) {
-         buf[strlen(buf)-1]='\0';
-         if(strstr(buf,"\r") != 0) buf[strlen(buf)-1]='\0';
-        if (getword_multisep(bufy,sizeof(bufy),buf,' ')<0) {
-           printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserTabFile);
-           exit(1);
-        }
-         for(z1=0; z1<=strlen(bufy); z1++) {
-            userfile[z2]=bufy[z1];
-            z2++;
-         }
-         strncat(userfile,":",1);
-         for(z1=0; z1<=strlen(buf); z1++) {
-            userfile[z2]=buf[z1];
-            z2++;
-         }
-         strncat(userfile,":",1);
-      }
-      fclose(fp_usr);
+   init_usertab(UserTabFile);
+
+   if (longline_prepare(&line)<0) {
+      debuga(_("Not enough memory to read a log file\n"));
+      exit(1);
    }
 
    sprintf ( sz_Download_Unsort , "%s/sarg/download.unsort", tmp);
-   bool_ShowReadStatistics = ( strcmp(ShowReadStatistics,"yes") == 0 ) ;
 
-   while(narq--) {
-      strcpy(arq,warq[iarq]);
-      iarq++;  
+   if(DataFile[0]=='\0') {
+      if((ReportType & REPORT_TYPE_DENIED) != 0) {
+         if((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) {
+            debuga(_("SARG: (log) Cannot open file: %s - %s\n"),denied_unsort,strerror(errno));
+            exit(1);
+         }
+      }
 
-   strcpy(arqtt,arq);
-   decomp(arq,zip,tmp);
-   if(debug) {
-      sprintf(msg, "%s: %s",text[7],arq);
-      debuga(msg);
+      if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
+         if((fp_authfail=MY_FOPEN(authfail_unsort,"w"))==NULL) {
+            debuga(_("SARG: (log) Cannot open file: %s - %s\n"),authfail_unsort,strerror(errno));
+            exit(1);
+         }
+      }
    }
 
-#if defined(HAVE_FOPEN64)
-   if((fp_in=fopen64(arq,"r"))==NULL) {
-#else
-   if((fp_in=fopen(arq,"r"))==NULL) {
-#endif
-     fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[8],arq,strerror(errno));
-     exit(1);
-   }
-   fgets(bufz,sizeof(bufz),fp_in);
-   if(!isalog && strncmp(bufz,"#Software: Mic",14) == 0) isalog++;
+   for (iarq=0 ; iarq<NAccessLog ; iarq++) {
+      strcpy(arq,AccessLog[iarq]);
 
-   if(strncmp(bufz,"*** SARG Log ***",16) == 0) {
-      if (getword(val2,sizeof(val2),arqtt,'-')<0 || getword(val2,sizeof(val2),arqtt,'_')<0 ||
-          getword(val3,sizeof(val3),arqtt,'-')<0 || getword(val3,sizeof(val3),arqtt,'_')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-         exit(1);
-      }
-      sprintf(period,"%s-%s",val2,val3);
-      sarglog=1;
-   } else lseek(fileno(fp_in), 0, 0);
-
-   if(strcmp(ParsedOutputLog, "no") != 0 && !sarglog) {
-      if(access(ParsedOutputLog,R_OK) != 0) {
-         sprintf(csort,"%s",ParsedOutputLog);
-         my_mkdir(csort);
+      strcpy(arqtt,arq);
+
+      if(strcmp(arq,"-")==0) {
+         if(debug)
+            debuga(_("Reading access log file: from stdin\n"));
+         fp_in=stdin;
+         from_stdin=1;
+      } else {
+         decomp(arq,zip,tmp);
+         if(debug)
+            debuga(_("Reading access log file: %s\n"),arq);
+         if((fp_in=MY_FOPEN(arq,"r"))==NULL) {
+            debuga(_("SARG: (log) Cannot open log file: %s - %s\n"),arq,strerror(errno));
+            exit(1);
+         }
+         from_stdin=0;
       }
-      sprintf(arq_log,"%s/sarg_temp.log",ParsedOutputLog);
-#if defined(HAVE_FOPEN64)
-      if((fp_log=fopen64(arq_log,"w"))==NULL) {
-#else
-      if((fp_log=fopen(arq_log,"w"))==NULL) {
-#endif
-        fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[8],arq_log,strerror(errno));
-        exit(1);
+      ilf=ILF_Unknown;
+      download_flag=0;
+      // pre-read the file only if we have to show stats
+      if(ShowReadStatistics && !from_stdin) {
+         size_t nread,i;
+         int skipcr=0;
+
+         recs1=0UL;
+         recs2=0UL;
+
+         while ((nread=fread(line.buffer,1,line.size,fp_in))>0) {
+            for (i=0 ; i<nread ; i++)
+               if (skipcr) {
+                  if (line.buffer[i]!='\n' && line.buffer[i]!='\r') {
+                     skipcr=0;
+                  }
+               } else {
+                  if (line.buffer[i]=='\n' || line.buffer[i]=='\r') {
+                     skipcr=1;
+                     recs1++;
+                  }
+               }
+         }
+         rewind(fp_in);
+         printf(_("SARG: Records in file: %lu, reading: %3.2f%%\r"),recs1,(float) 0);
+         fflush( stdout ) ;
       }
-      fputs("*** SARG Log ***\n",fp_log);
-   }
 
-   if(strstr(ReportType,"denied") != 0) {
-#if defined(HAVE_FOPEN64)
-      if((fp_denied=fopen64(tmp4,"w"))==NULL) {
-#else
-      if((fp_denied=fopen(tmp4,"w"))==NULL) {
-#endif
-         fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[45],tmp4,strerror(errno));
-         exit(1);
-      }
-   }
+      line.start=0;
+      line.end=0;
+      line.length=0;
+
+      while ((linebuf=longline_read(fp_in,&line))!=NULL) {
+         blen=strlen(linebuf);
+
+         if (ilf==ILF_Unknown) {
+            if(strncmp(linebuf,"#Software: Mic",14) == 0) {
+               fixendofline(linebuf);
+               if (debug)
+                  debuga(_("Log is from Microsoft ISA: %s\n"),linebuf);
+               ilf=ILF_Isa;
+               ilf_count[ilf]++;
+               continue;
+            }
 
-   if(DataFile[0]=='\0') {
-      if(strstr(ReportType,"denied") != 0 || strstr(ReportType,"auth_failures") != 0) {
-#if defined(HAVE_FOPEN64)
-         if((fp_authfail=fopen64(tmp6,"w"))==NULL) {
-#else
-         if((fp_authfail=fopen(tmp6,"w"))==NULL) {
-#endif
-            fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[45],tmp6,strerror(errno));
-            exit(1);
+            if(strncmp(linebuf,"*** SARG Log ***",16) == 0) {
+               getword_start(&gwarea,arqtt);
+               if (getword_skip(2000,&gwarea,'-')<0 || getword(val2,sizeof(val2),&gwarea,'_')<0 ||
+                   getword_skip(10,&gwarea,'-')<0 || getword(val3,sizeof(val3),&gwarea,'_')<0) {
+                  debuga(_("The name of the file is invalid: %s\n"),arq);
+                  exit(1);
+               }
+               sprintf(period,"%s-%s",val2,val3);
+               ilf=ILF_Sarg;
+               ilf_count[ilf]++;
+               continue;
+            }
          }
-      }
-   }
 
-   // pre-Read the file only if I have to show stats
-   if(bool_ShowReadStatistics) {
-      rewind(fp_in);
-      recs1=0;
-      recs2=0;
+         if(!fp_log && strcmp(ParsedOutputLog, "no") != 0 && ilf!=ILF_Sarg) {
+            if(access(ParsedOutputLog,R_OK) != 0) {
+               my_mkdir(ParsedOutputLog);
+            }
+            sprintf(arq_log,"%s/sarg_temp.log",ParsedOutputLog);
+            if((fp_log=MY_FOPEN(arq_log,"w"))==NULL) {
+               debuga(_("SARG: (log) Cannot open log file: %s - %s\n"),arq_log,strerror(errno));
+               exit(1);
+            }
+            fputs("*** SARG Log ***\n",fp_log);
+         }
 
-      while( fgets(bufz,sizeof(bufz),fp_in) != NULL ) recs1++;
-      rewind(fp_in);
-      printf("SARG: Records in file: " OFFSET_STRING ", reading: %3.2f%%\r",recs1,(float) 0);
-      fflush( stdout ) ;
-   }
-   
-   while(fgets(bufz,sizeof(bufz),fp_in)!=NULL) {
-       recs2++;
-       if( bool_ShowReadStatistics && ! --OutputNonZero) {
+         recs2++;
+         if( ShowReadStatistics && !from_stdin && ! --OutputNonZero) {
            perc = recs2 * 100 ;
            perc = perc / recs1 ;
-           printf("SARG: Records in file: " OFFSET_STRING ", reading: %3.2f%%\r",recs1,perc);
+           printf("SARG: Records in file: %lu, reading: %3.2f%%\r",recs1,perc);
            fflush (stdout);
            OutputNonZero = REPORT_EVERY_X_LINES ;
-        }
-        if(strlen(bufz) > MAXLEN-1) continue;
-        if(!bufz[0]) continue;
-        if(strstr(bufz,"HTTP/0.0") != 0) continue;
-        if(strstr(bufz,"logfile turned over") != 0) continue;
-        if(bufz[0] == ' ') continue;
-        if(strlen(bufz) < 58) continue;
-
-        // Record only hours usage which is required
-        tt = (time_t) strtoul( bufz, NULL, 10 );
-        t = localtime( &tt );
-
-        if( bsearch( &( t -> tm_wday ), weekdays.list, weekdays.len,
-                                        sizeof( int ), compar ) == NULL )
-          continue;
-
-        if( bsearch( &( t -> tm_hour ), hours.list, hours.len,
-                                        sizeof( int ), compar ) == NULL )
-          continue;
-
-        // exclude_string
-        exstring=0;
-        if(strlen(ExcludeString) > 0) {
-           strcpy(warea,bufz);
-           strcpy(html,ExcludeString);
-           while(strstr(html,":") != 0) {
-              if (getword_multisep(val1,sizeof(val1),html,':')<0) {
-                 printf("SARG: Maybe you have a broken record or garbage in your exclusion string.\n");
-                 exit(1);
-              }
-              if((str=(char *) strstr(warea,val1)) != (char *) NULL )
-                 exstring++;
-           }
-           if((str=(char *) strstr(warea,html)) != (char *) NULL )
-                 exstring++;
-        }
-        if(exstring) continue;
-
-        strcpy(bufy,bufz);
-        if ((str = strchr(bufz, '\n')) != NULL)
-           *str = '\0';          /* strip \n */       
-
-        totregsl++;
-        common=0;
-        if(debugm)
-                  printf("BUF=%s\n",bufz);
-
-        if(!sarglog && !isalog) {
-           if (getword(data,sizeof(data),bufz,' ')<0) {
-             printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
-             exit(1);
-           }
-           if((str=(char *) strstr(data, ".")) != (char *) NULL ) {
-              if((str=(char *) strstr(str+1, ".")) != (char *) NULL ) {
-                 strcpy(ip,data);
-                        strcpy(elap,"0");
-                        if(squid24) {
-                    if (getword(user,sizeof(user),bufz,' ')<0 || getword(none,sizeof(none),bufz,' ')<0) {
-                       printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                       exit(1);
-                    }
-                        } else {
-                       if (getword(none,sizeof(none),bufz,' ')<0 || getword(user,sizeof(user),bufz,' ')<0) {
-                       printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                       exit(1);
-                    }
-                 }
-                    if (getword(data,sizeof(data),bufz,']')<0 || getword(fun,sizeof(fun),bufz,'"')<0 ||
-                        getword(fun,sizeof(fun),bufz,' ')<0 || getword(url,sizeof(url),bufz,' ')<0 ||
-                     getword(code2,sizeof(code2),bufz,' ')<0 || getword(code2,sizeof(code2),bufz,' ')<0 ||
-                     getword(tam,sizeof(tam),bufz,' ')<0) {
-                    printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                    exit(1);
-                 }
-                 if((str=(char *) strstr(bufz, " ")) != (char *) NULL ) {
-                    if (getword(code,sizeof(code),bufz,' ')<0) {
-                       printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                       exit(1);
-                    }
-                 }
-                 else strcpy(code,bufz);
-
-                 if ((str = strchr(code, ':')) != NULL)
-                    *str = '/'; 
-
-                if(strcmp(tam,"\0") == 0)
-                   strcpy(tam,"0");
-
-                common++;
-                common_log=1;
-             }
-          }
-
-          if(!common) {
-             if (getword(elap,sizeof(elap),bufz,' ')<0) {
-             printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-             exit(1);
-          }
-             while(strcmp(elap,"") == 0 && strlen(bufz) > 0)
-                if (getword(elap,sizeof(elap),bufz,' ')<0) {
-                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                exit(1);
-             }
-              if(strlen(elap) < 1) continue;
-                 if (getword(ip,sizeof(ip),bufz,' ')<0){
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-              if (getword(code,sizeof(code),bufz,' ')<0){
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-              if (getword(tam,sizeof(tam),bufz,' ')<0){
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-              if (getword(fun,sizeof(fun),bufz,' ')<0){
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-              if (getword(url,sizeof(url),bufz,' ')<0){
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-//              while (strstr(bufz,"%20") != 0) {
-//                 getword(warea,bufz,' ');
-//                 strcat(url,warea);
-//              }
-              if (getword(user,sizeof(user),bufz,' ')<0){
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-              squid_log=1;
-              }
-        } else if(!isalog) {
-              if (getword(data,sizeof(data),bufz,' ')<0){
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(hora,sizeof(hora),bufz,' ')<0) {
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(user,sizeof(user),bufz,' ')<0) {
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(ip,sizeof(ip),bufz,' ')<0) {
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(url,sizeof(url),bufz,' ')<0){
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(tam,sizeof(tam),bufz,' ')<0){
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(code,sizeof(code),bufz,' ')<0){
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(elap,sizeof(elap),bufz,' ')<0){
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(smartfilter,sizeof(smartfilter),bufz,' ')<0){
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-        } else if(isalog) {
-           if(!i0) {
-              if (getword(val1,sizeof(val1),bufz,' ')<0){
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-              while(strstr(bufz,"\t") != 0) {
-                 if (getword(val1,sizeof(val1),bufz,'\t')<0){
-                    printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                    exit(1);
-                 }
-                 i0++;
-                 if(strcmp(val1,"c-ip") == 0) i1=i0;
-                 if(strcmp(val1,"cs-username") == 0) i2=i0;
-                 if(strcmp(val1,"date") == 0) i3=i0;
-                 if(strcmp(val1,"time") == 0) i4=i0;
-                 if(strcmp(val1,"time-taken") == 0) i5=i0;
-                 if(strcmp(val1,"sc-bytes") == 0) i6=i0;
-                 if(strcmp(val1,"cs-uri") == 0) i7=i0;
-                 if(strcmp(val1,"sc-status") == 0) i8=i0;
-              }
-           }
-           fgets(bufz,sizeof(bufz),fp_in);
-           strcpy(val1,bufz);
-           for(x=0; x<=i1-1; x++) {
-              if (getword3(ip,sizeof(ip),val1,'\t')<0) {
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-           }
-           strcpy(val1,bufz);
-           for(x=0; x<=i2-1; x++) {
-              if (getword3(user,sizeof(user),val1,'\t')<0) {
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-           }
-           strcpy(val1,bufz);
-           for(x=0; x<=i3-1; x++) {
-              if (getword3(data,sizeof(data),val1,'\t')<0) {
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-           }
-           strcpy(val1,bufz);
-           for(x=0; x<=i4-1; x++) {
-              if (getword3(hora,sizeof(hora),val1,'\t')<0) {
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-           }
-           strcpy(val1,bufz);
-           for(x=0; x<=i5-1; x++) {
-              if (getword3(elap,sizeof(elap),val1,'\t')<0) {
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-           }
-           strcpy(val1,bufz);
-           for(x=0; x<=i6-1; x++) {
-              if (getword3(tam,sizeof(tam),val1,'\t')<0) {
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-           }
-           strcpy(val1,bufz);
-           for(x=0; x<=i7-1; x++) {
-              if (getword3(url,sizeof(url),val1,'\t')<0) {
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-           }
-           strcpy(val1,bufz);
-           for(x=0; x<=i8-1; x++) {
-              if (getword3(code,sizeof(code),val1,'\t')<0) {
-                 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-                 exit(1);
-              }
-           }
-           
-           if(strcmp(code,"401") == 0 || strcmp(code,"403") == 0 || strcmp(code,"407") == 0) {
-              sprintf(val1,"DENIED/%s",code);
-              strcpy(code,val1);
-           }
-           if (getword(ano,sizeof(ano),data,'-')<0){
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(mes,sizeof(mes),data,'-')<0){
-              printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
-              exit(1);
-           }
-           strcpy(dia,data);
-           conv_month_name(mes);
-           sprintf(data," %s/%s/%s:%s",dia,mes,ano,hora);
-        }
-
-        if(strlen(user) > 150) {
-           totregsx++;
-           continue;
-        }
-
-       // include_users
-       if(strlen(IncludeUsers) > 0) {
-           sprintf(val1,":%s:",user);
-           if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL )
-              continue;
-       }
-
-        if(excode) {
-           if(vercode(code)) {
-              totregsx++;
-              continue;
-           }
-        }
-
-        if(testvaliduserchar(user))
-          continue;
-
-        if(strstr(user,"%20") != 0) {
-           if (getword(w,sizeof(w),user,'%')<0) {
-              printf("SARG: Maybe you have a broken user in your %s file.\n",arq);
-              exit(1);
-           }
-           strcpy(user,w);
-        }
-
-        while(strstr(user,"%5c") != 0) {
-           if (getword_multisep(w,sizeof(w),user,'%')<0){
-              printf("SARG: Maybe you have a broken user in your %s file.\n",arq);
-              exit(1);
-           }
-           strcpy(wuser,user+2);
-           sprintf(user,"%s.%s",w,wuser);
-        }
-
-       str=user;
-       for(z1=0; str[z1]; z1++) {
-          if(isalnum(str[z1]) || ispunct(str[z1])) {
-          if(str[z1]=='.') dotinuser++;
-          if(str[z1]=='?' || str[z1]=='.' || str[z1]==':' || str[z1]=='/' || str[z1]=='\\')
-             str[z1]='_';
-          }
-       }
-
-        strlow(user);
-        if(strncmp(NtlmUserFormat,"user",4) == 0) {
-           if(strstr(user,"_") != 0)
-              if (getword_multisep(warea,sizeof(warea),user,'_')<0){
-                 printf("SARG: Maybe you have a broken user in your %s file.\n",arq);
-                 exit(1);
-              }
-           if(strstr(user,"+") != 0)
-              if (getword_multisep(warea,sizeof(warea),user,'+')<0){
-                 printf("SARG: Maybe you have a broken user in your %s file.\n",arq);
-                 exit(1);
-              }
-        }
-
-        if(strstr(ReportType,"denied") != 0)
-           strcpy(urly,url);
-
-        if(strlen(DownloadSuffix)) {
-           suffix[0]='\0';
-           download_flag=0;
-           if(strncmp(url+strlen(url)-4,".",1) == 0)
-              strcpy(suffix,url+strlen(url)-3);
-           else strcpy(suffix,url+strlen(url)-4);
-           if(strstr(DownloadSuffix,suffix) != 0) {
-              strcpy(download_url,url);
-              download_flag=1;
-              download_count++;
-           }
-        }
-         
-        if (strchr(url,'/')) {
-           if (getword(w,sizeof(w),url,'/')<0){
-              printf("SARG: Maybe you have a broken url in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword(w,sizeof(w),url,'/')<0){
-              printf("SARG: Maybe you have a broken url in your %s file.\n",arq);
-              exit(1);
-           }
-           if (!strchr(url,'/')) {
-              totregsx++;
-              continue;
-          }
-        }
-
-        if(strcmp(LongUrl,"no") == 0) {
-           if (getword_multisep(w,sizeof(w),url,'/')<0){
-              printf("SARG: Maybe you have a broken url in your %s file.\n",arq);
-              exit(1);
-           }
-           strcpy(url,w);
-           if(strlen(url) > 512 && strstr(url,"%") != 0) {
-              if (getword_multisep(w,sizeof(w),url,'%')<0){
-                 printf("SARG: Maybe you have a broken url in your %s file.\n",arq);
-                 exit(1);
-              }
-              strcpy(url,w);
-           }
-        }
-
-        if(!sarglog) {
-           if(!common && !isalog) {
-              tt=atoi(data);
-              t=localtime(&tt);
-
-              strftime(tbuf2, 127, "%H%M", t);
-              if(strncmp(df,"u",1) == 0)
-                strftime(tbuf, 127, "%Y%b%d", t);
-              if(strncmp(df,"e",1) == 0)
-                strftime(tbuf, 127, "%d%b%Y", t);
-              if(strncmp(df,"w",1) == 0) {
-                 strcpy(IndexTree,"file");
-                strftime(tbuf, 127, "%Y.%U", t);
-              }
-
-              strftime(dia, 127, "%d/%m/%Y", t);
-              strftime(wdata, 127, "%Y%m%d", t);
-          
-              idata=atoi(wdata);
-
-              if(strncmp(df,"u",1)==0)
-                 strftime(dia, 127, "%m/%d/%Y", t);
-              sprintf(hora,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
-              } else {
-                 strcpy(wtemp,data+1);
-                 if (getword_multisep(data,sizeof(data),wtemp,':')<0){
-                 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-                 exit(1);
-              }
-              if (getword_multisep(hora,sizeof(hora),wtemp,' ')<0){
-                 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-                 exit(1);
-              }
-              if (getword_multisep(dia,sizeof(dia),data,'/')<0){
-                 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-                 exit(1);
-              }
-              if (getword_multisep(mes,sizeof(mes),data,'/')<0){
-                 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-                 exit(1);
-              }
-              if (getword_multisep(ano,sizeof(ano),data,'/')<0){
-                 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-                 exit(1);
-              }
-
-              if(strcmp(df,"u") == 0)
-                sprintf(tbuf,"%s%s%s",ano,mes,dia);
-              if(strcmp(df,"e") == 0)
-                sprintf(tbuf,"%s%s%s",dia,mes,ano);
-             builddia(dia,mes,ano,df,wdata);
-              idata=atoi(wdata);
-          }
-        } else {
-              if (getword_multisep(mes,sizeof(mes),data,'/')<0){
-              printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword_multisep(dia,sizeof(dia),data,'/')<0){
-              printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-              exit(1);
-           }
-           if (getword_multisep(ano,sizeof(ano),data,0)<0){
-              printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-              exit(1);
-           }
-        }
-
-        if(debugm)
-           printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil);
-
-        l=1;
-        if(strlen(us)>0){
-          if(strcmp(user,us)==0)
-             l=1;else l=0;
-       }
-           
-        if(l){
-           if(strlen(addr)>0){
-             if(strcmp(addr,ip)==0)
-                l=1;else l=0;
-          }
-           if(fhost) {
-//              l=vhexclude(excludefile,ip);
-              l=vhexclude(excludefile,url);
-              if(!l)
-                totregsx++;
-           }
-        }
-
-        if(l){
-           if(strlen(date) > 0){
-              if(idata >= dfrom && idata <= duntil)
-                l=1;else l=0;
-          }
-        }
-        if(l){
-           if(strlen(hm)>0) {
-              strcpy(whm,hora);
-              bzero(hmr,15);
-              chm++;
-              while(chm) {
-                 if (getword_multisep(warea,sizeof(warea),whm,':')<0){
-                    printf("SARG: Maybe you have a broken time in your %s file.\n",arq);
-                    exit(1);
-                 }
-                 strncat(hmr,warea,2);
-                 chm--;
-              }
-              strncat(hmr,whm,2);
-
-             if(atoi(hmr) >= atoi(hm) && atoi(hmr) <= atoi(hmf))
-                l=1;else l=0;
-          }
-        }
-        if(l){
-           if(strlen(site)>0){
-             if(strstr(url,site)!=0)
-                l=1;else l=0;
-          }
-        }
-
-        if(userip)
-           strcpy(user,ip);
-
-        if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) {
-           if(strcmp(RecordsWithoutUser,"ip") == 0)
-             strcpy(user,ip);
-           if(strcmp(RecordsWithoutUser,"ignore") == 0)
-             continue;
-           if(strcmp(RecordsWithoutUser,"everybody") == 0)
-             strcpy(user,"everybody");
-        }
-
-        if(dotinuser) {
-           str2=(char *)subs(user,"_",".");
-           strcpy(user,str2);
-           dotinuser=0;
-        }
-
-        if(puser) {
-           sprintf(wuser,":%s:",user);
-           if(strstr(userfile, wuser) == 0)
-              continue;
-        }
-        
-        if(l) {
-           if(fuser) {
-              l=vuexclude(excludeuser,user);
-              if(!l)
-                totregsx++;
-           }
-        }
-
-        if(l) {
-           if(userip)
-              fixip(user);
-        }
-
-        if(l&&max_elapsed) {
-           if(atol(elap)>max_elapsed) {
-              elap[0]='0';
-              elap[1]='\0';
-           }
-        }
-
-        if(l) {
-          if(strcmp(user,"-") !=0 && strlen(url) > 0 && strcmp(user," ") !=0 && strcmp(user,"") !=0 && strcmp(user,":") !=0){
-              if((str=(char *) strstr(bufz, "[SmartFilter:")) != (char *) NULL ) {
-                 str[strlen(str)-1]='\0';
-                 sprintf(smartfilter,"\"%s\"",str+1);
-              } else sprintf(smartfilter,"\"\"");
-
-              sprintf(bufz, "%s %s %s %s %s %s %s %s %s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter);
-
-#ifdef LEGACY_WRITE_USER
-              sprintf(tmp3,"%s/sarg/%s.unsort",tmp,user);
-#if defined(HAVE_FOPEN64)
-              if((fp_ou=fopen64(tmp3,"a"))==NULL) {
-#else
-              if((fp_ou=fopen(tmp3,"a"))==NULL) {
-#endif
-                fprintf(stderr, "%s: (zzzlog) %s: %s - %s\n",argv[0],text[9],tmp3,strerror(errno));
-                exit(1);
-              }
-              fputs(bufz,fp_ou);
+         }
+         if(blen < 58) continue;
+         if(strstr(linebuf,"HTTP/0.0") != 0) continue;
+         if(strstr(linebuf,"logfile turned over") != 0) continue;
+         if(linebuf[0] == ' ') continue;
+
+         // exclude_string
+         if(ExcludeString[0] != '\0') {
+            exstring=0;
+            getword_start(&gwarea,ExcludeString);
+            while(strchr(gwarea.current,':') != 0) {
+               if (getword_multisep(val1,sizeof(val1),&gwarea,':')<0) {
+                  debuga(_("Maybe you have a broken record or garbage in your exclusion string\n"));
+                  exit(1);
+               }
+               if((str=(char *) strstr(linebuf,val1)) != (char *) NULL )
+                  exstring++;
+            }
+            if((str=(char *) strstr(linebuf,gwarea.current)) != (char *) NULL )
+                  exstring++;
+            if(exstring) continue;
+         }
 
-#else
-                  if ( strcmp ( user , sz_Last_User ) != 0 ) {
-                        if ( fp_Write_User )
-                                fclose( fp_Write_User ) ;
-                        sprintf (tmp3, "%s/sarg/%s.unsort", tmp, user);
+         totregsl++;
+         if(debugm)
+            printf("BUF=%s\n",linebuf);
 
-#if defined(HAVE_FOPEN64)
-                        if ((fp_Write_User = fopen64 (tmp3, "a")) == NULL) {
-#else
-                        if ((fp_Write_User = fopen (tmp3, "a")) == NULL) {
-#endif
-                              fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], text[9], tmp3, strerror(errno));
-                                exit (1);
-                        }
-                        strcpy( sz_Last_User , user ) ;
+         if (ilf==ILF_Squid || ilf==ILF_Common || ilf==ILF_Unknown) {
+            getword_start(&gwarea,linebuf);
+            if (getword(data,sizeof(data),&gwarea,' ')<0) {
+               debuga(_("Maybe you have a broken time in your access.log file.\n"));
+               exit(1);
+            }
+            if((str=(char *) strchr(data, '.')) != (char *) NULL ) {
+               if((str=(char *) strchr(str+1, '.')) != (char *) NULL ) {
+                  strcpy(ip,data);
+                  strcpy(elap,"0");
+                  if(squid24) {
+                     if (getword(user,sizeof(user),&gwarea,' ')<0 || getword_skip(255,&gwarea,' ')<0) {
+                        debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+                        exit(1);
+                     }
+                  } else {
+                     if (getword_skip(255,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) {
+                        debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+                        exit(1);
+                     }
+                  }
+                  if (getword(data,sizeof(data),&gwarea,']')<0 || getword_skip(MAXLEN,&gwarea,'"')<0 ||
+                      getword(fun,sizeof(fun),&gwarea,' ')<0) {
+                     debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+                     exit(1);
+                  }
+                  if (getword_ptr(linebuf,&url,&gwarea,' ')<0) {
+                     debuga(_("Maybe you have a broken url in your %s file\n"),arq);
+                     exit(1);
+                  }
+                  if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0 ||
+                      getword(tam,sizeof(tam),&gwarea,' ')<0) {
+                     debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+                     exit(1);
+                  }
+                  if((str=(char *) strchr(gwarea.current, ' ')) != (char *) NULL ) {
+                     if (getword(code,sizeof(code),&gwarea,' ')<0) {
+                        debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+                        exit(1);
+                     }
+                  } else {
+                     if (getword(code,sizeof(code),&gwarea,'\0')<0) {
+                        debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+                        exit(1);
+                     }
                   }
-                  fputs (bufz, fp_Write_User);
-#endif
 
-              if(strcmp(ParsedOutputLog, "no") != 0 && !sarglog)
-                 fputs(bufz,fp_log);
+                  if ((str = strchr(code, ':')) != NULL)
+                     *str = '/';
 
-#ifdef LEGACY_WRITE_USER
-              fclose(fp_ou);
-#endif
-             totregsg++;
+                  if(strcmp(tam,"\0") == 0)
+                     strcpy(tam,"0");
 
-              if(download_flag && strstr(code,"DENIED") == 0) {
-                 ndownload = 1;
-                 sprintf(bufz, "%s %s %s %s %s\n",dia,hora,user,ip,download_url);
+                  ilf=ILF_Common;
+                  ilf_count[ilf]++;
+               }
+            }
 
-#ifdef LEGACY_WRITE_DOWNLOAD
-                 sprintf(tmp3,"%s/sarg/download.unsort",tmp);
-#if defined(HAVE_FOPEN64)
-                 if((fp_ou=fopen64(tmp3,"a"))==NULL) {
-#else
-                 if((fp_ou=fopen(tmp3,"a"))==NULL) {
-#endif
-                    fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[9],tmp3,strerror(errno));
-                    exit(1);
-                 }
-                 fputs(bufz,fp_ou);
-                 fclose(fp_ou);
-#else
-                 if ( ! fp_Download_Unsort ) {
-#if defined(HAVE_FOPEN64)
-                      if ((fp_Download_Unsort = fopen64 ( sz_Download_Unsort, "a")) == NULL) {
-#else
-                      if ((fp_Download_Unsort = fopen (sz_Download_Unsort, "a")) == NULL) {
-#endif
-                          fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], text[9], tmp3, strerror(errno));
-                          exit (1);
-                      }
-                 }
-                 fputs (bufz, fp_Download_Unsort);
+            if(ilf==ILF_Unknown || ilf==ILF_Squid) {
+               if (getword(elap,sizeof(elap),&gwarea,' ')<0) {
+                  debuga(_("Maybe you have a broken elapsed time in your %s file.\n"),arq);
+                  exit(1);
+               }
+               while(strcmp(elap,"") == 0 && gwarea.current[0] != '\0')
+                  if (getword(elap,sizeof(elap),&gwarea,' ')<0) {
+                     debuga(_("Maybe you have a broken elapsed time in your %s file.\n"),arq);
+                     exit(1);
+                  }
+               if(strlen(elap) < 1) continue;
+               if (getword(ip,sizeof(ip),&gwarea,' ')<0){
+                  debuga(_("Maybe you have a broken client IP address in your %s file.\n"),arq);
+                  exit(1);
+               }
+               if (getword(code,sizeof(code),&gwarea,' ')<0){
+                  debuga(_("Maybe you have a broken result code in your %s file.\n"),arq);
+                  exit(1);
+               }
+               if (getword(tam,sizeof(tam),&gwarea,' ')<0){
+                  debuga(_("Maybe you have a broken amount of data in your %s file.\n"),arq);
+                  exit(1);
+               }
+               if (getword(fun,sizeof(fun),&gwarea,' ')<0){
+                  debuga(_("Maybe you have a broken request method in your %s file.\n"),arq);
+                  exit(1);
+               }
+               if (getword_ptr(linebuf,&url,&gwarea,' ')<0){
+                  debuga(_("Maybe you have a broken url in your %s file\n"),arq);
+                  exit(1);
+               }
+               if (getword(user,sizeof(user),&gwarea,' ')<0){
+                  debuga(_("Maybe you have a broken user ID in your %s file.\n"),arq);
+                  exit(1);
+               }
+               ilf=ILF_Squid;
+               ilf_count[ilf]++;
+            }
+         }
+         if (ilf==ILF_Sarg) {
+            getword_start(&gwarea,linebuf);
+            if (getword(data,sizeof(data),&gwarea,'\t')<0){
+               debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+               exit(1);
+            }
+            if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
+               debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+               exit(1);
+            }
+            if (getword(user,sizeof(user),&gwarea,'\t')<0) {
+               debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+               exit(1);
+            }
+            if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
+               debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+               exit(1);
+            }
+            if (getword_ptr(linebuf,&url,&gwarea,'\t')<0){
+               debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
+               exit(1);
+            }
+            if (getword(tam,sizeof(tam),&gwarea,'\t')<0){
+               debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+               exit(1);
+            }
+            if (getword(code,sizeof(code),&gwarea,'\t')<0){
+               debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+               exit(1);
+            }
+            if (getword(elap,sizeof(elap),&gwarea,'\t')<0){
+               debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+               exit(1);
+            }
+            if (getword(smartfilter,sizeof(smartfilter),&gwarea,'\0')<0){
+               debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+               exit(1);
+            }
+         }
+         if (ilf==ILF_Isa) {
+            if (linebuf[0] == '#') {
+               int ncols,cols[ISACOL_Last];
+
+               fixendofline(linebuf);
+               getword_start(&gwarea,linebuf);
+               // remove the #Fields: column at the beginning of the line
+               if (getword_skip(1000,&gwarea,' ')<0){
+                  debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
+                  exit(1);
+               }
+               for (ncols=0 ; ncols<ISACOL_Last ; ncols++) cols[ncols]=-1;
+               ncols=0;
+               while(gwarea.current[0] != '\0') {
+                  if (getword(val1,sizeof(val1),&gwarea,'\t')<0){
+                     debuga(_("Maybe you have a broken record or garbage in your %s file.\n"),arq);
+                     exit(1);
+                  }
+                  if(strcmp(val1,"c-ip") == 0) cols[ISACOL_Ip]=ncols;
+                  if(strcmp(val1,"cs-username") == 0) cols[ISACOL_UserName]=ncols;
+                  if(strcmp(val1,"date") == 0) cols[ISACOL_Date]=ncols;
+                  if(strcmp(val1,"time") == 0) cols[ISACOL_Time]=ncols;
+                  if(strcmp(val1,"time-taken") == 0) cols[ISACOL_TimeTaken]=ncols;
+                  if(strcmp(val1,"sc-bytes") == 0) cols[ISACOL_Bytes]=ncols;
+                  if(strcmp(val1,"cs-uri") == 0) cols[ISACOL_Uri]=ncols;
+                  if(strcmp(val1,"sc-status") == 0) cols[ISACOL_Status]=ncols;
+                  ncols++;
+               }
+               if (cols[ISACOL_Ip]>=0) {
+                  isa_ncols=ncols;
+                  for (ncols=0 ; ncols<ISACOL_Last ; ncols++)
+                     isa_cols[ncols]=cols[ncols];
+               }
+               continue;
+            }
+            if (!isa_ncols) continue;
+            getword_start(&gwarea,linebuf);
+            for (x=0 ; x<isa_ncols ; x++) {
+               if (getword_ptr(linebuf,&str,&gwarea,'\t')<0) {
+                  debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
+                  exit(1);
+               }
+               if (x==isa_cols[ISACOL_Ip]) {
+                  if (strlen(str)>=sizeof(ip)) {
+                     debuga(_("Maybe you have a broken IP in your %s file\n"),arq);
+                     exit(1);
+                  }
+                  strcpy(ip,str);
+               } else if (x==isa_cols[ISACOL_UserName]) {
+                  if (strlen(str)>=sizeof(user)) {
+                     debuga(_("Maybe you have a broken user in your %s file\n"),arq);
+                     exit(1);
+                  }
+                  strcpy(user,str);
+               } else if (x==isa_cols[ISACOL_Date]) {
+                  if (strlen(str)>=sizeof(data)) {
+                     debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+                     exit(1);
+                  }
+                  strcpy(data,str);
+               } else if (x==isa_cols[ISACOL_Time]) {
+                  if (strlen(str)>=sizeof(hora)) {
+                     debuga(_("Maybe you have a broken time in your %s file\n"),arq);
+                     exit(1);
+                  }
+                  strcpy(hora,str);
+               } else if (x==isa_cols[ISACOL_TimeTaken]) {
+                  if (strlen(str)>=sizeof(elap)) {
+                     debuga(_("Maybe you have a broken download duration in your %s file\n"),arq);
+                     exit(1);
+                  }
+                  strcpy(elap,str);
+               } else if (x==isa_cols[ISACOL_Bytes]) {
+                  if (strlen(str)>=sizeof(tam)) {
+                     debuga(_("Maybe you have a broken download size in your %s file\n"),arq);
+                     exit(1);
+                  }
+                  strcpy(tam,str);
+               } else if (x==isa_cols[ISACOL_Uri]) {
+                  url=str;
+               } else if (x==isa_cols[ISACOL_Status]) {
+                  if (strlen(str)>=sizeof(code)) {
+                     debuga(_("Maybe you have a broken access code in your %s file\n"),arq);
+                     exit(1);
+                  }
+                  strcpy(code,str);
+               }
+            }
+
+            if(strcmp(code,"401") == 0 || strcmp(code,"403") == 0 || strcmp(code,"407") == 0) {
+               sprintf(val1,"DENIED/%s",code);
+               strcpy(code,val1);
+            }
+            getword_start(&gwarea,data);
+            if (getword(ano,sizeof(ano),&gwarea,'-')<0){
+               debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
+               exit(1);
+            }
+            if (getword(mes,sizeof(mes),&gwarea,'-')<0){
+               debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
+               exit(1);
+            }
+            if (getword(dia,sizeof(dia),&gwarea,'\0')<0){
+               debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
+               exit(1);
+            }
+            conv_month_name(mes);
+            sprintf(data," %s/%s/%s:%s",dia,mes,ano,hora);
+         }
+
+         if(ilf==ILF_Squid) {
+            tt=atoi(data);
+            t=localtime(&tt);
+
+            strftime(tbuf2, sizeof(tbuf2), "%H%M", t);
+            sprintf(mes,"%d",t->tm_mon+1);
+            conv_month_name(mes);
+            if(strncmp(df,"u",1) == 0)
+               sprintf(tbuf, "%04d%s%02d", t->tm_year+1900, mes, t->tm_mday);
+            if(strncmp(df,"e",1) == 0)
+               sprintf(tbuf, "%02d%s%04d", t->tm_mday, mes, t->tm_year+1900);
+            if(strncmp(df,"w",1) == 0) {
+               IndexTree=INDEX_TREE_FILE;
+               strftime(tbuf, sizeof(tbuf), "%Y.%U", t);
+            }
+
+            strftime(wdata, sizeof(wdata), "%Y%m%d", t);
+            idata=atoi(wdata);
+
+            if(strncmp(df,"u",1)==0)
+               strftime(dia, sizeof(dia), "%m/%d/%Y", t);
+            else
+               strftime(dia, sizeof(dia), "%d/%m/%Y", t);
+            sprintf(hora,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
+         } else if(ilf==ILF_Common || ilf==ILF_Isa) {
+            getword_start(&gwarea,data+1);
+            if (getword_multisep(data,sizeof(data),&gwarea,':')<0){
+               debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+               exit(1);
+            }
+            if (getword_multisep(hora,sizeof(hora),&gwarea,' ')<0){
+               debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+               exit(1);
+            }
+            getword_start(&gwarea,data);
+            if (getword(dia,sizeof(dia),&gwarea,'/')<0){
+               debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+               exit(1);
+            }
+            if (getword(mes,sizeof(mes),&gwarea,'/')<0){
+               debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+               exit(1);
+            }
+            if (getword(ano,sizeof(ano),&gwarea,'/')<0){
+               debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+               exit(1);
+            }
+
+            if(strcmp(df,"u") == 0)
+               snprintf(tbuf,sizeof(tbuf),"%s%s%s",ano,mes,dia);
+            if(strcmp(df,"e") == 0)
+               snprintf(tbuf,sizeof(tbuf),"%s%s%s",dia,mes,ano);
+            builddia(dia,mes,ano,df,wdata);
+            idata=atoi(wdata);
+            tt=computedate(ano,mes,dia);
+            t=localtime(&tt);
+         } else if (ilf==ILF_Sarg) {
+            getword_start(&gwarea,data);
+            if(strcmp(df,"u") == 0) {
+               if (getword(mes,sizeof(mes),&gwarea,'/')<0){
+                  debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+                  exit(1);
+               }
+               if (getword(dia,sizeof(dia),&gwarea,'/')<0){
+                  debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+                  exit(1);
+               }
+            } else {
+               if (getword(dia,sizeof(dia),&gwarea,'/')<0){
+                  debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+                  exit(1);
+               }
+               if (getword(mes,sizeof(mes),&gwarea,'/')<0){
+                  debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+                  exit(1);
+               }
+            }
+            if (getword(ano,sizeof(ano),&gwarea,0)<0){
+               debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+               exit(1);
+            }
+            snprintf(wdata,9,"%s%s%s",ano,mes,dia);
+            idata=atoi(wdata);
+            tt=computedate(ano,mes,dia);
+            t=localtime(&tt);
+         }
+
+         if(debugm)
+            printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil);
+
+         if(date[0] != '\0'){
+            if(idata < dfrom || idata > duntil) continue;
+         }
+
+         // Record only hours usage which is required
+         if( bsearch( &( t -> tm_wday ), weekdays.list, weekdays.len,
+                                          sizeof( int ), compar ) == NULL )
+            continue;
+
+         if( bsearch( &( t -> tm_hour ), hours.list, hours.len,
+                                          sizeof( int ), compar ) == NULL )
+            continue;
+
+
+         if(strlen(user) > MAX_USER_LEN) {
+            if (debugm) printf(_("User ID too long: %s\n"),user);
+            totregsx++;
+            continue;
+         }
+
+         // include_users
+         if(IncludeUsers[0] != '\0') {
+            sprintf(val1,":%s:",user);
+            if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL )
+               continue;
+         }
+
+         if(vercode(code)) {
+            if (debugm) printf(_("Excluded code: %s\n"),code);
+            totregsx++;
+            continue;
+         }
+
+         if(testvaliduserchar(user))
+            continue;
+
+#if 0
+         if((str = strstr(user,"%20")) != NULL) {
+            /*
+            This is a patch introduced to solve bug #1624251 reported at sourceforge but
+            the side effect is to truncate the name at the first space and merge the reports
+            of people whose name is identical up to the first space.
+
+            The old code used to truncate the user name at the first % if a %20 was
+            found anywhere in the string. That means the string could be truncated
+            at the wrong place if another % occured before the %20. This new code should
+            avoid that problem and only truncate at the space. There is no bug
+            report indicating that anybody noticed this.
+            */
+            *str='\0';
+         }
+
+         /*
+         Code prior to 2.2.7 used to replace any %xx by a dot as long as a %5c was
+         found in the user name.
+         */
+         while((str = strstr(user,"%5c")) != NULL) {
+            *str='.';
+            for (x=3 ; str[x] ; x++) str[x-2]=str[x];
+         }
 #endif
-              }
-
-              if(strstr(ReportType,"denied") != 0 || strstr(ReportType,"auth_failures") != 0) {
-                 if(strstr(code,"DENIED/403") != 0) {
-                    sprintf(bufz, "%s %s %s %s %s\n",dia,hora,user,ip,urly);
-                    fputs(bufz,fp_denied);
-                    denied_count++;
-                 }
-                 if(strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0) {
-                    sprintf(bufz, "%s %s %s %s %s\n",dia,hora,user,ip,urly);
-                    if(fp_authfail)
-                       fputs(bufz,fp_authfail);
-                    authfail_count++;
-                 }
-              }
-       
-              if((!totper || idata<mindate) && !sarglog){
-                    totper++;
-                 mindate=idata;
-                 sprintf(period,"%s-",tbuf);
-                 sprintf(per_hour,"%s-",tbuf2);
-                 if(strlen(date)>0)
-                    fixper(tbuf, period, cduntil);
-                 if(debugz){
-                    debugaz("tbuf",tbuf);
-                    debugaz("period",period);
-                 }
-             }
-           }
-
-           if(debugm){
-              printf("IP=\t%s\n",ip);
-              printf("USER=\t%s\n",user);
-              printf("ELAP=\t%s\n",elap);
-              printf("DATE=\t%s\n",dia);
-              printf("TIME=\t%s\n",hora);
-              printf("FUNC=\t%s\n",fun);
-              printf("URL=\t%s\n",url);
-              printf("CODE=\t%s\n",code);
-              printf("LEN=\t%s\n",tam);
-          }
-        }
+
+         urly=url;
+
+         if(ilf!=ILF_Sarg) {
+            /*
+            The full URL is not saved in sarg log. There is no point in testing the URL to detect
+            a downloaded file.
+            */
+            download_flag=is_download_suffix(url);
+            if (download_flag) {
+               download_url=url;
+               download_count++;
+            }
+         } else
+            download_flag=0;
+
+         // remove any protocol:// at the beginning of the URL
+         if ((str = strchr(url,'/')) != NULL && str[1] == '/') {
+            int i;
+
+            str+=2;
+            for (i=0 ; str[i] ; i++)
+               url[i]=str[i];
+            url[i]='\0';
+         }
+
+         if(!LongUrl) {
+            url_hostname(url,hostname,sizeof(hostname));
+            url=hostname;
+         }
+
+         if(url[0] == '\0') continue;
+
+         if(addr[0] != '\0'){
+            if(strcmp(addr,ip)!=0) continue;
+         }
+         if(fhost) {
+            if(!vhexclude(url)) {
+               if (debugm) printf(_("Excluded site: %s\n"),url);
+               totregsx++;
+               continue;
+            }
+         }
+
+         if(hm[0] != '\0') {
+            hmr[0]='\0';
+            chm++;
+            getword_start(&gwarea,hora);
+            while(chm) {
+               if (getword_multisep(warea,sizeof(warea),&gwarea,':')<0){
+                  debuga(_("Maybe you have a broken time in your %s file\n"),arq);
+                  exit(1);
+               }
+               strncat(hmr,warea,2);
+               chm--;
+            }
+            strncat(hmr,gwarea.current,2);
+
+            if(atoi(hmr) < atoi(hm) || atoi(hmr) > atoi(hmf)) continue;
+         }
+
+         if(site[0] != '\0'){
+            if(strstr(url,site)==0) continue;
+         }
+
+         if(userip) {
+            strcpy(user,ip);
+            id_is_ip=1;
+         } else {
+            id_is_ip=0;
+            if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) {
+               if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
+                  strcpy(user,ip);
+                  id_is_ip=1;
+               }
+               if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE)
+                  continue;
+               if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY)
+                  strcpy(user,"everybody");
+            } else {
+               strlow(user);
+               if(NtlmUserFormat == NTLMUSERFORMAT_USER) {
+                  if((str = strchr(user,'_')) != 0) {
+                     strcpy(warea,str+1);
+                     strcpy(user,warea);
+                  }
+                  if((str = strchr(user,'+')) != 0) {
+                     strcpy(warea,str+1);
+                     strcpy(user,warea);
+                  }
+               }
+            }
+         }
+
+         if(us[0] != '\0'){
+            if(strcmp(user,us)!=0) continue;
+         }
+
+         if(puser) {
+            sprintf(wuser,":%s:",user);
+            if(strstr(userfile, wuser) == 0)
+               continue;
+         }
+
+         if(fuser) {
+            if(!vuexclude(user)) {
+               if (debugm) printf(_("Excluded user: %s\n"),user);
+               totregsx++;
+               continue;
+            }
+         }
+
+         if(strcmp(user,"-") ==0 || strcmp(user," ") ==0 || strcmp(user,"") ==0 || strcmp(user,":") ==0)
+            continue;
+
+         if(max_elapsed) {
+            if(atol(elap)>max_elapsed) {
+               elap[0]='0';
+               elap[1]='\0';
+            }
+         }
+
+         if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) {
+            fixendofline(str);
+            sprintf(smartfilter,"\"%s\"",str+1);
+         } else sprintf(smartfilter,"\"\"");
+
+         nopen=0;
+         prev_ufile=NULL;
+         for (ufile=first_user_file ; ufile && strcmp(user,ufile->user->id)!=0 ; ufile=ufile->next) {
+            prev_ufile=ufile;
+            if (ufile->file) nopen++;
+         }
+         if (!ufile) {
+            ufile=malloc(sizeof(*ufile));
+            if (!ufile) {
+               debuga(_("Not enough memory to store the user %s\n"),user);
+               exit(1);
+            }
+            memset(ufile,0,sizeof(*ufile));
+            ufile->next=first_user_file;
+            first_user_file=ufile;
+            uinfo=userinfo_create(user);
+            ufile->user=uinfo;
+            uinfo->id_is_ip=id_is_ip;
+         } else {
+            if (prev_ufile) {
+               prev_ufile->next=ufile->next;
+               ufile->next=first_user_file;
+               first_user_file=ufile;
+            }
+         }
+
+         if (ufile->file==NULL) {
+            if (nopen>=maxopenfiles) {
+               x=0;
+               for (ufile1=first_user_file ; ufile1 ; ufile1=ufile1->next) {
+                  if (ufile1->file!=NULL) {
+                     if (x>=maxopenfiles) {
+                        fclose(ufile1->file);
+                        ufile1->file=NULL;
+                     }
+                     x++;
+                  }
+               }
+            }
+            if (snprintf (tmp3, sizeof(tmp3), "%s/sarg/%s.unsort", tmp, ufile->user->filename)>=sizeof(tmp3)) {
+               debuga(_("Temporary user file name too long: %s/sarg/%s.unsort\n"), tmp, ufile->user->filename);
+               exit(1);
+            }
+            if ((ufile->file = MY_FOPEN (tmp3, "a")) == NULL) {
+               debuga(_("(log) Cannot open temporary file: %s - %s\n"), tmp3, strerror(errno));
+               exit (1);
+            }
+         }
+
+         /*if ( strcmp ( user , sz_Last_User ) != 0 ) {
+            if ( fp_Write_User )
+               fclose( fp_Write_User ) ;
+            sprintf (tmp3, "%s/sarg/%s.unsort", tmp, user);
+
+            if ((fp_Write_User = MY_FOPEN (tmp3, "a")) == NULL) {
+               fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], _("Cannot open temporary file"), tmp3, strerror(errno));
+               exit (1);
+            }
+            strcpy( sz_Last_User , user ) ;
+         }*/
+         fprintf(ufile->file, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,ip,url,tam,code,elap,smartfilter);
+
+         if(fp_log && ilf!=ILF_Sarg)
+            fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter);
+
+         totregsg++;
+
+         if(!dataonly && download_flag && download_url && strstr(code,"DENIED") == 0) {
+            ndownload = 1;
+
+            if ( ! fp_Download_Unsort ) {
+               if ((fp_Download_Unsort = MY_FOPEN ( sz_Download_Unsort, "a")) == NULL) {
+                  debuga(_("(log) Cannot open temporary file: %s - %s\n"),tmp3, strerror(errno));
+                  exit (1);
+               }
+            }
+            fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,download_url);
+         }
+
+         if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
+            if(fp_denied && strstr(code,"DENIED/403") != 0) {
+               fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly);
+               denied_count++;
+            }
+            if(fp_authfail && (strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0)) {
+               fprintf(fp_authfail, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly);
+               authfail_count++;
+            }
+         }
+
+         if((!totper || idata<mindate) && ilf!=ILF_Sarg){
+            totper++;
+            mindate=idata;
+            sprintf(period,"%s-",tbuf);
+            strcpy(start_hour,tbuf2);
+            if(date[0] != '\0')
+               fixper(tbuf, period, cduntil);
+            if(debugz){
+               debugaz("tbuf",tbuf);
+               debugaz("period",period);
+            }
+         }
+
+         if(debugm){
+            printf("IP=\t%s\n",ip);
+            printf("USER=\t%s\n",user);
+            printf("ELAP=\t%s\n",elap);
+            printf("DATE=\t%s\n",dia);
+            printf("TIME=\t%s\n",hora);
+            printf("FUNC=\t%s\n",fun);
+            printf("URL=\t%s\n",url);
+            printf("CODE=\t%s\n",code);
+            printf("LEN=\t%s\n",tam);
+         }
+      }
+      if (!from_stdin) {
+         fclose(fp_in);
+         if( ShowReadStatistics )
+            printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs1, (float) 100 );
       }
-      if( bool_ShowReadStatistics )
-        printf("SARG: Records in file: " OFFSET_STRING ", reading: %3.2f%%\n",recs1, (float) 100 );
    }
 
-   if ( fp_Download_Unsort ) 
+   longline_free(&line);
+   if ( fp_Download_Unsort )
      fclose (fp_Download_Unsort);
 
-   if (fp_Write_User) 
-     fclose (fp_Write_User);
+   for (ufile=first_user_file ; ufile ; ufile=ufile1) {
+      ufile1=ufile->next;
+      if (ufile->file!=NULL) fclose(ufile->file);
+      free(ufile);
+   }
+
+   free_download();
+   free_excludecodes();
+   free_exclude();
 
    if(debug) {
-      sprintf(msg, "   %s: %ld, %s: %ld, %s: %ld",text[10],totregsl,text[11],totregsg,text[68],totregsx);
-      debuga(msg);
-  
-      if((common_log) && (squid_log))
-         debuga(text[12]);
-  
-      if((common_log) && (!squid_log)) 
-         debuga(text[13]);
-      if((!common_log) && (squid_log))
-         debuga(text[14]);
-
-      if(sarglog)
-         debuga(text[124]);
-      if((!common_log) && (!squid_log) && (!sarglog) && (!isalog)) {
+      int totalcount=0;
+
+      for (ilf=0 ; ilf<ILF_Last ; ilf++) totalcount+=ilf_count[ilf];
+
+      debuga(_("   Records read: %ld, written: %ld, excluded: %ld\n"),totregsl,totregsg,totregsx);
+
+      if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]>0)
+         debuga(_("Log with mixed records format (squid and common log)\n"));
+
+      if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]==0)
+         debuga(_("Common log format\n"));
+
+      if(ilf_count[ILF_Common]==0 && ilf_count[ILF_Squid]>0)
+         debuga(_("Squid log format\n"));
+
+      if(ilf_count[ILF_Sarg]>0)
+         debuga(_("Sarg log format\n"));
+
+      if(totalcount==0) {
          if(!totregsg) {
-            fprintf(stderr, "SARG: %s\n",text[16]);
-            fprintf(stderr, "SARG: %s\n",text[21]);
-        } else fprintf(stderr, "SARG: %s\n",text[15]);
-         bzero(msg,sizeof(msg));
-         fclose(fp_in);
-//         fclose(fp_ou);
+            debuga(_("No records found\n"));
+            debuga(_("End\n"));
+         } else debuga(_("Log with invalid format\n"));
          if(fp_denied)
             fclose(fp_denied);
          if(fp_authfail)
             fclose(fp_authfail);
-         unlink(tmp4);
-         unlink(tmp6);
+         userinfo_free();
+         if(userfile)
+            free(userfile);
+         close_usertab();
+         unlink(denied_unsort);
+         unlink(authfail_unsort);
          unlink(tmp3);
          exit(0);
       }
    }
+
    if(!totregsg){
-      fprintf(stderr, "SARG: %s\n",text[16]);
-      fprintf(stderr, "SARG: %s\n",text[21]);
-      fclose(fp_in);
+      debuga(_("No records found\n"));
+      debuga(_("End\n"));
 //      fclose(fp_ou);
       if(fp_denied)
          fclose(fp_denied);
       if(fp_authfail)
          fclose(fp_authfail);
+      userinfo_free();
+      if(userfile)
+         free(userfile);
+      close_usertab();
       exit(0);
    }
 
-   if(date[0] == '\0' && !sarglog) {
+   if(date[0] == '\0' && ilf_count[ILF_Sarg]==0) {
       strcat(period,tbuf);
-      strcat(per_hour,tbuf2);
    }
 
    if(debugz){
@@ -1550,226 +1558,139 @@ int main(argc,argv)
       debugaz("period",period);
    }
 
-   if(debug){
-     sprintf(msg, "%s: %s",text[17],period);
-     debuga(msg);
-   }
+   if(debug)
+      debuga(_("Period: %s\n"),period);
 
-   fclose(fp_in);
 //   fclose(fp_ou);
    if(fp_denied)
       fclose(fp_denied);
    if(fp_authfail)
       fclose(fp_authfail);
 
-   if(strcmp(ParsedOutputLog, "no") != 0 && !sarglog) {
-      fclose(fp_log); 
-      strcpy(val1,period);
-      if (getword_multisep(val2,sizeof(val2),val1,'-')<0){
-         printf("SARG: Maybe you have a broken date range definition.\n");
+   if(fp_log != NULL) {
+      fclose(fp_log);
+      strcpy(end_hour,tbuf2);
+      getword_start(&gwarea,period);
+      if (getword(val2,sizeof(val2),&gwarea,'-')<0){
+         debuga(_("Maybe you have a broken date range definition.\n"));
          exit(1);
       }
-      if (getword_multisep(val3,sizeof(val3),per_hour,'-')<0){
-         printf("SARG: Maybe you have a broken date range definition.\n");
+      if (getword(val1,sizeof(val1),&gwarea,'\0')<0){
+         debuga(_("Maybe you have a broken date range definition.\n"));
          exit(1);
       }
-      sprintf(val4,"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,val3,val1,per_hour);
-      rename(arq_log,val4);
-      strcpy(arq_log,val4);
-
-      if(strcmp(ParsedOutputLogCompress,"nocompress") != 0) {
-         sprintf(val1,"%s %s",ParsedOutputLogCompress,arq_log);
-         cstatus=system(val1);
-         if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-            fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
-            fprintf(stderr, "SARG: command: %s\n",val1);
-            exit(1);
+      sprintf(val4,"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,start_hour,val1,end_hour);
+      if (rename(arq_log,val4)) {
+         debuga(_("failed to rename %s to %s - %s\n"),arq_log,val4,strerror(errno));
+      } else {
+         strcpy(arq_log,val4);
+
+         if(strcmp(ParsedOutputLogCompress,"nocompress") != 0 && ParsedOutputLogCompress[0] != '\0') {
+            /*
+            No double quotes around ParsedOutputLogCompress because it may contain command line options. If double quotes are
+            necessary around the command name, put them in the configuration file.
+            */
+            sprintf(val1,"%s \"%s\"",ParsedOutputLogCompress,arq_log);
+            cstatus=system(val1);
+            if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+               debuga(_("command return status %d\n"),WEXITSTATUS(cstatus));
+               debuga(_("command: %s\n"),val1);
+               exit(1);
+            }
          }
       }
-
-      if(debug) {
-         sprintf(msg,"%s %s",text[123],arq_log);
-         debuga(msg);
-      }
+      if(debug)
+         debuga(_("Sarg parsed log saved as %s\n"),arq_log);
    }
 
-   if(strstr(ReportType,"denied") != 0) {
-      sprintf(csort,"sort -T %s -k 3,3 -k 5,5 -o '%s' '%s'",tmp,tmp5,tmp4);
+   if(DataFile[0] == '\0' && (ReportType & REPORT_TYPE_DENIED) != 0) {
+      sprintf(csort,"sort -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,denied_sort,denied_unsort);
       cstatus=system(csort);
       if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-         fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
-         fprintf(stderr, "SARG: sort command: %s\n",csort);
+         debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+         debuga(_("sort command: %s\n"),csort);
          exit(1);
       }
-      unlink(tmp4);
+      unlink(denied_unsort);
    }
+
    sort_users_log(tmp, debug);
 
-   if(strlen(DataFile) > 0)
+   if(DataFile[0] != '\0')
       data_file(tmp);
    else
       gerarel();
 
    unlink(tmp2);
-   if(strstr(ReportType,"denied") != 0)
-      unlink(tmp5);
-   if((strlen(zip) > 0 && strcmp(zip,"zcat") !=0)) {
-      recomp(arq, zip); }
+   if((ReportType & REPORT_TYPE_DENIED) != 0)
+      unlink(denied_sort);
+
+   if(zip[0] != '\0' && strcmp(zip,"zcat") !=0) {
+      recomp(arq, zip);
+   }
 //   else  unlink(arq);
 
    if(strcmp(tmp,"/tmp") != 0) {
-      sprintf(tmp4,"rm -rf %s",tmp);
-      system(tmp4);
+      unlinkdir(tmp,0);
    }
 
-   if(excludecode)
-      free(excludecode);
+   userinfo_free();
    if(userfile)
       free(userfile);
-   if(excludefile)
-      free(excludefile);
-   if(excludeuser)
-      free(excludeuser);
+   close_usertab();
 
    if(debug)
-      debuga(text[21]);
+      debuga(_("End\n"));
 
    exit(0);
 
 }
 
 
-void getusers(char *pwdfile, int debug)
+static void getusers(const char *pwdfile, int debug)
 {
 
    FILE *fp_usr;
    char buf[255];
-   char Msg[255];
-   char user[255];
-   unsigned long int nreg=0;
+   char *str;
+   long int nreg=0;
 
-   if(debug) {
-      sprintf(Msg,"%s: %s",text[60],pwdfile);
-      debuga(Msg);
-   }
+   if(debug)
+      debuga(_("Loading password file from: %s\n"),pwdfile);
 
    if ((fp_usr = fopen(pwdfile, "r")) == NULL) {
-      fprintf(stderr, "SARG: (getusers) %s: %s - %s\n",text[45],pwdfile,strerror(errno));
-      exit(1);
-   }
-
-   nreg = lseek(fileno(fp_usr), (off_t)0, SEEK_END);
-   nreg = nreg+5000; 
-   lseek(fileno(fp_usr), (off_t)0, 0);
-
-   if((userfile=(char *) malloc(nreg))==NULL){
-      fprintf(stderr, "SARG: %s (%ld):\n",text[59],nreg);
+      debuga(_("(getusers) Cannot open file: %s - %s\n"),pwdfile,strerror(errno));
       exit(1);
    }
 
-   bzero(userfile,nreg);
-   sprintf(userfile,":");
-
-   while(fgets(buf,255,fp_usr)!=NULL) {
-     if (getword_multisep(user,sizeof(user),buf,':')<0){
-        printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",pwdfile);
-        exit(1);
-     }
-     strncat(userfile,user,strlen(user));
-     strncat(userfile,":",1);
-   }
-
-   fclose(fp_usr);
-
-   return;
-}
-
-
-void gethexclude(char *hexfile, int debug)
-{
-
-   FILE *fp_ex;
-   char buf[255];
-   char Msg[255];
-   unsigned long int nreg=0;
-
-   if(debug) {
-      sprintf(Msg,"%s: %s",text[67],hexfile);
-      debuga(Msg);
-   }
-
-   if ((fp_ex = fopen(hexfile, "r")) == NULL) {
-      fprintf(stderr, "SARG: (gethexclude) %s: %s - %s\n",text[45],hexfile,strerror(errno));
+   fseek(fp_usr, 0, SEEK_END);
+   nreg = ftell(fp_usr);
+   if (nreg<0) {
+      debuga(_("Cannot get the size of file %s\n"),pwdfile);
       exit(1);
    }
+   nreg = nreg+5000;
+   fseek(fp_usr, 0, SEEK_SET);
 
-   nreg = lseek(fileno(fp_ex), (off_t)0, SEEK_END);
-   lseek(fileno(fp_ex), (off_t)0, 0);
-
-   if((excludefile=(char *) malloc(nreg+11))==NULL){
-      fprintf(stderr, "SARG: %s (%ld):\n",text[59],nreg);
-      exit(1);
-   }
-
-   bzero(excludefile,nreg+11);
-
-   while(fgets(buf,255,fp_ex)!=NULL){
-     if(strstr(buf,"#") != 0)
-        continue;
-     buf[strlen(buf)-1]='\0';
-     strcat(excludefile,buf);
-     strcat(excludefile," ");
-   }
-
-   strcat(excludefile,"*END* ");
-
-   fclose(fp_ex);
-
-   return;
-}
-
-
-void getuexclude(char *uexfile, int debug)
-{
-
-   FILE *fp_ex;
-   char buf[255];
-   char Msg[255];
-   unsigned long int nreg=0;
-
-   if(debug) {
-      sprintf(Msg,"%s: %s",text[67],uexfile);
-      debuga(Msg);
-   }
-
-   if ((fp_ex = fopen(uexfile, "r")) == NULL) {
-      fprintf(stderr, "SARG: (gethexclude) %s: %s - %s\n",text[45],uexfile,strerror(errno));
-      exit(1);
-   }
-
-   nreg = lseek(fileno(fp_ex), (off_t)0, SEEK_END);
-   lseek(fileno(fp_ex), (off_t)0, 0);
-
-   if((excludeuser=(char *) malloc(nreg+11))==NULL){
-      fprintf(stderr, "SARG: %s (%ld):\n",text[59],nreg);
+   if((userfile=(char *) malloc(nreg))==NULL){
+      debuga(_("malloc error (%ld):\n"),nreg);
       exit(1);
    }
 
-   bzero(excludeuser,nreg+11);
+   bzero(userfile,nreg);
+   strcpy(userfile,":");
 
-   while(fgets(buf,255,fp_ex)!=NULL){
-     if(strstr(buf,"#") != 0)
-        continue;
-     buf[strlen(buf)-1]='\0';
-     strcat(excludeuser,buf);
-     strcat(excludeuser," ");
+   while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
+      str=strchr(buf,':');
+      if (!str) {
+         debuga(_("You have an invalid user in your %s file.\n"),pwdfile);
+         exit(1);
+      }
+      str[1]=0;
+      strcat(userfile,buf);
    }
 
-   strcat(excludeuser,"*END* ");
-
-   fclose(fp_ex);
+   fclose(fp_usr);
 
    return;
 }