1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/select.h>
34 char *myFgets(char *buffer, int bufferSize, FILE * fp)
36 char *ret = fgets(buffer, bufferSize, fp);
37 if (ret && strlen(buffer) > 0 && buffer[strlen(buffer) - 1] == '\n') {
38 buffer[strlen(buffer) - 1] = '\0';
40 if (ret && strlen(buffer) > 0 && buffer[strlen(buffer) - 1] == '\r') {
41 buffer[strlen(buffer) - 1] = '\0';
46 char *strDupToUpper(char *str)
48 char *ret = xstrdup(str);
51 for (i = 0; i < strlen(str); i++)
52 ret[i] = toupper((int)ret[i]);
57 void stripReturnChar(char *string)
59 while (string && (string = strchr(string, '\n'))) {
64 void my_usleep(long usec)
71 select(0, NULL, NULL, NULL, &tv);
74 int ipv6Supported(void)
78 s = socket(AF_INET6, SOCK_STREAM, 0);
87 char *appendToString(char *dest, const char *src)
90 int srclen = strlen(src);
93 dest = xmalloc(srclen + 1);
94 memset(dest, 0, srclen + 1);
97 destlen = strlen(dest);
98 dest = xrealloc(dest, destlen + srclen + 1);
101 memcpy(dest + destlen, src, srclen);
102 dest[destlen + srclen] = '\0';
107 unsigned long readLEuint32(const unsigned char *p)
109 return ((unsigned long)p[0] << 0) |
110 ((unsigned long)p[1] << 8) |
111 ((unsigned long)p[2] << 16) | ((unsigned long)p[3] << 24);
114 mpd_malloc char *xstrdup(const char *s)
116 char *ret = strdup(s);
117 if (mpd_unlikely(!ret))
118 FATAL("OOM: strdup\n");
122 /* borrowed from git :) */
124 mpd_malloc void *xmalloc(size_t size)
128 assert(mpd_likely(size));
131 if (mpd_unlikely(!ret))
132 FATAL("OOM: malloc\n");
136 mpd_malloc void *xrealloc(void *ptr, size_t size)
138 void *ret = realloc(ptr, size);
140 /* some C libraries return NULL when size == 0,
141 * make sure we get a free()-able pointer (free(NULL)
142 * doesn't work with all C libraries, either) */
143 if (mpd_unlikely(!ret && !size))
144 ret = realloc(ptr, 1);
146 if (mpd_unlikely(!ret))
147 FATAL("OOM: realloc\n");
151 mpd_malloc void *xcalloc(size_t nmemb, size_t size)
155 assert(mpd_likely(nmemb && size));
157 ret = calloc(nmemb, size);
158 if (mpd_unlikely(!ret))
159 FATAL("OOM: calloc\n");
163 char *parsePath(char *path)
166 struct passwd *passwd;
172 if (path[0] != '/' && path[0] != '~') {
173 ERROR("\"%s\" is not an absolute path\n", path);
175 } else if (path[0] == '~') {
176 if (path[1] == '/' || path[1] == '\0') {
177 param = getConfigParam(CONF_USER);
178 if (param && param->value) {
179 passwd = getpwnam(param->value);
181 ERROR("no such user %s\n",
186 passwd = getpwuid(geteuid());
188 ERROR("problems getting passwd entry "
189 "for current user\n");
194 for (c = path + 1; *c != '\0' && *c != '/'; c++);
201 passwd = getpwnam(path + 1);
203 ERROR("user \"%s\" not found\n", path + 1);
211 newPath = xmalloc(strlen(passwd->pw_dir) + strlen(path + pos) + 1);
212 strcpy(newPath, passwd->pw_dir);
213 strcat(newPath, path + pos);
215 newPath = xstrdup(path);