From 281a56fb746f474f364b055f88565280a6033e63 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sat, 26 Nov 2016 17:41:31 +0100 Subject: [PATCH] tweaks: reshuffle some things in a more linear manner Also improve or correct some comments. --- src/files.c | 47 ++++++++++++++++++++++++----------------------- src/rcfile.c | 30 ++++++++++++------------------ 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/files.c b/src/files.c index 34426b33..c105db15 100644 --- a/src/files.c +++ b/src/files.c @@ -947,13 +947,10 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw #endif } -/* Open the file (and decide if it exists). If newfie is TRUE, display - * "New File" if the file is missing. Otherwise, say "[filename] not - * found". - * +/* Open the file with the given name. If the file does not exist, display + * "New File" if newfie is TRUE, and say "File not found" otherwise. * Return -2 if we say "New File", -1 if the file isn't opened, and the - * fd opened otherwise. The file might still have an error while reading - * with a 0 return value. *f is set to the opened file. */ + * obtained fd otherwise. *f is set to the opened file. */ int open_file(const char *filename, bool newfie, bool quiet, FILE **f) { struct stat fileinfo, fileinfo2; @@ -972,31 +969,35 @@ int open_file(const char *filename, bool newfie, bool quiet, FILE **f) full_filename = mallocstrcpy(full_filename, filename); if (stat(full_filename, &fileinfo) == -1) { - /* All cases below return. */ - free(full_filename); - if (newfie) { if (!quiet) statusbar(_("New File")); + free(full_filename); return -2; } - statusline(ALERT, _("File \"%s\" not found"), filename); - return -1; - } else if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) || - S_ISBLK(fileinfo.st_mode)) { - free(full_filename); - /* Don't open directories, character files, or block files. */ - statusline(ALERT, S_ISDIR(fileinfo.st_mode) ? - _("\"%s\" is a directory") : - _("\"%s\" is a device file"), filename); - return -1; - } else if ((fd = open(full_filename, O_RDONLY)) == -1) { + statusline(ALERT, _("File \"%s\" not found"), filename); free(full_filename); - statusline(ALERT, _("Error reading %s: %s"), filename, strerror(errno)); return -1; - } else { - /* The file is A-OK. Open it. */ + } + + /* Don't open directories, character files, or block files. */ + if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) || + S_ISBLK(fileinfo.st_mode)) { + statusline(ALERT, S_ISDIR(fileinfo.st_mode) ? + _("\"%s\" is a directory") : + _("\"%s\" is a device file"), filename); + free(full_filename); + return -1; + } + + /* Try opening the file. */ + fd = open(full_filename, O_RDONLY); + + if (fd == -1) + statusline(ALERT, _("Error reading %s: %s"), filename, strerror(errno)); + else { + /* The file is A-OK. Associate a stream with it. */ *f = fdopen(fd, "rb"); if (*f == NULL) { diff --git a/src/rcfile.c b/src/rcfile.c index af8c75dc..170a7181 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -533,28 +533,23 @@ static void parse_one_include(char *file) struct stat rcinfo; FILE *rcstream; - /* Can't get the specified file's full path because it may screw up - * our cwd depending on the parent directories' permissions (see - * Savannah bug #25297). */ - /* Don't open directories, character files, or block files. */ - if (stat(file, &rcinfo) != -1) { - if (S_ISDIR(rcinfo.st_mode) || S_ISCHR(rcinfo.st_mode) || - S_ISBLK(rcinfo.st_mode)) { - rcfile_error(S_ISDIR(rcinfo.st_mode) ? - _("\"%s\" is a directory") : - _("\"%s\" is a device file"), file); - } + if (stat(file, &rcinfo) != -1 && (S_ISDIR(rcinfo.st_mode) || + S_ISCHR(rcinfo.st_mode) || S_ISBLK(rcinfo.st_mode))) { + rcfile_error(S_ISDIR(rcinfo.st_mode) ? + _("\"%s\" is a directory") : + _("\"%s\" is a device file"), file); } - /* Open the new syntax file. */ - if ((rcstream = fopen(file, "rb")) == NULL) { - rcfile_error(_("Error reading %s: %s"), file, - strerror(errno)); + /* Open the included syntax file. */ + rcstream = fopen(file, "rb"); + + if (rcstream == NULL) { + rcfile_error(_("Error reading %s: %s"), file, strerror(errno)); return; } - /* Use the name and line number position of the new syntax file + /* Use the name and line number position of the included syntax file * while parsing it, so we can know where any errors in it are. */ nanorc = file; lineno = 0; @@ -585,8 +580,7 @@ void parse_includes(char *ptr) for (i = 0; i < files.gl_pathc; ++i) parse_one_include(files.gl_pathv[i]); } else - rcfile_error(_("Error expanding %s: %s"), option, - strerror(errno)); + rcfile_error(_("Error expanding %s: %s"), option, strerror(errno)); globfree(&files); free(expanded);