1 /* $NetBSD: getlogin.c,v 1.13.6.1 2009/01/10 22:59:51 christos Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 1988, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/cdefs.h>
59 #if defined(LIBC_SCCS) && !defined(lint)
61 static char sccsid
[] = "@(#)getlogin.c 8.1 (Berkeley) 6/4/93";
63 __RCSID("$NetBSD: getlogin.c,v 1.13.6.1 2009/01/10 22:59:51 christos Exp $");
65 #endif /* LIBC_SCCS and not lint */
67 #include "namespace.h"
68 #include <sys/param.h>
75 #include "reentrant.h"
79 __weak_alias(getlogin
,_getlogin
)
80 __weak_alias(getlogin_r
,_getlogin_r
)
81 __weak_alias(setlogin
,_setlogin
)
84 int __logname_valid
; /* known to setlogin() */
85 static char logname
[MAXLOGNAME
+ 1];
88 static mutex_t logname_mutex
= MUTEX_INITIALIZER
;
96 mutex_lock(&logname_mutex
);
97 if (__logname_valid
== 0) {
98 if (__getlogin(logname
, sizeof(logname
) - 1) < 0) {
99 mutex_unlock(&logname_mutex
);
100 return ((char *)NULL
);
104 rv
= (*logname
? logname
: (char *)NULL
);
105 mutex_unlock(&logname_mutex
);
111 getlogin_r(char *name
, size_t namelen
)
116 mutex_lock(&logname_mutex
);
117 if (__logname_valid
== 0) {
118 if (__getlogin(logname
, sizeof(logname
) - 1) < 0) {
120 mutex_unlock(&logname_mutex
);
125 len
= strlen(logname
) + 1;
129 strncpy(name
, logname
, len
);
132 mutex_unlock(&logname_mutex
);
138 setlogin(const char *name
)
142 retval
= __setlogin(name
);