<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>rpmio/ugid.c Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.2.17 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="modules.html">Modules</a> <a class="qindex" href="annotated.html">Data Structures</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Data Fields</a> <a class="qindex" href="globals.html">Globals</a> <a class="qindex" href="pages.html">Related Pages</a> </center> <hr><h1>rpmio/ugid.c</h1><a href="ugid_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 00005 <span class="preprocessor">#include "<a class="code" href="system_8h.html">system.h</a>"</span> 00006 <span class="preprocessor">#include "<a class="code" href="ugid_8h.html">ugid.h</a>"</span> 00007 <span class="preprocessor">#include "<a class="code" href="debug_8h.html">debug.h</a>"</span> 00008 00009 <span class="comment">/* unameToUid(), uidTouname() and the group variants are really poorly</span> 00010 <span class="comment"> implemented. They really ought to use hash tables. I just made the</span> 00011 <span class="comment"> guess that most files would be owned by root or the same person/group</span> 00012 <span class="comment"> who owned the last file. Those two values are cached, everything else</span> 00013 <span class="comment"> is looked up via getpw() and getgr() functions. If this performs</span> 00014 <span class="comment"> too poorly I'll have to implement it properly :-( */</span> 00015 <a name="l00016"></a><a class="code" href="ugid_8c.html#a0">00016</a> <span class="keywordtype">int</span> <a class="code" href="ugid_8c.html#a0">unameToUid</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> * thisUname, uid_t * uid) 00017 { 00018 <span class="comment">/*@only@*/</span> <span class="keyword">static</span> <span class="keywordtype">char</span> * lastUname = NULL; 00019 <span class="keyword">static</span> size_t lastUnameLen = 0; 00020 <span class="keyword">static</span> size_t lastUnameAlloced; 00021 <span class="keyword">static</span> uid_t lastUid; 00022 <span class="keyword">struct </span>passwd * pwent; 00023 size_t thisUnameLen; 00024 00025 <span class="keywordflow">if</span> (!thisUname) { 00026 lastUnameLen = 0; 00027 <span class="keywordflow">return</span> -1; 00028 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(thisUname, <span class="stringliteral">"root"</span>) == 0) { 00029 *uid = 0; 00030 <span class="keywordflow">return</span> 0; 00031 } 00032 00033 thisUnameLen = strlen(thisUname); 00034 <span class="keywordflow">if</span> (lastUname == NULL || thisUnameLen != lastUnameLen || 00035 strcmp(thisUname, lastUname) != 0) { 00036 <span class="keywordflow">if</span> (lastUnameAlloced < thisUnameLen + 1) { 00037 lastUnameAlloced = thisUnameLen + 10; 00038 lastUname = <a class="code" href="rpmmalloc_8c.html#a3">xrealloc</a>(lastUname, lastUnameAlloced); <span class="comment">/* XXX memory leak */</span> 00039 } 00040 strcpy(lastUname, thisUname); 00041 00042 pwent = getpwnam(thisUname); 00043 <span class="keywordflow">if</span> (pwent == NULL) { 00044 <span class="comment">/*@-internalglobs@*/</span> <span class="comment">/* FIX: shrug */</span> 00045 endpwent(); 00046 <span class="comment">/*@=internalglobs@*/</span> 00047 pwent = getpwnam(thisUname); 00048 <span class="keywordflow">if</span> (pwent == NULL) <span class="keywordflow">return</span> -1; 00049 } 00050 00051 lastUid = pwent->pw_uid; 00052 } 00053 00054 *uid = lastUid; 00055 00056 <span class="keywordflow">return</span> 0; 00057 } 00058 <a name="l00059"></a><a class="code" href="ugid_8c.html#a1">00059</a> <span class="keywordtype">int</span> <a class="code" href="ugid_8c.html#a1">gnameToGid</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> * thisGname, gid_t * gid) 00060 { 00061 <span class="comment">/*@only@*/</span> <span class="keyword">static</span> <span class="keywordtype">char</span> * lastGname = NULL; 00062 <span class="keyword">static</span> size_t lastGnameLen = 0; 00063 <span class="keyword">static</span> size_t lastGnameAlloced; 00064 <span class="keyword">static</span> gid_t lastGid; 00065 size_t thisGnameLen; 00066 <span class="keyword">struct </span>group * grent; 00067 00068 <span class="keywordflow">if</span> (thisGname == NULL) { 00069 lastGnameLen = 0; 00070 <span class="keywordflow">return</span> -1; 00071 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(thisGname, <span class="stringliteral">"root"</span>) == 0) { 00072 *gid = 0; 00073 <span class="keywordflow">return</span> 0; 00074 } 00075 00076 thisGnameLen = strlen(thisGname); 00077 <span class="keywordflow">if</span> (lastGname == NULL || thisGnameLen != lastGnameLen || 00078 strcmp(thisGname, lastGname) != 0) 00079 { 00080 <span class="keywordflow">if</span> (lastGnameAlloced < thisGnameLen + 1) { 00081 lastGnameAlloced = thisGnameLen + 10; 00082 lastGname = <a class="code" href="rpmmalloc_8c.html#a3">xrealloc</a>(lastGname, lastGnameAlloced); <span class="comment">/* XXX memory leak */</span> 00083 } 00084 strcpy(lastGname, thisGname); 00085 00086 grent = getgrnam(thisGname); 00087 <span class="keywordflow">if</span> (grent == NULL) { 00088 <span class="comment">/*@-internalglobs@*/</span> <span class="comment">/* FIX: shrug */</span> 00089 endgrent(); 00090 <span class="comment">/*@=internalglobs@*/</span> 00091 grent = getgrnam(thisGname); 00092 <span class="keywordflow">if</span> (grent == NULL) <span class="keywordflow">return</span> -1; 00093 } 00094 lastGid = grent->gr_gid; 00095 } 00096 00097 *gid = lastGid; 00098 00099 <span class="keywordflow">return</span> 0; 00100 } 00101 <a name="l00102"></a><a class="code" href="ugid_8c.html#a2">00102</a> <span class="keywordtype">char</span> * <a class="code" href="ugid_8c.html#a2">uidToUname</a>(uid_t uid) 00103 { 00104 <span class="keyword">static</span> uid_t lastUid = (uid_t) -1; 00105 <span class="comment">/*@only@*/</span> <span class="keyword">static</span> <span class="keywordtype">char</span> * lastUname = NULL; 00106 <span class="keyword">static</span> size_t lastUnameLen = 0; 00107 00108 <span class="keywordflow">if</span> (uid == (uid_t) -1) { 00109 lastUid = (uid_t) -1; 00110 <span class="keywordflow">return</span> NULL; 00111 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (uid == (uid_t) 0) { 00112 <span class="keywordflow">return</span> <span class="stringliteral">"root"</span>; 00113 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (uid == lastUid) { 00114 <span class="keywordflow">return</span> lastUname; 00115 } <span class="keywordflow">else</span> { 00116 <span class="keyword">struct </span>passwd * pwent = getpwuid(uid); 00117 size_t len; 00118 00119 <span class="keywordflow">if</span> (pwent == NULL) <span class="keywordflow">return</span> NULL; 00120 00121 lastUid = uid; 00122 len = strlen(pwent->pw_name); 00123 <span class="keywordflow">if</span> (lastUnameLen < len + 1) { 00124 lastUnameLen = len + 20; 00125 lastUname = <a class="code" href="rpmmalloc_8c.html#a3">xrealloc</a>(lastUname, lastUnameLen); 00126 } 00127 strcpy(lastUname, pwent->pw_name); 00128 00129 <span class="keywordflow">return</span> lastUname; 00130 } 00131 } 00132 <a name="l00133"></a><a class="code" href="ugid_8c.html#a3">00133</a> <span class="keywordtype">char</span> * <a class="code" href="ugid_8c.html#a3">gidToGname</a>(gid_t gid) 00134 { 00135 <span class="keyword">static</span> gid_t lastGid = (gid_t) -1; 00136 <span class="comment">/*@only@*/</span> <span class="keyword">static</span> <span class="keywordtype">char</span> * lastGname = NULL; 00137 <span class="keyword">static</span> size_t lastGnameLen = 0; 00138 00139 <span class="keywordflow">if</span> (gid == (gid_t) -1) { 00140 lastGid = (gid_t) -1; 00141 <span class="keywordflow">return</span> NULL; 00142 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (gid == (gid_t) 0) { 00143 <span class="keywordflow">return</span> <span class="stringliteral">"root"</span>; 00144 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (gid == lastGid) { 00145 <span class="keywordflow">return</span> lastGname; 00146 } <span class="keywordflow">else</span> { 00147 <span class="keyword">struct </span>group * grent = getgrgid(gid); 00148 size_t len; 00149 00150 <span class="keywordflow">if</span> (grent == NULL) <span class="keywordflow">return</span> NULL; 00151 00152 lastGid = gid; 00153 len = strlen(grent->gr_name); 00154 <span class="keywordflow">if</span> (lastGnameLen < len + 1) { 00155 lastGnameLen = len + 20; 00156 lastGname = <a class="code" href="rpmmalloc_8c.html#a3">xrealloc</a>(lastGname, lastGnameLen); 00157 } 00158 strcpy(lastGname, grent->gr_name); 00159 00160 <span class="keywordflow">return</span> lastGname; 00161 } 00162 } </pre></div><hr><address style="align: right;"><small>Generated on Thu Sep 12 22:15:02 2002 for rpm by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.17 </small></address> </body> </html>