cansniffer: terminate when settings file is not found

Clean up readsettings() and terminate when the settings file
'sniffset.*' is not found when given on commandline.

The readsettings() status output could not be read anyway due to
instant redraw from CAN frames.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
pull/200/head
Oliver Hartkopp 2020-04-29 07:18:47 +02:00
parent 6f44582c19
commit 6d112a7209
1 changed files with 13 additions and 16 deletions

View File

@ -127,7 +127,7 @@ int handle_keyb(int fd);
int handle_frame(int fd, long currcms); int handle_frame(int fd, long currcms);
int handle_timeo(int fd, long currcms); int handle_timeo(int fd, long currcms);
void writesettings(char* name); void writesettings(char* name);
void readsettings(char* name, int sockfd); int readsettings(char* name, int sockfd);
void print_usage(char *prg) void print_usage(char *prg)
{ {
@ -201,7 +201,10 @@ int main(int argc, char **argv)
while ((opt = getopt(argc, argv, "r:t:h:l:qbBc?")) != -1) { while ((opt = getopt(argc, argv, "r:t:h:l:qbBc?")) != -1) {
switch (opt) { switch (opt) {
case 'r': case 'r':
readsettings(optarg, 0); /* no BCM-setting here */ if (readsettings(optarg, 0) < 0) {
fprintf(stderr, "Unable to read setting file '%s%s'!\n", SETFNAME, optarg);
exit(1);
}
break; break;
case 't': case 't':
@ -405,7 +408,7 @@ int handle_keyb(int fd){
break; break;
case 'r' : case 'r' :
readsettings(&cmd[1], fd); readsettings(&cmd[1], fd); /* don't care about success */
break; break;
case 'q' : case 'q' :
@ -674,20 +677,17 @@ void writesettings(char* name){
printf("unable to write setting file '%s'!\n", fname); printf("unable to write setting file '%s'!\n", fname);
}; };
void readsettings(char* name, int sockfd){ int readsettings(char* name, int sockfd){
int fd; int fd;
char fname[30] = SETFNAME; char fname[30] = SETFNAME;
char buf[25] = {0}; char buf[25] = {0};
int i,j; int j, i = 0;
strncat(fname, name, 29 - strlen(fname)); strncat(fname, name, 29 - strlen(fname));
fd = open(fname, O_RDONLY); fd = open(fname, O_RDONLY);
if (fd > 0) { if (fd > 0) {
if (!sockfd)
printf("reading setting file '%s' ... ", fname);
for (i=0; i < 2048 ;i++) { for (i=0; i < 2048 ;i++) {
if (read(fd, &buf, 24) == 24) { if (read(fd, &buf, 24) == 24) {
if (buf[5] & 1) { if (buf[5] & 1) {
@ -710,17 +710,14 @@ void readsettings(char* name, int sockfd){
} }
} }
else { else {
if (!sockfd) /* error: were not able to read the entire fix settings block */
printf("was only able to read until index %d from setting file '%s'!\n", i = -1;
i, fname);
} }
} }
if (!sockfd)
printf("done\n");
close(fd); close(fd);
} }
else else
printf("unable to read setting file '%s'!\n", fname); return -1;
return i;
}; };