summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/getenv.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/getenv.3')
-rw-r--r--src/lib/libc/stdlib/getenv.3132
1 files changed, 79 insertions, 53 deletions
diff --git a/src/lib/libc/stdlib/getenv.3 b/src/lib/libc/stdlib/getenv.3
index 411eb35da4..c24e480ffb 100644
--- a/src/lib/libc/stdlib/getenv.3
+++ b/src/lib/libc/stdlib/getenv.3
@@ -1,5 +1,5 @@
1.\" Copyright (c) 1988, 1991 The Regents of the University of California. 1.\" Copyright (c) 1988, 1991, 1993
2.\" All rights reserved. 2.\" The Regents of the University of California. All rights reserved.
3.\" 3.\"
4.\" This code is derived from software contributed to Berkeley by 4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information 5.\" the American National Standards Committee X3, on Information
@@ -13,11 +13,7 @@
13.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the 14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution. 15.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software 16.\" 3. Neither the name of the University nor the names of its contributors
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software 17.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission. 18.\" without specific prior written permission.
23.\" 19.\"
@@ -33,10 +29,9 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
35.\" 31.\"
36.\" from: @(#)getenv.3 6.11 (Berkeley) 6/29/91 32.\" $OpenBSD: getenv.3,v 1.15 2010/07/06 20:52:00 naddy Exp $
37.\" $Id: getenv.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $
38.\" 33.\"
39.Dd June 29, 1991 34.Dd $Mdocdate: July 6 2010 $
40.Dt GETENV 3 35.Dt GETENV 3
41.Os 36.Os
42.Sh NAME 37.Sh NAME
@@ -52,82 +47,107 @@
52.Ft int 47.Ft int
53.Fn setenv "const char *name" "const char *value" "int overwrite" 48.Fn setenv "const char *name" "const char *value" "int overwrite"
54.Ft int 49.Ft int
55.Fn putenv "const char *string" 50.Fn putenv "char *string"
56.Ft void 51.Ft int
57.Fn unsetenv "const char *name" 52.Fn unsetenv "const char *name"
58.Sh DESCRIPTION 53.Sh DESCRIPTION
59These functions set, unset and fetch environment variables from the 54These functions set, unset, and fetch environment variables from the host
60host
61.Em environment list . 55.Em environment list .
62For compatibility with differing environment conventions, 56For compatibility with differing environment conventions, the given arguments
63the given arguments 57.Fa name
64.Ar name
65and 58and
66.Ar value 59.Fa value
67may be appended and prepended, 60may be appended and prepended, respectively, with an equal sign
68respectively,
69with an equal sign
70.Dq Li \&= . 61.Dq Li \&= .
71.Pp 62.Pp
72The 63The
73.Fn getenv 64.Fn getenv
74function obtains the current value of the environment variable, 65function obtains the current value of the environment variable
75.Ar name . 66.Fa name .
76If the variable 67If the variable
77.Ar name 68.Fa name
78is not in the current environment , 69is not in the current environment, a null pointer is returned.
79a null pointer is returned.
80.Pp 70.Pp
81The 71The
82.Fn setenv 72.Fn setenv
83function inserts or resets the environment variable 73function inserts or resets the environment variable
84.Ar name 74.Fa name
85in the current environment list. 75in the current environment list.
86If the variable 76If the variable
87.Ar name 77.Fa name
88does not exist in the list, 78does not exist in the list, it is inserted with the given
89it is inserted with the given 79.Fa value .
90.Ar value.
91If the variable does exist, the argument 80If the variable does exist, the argument
92.Ar overwrite 81.Fa overwrite
93is tested; if 82is tested; if
94.Ar overwrite is 83.Fa overwrite
95zero, the 84is zero, the variable is not reset, otherwise it is reset to the given
96variable is not reset, otherwise it is reset 85.Fa value .
97to the given
98.Ar value .
99.Pp 86.Pp
100The 87The
101.Fn putenv 88.Fn putenv
102function takes an argument of the form ``name=value'' and is 89function takes an argument of the form
103equivalent to: 90.Ar name Ns = Ns Ar value .
104.Bd -literal -offset indent 91The memory pointed to by
105setenv(name, value, 1); 92.Ar string
106.Ed 93becomes part of the environment and must not be deallocated by the caller.
94If the variable already exists, it will be overwritten.
95A common source of bugs is to pass a
96.Ar string
97argument that is a locally scoped string buffer.
98This will result in corruption of the environment after leaving
99the scope in which the variable is defined.
100For this reason, the
101.Fn setenv
102function is preferred over
103.Fn putenv .
107.Pp 104.Pp
108The 105The
109.Fn unsetenv 106.Fn unsetenv
110function 107function deletes all instances of the variable name pointed to by
111deletes all instances of the variable name pointed to by
112.Fa name 108.Fa name
113from the list. 109from the list.
114.Sh RETURN VALUES 110.Sh RETURN VALUES
115The functions 111These functions
116.Fn setenv
117and
118.Fn putenv
119return zero if successful; otherwise the global variable 112return zero if successful; otherwise the global variable
120.Va errno 113.Va errno
121is set to indicate the error and a 114is set to indicate the error and \-1 is returned.
122\-1 is returned. 115.Pp
116If
117.Fn getenv
118is successful, the string returned should be considered read-only.
123.Sh ERRORS 119.Sh ERRORS
124.Bl -tag -width Er 120.Bl -tag -width Er
121.It Bq Er EINVAL
122The
123.Fn setenv
124or
125.Fn unsetenv
126function was passed a
127.Ar name
128containing an
129.Sq =
130character.
131.Pp
132The
133.Fn unsetenv
134function was passed an empty
135.Ar name
136or a NULL pointer.
137.Pp
138The
139.Fn putenv
140function was passed a
141.Ar string
142that did not contain an
143.Sq =
144character.
125.It Bq Er ENOMEM 145.It Bq Er ENOMEM
126The function 146The
127.Fn setenv 147.Fn setenv
128or 148or
129.Fn putenv 149.Fn putenv
130failed because they were unable to allocate memory for the environment. 150function failed because it was unable to allocate memory for the environment.
131.El 151.El
132.Sh SEE ALSO 152.Sh SEE ALSO
133.Xr csh 1 , 153.Xr csh 1 ,
@@ -140,12 +160,18 @@ The
140function conforms to 160function conforms to
141.St -ansiC . 161.St -ansiC .
142.Sh HISTORY 162.Sh HISTORY
163The function
164.Fn getenv
165appeared in
166.At v7
167and
168.Bx 3 .
143The functions 169The functions
144.Fn setenv 170.Fn setenv
145and 171and
146.Fn unsetenv 172.Fn unsetenv
147appeared in 173appeared in
148.At v7 . 174.Bx 4.3 Tahoe .
149The 175The
150.Fn putenv 176.Fn putenv
151function appeared in 177function appeared in