]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - useragent.c
Indent the configure script for more readability.
[thirdparty/sarg.git] / useragent.c
index 67ccf8a047e2beee7804c9796f6f73faa24c8bd3..1e654d392afdaf0494e60d3e2e62afdfebe524f5 100644 (file)
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                      orso@penguintech.com.br
- *                                                            1998, 2005
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ *     http://sourceforge.net/projects/sarg/forums/forum/363374
  * ---------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
  */
 
 #include "include/conf.h"
+#include "include/defs.h"
+#include "include/filelist.h"
 
-void useragent()
-{
+FileListObject UserAgentLog=NULL;
 
-   FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL;
-   char tmp[MAXLEN], tmp2[MAXLEN];
-   char ip[MAXLEN], data[255], agent[255], user[255];
-   char ipantes[MAXLEN], nameantes[MAXLEN];
-   char tagent[7];
-   char csort[120];
-   char msg[255];
-   char ftime[128];
-   char user_old[255]="$#%0a3bc6";
-   char agent_old[255]="$#%0a3bc6";
-   char html[255];
-   char hfile[MAXLEN];
-   char idate[MAXLEN], fdate[MAXLEN];
-   int  agentot=0, agentot2=0, agentdif=0, cont=0;
-   float perc=0;
-   unsigned long totregsl=0;
-
-   ip[0]='\0';
-   data[0]='\0';
-   agent[0]='\0';
-   user[0]='\0';
-   user_old[0]='\0';
-   agent_old[0]='\0';
-   ipantes[0]='\0';
-   nameantes[0]='\0';
-
-   sprintf(hfile,"%s/%s/useragent.html", outdir,periodo);
-
-   sprintf(tmp,"%s/squagent.unsort",TempDir);
-   sprintf(tmp2,"%s/squagent.log",TempDir);
-
-   if((fp_in=fopen(UserAgentLog,"r"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],UserAgentLog);
-      exit(1);
-   }
-
-   if((fp_ou=fopen(tmp,"w"))==NULL) {
-      fprintf(stderr, "SARG: (email) %s: %s\n",text[45],tmp);
-      exit(1);
-   }
-
-   if(debug) {
-      sprintf(msg,"%s: %s",text[66],UserAgentLog);
-      debuga(msg);
-   }
-
-   while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
-      totregsl++;
-      getword(ip,buf,' ');
-      getword(data,buf,'[');
-      getword(data,buf,' ');
-      if(totregsl == 1)
-         strcpy(idate,data);
-      getword(agent,buf,'"');
-      getword(agent,buf,'"');
-
-      if(strlen(buf)) {
-         getword(user,buf,' ');
-         getword(user,buf,'\n');
-      }
-
-      if(user[0] == '-')
-         strcpy(user,ip);
-      if(strlen(user) == 0)
-         strcpy(user,ip);
-
-      sprintf(buf,"%s\\%s\\%s\\%s\\\n",ip,data,agent,user);
-      fputs(buf,fp_ou);
-      user[0]='\0';
-   }
-
-   strcpy(fdate,data);
-
-   if(debug) {
-      sprintf(msg, "   %s: %ld",text[10],totregsl);
-      debuga(msg);
-   }
-
-   fclose(fp_in);
-   fclose(fp_ou);
-
-   if (fp_ht) {
-      fclose(fp_ht);
-   }
-
-   if(debug) {
-      sprintf(msg,"%s: %s",text[54],tmp2);
-      debuga(msg);
-   }
-
-   sprintf(csort,"sort -n -t '\\' -k 4,4 -k 3,3 -k 2,2 -k 1,1 -o '%s' '%s'",tmp2,tmp);
-   system(csort);
-
-   unlink(tmp);
-
-   if((fp_in=fopen(tmp2,"r"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
-      exit(1);
-   }
-
-   if((fp_ht=fopen(hfile,"w"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
-      exit(1);
-   }
-   if(debug)
-      debuga(text[72]);
-
-   fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ht);
-   fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ht);
-   fputs("<html>\n",fp_ht);
-   fputs("<head>\n",fp_ht);
-   sprintf(html,"  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
-   fputs(html,fp_ht);
-   fputs("</head>\n",fp_ht);
-
-   if(strlen(FontFace) > 0) {
-      sprintf(html,"<font face=%s>\n",FontFace);
-      fputs(html,fp_ht);
-   }
-
-   sprintf(html,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
-   fputs(html,fp_ht);
-
-   if(strlen(LogoImage) > 0) {
-      fputs("<center><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
-      sprintf(html,"<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n",LogoImage,Width,Height,LogoText);
-      fputs(html,fp_ht);
-      fputs("<tr><td height=\"5\"></td></tr>\n",fp_ht);
-      fputs("</table>\n",fp_ht);
-   }
-
-   show_sarg(fp_ht,"..");
-   fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht);
-   sprintf(html,"<tr><th align=center colspan=2><b><font color=%s size=+1>%s</font></b></th></tr>\n",TiColor,text[105]);
-   fputs(html,fp_ht);
-
-   fputs("<tr><td></td></tr><tr><td></td></tr>",fp_ht);
-   fputs("</table></center>\n",fp_ht);
-
-   fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht);
-
-   sprintf(html,"<tr><td align=right bgcolor=%s><font size=%s>%s:</font><td align=left bgcolor=%s><font size=%s>%s - %s</font></td></td></tr>\n",HeaderBgColor,FontSize,text[89],TxBgColor,FontSize,idate,fdate);
-   fputs(html,fp_ht);
-
-   fputs("</table></center>\n",fp_ht);
-   fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht);
-   fputs("<tr><td></td><td></td></tr>",fp_ht);
-
-   sprintf(html,"<tr><th align=left bgcolor=%s><font size=%s color=%s>%s</font></th><th bgcolor=%s align=left><font size=%s color=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,HeaderColor,text[98],HeaderBgColor,FontSize,HeaderColor,text[106]);
-   fputs(html,fp_ou);
-
-   while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
-      getword(ip,buf,'\\');
-
-      if(strcmp(Ip2Name,"yes") == 0) {
-         if(strcmp(ip,ipantes) != 0) {
-            strcpy(ipantes,ip);
-            ip2name(ip);
-            strcpy(nameantes,ip);
-         } else strcpy(ip,nameantes);
-      }
-
-      getword(data,buf,'\\');
-      getword(agent,buf,'\\');
-      getword(user,buf,'\\');
-
-      if(strcmp(user,user_old) != 0) {
-         sprintf(html,"<tr><td align=left bgcolor=%s><font size=%s>%s</td><td align=left bgcolor=%s><font size=%s>%s</td></tr>\n",TxBgColor,FontSize,user,TxBgColor,FontSize,agent);
-         fputs(html,fp_ht);
-         strcpy(user_old,user);
-         strcpy(agent_old,agent);
-      } else {
-         if(strcmp(agent,agent_old) != 0) {
-            sprintf(html,"<tr><td></td><td align=left bgcolor=%s><font size=%s>%s</td></tr>\n",TxBgColor,FontSize,agent);
-            fputs(html,fp_ht);
-            strcpy(agent_old,agent);
-         }
-      }
-   }
-  
-   fputs("</table>\n",fp_ht);
-   fclose(fp_in);
-   fclose(fp_ou);
-
-   sprintf(csort,"sort -n -t '\\' -k 3,3 -o '%s' '%s'",tmp,tmp2);
-   system(csort);
-
-   unlink(tmp2);
-
-   if((fp_in=fopen(tmp,"r"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp);
-      exit(1);
-   }
-
-   if((fp_ou=fopen(tmp2,"w"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
-      exit(1);
-   }
-
-   agent_old[0]='\0';
-
-   while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
-      getword(ip,buf,'\\');
-      getword(data,buf,'\\');
-      getword(agent,buf,'\\');
-
-      if(!cont) {
-         cont++;
-         strcpy(agent_old,agent);
-      }
-     
-      agentot++;
-
-      if(strcmp(agent,agent_old) != 0) {
-         agentdif++;
-         sprintf(html,"%06d %s\n",agentot,agent_old);
-         fputs(html,fp_ou);
-         strcpy(agent_old,agent);
-         agentot2+=agentot;
-         agentot=1;
-      }
-   }
-   agentdif++;
-   sprintf(html,"%06d %s\n",agentot,agent);
-   fputs(html,fp_ou);
-   agentot2+=agentot;
-
-   fclose(fp_in);
-   fclose(fp_ou);
-
-   unlink(tmp);
-
-   sprintf(csort,"sort -n -r -k 1,1 -o '%s' '%s'",tmp,tmp2);
-   system(csort);
-
-   unlink(tmp2);
-
-   if((fp_in=fopen(tmp,"r"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp);
-      exit(1);
-   }
-
-   if((fp_ht=fopen(hfile,"a"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
-      exit(1);
-   }
-
-   fputs("<br><br>\n",fp_ht);
-
-   fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht);
-   sprintf(html,"<tr><th align=left bgcolor=%s><font size=%s color=%s>%s</font></th><th bgcolor=%s align=left><font size=%s color=%s>%s</font></th><th align=center bgcolor=%s><font size=%s color=%s>%%</font></th></tr>\n",HeaderBgColor,FontSize,HeaderColor,text[106],HeaderBgColor,FontSize,HeaderColor,text[107],HeaderBgColor,FontSize,HeaderColor);
-   fputs(html,fp_ht);
-
-   while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
-      buf[strlen(buf)-1]='\0';
-      getword(tagent,buf,' ');
-      perc=atoi(tagent) * 100;
-      perc=perc / agentot2;
-
-      sprintf(html,"<tr><td align=left bgcolor=%s><font size=%s>%s</td><td align=right bgcolor=%s><font size=%s>%d</td><td align=right bgcolor=%s><font size=%s>%3.2f</td></tr>\n",TxBgColor,FontSize,buf,TxBgColor,FontSize,atoi(tagent),TxBgColor,FontSize,perc);
-      fputs(html,fp_ht);
-   }
-
-   fputs("</table></html>\n",fp_ht);
-   if(strcmp(ShowSargInfo,"yes") == 0) {
-      zdate(ftime, DateFormat);
-      sprintf(html,"<br><br><center><font size=-2>%s <a href='%s'>%s-%s</a> %s %s</font></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
-      fputs(html,fp_ht);
-   }
-
-   fputs("</table>\n</body>\n</html>\n",fp_ht);
-   fclose(fp_in);
-   fclose(fp_ht);
-
-   unlink(tmp); 
-
-   return;
-      
+void useragent(void)
+{
+       FileObject *fp_log;
+       FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL;
+       char *ptr;
+       char buf[MAXLEN];
+       char ip[MAXLEN], data[MAXLEN], agent[MAXLEN], user[MAXLEN];
+       char ipbefore[MAXLEN], namebefore[MAXLEN];
+       char tagent[MAXLEN];
+       char user_old[MAXLEN]="$#%0a3bc6";
+       char agent_old[MAXLEN]="$#%0a3bc6";
+       char hfile[MAXLEN];
+       char idate[MAXLEN], fdate[MAXLEN];
+       char tmp2[MAXLEN];
+       char tmp3[MAXLEN];
+       char day[4],month[5],year[5], wdate[20];
+       char csort[MAXLEN];
+       const char *FileName;
+       int  agentot=0, agentot2=0, agentdif=0, cont=0, nagent;
+       unsigned long totregsl=0;
+       int cstatus;
+       int ndate;
+       double perc;
+       struct getwordstruct gwarea, gwarea1;
+       longline line;
+       FileListIterator FIter;
+
+       ip[0]='\0';
+       data[0]='\0';
+       agent[0]='\0';
+       user[0]='\0';
+       user_old[0]='\0';
+       agent_old[0]='\0';
+       ipbefore[0]='\0';
+       namebefore[0]='\0';
+
+       snprintf(tmp3,sizeof(tmp3),"%s/squagent.int_unsort",tmp);
+       snprintf(tmp2,sizeof(tmp2),"%s/squagent.int_log",tmp);
+
+       if((fp_ou=fopen(tmp3,"w"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if ((line=longline_create())==NULL) {
+               debuga(__FILE__,__LINE__,_("Not enough memory to read useragent log\n"));
+               exit(EXIT_FAILURE);
+       }
+
+       FIter=FileListIter_Open(UserAgentLog);
+       while ((FileName=FileListIter_Next(FIter))!=NULL)
+       {
+               longline_reset(line);
+               if((fp_log=decomp(FileName))==NULL) {
+                       debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),FileName,FileObject_GetLastOpenError());
+                       exit(EXIT_FAILURE);
+               }
+
+               if(debug) {
+                       debuga(__FILE__,__LINE__,_("Reading useragent log \"%s\"\n"),FileName);
+               }
+
+               while((ptr=longline_read(fp_log,line))!=NULL) {
+                       totregsl++;
+                       getword_start(&gwarea,ptr);
+                       if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 ||
+                               getword(data,sizeof(data),&gwarea,' ')<0) {
+                               debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),FileName);
+                               exit(EXIT_FAILURE);
+                       }
+                       getword_start(&gwarea1,data);
+                       if (getword(day,sizeof(day),&gwarea1,'/')<0 || getword(month,sizeof(month),&gwarea1,'/')<0 ||
+                               getword(year,sizeof(year),&gwarea1,':')<0) {
+                               debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),FileName);
+                               exit(EXIT_FAILURE);
+                       }
+                       if (dfrom!=0 || duntil!=0){
+                               buildymd(day,month,year,wdate,sizeof(wdate));
+                               ndate=atoi(wdate);
+                               if (ndate<dfrom) continue;
+                               if (ndate>duntil) break;
+                       }
+                       if(totregsl == 1)
+                               strcpy(idate,data);
+                       strcpy(fdate,data);
+                       if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) {
+                               debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),FileName);
+                               exit(EXIT_FAILURE);
+                       }
+
+                       if(gwarea.current[0]!='\0') {
+                               if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) {
+                                       debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),FileName);
+                                       exit(EXIT_FAILURE);
+                               }
+                               if(user[0] == '-')
+                                       strcpy(user,ip);
+                               if(user[0] == '\0')
+                                       strcpy(user,ip);
+                       } else {
+                               strcpy(user,ip);
+                       }
+
+                       fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user);
+                       useragent_count++;
+               }
+
+               if (FileObject_Close(fp_log)==EOF) {
+                       debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),FileName,FileObject_GetLastCloseError());
+                       exit(EXIT_FAILURE);
+               }
+       }
+       FileListIter_Close(FIter);
+       longline_destroy(&line);
+
+       if(debug) {
+               debuga(__FILE__,__LINE__,_("   Records read: %ld\n"),totregsl);
+       }
+
+       if (fclose(fp_ou)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tmp3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if(debug) {
+               debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),tmp2);
+       }
+
+       if (snprintf(csort,sizeof(csort),"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3)>=sizeof(csort)) {
+               debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
+               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((fp_in=fopen(tmp2,"r"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
+               exit(EXIT_FAILURE);
+       }
+
+       if (!KeepTempLog && unlink(tmp3)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname);
+       if((fp_ht=fopen(hfile,"w"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),hfile,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if(debug)
+               debuga(__FILE__,__LINE__,_("Making Useragent report\n"));
+
+       write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE);
+       fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report"));
+       fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate,fdate);
+       close_html_header(fp_ht);
+
+       fputs("<br><br>\n",fp_ht);
+
+       fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
+       fputs("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>",fp_ht);
+
+       fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT"));
+
+       while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
+               getword_start(&gwarea,buf);
+               if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),tmp2);
+                       exit(EXIT_FAILURE);
+               }
+
+               if(Ip2Name) {
+                       if(strcmp(ip,ipbefore) != 0) {
+                               strcpy(ipbefore,ip);
+                               ip2name(ip,sizeof(ip));
+                               strcpy(namebefore,ip);
+                       } else strcpy(ip,namebefore);
+               }
+
+               if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),tmp2);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword(user,sizeof(user),&gwarea,'\t')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid user ID in file \"%s\"\n"),tmp2);
+                       exit(EXIT_FAILURE);
+               }
+
+               if(strcmp(user,user_old) != 0) {
+                       fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">",user);
+                       output_html_string(fp_ht,agent,250);
+                       fputs("</td></tr>\n",fp_ht);
+                       strcpy(user_old,user);
+                       strcpy(agent_old,agent);
+               } else if(strcmp(agent,agent_old) != 0) {
+                       fputs("<tr><td></td><td class=\"data2\">",fp_ht);
+                       output_html_string(fp_ht,agent,250);
+                       fputs("</td></tr>\n",fp_ht);
+                       strcpy(agent_old,agent);
+               }
+       }
+
+       fputs("</table>\n",fp_ht);
+       if (fclose(fp_in)==EOF) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) {
+               debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
+               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((fp_in=fopen(tmp3,"r"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
+               exit(EXIT_FAILURE);
+       }
+
+       if (!KeepTempLog && unlink(tmp2)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if((fp_ou=fopen(tmp2,"w"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       agent_old[0]='\0';
+       cont=0;
+
+       while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
+               getword_start(&gwarea,buf);
+               if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),tmp3);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),tmp3);
+                       exit(EXIT_FAILURE);
+               }
+
+               if(!cont) {
+                       cont++;
+                       strcpy(agent_old,agent);
+               }
+
+               if(strcmp(agent,agent_old) != 0) {
+                       agentdif++;
+                       fprintf(fp_ou,"%06d %s\n",agentot,agent_old);
+                       strcpy(agent_old,agent);
+                       agentot2+=agentot;
+                       agentot=0;
+               }
+               agentot++;
+       }
+       agentdif++;
+       fprintf(fp_ou,"%06d %s\n",agentot,agent);
+       agentot2+=agentot;
+
+       if (fclose(fp_ou)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tmp2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if (fclose(fp_in)==EOF) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if (!KeepTempLog && unlink(tmp3)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if (snprintf(csort,sizeof(csort),"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) {
+               debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
+               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((fp_in=fopen(tmp3,"r"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
+               exit(EXIT_FAILURE);
+       }
+
+       if (!KeepTempLog && unlink(tmp2)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       fputs("<br><br>\n",fp_ht);
+
+       fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
+       fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_c\">%%</th></tr>\n",_("AGENT"),_("TOTAL"));
+
+       perc=0.;
+       while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
+               fixendofline(buf);
+               getword_start(&gwarea,buf);
+               if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),tmp3);
+                       exit(EXIT_FAILURE);
+               }
+               nagent=atoi(tagent);
+               perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.;
+
+               fputs("<tr><td class=\"data2\">",fp_ht);
+               output_html_string(fp_ht,gwarea.current,250);
+               fprintf(fp_ht,"</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",nagent,perc);
+       }
+       if (fclose(fp_in)==EOF) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       fputs("</table></div>\n",fp_ht);
+       write_html_trailer(fp_ht);
+       if (fclose(fp_ht)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),hfile,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if (!KeepTempLog && unlink(tmp3)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       return;
 }