Fixed expand_homedir to handle paths like "~user" correctly.

This commit is contained in:
Arnavion 2014-07-19 17:09:50 -07:00 committed by TingPing
parent c2ecb4c68c
commit 7c2c8b1403

View File

@ -252,30 +252,30 @@ expand_homedir (char *file)
char *ret, *user; char *ret, *user;
struct passwd *pw; struct passwd *pw;
if (*file == '~') if (file[0] == '~')
{ {
if (file[1] != '\0' && file[1] != '/') if (file[1] == '\0' || file[1] == '/')
{ return g_strconcat (g_get_home_dir (), &file[1], NULL);
user = strdup(file);
if (strchr(user,'/') != NULL) char *slash_pos;
*(strchr(user,'/')) = '\0';
if ((pw = getpwnam(user + 1)) == NULL) user = g_strdup(file);
{
free(user); slash_pos = strchr(user, '/');
return strdup(file); if (slash_pos != NULL)
} *slash_pos = '\0';
free(user);
user = strchr(file, '/') != NULL ? strchr(file,'/') : file; pw = getpwnam(user + 1);
ret = malloc(strlen(user) + strlen(pw->pw_dir) + 1); g_free(user);
strcpy(ret, pw->pw_dir);
strcat(ret, user); if (pw == NULL)
} return g_strdup(file);
slash_pos = strchr(file, '/');
if (slash_pos == NULL)
return g_strdup (pw->pw_dir);
else else
{ return g_strconcat (pw->pw_dir, slash_pos, NULL);
ret = malloc (strlen (file) + strlen (g_get_home_dir ()) + 1);
sprintf (ret, "%s%s", g_get_home_dir (), file + 1);
}
return ret;
} }
#endif #endif
return g_strdup (file); return g_strdup (file);