2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* myname.c - determine the current username and get the passwd entry
|
|
|
|
*
|
|
|
|
* Copyright (C) 1996 Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>
|
|
|
|
*
|
|
|
|
* This code may be freely used, modified and distributed for any purpose.
|
|
|
|
* There is no warranty, if it breaks you have to keep both pieces, etc.
|
|
|
|
* If you improve it, please send me your changes. Thanks!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-10-07 17:17:01 +05:30
|
|
|
#ident "$Id: myname.c,v 1.4 2005/08/31 17:24:57 kloczek Exp $"
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "defines.h"
|
|
|
|
#include <pwd.h>
|
|
|
|
#include "prototypes.h"
|
2007-10-07 17:15:23 +05:30
|
|
|
struct passwd *get_my_pwent (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct passwd *pw;
|
2007-10-07 17:15:23 +05:30
|
|
|
const char *cp = getlogin ();
|
|
|
|
uid_t ruid = getuid ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Try getlogin() first - if it fails or returns a non-existent
|
|
|
|
* username, or a username which doesn't match the real UID, fall
|
|
|
|
* back to getpwuid(getuid()). This should work reasonably with
|
|
|
|
* usernames longer than the utmp limit (8 characters), as well as
|
|
|
|
* shared UIDs - but not both at the same time...
|
|
|
|
*
|
|
|
|
* XXX - when running from su, will return the current user (not
|
|
|
|
* the original user, like getlogin() does). Does this matter?
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
if (cp && *cp && (pw = getpwnam (cp)) && pw->pw_uid == ruid)
|
2007-10-07 17:14:02 +05:30
|
|
|
return pw;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
return getpwuid (ruid);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|