14887Schin /*********************************************************************** 24887Schin * * 34887Schin * This software is part of the ast package * 4*8462SApril.Chin@Sun.COM * Copyright (c) 1985-2008 AT&T Intellectual Property * 54887Schin * and is licensed under the * 64887Schin * Common Public License, Version 1.0 * 7*8462SApril.Chin@Sun.COM * by AT&T Intellectual Property * 84887Schin * * 94887Schin * A copy of the License is available at * 104887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 124887Schin * * 134887Schin * Information and Software Systems Research * 144887Schin * AT&T Research * 154887Schin * Florham Park NJ * 164887Schin * * 174887Schin * Glenn Fowler <gsf@research.att.com> * 184887Schin * David Korn <dgk@research.att.com> * 194887Schin * Phong Vo <kpv@research.att.com> * 204887Schin * * 214887Schin ***********************************************************************/ 224887Schin #pragma prototyped 234887Schin /* 244887Schin * Glenn Fowler 254887Schin * AT&T Bell Laboratories 264887Schin * 274887Schin * uid number -> name 284887Schin */ 294887Schin 304887Schin #if defined(__STDPP__directive) && defined(__STDPP__hide) 314887Schin __STDPP__directive pragma pp:hide getpwuid 324887Schin #else 334887Schin #define getpwuid ______getpwuid 344887Schin #endif 354887Schin 364887Schin #include <ast.h> 374887Schin #include <cdt.h> 384887Schin #include <pwd.h> 394887Schin 404887Schin #if defined(__STDPP__directive) && defined(__STDPP__hide) 414887Schin __STDPP__directive pragma pp:nohide getpwuid 424887Schin #else 434887Schin #undef getpwuid 444887Schin #endif 454887Schin 464887Schin extern struct passwd* getpwuid(uid_t); 474887Schin 484887Schin typedef struct Id_s 494887Schin { 504887Schin Dtlink_t link; 514887Schin int id; 524887Schin char name[1]; 534887Schin } Id_t; 544887Schin 554887Schin /* 564887Schin * return uid name given uid number 574887Schin */ 584887Schin 594887Schin char* 604887Schin fmtuid(int uid) 614887Schin { 624887Schin register Id_t* ip; 634887Schin register char* name; 644887Schin register struct passwd* pw; 654887Schin int z; 664887Schin 674887Schin static Dt_t* dict; 684887Schin static Dtdisc_t disc; 694887Schin 704887Schin if (!dict) 714887Schin { 724887Schin disc.key = offsetof(Id_t, id); 734887Schin disc.size = sizeof(int); 744887Schin dict = dtopen(&disc, Dthash); 754887Schin } 764887Schin else if (ip = (Id_t*)dtmatch(dict, &uid)) 774887Schin return ip->name; 784887Schin if (pw = getpwuid(uid)) 794887Schin { 804887Schin name = pw->pw_name; 814887Schin #if _WINIX 824887Schin if (streq(name, "Administrator")) 834887Schin name = "root"; 844887Schin #endif 854887Schin } 864887Schin else if (uid == 0) 874887Schin name = "root"; 884887Schin else 894887Schin { 904887Schin name = fmtbuf(z = sizeof(uid) * 3 + 1); 914887Schin sfsprintf(name, z, "%I*d", sizeof(uid), uid); 924887Schin } 934887Schin if (dict && (ip = newof(0, Id_t, 1, strlen(name)))) 944887Schin { 954887Schin ip->id = uid; 964887Schin strcpy(ip->name, name); 974887Schin dtinsert(dict, ip); 984887Schin return ip->name; 994887Schin } 1004887Schin return name; 1014887Schin } 102