summaryrefslogtreecommitdiff
path: root/AuFileName.c
diff options
context:
space:
mode:
Diffstat (limited to 'AuFileName.c')
-rw-r--r--AuFileName.c30
1 files changed, 22 insertions, 8 deletions
diff --git a/AuFileName.c b/AuFileName.c
index f384f75..0904273 100644
--- a/AuFileName.c
+++ b/AuFileName.c
@@ -31,26 +31,34 @@ in this Software without prior written authorization from The Open Group.
#include <X11/Xos.h>
#include <stdlib.h>
+static char *buf = NULL;
+
+static void
+free_filename_buffer(void)
+{
+ free(buf);
+ buf = NULL;
+}
+
char *
XauFileName (void)
{
const char *slashDotXauthority = "/.Xauthority";
char *name;
- static char *buf;
- static int bsize;
+ static size_t bsize;
+ static int atexit_registered = 0;
#ifdef WIN32
char dir[128];
#endif
- int size;
+ size_t size;
if ((name = getenv ("XAUTHORITY")))
return name;
name = getenv ("HOME");
if (!name) {
#ifdef WIN32
- (void) strcpy (dir, "/users/");
if ((name = getenv("USERNAME"))) {
- (void) strcat (dir, name);
+ snprintf(dir, sizeof(dir), "/users/%s", name);
name = dir;
}
if (!name)
@@ -61,12 +69,18 @@ XauFileName (void)
if (size > bsize) {
if (buf)
free (buf);
- buf = malloc ((unsigned) size);
+ buf = malloc (size);
if (!buf)
return NULL;
+
+ if (!atexit_registered) {
+ atexit(free_filename_buffer);
+ atexit_registered = 1;
+ }
+
bsize = size;
}
- strcpy (buf, name);
- strcat (buf, slashDotXauthority + (name[1] == '\0' ? 1 : 0));
+ snprintf (buf, bsize, "%s%s", name,
+ slashDotXauthority + (name[1] == '\0' ? 1 : 0));
return buf;
}