xref: /titanic_50/usr/src/cmd/bnu/getpwinfo.c (revision eab227978ccdaa5a7cc9fd92ace768915dae3a2b)
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 #ident	"%Z%%M%	%I%	%E% SMI"	/* from SVR4 bnu:getpwinfo.c 2.8 */
27 
28 #include "uucp.h"
29 
30 #include <pwd.h>
31 extern struct passwd *getpwuid(), *getpwnam();
32 extern char	*getlogin();
33 
34 
35 /*
36  * get passwd file info for logname or uid
37  *	uid	-> uid #
38  *	name	-> address of buffer to return ascii user name
39  *		This will be set to pw->pw_name.
40  *
41  * return:
42  *	0	-> success
43  *	FAIL	-> failure (logname and uid not found)
44  */
45 int
46 guinfo(uid, name)
47 uid_t uid;
48 char *name;
49 {
50 	register struct passwd *pwd;
51 	char	*login_name;
52 
53 	/* look for this user as logged in utmp */
54 	if ((login_name = getlogin()) != NULL) {
55 		pwd = getpwnam(login_name);
56 		if (pwd != NULL && pwd->pw_uid == uid)
57 			goto uid_found;
58 	}
59 
60 	/* no dice on utmp -- get first from passwd file */
61 	if ((pwd = getpwuid(uid)) == NULL) {
62 	    if ((pwd = getpwuid(UUCPUID)) == NULL)
63 		/* can not find uid in passwd file */
64 		return(FAIL);
65 	}
66 
67 uid_found:
68 	(void) strcpy(name, pwd->pw_name);
69 	return(0);
70 }
71 
72 /*
73  * get passwd file info for name
74  *	name	-> ascii user name
75  *	uid	-> address of integer to return uid # in
76  *	path	-> address of buffer to return working directory in
77  * returns:
78  *	0	-> success
79  *	FAIL	-> failure
80  */
81 int
82 gninfo(name, uid, path)
83 char *path, *name;
84 uid_t *uid;
85 {
86 	register struct passwd *pwd;
87 
88 	if ((pwd = getpwnam(name)) == NULL) {
89 		/* can not find name in passwd file */
90 		*path = '\0';
91 		return(FAIL);
92 	}
93 
94 	(void) strcpy(path, pwd->pw_dir);
95 	*uid = pwd->pw_uid;
96 	return(0);
97 }
98 
99 
100