Move config file path expansion much earlier, keep the list of paths

around rather than freeing later, and add a config_files format variable
containing it. Suggested by kn@ a while back.
This commit is contained in:
nicm
2021-02-22 08:18:13 +00:00
parent e858270006
commit 6876381276
5 changed files with 63 additions and 34 deletions

37
tmux.c
View File

@ -142,11 +142,12 @@ expand_path(const char *path, const char *home)
return (xstrdup(path));
}
void
expand_paths(const char *s, char ***paths, u_int *n)
static void
expand_paths(const char *s, char ***paths, u_int *n, int ignore_errors)
{
const char *home = find_home();
char *copy, *next, *tmp, resolved[PATH_MAX], *expanded;
char *path;
u_int i;
*paths = NULL;
@ -162,20 +163,26 @@ expand_paths(const char *s, char ***paths, u_int *n)
if (realpath(expanded, resolved) == NULL) {
log_debug("%s: realpath(\"%s\") failed: %s", __func__,
expanded, strerror(errno));
if (ignore_errors) {
free(expanded);
continue;
}
path = expanded;
} else {
path = xstrdup(resolved);
free(expanded);
continue;
}
free(expanded);
for (i = 0; i < *n; i++) {
if (strcmp(resolved, (*paths)[i]) == 0)
if (strcmp(path, (*paths)[i]) == 0)
break;
}
if (i != *n) {
log_debug("%s: duplicate path: %s", __func__, resolved);
log_debug("%s: duplicate path: %s", __func__, path);
free(path);
continue;
}
*paths = xreallocarray(*paths, (*n) + 1, sizeof *paths);
(*paths)[(*n)++] = xstrdup(resolved);
(*paths)[(*n)++] = path;
}
free(copy);
}
@ -193,7 +200,7 @@ make_label(const char *label, char **cause)
label = "default";
uid = getuid();
expand_paths(TMUX_SOCK, &paths, &n);
expand_paths(TMUX_SOCK, &paths, &n, 1);
if (n == 0) {
xasprintf(cause, "no suitable socket path");
return (NULL);
@ -330,10 +337,11 @@ main(int argc, char **argv)
{
char *path = NULL, *label = NULL;
char *cause, **var;
const char *s, *shell, *cwd;
const char *s, *cwd;
int opt, keys, feat = 0;
uint64_t flags = 0;
const struct options_table_entry *oe;
u_int i;
if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL &&
setlocale(LC_CTYPE, "C.UTF-8") == NULL) {
@ -349,6 +357,7 @@ main(int argc, char **argv)
if (**argv == '-')
flags = CLIENT_LOGIN;
expand_paths(TMUX_CONF, &cfg_files, &cfg_nfiles, 1);
while ((opt = getopt(argc, argv, "2c:CDdf:lL:NqS:T:uUvV")) != -1) {
switch (opt) {
@ -368,7 +377,11 @@ main(int argc, char **argv)
flags |= CLIENT_CONTROL;
break;
case 'f':
set_cfg_file(optarg);
for (i = 0; i < cfg_nfiles; i++)
free(cfg_files[i]);
free(cfg_files);
expand_paths(optarg, &cfg_files, &cfg_nfiles, 0);
cfg_quiet = 0;
break;
case 'V':
printf("%s %s\n", getprogname(), getversion());
@ -460,8 +473,8 @@ main(int argc, char **argv)
* The default shell comes from SHELL or from the user's passwd entry
* if available.
*/
shell = getshell();
options_set_string(global_s_options, "default-shell", 0, "%s", shell);
options_set_string(global_s_options, "default-shell", 0, "%s",
getshell());
/* Override keys to vi if VISUAL or EDITOR are set. */
if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {