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