Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / gen / getlogin.c
blobc5b4df419ee12c402e20be95de6a09dbe3c72df0
1 /* $NetBSD: getlogin.c,v 1.13.6.1 2009/01/10 22:59:51 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.13.6.1 2009/01/10 22:59:51 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 __weak_alias(setlogin,_setlogin)
82 #endif
84 int __logname_valid; /* known to setlogin() */
85 static char logname[MAXLOGNAME + 1];
87 #ifdef _REENTRANT
88 static mutex_t logname_mutex = MUTEX_INITIALIZER;
89 #endif
91 char *
92 getlogin(void)
94 char *rv;
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);
102 __logname_valid = 1;
104 rv = (*logname ? logname : (char *)NULL);
105 mutex_unlock(&logname_mutex);
107 return rv;
111 getlogin_r(char *name, size_t namelen)
113 size_t len;
114 int rv;
116 mutex_lock(&logname_mutex);
117 if (__logname_valid == 0) {
118 if (__getlogin(logname, sizeof(logname) - 1) < 0) {
119 rv = errno;
120 mutex_unlock(&logname_mutex);
121 return (rv);
123 __logname_valid = 1;
125 len = strlen(logname) + 1;
126 if (len > namelen) {
127 rv = ERANGE;
128 } else {
129 strncpy(name, logname, len);
130 rv = 0;
132 mutex_unlock(&logname_mutex);
134 return (rv);
138 setlogin(const char *name)
140 int retval;
142 retval = __setlogin(name);
143 __logname_valid = 0;
145 return (retval);