323
328
/** Calls read() in a loop. Makes sure that as much as 'size' bytes,
324
* unless EOF is reached or an error occured */
329
* unless EOF is reached or an error occurred */
325
330
ssize_t pa_loop_read(int fd, void*data, size_t size, int *type) {
739
744
/* And then we check language dependant */
740
745
if ((expr = nl_langinfo(YESEXPR)))
742
if ((r = match(expr, v)) > 0)
747
if ((r = pa_match(expr, v)) > 0)
745
750
if ((expr = nl_langinfo(NOEXPR)))
747
if ((r = match(expr, v)) > 0)
752
if ((r = pa_match(expr, v)) > 0)
1034
#ifdef _SC_GETGR_R_SIZE_MAX
1029
1035
g_n = sysconf(_SC_GETGR_R_SIZE_MAX);
1030
1042
g_buf = pa_xmalloc((size_t) g_n);
1044
#ifdef _SC_GETPW_R_SIZE_MAX
1032
1045
p_n = sysconf(_SC_GETPW_R_SIZE_MAX);
1033
1052
p_buf = pa_xmalloc((size_t) p_n);
1036
if (getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr) != 0 || !gr) {
1055
#ifdef HAVE_GETGRNAM_R
1056
if (getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr) != 0 || !gr)
1058
if (!(gr = getgrnam(name)))
1039
1062
errno = ENOENT;
1045
1067
for (i = gr->gr_mem; *i; i++) {
1046
1068
struct passwd pwbuf, *pw;
1070
#ifdef HAVE_GETPWNAM_R
1049
1071
if (getpwnam_r(*i, &pwbuf, p_buf, (size_t) p_n, &pw) != 0 || !pw)
1073
if (!(pw = getpwnam(*i)))
1052
1077
if (pw->pw_uid == uid) {
1070
1095
struct group grbuf, *gr;
1097
#ifdef _SC_GETGR_R_SIZE_MAX
1072
1098
g_n = sysconf(_SC_GETGR_R_SIZE_MAX);
1073
1105
g_buf = pa_xmalloc((size_t) g_n);
1076
if (getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr) != 0 || !gr) {
1108
#ifdef HAVE_GETGRNAM_R
1109
if (getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr) != 0 || !gr)
1111
if (!(gr = getgrnam(name)))
1079
1115
errno = ENOENT;
1210
/* Check wheter the file has been removed meanwhile. When yes,
1245
/* Check whether the file has been removed meanwhile. When yes,
1211
1246
* restart this loop, otherwise, we're done */
1212
1247
if (st.st_nlink >= 1)
2463
2503
/* If no hostname was set we use the POSIX hostid. It's usually
2464
* the IPv4 address. Mit not be that stable. */
2504
* the IPv4 address. Might not be that stable. */
2465
2505
return pa_sprintf_malloc("%08lx", (unsigned long) gethostid);
2508
char *pa_session_id(void) {
2511
if (!(e = getenv("XDG_SESSION_COOKIE")))
2514
return pa_utf8_filter(e);
2468
2517
char *pa_uname_string(void) {
2469
2518
struct utsname u;
2471
pa_assert_se(uname(&u) == 0);
2520
pa_assert_se(uname(&u) >= 0);
2473
2522
return pa_sprintf_malloc("%s %s %s %s", u.sysname, u.machine, u.release, u.version);
2540
unsigned pa_gcd(unsigned a, unsigned b) {
2551
void pa_reduce(unsigned *num, unsigned *den) {
2553
unsigned gcd = pa_gcd(*num, *den);
2561
pa_assert(pa_gcd(*num, *den) == 1);
2564
unsigned pa_ncpus(void) {
2567
#ifdef _SC_NPROCESSORS_CONF
2568
ncpus = sysconf(_SC_NPROCESSORS_CONF);
2573
return ncpus <= 0 ? 1 : (unsigned) ncpus;
2576
char *pa_replace(const char*s, const char*a, const char *b) {
2585
sb = pa_strbuf_new();
2590
if (!(p = strstr(s, a)))
2593
pa_strbuf_putsn(sb, s, p-s);
2594
pa_strbuf_puts(sb, b);
2598
pa_strbuf_puts(sb, s);
2600
return pa_strbuf_tostring_free(sb);
2603
char *pa_unescape(char *p) {
2605
pa_bool_t escaped = FALSE;
2607
for (s = p, d = p; *s; s++) {
2608
if (!escaped && *s == '\\') {
2622
char *pa_realpath(const char *path) {
2626
/* We want only abolsute paths */
2627
if (path[0] != '/') {
2632
#if defined(__GLIBC__) || defined(__APPLE__)
2636
if (!(r = realpath(path, NULL)))
2639
/* We copy this here in case our pa_xmalloc() is not
2640
* implemented on top of libc malloc() */
2644
#elif defined(PATH_MAX)
2647
path_buf = pa_xmalloc(PATH_MAX);
2649
if (!(t = realpath(path, path_buf))) {
2655
#error "It's not clear whether this system supports realpath(..., NULL) like GNU libc does. If it doesn't we need a private version of realpath() here."