get_default_username.c revision 233294
192555Sdes/*
276259Sgreen * Copyright (c) 1997 - 1999 Kungliga Tekniska H��gskolan
369587Sgreen * (Royal Institute of Technology, Stockholm, Sweden).
469587Sgreen * All rights reserved.
569587Sgreen *
669587Sgreen * Redistribution and use in source and binary forms, with or without
769587Sgreen * modification, are permitted provided that the following conditions
869587Sgreen * are met:
969587Sgreen *
1069587Sgreen * 1. Redistributions of source code must retain the above copyright
1169587Sgreen *    notice, this list of conditions and the following disclaimer.
1269587Sgreen *
1369587Sgreen * 2. Redistributions in binary form must reproduce the above copyright
1469587Sgreen *    notice, this list of conditions and the following disclaimer in the
1569587Sgreen *    documentation and/or other materials provided with the distribution.
1669587Sgreen *
1769587Sgreen * 3. Neither the name of the Institute nor the names of its contributors
1869587Sgreen *    may be used to endorse or promote products derived from this software
1969587Sgreen *    without specific prior written permission.
2069587Sgreen *
2169587Sgreen * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2269587Sgreen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2369587Sgreen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2469587Sgreen * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2569587Sgreen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2669587Sgreen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2769587Sgreen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2869587Sgreen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2969587Sgreen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3069587Sgreen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3169587Sgreen * SUCH DAMAGE.
3269587Sgreen */
3369587Sgreen
3469587Sgreen#include <config.h>
3592555Sdes
3676259Sgreen#include "roken.h"
3776259Sgreen
3876259Sgreen/*
3969587Sgreen * Try to return what should be considered the default username or
4092555Sdes * NULL if we can't guess at all.
4192555Sdes */
4276259Sgreen
4392555SdesROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL
4476259Sgreenget_default_username (void)
4576259Sgreen{
4676259Sgreen    const char *user;
4776259Sgreen
4869587Sgreen    user = getenv ("USER");
49    if (user == NULL)
50	user = getenv ("LOGNAME");
51    if (user == NULL)
52	user = getenv ("USERNAME");
53
54#if defined(HAVE_GETLOGIN) && !defined(POSIX_GETLOGIN)
55    if (user == NULL) {
56	user = (const char *)getlogin ();
57	if (user != NULL)
58	    return user;
59    }
60#endif
61#ifdef HAVE_PWD_H
62    {
63	uid_t uid = getuid ();
64	struct passwd *pwd;
65
66	if (user != NULL) {
67	    pwd = k_getpwnam (user);
68	    if (pwd != NULL && pwd->pw_uid == uid)
69		return user;
70	}
71	pwd = k_getpwuid (uid);
72	if (pwd != NULL)
73	    return pwd->pw_name;
74    }
75#endif
76#ifdef _WIN32
77    /* TODO: We can call GetUserNameEx() and figure out a
78       username. However, callers do not free the return value of this
79       function. */
80#endif
81
82    return user;
83}
84