* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
static char sccsid[] = "@(#)getlogin.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/gen/getlogin.c,v 1.9 2003/10/29 10:45:01 tjr Exp $");
+__FBSDID("$FreeBSD: src/lib/libc/gen/getlogin.c,v 1.11 2009/12/05 19:04:21 ed Exp $");
#include <sys/param.h>
#include <errno.h>
#include <pwd.h>
-#include <utmp.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "namespace.h"
#include "libc_private.h"
-#define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&logname_mutex)
-#define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&logname_mutex)
+extern int __getlogin(char *, int);
-extern int _getlogin(char *, int);
-
-int _logname_valid; /* known to setlogin() */
-static pthread_mutex_t logname_mutex = PTHREAD_MUTEX_INITIALIZER;
+__private_extern__ pthread_mutex_t __logname_mutex = PTHREAD_MUTEX_INITIALIZER;
+__private_extern__ char *__logname = NULL;
static char *
getlogin_basic(int *status)
{
- static char logname[MAXLOGNAME];
+ if (__logname == NULL) {
+ __logname = calloc(1, MAXLOGNAME);
+ if (__logname == NULL) {
+ *status = ENOMEM;
+ return (NULL);
+ }
+ }
- if (_logname_valid == 0) {
- if (_getlogin(logname, sizeof(logname)) < 0) {
+ if (__logname[0] == 0) {
+ if (__getlogin(__logname, MAXLOGNAME) < 0) {
*status = errno;
return (NULL);
}
- _logname_valid = 1;
}
*status = 0;
- return (*logname ? logname : NULL);
+ return (*__logname ? __logname : NULL);
}
char *
char *result;
int status;
- THREAD_LOCK();
+ pthread_mutex_lock(&__logname_mutex);
result = getlogin_basic(&status);
- THREAD_UNLOCK();
+ pthread_mutex_unlock(&__logname_mutex);
return (result);
}
int
-getlogin_r(char *logname, int namelen)
+getlogin_r(char *logname, size_t namelen)
{
char *result;
- int len;
int status;
-
- THREAD_LOCK();
+
+ pthread_mutex_lock(&__logname_mutex);
result = getlogin_basic(&status);
if (status == 0) {
- if ((len = strlen(result) + 1) > namelen)
+ if (strlcpy(logname, __logname, namelen) > namelen) {
status = ERANGE;
- else
- strncpy(logname, result, len);
+ }
}
- THREAD_UNLOCK();
+ pthread_mutex_unlock(&__logname_mutex);
+
return (status);
}