Files
2024-02-19 00:21:47 -05:00

244 lines
9.6 KiB
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 The Open Group, All Rights Reserved -->
<title>getpwnam</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="getpwnam"></a> <a name="tag_03_244"></a><!-- getpwnam -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_244_01"></a>NAME</h4>
<blockquote>getpwnam, getpwnam_r - search user database for a name</blockquote>
<h4><a name="tag_03_244_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/pwd.h.html">pwd.h</a>&gt;<br>
<br>
struct passwd *getpwnam(const char *</tt><i>name</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 getpwnam_r(const char *</tt><i>name</i><tt>, struct passwd *</tt><i>pwd</i><tt>, char
*</tt><i>buffer</i><tt>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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_244_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>getpwnam</i>() function shall search the user database for an entry with a matching <i>name</i>.</p>
<p>The <i>getpwnam</i>() function need not be reentrant. A function that is not required to be reentrant is not required to be
thread-safe.</p>
<p>Applications wishing to check for error situations should set <i>errno</i> to 0 before calling <i>getpwnam</i>(). If
<i>getpwnam</i>() returns a null pointer and <i>errno</i> is 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>getpwnam_r</i>() function shall update the <b>passwd</b> structure pointed to by <i>pwd</i> and store a pointer to that
structure at the location pointed to by <i>result</i>. The structure shall contain an entry from the user database with a matching
<i>name</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_244_04"></a>RETURN VALUE</h4>
<blockquote>
<p>The <i>getpwnam</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>&lt;pwd.h&gt;</i></a> with a matching entry if found. A null pointer shall be returned if the requested
entry 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>, <i>getpwnam</i>(), or <a href=
"../functions/getpwuid.html"><i>getpwuid</i>()</a>.</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>getpwnam_r</i>() function shall return zero; otherwise, an error number shall be returned to indicate the
error. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
</blockquote>
<h4><a name="tag_03_244_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>getpwnam</i>() and <i>getpwnam_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>getpwnam</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>getpwnam_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_244_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_244_06_01"></a>Getting an Entry for the Login Name</h5>
<p>The following example uses the <a href="../functions/getlogin.html"><i>getlogin</i>()</a> function to return the name of the
user who logged in; this information is passed to the <i>getpwnam</i>() function to get the user database entry for that user.</p>
<pre>
<tt>#include &lt;sys/types.h&gt;
#include &lt;pwd.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
...
char *lgn;
struct passwd *pw;
...
if ((lgn = getlogin()) == NULL || (pw = getpwnam(lgn)) == NULL) {
fprintf(stderr, "Get of user information failed.\n"); exit(1);
}
...
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_244_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 the
process.</p>
<p>The <i>getpwnam_r</i>() function is thread-safe and returns values in a user-supplied buffer instead of possibly using a static
data area that may be overwritten by each call.</p>
</blockquote>
<h4><a name="tag_03_244_08"></a>RATIONALE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_244_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_244_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="getpwuid.html"><i>getpwuid</i>()</a> , the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/limits.h.html"><i>&lt;limits.h&gt;</i></a>, <a href="../basedefs/pwd.h.html"><i>&lt;pwd.h&gt;</i></a>, <a href=
"../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_244_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_244_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>getpwnam_r</i>() function is included for alignment with the POSIX Threads Extension.</p>
<p>A note indicating that the <i>getpwnam</i>() function need not be reentrant is added to the DESCRIPTION.</p>
</blockquote>
<h4><a name="tag_03_244_13"></a>Issue 6</h4>
<blockquote>
<p>The <i>getpwnam_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>name</i>.</p>
<p>In the SYNOPSIS, the optional include of the <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> header is
removed.</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>&lt;sys/types.h&gt;</i></a> has been removed. Although <a
href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> was required for conforming implementations of previous POSIX
specifications, 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 [EMFILE], [ENFILE], and [ENXIO] 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 a
static 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 &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; 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>