configure.ac: Move OggVorbis Encoder to Encoder Plugins.
[mpd-mk.git] / src / utils.c
blob53494cc5d68c804f23cbc20855dca5f9849a4ae4
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "utils.h"
22 #include "conf.h"
24 #include <glib.h>
26 #include <assert.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #include <errno.h>
32 #ifndef WIN32
33 #include <pwd.h>
34 #endif
36 #ifdef HAVE_IPV6
37 #include <sys/socket.h>
38 #endif
40 #ifdef WIN32
41 #include <windows.h>
42 #endif
44 char *parsePath(char *path)
46 #ifndef WIN32
47 if (!g_path_is_absolute(path) && path[0] != '~') {
48 g_warning("\"%s\" is not an absolute path", path);
49 return NULL;
50 } else if (path[0] == '~') {
51 size_t pos = 1;
52 const char *home;
54 if (path[1] == '/' || path[1] == '\0') {
55 const char *user = config_get_string(CONF_USER, NULL);
56 if (user != NULL) {
57 struct passwd *passwd = getpwnam(user);
58 if (!passwd) {
59 g_warning("no such user %s", user);
60 return NULL;
63 home = passwd->pw_dir;
64 } else {
65 home = g_get_home_dir();
66 if (home == NULL) {
67 g_warning("problems getting home "
68 "for current user");
69 return NULL;
72 } else {
73 bool foundSlash = false;
74 struct passwd *passwd;
75 char *c;
77 for (c = path + 1; *c != '\0' && *c != '/'; c++);
78 if (*c == '/') {
79 foundSlash = true;
80 *c = '\0';
82 pos = c - path;
84 passwd = getpwnam(path + 1);
85 if (!passwd) {
86 g_warning("user \"%s\" not found", path + 1);
87 return NULL;
90 if (foundSlash)
91 *c = '/';
93 home = passwd->pw_dir;
96 return g_strconcat(home, path + pos, NULL);
97 } else {
98 #endif
99 return g_strdup(path);
100 #ifndef WIN32
102 #endif
105 bool
106 string_array_contains(const char *const* haystack, const char *needle)
108 assert(haystack != NULL);
109 assert(needle != NULL);
111 for (; *haystack != NULL; ++haystack)
112 if (g_ascii_strcasecmp(*haystack, needle) == 0)
113 return true;
115 return false;