$OpenBSD: patch-util_c,v 1.2 2004/06/26 04:38:33 brad Exp $
--- util.c.orig	Mon Mar  1 12:06:58 2004
+++ util.c	Thu Jun 24 20:15:35 2004
@@ -1,5 +1,8 @@
 #include "config.h"
 #include "esd.h"
+#include <limits.h>
+#include <pwd.h>
+#include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <stdlib.h>
@@ -23,38 +26,31 @@ have_ipv6() {
 const char*
 esd_get_socket_dirname (void) 
 {
-	char *audiodev;
-	static char *dirname = NULL;
+	static char *sockdir = NULL, sockdirbuf[PATH_MAX];
+	struct passwd *pw;
 
-	if (dirname == NULL) {
-		if (!(audiodev = getenv("AUDIODEV"))) {
-			audiodev = "";
-		} else {
-			char *newdev = strrchr(audiodev, '/');
-			if (newdev != NULL) {
-				audiodev = newdev++;
-			}
-		}
-		dirname = malloc(strlen(audiodev) + sizeof("/tmp/.esd"));
-		strcpy(dirname, "/tmp/.esd");
-		strcat(dirname, audiodev);
+	if (sockdir != NULL)
+		return (sockdir);
+	pw = getpwuid(getuid());
+	if (pw == NULL || pw->pw_dir == NULL) {
+		fprintf(stderr, "esd: could not find home directory\n");
+		exit(1);
 	}
-
-	return dirname;
+	snprintf(sockdirbuf, sizeof(sockdirbuf), "%s/.esd", pw->pw_dir);
+	endpwent();
+	sockdir = sockdirbuf;
+	return (sockdir);
 }
 
 const char*
 esd_get_socket_name (void) 
 {
-	const char *dirname;
-	static char *name = NULL;
+	static char *sockname = NULL, socknamebuf[PATH_MAX];
 
-	if (name == NULL) {
-		dirname = esd_get_socket_dirname();
-		name = malloc(strlen(dirname) + sizeof("/socket"));
-		strcpy(name, dirname);
-		strcat(name, "/socket");
-	}
-
-	return name;
+	if (sockname != NULL)
+		return (sockname);
+	snprintf(socknamebuf, sizeof(socknamebuf), "%s/socket",
+		esd_get_socket_dirname());
+	sockname = socknamebuf;
+	return (sockname);
 }
