From: Bas Zoetekouw Subject: bug & patch Resent-Date: Fri, 9 Aug 2002 21:30:00 +0200 Resent-To: aumix-l@lilax.org Hi guys! A Debian user reported the following bug in aumix (more info at http://bugs.debian.org/155136): > jan@kontryhel:~$ mkdir foo > jan@kontryhel:~$ aumix -Sf foo > aumix: unable to open settings file > > ############################################### > # OK so far, here it comes: > jan@kontryhel:~$ aumix -Lf foo > # Et voil`a: no error message > jan@kontryhel:~$ echo $? > 0 > # And exit status is 0, too. > ############################################### The problem seems to be that fopen() doesn't return an error when opening a directory. I've written a little patch that does an extra check when trying to open a settings file for reading. It's tested on Linux, but on BSD, other header files might have to be included in common.h. diff -Naur aumix-2.7.orig/src/common.c aumix-2.7/src/common.c --- aumix-2.7.orig/src/common.c 2002-08-07 15:55:27.000000000 +0200 +++ aumix-2.7/src/common.c 2002-08-07 15:56:06.000000000 +0200 @@ -592,6 +592,25 @@ return y; } +FILE *check_and_fopen(const char *filename, const char *mode) +{ +/* Check whether *filename is a directory, and if not, call fopen on it. + + check_and_fopen() is meant to be a drop-in replacement for fopen() + */ + struct stat s; + int result; + + /* fopen will fail if mode=='w' and filename is a directory, so we + * only need to check the mode=='r' case */ + if (*mode == 'r') { + result = stat(filename, &s); + if (result != 0) return NULL; /* stat() failed */ + if (S_ISDIR(s.st_mode)) return NULL; /* filename is a directory */ + } + return fopen(filename, mode); +} + FILE *OpenDefaultFile(char *mode) { /* Open the settings file for reading or writing. @@ -615,19 +634,19 @@ home = getenv("HOME"); if (home && (strlen(home) + strlen(AUMIXRC)) < PATH_MAX) { sprintf(filename, "%s/.%s", home, AUMIXRC); - setfile = fopen(filename, mode); + setfile = check_and_fopen(filename, mode); } if (setfile == NULL) { if ((strlen(AUMIXRC_PATH) + strlen(AUMIXRC)) < PATH_MAX) { sprintf(filename, "%s/%s", AUMIXRC_PATH, AUMIXRC); - setfile = fopen(filename, mode); + setfile = check_and_fopen(filename, mode); } } if (setfile == NULL) { return NULL; } } else - setfile = fopen(save_filename, mode); + setfile = check_and_fopen(save_filename, mode); return setfile; } diff -Naur aumix-2.7.orig/src/common.h aumix-2.7/src/common.h --- aumix-2.7.orig/src/common.h 2002-08-07 15:55:05.000000000 +0200 +++ aumix-2.7/src/common.h 2002-08-07 15:56:06.000000000 +0200 @@ -21,6 +21,7 @@ #endif /* __linux__ */ #include #include +#include #include #include #include