src/nnn.c | 23 +++++++++++------------ diff --git a/src/nnn.c b/src/nnn.c index 0e41cd1f13bc7f2be64a14dfa6c2feaf78860f14..fa61f3763e2b810780d3c9cd61f40141c356d3e5 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -1127,6 +1127,11 @@ { return spawn("which", util, NULL, NULL, F_NORMAL | F_NOTRACE) == 0; } +static inline bool tilde_is_home(const char *s) +{ + return s[0] == '~' && (s[1] == '\0' || s[1] == '/'); +} + /* * Updates out with "dir/name or "/name" * Returns the number of bytes copied including the terminating NULL byte @@ -1137,16 +1142,10 @@ static size_t mkpath(const char *dir, const char *name, char *out) { size_t len = 0; - if (name[0] == '~') { //NOLINT + if (tilde_is_home(name)) { //NOLINT len = xstrsncpy(out, home, PATH_MAX); if (!name[1]) return len; - - if (name[1] != '/') { - out[0] = '\0'; - return 0; - } - --len; ++name; } else if (name[0] != '/') { // NOLINT @@ -1213,7 +1212,7 @@ if (!path) return NULL; - if (path[0] == '~') { + if (tilde_is_home(path)) { cwd = home; ++path; if (*path == '/') @@ -1333,7 +1332,7 @@ #endif static void convert_tilde(const char *path, char *buf) { - if (path[0] == '~') { + if (tilde_is_home(path)) { ssize_t len = xstrlen(home); ssize_t loclen = xstrlen(path); @@ -2769,7 +2768,7 @@ else convert_tilde(outfile, g_buf); int fd = open(outfile - ? (outfile[0] == '~' ? g_buf : outfile) + ? (tilde_is_home(outfile) ? g_buf : outfile) : cfgpath, O_CREAT | O_WRONLY | O_TRUNC, S_IWUSR | S_IRUSR); if (fd != -1) { @@ -3838,7 +3837,7 @@ return pluginstr + kvarr[r].off; val = bmstr + kvarr[r].off; convert_tilde(val, g_buf); - return abspath(((val[0] == '~') ? g_buf : val), NULL, buf); + return abspath((tilde_is_home(val) ? g_buf : val), NULL, buf); } } @@ -8297,7 +8296,7 @@ /* Set up configuration file paths */ if (xdgcfg && xdgcfg[0]) { DPRINTF_S(xdgcfg); - if (xdgcfg[0] == '~') { + if (tilde_is_home(xdgcfg)) { r = xstrsncpy(g_buf, home, PATH_MAX); xstrsncpy(g_buf + r - 1, xdgcfg + 1, PATH_MAX); xdgcfg = g_buf;