xref: /illumos-gate/usr/src/cmd/acct/lib/uidtonam.c (revision 1f5207b7604fb44407eb4342aff613f7c4508508)
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  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.6	*/
30 /*
31  * convert uid to login name; interface to getpwuid that keeps up to USIZE1
32  * names to avoid unnecessary accesses to passwd file
33  * returns ptr to NSZ-byte name (not necessarily null-terminated)
34  * returns ptr to "?" if cannot convert
35  */
36 
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include "acctdef.h"
41 #include <pwd.h>
42 
43 static int usize1;
44 static struct ulist {
45 	char	uname[NSZ];
46 	uid_t	uuid;
47 } ul[USIZE1];
48 
49 char *strncpy();
50 
51 char *
52 uidtonam(uid)
53 uid_t	uid;
54 {
55 	register struct ulist *up;
56 	struct passwd *getpwuid();
57 	register struct passwd *pp;
58 
59 	for (up = ul; up < &ul[usize1]; up++)
60 		if (uid == up->uuid)
61 			return(up->uname);
62 	setpwent();
63 	if ((pp = getpwuid(uid)) == NULL)
64 		return("?");
65 	else {
66 		if (usize1 < USIZE1) {
67 			up->uuid = uid;
68 			CPYN(up->uname, pp->pw_name);
69 			usize1++;
70 		}
71 		return(pp->pw_name);
72 	}
73 }
74