Commit 61fc01e7 authored by Max Kellermann's avatar Max Kellermann

utils: pass a const string to parsePath()

Remove the slash hack, allocate memory for the user name.
parent cceaec1d
...@@ -41,7 +41,8 @@ ...@@ -41,7 +41,8 @@
#include <windows.h> #include <windows.h>
#endif #endif
char *parsePath(char *path) char *
parsePath(const char *path)
{ {
#ifndef WIN32 #ifndef WIN32
if (!g_path_is_absolute(path) && path[0] != '~') { if (!g_path_is_absolute(path) && path[0] != '~') {
...@@ -71,27 +72,24 @@ char *parsePath(char *path) ...@@ -71,27 +72,24 @@ char *parsePath(char *path)
++path; ++path;
} else { } else {
bool foundSlash = false; ++path;
struct passwd *passwd;
char *c;
for (c = path + 1; *c != '\0' && *c != '/'; c++); const char *slash = strchr(path, '/');
if (*c == '/') { char *user = slash != NULL
foundSlash = true; ? g_strndup(path, slash - path)
*c = '\0'; : g_strdup(path);
}
passwd = getpwnam(path + 1); struct passwd *passwd = getpwnam(user);
if (!passwd) { if (!passwd) {
g_warning("user \"%s\" not found", path + 1); g_warning("user \"%s\" not found", user);
g_free(user);
return NULL; return NULL;
} }
if (foundSlash) g_free(user);
*c = '/';
home = passwd->pw_dir; home = passwd->pw_dir;
path = c; path = slash;
} }
return g_strconcat(home, path, NULL); return g_strconcat(home, path, NULL);
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
} while (0) } while (0)
#endif /* !assert_static */ #endif /* !assert_static */
char *parsePath(char *path); char *
parsePath(const char *path);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment