1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
24
25
26 /*
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31 #pragma ident "%Z%%M% %I% %E% SMI"
32 /*
33 * getname(name) -- get logname
34 *
35 * getname tries to find the user's logname from:
36 * ${LOGNAME}, if set and if it is telling the truth
37 * /etc/passwd, otherwise
38 *
39 * The logname is returned as the value of the function.
40 *
41 * Getname returns the user's user id converted to ASCII
42 * for unknown lognames.
43 *
44 */
45
46 #include "string.h"
47 #include "pwd.h"
48 #include "errno.h"
49 #include "sys/types.h"
50 #include "stdlib.h"
51 #include "unistd.h"
52
53 #include "lp.h"
54
55 char *
56 #if defined(__STDC__)
getname(void)57 getname (
58 void
59 )
60 #else
61 getname ()
62 #endif
63 {
64 uid_t uid;
65 struct passwd *p;
66 static char *logname = 0;
67 char *l;
68
69 if (logname)
70 return (logname);
71
72 uid = getuid();
73
74 setpwent ();
75 if (
76 !(l = getenv("LOGNAME"))
77 || !(p = getpwnam(l))
78 || p->pw_uid != uid
79 )
80 if ((p = getpwuid(uid)))
81 l = p->pw_name;
82 else
83 l = 0;
84 endpwent ();
85
86 if (l)
87 logname = Strdup(l);
88 else {
89 if (uid > 0) {
90 logname = Malloc(10 + 1);
91 if (logname)
92 sprintf (logname, "%d", uid);
93 }
94 }
95
96 if (!logname)
97 {
98 errno = ENOMEM;
99 }
100 else
101 {
102 errno = 0;
103 }
104
105 return (logname);
106 }
107