1
1

tweaks: use an early return when there is no tilde

Also improve a comment, and use a 'while' instead of a 'for'.
Этот коммит содержится в:
Benno Schulenberg 2019-09-17 14:08:38 +02:00
родитель d6e05d8376
Коммит b901a20a06

Просмотреть файл

@ -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;
}