]>
code.delx.au - pulseaudio/blob - src/polypcore/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
38 #include <sys/types.h>
46 #ifdef HAVE_SYS_RESOURCE_H
47 #include <sys/resource.h>
62 #include <samplerate.h>
78 #define PA_RUNTIME_PATH_PREFIX "/tmp/polypaudio-"
81 #define PA_RUNTIME_PATH_PREFIX "%TEMP%\\polypaudio-"
87 #define POLYP_ROOTENV "POLYP_ROOT"
89 int pa_set_root(HANDLE handle
) {
90 char library_path
[MAX_PATH
+ sizeof(POLYP_ROOTENV
) + 1], *sep
;
92 strcpy(library_path
, POLYP_ROOTENV
"=");
94 if (!GetModuleFileName(handle
, library_path
+ sizeof(POLYP_ROOTENV
), MAX_PATH
))
97 sep
= strrchr(library_path
, '\\');
101 if (_putenv(library_path
) < 0)
109 /** Make a file descriptor nonblock. Doesn't do any error checking */
110 void pa_make_nonblock_fd(int fd
) {
115 if ((v
= fcntl(fd
, F_GETFL
)) >= 0)
116 if (!(v
& O_NONBLOCK
))
117 fcntl(fd
, F_SETFL
, v
|O_NONBLOCK
);
118 #elif defined(OS_IS_WIN32)
120 if (ioctlsocket(fd
, FIONBIO
, &arg
) < 0) {
121 if (WSAGetLastError() == WSAENOTSOCK
)
122 pa_log_warn(__FILE__
": WARNING: Only sockets can be made non-blocking!\n");
125 pa_log_warn(__FILE__
": WARNING: Non-blocking I/O not supported.!\n");
129 /** Creates a directory securely */
130 int pa_make_secure_dir(const char* dir
) {
137 if (mkdir(dir
, 0700) < 0)
143 if (lstat(dir
, &st
) < 0)
145 if (stat(dir
, &st
) < 0)
150 if (!S_ISDIR(st
.st_mode
) || (st
.st_uid
!= getuid()) || ((st
.st_mode
& 0777) != 0700))
153 fprintf(stderr
, "FIXME: pa_make_secure_dir()\n");
163 /* Creates a the parent directory of the specified path securely */
164 int pa_make_secure_parent_dir(const char *fn
) {
166 char *slash
, *dir
= pa_xstrdup(fn
);
168 slash
= pa_path_get_filename(dir
);
173 if (pa_make_secure_dir(dir
) < 0)
184 /** Calls read() in a loop. Makes sure that as much as 'size' bytes,
185 * unless EOF is reached or an error occured */
186 ssize_t
pa_loop_read(int fd
, void*data
, size_t size
) {
188 assert(fd
>= 0 && data
&& size
);
193 if ((r
= read(fd
, data
, size
)) < 0)
200 data
= (uint8_t*) data
+ r
;
207 /** Similar to pa_loop_read(), but wraps write() */
208 ssize_t
pa_loop_write(int fd
, const void*data
, size_t size
) {
210 assert(fd
>= 0 && data
&& size
);
215 if ((r
= write(fd
, data
, size
)) < 0)
222 data
= (const uint8_t*) data
+ r
;
229 /* Print a warning messages in case that the given signal is not
230 * blocked or trapped */
231 void pa_check_signal_is_blocked(int sig
) {
232 #ifdef HAVE_SIGACTION
236 /* If POSIX threads are supported use thread-aware
237 * pthread_sigmask() function, to check if the signal is
238 * blocked. Otherwise fall back to sigprocmask() */
241 if (pthread_sigmask(SIG_SETMASK
, NULL
, &set
) < 0) {
243 if (sigprocmask(SIG_SETMASK
, NULL
, &set
) < 0) {
244 pa_log(__FILE__
": sigprocmask() failed: %s\n", strerror(errno
));
251 if (sigismember(&set
, sig
))
254 /* Check whether the signal is trapped */
256 if (sigaction(sig
, NULL
, &sa
) < 0) {
257 pa_log(__FILE__
": sigaction() failed: %s\n", strerror(errno
));
261 if (sa
.sa_handler
!= SIG_DFL
)
264 pa_log(__FILE__
": WARNING: %s is not trapped. This might cause malfunction!\n", pa_strsignal(sig
));
265 #else /* HAVE_SIGACTION */
266 pa_log(__FILE__
": WARNING: %s might not be trapped. This might cause malfunction!\n", pa_strsignal(sig
));
270 /* The following function is based on an example from the GNU libc
271 * documentation. This function is similar to GNU's asprintf(). */
272 char *pa_sprintf_malloc(const char *format
, ...) {
282 c
= pa_xrealloc(c
, size
);
284 va_start(ap
, format
);
285 r
= vsnprintf(c
, size
, format
, ap
);
288 if (r
> -1 && r
< size
)
291 if (r
> -1) /* glibc 2.1 */
298 /* Same as the previous function, but use a va_list instead of an
300 char *pa_vsprintf_malloc(const char *format
, va_list ap
) {
308 c
= pa_xrealloc(c
, size
);
309 r
= vsnprintf(c
, size
, format
, ap
);
311 if (r
> -1 && r
< size
)
314 if (r
> -1) /* glibc 2.1 */
321 /* Return the current username in the specified string buffer. */
322 char *pa_get_user_name(char *s
, size_t l
) {
327 struct passwd pw
, *r
;
332 if (!(p
= getenv("USER")) && !(p
= getenv("LOGNAME")) && !(p
= getenv("USERNAME"))) {
335 #ifdef HAVE_GETPWUID_R
336 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
338 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
339 * that do not support getpwuid_r. */
340 if ((r
= getpwuid(getuid())) == NULL
) {
342 snprintf(s
, l
, "%lu", (unsigned long) getuid());
348 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
349 DWORD size
= sizeof(buf
);
351 if (!GetUserName(buf
, &size
))
356 #else /* HAVE_PWD_H */
358 #endif /* HAVE_PWD_H */
361 return pa_strlcpy(s
, p
, l
);
364 /* Return the current hostname in the specified buffer. */
365 char *pa_get_host_name(char *s
, size_t l
) {
367 if (gethostname(s
, l
) < 0) {
368 pa_log(__FILE__
": gethostname(): %s\n", strerror(errno
));
375 /* Return the home directory of the current user */
376 char *pa_get_home_dir(char *s
, size_t l
) {
381 struct passwd pw
, *r
;
386 if ((e
= getenv("HOME")))
387 return pa_strlcpy(s
, e
, l
);
389 if ((e
= getenv("USERPROFILE")))
390 return pa_strlcpy(s
, e
, l
);
393 #ifdef HAVE_GETPWUID_R
394 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
395 pa_log(__FILE__
": getpwuid_r() failed\n");
397 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
398 * that do not support getpwuid_r. */
399 if ((r
= getpwuid(getuid())) == NULL
) {
400 pa_log(__FILE__
": getpwuid_r() failed\n");
405 return pa_strlcpy(s
, r
->pw_dir
, l
);
406 #else /* HAVE_PWD_H */
411 /* Similar to OpenBSD's strlcpy() function */
412 char *pa_strlcpy(char *b
, const char *s
, size_t l
) {
413 assert(b
&& s
&& l
> 0);
420 int pa_gettimeofday(struct timeval
*tv
) {
421 #ifdef HAVE_GETTIMEOFDAY
422 return gettimeofday(tv
, NULL
);
423 #elif defined(OS_IS_WIN32)
425 * Copied from implementation by Steven Edwards (LGPL).
426 * Found on wine mailing list.
429 #if defined(_MSC_VER) || defined(__BORLANDC__)
430 #define EPOCHFILETIME (116444736000000000i64)
432 #define EPOCHFILETIME (116444736000000000LL)
440 GetSystemTimeAsFileTime(&ft
);
441 li
.LowPart
= ft
.dwLowDateTime
;
442 li
.HighPart
= ft
.dwHighDateTime
;
443 t
= li
.QuadPart
; /* In 100-nanosecond intervals */
444 t
-= EPOCHFILETIME
; /* Offset to the Epoch time */
445 t
/= 10; /* In microseconds */
446 tv
->tv_sec
= (long)(t
/ 1000000);
447 tv
->tv_usec
= (long)(t
% 1000000);
452 #error "Platform lacks gettimeofday() or equivalent function."
456 /* Calculate the difference between the two specfified timeval
458 pa_usec_t
pa_timeval_diff(const struct timeval
*a
, const struct timeval
*b
) {
462 /* Check which whan is the earlier time and swap the two arguments if reuqired. */
463 if (pa_timeval_cmp(a
, b
) < 0) {
464 const struct timeval
*c
;
470 /* Calculate the second difference*/
471 r
= ((pa_usec_t
) a
->tv_sec
- b
->tv_sec
)* 1000000;
473 /* Calculate the microsecond difference */
474 if (a
->tv_usec
> b
->tv_usec
)
475 r
+= ((pa_usec_t
) a
->tv_usec
- b
->tv_usec
);
476 else if (a
->tv_usec
< b
->tv_usec
)
477 r
-= ((pa_usec_t
) b
->tv_usec
- a
->tv_usec
);
482 /* Compare the two timeval structs and return 0 when equal, negative when a < b, positive otherwse */
483 int pa_timeval_cmp(const struct timeval
*a
, const struct timeval
*b
) {
486 if (a
->tv_sec
< b
->tv_sec
)
489 if (a
->tv_sec
> b
->tv_sec
)
492 if (a
->tv_usec
< b
->tv_usec
)
495 if (a
->tv_usec
> b
->tv_usec
)
501 /* Return the time difference between now and the specified timestamp */
502 pa_usec_t
pa_timeval_age(const struct timeval
*tv
) {
505 pa_gettimeofday(&now
);
506 return pa_timeval_diff(&now
, tv
);
509 /* Add the specified time inmicroseconds to the specified timeval structure */
510 void pa_timeval_add(struct timeval
*tv
, pa_usec_t v
) {
515 tv
->tv_sec
+= (unsigned long) secs
;
521 while (tv
->tv_usec
>= 1000000) {
523 tv
->tv_usec
-= 1000000;
527 #define NICE_LEVEL (-15)
529 /* Raise the priority of the current process as much as possible and
530 sensible: set the nice level to -15 and enable realtime scheduling if
532 void pa_raise_priority(void) {
534 #ifdef HAVE_SYS_RESOURCE_H
535 if (setpriority(PRIO_PROCESS
, 0, NICE_LEVEL
) < 0)
536 pa_log_warn(__FILE__
": setpriority() failed: %s\n", strerror(errno
));
538 pa_log_info(__FILE__
": Successfully gained nice level %i.\n", NICE_LEVEL
);
541 #ifdef _POSIX_PRIORITY_SCHEDULING
543 struct sched_param sp
;
545 if (sched_getparam(0, &sp
) < 0) {
546 pa_log(__FILE__
": sched_getparam() failed: %s\n", strerror(errno
));
550 sp
.sched_priority
= 1;
551 if (sched_setscheduler(0, SCHED_FIFO
, &sp
) < 0) {
552 pa_log_warn(__FILE__
": sched_setscheduler() failed: %s\n", strerror(errno
));
556 pa_log_info(__FILE__
": Successfully enabled SCHED_FIFO scheduling.\n");
561 if (!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS
))
562 pa_log_warn(__FILE__
": SetPriorityClass() failed: 0x%08X\n", GetLastError());
564 pa_log_info(__FILE__
": Successfully gained high priority class.\n");
568 /* Reset the priority to normal, inverting the changes made by pa_raise_priority() */
569 void pa_reset_priority(void) {
571 SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS
);
574 #ifdef _POSIX_PRIORITY_SCHEDULING
576 struct sched_param sp
;
577 sched_getparam(0, &sp
);
578 sp
.sched_priority
= 0;
579 sched_setscheduler(0, SCHED_OTHER
, &sp
);
583 #ifdef HAVE_SYS_RESOURCE_H
584 setpriority(PRIO_PROCESS
, 0, 0);
588 /* Set the FD_CLOEXEC flag for a fd */
589 int pa_fd_set_cloexec(int fd
, int b
) {
595 if ((v
= fcntl(fd
, F_GETFD
, 0)) < 0)
598 v
= (v
& ~FD_CLOEXEC
) | (b
? FD_CLOEXEC
: 0);
600 if (fcntl(fd
, F_SETFD
, v
) < 0)
607 /* Return the binary file name of the current process. Works on Linux
608 * only. This shoul be used for eyecandy only, don't rely on return
610 char *pa_get_binary_name(char *s
, size_t l
) {
617 /* This works on Linux only */
619 snprintf(path
, sizeof(path
), "/proc/%u/exe", (unsigned) getpid());
620 if ((i
= readlink(path
, s
, l
-1)) < 0)
625 #elif defined(OS_IS_WIN32)
627 if (!GetModuleFileName(NULL
, path
, PATH_MAX
))
629 pa_strlcpy(s
, pa_path_get_filename(path
), l
);
636 /* Return a pointer to the filename inside a path (which is the last
638 char *pa_path_get_filename(const char *p
) {
641 if ((fn
= strrchr(p
, PATH_SEP
)))
647 /* Try to parse a boolean string value.*/
648 int pa_parse_boolean(const char *v
) {
650 if (!strcmp(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
652 else if (!strcmp(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
658 /* Split the specified string wherever one of the strings in delimiter
659 * occurs. Each time it is called returns a newly allocated string
660 * with pa_xmalloc(). The variable state points to, should be
661 * initiallized to NULL before the first call. */
662 char *pa_split(const char *c
, const char *delimiter
, const char**state
) {
663 const char *current
= *state
? *state
: c
;
669 l
= strcspn(current
, delimiter
);
675 return pa_xstrndup(current
, l
);
678 /* What is interpreted as whitespace? */
679 #define WHITESPACE " \t\n"
681 /* Split a string into words. Otherwise similar to pa_split(). */
682 char *pa_split_spaces(const char *c
, const char **state
) {
683 const char *current
= *state
? *state
: c
;
686 if (!*current
|| *c
== 0)
689 current
+= strspn(current
, WHITESPACE
);
690 l
= strcspn(current
, WHITESPACE
);
694 return pa_xstrndup(current
, l
);
697 /* Return the name of an UNIX signal. Similar to GNU's strsignal() */
698 const char *pa_strsignal(int sig
) {
700 case SIGINT
: return "SIGINT";
701 case SIGTERM
: return "SIGTERM";
703 case SIGUSR1
: return "SIGUSR1";
706 case SIGUSR2
: return "SIGUSR2";
709 case SIGXCPU
: return "SIGXCPU";
712 case SIGPIPE
: return "SIGPIPE";
715 case SIGCHLD
: return "SIGCHLD";
718 case SIGHUP
: return "SIGHUP";
720 default: return "UNKNOWN SIGNAL";
726 /* Check whether the specified GID and the group name match */
727 static int is_group(gid_t gid
, const char *name
) {
728 struct group group
, *result
= NULL
;
733 #ifdef HAVE_GETGRGID_R
734 #ifdef _SC_GETGR_R_SIZE_MAX
735 n
= sysconf(_SC_GETGR_R_SIZE_MAX
);
740 data
= pa_xmalloc(n
);
742 if (getgrgid_r(gid
, &group
, data
, n
, &result
) < 0 || !result
) {
743 pa_log(__FILE__
": getgrgid_r(%u) failed: %s\n", gid
, strerror(errno
));
748 r
= strcmp(name
, result
->gr_name
) == 0;
753 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X) that do not
754 * support getgrgid_r. */
755 if ((result
= getgrgid(gid
)) == NULL
) {
756 pa_log(__FILE__
": getgrgid(%u) failed: %s\n", gid
, strerror(errno
));
760 r
= strcmp(name
, result
->gr_name
) == 0;
768 /* Check the current user is member of the specified group */
769 int pa_uid_in_group(const char *name
, gid_t
*gid
) {
770 GETGROUPS_T
*gids
, tgid
;
771 int n
= sysconf(_SC_NGROUPS_MAX
);
776 gids
= pa_xmalloc(sizeof(GETGROUPS_T
)*n
);
778 if ((n
= getgroups(n
, gids
)) < 0) {
779 pa_log(__FILE__
": getgroups() failed: %s\n", strerror(errno
));
783 for (i
= 0; i
< n
; i
++) {
784 if (is_group(gids
[i
], name
) > 0) {
791 if (is_group(tgid
= getgid(), name
) > 0) {
805 #else /* HAVE_GRP_H */
807 int pa_uid_in_group(const char *name
, gid_t
*gid
) {
813 /* Lock or unlock a file entirely.
814 (advisory on UNIX, mandatory on Windows) */
815 int pa_lock_fd(int fd
, int b
) {
819 /* Try a R/W lock first */
821 flock
.l_type
= b
? F_WRLCK
: F_UNLCK
;
822 flock
.l_whence
= SEEK_SET
;
826 if (fcntl(fd
, F_SETLKW
, &flock
) >= 0)
829 /* Perhaps the file descriptor qas opened for read only, than try again with a read lock. */
830 if (b
&& errno
== EBADF
) {
831 flock
.l_type
= F_RDLCK
;
832 if (fcntl(fd
, F_SETLKW
, &flock
) >= 0)
836 pa_log(__FILE__
": %slock failed: %s\n", !b
? "un" : "", strerror(errno
));
840 HANDLE h
= (HANDLE
)_get_osfhandle(fd
);
842 if (b
&& LockFile(h
, 0, 0, 0xFFFFFFFF, 0xFFFFFFFF))
844 if (!b
&& UnlockFile(h
, 0, 0, 0xFFFFFFFF, 0xFFFFFFFF))
847 pa_log(__FILE__
": %slock failed: 0x%08X\n", !b
? "un" : "", GetLastError());
853 /* Remove trailing newlines from a string */
854 char* pa_strip_nl(char *s
) {
857 s
[strcspn(s
, "\r\n")] = 0;
861 /* Create a temporary lock file and lock it. */
862 int pa_lock_lockfile(const char *fn
) {
869 if ((fd
= open(fn
, O_CREAT
|O_RDWR
, S_IRUSR
|S_IWUSR
)) < 0) {
870 pa_log(__FILE__
": failed to create lock file '%s': %s\n", fn
, strerror(errno
));
874 if (pa_lock_fd(fd
, 1) < 0) {
875 pa_log(__FILE__
": failed to lock file '%s'.\n", fn
);
879 if (fstat(fd
, &st
) < 0) {
880 pa_log(__FILE__
": failed to fstat() file '%s'.\n", fn
);
884 /* Check wheter the file has been removed meanwhile. When yes, restart this loop, otherwise, we're done */
885 if (st
.st_nlink
>= 1)
888 if (pa_lock_fd(fd
, 0) < 0) {
889 pa_log(__FILE__
": failed to unlock file '%s'.\n", fn
);
894 pa_log(__FILE__
": failed to close file '%s'.\n", fn
);
911 /* Unlock a temporary lcok file */
912 int pa_unlock_lockfile(const char *fn
, int fd
) {
914 assert(fn
&& fd
>= 0);
916 if (unlink(fn
) < 0) {
917 pa_log_warn(__FILE__
": WARNING: unable to remove lock file '%s': %s\n", fn
, strerror(errno
));
921 if (pa_lock_fd(fd
, 0) < 0) {
922 pa_log_warn(__FILE__
": WARNING: failed to unlock file '%s'.\n", fn
);
927 pa_log_warn(__FILE__
": WARNING: failed to close lock file '%s': %s\n", fn
, strerror(errno
));
934 /* Try to open a configuration file. If "env" is specified, open the
935 * value of the specified environment variable. Otherwise look for a
936 * file "local" in the home directory or a file "global" in global
937 * file system. If "result" is non-NULL, a pointer to a newly
938 * allocated buffer containing the used configuration file is
940 FILE *pa_open_config_file(const char *global
, const char *local
, const char *env
, char **result
) {
947 if (!getenv(POLYP_ROOTENV
))
951 if (env
&& (fn
= getenv(env
))) {
953 if (!ExpandEnvironmentStrings(fn
, buf
, PATH_MAX
))
959 *result
= pa_xstrdup(fn
);
961 return fopen(fn
, "r");
964 if (local
&& pa_get_home_dir(h
, sizeof(h
))) {
968 fn
= lfn
= pa_sprintf_malloc("%s/%s", h
, local
);
971 if (!ExpandEnvironmentStrings(lfn
, buf
, PATH_MAX
))
978 if (f
|| errno
!= ENOENT
) {
980 *result
= pa_xstrdup(fn
);
996 if (!ExpandEnvironmentStrings(global
, buf
, PATH_MAX
))
1002 *result
= pa_xstrdup(global
);
1004 return fopen(global
, "r");
1007 /* Format the specified data as a hexademical string */
1008 char *pa_hexstr(const uint8_t* d
, size_t dlength
, char *s
, size_t slength
) {
1009 size_t i
= 0, j
= 0;
1010 const char hex
[] = "0123456789abcdef";
1011 assert(d
&& s
&& slength
> 0);
1013 while (i
< dlength
&& j
+3 <= slength
) {
1014 s
[j
++] = hex
[*d
>> 4];
1015 s
[j
++] = hex
[*d
& 0xF];
1021 s
[j
< slength
? j
: slength
] = 0;
1025 /* Convert a hexadecimal digit to a number or -1 if invalid */
1026 static int hexc(char c
) {
1027 if (c
>= '0' && c
<= '9')
1030 if (c
>= 'A' && c
<= 'F')
1031 return c
- 'A' + 10;
1033 if (c
>= 'a' && c
<= 'f')
1034 return c
- 'a' + 10;
1039 /* Parse a hexadecimal string as created by pa_hexstr() to a BLOB */
1040 size_t pa_parsehex(const char *p
, uint8_t *d
, size_t dlength
) {
1044 while (j
< dlength
&& *p
) {
1047 if ((b
= hexc(*(p
++))) < 0)
1050 d
[j
] = (uint8_t) (b
<< 4);
1055 if ((b
= hexc(*(p
++))) < 0)
1058 d
[j
] |= (uint8_t) b
;
1065 /* Return the fully qualified domain name in *s */
1066 char *pa_get_fqdn(char *s
, size_t l
) {
1068 #ifdef HAVE_GETADDRINFO
1069 struct addrinfo
*a
, hints
;
1072 if (!pa_get_host_name(hn
, sizeof(hn
)))
1075 #ifdef HAVE_GETADDRINFO
1076 memset(&hints
, 0, sizeof(hints
));
1077 hints
.ai_family
= AF_UNSPEC
;
1078 hints
.ai_flags
= AI_CANONNAME
;
1080 if (getaddrinfo(hn
, NULL
, &hints
, &a
) < 0 || !a
|| !a
->ai_canonname
|| !*a
->ai_canonname
)
1081 return pa_strlcpy(s
, hn
, l
);
1083 pa_strlcpy(s
, a
->ai_canonname
, l
);
1087 return pa_strlcpy(s
, hn
, l
);
1091 /* Returns nonzero when *s starts with *pfx */
1092 int pa_startswith(const char *s
, const char *pfx
) {
1097 return strlen(s
) >= l
&& strncmp(s
, pfx
, l
) == 0;
1100 /* if fn is null return the polypaudio run time path in s (/tmp/polypaudio)
1101 * if fn is non-null and starts with / return fn in s
1102 * otherwise append fn to the run time path and return it in s */
1103 char *pa_runtime_path(const char *fn
, char *s
, size_t l
) {
1107 if (fn
&& *fn
== '/')
1109 if (fn
&& strlen(fn
) >= 3 && isalpha(fn
[0]) && fn
[1] == ':' && fn
[2] == '\\')
1111 return pa_strlcpy(s
, fn
, l
);
1114 snprintf(s
, l
, "%s%s%c%s", PA_RUNTIME_PATH_PREFIX
, pa_get_user_name(u
, sizeof(u
)), PATH_SEP
, fn
);
1116 snprintf(s
, l
, "%s%s", PA_RUNTIME_PATH_PREFIX
, pa_get_user_name(u
, sizeof(u
)));
1122 ExpandEnvironmentStrings(buf
, s
, l
);
1129 /* Wait t milliseconds */
1130 int pa_msleep(unsigned long t
) {
1134 #elif defined(HAVE_NANOSLEEP)
1138 ts
.tv_nsec
= (t
% 1000) * 1000000;
1140 return nanosleep(&ts
, NULL
);
1142 #error "Platform lacks a sleep function."
1146 /* Convert the string s to a signed integer in *ret_i */
1147 int pa_atoi(const char *s
, int32_t *ret_i
) {
1152 l
= strtol(s
, &x
, 0);
1157 *ret_i
= (int32_t) l
;
1162 /* Convert the string s to an unsigned integer in *ret_u */
1163 int pa_atou(const char *s
, uint32_t *ret_u
) {
1168 l
= strtoul(s
, &x
, 0);
1173 *ret_u
= (uint32_t) l
;