tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / gen / getlogin.c
blob3821f66e52ada85809d2a5cd444a9e6057815383
1 /* $NetBSD: getlogin.c,v 1.15 2009/01/11 02:46:27 christos Exp $ */
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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
35 * are met:
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
55 * SUCH DAMAGE.
58 #include <sys/cdefs.h>
59 #if defined(LIBC_SCCS) && !defined(lint)
60 #if 0
61 static char sccsid[] = "@(#)getlogin.c 8.1 (Berkeley) 6/4/93";
62 #else
63 __RCSID("$NetBSD: getlogin.c,v 1.15 2009/01/11 02:46:27 christos Exp $");
64 #endif
65 #endif /* LIBC_SCCS and not lint */
67 #include "namespace.h"
68 #include <sys/param.h>
69 #include <pwd.h>
70 #include <utmp.h>
71 #include <stdio.h>
72 #include <string.h>
73 #include <unistd.h>
74 #include <errno.h>
75 #include "reentrant.h"
76 #include "extern.h"
78 #ifdef __weak_alias
79 __weak_alias(getlogin,_getlogin)
80 __weak_alias(getlogin_r,_getlogin_r)
81 #if !defined(__minix)
82 __weak_alias(setlogin,_setlogin)
83 #endif /* !defined(__minix) */
84 #endif
86 int __logname_valid; /* known to setlogin() */
87 static char logname[MAXLOGNAME + 1];
89 #ifdef _REENTRANT
90 static mutex_t logname_mutex = MUTEX_INITIALIZER;
91 #endif
93 char *
94 getlogin(void)
96 char *rv;
98 mutex_lock(&logname_mutex);
99 if (__logname_valid == 0) {
100 if (__getlogin(logname, sizeof(logname) - 1) < 0) {
101 mutex_unlock(&logname_mutex);
102 return ((char *)NULL);
104 __logname_valid = 1;
106 rv = (*logname ? logname : (char *)NULL);
107 mutex_unlock(&logname_mutex);
109 return rv;
113 getlogin_r(char *name, size_t namelen)
115 size_t len;
116 int rv;
118 mutex_lock(&logname_mutex);
119 if (__logname_valid == 0) {
120 if (__getlogin(logname, sizeof(logname) - 1) < 0) {
121 rv = errno;
122 mutex_unlock(&logname_mutex);
123 return (rv);
125 __logname_valid = 1;
127 len = strlen(logname) + 1;
128 if (len > namelen) {
129 rv = ERANGE;
130 } else {
131 strncpy(name, logname, len);
132 rv = 0;
134 mutex_unlock(&logname_mutex);
136 return (rv);
139 #if !defined(__minix)
141 setlogin(const char *name)
143 int retval;
145 retval = __setlogin(name);
146 __logname_valid = 0;
148 return (retval);
150 #endif /* !defined(__minix) */