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]
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
36 #include <sys/types.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 */
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))
83 getl_r_common(char *answer
, size_t namelen
, size_t maxlen
)
90 if ((me
= (off64_t
)ttyslot()) < 0)
92 if ((uf
= open64(UTMPX_FILE
, 0)) < 0)
94 (void) lseek64(uf
, me
* sizeof (ubuf
), SEEK_SET
);
95 if (read(uf
, &ubuf
, sizeof (ubuf
)) != sizeof (ubuf
)) {
100 if (ubuf
.ut_user
[0] == '\0')
103 /* Insufficient buffer size */
104 ulen
= strnlen(ubuf
.ut_user
, maxlen
);
105 if (namelen
<= ulen
) {
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
117 (void) strncpy(answer
, ubuf
.ut_user
, ulen
);
123 * POSIX.1c Draft-6 version of the function getlogin_r.
124 * It was implemented by Solaris 2.3.
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
)
143 if (getl_r_common(name
, (size_t)namelen
, LOGNAME_MAX_TRAD
) == NULL
) {
156 char *answer
= tsdalloc(_T_LOGIN
, LOGIN_NAME_MAX_TRAD
, 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.
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
)
186 if (getl_r_common(name
, (size_t)namelen
, NMAX
) == NULL
) {
197 * For extended login names, selected by redefine_extname in unistd.h.
202 char *answer
= tsdalloc(_T_LOGIN
, LOGIN_NAME_MAX
, NULL
);
206 return (getl_r_common(answer
, LOGIN_NAME_MAX
, NMAX
));