127702724Smrg/* 227702724Smrg 327702724SmrgCopyright 1988, 1998 The Open Group 427702724Smrg 527702724SmrgPermission to use, copy, modify, distribute, and sell this software and its 627702724Smrgdocumentation for any purpose is hereby granted without fee, provided that 727702724Smrgthe above copyright notice appear in all copies and that both that 827702724Smrgcopyright notice and this permission notice appear in supporting 927702724Smrgdocumentation. 1027702724Smrg 1127702724SmrgThe above copyright notice and this permission notice shall be included in 1227702724Smrgall copies or substantial portions of the Software. 1327702724Smrg 1427702724SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1527702724SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1627702724SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1727702724SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 1827702724SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 1927702724SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2027702724Smrg 2127702724SmrgExcept as contained in this notice, the name of The Open Group shall not be 2227702724Smrgused in advertising or otherwise to promote the sale, use or other dealings 2327702724Smrgin this Software without prior written authorization from The Open Group. 2427702724Smrg 2527702724Smrg*/ 2627702724Smrg 2727702724Smrg#ifdef HAVE_CONFIG_H 2827702724Smrg#include <config.h> 2927702724Smrg#endif 3027702724Smrg#include <X11/Xauth.h> 3127702724Smrg#include <X11/Xos.h> 325dc1b958Smrg#include <assert.h> 3327702724Smrg#include <stdlib.h> 3427702724Smrg 35313a12fdSmrgstatic char *buf = NULL; 36313a12fdSmrg 37313a12fdSmrgstatic void 38c41596d7Schristos#ifdef __NetBSD__ 39c41596d7Schristos__attribute__ ((__destructor__)) 40c41596d7Schristos#endif 41313a12fdSmrgfree_filename_buffer(void) 42313a12fdSmrg{ 43313a12fdSmrg free(buf); 44313a12fdSmrg buf = NULL; 45313a12fdSmrg} 46313a12fdSmrg 4727702724Smrgchar * 4827702724SmrgXauFileName (void) 4927702724Smrg{ 5027702724Smrg const char *slashDotXauthority = "/.Xauthority"; 5127702724Smrg char *name; 52313a12fdSmrg static size_t bsize; 53c41596d7Schristos#ifndef __NetBSD__ 54313a12fdSmrg static int atexit_registered = 0; 55c41596d7Schristos#endif 5627702724Smrg#ifdef WIN32 5727702724Smrg char dir[128]; 5827702724Smrg#endif 59313a12fdSmrg size_t size; 6027702724Smrg 6127702724Smrg if ((name = getenv ("XAUTHORITY"))) 6227702724Smrg return name; 6327702724Smrg name = getenv ("HOME"); 6427702724Smrg if (!name) { 6527702724Smrg#ifdef WIN32 6627702724Smrg if ((name = getenv("USERNAME"))) { 67313a12fdSmrg snprintf(dir, sizeof(dir), "/users/%s", name); 6827702724Smrg name = dir; 6927702724Smrg } 7027702724Smrg if (!name) 7127702724Smrg#endif 7227702724Smrg return NULL; 7327702724Smrg } 7427702724Smrg size = strlen (name) + strlen(&slashDotXauthority[1]) + 2; 755dc1b958Smrg if ((size > bsize) || (buf == NULL)) { 765dc1b958Smrg free (buf); 775dc1b958Smrg assert(size > 0); 78313a12fdSmrg buf = malloc (size); 795dc1b958Smrg if (!buf) { 805dc1b958Smrg bsize = 0; 8127702724Smrg return NULL; 825dc1b958Smrg } 83313a12fdSmrg 84c41596d7Schristos#ifndef __NetBSD__ 85313a12fdSmrg if (!atexit_registered) { 86313a12fdSmrg atexit(free_filename_buffer); 87313a12fdSmrg atexit_registered = 1; 88313a12fdSmrg } 89c41596d7Schristos#endif 90313a12fdSmrg 9127702724Smrg bsize = size; 9227702724Smrg } 93313a12fdSmrg snprintf (buf, bsize, "%s%s", name, 945dc1b958Smrg slashDotXauthority + (name[0] == '/' && name[1] == '\0' ? 1 : 0)); 9527702724Smrg return buf; 9627702724Smrg} 97