Blame AuFileName.c

Packit Service f89583
/*
Packit Service f89583
Packit Service f89583
Copyright 1988, 1998  The Open Group
Packit Service f89583
Packit Service f89583
Permission to use, copy, modify, distribute, and sell this software and its
Packit Service f89583
documentation for any purpose is hereby granted without fee, provided that
Packit Service f89583
the above copyright notice appear in all copies and that both that
Packit Service f89583
copyright notice and this permission notice appear in supporting
Packit Service f89583
documentation.
Packit Service f89583
Packit Service f89583
The above copyright notice and this permission notice shall be included in
Packit Service f89583
all copies or substantial portions of the Software.
Packit Service f89583
Packit Service f89583
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service f89583
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service f89583
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
Packit Service f89583
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit Service f89583
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit Service f89583
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit Service f89583
Packit Service f89583
Except as contained in this notice, the name of The Open Group shall not be
Packit Service f89583
used in advertising or otherwise to promote the sale, use or other dealings
Packit Service f89583
in this Software without prior written authorization from The Open Group.
Packit Service f89583
Packit Service f89583
*/
Packit Service f89583
Packit Service f89583
#ifdef HAVE_CONFIG_H
Packit Service f89583
#include <config.h>
Packit Service f89583
#endif
Packit Service f89583
#include <X11/Xauth.h>
Packit Service f89583
#include <X11/Xos.h>
Packit Service f89583
#include <assert.h>
Packit Service f89583
#include <stdlib.h>
Packit Service f89583
Packit Service f89583
static char *buf = NULL;
Packit Service f89583
Packit Service f89583
static void
Packit Service f89583
free_filename_buffer(void)
Packit Service f89583
{
Packit Service f89583
    free(buf);
Packit Service f89583
    buf = NULL;
Packit Service f89583
}
Packit Service f89583
Packit Service f89583
char *
Packit Service f89583
XauFileName (void)
Packit Service f89583
{
Packit Service f89583
    const char *slashDotXauthority = "/.Xauthority";
Packit Service f89583
    char    *name;
Packit Service f89583
    static size_t	bsize;
Packit Service f89583
    static int atexit_registered = 0;
Packit Service f89583
#ifdef WIN32
Packit Service f89583
    char    dir[128];
Packit Service f89583
#endif
Packit Service f89583
    size_t  size;
Packit Service f89583
Packit Service f89583
    if ((name = getenv ("XAUTHORITY")))
Packit Service f89583
	return name;
Packit Service f89583
    name = getenv ("HOME");
Packit Service f89583
    if (!name) {
Packit Service f89583
#ifdef WIN32
Packit Service f89583
	if ((name = getenv("USERNAME"))) {
Packit Service f89583
	    snprintf(dir, sizeof(dir), "/users/%s", name);
Packit Service f89583
	    name = dir;
Packit Service f89583
	}
Packit Service f89583
	if (!name)
Packit Service f89583
#endif
Packit Service f89583
	return NULL;
Packit Service f89583
    }
Packit Service f89583
    size = strlen (name) + strlen(&slashDotXauthority[1]) + 2;
Packit Service f89583
    if ((size > bsize) || (buf == NULL)) {
Packit Service f89583
	free (buf);
Packit Service f89583
        assert(size > 0);
Packit Service f89583
	buf = malloc (size);
Packit Service f89583
	if (!buf) {
Packit Service f89583
	    bsize = 0;
Packit Service f89583
	    return NULL;
Packit Service f89583
	}
Packit Service f89583
Packit Service f89583
        if (!atexit_registered) {
Packit Service f89583
            atexit(free_filename_buffer);
Packit Service f89583
            atexit_registered = 1;
Packit Service f89583
        }
Packit Service f89583
Packit Service f89583
	bsize = size;
Packit Service f89583
    }
Packit Service f89583
    snprintf (buf, bsize, "%s%s", name,
Packit Service f89583
              slashDotXauthority + (name[0] == '/' && name[1] == '\0' ? 1 : 0));
Packit Service f89583
    return buf;
Packit Service f89583
}