From 85191b5ccb1a8da897577ddf2a7911f42b0257ea Mon Sep 17 00:00:00 2001 From: op Date: Thu, 30 Mar 2023 19:00:02 +0000 Subject: [PATCH] don't access(conffile) This removes a few access(2) calls in the configuration file handling. startupfile() now opens and return the file and to avoid surprises it also uses a caller-provided buffer to store the filename. This also removes the extra adjustpath() that load() did: it has been moved to evalfile() only. with help, fixes and ok tb@ --- usr.bin/mg/def.h | 6 +++--- usr.bin/mg/extend.c | 34 ++++++++++++++++------------------ usr.bin/mg/fileio.c | 40 +++++++++++++++++++++++----------------- usr.bin/mg/main.c | 13 +++++++++---- usr.bin/mg/ttykbd.c | 12 +++++++----- 5 files changed, 58 insertions(+), 47 deletions(-) diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index 4159f41d079..5475b513036 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.177 2022/10/20 18:59:24 op Exp $ */ +/* $OpenBSD: def.h,v 1.178 2023/03/30 19:00:02 op Exp $ */ /* This file is in the public domain. */ @@ -486,7 +486,7 @@ int ffputbuf(FILE *, struct buffer *, int); int ffgetline(FILE *, char *, int, int *); int fbackupfile(const char *); char *adjustname(const char *, int); -char *startupfile(char *, char *); +FILE *startupfile(char *, char *, char *, size_t); int copy(char *, char *); struct list *make_file_list(char *); int fisdir(const char *); @@ -594,7 +594,7 @@ int extend(int, int); int evalexpr(int, int); int evalbuffer(int, int); int evalfile(int, int); -int load(const char *); +int load(FILE *, const char *); int excline(char *, int, int); char *skipwhite(char *); diff --git a/usr.bin/mg/extend.c b/usr.bin/mg/extend.c index 9826f46324f..5b88dfd61ef 100644 --- a/usr.bin/mg/extend.c +++ b/usr.bin/mg/extend.c @@ -1,4 +1,4 @@ -/* $OpenBSD: extend.c,v 1.77 2023/03/08 04:43:11 guenther Exp $ */ +/* $OpenBSD: extend.c,v 1.78 2023/03/30 19:00:02 op Exp $ */ /* This file is in the public domain. */ /* @@ -620,37 +620,36 @@ evalbuffer(int f, int n) int evalfile(int f, int n) { + FILE *ffp; char fname[NFILEN], *bufp; + int ret; if ((bufp = eread("Load file: ", fname, NFILEN, EFNEW | EFCR)) == NULL) return (ABORT); - else if (bufp[0] == '\0') + if (bufp[0] == '\0') + return (FALSE); + if ((bufp = adjustname(fname, TRUE)) == NULL) + return (FALSE); + ret = ffropen(&ffp, bufp, NULL); + if (ret == FIODIR) + (void)ffclose(ffp, NULL); + if (ret != FIOSUC) return (FALSE); - return (load(fname)); + ret = load(ffp, bufp); + (void)ffclose(ffp, NULL); + return (ret); } /* * load - go load the file name we got passed. */ int -load(const char *fname) +load(FILE *ffp, const char *fname) { - int s = TRUE, line, ret; + int s = TRUE, line; int nbytes = 0; char excbuf[BUFSIZE], fncpy[NFILEN]; - FILE *ffp; - - if ((fname = adjustname(fname, TRUE)) == NULL) - /* just to be careful */ - return (FALSE); - - ret = ffropen(&ffp, fname, NULL); - if (ret != FIOSUC) { - if (ret == FIODIR) - (void)ffclose(ffp, NULL); - return (FALSE); - } /* keep a note of fname in case of errors in loaded file. */ (void)strlcpy(fncpy, fname, sizeof(fncpy)); @@ -666,7 +665,6 @@ load(const char *fname) break; } } - (void)ffclose(ffp, NULL); excbuf[nbytes] = '\0'; if (s != FIOEOF || (nbytes && excline(excbuf, nbytes, ++line) != TRUE)) return (FALSE); diff --git a/usr.bin/mg/fileio.c b/usr.bin/mg/fileio.c index 6493c6c7e04..7b5bbc574ea 100644 --- a/usr.bin/mg/fileio.c +++ b/usr.bin/mg/fileio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fileio.c,v 1.110 2023/03/30 07:26:15 op Exp $ */ +/* $OpenBSD: fileio.c,v 1.111 2023/03/30 19:00:02 op Exp $ */ /* This file is in the public domain. */ @@ -328,10 +328,10 @@ adjustname(const char *fn, int slashslash) * the terminal driver in particular), accepts a suffix to be appended * to the startup file name. */ -char * -startupfile(char *suffix, char *conffile) +FILE * +startupfile(char *suffix, char *conffile, char *path, size_t len) { - static char file[NFILEN]; + FILE *ffp; char *home; int ret; @@ -339,34 +339,40 @@ startupfile(char *suffix, char *conffile) goto nohome; if (conffile != NULL) { - (void)strlcpy(file, conffile, NFILEN); + (void)strlcpy(path, conffile, len); } else if (suffix == NULL) { - ret = snprintf(file, sizeof(file), _PATH_MG_STARTUP, home); - if (ret < 0 || ret >= sizeof(file)) + ret = snprintf(path, len, _PATH_MG_STARTUP, home); + if (ret < 0 || ret >= len) return (NULL); } else { - ret = snprintf(file, sizeof(file), _PATH_MG_TERM, home, suffix); - if (ret < 0 || ret >= sizeof(file)) + ret = snprintf(path, len, _PATH_MG_TERM, home, suffix); + if (ret < 0 || ret >= len) return (NULL); } - if (access(file, R_OK) == 0) - return (file); + ret = ffropen(&ffp, path, NULL); + if (ret == FIOSUC) + return (ffp); + if (ret == FIODIR) + (void)ffclose(ffp, NULL); nohome: #ifdef STARTUPFILE if (suffix == NULL) { - ret = snprintf(file, sizeof(file), "%s", STARTUPFILE); - if (ret < 0 || ret >= sizeof(file)) + ret = snprintf(path, len, "%s", STARTUPFILE); + if (ret < 0 || ret >= len) return (NULL); } else { - ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE, + ret = snprintf(path, len, "%s%s", STARTUPFILE, suffix); - if (ret < 0 || ret >= sizeof(file)) + if (ret < 0 || ret >= len) return (NULL); } - if (access(file, R_OK) == 0) - return (file); + ret = ffropen(&ffp, path, NULL); + if (ret == FIOSUC) + return (ffp); + if (ret == FIODIR) + (void)ffclose(ffp, NULL); #endif /* STARTUPFILE */ return (NULL); } diff --git a/usr.bin/mg/main.c b/usr.bin/mg/main.c index 8a0a9fa640a..2e580b1499e 100644 --- a/usr.bin/mg/main.c +++ b/usr.bin/mg/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.92 2023/03/30 08:07:07 op Exp $ */ +/* $OpenBSD: main.c,v 1.93 2023/03/30 19:00:02 op Exp $ */ /* This file is in the public domain. */ @@ -62,6 +62,8 @@ usage() int main(int argc, char **argv) { + FILE *ffp; + char file[NFILEN]; char *cp, *conffile = NULL, *init_fcn_name = NULL; char *batchfile = NULL; PF init_fcn = NULL; @@ -107,7 +109,8 @@ main(int argc, char **argv) pty_init(); conffile = batchfile; } - if (conffile != NULL && access(conffile, R_OK) != 0) { + if ((ffp = startupfile(NULL, conffile, file, sizeof(file))) == NULL && + conffile != NULL) { fprintf(stderr, "%s: Problem with file: %s\n", __progname, conffile); exit(1); @@ -159,8 +162,10 @@ main(int argc, char **argv) update(CMODE); /* user startup file. */ - if ((cp = startupfile(NULL, conffile)) != NULL) - (void)load(cp); + if (ffp != NULL) { + (void)load(ffp, file); + ffclose(ffp, NULL); + } if (batch) return (0); diff --git a/usr.bin/mg/ttykbd.c b/usr.bin/mg/ttykbd.c index c457c9a5036..c1e5e1aeda4 100644 --- a/usr.bin/mg/ttykbd.c +++ b/usr.bin/mg/ttykbd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ttykbd.c,v 1.21 2023/03/30 08:07:07 op Exp $ */ +/* $OpenBSD: ttykbd.c,v 1.22 2023/03/30 19:00:02 op Exp $ */ /* This file is in the public domain. */ @@ -31,7 +31,8 @@ char *keystrings[] = {NULL}; void ttykeymapinit(void) { - char *cp; + char *cp, file[NFILEN]; + FILE *ffp; /* Bind keypad function keys. */ if (key_left) @@ -57,10 +58,11 @@ ttykeymapinit(void) if (key_dc) dobindkey(fundamental_map, "delete-char", key_dc); - if ((cp = getenv("TERM"))) { - if (((cp = startupfile(cp, NULL)) != NULL) && - (load(cp) != TRUE)) + if ((cp = getenv("TERM")) != NULL && + (ffp = startupfile(cp, NULL, file, sizeof(file))) != NULL) { + if (load(ffp, file) != TRUE) ewprintf("Error reading key initialization file"); + (void)ffclose(ffp, NULL); } if (keypad_xmit) /* turn on keypad */ -- 2.20.1