close filter files, more error handling

This commit is contained in:
cathugger 2020-05-22 22:30:14 +00:00
parent e933a9b806
commit a3e141e79b
No known key found for this signature in database
GPG key ID: 9BADDA2DAF6F01A8
2 changed files with 11 additions and 5 deletions

View file

@ -500,8 +500,10 @@ static bool loadfilterfile(const char *fname)
{ {
char buf[128]; char buf[128];
FILE *f = fopen(fname,"r"); FILE *f = fopen(fname,"r");
if (!f) if (!f) {
fprintf(stderr,"failed to load filter file \"%s\": %s\n",fname,strerror(errno));
return false; return false;
}
while (fgets(buf,sizeof(buf),f)) { while (fgets(buf,sizeof(buf),f)) {
for (char *p = buf;*p;++p) { for (char *p = buf;*p;++p) {
if (*p == '\n') { if (*p == '\n') {
@ -512,5 +514,11 @@ static bool loadfilterfile(const char *fname)
if (*buf && *buf != '#' && memcmp(buf,"//",2) != 0) if (*buf && *buf != '#' && memcmp(buf,"//",2) != 0)
filters_add(buf); filters_add(buf);
} }
int fe = ferror(f);
fclose(f);
if (fe != 0) {
fprintf(stderr,"failure while reading filter file \"%s\": %s\n",fname,strerror(fe));
return false;
}
return true; return true;
} }

6
main.c
View file

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
@ -258,11 +259,8 @@ int main(int argc,char **argv)
} }
else if (*arg == 'f') { else if (*arg == 'f') {
if (argc--) { if (argc--) {
const char *filename = *argv++; if (!loadfilterfile(*argv++))
if (!loadfilterfile(filename)) {
fprintf(stderr,"failed to load filter file %s\n",filename);
exit(1); exit(1);
}
} }
else else
e_additional(); e_additional();