simplify real_dir_from_tilde()

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4079 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
David Lawrence Ramsey 2007-04-18 17:13:36 +00:00
parent 6a8b350a0c
commit dc588aea5d
2 changed files with 15 additions and 16 deletions

View File

@ -1,5 +1,6 @@
2007-04-18 David Lawrence Ramsey <pooka109@gmail.com> 2007-04-18 David Lawrence Ramsey <pooka109@gmail.com>
* files.c (real_dir_from_tilde): Simplify.
* winio.c (parse_kbinput): Interpret Cancel and Shift-Cancel. * winio.c (parse_kbinput): Interpret Cancel and Shift-Cancel.
* winio.c (get_escape_seq_kbinput): Add missing comments. * winio.c (get_escape_seq_kbinput): Add missing comments.

View File

@ -1973,13 +1973,13 @@ void do_writeout_void(void)
* convert ~user/ and ~/ notation. */ * convert ~user/ and ~/ notation. */
char *real_dir_from_tilde(const char *buf) char *real_dir_from_tilde(const char *buf)
{ {
char *dirtmp = NULL; char *retval;
assert(buf != NULL); assert(buf != NULL);
if (buf[0] == '~') { if (buf[0] == '~') {
size_t i; size_t i;
const char *tilde_dir = NULL; char *tilde_dir;
/* Figure out how much of the str we need to compare. */ /* Figure out how much of the str we need to compare. */
for (i = 1; buf[i] != '/' && buf[i] != '\0'; i++) for (i = 1; buf[i] != '/' && buf[i] != '\0'; i++)
@ -1988,32 +1988,30 @@ char *real_dir_from_tilde(const char *buf)
/* Get the home directory. */ /* Get the home directory. */
if (i == 1) { if (i == 1) {
get_homedir(); get_homedir();
tilde_dir = homedir; tilde_dir = mallocstrcpy(NULL, homedir);
} else { } else {
const struct passwd *userdata; const struct passwd *userdata;
tilde_dir = mallocstrncpy(NULL, buf, i + 1);
tilde_dir[i] = '\0';
do { do {
userdata = getpwent(); userdata = getpwent();
} while (userdata != NULL && } while (userdata != NULL &&
(strncmp(userdata->pw_name, buf + 1, i - 1) != 0 || strcmp(userdata->pw_name, tilde_dir + 1) != 0);
strlen(userdata->pw_name) != strnlen(buf + 1, i - 1)));
endpwent(); endpwent();
if (userdata != NULL) if (userdata != NULL)
tilde_dir = userdata->pw_dir; tilde_dir = mallocstrcpy(tilde_dir, userdata->pw_dir);
} }
if (tilde_dir != NULL) { retval = charalloc(strlen(tilde_dir) + strlen(buf + i) + 1);
dirtmp = charalloc(strlen(tilde_dir) + strlen(buf + i) + 1); sprintf(retval, "%s%s", tilde_dir, buf + i);
sprintf(dirtmp, "%s%s", tilde_dir, buf + i);
}
}
/* Set a default value for dirtmp, in case the user's home directory free(tilde_dir);
* isn't found. */ } else
if (dirtmp == NULL) retval = mallocstrcpy(NULL, buf);
dirtmp = mallocstrcpy(NULL, buf);
return dirtmp; return retval;
} }
#if !defined(DISABLE_TABCOMP) || !defined(DISABLE_BROWSER) #if !defined(DISABLE_TABCOMP) || !defined(DISABLE_BROWSER)