]>
code.delx.au - pulseaudio/blob - src/polypcore/util.c
f53eef343958c52ec9fc60bcedbd68ec8d92e26a
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
38 #include <sys/types.h>
46 #ifdef HAVE_SYS_RESOURCE_H
47 #include <sys/resource.h>
62 #include <samplerate.h>
73 #include <polypcore/xmalloc.h>
74 #include <polypcore/log.h>
79 #define PA_RUNTIME_PATH_PREFIX "/tmp/polypaudio-"
82 #define PA_RUNTIME_PATH_PREFIX "%TEMP%\\polypaudio-"
88 #define POLYP_ROOTENV "POLYP_ROOT"
90 int pa_set_root(HANDLE handle
) {
91 char library_path
[MAX_PATH
+ sizeof(POLYP_ROOTENV
) + 1], *sep
;
93 strcpy(library_path
, POLYP_ROOTENV
"=");
95 if (!GetModuleFileName(handle
, library_path
+ sizeof(POLYP_ROOTENV
), MAX_PATH
))
98 sep
= strrchr(library_path
, '\\');
102 if (_putenv(library_path
) < 0)
110 /** Make a file descriptor nonblock. Doesn't do any error checking */
111 void pa_make_nonblock_fd(int fd
) {
116 if ((v
= fcntl(fd
, F_GETFL
)) >= 0)
117 if (!(v
& O_NONBLOCK
))
118 fcntl(fd
, F_SETFL
, v
|O_NONBLOCK
);
119 #elif defined(OS_IS_WIN32)
121 if (ioctlsocket(fd
, FIONBIO
, &arg
) < 0) {
122 if (WSAGetLastError() == WSAENOTSOCK
)
123 pa_log_warn(__FILE__
": WARNING: Only sockets can be made non-blocking!");
126 pa_log_warn(__FILE__
": WARNING: Non-blocking I/O not supported.!");
130 /** Creates a directory securely */
131 int pa_make_secure_dir(const char* dir
) {
138 if (mkdir(dir
, 0700) < 0)
144 if (lstat(dir
, &st
) < 0)
146 if (stat(dir
, &st
) < 0)
151 if (!S_ISDIR(st
.st_mode
) || (st
.st_uid
!= getuid()) || ((st
.st_mode
& 0777) != 0700))
154 fprintf(stderr
, "FIXME: pa_make_secure_dir()\n");
164 /* Creates a the parent directory of the specified path securely */
165 int pa_make_secure_parent_dir(const char *fn
) {
167 char *slash
, *dir
= pa_xstrdup(fn
);
169 slash
= pa_path_get_filename(dir
);
174 if (pa_make_secure_dir(dir
) < 0)
185 /** Calls read() in a loop. Makes sure that as much as 'size' bytes,
186 * unless EOF is reached or an error occured */
187 ssize_t
pa_loop_read(int fd
, void*data
, size_t size
) {
189 assert(fd
>= 0 && data
&& size
);
194 if ((r
= read(fd
, data
, size
)) < 0)
201 data
= (uint8_t*) data
+ r
;
208 /** Similar to pa_loop_read(), but wraps write() */
209 ssize_t
pa_loop_write(int fd
, const void*data
, size_t size
) {
211 assert(fd
>= 0 && data
&& size
);
216 if ((r
= write(fd
, data
, size
)) < 0)
223 data
= (const uint8_t*) data
+ r
;
230 /* Print a warning messages in case that the given signal is not
231 * blocked or trapped */
232 void pa_check_signal_is_blocked(int sig
) {
233 #ifdef HAVE_SIGACTION
237 /* If POSIX threads are supported use thread-aware
238 * pthread_sigmask() function, to check if the signal is
239 * blocked. Otherwise fall back to sigprocmask() */
242 if (pthread_sigmask(SIG_SETMASK
, NULL
, &set
) < 0) {
244 if (sigprocmask(SIG_SETMASK
, NULL
, &set
) < 0) {
245 pa_log(__FILE__
": sigprocmask() failed: %s", strerror(errno
));
252 if (sigismember(&set
, sig
))
255 /* Check whether the signal is trapped */
257 if (sigaction(sig
, NULL
, &sa
) < 0) {
258 pa_log(__FILE__
": sigaction() failed: %s", strerror(errno
));
262 if (sa
.sa_handler
!= SIG_DFL
)
265 pa_log(__FILE__
": WARNING: %s is not trapped. This might cause malfunction!", pa_strsignal(sig
));
266 #else /* HAVE_SIGACTION */
267 pa_log(__FILE__
": WARNING: %s might not be trapped. This might cause malfunction!", pa_strsignal(sig
));
271 /* The following function is based on an example from the GNU libc
272 * documentation. This function is similar to GNU's asprintf(). */
273 char *pa_sprintf_malloc(const char *format
, ...) {
283 c
= pa_xrealloc(c
, size
);
285 va_start(ap
, format
);
286 r
= vsnprintf(c
, size
, format
, ap
);
289 if (r
> -1 && r
< size
)
292 if (r
> -1) /* glibc 2.1 */
299 /* Same as the previous function, but use a va_list instead of an
301 char *pa_vsprintf_malloc(const char *format
, va_list ap
) {
309 c
= pa_xrealloc(c
, size
);
310 r
= vsnprintf(c
, size
, format
, ap
);
312 if (r
> -1 && r
< size
)
315 if (r
> -1) /* glibc 2.1 */
322 /* Return the current username in the specified string buffer. */
323 char *pa_get_user_name(char *s
, size_t l
) {
328 struct passwd pw
, *r
;
333 if (!(p
= getenv("USER")) && !(p
= getenv("LOGNAME")) && !(p
= getenv("USERNAME"))) {
336 #ifdef HAVE_GETPWUID_R
337 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
339 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
340 * that do not support getpwuid_r. */
341 if ((r
= getpwuid(getuid())) == NULL
) {
343 snprintf(s
, l
, "%lu", (unsigned long) getuid());
349 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
350 DWORD size
= sizeof(buf
);
352 if (!GetUserName(buf
, &size
))
357 #else /* HAVE_PWD_H */
359 #endif /* HAVE_PWD_H */
362 return pa_strlcpy(s
, p
, l
);
365 /* Return the current hostname in the specified buffer. */
366 char *pa_get_host_name(char *s
, size_t l
) {
368 if (gethostname(s
, l
) < 0) {
369 pa_log(__FILE__
": gethostname(): %s", strerror(errno
));
376 /* Return the home directory of the current user */
377 char *pa_get_home_dir(char *s
, size_t l
) {
382 struct passwd pw
, *r
;
387 if ((e
= getenv("HOME")))
388 return pa_strlcpy(s
, e
, l
);
390 if ((e
= getenv("USERPROFILE")))
391 return pa_strlcpy(s
, e
, l
);
394 #ifdef HAVE_GETPWUID_R
395 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
396 pa_log(__FILE__
": getpwuid_r() failed");
398 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
399 * that do not support getpwuid_r. */
400 if ((r
= getpwuid(getuid())) == NULL
) {
401 pa_log(__FILE__
": getpwuid_r() failed");
406 return pa_strlcpy(s
, r
->pw_dir
, l
);
407 #else /* HAVE_PWD_H */
412 /* Similar to OpenBSD's strlcpy() function */
413 char *pa_strlcpy(char *b
, const char *s
, size_t l
) {
414 assert(b
&& s
&& l
> 0);
421 int pa_gettimeofday(struct timeval
*tv
) {
422 #ifdef HAVE_GETTIMEOFDAY
423 return gettimeofday(tv
, NULL
);
424 #elif defined(OS_IS_WIN32)
426 * Copied from implementation by Steven Edwards (LGPL).
427 * Found on wine mailing list.
430 #if defined(_MSC_VER) || defined(__BORLANDC__)
431 #define EPOCHFILETIME (116444736000000000i64)
433 #define EPOCHFILETIME (116444736000000000LL)
441 GetSystemTimeAsFileTime(&ft
);
442 li
.LowPart
= ft
.dwLowDateTime
;
443 li
.HighPart
= ft
.dwHighDateTime
;
444 t
= li
.QuadPart
; /* In 100-nanosecond intervals */
445 t
-= EPOCHFILETIME
; /* Offset to the Epoch time */
446 t
/= 10; /* In microseconds */
447 tv
->tv_sec
= (long)(t
/ 1000000);
448 tv
->tv_usec
= (long)(t
% 1000000);
453 #error "Platform lacks gettimeofday() or equivalent function."
457 /* Calculate the difference between the two specfified timeval
459 pa_usec_t
pa_timeval_diff(const struct timeval
*a
, const struct timeval
*b
) {
463 /* Check which whan is the earlier time and swap the two arguments if reuqired. */
464 if (pa_timeval_cmp(a
, b
) < 0) {
465 const struct timeval
*c
;
471 /* Calculate the second difference*/
472 r
= ((pa_usec_t
) a
->tv_sec
- b
->tv_sec
)* 1000000;
474 /* Calculate the microsecond difference */
475 if (a
->tv_usec
> b
->tv_usec
)
476 r
+= ((pa_usec_t
) a
->tv_usec
- b
->tv_usec
);
477 else if (a
->tv_usec
< b
->tv_usec
)
478 r
-= ((pa_usec_t
) b
->tv_usec
- a
->tv_usec
);
483 /* Compare the two timeval structs and return 0 when equal, negative when a < b, positive otherwse */
484 int pa_timeval_cmp(const struct timeval
*a
, const struct timeval
*b
) {
487 if (a
->tv_sec
< b
->tv_sec
)
490 if (a
->tv_sec
> b
->tv_sec
)
493 if (a
->tv_usec
< b
->tv_usec
)
496 if (a
->tv_usec
> b
->tv_usec
)
502 /* Return the time difference between now and the specified timestamp */
503 pa_usec_t
pa_timeval_age(const struct timeval
*tv
) {
506 pa_gettimeofday(&now
);
507 return pa_timeval_diff(&now
, tv
);
510 /* Add the specified time inmicroseconds to the specified timeval structure */
511 void pa_timeval_add(struct timeval
*tv
, pa_usec_t v
) {
516 tv
->tv_sec
+= (unsigned long) secs
;
522 while (tv
->tv_usec
>= 1000000) {
524 tv
->tv_usec
-= 1000000;
528 #define NICE_LEVEL (-15)
530 /* Raise the priority of the current process as much as possible and
531 sensible: set the nice level to -15 and enable realtime scheduling if
533 void pa_raise_priority(void) {
535 #ifdef HAVE_SYS_RESOURCE_H
536 if (setpriority(PRIO_PROCESS
, 0, NICE_LEVEL
) < 0)
537 pa_log_warn(__FILE__
": setpriority() failed: %s", strerror(errno
));
539 pa_log_info(__FILE__
": Successfully gained nice level %i.", NICE_LEVEL
);
542 #ifdef _POSIX_PRIORITY_SCHEDULING
544 struct sched_param sp
;
546 if (sched_getparam(0, &sp
) < 0) {
547 pa_log(__FILE__
": sched_getparam() failed: %s", strerror(errno
));
551 sp
.sched_priority
= 1;
552 if (sched_setscheduler(0, SCHED_FIFO
, &sp
) < 0) {
553 pa_log_warn(__FILE__
": sched_setscheduler() failed: %s", strerror(errno
));
557 pa_log_info(__FILE__
": Successfully enabled SCHED_FIFO scheduling.");
562 if (!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS
))
563 pa_log_warn(__FILE__
": SetPriorityClass() failed: 0x%08X", GetLastError());
565 pa_log_info(__FILE__
": Successfully gained high priority class.");
569 /* Reset the priority to normal, inverting the changes made by pa_raise_priority() */
570 void pa_reset_priority(void) {
572 SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS
);
575 #ifdef _POSIX_PRIORITY_SCHEDULING
577 struct sched_param sp
;
578 sched_getparam(0, &sp
);
579 sp
.sched_priority
= 0;
580 sched_setscheduler(0, SCHED_OTHER
, &sp
);
584 #ifdef HAVE_SYS_RESOURCE_H
585 setpriority(PRIO_PROCESS
, 0, 0);
589 /* Set the FD_CLOEXEC flag for a fd */
590 int pa_fd_set_cloexec(int fd
, int b
) {
596 if ((v
= fcntl(fd
, F_GETFD
, 0)) < 0)
599 v
= (v
& ~FD_CLOEXEC
) | (b
? FD_CLOEXEC
: 0);
601 if (fcntl(fd
, F_SETFD
, v
) < 0)
608 /* Return the binary file name of the current process. Works on Linux
609 * only. This shoul be used for eyecandy only, don't rely on return
611 char *pa_get_binary_name(char *s
, size_t l
) {
618 /* This works on Linux only */
620 snprintf(path
, sizeof(path
), "/proc/%u/exe", (unsigned) getpid());
621 if ((i
= readlink(path
, s
, l
-1)) < 0)
626 #elif defined(OS_IS_WIN32)
628 if (!GetModuleFileName(NULL
, path
, PATH_MAX
))
630 pa_strlcpy(s
, pa_path_get_filename(path
), l
);
637 /* Return a pointer to the filename inside a path (which is the last
639 char *pa_path_get_filename(const char *p
) {
642 if ((fn
= strrchr(p
, PATH_SEP
)))
648 /* Try to parse a boolean string value.*/
649 int pa_parse_boolean(const char *v
) {
651 if (!strcmp(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
653 else if (!strcmp(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
659 /* Split the specified string wherever one of the strings in delimiter
660 * occurs. Each time it is called returns a newly allocated string
661 * with pa_xmalloc(). The variable state points to, should be
662 * initiallized to NULL before the first call. */
663 char *pa_split(const char *c
, const char *delimiter
, const char**state
) {
664 const char *current
= *state
? *state
: c
;
670 l
= strcspn(current
, delimiter
);
676 return pa_xstrndup(current
, l
);
679 /* What is interpreted as whitespace? */
680 #define WHITESPACE " \t\n"
682 /* Split a string into words. Otherwise similar to pa_split(). */
683 char *pa_split_spaces(const char *c
, const char **state
) {
684 const char *current
= *state
? *state
: c
;
687 if (!*current
|| *c
== 0)
690 current
+= strspn(current
, WHITESPACE
);
691 l
= strcspn(current
, WHITESPACE
);
695 return pa_xstrndup(current
, l
);
698 /* Return the name of an UNIX signal. Similar to GNU's strsignal() */
699 const char *pa_strsignal(int sig
) {
701 case SIGINT
: return "SIGINT";
702 case SIGTERM
: return "SIGTERM";
704 case SIGUSR1
: return "SIGUSR1";
707 case SIGUSR2
: return "SIGUSR2";
710 case SIGXCPU
: return "SIGXCPU";
713 case SIGPIPE
: return "SIGPIPE";
716 case SIGCHLD
: return "SIGCHLD";
719 case SIGHUP
: return "SIGHUP";
721 default: return "UNKNOWN SIGNAL";
727 /* Check whether the specified GID and the group name match */
728 static int is_group(gid_t gid
, const char *name
) {
729 struct group group
, *result
= NULL
;
734 #ifdef HAVE_GETGRGID_R
735 #ifdef _SC_GETGR_R_SIZE_MAX
736 n
= sysconf(_SC_GETGR_R_SIZE_MAX
);
741 data
= pa_xmalloc(n
);
743 if (getgrgid_r(gid
, &group
, data
, n
, &result
) < 0 || !result
) {
744 pa_log(__FILE__
": getgrgid_r(%u) failed: %s", gid
, strerror(errno
));
749 r
= strcmp(name
, result
->gr_name
) == 0;
754 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X) that do not
755 * support getgrgid_r. */
756 if ((result
= getgrgid(gid
)) == NULL
) {
757 pa_log(__FILE__
": getgrgid(%u) failed: %s", gid
, strerror(errno
));
761 r
= strcmp(name
, result
->gr_name
) == 0;
769 /* Check the current user is member of the specified group */
770 int pa_uid_in_group(const char *name
, gid_t
*gid
) {
771 GETGROUPS_T
*gids
, tgid
;
772 int n
= sysconf(_SC_NGROUPS_MAX
);
777 gids
= pa_xmalloc(sizeof(GETGROUPS_T
)*n
);
779 if ((n
= getgroups(n
, gids
)) < 0) {
780 pa_log(__FILE__
": getgroups() failed: %s", strerror(errno
));
784 for (i
= 0; i
< n
; i
++) {
785 if (is_group(gids
[i
], name
) > 0) {
792 if (is_group(tgid
= getgid(), name
) > 0) {
806 #else /* HAVE_GRP_H */
808 int pa_uid_in_group(const char *name
, gid_t
*gid
) {
814 /* Lock or unlock a file entirely.
815 (advisory on UNIX, mandatory on Windows) */
816 int pa_lock_fd(int fd
, int b
) {
820 /* Try a R/W lock first */
822 flock
.l_type
= b
? F_WRLCK
: F_UNLCK
;
823 flock
.l_whence
= SEEK_SET
;
827 if (fcntl(fd
, F_SETLKW
, &flock
) >= 0)
830 /* Perhaps the file descriptor qas opened for read only, than try again with a read lock. */
831 if (b
&& errno
== EBADF
) {
832 flock
.l_type
= F_RDLCK
;
833 if (fcntl(fd
, F_SETLKW
, &flock
) >= 0)
837 pa_log(__FILE__
": %slock failed: %s", !b
? "un" : "", strerror(errno
));
841 HANDLE h
= (HANDLE
)_get_osfhandle(fd
);
843 if (b
&& LockFile(h
, 0, 0, 0xFFFFFFFF, 0xFFFFFFFF))
845 if (!b
&& UnlockFile(h
, 0, 0, 0xFFFFFFFF, 0xFFFFFFFF))
848 pa_log(__FILE__
": %slock failed: 0x%08X", !b
? "un" : "", GetLastError());
854 /* Remove trailing newlines from a string */
855 char* pa_strip_nl(char *s
) {
858 s
[strcspn(s
, "\r\n")] = 0;
862 /* Create a temporary lock file and lock it. */
863 int pa_lock_lockfile(const char *fn
) {
870 if ((fd
= open(fn
, O_CREAT
|O_RDWR
, S_IRUSR
|S_IWUSR
)) < 0) {
871 pa_log(__FILE__
": failed to create lock file '%s': %s", fn
, strerror(errno
));
875 if (pa_lock_fd(fd
, 1) < 0) {
876 pa_log(__FILE__
": failed to lock file '%s'.", fn
);
880 if (fstat(fd
, &st
) < 0) {
881 pa_log(__FILE__
": failed to fstat() file '%s'.", fn
);
885 /* Check wheter the file has been removed meanwhile. When yes, restart this loop, otherwise, we're done */
886 if (st
.st_nlink
>= 1)
889 if (pa_lock_fd(fd
, 0) < 0) {
890 pa_log(__FILE__
": failed to unlock file '%s'.", fn
);
895 pa_log(__FILE__
": failed to close file '%s'.", fn
);
912 /* Unlock a temporary lcok file */
913 int pa_unlock_lockfile(const char *fn
, int fd
) {
915 assert(fn
&& fd
>= 0);
917 if (unlink(fn
) < 0) {
918 pa_log_warn(__FILE__
": WARNING: unable to remove lock file '%s': %s", fn
, strerror(errno
));
922 if (pa_lock_fd(fd
, 0) < 0) {
923 pa_log_warn(__FILE__
": WARNING: failed to unlock file '%s'.", fn
);
928 pa_log_warn(__FILE__
": WARNING: failed to close lock file '%s': %s", fn
, strerror(errno
));
935 /* Try to open a configuration file. If "env" is specified, open the
936 * value of the specified environment variable. Otherwise look for a
937 * file "local" in the home directory or a file "global" in global
938 * file system. If "result" is non-NULL, a pointer to a newly
939 * allocated buffer containing the used configuration file is
941 FILE *pa_open_config_file(const char *global
, const char *local
, const char *env
, char **result
) {
948 if (!getenv(POLYP_ROOTENV
))
952 if (env
&& (fn
= getenv(env
))) {
954 if (!ExpandEnvironmentStrings(fn
, buf
, PATH_MAX
))
960 *result
= pa_xstrdup(fn
);
962 return fopen(fn
, "r");
965 if (local
&& pa_get_home_dir(h
, sizeof(h
))) {
969 fn
= lfn
= pa_sprintf_malloc("%s/%s", h
, local
);
972 if (!ExpandEnvironmentStrings(lfn
, buf
, PATH_MAX
))
979 if (f
|| errno
!= ENOENT
) {
981 *result
= pa_xstrdup(fn
);
997 if (!ExpandEnvironmentStrings(global
, buf
, PATH_MAX
))
1003 *result
= pa_xstrdup(global
);
1005 return fopen(global
, "r");
1008 /* Format the specified data as a hexademical string */
1009 char *pa_hexstr(const uint8_t* d
, size_t dlength
, char *s
, size_t slength
) {
1010 size_t i
= 0, j
= 0;
1011 const char hex
[] = "0123456789abcdef";
1012 assert(d
&& s
&& slength
> 0);
1014 while (i
< dlength
&& j
+3 <= slength
) {
1015 s
[j
++] = hex
[*d
>> 4];
1016 s
[j
++] = hex
[*d
& 0xF];
1022 s
[j
< slength
? j
: slength
] = 0;
1026 /* Convert a hexadecimal digit to a number or -1 if invalid */
1027 static int hexc(char c
) {
1028 if (c
>= '0' && c
<= '9')
1031 if (c
>= 'A' && c
<= 'F')
1032 return c
- 'A' + 10;
1034 if (c
>= 'a' && c
<= 'f')
1035 return c
- 'a' + 10;
1040 /* Parse a hexadecimal string as created by pa_hexstr() to a BLOB */
1041 size_t pa_parsehex(const char *p
, uint8_t *d
, size_t dlength
) {
1045 while (j
< dlength
&& *p
) {
1048 if ((b
= hexc(*(p
++))) < 0)
1051 d
[j
] = (uint8_t) (b
<< 4);
1056 if ((b
= hexc(*(p
++))) < 0)
1059 d
[j
] |= (uint8_t) b
;
1066 /* Return the fully qualified domain name in *s */
1067 char *pa_get_fqdn(char *s
, size_t l
) {
1069 #ifdef HAVE_GETADDRINFO
1070 struct addrinfo
*a
, hints
;
1073 if (!pa_get_host_name(hn
, sizeof(hn
)))
1076 #ifdef HAVE_GETADDRINFO
1077 memset(&hints
, 0, sizeof(hints
));
1078 hints
.ai_family
= AF_UNSPEC
;
1079 hints
.ai_flags
= AI_CANONNAME
;
1081 if (getaddrinfo(hn
, NULL
, &hints
, &a
) < 0 || !a
|| !a
->ai_canonname
|| !*a
->ai_canonname
)
1082 return pa_strlcpy(s
, hn
, l
);
1084 pa_strlcpy(s
, a
->ai_canonname
, l
);
1088 return pa_strlcpy(s
, hn
, l
);
1092 /* Returns nonzero when *s starts with *pfx */
1093 int pa_startswith(const char *s
, const char *pfx
) {
1101 return strlen(s
) >= l
&& strncmp(s
, pfx
, l
) == 0;
1104 /* Returns nonzero when *s ends with *sfx */
1105 int pa_endswith(const char *s
, const char *sfx
) {
1114 return l1
>= l2
&& strcmp(s
+l1
-l2
, sfx
) == 0;
1117 /* if fn is null return the polypaudio run time path in s (/tmp/polypaudio)
1118 * if fn is non-null and starts with / return fn in s
1119 * otherwise append fn to the run time path and return it in s */
1120 char *pa_runtime_path(const char *fn
, char *s
, size_t l
) {
1124 if (fn
&& *fn
== '/')
1126 if (fn
&& strlen(fn
) >= 3 && isalpha(fn
[0]) && fn
[1] == ':' && fn
[2] == '\\')
1128 return pa_strlcpy(s
, fn
, l
);
1131 snprintf(s
, l
, "%s%s%c%s", PA_RUNTIME_PATH_PREFIX
, pa_get_user_name(u
, sizeof(u
)), PATH_SEP
, fn
);
1133 snprintf(s
, l
, "%s%s", PA_RUNTIME_PATH_PREFIX
, pa_get_user_name(u
, sizeof(u
)));
1139 ExpandEnvironmentStrings(buf
, s
, l
);
1146 /* Wait t milliseconds */
1147 int pa_msleep(unsigned long t
) {
1151 #elif defined(HAVE_NANOSLEEP)
1155 ts
.tv_nsec
= (t
% 1000) * 1000000;
1157 return nanosleep(&ts
, NULL
);
1159 #error "Platform lacks a sleep function."
1163 /* Convert the string s to a signed integer in *ret_i */
1164 int pa_atoi(const char *s
, int32_t *ret_i
) {
1169 l
= strtol(s
, &x
, 0);
1174 *ret_i
= (int32_t) l
;
1179 /* Convert the string s to an unsigned integer in *ret_u */
1180 int pa_atou(const char *s
, uint32_t *ret_u
) {
1185 l
= strtoul(s
, &x
, 0);
1190 *ret_u
= (uint32_t) l
;