summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorArnavion <arnavion@gmail.com>2014-07-19 17:09:50 -0700
committerTingPing <tingping@tingping.se>2014-07-28 14:49:14 -0400
commit7c2c8b14031d7dc28567e86195d9bcb9ecd75322 (patch)
treeaf749753b031d4c5317c6ef724ad5306ad3ca4af /src
parentc2ecb4c68c79c3be40849100f28da8961d7fcd54 (diff)
Fixed expand_homedir to handle paths like "~user" correctly.
Diffstat (limited to 'src')
-rw-r--r--src/common/util.c44
1 files changed, 22 insertions, 22 deletions
diff --git a/src/common/util.c b/src/common/util.c
index ebaab563..3b2b372e 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -252,30 +252,30 @@ expand_homedir (char *file)
 	char *ret, *user;
 	struct passwd *pw;
 
-	if (*file == '~')
+	if (file[0] == '~')
 	{
-		if (file[1] != '\0' && file[1] != '/')
-		{
-			user = strdup(file);
-			if (strchr(user,'/') != NULL)
-				*(strchr(user,'/')) = '\0';
-			if ((pw = getpwnam(user + 1)) == NULL)
-			{
-				free(user);
-				return strdup(file);
-			}
-			free(user);
-			user = strchr(file, '/') != NULL ? strchr(file,'/') : file;
-			ret = malloc(strlen(user) + strlen(pw->pw_dir) + 1);
-			strcpy(ret, pw->pw_dir);
-			strcat(ret, user);
-		}
+		if (file[1] == '\0' || file[1] == '/')
+			return g_strconcat (g_get_home_dir (), &file[1], NULL);
+
+		char *slash_pos;
+
+		user = g_strdup(file);
+
+		slash_pos = strchr(user, '/');
+		if (slash_pos != NULL)
+			*slash_pos = '\0';
+
+		pw = getpwnam(user + 1);
+		g_free(user);
+
+		if (pw == NULL)
+			return g_strdup(file);
+
+		slash_pos = strchr(file, '/');
+		if (slash_pos == NULL)
+			return g_strdup (pw->pw_dir);
 		else
-		{
-			ret = malloc (strlen (file) + strlen (g_get_home_dir ()) + 1);
-			sprintf (ret, "%s%s", g_get_home_dir (), file + 1);
-		}
-		return ret;
+			return g_strconcat (pw->pw_dir, slash_pos, NULL);
 	}
 #endif
 	return g_strdup (file);