]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Don't use strcmp to check strings one or zero characters long
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Tue, 21 Aug 2012 19:02:33 +0000 (21:02 +0200)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Tue, 21 Aug 2012 19:02:33 +0000 (21:02 +0200)
As a side effect, the date format is stored in a single character instead
of a string and df is now the only variable used globally to set the
date format.

14 files changed:
convlog.c
dansguardian_report.c
getconf.c
grepday.c
include/conf.h
include/defs.h
index.c
log.c
readlog.c
realtime.c
redirector.c
smartfilter.c
splitlog.c
util.c

index 5bd70a90ce41d59bed90897ecb9f4b2a1df8e1a0..df3dfdc7e8433ddc27f578d2f0e14c6e737f27d6 100644 (file)
--- a/convlog.c
+++ b/convlog.c
@@ -27,7 +27,7 @@
 #include "include/conf.h"
 #include "include/defs.h"
 
-void convlog(const char *arq, char *df, int dfrom, int duntil)
+void convlog(const char *arq, char df, int dfrom, int duntil)
 {
        FILE *fp_in;
        char *buf;
@@ -67,10 +67,12 @@ void convlog(const char *arq, char *df, int dfrom, int duntil)
                                continue;
                }
 
-               if(df[0]=='e')
+               if (df=='e')
                        strftime(dia, sizeof(dia), "%d/%m/%Y", t);
-               else
+               else if (df=='u')
                        strftime(dia, sizeof(dia), "%m/%d/%Y", t);
+               else //if (df=='w')
+                       strftime(dia, sizeof(dia), "%Y.%U", t);
 
                printf("%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t->tm_min,t->tm_sec,gwarea.current);
        }
index 4405a8145eb4e7066b76594e338959562d05cd34..c3fd140f8dd7d39fcb61db3c241bc637761c4523 100644 (file)
@@ -108,7 +108,7 @@ void dansguardian_report(void)
                        strcpy(user,ip);
 
                bzero(date, 15);
-               if(strncmp(df,"u",1) != 0) {
+               if (df!='u') {
                        strncpy(date,date2+6,2);
                        strcat(date,"/");
                        strncat(date,date2+4,2);
index b4cfba5a53ecbd4ef06f4832ad6f6f8d1c01eb5a..be25115f167e38f9558585016319a1d604d609ae 100644 (file)
--- a/getconf.c
+++ b/getconf.c
@@ -471,8 +471,7 @@ static void parmtest(char *buf)
                        debuga(_("Maybe you have a broken record or garbage in \"date_format\" parameter\n"));
                        exit(EXIT_FAILURE);
                }
-               strncpy(DateFormat,gwarea.current,1);
-               fixnone(DateFormat);
+               DateFormat=gwarea.current[0];
                return;
        }
 
index 84ad3115dc0e9a328e3e45aa594c9707e0bf541e..5dd097dae5b53b6fc51d80b36a6e2dafbbcfadcd 100644 (file)
--- a/grepday.c
+++ b/grepday.c
@@ -547,9 +547,9 @@ static void greport_plot(const struct userinfostruct *uinfo,struct PlotStruct *p
 
        t = time(NULL);
        local = localtime(&t);
-       if(DateFormat[0]=='u')
+       if (df=='u')
                strftime(ftime, sizeof(ftime), "%b/%d/%Y %H:%M", local);
-       if(DateFormat[0]=='e')
+       if (df=='e')
                strftime(ftime, sizeof(ftime), "%d/%b/%Y-%H:%M", local);
 
        x=ImgXSize*5/12;
index 950b7da7b2ca54a677ef62262f7d82273b65a4bf..dd80cd22bed8e054ef01567261598475bdf2e2a9 100755 (executable)
@@ -303,7 +303,7 @@ char parse_out[MAXLEN];
 char arqtt[MAXLEN];
 char html[MAXLEN];
 char ConfigFile[MAXLEN];
-char df[20];
+char df;
 int LastLog;
 bool RemoveTempFiles;
 char ReplaceIndex[256];
@@ -350,7 +350,7 @@ char UserAgentLog[255];
 char module[255];
 char ExcludeHosts[255];
 char ExcludeUsers[255];
-char DateFormat[2];
+char DateFormat;
 char PerUserLimitFile[255];
 int PerUserLimit;
 bool UserIp;
index c3d659527588572c72e4fbe1173387d07b4e7ed9..03f278acc7aae98ea195fb947b87c2d69f9b46c8 100755 (executable)
@@ -125,7 +125,7 @@ void authfail_cleanup(void);
 void ccharset(char *CharSet);
 
 // convlog.c
-void convlog(const char *arq, char *df, int dfrom, int duntil);
+void convlog(const char* arq, char df, int dfrom, int duntil);
 
 // css.c
 void css_content(FILE *fp_css);
@@ -231,7 +231,7 @@ void tmpsort(const struct userinfostruct *uinfo);
 void sort_labels(const char **label,const char **order);
 
 // splitlog.c
-void splitlog(const char *arq, const char *df, int dfrom, int duntil, int convert, const char *splitprefix);
+void splitlog(const char *arq, char df, int dfrom, int duntil, int convert, const char *splitprefix);
 
 // topsites.c
 void topsites(void);
@@ -338,7 +338,7 @@ int getperiod_fromsarglog(const char *arqtt,struct periodstruct *period);
 void getperiod_fromrange(struct periodstruct *period,int dfrom,int duntil);
 int getperiod_buildtext(struct periodstruct *period);
 void removetmp(const char *outdir);
-void zdate(char *ftime,int ftimesize, const char *DateFormat);
+void zdate(char *ftime,int ftimesize, char DateFormat);
 char *get_param_value(const char *param,char *line);
 int compar( const void *, const void * );
 void unlinkdir(const char *dir,bool contentonly);
diff --git a/index.c b/index.c
index d6af010a374a664e3cd4c5a927a5c82d1a178d65..5b2c35e8bcfb650fb18752a21d0a2fae928a0e81 100644 (file)
--- a/index.c
+++ b/index.c
@@ -345,7 +345,7 @@ static void make_file_index(void)
                        debuga(_("not enough memory to sort the index\n"));
                        exit(EXIT_FAILURE);
                }
-               if(strcmp(df,"u") == 0) {
+               if (df=='u') {
                        item->year=atoi(direntp->d_name);
                        item->month=conv_month(direntp->d_name+4);
                        item->day=atoi(direntp->d_name+7);
@@ -484,7 +484,7 @@ static void file_index_to_date_index(const char *entry)
        d1=0;
        d2=0;
        i=0;
-       if(strcmp(df,"u") == 0) {
+       if (df=='u') {
                for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
                        y1=y1*10+(entry[i++]-'0');
                if (j!=4) return;
@@ -508,7 +508,7 @@ static void file_index_to_date_index(const char *entry)
                for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
                        d2=d2*10+(entry[i++]-'0');
                if (j!=2) return;
-       } else if(strcmp(df,"e") == 0) {
+       } else if (df=='e') {
                for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
                        d1=d1*10+(entry[i++]-'0');
                if (j!=2) return;
@@ -655,8 +655,8 @@ static void date_index_to_file_index(const char *entry)
                                continue;
                        }
 
-                       if(strcmp(df,"u") == 0) sprintf(newdir,"%s%04d%s%02d-%04d%s%02d",outdir,y1,sm1,d1,y1,sm2,d2);
-                       else if(strcmp(df,"e") == 0) sprintf(newdir,"%s%02d%s%04d-%02d%s%04d",outdir,d1,sm1,y1,d2,sm2,y1);
+                       if (df=='u') sprintf(newdir,"%s%04d%s%02d-%04d%s%02d",outdir,y1,sm1,d1,y1,sm2,d2);
+                       else if (df=='e') sprintf(newdir,"%s%02d%s%04d-%02d%s%04d",outdir,d1,sm1,y1,d2,sm2,y1);
                        else continue;
                        sprintf(olddir,"%s%04d/%s/%s",outdir,y1,direntp2->d_name,direntp3->d_name);
                        if(rename(olddir,newdir)) {
diff --git a/log.c b/log.c
index de59db5921d93b5af0040eb7711047909f9fc069..6dcc08d1f4780ad766a485747e2ca495c204c328 100644 (file)
--- a/log.c
+++ b/log.c
@@ -129,7 +129,7 @@ int main(int argc,char *argv[])
        strcpy(OutputDir,"/var/www/html/squid-reports");
        AnonymousOutputFiles=false;
        Ip2Name=false;
-       strcpy(DateFormat,"u");
+       DateFormat='u';
        OverwriteReport=false;
        RemoveTempFiles=true;
        strcpy(ReplaceIndex,"index.html");
@@ -204,7 +204,7 @@ int main(int argc,char *argv[])
        tmp[0]='\0';
        us[0]='\0';
        ReadFilter.DateRange[0]='\0';
-       df[0]='\0';
+       df='\0';
        uagent[0]='\0';
        hexclude[0]='\0';
        addr[0]='\0';
@@ -290,7 +290,7 @@ int main(int argc,char *argv[])
                                safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile));
                                break;
                        case 'g':
-                               safe_strcpy(df,optarg,sizeof(df));
+                               df=*optarg;
                                break;
                        case 'h':
                                usage(argv[0]);
@@ -455,14 +455,9 @@ int main(int argc,char *argv[])
        if(DataFile[0] != '\0')
                dataonly++;
 
-       if(df[0] == '\0') strcpy(df,DateFormat);
-       else strcpy(DateFormat,df);
-
-       if(df[0] == '\0') {
-               strcpy(df,"u");
-               strcpy(DateFormat,"u");
-       }
-       if (df[0]=='w')
+       if (df=='\0') df=DateFormat;
+       if (df=='\0') df='u';
+       if (df=='w')
                IndexTree=INDEX_TREE_FILE;
 
        if(NAccessLog == 0) {
@@ -568,11 +563,11 @@ int main(int argc,char *argv[])
                debuga(_("                 Date from-until (-d) = %s\n"),ReadFilter.DateRange);
                debuga(_("   Email address to send reports (-e) = %s\n"),email);
                debuga(_("                     Config file (-f) = %s\n"),ConfigFile);
-               if(strcmp(df,"e") == 0)
+               if (df=='e')
                        debuga(_("                     Date format (-g) = Europe (dd/mm/yyyy)\n"));
-               if(strcmp(df,"u") == 0)
+               else if (df=='u')
                        debuga(_("                     Date format (-g) = USA (mm/dd/yyyy)\n"));
-               if(strcmp(df,"w") == 0)
+               else if (df=='w')
                        debuga(_("                     Date format (-g) = Sites & Users (yyyy/ww)\n"));
                debuga(_("                       IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
                debuga(_("            Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
@@ -601,11 +596,11 @@ int main(int argc,char *argv[])
                printf(_("                 Date from-until (-d) = %s\n"),ReadFilter.DateRange);
                printf(_("   Email address to send reports (-e) = %s\n"),email);
                printf(_("                     Config file (-f) = %s\n"),ConfigFile);
-               if(strcmp(df,"e") == 0)
+               if (df=='e')
                        printf(_("                     Date format (-g) = Europe (dd/mm/yyyy)\n"));
-               if(strcmp(df,"u") == 0)
+               else if (df=='u')
                        printf(_("                     Date format (-g) = USA (mm/dd/yyyy)\n"));
-               if(strcmp(df,"w") == 0)
+               else if (df=='w')
                        printf(_("                     Date format (-g) = Sites & Users (yyyy/ww)\n"));
                printf(_("                       IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
                printf(_("            Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
index f65f1da219212fbf8f6a58b3b84b16505e29f14a..cdc0a3109436761624c206f0eeb87a8b1c36932c 100644 (file)
--- a/readlog.c
+++ b/readlog.c
@@ -720,7 +720,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                id_is_ip=true;
                        } else {
                                id_is_ip=false;
-                               if(strcmp(log_entry.User,"-") == 0 || strcmp(log_entry.User," ") == 0 || strcmp(log_entry.User,"") == 0) {
+                               if ((log_entry.User[0]=='\0') || (log_entry.User[1]=='\0' && (log_entry.User[0]=='-' || log_entry.User[0]==' '))) {
                                        if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
                                                log_entry.User=log_entry.Ip;
                                                id_is_ip=true;
index 746286b9eded9b11b6d18dd3c86b52a5f16ec63b..43f8110aeabd4761fc1465a83bb72550d9f8caea 100755 (executable)
@@ -264,9 +264,9 @@ static void datashow(const char *tmp)
 
                tt=(time_t)dat;
                t=localtime(&tt);
-               if(DateFormat[0]=='u')
+               if (df=='u')
                        strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M", t);
-               else if(DateFormat[0]=='e')
+               else if (df=='e')
                        strftime(tbuf, sizeof(tbuf), "%d-%m-%Y %H:%M", t);
                
                printf("<tr><td class=\"data\">%s</td><td class=\"data3\">%s</td><td class=\"data3\">%s</td><td class=\"data3\">%s</td><td class=\"data2\"><a href=\"http://%s\">%s</td></tr>\n",tbuf,ip,name,typ,url,url);
index 87051a6cf9af49ba928333b115ba317420ef5dee..14643b1d98031d7b4fd353d0bb7a8b8da8666c43 100644 (file)
@@ -188,7 +188,7 @@ static void parse_log(FILE *fp_ou,char *buf)
                id_is_ip=true;
        } else {
                id_is_ip=false;
-               if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) {
+               if (user[0]=='\0' || (user[1]=='\0' && (user[0]=='-' || user[0]==' '))) {
                        if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
                                strcpy(user,ip);
                                id_is_ip=true;
index 240a65b2f84123b5b22c375bfe8cbe18e943b15a..4dd068bcd92d1ae5072a3b1a396f22b4fe34ead9 100644 (file)
@@ -136,7 +136,7 @@ void smartfilter_report(void)
                                fuser=0;
                                fputs("</table>\n",fp_user);
                                if(ShowSargInfo) {
-                                       zdate(ftime, sizeof(ftime), DateFormat);
+                                       zdate(ftime, sizeof(ftime), df);
                                        fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
                                }
                                fputs("</body>\n</html>\n",fp_user);
@@ -190,7 +190,7 @@ void smartfilter_report(void)
        fputs("</table>\n",fp_ou);
 
        if(ShowSargInfo) {
-               zdate(ftime, sizeof(ftime), DateFormat);
+               zdate(ftime, sizeof(ftime), df);
                fprintf(fp_ou,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
        }
 
@@ -200,7 +200,7 @@ void smartfilter_report(void)
        if(fp_user) {
                fputs("</table>\n",fp_user);
                if(ShowSargInfo) {
-                       zdate(ftime, sizeof(ftime), DateFormat);
+                       zdate(ftime, sizeof(ftime), df);
                        fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
                }
                fputs("</body>\n</html>\n",fp_user);
index 82e5a2a015cf6a56378e6404b6db9d6bce2aad28..4a1bd63c9d219810526f86cd8fad233421dd742f 100644 (file)
@@ -44,7 +44,7 @@ US date format.
 \param splitprefix If not empty, the output file is written in separate files (one for each day) and
 the files are named after the day they contain prefixed with the string contained in this variable.
 */
-void splitlog(const char *arq, const char *df, int dfrom, int duntil, int convert, const char *splitprefix)
+void splitlog(const char *arq, char df, int dfrom, int duntil, int convert, const char *splitprefix)
 {
        FILE *fp_in;
        FILE *fp_ou=NULL;
@@ -129,7 +129,7 @@ void splitlog(const char *arq, const char *df, int dfrom, int duntil, int conver
                if(!convert) {
                        fprintf(fp_ou,"%s %s\n",data,gwarea.current);
                } else {
-                       if(df[0]=='e')
+                       if (df=='e')
                                strftime(dia, sizeof(dia), "%d/%m/%Y", t);
                        else
                                strftime(dia, sizeof(dia), "%m/%d/%Y", t);
diff --git a/util.c b/util.c
index 0c4083bc9e35b9efd0569e4912665a1d9c962e73..2291ca45a07652d779b07d434962130fb2401a11 100644 (file)
--- a/util.c
+++ b/util.c
@@ -954,11 +954,11 @@ int getperiod_buildtext(struct periodstruct *period)
        int range;
        char text1[40], text2[40];
 
-       if(df[0]=='u') {
+       if (df=='u') {
                i=strftime(text1, sizeof(text1), "%Y %b %d", &period->start);
-       }else if(df[0]=='e') {
+       } else if(df=='e') {
                i=strftime(text1, sizeof(text1), "%d %b %Y", &period->start);
-       } else /*if(df[0]=='w')*/ {
+       } else /*if (df=='w')*/ {
                IndexTree=INDEX_TREE_FILE;
                i=strftime(text1, sizeof(text1), "%Y.%U", &period->start);
        }
@@ -968,9 +968,9 @@ int getperiod_buildtext(struct periodstruct *period)
               period->start.tm_mon!=period->end.tm_mon ||
               period->start.tm_mday!=period->end.tm_mday);
        if (range) {
-               if(df[0]=='u') {
+               if (df=='u') {
                        i=strftime(text2, sizeof(text2)-i, "%Y %b %d", &period->end);
-               } else if(df[0]=='e') {
+               } else if (df=='e') {
                        i=strftime(text2, sizeof(text2)-i, "%d %b %Y", &period->end);
                } else {
                        i=strftime(text2, sizeof(text2)-i, "%Y.%U", &period->end);
@@ -1086,13 +1086,13 @@ int vrfydir(const struct periodstruct *per1, const char *addr, const char *site,
                wlen+=sprintf(wdir+wlen,"/%02d",d1);
                if(d1!=d2) wlen+=sprintf(wdir+wlen,"-%02d",d2);
        } else {
-               if(df[0] == 'u') {
+               if (df == 'u') {
                        wlen=snprintf(wdir+wlen,sizeof(wdir)-wlen,"%04d%s%02d-%04d%s%02d",y1,
                                conv_month_name(m1),d1,y2,conv_month_name(m2),d2);
-               } else if(df[0] == 'e') {
+               } else if (df == 'e') {
                        wlen=snprintf(wdir+wlen,sizeof(wdir)-wlen,"%02d%s%04d-%02d%s%04d",d1,
                                conv_month_name(m1),y1,d2,conv_month_name(m2),y2);
-               } else if(df[0] == 'w') {
+               } else if (df == 'w') {
                        wlen2=strftime(wdir+wlen, sizeof(wdir)-wlen, "%Y.%U", &per1->start);
                        if (wlen2==0) return(-1);
                        wlen+=wlen2;
@@ -1234,18 +1234,18 @@ void strip_latin(char *line)
        return;
 }
 
-void zdate(char *ftime,int ftimesize, const char *DateFormat)
+void zdate(char *ftime,int ftimesize, char DateFormat)
 {
        time_t t;
        struct tm *local;
 
        t = time(NULL);
        local = localtime(&t);
-       if(strcmp(DateFormat,"u") == 0)
+       if (DateFormat=='u')
                strftime(ftime, ftimesize, "%b/%d/%Y %H:%M", local);
-       if(strcmp(DateFormat,"e") == 0)
+       else if (DateFormat=='e')
                strftime(ftime, ftimesize, "%d/%b/%Y-%H:%M", local);
-       if(strcmp(DateFormat,"w") == 0)
+       else if (DateFormat=='w')
                strftime(ftime, ftimesize, "%W-%H-%M", local);
        return;
 }
@@ -1692,7 +1692,7 @@ void show_info(FILE *fp_ou)
        char ftime[127];
 
        if(!ShowSargInfo) return;
-       zdate(ftime, sizeof(ftime), DateFormat);
+       zdate(ftime, sizeof(ftime), df);
        fprintf(fp_ou,"<div class=\"info\">%s <a href='%s'>%s-%s</a> %s %s</div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
 }