xref: /titanic_51/usr/src/lib/libast/common/string/fmtgid.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin *                                                                      *
3da2e3ebdSchin *               This software is part of the ast package               *
4*3e14f97fSRoger A. Faulkner *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5da2e3ebdSchin *                      and is licensed under the                       *
6da2e3ebdSchin *                  Common Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin *                                                                      *
9da2e3ebdSchin *                A copy of the License is available at                 *
10da2e3ebdSchin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebdSchin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebdSchin *                                                                      *
13da2e3ebdSchin *              Information and Software Systems Research               *
14da2e3ebdSchin *                            AT&T Research                             *
15da2e3ebdSchin *                           Florham Park NJ                            *
16da2e3ebdSchin *                                                                      *
17da2e3ebdSchin *                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebdSchin *                  David Korn <dgk@research.att.com>                   *
19da2e3ebdSchin *                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebdSchin *                                                                      *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin /*
24da2e3ebdSchin  * Glenn Fowler
25da2e3ebdSchin  * AT&T Bell Laboratories
26da2e3ebdSchin  *
27da2e3ebdSchin  * cached gid number -> name
28da2e3ebdSchin  */
29da2e3ebdSchin 
30da2e3ebdSchin #if defined(__STDPP__directive) && defined(__STDPP__hide)
31da2e3ebdSchin __STDPP__directive pragma pp:hide getgrgid
32da2e3ebdSchin #else
33da2e3ebdSchin #define getgrgid	______getgrgid
34da2e3ebdSchin #endif
35da2e3ebdSchin 
36da2e3ebdSchin #include <ast.h>
37da2e3ebdSchin #include <cdt.h>
38da2e3ebdSchin #include <grp.h>
39da2e3ebdSchin 
40da2e3ebdSchin #if defined(__STDPP__directive) && defined(__STDPP__hide)
41da2e3ebdSchin __STDPP__directive pragma pp:nohide getgrgid
42da2e3ebdSchin #else
43da2e3ebdSchin #undef	getgrgid
44da2e3ebdSchin #endif
45da2e3ebdSchin 
46da2e3ebdSchin extern struct group*	getgrgid(gid_t);
47da2e3ebdSchin 
48da2e3ebdSchin typedef struct Id_s
49da2e3ebdSchin {
50da2e3ebdSchin 	Dtlink_t	link;
51da2e3ebdSchin 	int		id;
52da2e3ebdSchin 	char		name[1];
53da2e3ebdSchin } Id_t;
54da2e3ebdSchin 
55da2e3ebdSchin /*
56da2e3ebdSchin  * return gid name given gid number
57da2e3ebdSchin  */
58da2e3ebdSchin 
59da2e3ebdSchin char*
fmtgid(int gid)60da2e3ebdSchin fmtgid(int gid)
61da2e3ebdSchin {
62da2e3ebdSchin 	register Id_t*		ip;
63da2e3ebdSchin 	register char*		name;
64da2e3ebdSchin 	register struct group*	gr;
65da2e3ebdSchin 	int			z;
66da2e3ebdSchin 
67da2e3ebdSchin 	static Dt_t*		dict;
68da2e3ebdSchin 	static Dtdisc_t		disc;
69da2e3ebdSchin 
70da2e3ebdSchin 	if (!dict)
71da2e3ebdSchin 	{
72da2e3ebdSchin 		disc.key = offsetof(Id_t, id);
73da2e3ebdSchin 		disc.size = sizeof(int);
74da2e3ebdSchin 		dict = dtopen(&disc, Dthash);
75da2e3ebdSchin 	}
76da2e3ebdSchin 	else if (ip = (Id_t*)dtmatch(dict, &gid))
77da2e3ebdSchin 		return ip->name;
78da2e3ebdSchin 	if (gr = getgrgid(gid))
79da2e3ebdSchin 	{
80da2e3ebdSchin 		name = gr->gr_name;
81da2e3ebdSchin #if _WINIX
82da2e3ebdSchin 		if (streq(name, "Administrators"))
83da2e3ebdSchin 			name = "sys";
84da2e3ebdSchin #endif
85da2e3ebdSchin 	}
86da2e3ebdSchin 	else if (gid == 0)
87da2e3ebdSchin 		name = "sys";
88da2e3ebdSchin 	else
89da2e3ebdSchin 	{
90da2e3ebdSchin 		name = fmtbuf(z = sizeof(gid) * 3 + 1);
91da2e3ebdSchin 		sfsprintf(name, z, "%I*d", sizeof(gid), gid);
92da2e3ebdSchin 	}
93da2e3ebdSchin 	if (dict && (ip = newof(0, Id_t, 1, strlen(name))))
94da2e3ebdSchin 	{
95da2e3ebdSchin 		ip->id = gid;
96da2e3ebdSchin 		strcpy(ip->name, name);
97da2e3ebdSchin 		dtinsert(dict, ip);
98da2e3ebdSchin 		return ip->name;
99da2e3ebdSchin 	}
100da2e3ebdSchin 	return name;
101da2e3ebdSchin }
102