2 * Copyright (c) 1996 - 1997, Marek Michałkiewicz
3 * Copyright (c) 2003 - 2005, Tomasz Kłoczko
4 * Copyright (c) 2007 - 2009, Nicolas François
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the copyright holders or contributors may not be used to
16 * endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * myname.c - determine the current username and get the passwd entry
39 #ident "$Id: myname.c 2813 2009-04-26 17:10:49Z nekral-guest $"
43 #include "prototypes.h"
44 /*@null@*/ /*@only@*/struct passwd *get_my_pwent (void)
47 const char *cp = getlogin ();
48 uid_t ruid = getuid ();
51 * Try getlogin() first - if it fails or returns a non-existent
52 * username, or a username which doesn't match the real UID, fall
53 * back to getpwuid(getuid()). This should work reasonably with
54 * usernames longer than the utmp limit (8 characters), as well as
55 * shared UIDs - but not both at the same time...
57 * XXX - when running from su, will return the current user (not
58 * the original user, like getlogin() does). Does this matter?
60 if ((NULL != cp) && ('\0' != *cp)) {
62 if ((NULL != pw) && (pw->pw_uid == ruid)) {
67 return xgetpwuid (ruid);