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

NAME

6       getenv - get value of an environment variable
7

SYNOPSIS

9       #include <stdlib.h>
10
11       char *getenv(const char *name);
12
13

DESCRIPTION

15       The  getenv()  function  shall  search  the  environment of the calling
16       process (see the Base Definitions volume of IEEE Std 1003.1-2001, Chap‐
17       ter  8,  Environment Variables) for the environment variable name if it
18       exists and return a pointer to the value of the  environment  variable.
19       If  the  specified environment variable cannot be found, a null pointer
20       shall be returned. The application shall ensure that it does not modify
21       the string pointed to by the getenv() function.
22
23       The  string  pointed  to  may  be  overwritten  by a subsequent call to
24       getenv(), setenv(), or unsetenv(),  but shall not be overwritten  by  a
25       call to any other function in this volume of IEEE Std 1003.1-2001.
26
27       If the application modifies environ or the pointers to which it points,
28       the behavior of getenv() is undefined.
29
30       The getenv() function need not be reentrant. A  function  that  is  not
31       required to be reentrant is not required to be thread-safe.
32

RETURN VALUE

34       Upon successful completion, getenv() shall return a pointer to a string
35       containing the value for the specified name. If the specified name can‐
36       not  be found in the environment of the calling process, a null pointer
37       shall be returned.
38
39       The return value from getenv() may point to static data  which  may  be
40       overwritten by subsequent calls to getenv(), setenv(), or unsetenv().
41
42       On  XSI-conformant systems, the return value from getenv() may point to
43       static data which may  also  be  overwritten  by  subsequent  calls  to
44       putenv().
45

ERRORS

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

EXAMPLES

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

APPLICATION USAGE

65       None.
66

RATIONALE

68       The clearenv() function was considered but rejected. The putenv() func‐
69       tion has now been included for alignment with the Single UNIX  Specifi‐
70       cation.
71
72       The  getenv() function is inherently not reentrant because it returns a
73       value pointing to static data.
74
75       Conforming applications are required not to  modify  environ  directly,
76       but  to use only the functions described here to manipulate the process
77       environment as an abstract object.  Thus,  the  implementation  of  the
78       environment  access functions has complete control over the data struc‐
79       ture used to represent the environment (subject to the requirement that
80       environ  be  maintained  as a list of strings with embedded equal signs
81       for applications that wish to scan the  environment).  This  constraint
82       allows  the  implementation to properly manage the memory it allocates,
83       either by using allocated storage for all variables  (copying  them  on
84       the  first  invocation  of setenv() or unsetenv()), or keeping track of
85       which strings are currently in allocated space and which are not, via a
86       separate  table or some other means. This enables the implementation to
87       free any allocated space used by strings (and perhaps the  pointers  to
88       them) stored in environ when unsetenv() is called. A C runtime start-up
89       procedure (that which invokes main() and perhaps  initializes  environ)
90       can  also initialize a flag indicating that none of the environment has
91       yet been copied to allocated storage, or that the  separate  table  has
92       not yet been initialized.
93
94       In  fact,  for higher performance of getenv(), the implementation could
95       also maintain a separate copy of the environment in  a  data  structure
96       that  could  be searched much more quickly (such as an indexed hash ta‐
97       ble, or a binary tree), and update both it and the linear list at envi‐
98       ron when setenv() or unsetenv() is invoked.
99
100       Performance  of  getenv()  can be important for applications which have
101       large numbers of environment variables.  Typically,  applications  like
102       this  use  the  environment as a resource database of user-configurable
103       parameters. The fact that these variables are in the user's shell envi‐
104       ronment  usually  means  that  any  other program that uses environment
105       variables (such as ls, which attempts  to  use  COLUMNS  ),  or  really
106       almost  any  utility  (  LANG , LC_ALL , and so on) is similarly slowed
107       down by the linear search through the variables.
108
109       An implementation that maintains separate data structures, or even  one
110       that  manages  the  memory it consumes, is not currently required as it
111       was thought it would reduce consensus among  implementors  who  do  not
112       want to change their historical implementations.
113
114       The  POSIX  Threads  Extension  states that multi-threaded applications
115       must not modify environ directly, and that IEEE Std 1003.1-2001 is pro‐
116       viding  functions  which  such  applications  can  use in the future to
117       manipulate the environment in a thread-safe manner. Thus,  moving  away
118       from  application  use  of environ is desirable from that standpoint as
119       well.
120

FUTURE DIRECTIONS

122       None.
123

SEE ALSO

125       exec() , putenv() , setenv() , unsetenv() , the Base Definitions volume
126       of IEEE Std 1003.1-2001, Chapter 8, Environment Variables, <stdlib.h>
127
129       Portions  of  this text are reprinted and reproduced in electronic form
130       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
131       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
132       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
133       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
134       event of any discrepancy between this version and the original IEEE and
135       The  Open Group Standard, the original IEEE and The Open Group Standard
136       is the referee document. The original Standard can be  obtained  online
137       at http://www.opengroup.org/unix/online.html .
138
139
140
141IEEE/The Open Group                  2003                            GETENV(P)
Impressum