]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - email.c
Change a message string.
[thirdparty/sarg.git] / email.c
diff --git a/email.c b/email.c
index 2c0d7b415771a648d0b05c196a3c516a5f0f9ffd..a04a097a47deb5bd2846984396714a03115bf198 100644 (file)
--- a/email.c
+++ b/email.c
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2008
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ *     http://sourceforge.net/projects/sarg/forums/forum/363374
  * ---------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
 #include "include/conf.h"
 #include "include/defs.h"
 
-int geramail(const char *dirname, int debug, const char *outdir, int userip, const char *email, const char *TempDir)
-{
+#ifdef ENABLE_DOUBLE_CHECK_DATA
+extern struct globalstatstruct globstat;
+#endif
 
-   FILE *fp_in, *fp_top1, *fp_top2, *fp_top3;
-   long long int ttnbytes=0, ttnacc=0, tnacc=0;
-   long long int tnbytes=0, ttnelap=0, tnelap=0;
-   float perc=0.00;
-   float perc2=0.00;
-   int posicao=0;
-   char olduser[MAXLEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN];
-   char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAXLEN], nacc[20], nbytes[20], url[MAXLEN], tusr[MAXLEN];
-   char ip[MAXLEN], hora[9], data[15], elap[16], user2[MAXLEN], wperc[8], wperc2[8];
-   char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
-   char incac[30], oucac[30];
-   int totuser=0;
-   time_t t;
-   struct tm *local;
-   int cstatus;
-   char warea[MAXLEN];
-
-   strcpy(wger,dirname);
-   strcpy(top1,dirname);
-   strcpy(top2,dirname);
-   strcpy(top3,dirname);
-   strcpy(tusr,dirname);
-   strcat(wger,"/sarg-general");
-   strcat(top1,"/top");
-   strcat(top2,"/top.tmp");
-   strcat(top3,"/report");
-   strcat(tusr,"/sarg-users");
-
-   if((fp_in=fopen(wger,"r"))==NULL) {
-      fprintf(stderr, "SARG: (email) %s: %s\n",text[45],wger);
-      exit(1);
-   }
-
-   if((fp_top1=fopen(top1,"w"))==NULL) {
-      fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
-      exit(1);
-   }
-
-   if((fp_top2=fopen(top2,"w"))==NULL) {
-      fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top2);
-      exit(1);
-   }
-
-   olduser[0]='\0';
-   totuser=0;
-
-   while(fgets(warea,sizeof(warea),fp_in))
-   {
-      if (getword(user,sizeof(user),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken user in your %s file.\n",wger);
-         exit(1);
-      }
-      if(strcmp(user,"TOTAL") == 0) {
-         continue;
-      }
-      if (getword(nacc,sizeof(nacc),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken number of access in your %s file.\n",wger);
-         exit(1);
-      }
-      if (getword(nbytes,sizeof(nbytes),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",wger);
-         exit(1);
-      }
-      if (getword(url,sizeof(url),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken url in your %s file.\n",wger);
-         exit(1);
-      }
-      if (getword(ip,sizeof(ip),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken source IP address in your %s file.\n",wger);
-         exit(1);
-      }
-      if (getword(hora,sizeof(hora),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken time in your %s file.\n",wger);
-         exit(1);
-      }
-      if (getword(data,sizeof(data),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken date in your %s file.\n",wger);
-         exit(1);
-      }
-      if (getword(elap,sizeof(elap),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",wger);
-         exit(1);
-      }
-      if (getword(incac,sizeof(incac),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken in cache column in your %s file.\n",wger);
-         exit(1);
-      }
-      if (getword(oucac,sizeof(oucac),warea,0)<0) {
-         printf("SARG: Maybe you have a broken not in cache column in your %s file.\n",wger);
-         exit(1);
-      }
-
-      if(strcmp(olduser,user) != 0)
-      {
-         totuser++;
-
-         if (olduser[0] != '\0') {
+int geramail(const char *dirname, int debug, const char *outdir, const char *email, const char *TempDir)
+{
+       FileObject *fp_in;
+       FILE *fp_top1, *fp_top2, *fp_top3;
+       long long int ttnbytes=0, ttnacc=0, tnacc=0;
+       long long int tnbytes=0, ttnelap=0, tnelap=0;
+       long long int nacc, nbytes, elap;
+       long long int avgacc, avgelap;
+       double perc=0.00;
+       double perc2=0.00;
+       int posicao=0;
+       char olduser[MAX_USER_LEN], csort[MAXLEN];
+       char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN];
+       char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
+       char *buf;
+       char warea[MAXLEN];
+       char Subject[120];
+       int totuser=0;
+       time_t t;
+       struct tm *local;
+       int cstatus;
+       struct getwordstruct gwarea;
+       struct generalitemstruct item;
+       longline line;
+       const struct userinfostruct *uinfo;
+
+       snprintf(wger,sizeof(wger),"%s/sarg-general",dirname);
+       if((fp_in=FileObject_Open(wger))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wger,FileObject_GetLastOpenError());
+               exit(EXIT_FAILURE);
+       }
+
+       snprintf(top2,sizeof(top2),"%s/top.tmp",dirname);
+       if((fp_top2=fopen(top2,"w"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       olduser[0]='\0';
+       totuser=0;
+
+       if ((line=longline_create())==NULL) {
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),wger);
+               exit(EXIT_FAILURE);
+       }
+
+       while((buf=longline_read(fp_in,line))!=NULL) {
+               ger_read(buf,&item,wger);
+               if(item.total) continue;
+               if(strcmp(olduser,item.user) != 0) {
+                       totuser++;
+
+                       if (olduser[0] != '\0') {
 #if defined(__FreeBSD__)
-            fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
-#elif defined(__alpha) || __ALPHA
-            fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap);
+                               fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
 #else
-            fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
+                               fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap);
 #endif
-            ttnbytes=ttnbytes+tnbytes;
-            ttnacc=ttnacc+tnacc;
-            ttnelap=ttnelap+tnelap;
-         }
-         strcpy(olduser,user);
-         tnbytes=0;
-         tnacc=0;
-         tnelap=0;
-      }
-
-      tnbytes=tnbytes+atol(nbytes);
-      tnacc=tnacc+atol(nacc);
-      tnelap=tnelap+atol(elap);
-   }
-
-   if (olduser[0] != '\0') {
+                               ttnbytes+=tnbytes;
+                               ttnacc+=tnacc;
+                               ttnelap+=tnelap;
+                       }
+                       strcpy(olduser,item.user);
+                       tnbytes=0;
+                       tnacc=0;
+                       tnelap=0;
+               }
+
+               tnbytes+=item.nbytes;
+               tnacc+=item.nacc;
+               tnelap+=item.nelap;
+       }
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wger,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
+       }
+       longline_destroy(&line);
+
+       if (olduser[0] != '\0') {
 #if defined(__FreeBSD__)
-      fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
-#elif defined(__alpha) || __ALPHA
-      fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap);
+               fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
 #else
-      fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
+               fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap);
 #endif
-      ttnbytes=ttnbytes+tnbytes;
-      ttnacc=ttnacc+tnacc;
-      ttnelap=ttnelap+tnelap;
-   }
-
-   fclose(fp_in);
-   fclose(fp_top2);
-
-   sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2);
-   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);
-      exit(1);
-   }
-
-   unlink(top2);
-
-   if((fp_top1=fopen(top1,"a"))==NULL) {
-      fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
-      exit(1);
-   }
-#if defined(__FreeBSD__)
-   fprintf(fp_top1,"TOTAL\t%qu\t%qu\t%qu\n",ttnbytes,ttnacc,ttnelap);
-#elif defined(__alpha) || __ALPHA
-   fprintf(fp_top1,"TOTAL\t%ld\t%ld\t%ld\n",ttnbytes,ttnacc,ttnelap);
-#else
-   fprintf(fp_top1,"TOTAL\t%lld\t%lld\t%lld\n",ttnbytes,ttnacc,ttnelap);
+               ttnbytes+=tnbytes;
+               ttnacc+=tnacc;
+               ttnelap+=tnelap;
+       }
+
+       if (fclose(fp_top2)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+#ifdef ENABLE_DOUBLE_CHECK_DATA
+       if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttnelap!=globstat.elap) {
+               debuga(__FILE__,__LINE__,_("Total statistics mismatch when reading \"%s\" to produce the email report\n"),wger);
+               exit(EXIT_FAILURE);
+       }
 #endif
-   fclose(fp_top1);
-
-   /*
-   * Obtem o period
-   */
-
-   strcpy(arqper,dirname);
-   strcat(arqper,"/sarg-period");
-
-   if ((fp_in = fopen(arqper, "r")) == 0){
-      fprintf(stderr, "SARG: (email) %s: %s\n",text[45],arqper);
-      exit(1);
-   }
-
-   if (!fgets(period,sizeof(period),fp_in)) {
-      fprintf(stderr,"SARG: (email) read error in %s\n",arqper);
-      exit(1);
-   }
-   fclose(fp_in);
-
-   if((fp_top1=fopen(top1,"r"))==NULL) {
-      fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
-      exit(1);
-   }
-
-   if((fp_top3=fopen(top3,"w"))==NULL) {
-      fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
-      exit(1);
-   }
-
-   strcpy(strip1,text[88]);
-   strip_latin(strip1);
-   fprintf(fp_top3,"%s\n",strip1);
-
-   strcpy(strip1,text[97]);
-   strip_latin(strip1);
-   fprintf(fp_top3,"%s\n",strip1);
-
-   strcpy(strip1,text[89]);
-   strip_latin(strip1);
-   fprintf(fp_top3,"%s %s\n\n",strip1,period);
-
-   strcpy(strip1,text[100]);
-   strip_latin(strip1);
-   strcpy(strip2,text[98]);
-   strip_latin(strip2);
-   strcpy(strip3,text[92]);
-   strip_latin(strip3);
-   strcpy(strip4,text[93]);
-   strip_latin(strip4);
-   strcpy(strip5,text[94]);
-   strip_latin(strip5);
-   strcpy(strip6,text[95]);
-   strip_latin(strip6);
-   strcpy(strip7,text[99]);
-   strip_latin(strip7);
-
-   fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
-
-   while(fgets(warea,sizeof(warea),fp_top1))
-   {
-      if (getword(user,sizeof(user),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken user in your %s file.\n",top1);
-         exit(1);
-      }
-      if (getword(nbytes,sizeof(nbytes),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",top1);
-         exit(1);
-      }
-      if (getword(nacc,sizeof(nacc),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken number of access in your %s file.\n",top1);
-         exit(1);
-      }
-      if (getword(elap,sizeof(elap),warea,'\t')<0) {
-         printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",top1);
-         exit(1);
-      }
-
-      if(strchr(user,'_') != NULL)
-         fixip(user);
-
-      strcpy(user2,user);
-
-      tnbytes=atol(nbytes);
-
-      if(tnbytes) {
-         perc=ttnbytes / 100;
-         perc=tnbytes / perc;
-      } else perc = 0;
-
-      if(atol(elap)) {
-         perc2=ttnelap / 100;
-         perc2=atol(elap) / perc2;
-      } else perc2 = 0;
-
-      posicao++;
-      tnelap=atol(elap);
-
-      sprintf(wperc,"%3.2f%%",perc);
-      sprintf(wperc2,"%3.2f%%",perc2);
-
-      if(strcmp(user,"TOTAL") == 0){
-         fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
+
+       if (snprintf(csort,sizeof(csort),"sort -n -T \"%s\" -t \"\t\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2)>=sizeof(csort)) {
+               debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),top2,top1);
+               exit(EXIT_FAILURE);
+       }
+       cstatus=system(csort);
+       if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+               debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
+               exit(EXIT_FAILURE);
+       }
+
+       if (!KeepTempLog && unlink(top2)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),top2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if((fp_top1=fopen(top1,"r"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top1,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       snprintf(top3,sizeof(top3),"%s/report",dirname);
+       if((fp_top3=fopen(top3,"w"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       safe_strcpy(strip1,_("Squid User Access Report"),sizeof(strip1));
+       strip_latin(strip1);
+       fprintf(fp_top3,"%s\n",strip1);
+
+       safe_strcpy(strip1,_("Decreasing Access (bytes)"),sizeof(strip1));
+       strip_latin(strip1);
+       fprintf(fp_top3,"%s\n",strip1);
+
+       safe_strcpy(strip1,_("Period"),sizeof(strip1));
+       strip_latin(strip1);
+       fprintf(fp_top3,"%s %s\n\n",strip1,period.text);
+
+       safe_strcpy(strip1,_("NUM"),sizeof(strip1));
+       strip_latin(strip1);
+       safe_strcpy(strip2,_("USERID"),sizeof(strip2));
+       strip_latin(strip2);
+       safe_strcpy(strip3,_("CONNECT"),sizeof(strip3));
+       strip_latin(strip3);
+       safe_strcpy(strip4,_("BYTES"),sizeof(strip4));
+       strip_latin(strip4);
+       safe_strcpy(strip5,_("ELAPSED TIME"),sizeof(strip5));
+       strip_latin(strip5);
+       safe_strcpy(strip6,_("MILLISEC"),sizeof(strip6));
+       strip_latin(strip6);
+       safe_strcpy(strip7,pgettext("duration","TIME"),sizeof(strip7));
+       strip_latin(strip7);
+
+       fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
+
+       while(fgets(warea,sizeof(warea),fp_top1)) {
+               fixendofline(warea);
+               getword_start(&gwarea,warea);
+               if (getword(user,sizeof(user),&gwarea,'\t')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid user ID in file \"%s\"\n"),top1);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),top1);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword_atoll(&nacc,&gwarea,'\t')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),top1);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword_atoll(&elap,&gwarea,'\0')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),top1);
+                       exit(EXIT_FAILURE);
+               }
+
+               uinfo=userinfo_find_from_id(user);
+               if (!uinfo) {
+                       debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,top1);
+                       exit(EXIT_FAILURE);
+               }
+
+               perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0;
+               perc2=(ttnelap) ? elap * 100. / ttnelap : 0;
+
+               posicao++;
+
 #if defined(__FreeBSD__)
-         fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
-#elif defined(__alpha) || __ALPHA
-         fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
+               fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10qu %3.2lf%%\n",posicao,uinfo->label,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2);
 #else
-         fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
+               fprintf(fp_top3,"%7d %20s %8"PRIu64" %15s %3.2lf%% %10s %10"PRIu64" %3.2lf%%\n",posicao,uinfo->label,(uint64_t)nacc,fixnum(nbytes,1),perc,buildtime(elap),(uint64_t)elap,perc2);
 #endif
-      } else {
+       }
+
+       // output total
+       fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
 #if defined(__FreeBSD__)
-         fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10qu %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
-#elif defined(__alpha) || __ALPHA
-         fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10ld %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
+       fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
 #else
-         fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
+       fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",_("TOTAL")," ",(uint64_t)ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),(uint64_t)ttnelap);
 #endif
-      }
-   }
-
-   if(ttnbytes) {
-      tnbytes=ttnbytes / totuser;
-   } else tnbytes=0;
 
-   strcpy(strip1,text[96]);
-   strip_latin(strip1);
+       // compute and write average
+       if (totuser>0) {
+               tnbytes=(totuser) ? ttnbytes / totuser : 0;
+               avgacc=ttnacc/totuser;
+               avgelap=ttnelap/totuser;
+       } else {
+               tnbytes=0;
+               avgacc=0;
+               avgelap=0;
+       }
+
+       safe_strcpy(strip1,_("AVERAGE"),sizeof(strip1));
+       strip_latin(strip1);
 #if defined(__FreeBSD__)
-   fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
-#elif defined(__alpha) || __ALPHA
-   fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
+       fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),avgelap);
 #else
-   fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
+       fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",strip1," ",(uint64_t)avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),(uint64_t)avgelap);
 #endif
 
-   fclose(fp_top1);
-   unlink(top1);
-
-   t = time(NULL);
-   local = localtime(&t);
-   fprintf(fp_top3, "\n%s\n", asctime(local));
-
-   fclose(fp_top3);
-
-   if(strcmp(email,"stdout") == 0) {
-      if((fp_top3=fopen(top3,"r"))==NULL) {
-         fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
-         exit(1);
-      }
-
-      while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
-         printf("%s",buf);
-    } else {
-      sprintf(buf,"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,text[55],asctime(local),email,top3);
-      cstatus=system(buf);
-      if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-         fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
-         fprintf(stderr, "SARG: command: %s\n",buf);
-         exit(1);
-      }
-    }
-
-   if (snprintf(csort,sizeof(csort),"rm -rf \"%s/sarg\"",TempDir)>=sizeof(csort)) {
-      fprintf(stderr,"SARG: Temporary directory name too long: %s\n",TempDir);
-      exit(1);
-   }
-   cstatus=system(csort);
-   if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-      fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
-      fprintf(stderr, "SARG: command: %s\n",csort);
-      exit(1);
-   }
-
-   return (0);
+       if (fclose(fp_top1)==EOF) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),top1,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if (!KeepTempLog && unlink(top1)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),top1,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       t = time(NULL);
+       local = localtime(&t);
+       fprintf(fp_top3, "\n%s\n", asctime(local));
+
+       if (fclose(fp_top3)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if(strcmp(email,"stdout") == 0) {
+               if((fp_top3=fopen(top3,"r"))==NULL) {
+                       debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top3,strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+
+               while(fgets(warea,sizeof(warea),fp_top3)!=NULL)
+                       fputs(warea,stdout);
+               if (fclose(fp_top3)==EOF) {
+                       debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),top3,strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+       } else {
+               /* TRANSLATORS: The string is formatted using strftime. You can use
+                  any string formatting marker allowed by strftime. */
+               strftime(Subject,sizeof(Subject),_("SARG report, %c"),local);
+               snprintf(warea,sizeof(warea),"%s -s \"%s\" \"%s\" <\"%s\"",MailUtility,Subject,email,top3);
+               if (debug)
+                       debuga(__FILE__,__LINE__,_("Sending mail with command: %s\n"),warea);
+               cstatus=system(warea);
+               if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+                       debuga(__FILE__,__LINE__,_("command return status %d\n"),WEXITSTATUS(cstatus));
+                       debuga(__FILE__,__LINE__,_("command: %s\n"),warea);
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       //unlinkdir(TempDir,0);
+
+       return (0);
 }