summaryrefslogtreecommitdiff
path: root/devdocs/c/program%2Fgetenv.html
blob: 912fe7b3fd0e1fa87c3588e7b19c1ed3635a7b29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    <h1 id="firstHeading" class="firstHeading">getenv, getenv_s</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdlib.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">char *getenv( const char *name );</pre>
</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t getenv_s( size_t *restrict len, char *restrict value,
                  rsize_t valuesz, const char *restrict name );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <div class="t-li1">
<span class="t-li">1)</span> Searches for an environmental variable with name <code>name</code> in the host-specified environment list and returns a pointer to the string that is associated with the matched environment variable. The set of environmental variables and methods of altering it are implementation-defined.</div> <div class="t-li1">
 This function is not required to be thread-safe. Another call to <code>getenv</code>, as well as a call to the POSIX functions <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html"><code>setenv()</code></a>, <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/unsetenv.html"><code>unsetenv()</code></a>, and <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html"><code>putenv()</code></a> may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.</div> <div class="t-li1">
 Modifying the string returned by <code>getenv</code> invokes undefined behavior.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the values of the environment variable is written to the user-provided buffer <code>value</code> (unless null) and the number of bytes written is stored in the user-provided location <code>*len</code> (unless null). If the environment variable is not set in the environment, zero is written to <code>*len</code> (unless null) and <code>'\0'</code> is written to <code>value[0]</code> (unless null). In addition, the following errors are detected at runtime and call the currently installed <a href="../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <dl>
<dd>
<ul>
<li> <code>name</code> is a null pointer </li>
<li> <code>valuesz</code> is greater than <code>RSIZE_MAX</code> </li>
<li> <code>value</code> is a null pointer and <code>valuesz</code> is not zero </li>
</ul> </dd>
<dd>As with all bounds-checked functions, <code>getenv_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../program" title="c/program"><code>&lt;stdlib.h&gt;</code></a>.</dd>
</dl>
</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> name </td> <td> - </td> <td> null-terminated character string identifying the name of the environmental variable to look for </td>
</tr> <tr class="t-par"> <td> len </td> <td> - </td> <td> pointer to a user-provided location where <code>getenv_s</code> will store the length of the environment variable </td>
</tr> <tr class="t-par"> <td> value </td> <td> - </td> <td> pointer to a user-provided character array where <code>getenv_s</code> will store the contents of the environment variable </td>
</tr> <tr class="t-par"> <td> valuesz </td> <td> - </td> <td> maximum number of characters that <code>getenv_s</code> is allowed to write to <code>dest</code> (size of the buffer) </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1">
<span class="t-li">1)</span> character string identifying the value of the environmental variable or null pointer if such variable is not found.</div> <div class="t-li1">
<span class="t-li">2)</span> zero if the environment variable was found, non-zero if it was not found or if a runtime constraint violation occurred. On any error, writes zero to <code>*len</code> (unless <code>len</code> is a null pointer).</div> <h3 id="Notes"> Notes</h3> <p>On POSIX systems, the <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08">environment variables</a> are also accessible through the global variable <code>environ</code>, declared as <code>extern char **environ;</code> in <code>&lt;unistd.h&gt;</code>, and through the optional third argument, <code>envp</code>, of <a href="../language/main_function" title="c/language/main function">the main function</a>.</p>
<p>The call to <code>getenv_s</code> with a null pointer for <code>value</code> and zero for <code>valuesz</code> is used to determine the size of the buffer required to hold the entire result.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
 
int main(void)
{
    const char *name = "PATH";
    const char *env_p = getenv(name);
    if (env_p)
        printf("Your %s is %s\n", name, env_p);
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Your PATH is /home/gamer/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/share/games</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul>
<li> 7.22.4.6 The getenv function (p: TBD) </li>
<li> K.3.6.2.1 The getenv_s function (p: TBD) </li>
</ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.22.4.6 The getenv function (p: 256-257) </li>
<li> K.3.6.2.1 The getenv_s function (p: 440-441) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.22.4.6 The getenv function (p: 352-353) </li>
<li> K.3.6.2.1 The getenv_s function (p: 606-607) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.4.5 The getenv function (p: 317) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.10.4.4 The getenv function </li></ul>
</ul>                      <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/utility/program/getenv" title="cpp/utility/program/getenv">C++ documentation</a></span> for <code>getenv</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/program/getenv" class="_attribution-link">https://en.cppreference.com/w/c/program/getenv</a>
  </p>
</div>