From f6e182ca72a56def7fa7cb05683ab6adf9fbb180 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Mon, 27 May 2019 09:38:08 +0200 Subject: [PATCH] tweaks: check in a single place for files that should not be opened --- src/files.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/files.c b/src/files.c index c25a76f5..20b4d2da 100644 --- a/src/files.c +++ b/src/files.c @@ -411,6 +411,7 @@ bool open_buffer(const char *filename, bool new_buffer) { char *realname; /* The filename after tilde expansion. */ + struct stat fileinfo; FILE *f; int rc; /* rc == -2 means that we have a new file. -1 means that the @@ -429,15 +430,18 @@ bool open_buffer(const char *filename, bool new_buffer) realname = real_dir_from_tilde(filename); - /* When the given filename refers to a directory, don't try to open it. */ - if (*filename != '\0') { - struct stat fileinfo; - - if (stat(realname, &fileinfo) == 0 && S_ISDIR(fileinfo.st_mode)) { + /* Don't try to open directories, character files, or block files. */ + if (*filename != '\0' && stat(realname, &fileinfo) == 0) { + if (S_ISDIR(fileinfo.st_mode)) { statusline(ALERT, _("\"%s\" is a directory"), realname); free(realname); return FALSE; } + if (S_ISCHR(fileinfo.st_mode) || S_ISBLK(fileinfo.st_mode)) { + statusline(ALERT, _("\"%s\" is a device file"), realname); + free(realname); + return FALSE; + } } /* If we're going to load into a new buffer, first create the new @@ -929,16 +933,6 @@ int open_file(const char *filename, bool newfie, FILE **f) return -1; } - /* 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; - } - if (S_ISFIFO(fileinfo.st_mode)) statusbar(_("Reading from FIFO..."));