xref: /netbsd-src/lib/libc/stdlib/getenv.3 (revision 8e6ab8837d8d6b9198e67c1c445300b483e2f304)
1.\"	$NetBSD: getenv.3,v 1.16 2003/04/16 13:34:46 wiz Exp $
2.\"
3.\" Copyright (c) 1988, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" the American National Standards Committee X3, on Information
8.\" Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. All advertising materials mentioning features or use of this software
19.\"    must display the following acknowledgement:
20.\"	This product includes software developed by the University of
21.\"	California, Berkeley and its contributors.
22.\" 4. Neither the name of the University nor the names of its contributors
23.\"    may be used to endorse or promote products derived from this software
24.\"    without specific prior written permission.
25.\"
26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36.\" SUCH DAMAGE.
37.\"
38.\"     from: @(#)getenv.3	8.2 (Berkeley) 12/11/93
39.\"
40.Dd April 1, 2003
41.Dt GETENV 3
42.Os
43.Sh NAME
44.Nm getenv ,
45.Nm putenv ,
46.Nm setenv ,
47.Nm unsetenv
48.Nd environment variable functions
49.Sh LIBRARY
50.Lb libc
51.Sh SYNOPSIS
52.In stdlib.h
53.Ft char *
54.Fn getenv "const char *name"
55.Ft int
56.Fn setenv "const char *name" "const char *value" "int overwrite"
57.Ft int
58.Fn putenv "const char *string"
59.Ft int
60.Fn unsetenv "const char *name"
61.Sh DESCRIPTION
62These functions set, unset and fetch environment variables from the
63host
64.Em environment list .
65For compatibility with differing environment conventions,
66the given arguments
67.Ar name
68and
69.Ar value
70may be appended and prepended,
71respectively,
72with an equal sign
73.Dq Li \&= ,
74except for
75.Fn unsetenv .
76.Pp
77The
78.Fn getenv
79function obtains the current value of the environment variable,
80.Ar name .
81If the variable
82.Ar name
83is not in the current environment,
84a null pointer is returned.
85.Pp
86The
87.Fn setenv
88function inserts or resets the environment variable
89.Ar name
90in the current environment list.
91If the variable
92.Ar name
93does not exist in the list,
94it is inserted with the given
95.Ar value .
96If the variable does exist, the argument
97.Ar overwrite
98is tested; if
99.Ar overwrite is
100zero, the
101variable is not reset, otherwise it is reset
102to the given
103.Ar value .
104.Pp
105The
106.Fn putenv
107function takes an argument of the form ``name=value'' and is
108equivalent to:
109.Bd -literal -offset indent
110setenv(name, value, 1);
111.Ed
112.Pp
113The
114.Fn unsetenv
115function
116deletes all instances of the variable name pointed to by
117.Fa name
118from the list.
119.Sh RETURN VALUES
120The functions
121.Fn setenv ,
122.Fn putenv
123and
124.Fn unsetenv
125return zero if successful; otherwise the global variable
126.Va errno
127is set to indicate the error and a
128\-1 is returned.
129.Pp
130If
131.Fn getenv
132is successful, the string returned should be considered read-only.
133.Sh ERRORS
134.Bl -tag -width Er
135.It Bq Er EINVAL
136The
137.Fa name
138argument to
139.Fn unsetenv
140is a null pointer, points to an empty string, or points to a string
141containing an
142.Dq Li \&=
143character.
144.It Bq Er ENOMEM
145The function
146.Fn setenv
147or
148.Fn putenv
149failed because they were unable to allocate memory for the environment.
150.El
151.Sh SEE ALSO
152.Xr csh 1 ,
153.Xr sh 1 ,
154.Xr execve 2 ,
155.Xr environ 7
156.Sh STANDARDS
157The
158.Fn getenv
159function conforms to
160.St -ansiC .
161The
162.Fn putenv
163function conforms to
164.St -xpg4 .
165The
166.Fn unsetenv
167function conforms to
168.St -p1003.1-2001 .
169.Sh HISTORY
170The functions
171.Fn setenv
172and
173.Fn unsetenv
174appeared in
175.At v7 .
176The
177.Fn putenv
178function appeared in
179.Bx 4.3 Reno .
180