]>
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>
73 #include <polyp/xmalloc.h>
75 #include <polypcore/log.h>
80 #define PA_RUNTIME_PATH_PREFIX "/tmp/polypaudio-"
83 #define PA_RUNTIME_PATH_PREFIX "%TEMP%\\polypaudio-"
89 #define POLYP_ROOTENV "POLYP_ROOT"
91 int pa_set_root(HANDLE handle
) {
92 char library_path
[MAX_PATH
+ sizeof(POLYP_ROOTENV
) + 1], *sep
;
94 strcpy(library_path
, POLYP_ROOTENV
"=");
96 if (!GetModuleFileName(handle
, library_path
+ sizeof(POLYP_ROOTENV
), MAX_PATH
))
99 sep
= strrchr(library_path
, '\\');
103 if (_putenv(library_path
) < 0)
111 /** Make a file descriptor nonblock. Doesn't do any error checking */
112 void pa_make_nonblock_fd(int fd
) {
117 if ((v
= fcntl(fd
, F_GETFL
)) >= 0)
118 if (!(v
& O_NONBLOCK
))
119 fcntl(fd
, F_SETFL
, v
|O_NONBLOCK
);
120 #elif defined(OS_IS_WIN32)
122 if (ioctlsocket(fd
, FIONBIO
, &arg
) < 0) {
123 if (WSAGetLastError() == WSAENOTSOCK
)
124 pa_log_warn(__FILE__
": WARNING: Only sockets can be made non-blocking!");
127 pa_log_warn(__FILE__
": WARNING: Non-blocking I/O not supported.!");
131 /** Creates a directory securely */
132 int pa_make_secure_dir(const char* dir
) {
139 if (mkdir(dir
, 0700) < 0)
145 chown(dir
, getuid(), getgid());
152 if (lstat(dir
, &st
) < 0)
154 if (stat(dir
, &st
) < 0)
159 if (!S_ISDIR(st
.st_mode
) || (st
.st_uid
!= getuid()) || ((st
.st_mode
& 0777) != 0700))
162 fprintf(stderr
, "FIXME: pa_make_secure_dir()\n");
172 /* Return a newly allocated sting containing the parent directory of the specified file */
173 char *pa_parent_dir(const char *fn
) {
174 char *slash
, *dir
= pa_xstrdup(fn
);
176 slash
= (char*) pa_path_get_filename(dir
);
184 /* Creates a the parent directory of the specified path securely */
185 int pa_make_secure_parent_dir(const char *fn
) {
189 if (!(dir
= pa_parent_dir(fn
)))
192 if (pa_make_secure_dir(dir
) < 0)
202 /** Platform independent read function. Necessary since not all systems
203 * treat all file descriptors equal. */
204 ssize_t
pa_read(int fd
, void *buf
, size_t count
) {
208 r
= recv(fd
, buf
, count
, 0);
210 if (WSAGetLastError() != WSAENOTSOCK
) {
211 errno
= WSAGetLastError();
218 r
= read(fd
, buf
, count
);
223 /** Similar to pa_read(), but handles writes */
224 ssize_t
pa_write(int fd
, const void *buf
, size_t count
) {
228 r
= send(fd
, buf
, count
, 0);
230 if (WSAGetLastError() != WSAENOTSOCK
) {
231 errno
= WSAGetLastError();
238 r
= write(fd
, buf
, count
);
243 /** Calls read() in a loop. Makes sure that as much as 'size' bytes,
244 * unless EOF is reached or an error occured */
245 ssize_t
pa_loop_read(int fd
, void*data
, size_t size
) {
247 assert(fd
>= 0 && data
&& size
);
252 if ((r
= pa_read(fd
, data
, size
)) < 0)
259 data
= (uint8_t*) data
+ r
;
266 /** Similar to pa_loop_read(), but wraps write() */
267 ssize_t
pa_loop_write(int fd
, const void*data
, size_t size
) {
269 assert(fd
>= 0 && data
&& size
);
274 if ((r
= pa_write(fd
, data
, size
)) < 0)
281 data
= (const uint8_t*) data
+ r
;
288 /* Print a warning messages in case that the given signal is not
289 * blocked or trapped */
290 void pa_check_signal_is_blocked(int sig
) {
291 #ifdef HAVE_SIGACTION
295 /* If POSIX threads are supported use thread-aware
296 * pthread_sigmask() function, to check if the signal is
297 * blocked. Otherwise fall back to sigprocmask() */
300 if (pthread_sigmask(SIG_SETMASK
, NULL
, &set
) < 0) {
302 if (sigprocmask(SIG_SETMASK
, NULL
, &set
) < 0) {
303 pa_log(__FILE__
": sigprocmask() failed: %s", strerror(errno
));
310 if (sigismember(&set
, sig
))
313 /* Check whether the signal is trapped */
315 if (sigaction(sig
, NULL
, &sa
) < 0) {
316 pa_log(__FILE__
": sigaction() failed: %s", strerror(errno
));
320 if (sa
.sa_handler
!= SIG_DFL
)
323 pa_log(__FILE__
": WARNING: %s is not trapped. This might cause malfunction!", pa_strsignal(sig
));
324 #else /* HAVE_SIGACTION */
325 pa_log(__FILE__
": WARNING: %s might not be trapped. This might cause malfunction!", pa_strsignal(sig
));
329 /* The following function is based on an example from the GNU libc
330 * documentation. This function is similar to GNU's asprintf(). */
331 char *pa_sprintf_malloc(const char *format
, ...) {
341 c
= pa_xrealloc(c
, size
);
343 va_start(ap
, format
);
344 r
= vsnprintf(c
, size
, format
, ap
);
347 if (r
> -1 && r
< size
)
350 if (r
> -1) /* glibc 2.1 */
357 /* Same as the previous function, but use a va_list instead of an
359 char *pa_vsprintf_malloc(const char *format
, va_list ap
) {
367 c
= pa_xrealloc(c
, size
);
368 r
= vsnprintf(c
, size
, format
, ap
);
370 if (r
> -1 && r
< size
)
373 if (r
> -1) /* glibc 2.1 */
380 /* Return the current username in the specified string buffer. */
381 char *pa_get_user_name(char *s
, size_t l
) {
386 struct passwd pw
, *r
;
391 if (!(p
= getenv("USER")) && !(p
= getenv("LOGNAME")) && !(p
= getenv("USERNAME"))) {
394 #ifdef HAVE_GETPWUID_R
395 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
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
) {
401 snprintf(s
, l
, "%lu", (unsigned long) getuid());
407 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
408 DWORD size
= sizeof(buf
);
410 if (!GetUserName(buf
, &size
))
415 #else /* HAVE_PWD_H */
417 #endif /* HAVE_PWD_H */
420 return pa_strlcpy(s
, p
, l
);
423 /* Return the current hostname in the specified buffer. */
424 char *pa_get_host_name(char *s
, size_t l
) {
426 if (gethostname(s
, l
) < 0) {
427 pa_log(__FILE__
": gethostname(): %s", strerror(errno
));
434 /* Return the home directory of the current user */
435 char *pa_get_home_dir(char *s
, size_t l
) {
440 struct passwd pw
, *r
;
445 if ((e
= getenv("HOME")))
446 return pa_strlcpy(s
, e
, l
);
448 if ((e
= getenv("USERPROFILE")))
449 return pa_strlcpy(s
, e
, l
);
452 #ifdef HAVE_GETPWUID_R
453 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
454 pa_log(__FILE__
": getpwuid_r() failed");
456 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
457 * that do not support getpwuid_r. */
458 if ((r
= getpwuid(getuid())) == NULL
) {
459 pa_log(__FILE__
": getpwuid_r() failed");
464 return pa_strlcpy(s
, r
->pw_dir
, l
);
465 #else /* HAVE_PWD_H */
470 /* Similar to OpenBSD's strlcpy() function */
471 char *pa_strlcpy(char *b
, const char *s
, size_t l
) {
472 assert(b
&& s
&& l
> 0);
479 struct timeval
*pa_gettimeofday(struct timeval
*tv
) {
480 #ifdef HAVE_GETTIMEOFDAY
483 return gettimeofday(tv
, NULL
) < 0 ? NULL
: tv
;
484 #elif defined(OS_IS_WIN32)
486 * Copied from implementation by Steven Edwards (LGPL).
487 * Found on wine mailing list.
490 #if defined(_MSC_VER) || defined(__BORLANDC__)
491 #define EPOCHFILETIME (116444736000000000i64)
493 #define EPOCHFILETIME (116444736000000000LL)
502 GetSystemTimeAsFileTime(&ft
);
503 li
.LowPart
= ft
.dwLowDateTime
;
504 li
.HighPart
= ft
.dwHighDateTime
;
505 t
= li
.QuadPart
; /* In 100-nanosecond intervals */
506 t
-= EPOCHFILETIME
; /* Offset to the Epoch time */
507 t
/= 10; /* In microseconds */
508 tv
->tv_sec
= (long)(t
/ 1000000);
509 tv
->tv_usec
= (long)(t
% 1000000);
513 #error "Platform lacks gettimeofday() or equivalent function."
517 /* Calculate the difference between the two specfified timeval
519 pa_usec_t
pa_timeval_diff(const struct timeval
*a
, const struct timeval
*b
) {
523 /* Check which whan is the earlier time and swap the two arguments if reuqired. */
524 if (pa_timeval_cmp(a
, b
) < 0) {
525 const struct timeval
*c
;
531 /* Calculate the second difference*/
532 r
= ((pa_usec_t
) a
->tv_sec
- b
->tv_sec
)* 1000000;
534 /* Calculate the microsecond difference */
535 if (a
->tv_usec
> b
->tv_usec
)
536 r
+= ((pa_usec_t
) a
->tv_usec
- b
->tv_usec
);
537 else if (a
->tv_usec
< b
->tv_usec
)
538 r
-= ((pa_usec_t
) b
->tv_usec
- a
->tv_usec
);
543 /* Compare the two timeval structs and return 0 when equal, negative when a < b, positive otherwse */
544 int pa_timeval_cmp(const struct timeval
*a
, const struct timeval
*b
) {
547 if (a
->tv_sec
< b
->tv_sec
)
550 if (a
->tv_sec
> b
->tv_sec
)
553 if (a
->tv_usec
< b
->tv_usec
)
556 if (a
->tv_usec
> b
->tv_usec
)
562 /* Return the time difference between now and the specified timestamp */
563 pa_usec_t
pa_timeval_age(const struct timeval
*tv
) {
567 return pa_timeval_diff(pa_gettimeofday(&now
), tv
);
570 /* Add the specified time inmicroseconds to the specified timeval structure */
571 void pa_timeval_add(struct timeval
*tv
, pa_usec_t v
) {
576 tv
->tv_sec
+= (unsigned long) secs
;
582 while (tv
->tv_usec
>= 1000000) {
584 tv
->tv_usec
-= 1000000;
588 #define NICE_LEVEL (-15)
590 /* Raise the priority of the current process as much as possible and
591 sensible: set the nice level to -15 and enable realtime scheduling if
593 void pa_raise_priority(void) {
595 #ifdef HAVE_SYS_RESOURCE_H
596 if (setpriority(PRIO_PROCESS
, 0, NICE_LEVEL
) < 0)
597 pa_log_warn(__FILE__
": setpriority() failed: %s", strerror(errno
));
599 pa_log_info(__FILE__
": Successfully gained nice level %i.", NICE_LEVEL
);
602 #ifdef _POSIX_PRIORITY_SCHEDULING
604 struct sched_param sp
;
606 if (sched_getparam(0, &sp
) < 0) {
607 pa_log(__FILE__
": sched_getparam() failed: %s", strerror(errno
));
611 sp
.sched_priority
= 1;
612 if (sched_setscheduler(0, SCHED_FIFO
, &sp
) < 0) {
613 pa_log_warn(__FILE__
": sched_setscheduler() failed: %s", strerror(errno
));
617 pa_log_info(__FILE__
": Successfully enabled SCHED_FIFO scheduling.");
622 if (!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS
))
623 pa_log_warn(__FILE__
": SetPriorityClass() failed: 0x%08X", GetLastError());
625 pa_log_info(__FILE__
": Successfully gained high priority class.");
629 /* Reset the priority to normal, inverting the changes made by pa_raise_priority() */
630 void pa_reset_priority(void) {
632 SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS
);
635 #ifdef _POSIX_PRIORITY_SCHEDULING
637 struct sched_param sp
;
638 sched_getparam(0, &sp
);
639 sp
.sched_priority
= 0;
640 sched_setscheduler(0, SCHED_OTHER
, &sp
);
644 #ifdef HAVE_SYS_RESOURCE_H
645 setpriority(PRIO_PROCESS
, 0, 0);
649 /* Set the FD_CLOEXEC flag for a fd */
650 int pa_fd_set_cloexec(int fd
, int b
) {
656 if ((v
= fcntl(fd
, F_GETFD
, 0)) < 0)
659 v
= (v
& ~FD_CLOEXEC
) | (b
? FD_CLOEXEC
: 0);
661 if (fcntl(fd
, F_SETFD
, v
) < 0)
668 /* Return the binary file name of the current process. Works on Linux
669 * only. This shoul be used for eyecandy only, don't rely on return
671 char *pa_get_binary_name(char *s
, size_t l
) {
678 /* This works on Linux only */
680 snprintf(path
, sizeof(path
), "/proc/%u/exe", (unsigned) getpid());
681 if ((i
= readlink(path
, s
, l
-1)) < 0)
686 #elif defined(OS_IS_WIN32)
688 if (!GetModuleFileName(NULL
, path
, PATH_MAX
))
690 pa_strlcpy(s
, pa_path_get_filename(path
), l
);
697 /* Return a pointer to the filename inside a path (which is the last
699 const char *pa_path_get_filename(const char *p
) {
702 if ((fn
= strrchr(p
, PATH_SEP
)))
705 return (const char*) p
;
708 /* Try to parse a boolean string value.*/
709 int pa_parse_boolean(const char *v
) {
711 if (!strcmp(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
713 else if (!strcmp(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
719 /* Split the specified string wherever one of the strings in delimiter
720 * occurs. Each time it is called returns a newly allocated string
721 * with pa_xmalloc(). The variable state points to, should be
722 * initiallized to NULL before the first call. */
723 char *pa_split(const char *c
, const char *delimiter
, const char**state
) {
724 const char *current
= *state
? *state
: c
;
730 l
= strcspn(current
, delimiter
);
736 return pa_xstrndup(current
, l
);
739 /* What is interpreted as whitespace? */
740 #define WHITESPACE " \t\n"
742 /* Split a string into words. Otherwise similar to pa_split(). */
743 char *pa_split_spaces(const char *c
, const char **state
) {
744 const char *current
= *state
? *state
: c
;
747 if (!*current
|| *c
== 0)
750 current
+= strspn(current
, WHITESPACE
);
751 l
= strcspn(current
, WHITESPACE
);
755 return pa_xstrndup(current
, l
);
758 /* Return the name of an UNIX signal. Similar to GNU's strsignal() */
759 const char *pa_strsignal(int sig
) {
761 case SIGINT
: return "SIGINT";
762 case SIGTERM
: return "SIGTERM";
764 case SIGUSR1
: return "SIGUSR1";
767 case SIGUSR2
: return "SIGUSR2";
770 case SIGXCPU
: return "SIGXCPU";
773 case SIGPIPE
: return "SIGPIPE";
776 case SIGCHLD
: return "SIGCHLD";
779 case SIGHUP
: return "SIGHUP";
781 default: return "UNKNOWN SIGNAL";
787 /* Check whether the specified GID and the group name match */
788 static int is_group(gid_t gid
, const char *name
) {
789 struct group group
, *result
= NULL
;
794 #ifdef HAVE_GETGRGID_R
795 #ifdef _SC_GETGR_R_SIZE_MAX
796 n
= sysconf(_SC_GETGR_R_SIZE_MAX
);
801 data
= pa_xmalloc(n
);
803 if (getgrgid_r(gid
, &group
, data
, n
, &result
) < 0 || !result
) {
804 pa_log(__FILE__
": getgrgid_r(%u) failed: %s", gid
, strerror(errno
));
808 r
= strcmp(name
, result
->gr_name
) == 0;
813 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X) that do not
814 * support getgrgid_r. */
815 if ((result
= getgrgid(gid
)) == NULL
) {
816 pa_log(__FILE__
": getgrgid(%u) failed: %s", gid
, strerror(errno
));
820 r
= strcmp(name
, result
->gr_name
) == 0;
828 /* Check the current user is member of the specified group */
829 int pa_own_uid_in_group(const char *name
, gid_t
*gid
) {
830 GETGROUPS_T
*gids
, tgid
;
831 int n
= sysconf(_SC_NGROUPS_MAX
);
836 gids
= pa_xmalloc(sizeof(GETGROUPS_T
)*n
);
838 if ((n
= getgroups(n
, gids
)) < 0) {
839 pa_log(__FILE__
": getgroups() failed: %s", strerror(errno
));
843 for (i
= 0; i
< n
; i
++) {
844 if (is_group(gids
[i
], name
) > 0) {
851 if (is_group(tgid
= getgid(), name
) > 0) {
865 int pa_uid_in_group(uid_t uid
, const char *name
) {
868 struct group grbuf
, *gr
;
872 g_n
= sysconf(_SC_GETGR_R_SIZE_MAX
);
873 g_buf
= pa_xmalloc(g_n
);
875 p_n
= sysconf(_SC_GETPW_R_SIZE_MAX
);
876 p_buf
= pa_xmalloc(p_n
);
878 if (getgrnam_r(name
, &grbuf
, g_buf
, (size_t) g_n
, &gr
) != 0 || !gr
)
882 for (i
= gr
->gr_mem
; *i
; i
++) {
883 struct passwd pwbuf
, *pw
;
885 if (getpwnam_r(*i
, &pwbuf
, p_buf
, (size_t) p_n
, &pw
) != 0 || !pw
)
888 if (pw
->pw_uid
== uid
) {
901 #else /* HAVE_GRP_H */
903 int pa_own_uid_in_group(const char *name
, gid_t
*gid
) {
908 int pa_uid_in_group(uid_t uid
, const char *name
) {
914 /* Lock or unlock a file entirely.
915 (advisory on UNIX, mandatory on Windows) */
916 int pa_lock_fd(int fd
, int b
) {
920 /* Try a R/W lock first */
922 flock
.l_type
= b
? F_WRLCK
: F_UNLCK
;
923 flock
.l_whence
= SEEK_SET
;
927 if (fcntl(fd
, F_SETLKW
, &flock
) >= 0)
930 /* Perhaps the file descriptor qas opened for read only, than try again with a read lock. */
931 if (b
&& errno
== EBADF
) {
932 flock
.l_type
= F_RDLCK
;
933 if (fcntl(fd
, F_SETLKW
, &flock
) >= 0)
937 pa_log(__FILE__
": %slock failed: %s", !b
? "un" : "", strerror(errno
));
941 HANDLE h
= (HANDLE
)_get_osfhandle(fd
);
943 if (b
&& LockFile(h
, 0, 0, 0xFFFFFFFF, 0xFFFFFFFF))
945 if (!b
&& UnlockFile(h
, 0, 0, 0xFFFFFFFF, 0xFFFFFFFF))
948 pa_log(__FILE__
": %slock failed: 0x%08X", !b
? "un" : "", GetLastError());
954 /* Remove trailing newlines from a string */
955 char* pa_strip_nl(char *s
) {
958 s
[strcspn(s
, "\r\n")] = 0;
962 /* Create a temporary lock file and lock it. */
963 int pa_lock_lockfile(const char *fn
) {
970 if ((fd
= open(fn
, O_CREAT
|O_RDWR
, S_IRUSR
|S_IWUSR
)) < 0) {
971 pa_log(__FILE__
": failed to create lock file '%s': %s", fn
, strerror(errno
));
975 if (pa_lock_fd(fd
, 1) < 0) {
976 pa_log(__FILE__
": failed to lock file '%s'.", fn
);
980 if (fstat(fd
, &st
) < 0) {
981 pa_log(__FILE__
": failed to fstat() file '%s'.", fn
);
985 /* Check wheter the file has been removed meanwhile. When yes, restart this loop, otherwise, we're done */
986 if (st
.st_nlink
>= 1)
989 if (pa_lock_fd(fd
, 0) < 0) {
990 pa_log(__FILE__
": failed to unlock file '%s'.", fn
);
995 pa_log(__FILE__
": failed to close file '%s'.", fn
);
1012 /* Unlock a temporary lcok file */
1013 int pa_unlock_lockfile(const char *fn
, int fd
) {
1015 assert(fn
&& fd
>= 0);
1017 if (unlink(fn
) < 0) {
1018 pa_log_warn(__FILE__
": WARNING: unable to remove lock file '%s': %s", fn
, strerror(errno
));
1022 if (pa_lock_fd(fd
, 0) < 0) {
1023 pa_log_warn(__FILE__
": WARNING: failed to unlock file '%s'.", fn
);
1027 if (close(fd
) < 0) {
1028 pa_log_warn(__FILE__
": WARNING: failed to close lock file '%s': %s", fn
, strerror(errno
));
1035 /* Try to open a configuration file. If "env" is specified, open the
1036 * value of the specified environment variable. Otherwise look for a
1037 * file "local" in the home directory or a file "global" in global
1038 * file system. If "result" is non-NULL, a pointer to a newly
1039 * allocated buffer containing the used configuration file is
1041 FILE *pa_open_config_file(const char *global
, const char *local
, const char *env
, char **result
, const char *mode
) {
1048 if (!getenv(POLYP_ROOTENV
))
1052 if (env
&& (fn
= getenv(env
))) {
1054 if (!ExpandEnvironmentStrings(fn
, buf
, PATH_MAX
))
1060 *result
= pa_xstrdup(fn
);
1062 return fopen(fn
, mode
);
1065 if (local
&& pa_get_home_dir(h
, sizeof(h
))) {
1069 fn
= lfn
= pa_sprintf_malloc("%s/%s", h
, local
);
1072 if (!ExpandEnvironmentStrings(lfn
, buf
, PATH_MAX
))
1077 f
= fopen(fn
, mode
);
1079 if (f
|| errno
!= ENOENT
) {
1081 *result
= pa_xstrdup(fn
);
1097 if (!ExpandEnvironmentStrings(global
, buf
, PATH_MAX
))
1103 *result
= pa_xstrdup(global
);
1105 return fopen(global
, mode
);
1108 /* Format the specified data as a hexademical string */
1109 char *pa_hexstr(const uint8_t* d
, size_t dlength
, char *s
, size_t slength
) {
1110 size_t i
= 0, j
= 0;
1111 const char hex
[] = "0123456789abcdef";
1112 assert(d
&& s
&& slength
> 0);
1114 while (i
< dlength
&& j
+3 <= slength
) {
1115 s
[j
++] = hex
[*d
>> 4];
1116 s
[j
++] = hex
[*d
& 0xF];
1122 s
[j
< slength
? j
: slength
] = 0;
1126 /* Convert a hexadecimal digit to a number or -1 if invalid */
1127 static int hexc(char c
) {
1128 if (c
>= '0' && c
<= '9')
1131 if (c
>= 'A' && c
<= 'F')
1132 return c
- 'A' + 10;
1134 if (c
>= 'a' && c
<= 'f')
1135 return c
- 'a' + 10;
1140 /* Parse a hexadecimal string as created by pa_hexstr() to a BLOB */
1141 size_t pa_parsehex(const char *p
, uint8_t *d
, size_t dlength
) {
1145 while (j
< dlength
&& *p
) {
1148 if ((b
= hexc(*(p
++))) < 0)
1151 d
[j
] = (uint8_t) (b
<< 4);
1156 if ((b
= hexc(*(p
++))) < 0)
1159 d
[j
] |= (uint8_t) b
;
1166 /* Return the fully qualified domain name in *s */
1167 char *pa_get_fqdn(char *s
, size_t l
) {
1169 #ifdef HAVE_GETADDRINFO
1170 struct addrinfo
*a
, hints
;
1173 if (!pa_get_host_name(hn
, sizeof(hn
)))
1176 #ifdef HAVE_GETADDRINFO
1177 memset(&hints
, 0, sizeof(hints
));
1178 hints
.ai_family
= AF_UNSPEC
;
1179 hints
.ai_flags
= AI_CANONNAME
;
1181 if (getaddrinfo(hn
, NULL
, &hints
, &a
) < 0 || !a
|| !a
->ai_canonname
|| !*a
->ai_canonname
)
1182 return pa_strlcpy(s
, hn
, l
);
1184 pa_strlcpy(s
, a
->ai_canonname
, l
);
1188 return pa_strlcpy(s
, hn
, l
);
1192 /* Returns nonzero when *s starts with *pfx */
1193 int pa_startswith(const char *s
, const char *pfx
) {
1201 return strlen(s
) >= l
&& strncmp(s
, pfx
, l
) == 0;
1204 /* Returns nonzero when *s ends with *sfx */
1205 int pa_endswith(const char *s
, const char *sfx
) {
1214 return l1
>= l2
&& strcmp(s
+l1
-l2
, sfx
) == 0;
1217 /* if fn is null return the polypaudio run time path in s (/tmp/polypaudio)
1218 * if fn is non-null and starts with / return fn in s
1219 * otherwise append fn to the run time path and return it in s */
1220 char *pa_runtime_path(const char *fn
, char *s
, size_t l
) {
1224 if (fn
&& *fn
== '/')
1226 if (fn
&& strlen(fn
) >= 3 && isalpha(fn
[0]) && fn
[1] == ':' && fn
[2] == '\\')
1228 return pa_strlcpy(s
, fn
, l
);
1231 snprintf(s
, l
, "%s%s%c%s", PA_RUNTIME_PATH_PREFIX
, pa_get_user_name(u
, sizeof(u
)), PATH_SEP
, fn
);
1233 snprintf(s
, l
, "%s%s", PA_RUNTIME_PATH_PREFIX
, pa_get_user_name(u
, sizeof(u
)));
1239 ExpandEnvironmentStrings(buf
, s
, l
);
1246 /* Wait t milliseconds */
1247 int pa_msleep(unsigned long t
) {
1251 #elif defined(HAVE_NANOSLEEP)
1255 ts
.tv_nsec
= (t
% 1000) * 1000000;
1257 return nanosleep(&ts
, NULL
);
1259 #error "Platform lacks a sleep function."
1263 /* Convert the string s to a signed integer in *ret_i */
1264 int pa_atoi(const char *s
, int32_t *ret_i
) {
1269 l
= strtol(s
, &x
, 0);
1274 *ret_i
= (int32_t) l
;
1279 /* Convert the string s to an unsigned integer in *ret_u */
1280 int pa_atou(const char *s
, uint32_t *ret_u
) {
1285 l
= strtoul(s
, &x
, 0);
1290 *ret_u
= (uint32_t) l
;