tweaks: reshuffle some things in a more linear manner

Also improve or correct some comments.
master
Benno Schulenberg 2016-11-26 17:41:31 +01:00
parent 592d0d6c9a
commit 281a56fb74
2 changed files with 36 additions and 41 deletions

View File

@ -947,13 +947,10 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw
#endif #endif
} }
/* Open the file (and decide if it exists). If newfie is TRUE, display /* Open the file with the given name. If the file does not exist, display
* "New File" if the file is missing. Otherwise, say "[filename] not * "New File" if newfie is TRUE, and say "File not found" otherwise.
* found".
*
* Return -2 if we say "New File", -1 if the file isn't opened, and the * 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 * obtained fd otherwise. *f is set to the opened file. */
* with a 0 return value. *f is set to the opened file. */
int open_file(const char *filename, bool newfie, bool quiet, FILE **f) int open_file(const char *filename, bool newfie, bool quiet, FILE **f)
{ {
struct stat fileinfo, fileinfo2; 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); full_filename = mallocstrcpy(full_filename, filename);
if (stat(full_filename, &fileinfo) == -1) { if (stat(full_filename, &fileinfo) == -1) {
/* All cases below return. */
free(full_filename);
if (newfie) { if (newfie) {
if (!quiet) if (!quiet)
statusbar(_("New File")); statusbar(_("New File"));
free(full_filename);
return -2; return -2;
} }
statusline(ALERT, _("File \"%s\" not found"), filename); 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); free(full_filename);
return -1;
}
/* Don't open directories, character files, or block files. */ /* 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) ? statusline(ALERT, S_ISDIR(fileinfo.st_mode) ?
_("\"%s\" is a directory") : _("\"%s\" is a directory") :
_("\"%s\" is a device file"), filename); _("\"%s\" is a device file"), filename);
return -1;
} else if ((fd = open(full_filename, O_RDONLY)) == -1) {
free(full_filename); free(full_filename);
statusline(ALERT, _("Error reading %s: %s"), filename, strerror(errno));
return -1; return -1;
} else { }
/* The file is A-OK. Open it. */
/* 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"); *f = fdopen(fd, "rb");
if (*f == NULL) { if (*f == NULL) {

View File

@ -533,28 +533,23 @@ static void parse_one_include(char *file)
struct stat rcinfo; struct stat rcinfo;
FILE *rcstream; 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. */ /* Don't open directories, character files, or block files. */
if (stat(file, &rcinfo) != -1) { if (stat(file, &rcinfo) != -1 && (S_ISDIR(rcinfo.st_mode) ||
if (S_ISDIR(rcinfo.st_mode) || S_ISCHR(rcinfo.st_mode) || S_ISCHR(rcinfo.st_mode) || S_ISBLK(rcinfo.st_mode))) {
S_ISBLK(rcinfo.st_mode)) {
rcfile_error(S_ISDIR(rcinfo.st_mode) ? rcfile_error(S_ISDIR(rcinfo.st_mode) ?
_("\"%s\" is a directory") : _("\"%s\" is a directory") :
_("\"%s\" is a device file"), file); _("\"%s\" is a device file"), file);
} }
}
/* Open the new syntax file. */ /* Open the included syntax file. */
if ((rcstream = fopen(file, "rb")) == NULL) { rcstream = fopen(file, "rb");
rcfile_error(_("Error reading %s: %s"), file,
strerror(errno)); if (rcstream == NULL) {
rcfile_error(_("Error reading %s: %s"), file, strerror(errno));
return; 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. */ * while parsing it, so we can know where any errors in it are. */
nanorc = file; nanorc = file;
lineno = 0; lineno = 0;
@ -585,8 +580,7 @@ void parse_includes(char *ptr)
for (i = 0; i < files.gl_pathc; ++i) for (i = 0; i < files.gl_pathc; ++i)
parse_one_include(files.gl_pathv[i]); parse_one_include(files.gl_pathv[i]);
} else } else
rcfile_error(_("Error expanding %s: %s"), option, rcfile_error(_("Error expanding %s: %s"), option, strerror(errno));
strerror(errno));
globfree(&files); globfree(&files);
free(expanded); free(expanded);