]>
code.delx.au - pulseaudio/blob - src/polyp/util.c
4 This file is part of polypaudio.
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.
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.
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
34 #include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
52 #include "../polypcore/winsock.h"
54 #include <polypcore/core-error.h>
55 #include <polypcore/log.h>
56 #include <polypcore/core-util.h>
66 char *pa_get_user_name(char *s
, size_t l
) {
76 if (!(p
= getenv("USER")) && !(p
= getenv("LOGNAME")) && !(p
= getenv("USERNAME"))) {
79 #ifdef HAVE_GETPWUID_R
80 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
82 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
83 * that do not support getpwuid_r. */
84 if ((r
= getpwuid(getuid())) == NULL
) {
86 snprintf(s
, l
, "%lu", (unsigned long) getuid());
92 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
93 DWORD size
= sizeof(buf
);
95 if (!GetUserName(buf
, &size
))
100 #else /* HAVE_PWD_H */
102 #endif /* HAVE_PWD_H */
105 return pa_strlcpy(s
, p
, l
);
108 char *pa_get_host_name(char *s
, size_t l
) {
110 if (gethostname(s
, l
) < 0) {
111 pa_log(__FILE__
": gethostname(): %s", pa_cstrerror(errno
));
118 char *pa_get_home_dir(char *s
, size_t l
) {
123 struct passwd pw
, *r
;
128 if ((e
= getenv("HOME")))
129 return pa_strlcpy(s
, e
, l
);
131 if ((e
= getenv("USERPROFILE")))
132 return pa_strlcpy(s
, e
, l
);
135 #ifdef HAVE_GETPWUID_R
136 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
137 pa_log(__FILE__
": getpwuid_r() failed");
139 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
140 * that do not support getpwuid_r. */
141 if ((r
= getpwuid(getuid())) == NULL
) {
142 pa_log(__FILE__
": getpwuid_r() failed");
147 return pa_strlcpy(s
, r
->pw_dir
, l
);
148 #else /* HAVE_PWD_H */
153 char *pa_get_binary_name(char *s
, size_t l
) {
160 /* This works on Linux only */
162 snprintf(path
, sizeof(path
), "/proc/%u/exe", (unsigned) getpid());
163 if ((i
= readlink(path
, s
, l
-1)) < 0)
168 #elif defined(OS_IS_WIN32)
170 if (!GetModuleFileName(NULL
, path
, PATH_MAX
))
172 pa_strlcpy(s
, pa_path_get_filename(path
), l
);
179 const char *pa_path_get_filename(const char *p
) {
182 if ((fn
= strrchr(p
, PATH_SEP
)))
185 return (const char*) p
;
188 char *pa_get_fqdn(char *s
, size_t l
) {
190 #ifdef HAVE_GETADDRINFO
191 struct addrinfo
*a
, hints
;
194 if (!pa_get_host_name(hn
, sizeof(hn
)))
197 #ifdef HAVE_GETADDRINFO
198 memset(&hints
, 0, sizeof(hints
));
199 hints
.ai_family
= AF_UNSPEC
;
200 hints
.ai_flags
= AI_CANONNAME
;
202 if (getaddrinfo(hn
, NULL
, &hints
, &a
) < 0 || !a
|| !a
->ai_canonname
|| !*a
->ai_canonname
)
203 return pa_strlcpy(s
, hn
, l
);
205 pa_strlcpy(s
, a
->ai_canonname
, l
);
209 return pa_strlcpy(s
, hn
, l
);
213 int pa_msleep(unsigned long t
) {
217 #elif defined(HAVE_NANOSLEEP)
221 ts
.tv_nsec
= (t
% 1000) * 1000000;
223 return nanosleep(&ts
, NULL
);
225 #error "Platform lacks a sleep function."