1/* 2 3Copyright 1988, 1998 The Open Group 4 5Permission to use, copy, modify, distribute, and sell this software and its 6documentation for any purpose is hereby granted without fee, provided that 7the above copyright notice appear in all copies and that both that 8copyright notice and this permission notice appear in supporting 9documentation. 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 21Except as contained in this notice, the name of The Open Group shall not be 22used in advertising or otherwise to promote the sale, use or other dealings 23in this Software without prior written authorization from The Open Group. 24 25*/ 26 27#ifdef HAVE_CONFIG_H 28#include <config.h> 29#endif 30#include <X11/Xauth.h> 31#include <X11/Xos.h> 32#include <assert.h> 33#include <stdlib.h> 34 35static char *buf = NULL; 36 37static void 38#ifdef __NetBSD__ 39__attribute__ ((__destructor__)) 40#endif 41free_filename_buffer(void) 42{ 43 free(buf); 44 buf = NULL; 45} 46 47char * 48XauFileName (void) 49{ 50 const char *slashDotXauthority = "/.Xauthority"; 51 char *name; 52 static size_t bsize; 53#ifndef __NetBSD__ 54 static int atexit_registered = 0; 55#endif 56#ifdef WIN32 57 char dir[128]; 58#endif 59 size_t size; 60 61 if ((name = getenv ("XAUTHORITY"))) 62 return name; 63 name = getenv ("HOME"); 64 if (!name) { 65#ifdef WIN32 66 if ((name = getenv("USERNAME"))) { 67 snprintf(dir, sizeof(dir), "/users/%s", name); 68 name = dir; 69 } 70 if (!name) 71#endif 72 return NULL; 73 } 74 size = strlen (name) + strlen(&slashDotXauthority[1]) + 2; 75 if ((size > bsize) || (buf == NULL)) { 76 free (buf); 77 assert(size > 0); 78 buf = malloc (size); 79 if (!buf) { 80 bsize = 0; 81 return NULL; 82 } 83 84#ifndef __NetBSD__ 85 if (!atexit_registered) { 86 atexit(free_filename_buffer); 87 atexit_registered = 1; 88 } 89#endif 90 91 bsize = size; 92 } 93 snprintf (buf, bsize, "%s%s", name, 94 slashDotXauthority + (name[0] == '/' && name[1] == '\0' ? 1 : 0)); 95 return buf; 96} 97