From: Frédéric Marchal Date: Tue, 21 Aug 2012 19:02:33 +0000 (+0200) Subject: Don't use strcmp to check strings one or zero characters long X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=81a022d84b782670df4a4be56ee25ffcfbdd6ea8;p=thirdparty%2Fsarg.git Don't use strcmp to check strings one or zero characters long 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. --- diff --git a/convlog.c b/convlog.c index 5bd70a9..df3dfdc 100644 --- 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); } diff --git a/dansguardian_report.c b/dansguardian_report.c index 4405a81..c3fd140 100644 --- a/dansguardian_report.c +++ b/dansguardian_report.c @@ -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); diff --git a/getconf.c b/getconf.c index b4cfba5..be25115 100644 --- 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; } diff --git a/grepday.c b/grepday.c index 84ad311..5dd097d 100644 --- 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; diff --git a/include/conf.h b/include/conf.h index 950b7da..dd80cd2 100755 --- a/include/conf.h +++ b/include/conf.h @@ -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; diff --git a/include/defs.h b/include/defs.h index c3d6595..03f278a 100755 --- a/include/defs.h +++ b/include/defs.h @@ -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 d6af010..5b2c35e 100644 --- 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 de59db5..6dcc08d 100644 --- 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")); diff --git a/readlog.c b/readlog.c index f65f1da..cdc0a31 100644 --- 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; diff --git a/realtime.c b/realtime.c index 746286b..43f8110 100755 --- a/realtime.c +++ b/realtime.c @@ -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("%s%s%s%s%s\n",tbuf,ip,name,typ,url,url); diff --git a/redirector.c b/redirector.c index 87051a6..14643b1 100644 --- a/redirector.c +++ b/redirector.c @@ -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; diff --git a/smartfilter.c b/smartfilter.c index 240a65b..4dd068b 100644 --- a/smartfilter.c +++ b/smartfilter.c @@ -136,7 +136,7 @@ void smartfilter_report(void) fuser=0; fputs("\n",fp_user); if(ShowSargInfo) { - zdate(ftime, sizeof(ftime), DateFormat); + zdate(ftime, sizeof(ftime), df); fprintf(fp_user,"

%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); } fputs("\n\n",fp_user); @@ -190,7 +190,7 @@ void smartfilter_report(void) fputs("\n",fp_ou); if(ShowSargInfo) { - zdate(ftime, sizeof(ftime), DateFormat); + zdate(ftime, sizeof(ftime), df); fprintf(fp_ou,"

%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); } @@ -200,7 +200,7 @@ void smartfilter_report(void) if(fp_user) { fputs("\n",fp_user); if(ShowSargInfo) { - zdate(ftime, sizeof(ftime), DateFormat); + zdate(ftime, sizeof(ftime), df); fprintf(fp_user,"

%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); } fputs("\n\n",fp_user); diff --git a/splitlog.c b/splitlog.c index 82e5a2a..4a1bd63 100644 --- a/splitlog.c +++ b/splitlog.c @@ -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 0c4083b..2291ca4 100644 --- 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,"
%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); }