]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - totday.c
Update the po files
[thirdparty/sarg.git] / totday.c
index 40e03b531bd26de697843de7682e4c54841cb1ef..61472e2c0c2333e520d5fe180d418a0968d0e3d8 100644 (file)
--- a/totday.c
+++ b/totday.c
@@ -32,83 +32,112 @@ void day_totalize(const char *tmp, const struct userinfostruct *uinfo, int index
 
    FILE *fp_in, *fp_ou;
 
-   char data[20];
-   char hora[20];
-   char min[20];
-   char elap[20];
-   char odata[20];
-   char ohora[20];
-   char oelap[20];
-   char csort[255];
-   char wdirname[MAXLEN];
-   char sortout[MAXLEN];
-   char arqout[MAXLEN];
-   int  regs=0;
-   long long int telap=0;
-   int cstatus;
+   char buf[200];
+   char date[20];
+   long long int hour;
+   long long int bytes;
+   long long int elap;
+   long long int tbytes[MAX_DATETIME_DAYS*24];
+   long long int telap[MAX_DATETIME_DAYS*24];
+   int day,month,year;
+   int daylist[MAX_DATETIME_DAYS];
+   int ndaylist;
+   int daynum;
+   int dayidx;
+   int i;
+   char wdirname[2048];
+   char arqout[2048];
+   char colsep;
    struct getwordstruct gwarea;
 
    if(indexonly) return;
    if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
+   if (datetimeby==0) return;
 
-   sprintf(wdirname,"%s/%s.htmp",tmp,uinfo->filename);
-   sprintf(arqout,"%s/%s.day",tmp,uinfo->filename);
-   sprintf(sortout,"%s/%s.sort",tmp,uinfo->filename);
-
-   sprintf(csort,"sort -k 1,1 -k 2,2 -o \"%s\" \"%s\"",sortout,wdirname);
-   cstatus=system(csort);
-   if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-      debuga(_("sort command return status %d"),WEXITSTATUS(cstatus));
-      debuga(_("sort command: %s"),csort);
-      exit(1);
+   if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
+      debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".htmp");
+      exit(EXIT_FAILURE);
    }
-   if((fp_in=fopen(sortout,"r"))==NULL) {
-      fprintf(stderr, "SARG: (totday) %s: %s\n",_("Cannot open log file"),sortout);
-      debuga(_("sort command: %s"),csort);
-      exit(1);
+   if (snprintf(arqout,sizeof(arqout),"%s/%s.day",tmp,uinfo->filename)>=sizeof(arqout)) {
+      debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".day");
+      exit(EXIT_FAILURE);
    }
 
-   unlink(wdirname);
-
-   if((fp_ou=fopen(arqout,"w"))==NULL) {
-     fprintf(stderr, "SARG: (totday) %s: %s\n",_("Cannot open log file"),arqout);
-     exit(1);
+   if((fp_in=fopen(wdirname,"r"))==NULL) {
+      debuga(_("(totday) Cannot open log file %s\n"),wdirname);
+      exit(EXIT_FAILURE);
    }
 
+   memset(tbytes,0,sizeof(tbytes));
+   memset(telap,0,sizeof(tbytes));
+   ndaylist=0;
+
    while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
       fixendofline(buf);
       getword_start(&gwarea,buf);
-      if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,':')<0 ||
-          getword(min,sizeof(min),&gwarea,':')<0 || getword_skip(20,&gwarea,'\t')<0 ||
-          getword(elap,sizeof(elap),&gwarea,0)<0) {
-         debuga(_("There is a broken record or garbage in file %s"),sortout);
-         exit(1);
+      if (getword(date,sizeof(date),&gwarea,'\t')<0 || getword_atoll(&hour,&gwarea,'\t')<0) {
+         debuga(_("There is a broken record or garbage in file %s\n"),wdirname);
+         exit(EXIT_FAILURE);
       }
-
-      if(!regs) {
-         strcpy(odata,data);
-         strcpy(ohora,hora);
-         strcpy(oelap,elap);
-         regs++;
+      if (sscanf(date,"%d/%d/%d",&day,&month,&year)!=3) continue;
+      if (day<1 || day>31 || month<1 || month>12 || year>9999) continue;
+      if (hour<0 || hour>=24) continue;
+      daynum=(year*10000)+(month*100)+day;
+      for (dayidx=0 ; dayidx<ndaylist && daynum!=daylist[dayidx] ; dayidx++);
+      if (dayidx>=ndaylist) {
+         if (dayidx>=sizeof(daylist)/sizeof(*daylist)) {
+            debuga(_("Too many different dates in %s\n"),wdirname);
+            exit(EXIT_FAILURE);
+         }
+         daylist[ndaylist++]=daynum;
       }
-
-      if(strcmp(hora,ohora) != 0 || strcmp(data,odata) != 0) {
-         fprintf(fp_ou,"%s\t%s\t%015lld\n",odata,ohora,telap);
-         strcpy(odata,data);
-         strcpy(ohora,hora);
-         telap=0;
+      i=dayidx*24+hour;
+      if ((datetimeby & DATETIME_BYTE)!=0) {
+         colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0';
+         if (getword_atoll(&bytes,&gwarea,colsep)<0) {
+            debuga(_("Invalid number of bytes in file %s\n"),wdirname);
+            exit(EXIT_FAILURE);
+         }
+         tbytes[i]+=bytes;
+      }
+      if ((datetimeby & DATETIME_ELAP)!=0) {
+         if (getword_atoll(&elap,&gwarea,'\0')<0) {
+            debuga(_("Invalid elapsed time in file %s\n"),wdirname);
+            exit(EXIT_FAILURE);
+         }
+         telap[i]+=elap;
       }
-
-      telap+=my_atoll(elap);
    }
+   fclose(fp_in);
 
-   fprintf(fp_ou,"%s\t%s\t%015lld\n",data,hora,telap);
+   if((fp_ou=fopen(arqout,"w"))==NULL) {
+     debuga(_("(totday) Cannot open log file %s\n"),arqout);
+     exit(EXIT_FAILURE);
+   }
 
-   fclose(fp_in);
-   fclose(fp_ou);
+   for (i=0 ; i<sizeof(tbytes)/sizeof(*tbytes) ; i++) {
+      if (tbytes[i]==0 && telap[i]==0) continue;
+      dayidx=i/24;
+      if (dayidx>sizeof(daylist)/sizeof(*daylist)) continue;
+      hour=i%24;
+      daynum=daylist[dayidx];
+      day=daynum%100;
+      month=(daynum/100)%100;
+      year=daynum/10000;
+      fprintf(fp_ou,"%d/%d/%d\t%lld",day,month,year,hour);
+      if ((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%lld",tbytes[i]);
+      if ((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%lld",telap[i]);
+      fputs("\n",fp_ou);
+   }
 
-   unlink(sortout);
+   if (fclose(fp_ou)==EOF) {
+      debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
 
+   if (unlink(wdirname)==-1) {
+      debuga(_("Cannot delete temporary file %s - %s\n"),wdirname,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    return;
-
 }