xref: /netbsd-src/lib/libc/stdlib/getenv.3 (revision 83306973da38d6ed1d026789e3a06d7072722307)
1*83306973Swiz.\"	$NetBSD: getenv.3,v 1.25 2010/10/26 22:34:33 wiz Exp $
26dda330eSthorpej.\"
35f8a6c06Sperry.\" Copyright (c) 1988, 1991, 1993
45f8a6c06Sperry.\"	The Regents of the University of California.  All rights reserved.
561f28255Scgd.\"
661f28255Scgd.\" This code is derived from software contributed to Berkeley by
761f28255Scgd.\" the American National Standards Committee X3, on Information
861f28255Scgd.\" Processing Systems.
961f28255Scgd.\"
1061f28255Scgd.\" Redistribution and use in source and binary forms, with or without
1161f28255Scgd.\" modification, are permitted provided that the following conditions
1261f28255Scgd.\" are met:
1361f28255Scgd.\" 1. Redistributions of source code must retain the above copyright
1461f28255Scgd.\"    notice, this list of conditions and the following disclaimer.
1561f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd.\"    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd.\"    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd.\"    may be used to endorse or promote products derived from this software
2061f28255Scgd.\"    without specific prior written permission.
2161f28255Scgd.\"
2261f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd.\" SUCH DAMAGE.
3361f28255Scgd.\"
345f8a6c06Sperry.\"     from: @(#)getenv.3	8.2 (Berkeley) 12/11/93
3561f28255Scgd.\"
365d635760Snjoly.Dd October 25, 2010
3761f28255Scgd.Dt GETENV 3
3861f28255Scgd.Os
3961f28255Scgd.Sh NAME
4061f28255Scgd.Nm getenv ,
41cc85a7cfSchristos.Nm getenv_r ,
4261f28255Scgd.Nm putenv ,
4361f28255Scgd.Nm setenv ,
4461f28255Scgd.Nm unsetenv
4561f28255Scgd.Nd environment variable functions
46312aca53Sperry.Sh LIBRARY
47312aca53Sperry.Lb libc
4861f28255Scgd.Sh SYNOPSIS
49472351e1Swiz.In stdlib.h
5061f28255Scgd.Ft char *
5161f28255Scgd.Fn getenv "const char *name"
5261f28255Scgd.Ft int
53cc85a7cfSchristos.Fn getenv_r "const char *name" "char *buf" "size_t len"
54cc85a7cfSchristos.Ft int
5561f28255Scgd.Fn setenv "const char *name" "const char *value" "int overwrite"
5661f28255Scgd.Ft int
577acc3301Schristos.Fn putenv "char *string"
581a9947a7Sdrochner.Ft int
5961f28255Scgd.Fn unsetenv "const char *name"
6061f28255Scgd.Sh DESCRIPTION
6161f28255ScgdThese functions set, unset and fetch environment variables from the
6261f28255Scgdhost
6361f28255Scgd.Em environment list .
6461f28255ScgdFor compatibility with differing environment conventions,
654c968434Snjolythe
664c968434Snjoly.Fn getenv
674c968434Snjolyor
684c968434Snjoly.Fn getenv_r
694c968434Snjolygiven argument
7061f28255Scgd.Ar name
714c968434Snjolymay be appended with an equal sign
724c968434Snjoly.Dq Li \&= .
7361f28255Scgd.Pp
7461f28255ScgdThe
7561f28255Scgd.Fn getenv
7672a37764Swizfunction obtains the current value of the environment variable
7761f28255Scgd.Ar name .
7861f28255ScgdIf the variable
7961f28255Scgd.Ar name
8072a37764Swizis not in the current environment, a
81cc85a7cfSchristos.Dv NULL
82cc85a7cfSchristospointer is returned.
83cc85a7cfSchristos.Pp
84cc85a7cfSchristosThe
85cc85a7cfSchristos.Fn getenv_r
8672a37764Swizfunction obtains the current value of the environment variable
87cc85a7cfSchristos.Fa name
8872a37764Swizand copies it to
89cc85a7cfSchristos.Fa buf .
90cc85a7cfSchristosIf
91cc85a7cfSchristos.Fa name
92cc85a7cfSchristosis not in the current environment, or the string length of the value of
93cc85a7cfSchristos.Fa name
94cc85a7cfSchristosis longer than
95cc85a7cfSchristos.Fa len
9672a37764Swizcharacters, then \-1 is returned and
97cc85a7cfSchristos.Va errno
98cc85a7cfSchristosis set to indicate the error.
9961f28255Scgd.Pp
10061f28255ScgdThe
10161f28255Scgd.Fn setenv
10261f28255Scgdfunction inserts or resets the environment variable
10361f28255Scgd.Ar name
10461f28255Scgdin the current environment list.
10561f28255ScgdIf the variable
10661f28255Scgd.Ar name
10761f28255Scgddoes not exist in the list,
10861f28255Scgdit is inserted with the given
10961f28255Scgd.Ar value .
11061f28255ScgdIf the variable does exist, the argument
11161f28255Scgd.Ar overwrite
11261f28255Scgdis tested; if
11361f28255Scgd.Ar overwrite is
11461f28255Scgdzero, the
11561f28255Scgdvariable is not reset, otherwise it is reset
11661f28255Scgdto the given
11761f28255Scgd.Ar value .
11861f28255Scgd.Pp
11961f28255ScgdThe
12061f28255Scgd.Fn putenv
1217acc3301Schristosfunction takes an argument of the form
1227acc3301Schristos.Dq name=value
1237acc3301Schristosand it will set the environment variable
1247acc3301Schristos.Dq name
1257acc3301Schristosequal to
1267acc3301Schristos.Dq value
1277acc3301Schristosby altering an existing entry, or creating a new one if an existing
128732dd678Swizone does not exist.
1297acc3301SchristosThe actual string argument passed to
1307acc3301Schristos.Fn putenv
1317acc3301Schristoswill become part of the environment.
1327acc3301SchristosIf one changes the string, the environment will also change.
13361f28255Scgd.Pp
13461f28255ScgdThe
13561f28255Scgd.Fn unsetenv
13661f28255Scgdfunction
13761f28255Scgddeletes all instances of the variable name pointed to by
13861f28255Scgd.Fa name
13961f28255Scgdfrom the list.
14061f28255Scgd.Sh RETURN VALUES
14161f28255ScgdThe functions
142cc85a7cfSchristos.Fn getenv_r ,
14313dee93fSkleink.Fn setenv ,
144cc85a7cfSchristos.Fn putenv ,
14513dee93fSkleinkand
14613dee93fSkleink.Fn unsetenv
14761f28255Scgdreturn zero if successful; otherwise the global variable
14861f28255Scgd.Va errno
14961f28255Scgdis set to indicate the error and a
15061f28255Scgd\-1 is returned.
1511060c75fSyamt.Pp
1521060c75fSyamtIf
1531060c75fSyamt.Fn getenv
1541060c75fSyamtis successful, the string returned should be considered read-only.
15561f28255Scgd.Sh ERRORS
156a66bb899Sjtc.Bl -tag -width Er
15713dee93fSkleink.It Bq Er EINVAL
15813dee93fSkleinkThe
15913dee93fSkleink.Fa name
16013dee93fSkleinkargument to
1614c968434Snjoly.Fn setenv
1624c968434Snjolyor
16313dee93fSkleink.Fn unsetenv
16413dee93fSkleinkis a null pointer, points to an empty string, or points to a string
16513dee93fSkleinkcontaining an
16613dee93fSkleink.Dq Li \&=
16713dee93fSkleinkcharacter.
1684c968434SnjolyThe
1694c968434Snjoly.Fa value
1704c968434Snjolyargument to
1714c968434Snjoly.Fn setenv
1724c968434Snjolyis a null pointer.
1735d635760SnjolyThe
1745d635760Snjoly.Fa string
1755d635760Snjolyargument to
1765d635760Snjoly.Fn putenv
177*83306973Swizis a null pointer, or points to a string that either starts with a
1785d635760Snjoly.Dq Li \&=
179*83306973Swizcharacter or does not contain one at all.
18061f28255Scgd.It Bq Er ENOMEM
18161f28255ScgdThe function
18261f28255Scgd.Fn setenv
18361f28255Scgdor
18461f28255Scgd.Fn putenv
18561f28255Scgdfailed because they were unable to allocate memory for the environment.
18661f28255Scgd.El
187abdcf63aSnjoly.Pp
188cc85a7cfSchristosThe function
189cc85a7cfSchristos.Fn getenv_r
190cc85a7cfSchristoscan return the following errors:
191cc85a7cfSchristos.Bl -tag -width Er
192cc85a7cfSchristos.It Bq Er ENOENT
193cc85a7cfSchristosThe variable
194cc85a7cfSchristos.Fa name
195cc85a7cfSchristoswas not found in the environment.
196cc85a7cfSchristos.It Bq Er ERANGE
197cc85a7cfSchristosThe value of the named variable is too long to fit in the supplied buffer.
198cc85a7cfSchristos.El
19961f28255Scgd.Sh SEE ALSO
20061f28255Scgd.Xr csh 1 ,
20161f28255Scgd.Xr sh 1 ,
20261f28255Scgd.Xr execve 2 ,
20361f28255Scgd.Xr environ 7
20461f28255Scgd.Sh STANDARDS
20561f28255ScgdThe
20661f28255Scgd.Fn getenv
20761f28255Scgdfunction conforms to
20861f28255Scgd.St -ansiC .
209ee3e4799SkleinkThe
210ee3e4799Skleink.Fn putenv
211ee3e4799Skleinkfunction conforms to
212ee3e4799Skleink.St -xpg4 .
21313dee93fSkleinkThe
21413dee93fSkleink.Fn unsetenv
21513dee93fSkleinkfunction conforms to
21613dee93fSkleink.St -p1003.1-2001 .
21761f28255Scgd.Sh HISTORY
21861f28255ScgdThe functions
21961f28255Scgd.Fn setenv
22061f28255Scgdand
22161f28255Scgd.Fn unsetenv
22261f28255Scgdappeared in
22361f28255Scgd.At v7 .
22461f28255ScgdThe
22561f28255Scgd.Fn putenv
22661f28255Scgdfunction appeared in
22761f28255Scgd.Bx 4.3 Reno .
228