]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - util.c
Use boolean to enable all the options instead of string compares.
[thirdparty/sarg.git] / util.c
diff --git a/util.c b/util.c
index aa728d7fe0407fe98d22e1a3e07f38f49606f6f9..13bb78b0be8d7d176c0bca77dab50010da07a8ad 100644 (file)
--- a/util.c
+++ b/util.c
 #include "include/conf.h"
 #include "include/defs.h"
 
+#if defined(HAVE_BACKTRACE)
+#define USE_GETWORD_BACKTRACE 1
+#else
+#define USE_GETWORD_BACKTRACE 0
+#endif
+
 static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
 
 //! The list of the HTTP codes to exclude from the report.
@@ -56,6 +62,26 @@ static char *excludecode=NULL;
     }
 }*/
 
+#if USE_GETWORD_BACKTRACE
+static void getword_backtrace(void)
+{
+   void *buffer[5];
+   int i, n;
+   char **calls;
+
+   n=backtrace(buffer,sizeof(buffer)/sizeof(buffer[0]));
+   if (n<=0) return;
+   calls=backtrace_symbols(buffer,n);
+   if (calls) {
+      fprintf(stderr,"SARG: getword backtrace:\n");
+      for (i=0 ; i<n ; i++) {
+         fprintf(stderr,"SARG: %d:%s\n",i+1,calls[i]);
+      }
+      free(calls);
+   }
+}
+#endif //USE_GETWORD_BACKTRACE
+
 void getword_start(struct getwordstruct *gwarea, const char *line)
 {
    gwarea->beginning=line;
@@ -79,6 +105,9 @@ int getword(char *word, int limit, struct getwordstruct *gwarea, int stop)
         printf("SARG: searching for \'x%x\'\n",stop);
         //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
         word[(limit>0) ? limit-1 : 0]='\0';
+#if USE_GETWORD_BACKTRACE
+         getword_backtrace();
+#endif
         return(-1);
      }
      word[x] = gwarea->current[x];
@@ -102,6 +131,9 @@ int getword_multisep(char *word, int limit, struct getwordstruct *gwarea, int st
         printf("SARG: searching for \'x%x\'\n",stop);
         //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
         if (limit>0) word[limit-1]='\0';
+#if USE_GETWORD_BACKTRACE
+        getword_backtrace();
+#endif
         //exit(1);
         return(-1);
      }
@@ -125,6 +157,9 @@ int getword_skip(int limit, struct getwordstruct *gwarea, int stop)
          printf("SARG: Record=\"%s\"\n",gwarea->current);
          printf("SARG: searching for \'x%x\'\n",stop);
          //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
+#if USE_GETWORD_BACKTRACE
+         getword_backtrace();
+#endif
          return(-1);
       }
    }
@@ -137,7 +172,14 @@ int getword_skip(int limit, struct getwordstruct *gwarea, int stop)
 int getword_atoll(long long int *number, struct getwordstruct *gwarea, int stop)
 {
    int x;
+   int sign=+1;
 
+   if (gwarea->current[0] == '-') {
+      gwarea->current++;
+      sign=-1;
+   } else if (gwarea->current[0] == '+') {
+      gwarea->current++;
+   }
    *number=0LL;
    for(x=0;isdigit(gwarea->current[x]);x++) {
       *number=(*number * 10) + gwarea->current[x]-'0';
@@ -148,8 +190,12 @@ int getword_atoll(long long int *number, struct getwordstruct *gwarea, int stop)
       printf("SARG: Record=\"%s\"\n",gwarea->current);
       printf("SARG: searching for \'x%x\'\n",stop);
       //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
+#if USE_GETWORD_BACKTRACE
+      getword_backtrace();
+#endif
       return(-1);
    }
+   *number*=sign;
 
    if (gwarea->current[x]) ++x;
    gwarea->current+=x;
@@ -180,7 +226,7 @@ long long int my_atoll (const char *nptr)
   return returnval;
 }
 
-static int is_absolute(const char *path)
+int is_absolute(const char *path)
 {
    if (*path=='/') return(1);
 #ifdef WINDOWS
@@ -343,12 +389,9 @@ void name_month(char *month,int month_len)
 
 void fixper(char *tbuf, char *period, const char *duntil)
 {
-
    char warea[50];
    char dia[5], mes[5], ano[5];
-   int  x;
-
-   warea[0]='\0';
+   int x;
 
    strncpy(dia,duntil+6,2);
    dia[2]='\0';
@@ -363,8 +406,10 @@ void fixper(char *tbuf, char *period, const char *duntil)
 
    if(strcmp(df,"e") == 0)
       sprintf(warea,"%s%s%s",dia,mes,ano);
-   if(strcmp(df,"u") == 0)
+   else if(strcmp(df,"u") == 0)
       sprintf(warea,"%s%s%s",ano,mes,dia);
+   else
+      warea[0]='\0';
 
    strcat(period,warea);
 }
@@ -492,9 +537,7 @@ char *fixnum(long long int value, int n)
       if ( k == 2 && i != 0 )  {
          k = 0;
          pbuf[j++] = num[i];
-         if(strcmp(UseComma,"yes") == 0)
-            pbuf[j++] = ',';
-         else pbuf[j++] = '.';
+         pbuf[j++] = (UseComma) ? ',' : '.';
          continue;
       }
       pbuf[j] = num[i];
@@ -534,9 +577,7 @@ char *fixnum2(long long int value, int n)
       if ( k == 2 && i != 0 )  {
          k = 0;
          pbuf[j++] = num[i];
-         if(strcmp(UseComma,"yes") == 0)
-            pbuf[j++] = ',';
-         else pbuf[j++] = '.';
+         pbuf[j++] = (UseComma) ? ',' : '.';
          continue;
       }
       pbuf[j] = num[i];
@@ -753,7 +794,7 @@ void gperiod(const char *dirname, const char *period)
 
 }
 
-void vrfydir(const char *dir, const char *per1, const char *addr, const char *site, const char *us, const char *form)
+void vrfydir(const char *per1, const char *addr, const char *site, const char *us, const char *form)
 {
    FILE *img_in, *img_ou;
    FILE *fp_ou;
@@ -767,18 +808,18 @@ void vrfydir(const char *dir, const char *per1, const char *addr, const char *si
    struct dirent *direntp;
    char y1[5], y2[5];
    char d1[3], d2[3];
-   char m1[4], m2[4];
+   char m1[8], m2[8];
    time_t curtime;
    struct tm *loctm;
    int cstatus;
 
    if(strcmp(IndexTree,"date") == 0) {
-      bzero(y1,5);
-      bzero(y2,5);
-      bzero(d1,3);
-      bzero(d2,3);
-      bzero(m1,4);
-      bzero(m2,4);
+      bzero(y1,sizeof(y1));
+      bzero(y2,sizeof(y2));
+      bzero(d1,sizeof(d1));
+      bzero(d2,sizeof(d2));
+      bzero(m1,sizeof(m1));
+      bzero(m2,sizeof(m2));
       if(strncmp(df,"u",1) == 0) {
          strncpy(y1,period,4);
          strncpy(y2,period+10,4);
@@ -799,39 +840,40 @@ void vrfydir(const char *dir, const char *per1, const char *addr, const char *si
 
       sprintf(wdir,"%s%s",outdir,y1);
       if(strcmp(y1,y2) != 0) {
-         strncat(wdir,"-",1);
-         strncat(wdir,y2,strlen(y2));
+         strcat(wdir,"-");
+         strcat(wdir,y2);
       }
       if(access(wdir, R_OK) != 0)
          my_mkdir(wdir);
 
-      strncat(wdir,"/",1);
-      strncat(wdir,m1,strlen(m1));
+      strcat(wdir,"/");
+      strcat(wdir,m1);
       if(strcmp(m1,m2) != 0) {
-         strncat(wdir,"-",1);
-         strncat(wdir,m2,strlen(m2));
+         strcat(wdir,"-");
+         strcat(wdir,m2);
       }
       if(access(wdir, R_OK) != 0)
          my_mkdir(wdir);
 
-      strncat(wdir,"/",1);
-      strncat(wdir,d1,strlen(d1));
+      strcat(wdir,"/");
+      strcat(wdir,d1);
       if(strcmp(d1,d2) != 0) {
-         strncat(wdir,"-",1);
-         strncat(wdir,d2,strlen(d2));
+         strcat(wdir,"-");
+         strcat(wdir,d2);
       }
-   } else
-      strcpy(wdir,dir);
+   } else {
+      sprintf(wdir, "%s%s", outdir, per1);
+   }
 
-   if(strlen(us) > 0) {
+   if(us[0] != '\0') {
       strcat(wdir,"-");
       strcat(wdir,us);
    }
-   if(strlen(addr) > 0) {
+   if(addr[0] != '\0') {
       strcat(wdir,"-");
       strcat(wdir,addr);
    }
-   if(strlen(site) > 0) {
+   if(site[0] != '\0') {
       strcat(wdir,"-");
       strcat(wdir,site);
    }
@@ -840,8 +882,7 @@ void vrfydir(const char *dir, const char *per1, const char *addr, const char *si
       strcpy(dirname,wdir);
 
    if(strcmp(IndexTree,"date") != 0) {
-      strcpy(dirname2,dirname);
-      if(strcmp(OverwriteReport,"no") == 0) {
+      if(!OverwriteReport) {
          while(num) {
             if(access(wdir,R_OK) == 0) {
                sprintf(wdir,"%s.%d",dirname,num);
@@ -854,12 +895,12 @@ void vrfydir(const char *dir, const char *per1, const char *addr, const char *si
 
          if(count > 0) {
             if(debug)
-               fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname2,text[52],wdir);
-            rename(dirname2,wdir);
+               fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname,text[52],wdir);
+            rename(dirname,wdir);
          }
       } else {
-         if(access(dir,R_OK) == 0) {
-            sprintf(csort,"rm -r \"%s\"",dir);
+         if(access(wdir,R_OK) == 0) {
+            sprintf(csort,"rm -r \"%s\"",wdir);
             cstatus=system(csort);
             if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
                fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
@@ -871,7 +912,7 @@ void vrfydir(const char *dir, const char *per1, const char *addr, const char *si
       my_mkdir(dirname);
    } else {
       strcpy(dirname2,wdir);
-      if(strcmp(OverwriteReport,"no") == 0) {
+      if(!OverwriteReport) {
          while(num) {
             if(access(wdir,R_OK) == 0) {
                sprintf(wdir,"%s.%d",dirname2,num);
@@ -986,15 +1027,12 @@ void zdate(char *ftime,int ftimesize, const char *DateFormat)
 
    t = time(NULL);
    local = localtime(&t);
-   /*
    if(strcmp(DateFormat,"u") == 0)
       strftime(ftime, ftimesize, "%b/%d/%Y %H:%M", local);
    if(strcmp(DateFormat,"e") == 0)
       strftime(ftime, ftimesize, "%d/%b/%Y-%H:%M", local);
    if(strcmp(DateFormat,"w") == 0)
       strftime(ftime, ftimesize, "%V-%H-%M", local);
-   */
-   strftime(ftime, ftimesize, "%x %X", local);
    return;
 }
 
@@ -1043,7 +1081,6 @@ void date_from(char *date, char *dfrom, char *duntil)
    int anou;
    char wdate[50];
 
-
    strncpy(wdate,date,sizeof(wdate)-1);
    wdate[sizeof(wdate)-1]='\0';
    if(strchr(wdate,'-') == NULL) {
@@ -1137,7 +1174,7 @@ void removetmp(const char *outdir)
    FILE *fp_in;
    char warea[256];
 
-   if(strcmp(RemoveTempFiles,"yes") != 0)
+   if(!RemoveTempFiles)
       return;
 
    if(debug) {
@@ -1367,14 +1404,14 @@ void show_info(FILE *fp_ou)
 {
    char ftime[127];
 
-   if(strcmp(ShowSargInfo,"yes") != 0) return;
+   if(!ShowSargInfo) return;
    zdate(ftime, sizeof(ftime), DateFormat);
    fprintf(fp_ou,"<div align=\"center\"><table><tr><td><br><br></td><td class=\"info\">%s <a href='%s'><font class=\"info\">%s-%s</font></a> %s %s</td></tr></table></div>\n",text[108],URL,PGM,VERSION,text[109],ftime);
 }
 
 void show_sarg(FILE *fp_ou, const char *ind)
 {
-   if(strcmp(ShowSargLogo,"yes") == 0) fprintf(fp_ou,"<div align=\"center\"><table class=\"logo\">\n<tr><th><a href=\"http://sarg.sourceforge.net\"><img src=\"%s/images/sarg.png\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\" alt=\"Sarg\"></a>&nbsp;Squid Analysis Report Generator</th></tr>\n<tr><th class=\"title\">&nbsp</th></tr>\n</table></div>\n",ind);
+   if(ShowSargLogo) fprintf(fp_ou,"<div align=\"center\"><table class=\"logo\">\n<tr><th><a href=\"http://sarg.sourceforge.net\"><img src=\"%s/images/sarg.png\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\" alt=\"Sarg\"></a>&nbsp;Squid Analysis Report Generator</th></tr>\n<tr><th class=\"title\">&nbsp</th></tr>\n</table></div>\n",ind);
 }
 
 void write_logo_image(FILE *fp_ou)