AuFileName.c revision 313a12fd
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> 3227702724Smrg#include <stdlib.h> 3327702724Smrg 34313a12fdSmrgstatic char *buf = NULL; 35313a12fdSmrg 36313a12fdSmrgstatic void 37313a12fdSmrgfree_filename_buffer(void) 38313a12fdSmrg{ 39313a12fdSmrg free(buf); 40313a12fdSmrg buf = NULL; 41313a12fdSmrg} 42313a12fdSmrg 4327702724Smrgchar * 4427702724SmrgXauFileName (void) 4527702724Smrg{ 4627702724Smrg const char *slashDotXauthority = "/.Xauthority"; 4727702724Smrg char *name; 48313a12fdSmrg static size_t bsize; 49313a12fdSmrg static int atexit_registered = 0; 5027702724Smrg#ifdef WIN32 5127702724Smrg char dir[128]; 5227702724Smrg#endif 53313a12fdSmrg size_t size; 5427702724Smrg 5527702724Smrg if ((name = getenv ("XAUTHORITY"))) 5627702724Smrg return name; 5727702724Smrg name = getenv ("HOME"); 5827702724Smrg if (!name) { 5927702724Smrg#ifdef WIN32 6027702724Smrg if ((name = getenv("USERNAME"))) { 61313a12fdSmrg snprintf(dir, sizeof(dir), "/users/%s", name); 6227702724Smrg name = dir; 6327702724Smrg } 6427702724Smrg if (!name) 6527702724Smrg#endif 6627702724Smrg return NULL; 6727702724Smrg } 6827702724Smrg size = strlen (name) + strlen(&slashDotXauthority[1]) + 2; 6927702724Smrg if (size > bsize) { 7027702724Smrg if (buf) 7127702724Smrg free (buf); 72313a12fdSmrg buf = malloc (size); 7327702724Smrg if (!buf) 7427702724Smrg return NULL; 75313a12fdSmrg 76313a12fdSmrg if (!atexit_registered) { 77313a12fdSmrg atexit(free_filename_buffer); 78313a12fdSmrg atexit_registered = 1; 79313a12fdSmrg } 80313a12fdSmrg 8127702724Smrg bsize = size; 8227702724Smrg } 83313a12fdSmrg snprintf (buf, bsize, "%s%s", name, 84313a12fdSmrg slashDotXauthority + (name[1] == '\0' ? 1 : 0)); 8527702724Smrg return buf; 8627702724Smrg} 87