xref: /original-bsd/lib/libc/stdlib/getenv.3 (revision bdc0a208)
1.\" Copyright (c) 1988, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)getenv.3	6.10 (Berkeley) 04/19/91
7.\"
8.Dd
9.Dt GETENV 3
10.Os
11.Sh NAME
12.Nm getenv ,
13.Nm putenv ,
14.Nm setenv ,
15.Nm unsetenv
16.Nd environment variable functions
17.Sh SYNOPSIS
18.Fd #include <stdlib.h>
19.Ft char *
20.Fn getenv "const char *name"
21.Ft int
22.Fn setenv "const char *name" "const char *value" "int overwrite"
23.Ft int
24.Fn putenv "const char *string"
25.Ft void
26.Fn unsetenv "const char *name"
27.Sh DESCRIPTION
28These functions set, unset and fetch environment variables from the
29host
30.Em environment list .
31For compatibility with differing environment conventions,
32the given arguments
33.Ar name
34and
35.Ar value
36may be appended and prepended,
37respectively,
38with an equal sign
39.Dq Li \&= .
40.Pp
41The
42.Fn getenv
43function obtains the current value of the environment variable,
44.Ar name .
45If the variable
46.Ar name
47is not in the current environment ,
48a null pointer is returned.
49.Pp
50The
51.Fn setenv
52function inserts or resets the environment variable
53.Ar name
54in the current environment list.
55If the variable
56.Ar name
57does not exist in the list,
58it is inserted with the given
59.Ar value.
60If the variable does exist, the argument
61.Ar overwrite
62is tested; if
63.Ar overwrite is
64zero, the
65variable is not reset, otherwise it is reset
66to the given
67.Ar value .
68.Pp
69The
70.Fn putenv
71function
72performs the equivalent of:
73.Bd -literal -offset indent
74setenv(name, value, 1);
75.Ed
76.Pp
77The
78.Fn unsetenv
79function
80deletes all instances of the variable name pointed to by
81.Fa name
82from the list.
83.Sh RETURN VALUES
84The functions
85.Fn setenv
86and
87.Fn putenv
88return zero if successful; otherwise the global variable
89.Va errno
90is set to indicate the error and a
91\-1 is returned.
92.Sh ERRORS
93.Bl -tag -width [ENOMEM]
94.It Bq Er ENOMEM
95The function
96.Fn setenv
97or
98.Fn putenv
99failed because they were unable to allocate memory for the environment.
100.El
101.Sh SEE ALSO
102.Xr csh 1 ,
103.Xr sh 1 ,
104.Xr execve 2 ,
105.Xr environ 7
106.Sh STANDARDS
107The
108.Fn getenv
109function conforms to
110.St -ansiC .
111.Sh HISTORY
112The functions
113.Fn setenv
114and
115.Fn unsetenv
116appeared in
117.At v7 .
118The
119.Fn putenv
120function appeared in
121.Bx 4.3 Reno .
122