]> code.delx.au - pulseaudio/blobdiff - src/pulse/util.c
Use <pulsecore/socket.h> instead of <sys/socket.h>
[pulseaudio] / src / pulse / util.c
index b20ea46ac98a821c56c547f3df2e7125b46b770e..3206e94c6ff4da3e6511c1c84f1c21f9737af384 100644 (file)
 #include <pwd.h>
 #endif
 
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-
 #ifdef HAVE_NETDB_H
 #include <netdb.h>
 #endif
 #endif
 
 #include <pulse/xmalloc.h>
-#include <pulsecore/winsock.h>
+#include <pulse/timeval.h>
+
+#include <pulsecore/socket.h>
 #include <pulsecore/core-error.h>
 #include <pulsecore/log.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/usergroup.h>
 
 #include "util.h"
 
 char *pa_get_user_name(char *s, size_t l) {
     const char *p;
+    char *name = NULL;
+#ifdef OS_IS_WIN32
     char buf[1024];
+#endif
 
 #ifdef HAVE_PWD_H
-    struct passwd pw, *r;
+    struct passwd *r;
 #endif
 
     pa_assert(s);
     pa_assert(l > 0);
 
-    if (!(p = (getuid() == 0 ? "root" : NULL)) &&
-        !(p = getenv("USER")) &&
-        !(p = getenv("LOGNAME")) &&
-        !(p = getenv("USERNAME"))) {
+    if ((p = (getuid() == 0 ? "root" : NULL)) ||
+        (p = getenv("USER")) ||
+        (p = getenv("LOGNAME")) ||
+        (p = getenv("USERNAME")))
+    {
+        name = pa_strlcpy(s, p, l);
+    } else {
 #ifdef HAVE_PWD_H
 
-#ifdef HAVE_GETPWUID_R
-        if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
-#else
-        /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
-            * that do not support getpwuid_r. */
-        if ((r = getpwuid(getuid())) == NULL) {
-#endif
+        if ((r = pa_getpwuid_malloc(getuid())) == NULL) {
             pa_snprintf(s, l, "%lu", (unsigned long) getuid());
             return s;
         }
 
-        p = r->pw_name;
+        name = pa_strlcpy(s, r->pw_name, l);
+        pa_getpwuid_free(r);
 
 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
         DWORD size = sizeof(buf);
@@ -100,7 +100,7 @@ char *pa_get_user_name(char *s, size_t l) {
             return NULL;
         }
 
-        p = buf;
+        name = pa_strlcpy(s, buf, l);
 
 #else /* HAVE_PWD_H */
 
@@ -108,7 +108,7 @@ char *pa_get_user_name(char *s, size_t l) {
 #endif /* HAVE_PWD_H */
     }
 
-    return pa_strlcpy(s, p, l);
+    return name;
 }
 
 char *pa_get_host_name(char *s, size_t l) {
@@ -124,11 +124,10 @@ char *pa_get_host_name(char *s, size_t l) {
 }
 
 char *pa_get_home_dir(char *s, size_t l) {
-    char *e;
+    char *e, *dir;
 
 #ifdef HAVE_PWD_H
-    char buf[1024];
-    struct passwd pw, *r;
+    struct passwd *r;
 #endif
 
     pa_assert(s);
@@ -141,22 +140,19 @@ char *pa_get_home_dir(char *s, size_t l) {
         return pa_strlcpy(s, e, l);
 
 #ifdef HAVE_PWD_H
-
     errno = 0;
-#ifdef HAVE_GETPWUID_R
-    if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
-#else
-    /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
-        * that do not support getpwuid_r. */
-    if ((r = getpwuid(getuid())) == NULL) {
-#endif
+    if ((r = pa_getpwuid_malloc(getuid())) == NULL) {
         if (!errno)
             errno = ENOENT;
 
         return NULL;
     }
 
-    return pa_strlcpy(s, r->pw_dir, l);
+    dir = pa_strlcpy(s, r->pw_dir, l);
+
+    pa_getpwuid_free(r);
+
+    return dir;
 #else /* HAVE_PWD_H */
 
     errno = ENOENT;
@@ -189,7 +185,18 @@ char *pa_get_binary_name(char *s, size_t l) {
             return s;
         }
     }
+#endif
+
+#ifdef __FreeBSD__
+    {
+        char *rp;
 
+        if ((rp = pa_readlink("/proc/curproc/file"))) {
+            pa_strlcpy(s, pa_path_get_filename(rp), l);
+            pa_xfree(rp);
+            return s;
+        }
+    }
 #endif
 
 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
@@ -217,7 +224,8 @@ char *pa_get_binary_name(char *s, size_t l) {
 char *pa_path_get_filename(const char *p) {
     char *fn;
 
-    pa_assert(p);
+    if (!p)
+        return NULL;
 
     if ((fn = strrchr(p, PA_PATH_SEP_CHAR)))
         return fn+1;
@@ -260,8 +268,8 @@ int pa_msleep(unsigned long t) {
 #elif defined(HAVE_NANOSLEEP)
     struct timespec ts;
 
-    ts.tv_sec = (time_t) (t/1000UL);
-    ts.tv_nsec = (long) ((t % 1000UL) * 1000000UL);
+    ts.tv_sec = (time_t) (t / PA_MSEC_PER_SEC);
+    ts.tv_nsec = (long) ((t % PA_MSEC_PER_SEC) * PA_NSEC_PER_MSEC);
 
     return nanosleep(&ts, NULL);
 #else