]> code.delx.au - pulseaudio/blob - src/pulse/util.c
simple modernizations: s/assert/pa_assert
[pulseaudio] / src / pulse / util.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38
39 #ifdef HAVE_PWD_H
40 #include <pwd.h>
41 #endif
42
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
45 #endif
46
47 #ifdef HAVE_NETDB_H
48 #include <netdb.h>
49 #endif
50
51 #ifdef HAVE_WINDOWS_H
52 #include <windows.h>
53 #endif
54
55 #ifdef HAVE_SYS_PRCTL_H
56 #include <sys/prctl.h>
57 #endif
58
59 #include <pulsecore/winsock.h>
60 #include <pulsecore/core-error.h>
61 #include <pulsecore/log.h>
62 #include <pulsecore/core-util.h>
63 #include <pulsecore/macro.h>
64
65 #include "util.h"
66
67 #ifndef OS_IS_WIN32
68 #define PATH_SEP '/'
69 #else
70 #define PATH_SEP '\\'
71 #endif
72
73 char *pa_get_user_name(char *s, size_t l) {
74 char *p;
75 char buf[1024];
76
77 #ifdef HAVE_PWD_H
78 struct passwd pw, *r;
79 #endif
80
81 pa_assert(s);
82 pa_assert(l > 0);
83
84 if (!(p = getenv("USER")) && !(p = getenv("LOGNAME")) && !(p = getenv("USERNAME"))) {
85 #ifdef HAVE_PWD_H
86
87 #ifdef HAVE_GETPWUID_R
88 if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
89 #else
90 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
91 * that do not support getpwuid_r. */
92 if ((r = getpwuid(getuid())) == NULL) {
93 #endif
94 pa_snprintf(s, l, "%lu", (unsigned long) getuid());
95 return s;
96 }
97
98 p = r->pw_name;
99
100 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
101 DWORD size = sizeof(buf);
102
103 if (!GetUserName(buf, &size))
104 return NULL;
105
106 p = buf;
107
108 #else /* HAVE_PWD_H */
109 return NULL;
110 #endif /* HAVE_PWD_H */
111 }
112
113 return pa_strlcpy(s, p, l);
114 }
115
116 char *pa_get_host_name(char *s, size_t l) {
117
118 pa_assert(s);
119 pa_assert(l > 0);
120
121 if (gethostname(s, l) < 0) {
122 pa_log("gethostname(): %s", pa_cstrerror(errno));
123 return NULL;
124 }
125
126 s[l-1] = 0;
127 return s;
128 }
129
130 char *pa_get_home_dir(char *s, size_t l) {
131 char *e;
132
133 #ifdef HAVE_PWD_H
134 char buf[1024];
135 struct passwd pw, *r;
136 #endif
137
138 pa_assert(s);
139 pa_assert(l > 0);
140
141 if ((e = getenv("HOME")))
142 return pa_strlcpy(s, e, l);
143
144 if ((e = getenv("USERPROFILE")))
145 return pa_strlcpy(s, e, l);
146
147 #ifdef HAVE_PWD_H
148 #ifdef HAVE_GETPWUID_R
149 if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
150 pa_log("getpwuid_r() failed");
151 #else
152 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
153 * that do not support getpwuid_r. */
154 if ((r = getpwuid(getuid())) == NULL) {
155 pa_log("getpwuid_r() failed");
156 #endif
157 return NULL;
158 }
159
160 return pa_strlcpy(s, r->pw_dir, l);
161 #else /* HAVE_PWD_H */
162 return NULL;
163 #endif
164 }
165
166 char *pa_get_binary_name(char *s, size_t l) {
167
168 pa_assert(s);
169 pa_assert(l > 0);
170
171 #if defined(OS_IS_WIN32)
172 {
173 char path[PATH_MAX];
174
175 if (GetModuleFileName(NULL, path, PATH_MAX))
176 return pa_strlcpy(s, pa_path_get_filename(path), l);
177 }
178 #endif
179
180 #ifdef __linux__
181 {
182 int i;
183 char path[PATH_MAX];
184 /* This works on Linux only */
185
186 if ((i = readlink("/proc/self/exe", path, sizeof(path)-1)) >= 0) {
187 path[i] = 0;
188 return pa_strlcpy(s, pa_path_get_filename(path), l);
189 }
190 }
191
192 #endif
193
194 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
195 {
196
197 #ifndef TASK_COMM_LEN
198 /* Actually defined in linux/sched.h */
199 #define TASK_COMM_LEN 16
200 #endif
201
202 char tcomm[TASK_COMM_LEN+1];
203 memset(tcomm, 0, sizeof(tcomm));
204
205 /* This works on Linux only */
206 if (prctl(PR_GET_NAME, (unsigned long) tcomm, 0, 0, 0) == 0)
207 return pa_strlcpy(s, tcomm, l);
208
209 }
210 #endif
211
212 return NULL;
213 }
214
215 char *pa_path_get_filename(const char *p) {
216 char *fn;
217
218 pa_assert(p);
219
220 if ((fn = strrchr(p, PATH_SEP)))
221 return fn+1;
222
223 return (char*) p;
224 }
225
226 char *pa_get_fqdn(char *s, size_t l) {
227 char hn[256];
228 #ifdef HAVE_GETADDRINFO
229 struct addrinfo *a, hints;
230 #endif
231
232 pa_assert(s);
233 pa_assert(l > 0);
234
235 if (!pa_get_host_name(hn, sizeof(hn)))
236 return NULL;
237
238 #ifdef HAVE_GETADDRINFO
239 memset(&hints, 0, sizeof(hints));
240 hints.ai_family = AF_UNSPEC;
241 hints.ai_flags = AI_CANONNAME;
242
243 if (getaddrinfo(hn, NULL, &hints, &a) < 0 || !a || !a->ai_canonname || !*a->ai_canonname)
244 return pa_strlcpy(s, hn, l);
245
246 pa_strlcpy(s, a->ai_canonname, l);
247 freeaddrinfo(a);
248 return s;
249 #else
250 return pa_strlcpy(s, hn, l);
251 #endif
252 }
253
254 int pa_msleep(unsigned long t) {
255 #ifdef OS_IS_WIN32
256 Sleep(t);
257 return 0;
258 #elif defined(HAVE_NANOSLEEP)
259 struct timespec ts;
260
261 ts.tv_sec = t/1000;
262 ts.tv_nsec = (t % 1000) * 1000000;
263
264 return nanosleep(&ts, NULL);
265 #else
266 #error "Platform lacks a sleep function."
267 #endif
268 }