1GETENV(3P)                 POSIX Programmer's Manual                GETENV(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       getenv — get value of an environment variable
14

SYNOPSIS

16       #include <stdlib.h>
17
18       char *getenv(const char *name);
19

DESCRIPTION

21       The functionality described on this reference page is aligned with  the
22       ISO C  standard.  Any  conflict between the requirements described here
23       and the ISO C standard is unintentional. This  volume  of  POSIX.1‐2008
24       defers to the ISO C standard.
25
26       The  getenv()  function  shall  search  the  environment of the calling
27       process (see the Base Definitions volume of  POSIX.1‐2008,  Chapter  8,
28       Environment  Variables)  for the environment variable name if it exists
29       and return a pointer to the value of the environment variable.  If  the
30       specified environment variable cannot be found, a null pointer shall be
31       returned. The application shall ensure that  it  does  not  modify  the
32       string pointed to by the getenv() function.
33
34       The  returned string pointer might be invalidated or the string content
35       might be overwritten  by  a  subsequent  call  to  getenv(),  setenv(),
36       unsetenv(),  or  (if supported) putenv() but they shall not be affected
37       by a call to any other function in this volume of POSIX.1‐2008.
38
39       The getenv() function need not be thread-safe.
40

RETURN VALUE

42       Upon successful completion, getenv() shall return a pointer to a string
43       containing  the  value  for  the specified name.  If the specified name
44       cannot be found in the environment  of  the  calling  process,  a  null
45       pointer shall be returned.
46

ERRORS

48       No errors are defined.
49
50       The following sections are informative.
51

EXAMPLES

53   Getting the Value of an Environment Variable
54       The following example gets the value of the HOME environment variable.
55
56           #include <stdlib.h>
57           ...
58           const char *name = "HOME";
59           char *value;
60
61           value = getenv(name);
62

APPLICATION USAGE

64       None.
65

RATIONALE

67       The clearenv() function was considered but rejected. The putenv() func‐
68       tion has now been included for alignment with the Single UNIX  Specifi‐
69       cation.
70
71       The  getenv() function is inherently not thread-safe because it returns
72       a value pointing to static data.
73
74       Conforming applications are required not to directly modify the  point‐
75       ers  to which environ points, but to use only the setenv(), unsetenv(),
76       and putenv() functions, or assignment to environ itself, to  manipulate
77       the  process  environment. This constraint allows the implementation to
78       properly manage the memory it allocates. This enables  the  implementa‐
79       tion  to  free  any  space it has allocated to strings (and perhaps the
80       pointers to them) stored in environ when unsetenv() is called. A C run‐
81       time start-up procedure (that which invokes main() and perhaps initial‐
82       izes environ) can also initialize a flag indicating that  none  of  the
83       environment has yet been copied to allocated storage, or that the sepa‐
84       rate table has not yet been initialized. If the application switches to
85       a  complete  new  environment by assigning a new value to environ, this
86       can be detected by getenv(), setenv(), unsetenv(), or putenv() and  the
87       implementation can at that point reinitialize based on the new environ‐
88       ment. (This may include copying the  environment  strings  into  a  new
89       array and assigning environ to point to it.)
90
91       In  fact,  for  higher performance of getenv(), implementations that do
92       not provide putenv() could also maintain a separate copy of  the  envi‐
93       ronment  in  a  data structure that could be searched much more quickly
94       (such as an indexed hash table, or a binary tree), and update  both  it
95       and  the linear list at environ when setenv() or unsetenv() is invoked.
96       On implementations that do provide putenv(), such a copy might still be
97       worthwhile  but  would need to allow for the fact that applications can
98       directly modify the content of environment strings added with putenv().
99       For  example,  if  an environment string found by searching the copy is
100       one that was added using putenv(), the  implementation  would  need  to
101       check that the string in environ still has the same name (and value, if
102       the copy includes values), and whenever searching the copy produces  no
103       match  the  implementation  would  then need to search each environment
104       string in environ that was added using putenv() in  case  any  of  them
105       have  changed  their names and now match. Thus, each use of putenv() to
106       add to the environment would reduce the speed advantage of  having  the
107       copy.
108
109       Performance  of  getenv()  can be important for applications which have
110       large numbers of environment variables.  Typically,  applications  like
111       this  use  the  environment as a resource database of user-configurable
112       parameters.  The fact that these variables  are  in  the  user's  shell
113       environment  usually means that any other program that uses environment
114       variables (such as ls, which attempts to use COLUMNS), or really almost
115       any  utility  (LANG, LC_ALL, and so on) is similarly slowed down by the
116       linear search through the variables.
117
118       An implementation that maintains separate data structures, or even  one
119       that  manages  the  memory it consumes, is not currently required as it
120       was thought it would reduce consensus among  implementors  who  do  not
121       want to change their historical implementations.
122

FUTURE DIRECTIONS

124       A future version may add one or more functions to access and modify the
125       environment in a thread-safe manner.
126

SEE ALSO

128       exec, putenv(), setenv(), unsetenv()
129
130       The Base Definitions volume of  POSIX.1‐2008,  Chapter  8,  Environment
131       Variables, <stdlib.h>
132
134       Portions  of  this text are reprinted and reproduced in electronic form
135       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
136       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
137       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
138       cal  and  Electronics  Engineers,  Inc  and  The  Open Group.  (This is
139       POSIX.1-2008 with the 2013 Technical Corrigendum  1  applied.)  In  the
140       event of any discrepancy between this version and the original IEEE and
141       The Open Group Standard, the original IEEE and The Open Group  Standard
142       is  the  referee document. The original Standard can be obtained online
143       at http://www.unix.org/online.html .
144
145       Any typographical or formatting errors that appear  in  this  page  are
146       most likely to have been introduced during the conversion of the source
147       files to man page format. To report such errors,  see  https://www.ker
148       nel.org/doc/man-pages/reporting_bugs.html .
149
150
151
152IEEE/The Open Group                  2013                           GETENV(3P)
Impressum