commit b901a20a06e73fe2fe337f98e5cbfc495dca7332
parent d6e05d83764ea447dcb92f19c2ade38a4cf26a2e
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Tue, 17 Sep 2019 14:08:38 +0200
tweaks: use an early return when there is no tilde
Also improve a comment, and use a 'while' instead of a 'for'.
Diffstat:
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/src/files.c b/src/files.c
@@ -2303,21 +2303,22 @@ void do_savefile(void)
close_and_go();
}
-/* Return a malloc()ed string containing the actual directory, used to
- * convert ~user/ and ~/ notation. */
+/* Convert the tilde notation when the given path begins with ~/ or ~user/.
+ * Return an allocated string containing the expanded path. */
char *real_dir_from_tilde(const char *buf)
{
char *retval;
- if (*buf == '~') {
+ if (*buf != '~')
+ return mallocstrcpy(NULL, buf);
+
size_t i = 1;
char *tilde_dir;
/* Figure out how much of the string we need to compare. */
- for (; buf[i] != '/' && buf[i] != '\0'; i++)
- ;
+ while (buf[i] != '/' && buf[i] != '\0')
+ i++;
- /* Get the home directory. */
if (i == 1) {
get_homedir();
tilde_dir = mallocstrcpy(NULL, homedir);
@@ -2344,8 +2345,6 @@ char *real_dir_from_tilde(const char *buf)
sprintf(retval, "%s%s", tilde_dir, buf + i);
free(tilde_dir);
- } else
- retval = mallocstrcpy(NULL, buf);
return retval;
}