mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-11 16:50:42 +00:00
Use current user id to get user name.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@18242 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
d2936869b9
commit
34fcca39c1
2 changed files with 43 additions and 59 deletions
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
* Source/NSNumber.m: Raise exception if user code incorrectly tries
|
* Source/NSNumber.m: Raise exception if user code incorrectly tries
|
||||||
to deallocate a cached small number.
|
to deallocate a cached small number.
|
||||||
|
* Source/NSUser.m: Use geteuid() to determine name of current user
|
||||||
|
under unix.
|
||||||
|
|
||||||
2003-11-25 Richard Frith-Macdonald <rfm@gnu.org>
|
2003-11-25 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
|
|
100
Source/NSUser.m
100
Source/NSUser.m
|
@ -104,31 +104,26 @@ GSSetUserName(NSString* name)
|
||||||
{
|
{
|
||||||
NSUserName(); // Ensure we know the old user name.
|
NSUserName(); // Ensure we know the old user name.
|
||||||
}
|
}
|
||||||
if ([theUserName isEqualToString: name] == NO)
|
/*
|
||||||
{
|
* We can destroy the cached user path so that next time
|
||||||
/*
|
* anything wants it, it will be regenerated.
|
||||||
* We can destroy the cached user path so that next time
|
*/
|
||||||
* anything wants it, it will be regenerated.
|
DESTROY(gnustep_user_root);
|
||||||
*/
|
/*
|
||||||
DESTROY(gnustep_user_root);
|
* Next we can set up the new user name, and reset the user defaults
|
||||||
/*
|
* system so that standard user defaults will be those of the new
|
||||||
* Next we can set up the new user name, and reset the user defaults
|
* user.
|
||||||
* system so that standard user defaults will be those of the new
|
*/
|
||||||
* user.
|
ASSIGN(theUserName, name);
|
||||||
*/
|
[NSUserDefaults resetStandardUserDefaults];
|
||||||
ASSIGN(theUserName, name);
|
|
||||||
[NSUserDefaults resetStandardUserDefaults];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the caller's login name as an NSString object.
|
* Return the caller's login name as an NSString object.<br />
|
||||||
* The 'LOGNAME' environment variable is our primary source, but we use
|
* Under ms-windows, the 'LOGNAME' environment variable is used as the
|
||||||
* other system-dependent sources if LOGNAME is not set. This function
|
* user name.<br />
|
||||||
* is intended to return the name under which the user logged in rather
|
* Under unix-like systems, the name associated with the current
|
||||||
* than the name associated with their numeric user ID (though the two
|
* effective user ID is used.
|
||||||
* are usually the same). If you have a setuid program and want to
|
|
||||||
* change the user to reflect the uid, use GSSetUserName()
|
|
||||||
*/
|
*/
|
||||||
/* NOTE FOR DEVELOPERS.
|
/* NOTE FOR DEVELOPERS.
|
||||||
* If you change the behavior of this method you must also change
|
* If you change the behavior of this method you must also change
|
||||||
|
@ -137,10 +132,10 @@ GSSetUserName(NSString* name)
|
||||||
NSString *
|
NSString *
|
||||||
NSUserName(void)
|
NSUserName(void)
|
||||||
{
|
{
|
||||||
|
#if defined(__WIN32__)
|
||||||
if (theUserName == nil)
|
if (theUserName == nil)
|
||||||
{
|
{
|
||||||
const char *loginName = 0;
|
const char *loginName = 0;
|
||||||
#if defined(__WIN32__)
|
|
||||||
/* The GetUserName function returns the current user name */
|
/* The GetUserName function returns the current user name */
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
DWORD n = 1024;
|
DWORD n = 1024;
|
||||||
|
@ -149,48 +144,35 @@ NSUserName(void)
|
||||||
loginName = buf;
|
loginName = buf;
|
||||||
else if (GetUserName(buf, &n) != 0 && buf[0] != '\0')
|
else if (GetUserName(buf, &n) != 0 && buf[0] != '\0')
|
||||||
loginName = buf;
|
loginName = buf;
|
||||||
#else
|
|
||||||
loginName = getenv("LOGNAME");
|
|
||||||
#ifdef HAVE_GETPWNAM
|
|
||||||
/*
|
|
||||||
* Check that LOGNAME contained legal name.
|
|
||||||
*/
|
|
||||||
if (loginName != 0 && getpwnam(loginName) == 0)
|
|
||||||
{
|
|
||||||
loginName = 0;
|
|
||||||
}
|
|
||||||
#endif /* HAVE_GETPWNAM */
|
|
||||||
#ifdef HAVE_GETLOGIN
|
|
||||||
/*
|
|
||||||
* Try getlogin() if LOGNAME environmentm variable didn't work.
|
|
||||||
*/
|
|
||||||
if (loginName == 0)
|
|
||||||
{
|
|
||||||
loginName = getlogin();
|
|
||||||
}
|
|
||||||
#endif /* HAVE_GETLOGIN */
|
|
||||||
#ifdef HAVE_GETPWUID
|
|
||||||
/*
|
|
||||||
* Try getting the name of the effective user as a last resort.
|
|
||||||
*/
|
|
||||||
if (loginName == 0)
|
|
||||||
{
|
|
||||||
#ifdef HAVE_GETEUID
|
|
||||||
int uid = geteuid();
|
|
||||||
#else
|
|
||||||
int uid = getuid();
|
|
||||||
#endif /* HAVE_GETEUID */
|
|
||||||
struct passwd *pwent = getpwuid (uid);
|
|
||||||
loginName = pwent->pw_name;
|
|
||||||
}
|
|
||||||
#endif /* HAVE_GETPWUID */
|
|
||||||
#endif
|
|
||||||
if (loginName)
|
if (loginName)
|
||||||
theUserName = [[NSString alloc] initWithCString: loginName];
|
theUserName = [[NSString alloc] initWithCString: loginName];
|
||||||
else
|
else
|
||||||
[NSException raise: NSInternalInconsistencyException
|
[NSException raise: NSInternalInconsistencyException
|
||||||
format: @"Unable to determine current user name"];
|
format: @"Unable to determine current user name"];
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
static int olduid = 0;
|
||||||
|
#ifdef HAVE_GETEUID
|
||||||
|
int uid = geteuid();
|
||||||
|
#else
|
||||||
|
int uid = getuid();
|
||||||
|
#endif /* HAVE_GETEUID */
|
||||||
|
|
||||||
|
if (theUserName == nil || uid != olduid)
|
||||||
|
{
|
||||||
|
const char *loginName = 0;
|
||||||
|
#ifdef HAVE_GETPWUID
|
||||||
|
struct passwd *pwent = getpwuid (uid);
|
||||||
|
loginName = pwent->pw_name;
|
||||||
|
#endif /* HAVE_GETPWUID */
|
||||||
|
olduid = uid;
|
||||||
|
if (loginName)
|
||||||
|
theUserName = [[NSString alloc] initWithCString: loginName];
|
||||||
|
else
|
||||||
|
[NSException raise: NSInternalInconsistencyException
|
||||||
|
format: @"Unable to determine current user name"];
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return theUserName;
|
return theUserName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue