Initial commit for Tizen
[profile/extras/shadow-utils.git] / libmisc / myname.c
1 /*
2  * Copyright (c) 1996 - 1997, Marek Michałkiewicz
3  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
4  * Copyright (c) 2007 - 2009, Nicolas François
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
30  */
31
32 /*
33  * myname.c - determine the current username and get the passwd entry
34  *
35  */
36
37 #include <config.h>
38
39 #ident "$Id: myname.c 2813 2009-04-26 17:10:49Z nekral-guest $"
40
41 #include "defines.h"
42 #include <pwd.h>
43 #include "prototypes.h"
44 /*@null@*/ /*@only@*/struct passwd *get_my_pwent (void)
45 {
46         struct passwd *pw;
47         const char *cp = getlogin ();
48         uid_t ruid = getuid ();
49
50         /*
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...
56          *
57          * XXX - when running from su, will return the current user (not
58          * the original user, like getlogin() does).  Does this matter?
59          */
60         if ((NULL != cp) && ('\0' != *cp)) {
61                 pw = xgetpwnam (cp);
62                 if ((NULL != pw) && (pw->pw_uid == ruid)) {
63                         return pw;
64                 }
65         }
66
67         return xgetpwuid (ruid);
68 }
69