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