tweaks: factor out a small function

master
Benno Schulenberg 2016-11-27 17:27:04 +01:00
parent 925a001658
commit 981a1d39bf
1 changed files with 22 additions and 24 deletions

View File

@ -530,21 +530,30 @@ void parse_binding(char *ptr, bool dobind)
free(keycopy); free(keycopy);
} }
/* Verify that the given file is not a folder nor a device. */
bool is_good_file(char *file)
{
struct stat rcinfo;
/* If the thing exists, it may not be a directory nor a device. */
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);
return FALSE;
} else
return TRUE;
}
#ifndef DISABLE_COLOR #ifndef DISABLE_COLOR
/* Read and parse one included syntax file. */ /* Read and parse one included syntax file. */
static void parse_one_include(char *file) static void parse_one_include(char *file)
{ {
struct stat rcinfo;
FILE *rcstream; FILE *rcstream;
/* Don't open directories, character files, or block files. */ /* Don't open directories, character files, or block files. */
if (stat(file, &rcinfo) != -1 && (S_ISDIR(rcinfo.st_mode) || if (!is_good_file(file))
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);
return; return;
}
/* Open the included syntax file. */ /* Open the included syntax file. */
rcstream = fopen(file, "rb"); rcstream = fopen(file, "rb");
@ -1226,19 +1235,13 @@ void parse_rcfile(FILE *rcstream, bool syntax_only)
/* First read the system-wide rcfile, then the user's rcfile. */ /* First read the system-wide rcfile, then the user's rcfile. */
void do_rcfile(void) void do_rcfile(void)
{ {
struct stat rcinfo;
FILE *rcstream; FILE *rcstream;
nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc"); nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");
/* Don't open directories, character files, or block files. */ /* Warn about directories, character files, or block files. */
if (stat(nanorc, &rcinfo) != -1) { if (!is_good_file(nanorc))
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"), nanorc);
}
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Parsing file \"%s\"\n", nanorc); fprintf(stderr, "Parsing file \"%s\"\n", nanorc);
@ -1264,14 +1267,9 @@ void do_rcfile(void)
nanorc = charealloc(nanorc, strlen(homedir) + strlen(RCFILE_NAME) + 2); nanorc = charealloc(nanorc, strlen(homedir) + strlen(RCFILE_NAME) + 2);
sprintf(nanorc, "%s/%s", homedir, RCFILE_NAME); sprintf(nanorc, "%s/%s", homedir, RCFILE_NAME);
/* Don't open directories, character files, or block files. */ /* Warn about directories, character files, or block files. */
if (stat(nanorc, &rcinfo) != -1) { if (!is_good_file(nanorc))
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"), nanorc);
}
/* Try to open the current user's nanorc. */ /* Try to open the current user's nanorc. */
rcstream = fopen(nanorc, "rb"); rcstream = fopen(nanorc, "rb");