📄 getpwuid.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>getpwuid</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="getpwuid"></a> <a name="tag_03_245"></a><!-- getpwuid --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_245_01"></a>NAME</h4><blockquote>getpwuid, getpwuid_r - search user database for a user ID</blockquote><h4><a name="tag_03_245_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include <<a href="../basedefs/pwd.h.html">pwd.h</a>><br><br> struct passwd *getpwuid(uid_t</tt> <i>uid</i><tt>);<br></tt></code></p><div class="box"><code><tt><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> int getpwuid_r(uid_t</tt> <i>uid</i><tt>, struct passwd *</tt><i>pwd</i><tt>, char*</tt><i>buffer</i><tt>,<br> size_t</tt> <i>bufsize</i><tt>, struct passwd **</tt><i>result</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_245_03"></a>DESCRIPTION</h4><blockquote><p>The <i>getpwuid</i>() function shall search the user database for an entry with a matching <i>uid</i>.</p><p>The <i>getpwuid</i>() function need not be reentrant. A function that is not required to be reentrant is not required to bethread-safe.</p><p>Applications wishing to check for error situations should set <i>errno</i> to 0 before calling <i>getpwuid</i>(). If<i>getpwuid</i>() returns a null pointer and <i>errno</i> is set to non-zero, an error occurred.</p><p><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The <i>getpwuid_r</i>() function shall update the <b>passwd</b> structure pointed to by <i>pwd</i> and store a pointer to thatstructure at the location pointed to by <i>result</i>. The structure shall contain an entry from the user database with a matching<i>uid</i>. Storage referenced by the structure is allocated from the memory provided with the <i>buffer</i> parameter, which is<i>bufsize</i> bytes in size. The maximum size needed for this buffer can be determined with the {_SC_GETPW_R_SIZE_MAX} <a href="../functions/sysconf.html"><i>sysconf</i>()</a> parameter. A NULL pointer shall be returned at the location pointed to by<i>result</i> on error or if the requested entry is not found. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_245_04"></a>RETURN VALUE</h4><blockquote><p>The <i>getpwuid</i>() function shall return a pointer to a <b>struct passwd</b> with the structure as defined in <a href="../basedefs/pwd.h.html"><i><pwd.h></i></a> with a matching entry if found. A null pointer shall be returned if the requestedentry is not found, or an error occurs. On error, <i>errno</i> shall be set to indicate the error.</p><p>The return value may point to a static area which is overwritten by a subsequent call to <a href="../functions/getpwent.html"><i>getpwent</i>()</a>, <a href="../functions/getpwnam.html"><i>getpwnam</i>()</a>, or<i>getpwuid</i>().</p><p><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If successful, the <i>getpwuid_r</i>() function shall return zero; otherwise, an error number shall be returned to indicate theerror. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_245_05"></a>ERRORS</h4><blockquote><p>The <i>getpwuid</i>() and <i>getpwuid_r</i>() functions may fail if:</p><dl compact><dt>[EIO]</dt><dd>An I/O error has occurred.</dd><dt>[EINTR]</dt><dd>A signal was caught during <i>getpwuid</i>().</dd><dt>[EMFILE]</dt><dd>{OPEN_MAX} file descriptors are currently open in the calling process.</dd><dt>[ENFILE]</dt><dd>The maximum allowable number of files is currently open in the system.</dd></dl><p>The <i>getpwuid_r</i>() function may fail if:</p><dl compact><dt>[ERANGE]</dt><dd><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Insufficient storage was supplied via <i>buffer</i> and <i>bufsize</i> to contain the data to be referenced by the resulting<b>passwd</b> structure. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_245_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_245_06_01"></a>Getting an Entry for the Root User</h5><p>The following example gets the user database entry for the user with user ID 0 (root).</p><pre><tt>#include <sys/types.h>#include <pwd.h>...uid_t id = 0;struct passwd *pwd;<br>pwd = getpwuid(id);</tt></pre><h5><a name="tag_03_245_06_02"></a>Finding the Name for the Effective User ID</h5><p>The following example defines <i>pws</i> as a pointer to a structure of type <b>passwd</b>, which is used to store the structurepointer returned by the call to the <i>getpwuid</i>() function. The <a href="../functions/geteuid.html"><i>geteuid</i>()</a>function shall return the effective user ID of the calling process; this is used as the search criteria for the <i>getpwuid</i>()function. The call to <i>getpwuid</i>() shall return a pointer to the structure containing that user ID value.</p><pre><tt>#include <unistd.h>#include <sys/types.h>#include <pwd.h>...struct passwd *pws;pws = getpwuid(geteuid());</tt></pre><h5><a name="tag_03_245_06_03"></a>Finding an Entry in the User Database</h5><p>The following example uses <i>getpwuid</i>() to search the user database for a user ID that was previously stored in a<b>stat</b> structure, then prints out the user name if it is found. If the user is not found, the program prints the numeric valueof the user ID for the entry.</p><pre><tt>#include <sys/types.h>#include <pwd.h>#include <stdio.h>...struct stat statbuf;struct passwd *pwd;...if ((pwd = getpwuid(statbuf.st_uid)) != NULL) printf(" %-8.8s", pwd->pw_name);else printf(" %-8d", statbuf.st_uid);</tt></pre></blockquote><h4><a name="tag_03_245_07"></a>APPLICATION USAGE</h4><blockquote><p>Three names associated with the current process can be determined: <i>getpwuid</i>( <a href="../functions/geteuid.html"><i>geteuid</i>()</a>) returns the name associated with the effective user ID of the process; <a href="../functions/getlogin.html"><i>getlogin</i>()</a> returns the name associated with the current login activity; and<i>getpwuid</i>( <a href="../functions/getuid.html"><i>getuid</i>()</a>) returns the name associated with the real user ID of theprocess.</p><p>The <i>getpwuid_r</i>() function is thread-safe and returns values in a user-supplied buffer instead of possibly using a staticdata area that may be overwritten by each call.</p></blockquote><h4><a name="tag_03_245_08"></a>RATIONALE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_245_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_245_10"></a>SEE ALSO</h4><blockquote><p><a href="getpwnam.html"><i>getpwnam</i>()</a> , <a href="geteuid.html"><i>geteuid</i>()</a> , <a href="getuid.html"><i>getuid</i>()</a> , <a href="getlogin.html"><i>getlogin</i>()</a> , the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/limits.h.html"><i><limits.h></i></a>, <a href="../basedefs/pwd.h.html"><i><pwd.h></i></a>, <a href="../basedefs/sys/types.h.html"><i><sys/types.h></i></a></p></blockquote><h4><a name="tag_03_245_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from System V Release 2.0.</p></blockquote><h4><a name="tag_03_245_12"></a>Issue 5</h4><blockquote><p>Normative text previously in the APPLICATION USAGE section is moved to the RETURN VALUE section.</p><p>The <i>getpwuid_r</i>() function is included for alignment with the POSIX Threads Extension.</p><p>A note indicating that the <i>getpwuid</i>() function need not be reentrant is added to the DESCRIPTION.</p></blockquote><h4><a name="tag_03_245_13"></a>Issue 6</h4><blockquote><p>The <i>getpwuid_r</i>() function is marked as part of the Thread-Safe Functions option.</p><p>The Open Group Corrigendum U028/3 is applied, correcting text in the DESCRIPTION describing matching the <i>uid</i>.</p><p>In the SYNOPSIS, the optional include of the <a href="../basedefs/sys/types.h.html"><i><sys/types.h></i></a> header isremoved.</p><p>In the DESCRIPTION, the note about reentrancy is expanded to cover thread-safety.</p><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>The requirement to include <a href="../basedefs/sys/types.h.html"><i><sys/types.h></i></a> has been removed. Although <ahref="../basedefs/sys/types.h.html"><i><sys/types.h></i></a> was required for conforming implementations of previous POSIXspecifications, it was not required for UNIX applications.</p></li><li><p>In the RETURN VALUE section, the requirement to set <i>errno</i> on error is added.</p></li><li><p>The [EIO], [EINTR], [EMFILE], and [ENFILE] optional error conditions are added.</p></li></ul><p>The APPLICATION USAGE section is updated to include a note on the thread-safe function and its avoidance of possibly using astatic data area.</p><p>IEEE PASC Interpretation 1003.1 #116 is applied, changing the description of the size of the buffer from <i>bufsize</i>characters to bytes.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -