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

NAME

12       getenv — get value of an environment variable
13

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

51       No errors are defined.
52
53       The following sections are informative.
54

EXAMPLES

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

APPLICATION USAGE

68       None.
69

RATIONALE

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

FUTURE DIRECTIONS

128       A future version may add one or more functions to access and modify the
129       environment in a thread-safe manner.
130

SEE ALSO

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