Rocco's groovy fixes to real_dir_from_tilde
git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@502 35c25a1d-7b9e-4130-9fde-d3aeb78583b8master
parent
0a06e07831
commit
e5e4d49316
|
@ -13,7 +13,7 @@ General
|
||||||
username_tab_completion()
|
username_tab_completion()
|
||||||
- Optimization and removal of useless vars (Rocco).
|
- Optimization and removal of useless vars (Rocco).
|
||||||
real_dir_from_tilde()
|
real_dir_from_tilde()
|
||||||
- Rewritten using getpwent (suggested by Adam).
|
- Rewritten using getpwent (suggested by Adam, much optimized by Rocco).
|
||||||
- global.c:
|
- global.c:
|
||||||
- Don't define toggles global or toggle_init_one if using --tiny.
|
- Don't define toggles global or toggle_init_one if using --tiny.
|
||||||
- nano.c:
|
- nano.c:
|
||||||
|
|
50
files.c
50
files.c
|
@ -582,50 +582,52 @@ int do_writeout_void(void)
|
||||||
*/
|
*/
|
||||||
char *real_dir_from_tilde(char *buf)
|
char *real_dir_from_tilde(char *buf)
|
||||||
{
|
{
|
||||||
char *dirtmp = NULL;
|
char *dirtmp = NULL, *find_user = NULL;
|
||||||
int searchctr = 1;
|
int i = 1;
|
||||||
struct passwd *userdata;
|
struct passwd *userdata;
|
||||||
|
|
||||||
|
/* set a default value for dirtmp, in the case user home dir not found */
|
||||||
|
dirtmp = mallocstrcpy(dirtmp, buf);
|
||||||
|
|
||||||
if (buf[0] == '~') {
|
if (buf[0] == '~') {
|
||||||
if (buf[1] == '~')
|
if (buf[1] == 0 || buf[1] == '/') {
|
||||||
goto abort; /* Handle ~~ without segfaulting =) */
|
|
||||||
else if (buf[1] == 0 || buf[1] == '/') {
|
|
||||||
if (getenv("HOME") != NULL) {
|
if (getenv("HOME") != NULL) {
|
||||||
|
|
||||||
|
free(dirtmp);
|
||||||
dirtmp = nmalloc(strlen(buf) + 2 + strlen(getenv("HOME")));
|
dirtmp = nmalloc(strlen(buf) + 2 + strlen(getenv("HOME")));
|
||||||
|
|
||||||
if (strlen(buf) > 2)
|
sprintf(dirtmp, "%s%s", getenv("HOME"), &buf[1]);
|
||||||
sprintf(dirtmp, "%s/%s", getenv("HOME"), &buf[2]);
|
|
||||||
else
|
|
||||||
sprintf(dirtmp, "%s/", getenv("HOME"));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} else if (buf[1] != 0) {
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
/* Figure how how much of of the str we need to compare */
|
/* Figure how how much of of the str we need to compare */
|
||||||
for (searchctr = 1; buf[searchctr] != '/' &&
|
for (i = 1; buf[i] != '/' && buf[i] != 0; i++)
|
||||||
buf[searchctr] != 0; searchctr++);
|
;
|
||||||
|
|
||||||
|
find_user = mallocstrcpy(find_user, &buf[1]);
|
||||||
|
find_user[i - 1] = 0;
|
||||||
|
|
||||||
for (userdata = getpwent(); userdata != NULL &&
|
for (userdata = getpwent(); userdata != NULL &&
|
||||||
strncmp(userdata->pw_name, &buf[1], searchctr - 1);
|
strcmp(userdata->pw_name, find_user);
|
||||||
userdata = getpwent());
|
userdata = getpwent());
|
||||||
|
|
||||||
if (userdata == NULL) /* No such user or getpwent() failed */
|
free(find_user);
|
||||||
goto abort;
|
|
||||||
|
|
||||||
/* Else copy the new string into the new buf */
|
if (userdata != NULL) { /* User found */
|
||||||
dirtmp = nmalloc(strlen(buf) + 2 + strlen(userdata->pw_dir));
|
|
||||||
|
free(dirtmp);
|
||||||
|
dirtmp = nmalloc(strlen(buf) + 2 + strlen(userdata->pw_dir));
|
||||||
|
sprintf(dirtmp, "%s%s", userdata->pw_dir, &buf[i]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
sprintf(dirtmp, "%s%s", userdata->pw_dir, &buf[searchctr]);
|
|
||||||
endpwent();
|
endpwent();
|
||||||
}
|
}
|
||||||
} else
|
}
|
||||||
dirtmp = mallocstrcpy(dirtmp, buf);
|
|
||||||
|
|
||||||
return dirtmp;
|
return dirtmp;
|
||||||
|
|
||||||
abort:
|
|
||||||
dirtmp = mallocstrcpy(dirtmp, buf);
|
|
||||||
return dirtmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Tack a slash onto the string we're completing if it's a directory */
|
/* Tack a slash onto the string we're completing if it's a directory */
|
||||||
|
|
Loading…
Reference in New Issue