214 lines
10 KiB
HTML
214 lines
10 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>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-2001<br>
|
|
Copyright © 2001 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 <<a href="../basedefs/stdlib.h.html">stdlib.h</a>><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 C standard. Any conflict between the
|
|
requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to
|
|
the ISO 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 of
|
|
IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap08.html">Chapter 8, Environment Variables</a>) for the environment
|
|
variable <i>name</i> if it exists and return a pointer to the value of the environment variable. If the specified environment
|
|
variable cannot be found, a null pointer shall be returned. The application shall ensure that it does not modify the string pointed
|
|
to 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 volume
|
|
of IEEE Std 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"> If
|
|
the 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 be
|
|
thread-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 be
|
|
returned.</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"> <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"></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 by
|
|
subsequent 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 <stdlib.h>
|
|
...
|
|
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> function
|
|
has 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 to
|
|
manipulate the process environment as an abstract object. Thus, the implementation of the environment access functions has complete
|
|
control over the data structure used to represent the environment (subject to the requirement that <i>environ</i> be maintained as
|
|
a list of strings with embedded equal signs for applications that wish to scan the environment). This constraint allows the
|
|
implementation to properly manage the memory it allocates, either by using allocated storage for all variables (copying them on the
|
|
first 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 which
|
|
are not, via a separate table or some other means. This enables the implementation to free any allocated space used by strings (and
|
|
perhaps the pointers to them) stored in <i>environ</i> when <a href="../functions/unsetenv.html"><i>unsetenv</i>()</a> is called. A
|
|
C runtime start-up procedure (that which invokes <i>main</i>() and perhaps initializes <i>environ</i>) can also initialize a flag
|
|
indicating that none of the environment has yet been copied to allocated storage, or that the separate table has not yet been
|
|
initialized.</p>
|
|
|
|
<p>In fact, for higher performance of <i>getenv</i>(), the implementation could also maintain a separate copy of the environment in
|
|
a data structure that could be searched much more quickly (such as an indexed hash table, or a binary tree), and update both it and
|
|
the 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 variables
|
|
are 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 currently
|
|
required as it was thought it would reduce consensus among implementors who do not want to change their historical
|
|
implementations.</p>
|
|
|
|
<p>The POSIX Threads Extension states that multi-threaded applications must not modify <i>environ</i> directly, and that
|
|
IEEE Std 1003.1-2001 is providing functions which such applications can use in the future to manipulate the environment
|
|
in 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> , <a
|
|
href="setenv.html"><i>setenv</i>()</a> , <a href="unsetenv.html"><i>unsetenv</i>()</a> , the Base Definitions volume of
|
|
IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap08.html">Chapter 8, Environment Variables</a>, <a href=
|
|
"../basedefs/stdlib.h.html"><i><stdlib.h></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 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 "must" for application requirements.</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>
|
|
|