dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libc / port / gen / getlogin.c
blobfadf3e055b9741aeecd66323059b6714f3bead40
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2013 Gary Mills
25 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
29 /* Copyright (c) 1988 AT&T */
30 /* All Rights Reserved */
32 #pragma weak _getlogin = getloginx
33 #pragma weak _getlogin_r = getloginx_r
35 #include "lint.h"
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <limits.h>
42 #include "utmpx.h"
43 #include <unistd.h>
44 #include <errno.h>
45 #include <thread.h>
46 #include <synch.h>
47 #include <mtlib.h>
48 #include "tsd.h"
50 /* Revert the renames done in unistd.h */
51 #ifdef __PRAGMA_REDEFINE_EXTNAME
52 #pragma redefine_extname getlogint getlogin
53 #pragma redefine_extname getlogint_r getlogin_r
54 #pragma redefine_extname __posix_getlogint_r __posix_getlogin_r
55 #else /* __PRAGMA_REDEFINE_EXTNAME */
56 #ifdef getlogin
57 #undef getlogin
58 #endif /* getlogin */
59 #ifdef getlogin_r
60 #undef getlogin_r
61 #endif /* getlogin_r */
62 #ifdef __posix_getlogin_r
63 #undef __posix_getlogin_r
64 #endif /* __posix_getlogin_r */
65 #define getlogint getlogin
66 #define getlogint_r getlogin_r
67 #define __posix_getlogint_r __posix_getlogin_r
68 #endif /* __PRAGMA_REDEFINE_EXTNAME */
69 extern char *getlogint(void);
70 extern char *getlogint_r(char *, int);
71 extern int __posix_getlogint_r(char *, int);
74 * Use the full length of a login name.
75 * The utmpx interface provides for a 32 character login name.
77 #define NMAX (sizeof (((struct utmpx *)0)->ut_user))
80 * Common function
82 static char *
83 getl_r_common(char *answer, size_t namelen, size_t maxlen)
85 int uf;
86 off64_t me;
87 struct futmpx ubuf;
88 size_t ulen;
90 if ((me = (off64_t)ttyslot()) < 0)
91 return (NULL);
92 if ((uf = open64(UTMPX_FILE, 0)) < 0)
93 return (NULL);
94 (void) lseek64(uf, me * sizeof (ubuf), SEEK_SET);
95 if (read(uf, &ubuf, sizeof (ubuf)) != sizeof (ubuf)) {
96 (void) close(uf);
97 return (NULL);
99 (void) close(uf);
100 if (ubuf.ut_user[0] == '\0')
101 return (NULL);
103 /* Insufficient buffer size */
104 ulen = strnlen(ubuf.ut_user, maxlen);
105 if (namelen <= ulen) {
106 errno = ERANGE;
107 return (NULL);
111 * While the interface to getlogin_r says that a user should supply a
112 * buffer with at least LOGIN_NAME_MAX bytes, we shouldn't assume they
113 * have, especially since we've been supplied with its actual size.
114 * Doing otherwise is just asking us to corrupt memory (and has in the
115 * past).
117 (void) strncpy(answer, ubuf.ut_user, ulen);
118 answer[ulen] = '\0';
119 return (answer);
123 * POSIX.1c Draft-6 version of the function getlogin_r.
124 * It was implemented by Solaris 2.3.
126 char *
127 getlogint_r(char *answer, int namelen)
129 return (getl_r_common(answer, (size_t)namelen, LOGNAME_MAX_TRAD));
133 * POSIX.1c standard version of the function getlogin_r.
134 * User gets it via static getlogin_r from the header file.
137 __posix_getlogint_r(char *name, int namelen)
139 int nerrno = 0;
140 int oerrno = errno;
142 errno = 0;
143 if (getl_r_common(name, (size_t)namelen, LOGNAME_MAX_TRAD) == NULL) {
144 if (errno == 0)
145 nerrno = EINVAL;
146 else
147 nerrno = errno;
149 errno = oerrno;
150 return (nerrno);
153 char *
154 getlogint(void)
156 char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX_TRAD, NULL);
158 if (answer == NULL)
159 return (NULL);
160 return (getl_r_common(answer, LOGIN_NAME_MAX_TRAD, LOGNAME_MAX_TRAD));
164 * POSIX.1c Draft-6 version of the function getlogin_r.
165 * It was implemented by Solaris 2.3.
166 * For extended login names, selected by redefine_extname in unistd.h.
168 char *
169 getloginx_r(char *answer, int namelen)
171 return (getl_r_common(answer, (size_t)namelen, NMAX));
175 * POSIX.1c standard version of the function getlogin_r.
176 * User gets it via static getlogin_r from the header file.
177 * For extended login names, selected by redefine_extname in unistd.h.
180 __posix_getloginx_r(char *name, int namelen)
182 int nerrno = 0;
183 int oerrno = errno;
185 errno = 0;
186 if (getl_r_common(name, (size_t)namelen, NMAX) == NULL) {
187 if (errno == 0)
188 nerrno = EINVAL;
189 else
190 nerrno = errno;
192 errno = oerrno;
193 return (nerrno);
197 * For extended login names, selected by redefine_extname in unistd.h.
199 char *
200 getloginx(void)
202 char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX, NULL);
204 if (answer == NULL)
205 return (NULL);
206 return (getl_r_common(answer, LOGIN_NAME_MAX, NMAX));