aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/utils.c71
1 files changed, 36 insertions, 35 deletions
diff --git a/src/utils.c b/src/utils.c
index 53494cc5d..d3b21d369 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2010 The Music Player Daemon Project
+ * Copyright (C) 2003-2011 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@@ -19,6 +19,7 @@
#include "config.h"
#include "utils.h"
+#include "glib_compat.h"
#include "conf.h"
#include <glib.h>
@@ -41,14 +42,25 @@
#include <windows.h>
#endif
-char *parsePath(char *path)
+G_GNUC_CONST
+static inline GQuark
+parse_path_quark(void)
{
+ return g_quark_from_static_string("path");
+}
+
+char *
+parsePath(const char *path, G_GNUC_UNUSED GError **error_r)
+{
+ assert(path != NULL);
+ assert(error_r == NULL || *error_r == NULL);
+
#ifndef WIN32
if (!g_path_is_absolute(path) && path[0] != '~') {
- g_warning("\"%s\" is not an absolute path", path);
+ g_set_error(error_r, parse_path_quark(), 0,
+ "not an absolute path: %s", path);
return NULL;
} else if (path[0] == '~') {
- size_t pos = 1;
const char *home;
if (path[1] == '/' || path[1] == '\0') {
@@ -56,7 +68,8 @@ char *parsePath(char *path)
if (user != NULL) {
struct passwd *passwd = getpwnam(user);
if (!passwd) {
- g_warning("no such user %s", user);
+ g_set_error(error_r, parse_path_quark(), 0,
+ "no such user: %s", user);
return NULL;
}
@@ -64,36 +77,37 @@ char *parsePath(char *path)
} else {
home = g_get_home_dir();
if (home == NULL) {
- g_warning("problems getting home "
- "for current user");
+ g_set_error_literal(error_r, parse_path_quark(), 0,
+ "problems getting home "
+ "for current user");
return NULL;
}
}
+
+ ++path;
} else {
- bool foundSlash = false;
- struct passwd *passwd;
- char *c;
-
- for (c = path + 1; *c != '\0' && *c != '/'; c++);
- if (*c == '/') {
- foundSlash = true;
- *c = '\0';
- }
- pos = c - path;
+ ++path;
- passwd = getpwnam(path + 1);
+ const char *slash = strchr(path, '/');
+ char *user = slash != NULL
+ ? g_strndup(path, slash - path)
+ : g_strdup(path);
+
+ struct passwd *passwd = getpwnam(user);
if (!passwd) {
- g_warning("user \"%s\" not found", path + 1);
+ g_set_error(error_r, parse_path_quark(), 0,
+ "no such user: %s", user);
+ g_free(user);
return NULL;
}
- if (foundSlash)
- *c = '/';
+ g_free(user);
home = passwd->pw_dir;
+ path = slash;
}
- return g_strconcat(home, path + pos, NULL);
+ return g_strconcat(home, path, NULL);
} else {
#endif
return g_strdup(path);
@@ -101,16 +115,3 @@ char *parsePath(char *path)
}
#endif
}
-
-bool
-string_array_contains(const char *const* haystack, const char *needle)
-{
- assert(haystack != NULL);
- assert(needle != NULL);
-
- for (; *haystack != NULL; ++haystack)
- if (g_ascii_strcasecmp(*haystack, needle) == 0)
- return true;
-
- return false;
-}