⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getenv.html

📁 posix标准英文,html格式
💻 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-2004 IEEE and The Open Group, All Rights Reserved --><title>getenv</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="getenv"></a> <a name="tag_03_218"></a><!-- getenv --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright &copy; 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_218_01"></a>NAME</h4><blockquote>getenv - get value of an environment variable</blockquote><h4><a name="tag_03_218_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include &lt;<a href="../basedefs/stdlib.h.html">stdlib.h</a>&gt;<br><br> char *getenv(const char *</tt><i>name</i><tt>);<br></tt></code></p></blockquote><h4><a name="tag_03_218_03"></a>DESCRIPTION</h4><blockquote><div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any conflict between therequirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers tothe ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div><p>The <i>getenv</i>() function shall search the environment of the calling process (see the Base Definitions volume ofIEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html">Chapter 8, Environment Variables</a>) for the environmentvariable <i>name</i> if it exists and return a pointer to the value of the environment variable. If the specified environmentvariable cannot be found, a null pointer shall be returned. The application shall ensure that it does not modify the string pointedto by the <i>getenv</i>() function.</p><p>The string pointed to may be overwritten by a subsequent call to <i>getenv</i>(), <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> <a href="../functions/setenv.html"><i>setenv</i>()</a>, or <a href="../functions/unsetenv.html"><i>unsetenv</i>()</a>, <img src="../images/opt-end.gif" alt="[Option End]" border="0"> but shall not be overwritten by a call to any other function in this volumeof IEEE&nbsp;Std&nbsp;1003.1-2001.</p><p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Ifthe application modifies <i>environ</i> or the pointers to which it points, the behavior of <i>getenv</i>() is undefined.</p><p>The <i>getenv</i>() function need not be reentrant. A function that is not required to be reentrant is not required to bethread-safe. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_218_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>getenv</i>() shall return a pointer to a string containing the <i>value</i> for the specified<i>name</i>. If the specified <i>name</i> cannot be found in the environment of the calling process, a null pointer shall bereturned.</p><p>The return value from <i>getenv</i>() may point to static data which may be overwritten by subsequent calls to <i>getenv</i>(),<sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> <ahref="../functions/setenv.html"><i>setenv</i>()</a>, or <a href="../functions/unsetenv.html"><i>unsetenv</i>()</a>. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">On XSI-conformant systems, the return value from <i>getenv</i>() may point to static data which may also be overwritten bysubsequent calls to <a href="../functions/putenv.html"><i>putenv</i>()</a>. <img src="../images/opt-end.gif" alt="[Option End]"border="0"></p></blockquote><h4><a name="tag_03_218_05"></a>ERRORS</h4><blockquote><p>No errors are defined.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_218_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_218_06_01"></a>Getting the Value of an Environment Variable</h5><p>The following example gets the value of the <i>HOME</i> environment variable.</p><pre><tt>#include &lt;stdlib.h&gt;...const char *name = "HOME";char *value;<br>value = getenv(name);</tt></pre></blockquote><h4><a name="tag_03_218_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_218_08"></a>RATIONALE</h4><blockquote><p>The <i>clearenv</i>() function was considered but rejected. The <a href="../functions/putenv.html"><i>putenv</i>()</a> functionhas now been included for alignment with the Single UNIX Specification.</p><p>The <i>getenv</i>() function is inherently not reentrant because it returns a value pointing to static data.</p><p>Conforming applications are required not to modify <i>environ</i> directly, but to use only the functions described here tomanipulate the process environment as an abstract object. Thus, the implementation of the environment access functions has completecontrol over the data structure used to represent the environment (subject to the requirement that <i>environ</i> be maintained asa list of strings with embedded equal signs for applications that wish to scan the environment). This constraint allows theimplementation to properly manage the memory it allocates, either by using allocated storage for all variables (copying them on thefirst invocation of <a href="../functions/setenv.html"><i>setenv</i>()</a> or <a href="../functions/unsetenv.html"><i>unsetenv</i>()</a>), or keeping track of which strings are currently in allocated space and whichare not, via a separate table or some other means. This enables the implementation to free any allocated space used by strings (andperhaps the pointers to them) stored in <i>environ</i> when <a href="../functions/unsetenv.html"><i>unsetenv</i>()</a> is called. AC runtime start-up procedure (that which invokes <i>main</i>() and perhaps initializes <i>environ</i>) can also initialize a flagindicating that none of the environment has yet been copied to allocated storage, or that the separate table has not yet beeninitialized.</p><p>In fact, for higher performance of <i>getenv</i>(), the implementation could also maintain a separate copy of the environment ina data structure that could be searched much more quickly (such as an indexed hash table, or a binary tree), and update both it andthe linear list at <i>environ</i> when <a href="../functions/setenv.html"><i>setenv</i>()</a> or <a href="../functions/unsetenv.html"><i>unsetenv</i>()</a> is invoked.</p><p>Performance of <i>getenv</i>() can be important for applications which have large numbers of environment variables. Typically,applications like this use the environment as a resource database of user-configurable parameters. The fact that these variablesare in the user's shell environment usually means that any other program that uses environment variables (such as <a href="../utilities/ls.html"><i>ls</i></a>, which attempts to use <i>COLUMNS ),</i> or really almost any utility ( <i>LANG ,</i><i>LC_ALL ,</i> and so on) is similarly slowed down by the linear search through the variables.</p><p>An implementation that maintains separate data structures, or even one that manages the memory it consumes, is not currentlyrequired as it was thought it would reduce consensus among implementors who do not want to change their historicalimplementations.</p><p>The POSIX Threads Extension states that multi-threaded applications must not modify <i>environ</i> directly, and thatIEEE&nbsp;Std&nbsp;1003.1-2001 is providing functions which such applications can use in the future to manipulate the environmentin a thread-safe manner. Thus, moving away from application use of <i>environ</i> is desirable from that standpoint as well.</p></blockquote><h4><a name="tag_03_218_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_218_10"></a>SEE ALSO</h4><blockquote><p><a href="exec.html"><i><a href="../functions/exec.html">exec</a></i>()</a>, <a href="putenv.html"><i>putenv</i>()</a>, <ahref="setenv.html"><i>setenv</i>()</a>, <a href="unsetenv.html"><i>unsetenv</i>()</a>, the Base Definitions volume ofIEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html">Chapter 8, Environment Variables</a>, <a href="../basedefs/stdlib.h.html"><i>&lt;stdlib.h&gt;</i></a></p></blockquote><h4><a name="tag_03_218_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_218_12"></a>Issue 5</h4><blockquote><p>Normative text previously in the APPLICATION USAGE section is moved to the RETURN VALUE section.</p><p>A note indicating that this function need not be reentrant is added to the DESCRIPTION.</p></blockquote><h4><a name="tag_03_218_13"></a>Issue 6</h4><blockquote><p>The following changes were made to align with the IEEE&nbsp;P1003.1a draft standard:</p><ul><li><p>References added to the new <a href="../functions/setenv.html"><i>setenv</i>()</a> and <a href="../functions/unsetenv.html"><i>unsetenv</i>()</a> functions.</p></li></ul><p>The DESCRIPTION is updated to avoid use of the term &quot;must&quot; for application requirements.</p></blockquote><div class="box"><em>End of informative text.</em></div><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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -