]>
Commit | Line | Data |
---|---|---|
5b2abdfb A |
1 | .\" Copyright (c) 1988, 1991, 1993 |
2 | .\" The Regents of the University of California. All rights reserved. | |
3 | .\" | |
4 | .\" This code is derived from software contributed to Berkeley by | |
5 | .\" the American National Standards Committee X3, on Information | |
6 | .\" Processing Systems. | |
7 | .\" | |
8 | .\" Redistribution and use in source and binary forms, with or without | |
9 | .\" modification, are permitted provided that the following conditions | |
10 | .\" are met: | |
11 | .\" 1. Redistributions of source code must retain the above copyright | |
12 | .\" notice, this list of conditions and the following disclaimer. | |
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
14 | .\" notice, this list of conditions and the following disclaimer in the | |
15 | .\" documentation and/or other materials provided with the distribution. | |
974e3884 | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
5b2abdfb A |
17 | .\" may be used to endorse or promote products derived from this software |
18 | .\" without specific prior written permission. | |
19 | .\" | |
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | .\" SUCH DAMAGE. | |
31 | .\" | |
32 | .\" @(#)getenv.3 8.2 (Berkeley) 12/11/93 | |
974e3884 | 33 | .\" $FreeBSD$ |
5b2abdfb | 34 | .\" |
974e3884 | 35 | .Dd June 20, 2007 |
5b2abdfb A |
36 | .Dt GETENV 3 |
37 | .Os | |
38 | .Sh NAME | |
39 | .Nm getenv , | |
40 | .Nm putenv , | |
41 | .Nm setenv , | |
42 | .Nm unsetenv | |
43 | .Nd environment variable functions | |
44 | .Sh LIBRARY | |
45 | .Lb libc | |
46 | .Sh SYNOPSIS | |
47 | .In stdlib.h | |
48 | .Ft char * | |
974e3884 | 49 | .Fn getenv "const char *name" |
5b2abdfb | 50 | .Ft int |
974e3884 | 51 | .Fn setenv "const char *name" "const char *value" "int overwrite" |
5b2abdfb | 52 | .Ft int |
974e3884 | 53 | .Fn putenv "char *string" |
ad3c9f2a | 54 | .Ft int |
974e3884 | 55 | .Fn unsetenv "const char *name" |
5b2abdfb A |
56 | .Sh DESCRIPTION |
57 | These functions set, unset and fetch environment variables from the | |
58 | host | |
59 | .Em environment list . | |
5b2abdfb A |
60 | .Pp |
61 | The | |
62 | .Fn getenv | |
63 | function obtains the current value of the environment variable, | |
9385eb3d | 64 | .Fa name . |
974e3884 A |
65 | The application should not modify the string pointed |
66 | to by the | |
67 | .Fn getenv | |
68 | function. | |
5b2abdfb A |
69 | .Pp |
70 | The | |
71 | .Fn setenv | |
72 | function inserts or resets the environment variable | |
9385eb3d | 73 | .Fa name |
5b2abdfb A |
74 | in the current environment list. |
75 | If the variable | |
9385eb3d | 76 | .Fa name |
5b2abdfb A |
77 | does not exist in the list, |
78 | it is inserted with the given | |
9385eb3d | 79 | .Fa value . |
5b2abdfb | 80 | If the variable does exist, the argument |
9385eb3d | 81 | .Fa overwrite |
5b2abdfb | 82 | is tested; if |
9385eb3d | 83 | .Fa overwrite |
974e3884 | 84 | is zero, the |
5b2abdfb A |
85 | variable is not reset, otherwise it is reset |
86 | to the given | |
9385eb3d | 87 | .Fa value . |
5b2abdfb A |
88 | .Pp |
89 | The | |
90 | .Fn putenv | |
91 | function takes an argument of the form ``name=value'' and is | |
92 | equivalent to: | |
93 | .Bd -literal -offset indent | |
94 | setenv(name, value, 1); | |
95 | .Ed | |
96 | .Pp | |
ad3c9f2a A |
97 | The string pointed to by |
98 | .Fa string | |
99 | becomes part of the environment. | |
100 | A program should not alter or free the string, | |
101 | and should not use stack or other transient string variables | |
102 | as arguments to | |
103 | .Fn putenv . | |
104 | The | |
105 | .Fn setenv | |
106 | function is strongly preferred to | |
107 | .Fn putenv . | |
108 | .Pp | |
5b2abdfb A |
109 | The |
110 | .Fn unsetenv | |
111 | function | |
112 | deletes all instances of the variable name pointed to by | |
113 | .Fa name | |
114 | from the list. | |
ad3c9f2a A |
115 | Note that only the variable name (e.g., "NAME") should be given; |
116 | "NAME=value" will not work. | |
5b2abdfb | 117 | .Sh RETURN VALUES |
3d9156a7 A |
118 | The |
119 | .Fn getenv | |
120 | function returns the value of the environment variable as a | |
121 | .Dv NUL Ns | |
122 | -terminated string. | |
123 | If the variable | |
124 | .Fa name | |
125 | is not in the current environment, | |
126 | .Dv NULL | |
127 | is returned. | |
128 | .Pp | |
ad3c9f2a | 129 | .Rv -std setenv putenv unsetenv |
5b2abdfb A |
130 | .Sh ERRORS |
131 | .Bl -tag -width Er | |
ad3c9f2a A |
132 | .It Bq Er EINVAL |
133 | The function | |
974e3884 A |
134 | .Fn getenv , |
135 | .Fn setenv | |
136 | or | |
ad3c9f2a | 137 | .Fn unsetenv |
974e3884 | 138 | failed because the |
ad3c9f2a | 139 | .Fa name |
974e3884 A |
140 | is a |
141 | .Dv NULL | |
142 | pointer, points to an empty string, or points to a string containing an | |
143 | .Dq Li \&= | |
144 | character. | |
145 | .Pp | |
146 | The function | |
147 | .Fn putenv | |
148 | failed because | |
149 | .Fa string | |
150 | is a | |
151 | .Dv NULL | |
152 | pointer or | |
153 | .Fa string | |
154 | is without an | |
155 | .Dq Li \&= | |
156 | character. | |
5b2abdfb A |
157 | .It Bq Er ENOMEM |
158 | The function | |
974e3884 A |
159 | .Fn setenv , |
160 | .Fn unsetenv | |
5b2abdfb A |
161 | or |
162 | .Fn putenv | |
ad3c9f2a | 163 | failed because it was unable to allocate memory for the environment. |
5b2abdfb | 164 | .El |
ad3c9f2a A |
165 | .Sh LEGACY SYNOPSIS |
166 | .Fd #include <stdlib.h> | |
167 | .Pp | |
168 | .Ft void | |
169 | .br | |
170 | .Fo unsetenv | |
171 | .Fa "const char *name" | |
172 | .Fc ; | |
173 | .Pp | |
174 | .Fn unsetenv | |
175 | doesn't return a value. | |
176 | .Sh COMPATIBILITY | |
177 | .Fn putenv | |
178 | no longer copies its input buffer. | |
179 | This often appears in crash logs as a crash in | |
180 | .Fn getenv . | |
181 | Avoid passing local buffers or freeing the memory | |
182 | that is passed to | |
183 | .Fn putenv . | |
184 | Use | |
185 | .Fn setenv , | |
186 | which still makes an internal copy of its buffers. | |
187 | .Pp | |
188 | .Fn unsetenv | |
189 | no longer parses the variable name; | |
190 | e.g., unsetenv ("FOO=BAR") no longer works. | |
191 | Use unsetenv("FOO"). | |
192 | .Fn unsetenv | |
193 | also now returns a status value and will set | |
194 | .Va errno | |
195 | to EINVAL if | |
196 | .Fa name | |
197 | is not a defined environment variable. | |
5b2abdfb A |
198 | .Sh SEE ALSO |
199 | .Xr csh 1 , | |
200 | .Xr sh 1 , | |
201 | .Xr execve 2 , | |
ad3c9f2a | 202 | .Xr compat 5 , |
5b2abdfb A |
203 | .Xr environ 7 |
204 | .Sh STANDARDS | |
205 | The | |
206 | .Fn getenv | |
207 | function conforms to | |
208 | .St -isoC . | |
974e3884 A |
209 | The |
210 | .Fn setenv , | |
211 | .Fn putenv | |
212 | and | |
213 | .Fn unsetenv | |
214 | functions conforms to | |
215 | .St -p1003.1-2001 . | |
216 | .Sh HISTORY | |
217 | The functions | |
218 | .Fn setenv | |
219 | and | |
220 | .Fn unsetenv | |
221 | appeared in | |
222 | .At v7 . | |
223 | The | |
224 | .Fn putenv | |
225 | function appeared in | |
226 | .Bx 4.3 Reno . | |
5b2abdfb A |
227 | .Sh BUGS |
228 | Successive calls to | |
229 | .Fn setenv | |
974e3884 | 230 | that assign a larger-sized |
9385eb3d | 231 | .Fa value |
974e3884 | 232 | than any previous value to the same |
9385eb3d | 233 | .Fa name |
3d9156a7 A |
234 | will result in a memory leak. |
235 | The | |
5b2abdfb | 236 | .Fx |
974e3884 | 237 | semantics for this function |
5b2abdfb | 238 | (namely, that the contents of |
9385eb3d | 239 | .Fa value |
5b2abdfb | 240 | are copied and that old values remain accessible indefinitely) make this |
3d9156a7 A |
241 | bug unavoidable. |
242 | Future versions may eliminate one or both of these | |
5b2abdfb | 243 | semantic guarantees in order to fix the bug. |