/*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
- * 1998, 2013
+ * 1998, 2015
*
* SARG donations:
* please look at http://sarg.sourceforge.net/donations.php
if (n<=0) return;
calls=backtrace_symbols(buffer,n);
if (calls) {
- debuga(_("getword backtrace:\n"));
+ debuga(__FILE__,__LINE__,_("getword backtrace:\n"));
for (i=0 ; i<n ; i++) {
fprintf(stderr,"SARG: %d:%s\n",i+1,calls[i]);
}
void getword_restart(struct getwordstruct *gwarea)
{
if (gwarea->modified) {
- debuga(_("Cannot parse again the line as it was modified\n"));
+ debuga(__FILE__,__LINE__,_("Cannot parse again the line as it was modified\n"));
exit(EXIT_FAILURE);
}
gwarea->current=gwarea->beginning;
for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) {
if(x>=limit) {
- debuga(_("End of word not found in getword after %d bytes.\n"),x);
- debuga(_("Line=\"%s\"\n"),gwarea->beginning);
- debuga(_("Record=\"%s\"\n"),gwarea->current);
- debuga(_("searching for \'x%x\'\n"),stop);
+ /*
+ TRANSLATORS: The %s is the name of the function reporting the
+ error message.
+ */
+ debuga(__FILE__,__LINE__,_("End of word not found in %s after %d bytes.\n"),__func__,x);
+ debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+ debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
word[(limit>0) ? limit-1 : 0]='\0';
#if USE_GETWORD_BACKTRACE
getword_backtrace();
for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) {
if(x>=limit) {
- debuga(_("End of word not found in getword_multisep after %d bytes.\n"),x);
- debuga(_("Line=\"%s\"\n"),gwarea->beginning);
- debuga(_("Record=\"%s\"\n"),gwarea->current);
- debuga(_("searching for \'x%x\'\n"),stop);
+ debuga(__FILE__,__LINE__,_("End of word not found in %s after %d bytes.\n"),__func__,x);
+ debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+ debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
if (limit>0) word[limit-1]='\0';
#if USE_GETWORD_BACKTRACE
getword_backtrace();
for(x=0;(gwarea->current[x] && (gwarea->current[x] != stop ));x++) {
if(x>=limit) {
- debuga(_("End of word not found in getword_skip after %d bytes.\n"),x);
- debuga(_("Line=\"%s\"\n"),gwarea->beginning);
- debuga(_("Record=\"%s\"\n"),gwarea->current);
- debuga(_("searching for \'x%x\'\n"),stop);
+ debuga(__FILE__,__LINE__,_("End of word not found in %s after %d bytes.\n"),__func__,x);
+ debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+ debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
#if USE_GETWORD_BACKTRACE
getword_backtrace();
#endif
TRANSLATORS: The first %s is the function name (in the source code) where the
overflow is detected.
*/
- debuga(_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
return(-1);
}
*number=(*number * 10) + digit;
/*
TRANSLATORS: The %s is the function name, in the source code, where the problem occured.
*/
- debuga(_("End of number not found in %s after %d bytes.\n"),__func__,x);
- debuga(_("Line=\"%s\"\n"),gwarea->beginning);
- debuga(_("Record=\"%s\"\n"),gwarea->current);
- debuga(_("searching for \'x%x\'\n"),stop);
+ debuga(__FILE__,__LINE__,_("End of number not found in %s after %d bytes.\n"),__func__,x);
+ debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+ debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
#if USE_GETWORD_BACKTRACE
getword_backtrace();
#endif
for(x=0;isdigit(gwarea->current[x]);x++) {
digit=gwarea->current[x]-'0';
if (*number > (INT_MAX-digit)/10) {
- debuga(_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
return(-1);
}
*number=(*number * 10) + digit;
}
if(gwarea->current[x] && gwarea->current[x]!=stop) {
- debuga(_("End of number not found in %s after %d bytes.\n"),__func__,x);
- debuga(_("Line=\"%s\"\n"),gwarea->beginning);
- debuga(_("Record=\"%s\"\n"),gwarea->current);
- debuga(_("searching for \'x%x\'\n"),stop);
+ debuga(__FILE__,__LINE__,_("End of number not found in %s after %d bytes.\n"),__func__,x);
+ debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+ debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
#if USE_GETWORD_BACKTRACE
getword_backtrace();
#endif
for(x=0;isdigit(gwarea->current[x]);x++) {
digit=gwarea->current[x]-'0';
if (*number > (LONG_MAX-digit)/10) {
- debuga(_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
return(-1);
}
*number=(*number * 10) + digit;
}
if(gwarea->current[x] && gwarea->current[x]!=stop) {
- debuga(_("End of number not found in %s after %d bytes.\n"),__func__,x);
- debuga(_("Line=\"%s\"\n"),gwarea->beginning);
- debuga(_("Record=\"%s\"\n"),gwarea->current);
- debuga(_("searching for \'x%x\'\n"),stop);
+ debuga(__FILE__,__LINE__,_("End of number not found in %s after %d bytes.\n"),__func__,x);
+ debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+ debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
#if USE_GETWORD_BACKTRACE
getword_backtrace();
#endif
int digit;
if (gwarea->current[0] == '-') {
- debuga(_("getword_atolu got a negative number.\n"));
- debuga(_("Line=\"%s\"\n"),gwarea->beginning);
- debuga(_("Record=\"%s\"\n"),gwarea->current);
+ debuga(__FILE__,__LINE__,_("getword_atolu got a negative number.\n"));
+ debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
return(-1);
}
if (gwarea->current[0] == '+') {
for(x=0;isdigit(gwarea->current[x]);x++) {
digit=gwarea->current[x]-'0';
if (*number > (ULONG_MAX-digit)/10) {
- debuga(_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Integer overflow detected in %s in line %s\n"),__func__,gwarea->beginning);
return(-1);
}
*number=(*number * 10) + digit;
}
if(gwarea->current[x] && gwarea->current[x]!=stop) {
- debuga(_("End of number not found in %s after %d bytes.\n"),__func__,x);
- debuga(_("Line=\"%s\"\n"),gwarea->beginning);
- debuga(_("Record=\"%s\"\n"),gwarea->current);
- debuga(_("searching for \'x%x\'\n"),stop);
+ debuga(__FILE__,__LINE__,_("End of number not found in %s after %d bytes.\n"),__func__,x);
+ debuga(__FILE__,__LINE__,_("Line=\"%s\"\n"),gwarea->beginning);
+ debuga(__FILE__,__LINE__,_("Record=\"%s\"\n"),gwarea->current);
+ debuga(__FILE__,__LINE__,_("searching for \'x%x\'\n"),stop);
#if USE_GETWORD_BACKTRACE
getword_backtrace();
#endif
int start;
if (orig_line && orig_line!=gwarea->beginning) {
- debuga(_("Invalid buffer passed to getword_ptr\n"));
+ debuga(__FILE__,__LINE__,_("Invalid buffer passed to getword_ptr\n"));
return(-1);
}
int chars;
if(!is_absolute(name)) {
- debuga(_("Invalid path (%s). Please, use absolute paths only.\n"),name);
+ debuga(__FILE__,__LINE__,_("Invalid path (%s). Please, use absolute paths only.\n"),name);
exit(EXIT_FAILURE);
}
chars=0;
for (i=0 ; name[i] ; i++) {
if (i>=sizeof(w0)) {
- debuga(_("Path too long: "));
+ debuga(__FILE__,__LINE__,_("Path too long: "));
debuga_more("%s\n",name);
exit(EXIT_FAILURE);
}
w0[i] = '\0';
if (access(w0, R_OK) != 0) {
if (PortableMkDir(w0,0755)) {
- debuga(_("Cannot create directory \"%s\": %s\n"),w0,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot create directory \"%s\": %s\n"),w0,strerror(errno));
exit(EXIT_FAILURE);
}
}
if (access(name, R_OK) != 0) {
if (PortableMkDir(name,0755)) {
- debuga(_("Cannot create directory \"%s\": %s\n"),name,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot create directory \"%s\": %s\n"),name,strerror(errno));
exit(EXIT_FAILURE);
}
}
ssize--;
if (len>ssize) {
- debuga(_("The requested number of digits passed to my_lltoa (%d) is bigger than the output buffer size (%d)\n"),len,ssize);
+ debuga(__FILE__,__LINE__,_("The requested number of digits passed to my_lltoa (%d) is bigger than the output buffer size (%d)\n"),len,ssize);
abort();
}
\param msg The printf like message to format.
\param ... The arguments to format in the message.
*/
-void debuga(const char *msg,...)
+void debuga(const char *File,int Line,const char *msg,...)
{
va_list ap;
- fputs(_("SARG: "),stderr);
+ if (debugz>=LogLevel_Source) {
+ /* The path is removed because every source file is in the same directory.
+ * There is no point in reporting the full path from the build directory.
+ */
+ const char *ptr=strrchr(File,'/');
+ if (!ptr) ptr=File;
+ /* TRANSLATORS: This is the prefix to stderr messages when the debug level is
+ set to display the source file (%s) and the line number (%d). */
+ fprintf(stderr,_("SARG(%s:%d): "),ptr,Line);
+ } else {
+ /* TRANSLATORS: This is the prefix to stderr messages when the debug level
+ is low. */
+ fputs(_("SARG: "),stderr);
+ }
va_start(ap,msg);
vfprintf(stderr,msg,ap);
va_end(ap);
\param msg The printf like message to format.
\param ... The arguments to format in the message.
*/
-void debugaz(const char *msg,...)
+void debugaz(const char *File,int Line,const char *msg,...)
{
va_list ap;
- fputs(_("SARG: (info) "),stderr);
+ if (debugz>=LogLevel_Source) {
+ /* The path is removed because every source file is in the same directory.
+ * There is no point in reporting the full path from the build directory.
+ */
+ const char *ptr=strrchr(File,'/');
+ if (!ptr) ptr=File;
+ /* TRANSLATORS: This is the prefix to information messages when the debug level is
+ set to display the source file (%s) and the line number (%d). */
+ fprintf(stderr,_("SARG(%s:%d): (info) "),ptr,Line);
+ } else {
+ /* TRANSLATORS: This is the prefix to information messages when the debug level
+ is low. */
+ fputs(_("SARG: (info) "),stderr);
+ }
va_start(ap,msg);
vfprintf(stderr,msg,ap);
va_end(ap);
char wdir[MAXLEN];
if (snprintf(wdir,sizeof(wdir),"%s%s/sarg-date",dirname,name)>=sizeof(wdir)) {
- debuga(_("Buffer too small to store "));
+ debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s%s/sarg-date",dirname,name);
exit(EXIT_FAILURE);
}
if ((fp_in = fopen(wdir, "rt")) == 0) {
if (snprintf(wdir,sizeof(wdir),"%s%s/date",dirname,name)>=sizeof(wdir)) {
- debuga(_("Buffer too small to store "));
+ debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s%s/date",dirname,name);
exit(EXIT_FAILURE);
}
}
if (!fgets(data,80,fp_in)) {
- debuga(_("Failed to read the date in %s\n"),wdir);
+ debuga(__FILE__,__LINE__,_("Failed to read the date in file \"%s\"\n"),wdir);
+ exit(EXIT_FAILURE);
+ }
+ if (fclose(fp_in)==EOF) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdir,strerror(errno));
exit(EXIT_FAILURE);
}
- fclose(fp_in);
fixendofline(data);
return(0);
int nuser;
if (snprintf(wdir,sizeof(wdir),"%s%s/sarg-users",dirname,name)>=sizeof(wdir)) {
- debuga(_("Buffer too small to store "));
+ debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s%s/sarg-users",dirname,name);
exit(EXIT_FAILURE);
}
if((fp_in=fopen(wdir,"r"))==NULL) {
if (snprintf(wdir,sizeof(wdir),"%s%s/users",dirname,name)>=sizeof(wdir)) {
- debuga(_("Buffer too small to store "));
+ debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s%s/users",dirname,name);
exit(EXIT_FAILURE);
}
}
if (!fgets(tuser,sizeof(tuser),fp_in)) {
- debuga(_("Failed to read the number of users in %s\n"),wdir);
+ debuga(__FILE__,__LINE__,_("Failed to read the number of users in file \"%s\"\n"),wdir);
+ exit(EXIT_FAILURE);
+ }
+ if (fclose(fp_in)==EOF) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdir,strerror(errno));
exit(EXIT_FAILURE);
}
- fclose(fp_in);
nuser=atoi(tuser);
return(nuser);
void obttotal(const char *dirname, const char *name, int nuser, long long int *tbytes, long long int *media)
{
- FILE *fp_in;
+ FileObject *fp_in;
char *buf;
char wdir[MAXLEN];
char user[MAX_USER_LEN];
*media=0;
if (snprintf(wdir,sizeof(wdir),"%s%s/sarg-general",dirname,name)>=sizeof(wdir)) {
- debuga(_("Buffer too small to store "));
+ debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s%s/sarg-general",dirname,name);
exit(EXIT_FAILURE);
}
- if ((fp_in = fopen(wdir, "r")) == 0) {
+ if ((fp_in = FileObject_Open(wdir)) == NULL) {
if (snprintf(wdir,sizeof(wdir),"%s%s/general",dirname,name)>=sizeof(wdir)) {
- debuga(_("Buffer too small to store "));
+ debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s%s/general",dirname,name);
exit(EXIT_FAILURE);
}
- if ((fp_in = fopen(wdir, "r")) == 0) {
+ if ((fp_in = FileObject_Open(wdir)) == NULL) {
return;
}
}
if ((line=longline_create())==NULL) {
- debuga(_("Not enough memory to read the file %s\n"),wdir);
+ debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),wdir);
exit(EXIT_FAILURE);
}
continue;
getword_start(&gwarea,buf);
if (getword(user,sizeof(user),&gwarea,sep)<0) {
- debuga(_("There is a invalid user in file %s\n"),wdir);
+ debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),wdir);
exit(EXIT_FAILURE);
}
if(strcmp(user,"TOTAL") != 0)
continue;
if (getword_skip(MAXLEN,&gwarea,sep)<0) {
- debuga(_("There a broken total number of access in file %s\n"),wdir);
+ debuga(__FILE__,__LINE__,_("Invalid total number of accesses in file \"%s\"\n"),wdir);
exit(EXIT_FAILURE);
}
if (getword_atoll(tbytes,&gwarea,sep)<0) {
- debuga(_("There is a broken number of bytes in file %s\n"),wdir);
+ debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),wdir);
exit(EXIT_FAILURE);
}
break;
}
- fclose(fp_in);
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdir,FileObject_GetLastCloseError());
+ exit(EXIT_FAILURE);
+ }
longline_destroy(&line);
if (nuser <= 0)
struct stat info;
if (snprintf(images,sizeof(images),"%simages",outdir)>=sizeof(images)) {
- debuga(_("Cannot copy images to target directory %simages\n"),outdir);
+ debuga(__FILE__,__LINE__,_("Cannot copy images to target directory %simages\n"),outdir);
exit(EXIT_FAILURE);
}
if (access(images,R_OK)!=0) {
if (PortableMkDir(images,0755)) {
- debuga(_("Cannot create directory \"%s\": %s\n"),images,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot create directory \"%s\": %s\n"),images,strerror(errno));
exit(EXIT_FAILURE);
}
}
dirp = opendir(ImageDir);
if(dirp==NULL) {
- debuga(_("Cannot open directory \"%s\": %s\n"),ImageDir,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open directory \"%s\": %s\n"),ImageDir,strerror(errno));
return;
}
while ((direntp = readdir( dirp )) != NULL ){
if(direntp->d_name[0]=='.')
continue;
if (snprintf(srcfile,sizeof(srcfile),"%s/%s",ImageDir,direntp->d_name)>=sizeof(srcfile)) {
- debuga(_("Buffer too small to store "));
+ debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s/%s",ImageDir,direntp->d_name);
exit(EXIT_FAILURE);
}
if (stat(srcfile,&info)) {
- debuga(_("Cannot stat \"%s\": %s\n"),srcfile,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot stat \"%s\": %s\n"),srcfile,strerror(errno));
continue;
}
if (S_ISREG(info.st_mode)) {
if (snprintf(dstfile,sizeof(dstfile),"%s/%s",images,direntp->d_name)>=sizeof(dstfile)) {
- debuga(_("Buffer too small to store "));
+ debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s/%s",images,direntp->d_name);
exit(EXIT_FAILURE);
}
if(img_ou!=NULL) {
while ((nread = fread(buffer,1,sizeof(buffer),img_in))>0) {
if (fwrite(buffer,1,nread,img_ou)!=nread) {
- debuga(_("Failed to copy image %s to %s\n"),srcfile,dstfile);
+ debuga(__FILE__,__LINE__,_("Failed to copy image %s to %s\n"),srcfile,dstfile);
break;
}
}
if (fclose(img_ou)==EOF) {
- debuga(_("Error while copying image %s: %s\n"),dstfile,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),dstfile,strerror(errno));
exit(EXIT_FAILURE);
}
} else
- debuga(_("Cannot open file \"%s\": %s\n"), dstfile, strerror(errno));
- fclose(img_in);
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"), dstfile, strerror(errno));
+ if (fclose(img_in)==EOF) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),srcfile,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
} else
- debuga(_("Cannot open file \"%s\": %s\n"), srcfile, strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"), srcfile, strerror(errno));
}
}
(void) closedir(dirp);
return(true);
}
-int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us, const char *form)
+/*!
+ * Create a directory to generate a report for the specified connection data
+ * and populate it with the a <tt>sarg-date</tt> file containing the current
+ * date.
+ *
+ * The function also create an <tt>images</tt> directory in \a dir and copy all
+ * the files from the <tt>SYSCONFDIR/images</tt> into that directory.
+ *
+ * \param per1 The date range in the form: YYYYMMMDD-YYYYMMMDD or DDMMMYYYY-DDMMMYYYY depending on the value of
+ * ::DateFormat.
+ * \param addr The ip address or host name to which the report is limited. If the string is empty, all the addresses are accepted.
+ * \param site The destination site to which the report is limited. If the string is empty, all the sites are accepted.
+ * \param us The user to whom the report is limited. It is an empty string if all the users are accepted.
+ */
+int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us)
{
FILE *fp_ou;
- int num=1, count=0;
char wdir[MAXLEN];
- char dirname2[MAXLEN];
int y1, y2;
int m1, m2;
int d1, d2;
strcpy(outdirname,wdir);
- if(IndexTree != INDEX_TREE_DATE) {
- if(!OverwriteReport) {
- while(num) {
- if(access(wdir,R_OK) == 0) {
- sprintf(wdir,"%s.%d",outdirname,num);
- num++;
- count++;
- } else
- break;
- }
+ // manufacture a new unique name if configured to keep old reports or overwrite old report if configured to do so
+ if (!OverwriteReport) {
+ int num=1;
- if(count > 0) {
- if(debug)
- debuga(_("File %s already exists, moved to %s\n"),outdirname,wdir);
- rename(outdirname,wdir);
- }
- } else {
- if(access(outdirname,R_OK) == 0) {
- unlinkdir(outdirname,1);
- }
+ while (access(wdir,R_OK)==0 || errno==EACCES) //file exist or can't be read
+ {
+ sprintf(wdir,"%s.%d",outdirname,num);
+ num++;
+ }
+ if (num>1) {
+ if(debug)
+ debuga(__FILE__,__LINE__,_("File %s already exists, moved to %s\n"),outdirname,wdir);
+ rename(outdirname,wdir);
}
- my_mkdir(outdirname);
} else {
- strcpy(dirname2,wdir);
- if(!OverwriteReport) {
- while(num) {
- if(access(wdir,R_OK) == 0) {
- sprintf(wdir,"%s.%d",dirname2,num);
- num++;
- count++;
- } else
- break;
- }
-
- if(count > 0) {
- if(debug)
- debuga(_("File %s already exists, moved to %s\n"),dirname2,wdir);
- rename(dirname2,wdir);
- strcpy(dirname2,wdir);
- }
- } else {
- if(access(wdir,R_OK) == 0) {
- unlinkdir(wdir,1);
- }
+ if(access(outdirname,R_OK) == 0) {
+ unlinkdir(outdirname,1);
}
-
- if(access(wdir, R_OK) != 0)
- my_mkdir(wdir);
}
+ my_mkdir(outdirname);
- strcpy(dirname2,wdir);
-
+ // create sarg-date to keep track of the report creation date
if (snprintf(wdir,sizeof(wdir),"%s/sarg-date",outdirname)>=sizeof(wdir)) {
- debuga(_("Buffer too small to store "));
+ debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s/sarg-date",outdirname);
exit(EXIT_FAILURE);
}
if ((fp_ou = fopen(wdir, "wt")) == 0) {
- debuga(_("cannot open %s for writing: %s\n"),wdir,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdir,strerror(errno));
perror("SARG:");
exit(EXIT_FAILURE);
}
loctm=localtime(&curtime);
strftime(wdir,sizeof(wdir),"%Y-%m-%d %H:%M:%S",loctm);
if (fprintf(fp_ou,"%s %d\n",wdir,loctm->tm_isdst)<0) {
- debuga(_("Failed to write the date in %s\n"),wdir);
+ debuga(__FILE__,__LINE__,_("Failed to write the date in %s\n"),wdir);
perror("SARG:");
exit(EXIT_FAILURE);
}
if (fclose(fp_ou)==EOF) {
- debuga(_("Failed to write the date in %s: %s\n"),wdir,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),wdir,strerror(errno));
exit(EXIT_FAILURE);
}
void safe_strcpy(char *dest,const char *src,int length)
{
if (length<=0) {
- debuga(_("Invalid buffer length passed to the function to safely copy a string\n"));
+ debuga(__FILE__,__LINE__,_("Invalid buffer length passed to the function to safely copy a string\n"));
exit(EXIT_FAILURE);
}
strncpy(dest,src,length-1);
int next=-1;
if (sscanf(date,"%d/%d/%d%n",&d0,&m0,&y0,&next)!=3 || y0<100 || m0<1 || m0>12 || d0<1 || d0>31 || next<0) {
- debuga(_("The date passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
+ debuga(__FILE__,__LINE__,_("The date passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
exit(EXIT_FAILURE);
}
if (date[next]=='-') {
if (sscanf(date+next+1,"%d/%d/%d",&d1,&m1,&y1)!=3 || y1<100 || m1<1 || m1>12 || d1<1 || d1>31) {
- debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
+ debuga(__FILE__,__LINE__,_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
exit(EXIT_FAILURE);
}
} else if (date[next]!='\0') {
- debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
+ debuga(__FILE__,__LINE__,_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
exit(EXIT_FAILURE);
} else {
d1=d0;
struct tm *Date0,Date1;
if (time(&Today)==(time_t)-1) {
- debuga(_("Failed to get the current time\n"));
+ debuga(__FILE__,__LINE__,_("Failed to get the current time\n"));
exit(EXIT_FAILURE);
}
if (sscanf(date,"day-%d",&i)==1) {
if (i<0) {
- debuga(_("Invalid number of days in -d parameter\n"));
+ debuga(__FILE__,__LINE__,_("Invalid number of days in -d parameter\n"));
exit(EXIT_FAILURE);
}
Today-=i*24*60*60;
Date0=localtime(&Today);
if (Date0==NULL) {
- debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
exit(EXIT_FAILURE);
}
y0=y1=Date0->tm_year+1900;
time_t WeekBegin;
if (i<0) {
- debuga(_("Invalid number of weeks in -d parameter\n"));
+ debuga(__FILE__,__LINE__,_("Invalid number of weeks in -d parameter\n"));
exit(EXIT_FAILURE);
}
Date0=localtime(&Today);
if (Date0==NULL) {
- debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
exit(EXIT_FAILURE);
}
WeekBegin=Today-((Date0->tm_wday-FirstWeekDay+7)%7)*24*60*60;
WeekBegin-=i*7*24*60*60;
Date0=localtime(&WeekBegin);
if (Date0==NULL) {
- debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
exit(EXIT_FAILURE);
}
y0=Date0->tm_year+1900;
WeekBegin+=6*24*60*60;
Date0=localtime(&WeekBegin);
if (Date0==NULL) {
- debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
exit(EXIT_FAILURE);
}
y1=Date0->tm_year+1900;
d1=Date0->tm_mday;
} else if (sscanf(date,"month-%d",&i)==1) {
if (i<0) {
- debuga(_("Invalid number of months in -d parameter\n"));
+ debuga(__FILE__,__LINE__,_("Invalid number of months in -d parameter\n"));
exit(EXIT_FAILURE);
}
Date0=localtime(&Today);
if (Date0==NULL) {
- debuga(_("Cannot convert local time: %s\n"),strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot convert local time: %s\n"),strerror(errno));
exit(EXIT_FAILURE);
}
if (Date0->tm_mon<i%12) {
m1=Date0->tm_mon+1;
d1=Date0->tm_mday;
} else {
- debuga(_("Invalid date range passed on command line\n"));
+ debuga(__FILE__,__LINE__,_("Invalid date range passed on command line\n"));
exit(EXIT_FAILURE);
}
}
return;
if(debug) {
- debuga(_("Purging temporary file sarg-general\n"));
+ debuga(__FILE__,__LINE__,_("Purging temporary file sarg-general\n"));
}
if (snprintf(filename,sizeof(filename),"%s/sarg-general",outdir)>=sizeof(filename)) {
- debuga(_("Path too long: "));
+ debuga(__FILE__,__LINE__,_("Path too long: "));
debuga_more("%s/sarg-period\n",outdir);
exit(EXIT_FAILURE);
}
if((fp_gen=fopen(filename,"w"))==NULL){
- debuga(_("Cannot open file \"%s\": %s\n"),filename,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),filename,strerror(errno));
exit(EXIT_FAILURE);
}
totalger(fp_gen,filename);
if (fclose(fp_gen)==EOF) {
- debuga(_("Failed to close %s after writing the total line: %s\n"),filename,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),filename,strerror(errno));
exit(EXIT_FAILURE);
}
}
return;
if((fp_in=fopen(ExcludeCodes,"r"))==NULL) {
- debuga(_("Cannot open file \"%s\": %s\n"),ExcludeCodes,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ExcludeCodes,strerror(errno));
exit(EXIT_FAILURE);
}
if (fseek(fp_in, 0, SEEK_END)==-1) {
- debuga(_("Failed to move till the end of the excluded codes file %s: %s\n"),ExcludeCodes,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Failed to move till the end of file \"%s\": %s\n"),ExcludeCodes,strerror(errno));
exit(EXIT_FAILURE);
}
MemSize = ftell(fp_in);
if (MemSize<0) {
- debuga(_("Cannot get the size of file %s\n"),ExcludeCodes);
+ debuga(__FILE__,__LINE__,_("Cannot get the size of file \"%s\"\n"),ExcludeCodes);
exit(EXIT_FAILURE);
}
if (fseek(fp_in, 0, SEEK_SET)==-1) {
- debuga(_("Failed to rewind the excluded codes file %s: %s\n"),ExcludeCodes,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Failed to rewind file \"%s\": %s\n"),ExcludeCodes,strerror(errno));
exit(EXIT_FAILURE);
}
MemSize+=1;
if((excludecode=(char *) malloc(MemSize))==NULL) {
- debuga(_("malloc error (%ld bytes required)\n"),MemSize);
+ debuga(__FILE__,__LINE__,_("malloc error (%ld bytes required)\n"),MemSize);
exit(EXIT_FAILURE);
}
memset(excludecode,0,MemSize);
for (i=strlen(data)-1 ; i>=0 && (unsigned char)data[i]<=' ' ; i--) data[i]='\0';
if (i<0) continue;
if (Stored+i+2>=MemSize) {
- debuga(_("Too many codes to exclude in file %s\n"),ExcludeCodes);
+ debuga(__FILE__,__LINE__,_("Too many codes to exclude in file \"%s\"\n"),ExcludeCodes);
break;
}
strcat(excludecode,data);
Stored+=i+1;
}
- fclose(fp_in);
+ if (fclose(fp_in)==EOF) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),ExcludeCodes,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
return;
}
if(!ShowSargInfo) return;
zdate(ftime, sizeof(ftime), df);
fputs("<div class=\"info\">",fp_ou);
- fprintf(fp_ou,_("Generated by <a href='%s'>%s-%s</a> on %s"),URL,PGM,VERSION,ftime);
+ fprintf(fp_ou,_("Generated by <a href=\"%s\">%s-%s</a> on %s"),URL,PGM,VERSION,ftime);
fputs("</div>\n",fp_ou);
}
printf(_("SARG Version: %s\n"),VERSION);
#if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
if (debug) {
- printf(_("\nFor the translation to work, a valid message file should be installed as "
+ printf(_("\nFor the translation to work, a valid message file should be copied to "
"\"%s/<Locale>/LC_MESSAGES/%s.mo\" where <Locale> is derived from the effective locale.\n"),LOCALEDIR,PACKAGE_NAME);
if (CurrentLocale) {
printf(_("Currently effective locale is \"%s\".\n"),CurrentLocale);
printf(_("If this message is in English, then your language is not supported or not correctly installed.\n"));
}
#endif
+ if (debug) {
+#ifdef HAVE_GLOB_H
+ printf(_("File globbing compiled in.\n"));
+#else
+ printf(_("File globbing NOT compiled in.\n"));
+#endif
+ }
exit(EXIT_SUCCESS);
}
(direntp->d_name[1] == '.' && direntp->d_name[2] == '\0')))
continue;
if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) {
- debuga(_("Path too long: "));
+ debuga(__FILE__,__LINE__,_("Path too long: "));
debuga_more("%s/%s\n",dir,direntp->d_name);
exit(EXIT_FAILURE);
}
err=stat(dname,&st);
#endif
if (err) {
- debuga(_("Cannot stat \"%s\": %s\n"),dname,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot stat \"%s\": %s\n"),dname,strerror(errno));
exit(EXIT_FAILURE);
}
if (S_ISREG(st.st_mode)) {
if (unlink(dname)) {
- debuga(_("Cannot delete \"%s\": %s\n"),dname,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),dname,strerror(errno));
exit(EXIT_FAILURE);
}
} else if (S_ISDIR(st.st_mode)) {
unlinkdir(dname,0);
} else {
- debuga(_("Unknown path type %s\n"),dname);
+ debuga(__FILE__,__LINE__,_("Don't know how to delete \"%s\" (not a regular file nor a directory)\n"),dname);
}
}
closedir(dirp);
if (!contentonly) {
if (rmdir(dir)) {
- debuga(_("Cannot delete \"%s\": %s\n"),dir,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),dir,strerror(errno));
exit(EXIT_FAILURE);
}
}
".utmp",
".ip",
"lastlog1",
- "lastlog"
+ "lastlog",
+ "emailrep"
};
dirp=opendir(dir);
if (dlen>=elen && strcasecmp(direntp->d_name+dlen-elen,TmpExt[i])==0) break;
}
if (i<0) {
- debuga(_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
+ debuga(__FILE__,__LINE__,_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
"Please check the temporary directory you gave to sarg. Adjust the path to a safe "
"directory or manually delete the content of \"%s\"\n"),direntp->d_name,dir,dir);
exit(EXIT_FAILURE);
}
if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) {
- debuga(_("Path too long: "));
+ debuga(__FILE__,__LINE__,_("Path too long: "));
debuga_more("%s/%s\n",dir,direntp->d_name);
exit(EXIT_FAILURE);
}
err=stat(dname,&st);
#endif
if (err) {
- debuga(_("Cannot stat \"%s\": %s\n"),dname,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot stat \"%s\": %s\n"),dname,strerror(errno));
exit(EXIT_FAILURE);
}
- if (S_ISDIR(st.st_mode)) {
- unlinkdir(dname,0);
- } else if (!S_ISREG(st.st_mode)) {
- debuga(_("Unknown path type for \"%s\". Check temporary directory\n"),dname);
+ if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) {
+ debuga(__FILE__,__LINE__,_("Unknown path type for \"%s\". Check temporary directory\n"),dname);
exit(EXIT_FAILURE);
}
}
if (dlen>=elen && strcasecmp(direntp->d_name+dlen-elen,TmpExt[i])==0) break;
}
if (i<0) {
- debuga(_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
+ debuga(__FILE__,__LINE__,_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
"Please check the temporary directory you gave to sarg. Adjust the path to a safe "
"directory or manually delete the content of \"%s\"\n"),direntp->d_name,dir,dir);
exit(EXIT_FAILURE);
}
if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) {
- debuga(_("Path too long: "));
+ debuga(__FILE__,__LINE__,_("Path too long: "));
debuga_more("%s/%s\n",dir,direntp->d_name);
exit(EXIT_FAILURE);
}
err=stat(dname,&st);
#endif
if (err) {
- debuga(_("Cannot stat \"%s\": %s\n"),dname,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot stat \"%s\": %s\n"),dname,strerror(errno));
exit(EXIT_FAILURE);
}
- if (S_ISREG(st.st_mode)) {
+ if (S_ISDIR(st.st_mode)) {
+ unlinkdir(dname,0);
+ } else if (S_ISREG(st.st_mode)) {
if (unlink(dname)) {
- debuga(_("Cannot delete \"%s\": %s\n"),dname,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),dname,strerror(errno));
exit(EXIT_FAILURE);
}
} else {
- debuga(_("Unknown path type %s\n"),dname);
+ debuga(__FILE__,__LINE__,_("Don't know how to delete \"%s\" (not a regular file)\n"),dname);
}
}
closedir(dirp);