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@
-/* $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. */
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 *);
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 *);
-/* $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. */
/*
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));
break;
}
}
- (void)ffclose(ffp, NULL);
excbuf[nbytes] = '\0';
if (s != FIOEOF || (nbytes && excline(excbuf, nbytes, ++line) != TRUE))
return (FALSE);
-/* $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. */
* 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;
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);
}
-/* $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. */
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;
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);
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);
-/* $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. */
void
ttykeymapinit(void)
{
- char *cp;
+ char *cp, file[NFILEN];
+ FILE *ffp;
/* Bind keypad function keys. */
if (key_left)
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 */