]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - topuser.c
Be more consistent with the use of the temporary directory (i.e. always use /tmp...
[thirdparty/sarg.git] / topuser.c
index a69af92a10cd0328f36e141e3a1be2ae7f009832..444cfc2ac6bdb59c50fa5321057feac1be416477 100644 (file)
--- a/topuser.c
+++ b/topuser.c
@@ -41,7 +41,7 @@ void topuser(void)
    double perc2=0.00;
    double inperc=0.00, ouperc=0.00;
    int posicao=0;
-   char olduser[MAX_USER_LEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN];
+   char olduser[MAX_USER_LEN], csort[MAXLEN];
    char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN];
    char user[MAX_USER_LEN], tusr[MAXLEN];
    char ipantes[MAXLEN], nameantes[MAXLEN];
@@ -49,73 +49,50 @@ void topuser(void)
    const char *order="-r";
    char title[80];
    char *warea;
-   char ltext110[100];
    int  totuser=0;
    int  topcount=0;
-   int i;
-   char *s;
    int cstatus;
    struct getwordstruct gwarea;
-   struct longlinestruct line;
+   longline line;
    struct generalitemstruct item;
    const struct userinfostruct *uinfo;
 
    ipantes[0]='\0';
    nameantes[0]='\0';
 
-   /*
-   * get period
-   */
-   strcpy(arqper,dirname);
-   strcat(arqper,"/sarg-period");
-   if ((fp_in = fopen(arqper, "r")) == 0) {
-      fprintf(stderr, "SARG: (topuser) %s: %s\n",_("Cannot open file"),arqper);
-      exit(1);
-   }
-   if (!fgets(period,sizeof(period),fp_in)) {
-      fprintf(stderr,"SARG: (topuser) Read error in %s\n",arqper);
-      exit(1);
-   }
-   fclose(fp_in);
-
-   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(tusr,"/sarg-users");
-   strcat(top3,"/index.html");
-
    ntopuser = 0;
+   snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname);
    if((fp_in=fopen(wger,"r"))==NULL) {
-      fprintf(stderr, "SARG: (topuser) %s: %s\n",_("Cannot open file"),wger);
-      exit(1);
+      debuga(_("(topuser) Cannot open file %s\n"),wger);
+      exit(EXIT_FAILURE);
    }
 
+   snprintf(top2,sizeof(top2),"%s/top.tmp",outdirname);
    if((fp_top2=fopen(top2,"w"))==NULL) {
-      fprintf(stderr, "SARG: (topuser) %s: %s\n",_("Cannot open file"),top2);
-      exit(1);
+      debuga(_("(topuser) Cannot open file %s\n"),top2);
+      exit(EXIT_FAILURE);
    }
 
    olduser[0]='\0';
    totuser=0;
 
-   if (longline_prepare(&line)<0) {
-      debuga(_("Not enough memory to read the file %s"),wger);
-      exit(1);
+   if ((line=longline_create())==NULL) {
+      debuga(_("Not enough memory to read the file %s\n"),wger);
+      exit(EXIT_FAILURE);
    }
 
-   while((warea=longline_read(fp_in,&line))!=NULL) {
+   while((warea=longline_read(fp_in,line))!=NULL) {
       ger_read(warea,&item,wger);
       if(item.total) continue;
       if(strcmp(olduser,item.user) != 0) {
          totuser++;
 
          if (olduser[0] != '\0') {
-            fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap,tnincache,tnoucache);
+            /*
+            This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
+            to print a long long int unless it is exactly 64-bits long.
+            */
+            fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache);
 
             ttnbytes+=tnbytes;
             ttnacc+=tnacc;
@@ -138,10 +115,14 @@ void topuser(void)
       tnoucache+=item.oucache;
    }
    fclose(fp_in);
-   longline_free(&line);
+   longline_destroy(&line);
 
    if (olduser[0] != '\0') {
-      fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap,tnincache,tnoucache);
+      /*
+      This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
+      to print a long long int unless it is exactly 64-bits long.
+      */
+      fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache);
 
       ttnbytes+=tnbytes;
       ttnacc+=tnacc;
@@ -166,42 +147,45 @@ void topuser(void)
    if(strcmp(TopuserSortOrder,"normal") == 0)
       order="";
 
-   sprintf(csort,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", TempDir, order, sfield, top1, top2);
+   snprintf(top1,sizeof(top1),"%s/top",outdirname);
+   sprintf(csort,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", tmp, order, sfield, top1, top2);
    cstatus=system(csort);
    if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-      debuga(_("sort command return status %d"),WEXITSTATUS(cstatus));
-      debuga(_("sort command: %s"),csort);
-      exit(1);
+      debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+      debuga(_("sort command: %s\n"),csort);
+      exit(EXIT_FAILURE);
    }
 
    if((fp_top1=fopen(top1,"r"))==NULL) {
-      debuga("SARG: (topuser) %s: %s\n",_("Cannot open file"),top1);
-      exit(1);
+      debuga(_("(topuser) Cannot open file %s\n"),top1);
+      exit(EXIT_FAILURE);
    }
 
    unlink(top2);
 
+   snprintf(top3,sizeof(top3),"%s/index.html",outdirname);
    if((fp_top3=fopen(top3,"w"))==NULL) {
-      fprintf(stderr, "SARG: (topuser) %s: %s\n",_("Cannot open file"),top3);
-      exit(1);
+      debuga(_("(topuser) Cannot open file %s\n"),top3);
+      exit(EXIT_FAILURE);
    }
 
-   snprintf(title,sizeof(title),_("SARG report for %s"),period);
+   snprintf(title,sizeof(title),_("SARG report for %s"),period.text);
    write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title);
-   fprintf(fp_top3,"<tr><td class=\"header_c\">%s: %s</td></tr>\n",_("Period"),period);
+   fputs("<tr><td class=\"header_c\">",fp_top3);
+   fprintf(fp_top3,_("Period: %s"),period.html);
+   fputs("</td></tr>\n",fp_top3);
    fprintf(fp_top3,"<tr><td class=\"header_c\">%s: %s, %s</td></tr>\n",_("Sort"),TopuserSortField,TopuserSortOrder);
-   fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Topuser"));
+   fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Top users"));
    close_html_header(fp_top3);
 
    fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_top3);
-   fputs("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n",fp_top3);
 
-   if((ReportType & REPORT_TYPE_TOPSITES) != 0 && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"topsites.html\">%s</a></td></tr>\n",_("Topsites"));
+   if((ReportType & REPORT_TYPE_TOPSITES) != 0 && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"topsites.html\">%s</a></td></tr>\n",_("Top sites"));
    if((ReportType & REPORT_TYPE_SITES_USERS) != 0 && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"siteuser.html\">%s</a></td></tr>\n",_("Sites & Users"));
    if(dansguardian_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
-   if(squidguard_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"squidguard.html\">%s</a></td></tr>\n",_("squidGuard"));
+   if(squidguard_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
    if ((ReportType & REPORT_TYPE_DOWNLOADS) != 0 && download_count && !Privacy && ndownload) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads"));
-   if ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied"));
+   if ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
    if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0 && authfail_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures"));
    if(smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
    if(UserAgentLog[0] != '\0' && useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));
@@ -229,11 +213,11 @@ void topuser(void)
    if((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
       fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
    if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
-      fprintf(fp_top3,"<th class=\"header_c\" colspan=\"2\">%s-%s-%s</th>",_("IN"),_("CACHE"),_("OUT"));
+      fprintf(fp_top3,"<th class=\"header_c\" colspan=\"2\">%s</th>",_("IN-CACHE-OUT"));
    if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
       fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME"));
    if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
-      fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("MILISEC"));
+      fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
    if((TopUserFields & TOPUSERFIELDS_PTIME) != 0)
       fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("TIME"));
 
@@ -241,36 +225,36 @@ void topuser(void)
 
    ntopuser = 0;
 
-   if (longline_prepare(&line)<0) {
-      debuga(_("Not enough memory to read the downloaded files"));
-      exit(1);
+   if ((line=longline_create())==NULL) {
+      debuga(_("Not enough memory to read the downloaded files\n"));
+      exit(EXIT_FAILURE);
    }
 
-   while((warea=longline_read(fp_top1,&line))!=NULL) {
+   while((warea=longline_read(fp_top1,line))!=NULL) {
       getword_start(&gwarea,warea);
       if (getword(user,sizeof(user),&gwarea,'\t')<0) {
-         debuga(_("There may be a broken user in file %s."),top1);
-         exit(1);
+         debuga(_("There is a broken user in file %s\n"),top1);
+         exit(EXIT_FAILURE);
       }
       if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
-         debuga(_("There may be a broken number of bytes in file %s."),top1);
-         exit(1);
+         debuga(_("There is a broken number of bytes in file %s\n"),top1);
+         exit(EXIT_FAILURE);
       }
       if (getword_atoll(&nacc,&gwarea,'\t')<0) {
-         debuga(_("There may be a broken number of access in file %s."),top1);
-         exit(1);
+         debuga(_("There is a broken number of access in file %s\n"),top1);
+         exit(EXIT_FAILURE);
       }
       if (getword_atoll(&elap,&gwarea,'\t')<0) {
-         debuga(_("There may be a broken elpased time in file %s."),top1);
-         exit(1);
+         debuga(_("There is a broken elpased time in file %s\n"),top1);
+         exit(EXIT_FAILURE);
       }
       if (getword_atoll(&incac,&gwarea,'\t')<0) {
-         debuga(_("There may be a broken in-cache size in file %s."),top1);
-         exit(1);
+         debuga(_("There is a broken in-cache size in file %s\n"),top1);
+         exit(EXIT_FAILURE);
       }
       if (getword_atoll(&oucac,&gwarea,'\n')<0) {
-         debuga(_("There may be a broken out-of-cache size in file %s."),top1);
-         exit(1);
+         debuga(_("There is a broken out-of-cache size in file %s\n"),top1);
+         exit(EXIT_FAILURE);
       }
       if(nacc < 1)
          continue;
@@ -281,8 +265,8 @@ void topuser(void)
 
       uinfo=userinfo_find_from_id(user);
       if (!uinfo) {
-         debuga(_("Unknown user ID %s in file %s"),user,top1);
-         exit(1);
+         debuga(_("Unknown user ID %s in file %s\n"),user,top1);
+         exit(EXIT_FAILURE);
       }
 
       fputs("<tr>",fp_top3);
@@ -292,23 +276,16 @@ void topuser(void)
          fprintf(fp_top3,"<td class=\"data\">%d</td>",posicao);
 
       if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) {
-         s=_("DATE/TIME");
-         for(i=0 ; i<sizeof(ltext110)-1 && s[i] ; i++)
-            ltext110[i]=tolower(s[i]);
-         ltext110[i]='\0';
          fputs("<td class=\"data2\">",fp_top3);
 #ifdef HAVE_GD
          if(Graphs && GraphFont[0]!='\0') {
-            fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,ImageFile,_("Graphic"));
+            //fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,ImageFile,_("Graphic"));
+            fprintf(fp_top3,"<a href=\"%s/graph.html\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,ImageFile,_("Graphic"));
          }
 #endif
-         fprintf(fp_top3,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" title=\"%s",uinfo->filename,uinfo->filename,ImageFile,ltext110);
-#ifdef HAVE_GD
-         fprintf(fp_top3," %s",_("Report"));
-#endif
-         fputs("\" alt=\"T\"></a></td>",fp_top3);
+         fprintf(fp_top3,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" title=\"%s\" alt=\"T\"></a></td>",uinfo->filename,uinfo->filename,ImageFile,_("date/time report"));
       } else {
-         sprintf(val1,"%s/d%s.html",dirname,uinfo->filename);
+         sprintf(val1,"%s/d%s.html",outdirname,uinfo->filename);
          unlink(val1);
       }
       if((TopUserFields & TOPUSERFIELDS_USERID) != 0) {
@@ -345,7 +322,7 @@ void topuser(void)
    }
    fclose(fp_top1);
    unlink(top1);
-   longline_free(&line);
+   longline_destroy(&line);
 
    if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) {
       fputs("<tr>",fp_top3);
@@ -401,15 +378,19 @@ void topuser(void)
    }
 
    fputs("</table></div>\n",fp_top3);
-   write_html_trailer(fp_top3);
-   fclose(fp_top3);
+   if (write_html_trailer(fp_top3)<0)
+      debuga(_("Write error in top user list %s\n"),top3);
+   if (fclose(fp_top3)==EOF)
+      debuga(_("Failed to close the top user list %s - %s\n"),top3,strerror(errno));
 
+   snprintf(tusr,sizeof(tusr),"%s/sarg-users",outdirname);
    if((fp_ou=fopen(tusr,"w"))==NULL) {
-      fprintf(stderr, "SARG: (topuser) %s: %s\n",_("Cannot open file"),tusr);
-      exit(1);
+      debuga(_("(topuser) Cannot open file %s\n"),tusr);
+      exit(EXIT_FAILURE);
    }
    fprintf(fp_ou,"%d\n",totuser);
-   fclose(fp_ou);
+   if (fclose(fp_ou)==EOF)
+      debuga(_("Failed to close file %s - %s\n"),tusr,strerror(errno));
 
    return;
 }