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
19

DESCRIPTION

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

RETURN VALUE

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

ERRORS

53       No errors are defined.
54
55       The following sections are informative.
56

EXAMPLES

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

APPLICATION USAGE

71       None.
72

RATIONALE

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

FUTURE DIRECTIONS

128       None.
129

SEE ALSO

131       exec(),  putenv(), setenv(), unsetenv(), the Base Definitions volume of
132       IEEE Std 1003.1-2001, Chapter 8, Environment Variables, <stdlib.h>
133
135       Portions of this text are reprinted and reproduced in  electronic  form
136       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
137       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
138       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
139       Electrical and Electronics Engineers, Inc and The Open  Group.  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.opengroup.org/unix/online.html .
144
145
146
147IEEE/The Open Group                  2003                           GETENV(3P)
Impressum