tDon't attempt to open non-regular files - ledit - Text editor (WIP) | |
git clone git://lumidify.org/ledit.git (fast, but not encrypted) | |
git clone https://lumidify.org/git/ledit.git (encrypted, but very slow) | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit fcb4dcfe38932c8c4550b3f6b007d467ee5b7042 | |
parent bab18b62da4d443665698fa3e8bc0e28fc9bbfe0 | |
Author: lumidify <[email protected]> | |
Date: Sat, 4 Nov 2023 21:18:29 +0100 | |
Don't attempt to open non-regular files | |
Diffstat: | |
M ledit.c | 50 +++++++++++++++++++++++++----… | |
1 file changed, 41 insertions(+), 9 deletions(-) | |
--- | |
diff --git a/ledit.c b/ledit.c | |
t@@ -600,42 +600,59 @@ setup(int argc, char *argv[]) { | |
clock_gettime(CLOCK_MONOTONIC, &last); | |
#endif | |
+ /* FIXME: Technically, there's a race condition between checking stat … | |
+ opening the files. This is mainly important when checking if the fi… | |
+ file because that is not an error for functions like fopen, so bad … | |
+ if a non-regular file (e.g. a directory) is given to one of the fil… | |
+ functions. However, I don't know of any portable way to have one of… | |
+ check that, so this is the best I can do. */ | |
char *stat_errstr = NULL, *load_errstr = NULL, *load_default_errstr = … | |
char *cfgfile = NULL; | |
if (!opt_filename) { | |
uid_t uid = getuid(); | |
struct passwd *pw = getpwuid(uid); | |
if (!pw) { | |
- fprintf(stderr, "Unable to determine home directory\n"… | |
+ stat_errstr = ledit_strdup("Unable to determine home d… | |
} else { | |
cfgfile = ledit_strcat(pw->pw_dir, "/.leditrc"); | |
struct stat cfgst; | |
- /* FIXME: maybe only when ENOENT? */ | |
if (stat(cfgfile, &cfgst)) { | |
free(cfgfile); | |
cfgfile = NULL; | |
+ if (errno != ENOENT) { | |
+ stat_errstr = print_fmt("Unable to loa… | |
+ } | |
+ } else if (!S_ISREG(cfgst.st_mode)) { | |
+ stat_errstr = ledit_strdup("Unable to load con… | |
+ free(cfgfile); | |
+ cfgfile = NULL; | |
} | |
} | |
} else { | |
struct stat cfgst; | |
if (stat(opt_filename, &cfgst)) { | |
- stat_errstr = print_fmt("Unable to load configuration … | |
- fprintf(stderr, "%s\n", stat_errstr); | |
+ stat_errstr = print_fmt("Unable to load configuration … | |
+ } else if (!S_ISREG(cfgst.st_mode)) { | |
+ stat_errstr = print_fmt("Unable to load configuration … | |
} else { | |
cfgfile = ledit_strdup(opt_filename); | |
} | |
} | |
+ if (stat_errstr) | |
+ fprintf(stderr, "%s\n", stat_errstr); | |
if (config_loadfile(&common, cfgfile, &load_errstr)) { | |
fprintf(stderr, "%s\n", load_errstr); | |
+ fprintf(stderr, "Unable to load configuration '%s'\n", cfgfile… | |
int failure = 1; | |
if (cfgfile) { | |
/* retry with default config */ | |
failure = config_loadfile(&common, NULL, &load_default… | |
} | |
if (failure) { | |
- fprintf(stderr, "Unable to load configuration: %s\n", … | |
- if (load_default_errstr) | |
- fprintf(stderr, "Also unable to load default c… | |
+ if (load_default_errstr) { | |
+ fprintf(stderr, "%s\n", load_default_errstr); | |
+ fprintf(stderr, "Also unable to load default c… | |
+ } | |
free(stat_errstr); | |
free(load_errstr); | |
free(load_default_errstr); | |
t@@ -683,6 +700,8 @@ setup(int argc, char *argv[]) { | |
int readonly = 0; | |
int error = 0; | |
/* FIXME: maybe copy vi and open file in /tmp by default? */ | |
+ /* FIXME: when other methods of opening files (:r, etc.) are s… | |
+ all this checking needs to be moved to a place where it can… | |
if (stat(argv[0], &sb)) { | |
if (errno == ENOENT) { | |
/* note that there may still be a failure | |
t@@ -690,18 +709,32 @@ setup(int argc, char *argv[]) { | |
the path does not exist */ | |
newfile = 1; | |
} else { | |
+ fprintf( | |
+ stderr, "Error opening file '%s': %s\n", | |
+ argv[0], strerror(errno) | |
+ ); | |
window_show_message_fmt( | |
view->window, "Error opening file '%s': %s… | |
argv[0], strerror(errno) | |
); | |
error = 1; | |
} | |
+ } else if (!S_ISREG(sb.st_mode)) { | |
+ fprintf(stderr, "Error opening file '%s': Is not a reg… | |
+ window_show_message_fmt( | |
+ view->window, "Error opening file '%s': Is not a r… | |
+ ); | |
+ error = 1; | |
} | |
if (access(argv[0], W_OK)) { | |
readonly = 1; | |
} | |
- if (!newfile) { | |
+ if (!newfile && !error) { | |
if (buffer_load_file(buffer, argv[0], 0, &load_err)) { | |
+ fprintf( | |
+ stderr, "Error opening file '%s': %s\n", | |
+ argv[0], load_err | |
+ ); | |
window_show_message_fmt( | |
view->window, "Error opening file '%s': %s… | |
argv[0], load_err | |
t@@ -712,7 +745,6 @@ setup(int argc, char *argv[]) { | |
} | |
if (!error) { | |
buffer->filename = ledit_strdup(argv[0]); | |
- /* FIXME: show this *in addition* to error */ | |
if (!show_error) { | |
if (newfile) { | |
window_show_message_fmt(view->window, … |