1*2fe8fb19SBen Gras.\" $NetBSD: getenv.3,v 1.25 2010/10/26 22:34:33 wiz Exp $ 2*2fe8fb19SBen Gras.\" 3*2fe8fb19SBen Gras.\" Copyright (c) 1988, 1991, 1993 4*2fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 5*2fe8fb19SBen Gras.\" 6*2fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by 7*2fe8fb19SBen Gras.\" the American National Standards Committee X3, on Information 8*2fe8fb19SBen Gras.\" Processing Systems. 9*2fe8fb19SBen Gras.\" 10*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 11*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 12*2fe8fb19SBen Gras.\" are met: 13*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 14*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 15*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 16*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 17*2fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 18*2fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 19*2fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 20*2fe8fb19SBen Gras.\" without specific prior written permission. 21*2fe8fb19SBen Gras.\" 22*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23*2fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24*2fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25*2fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26*2fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27*2fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28*2fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29*2fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30*2fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31*2fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32*2fe8fb19SBen Gras.\" SUCH DAMAGE. 33*2fe8fb19SBen Gras.\" 34*2fe8fb19SBen Gras.\" from: @(#)getenv.3 8.2 (Berkeley) 12/11/93 35*2fe8fb19SBen Gras.\" 36*2fe8fb19SBen Gras.Dd October 25, 2010 37*2fe8fb19SBen Gras.Dt GETENV 3 38*2fe8fb19SBen Gras.Os 39*2fe8fb19SBen Gras.Sh NAME 40*2fe8fb19SBen Gras.Nm getenv , 41*2fe8fb19SBen Gras.Nm getenv_r , 42*2fe8fb19SBen Gras.Nm putenv , 43*2fe8fb19SBen Gras.Nm setenv , 44*2fe8fb19SBen Gras.Nm unsetenv 45*2fe8fb19SBen Gras.Nd environment variable functions 46*2fe8fb19SBen Gras.Sh LIBRARY 47*2fe8fb19SBen Gras.Lb libc 48*2fe8fb19SBen Gras.Sh SYNOPSIS 49*2fe8fb19SBen Gras.In stdlib.h 50*2fe8fb19SBen Gras.Ft char * 51*2fe8fb19SBen Gras.Fn getenv "const char *name" 52*2fe8fb19SBen Gras.Ft int 53*2fe8fb19SBen Gras.Fn getenv_r "const char *name" "char *buf" "size_t len" 54*2fe8fb19SBen Gras.Ft int 55*2fe8fb19SBen Gras.Fn setenv "const char *name" "const char *value" "int overwrite" 56*2fe8fb19SBen Gras.Ft int 57*2fe8fb19SBen Gras.Fn putenv "char *string" 58*2fe8fb19SBen Gras.Ft int 59*2fe8fb19SBen Gras.Fn unsetenv "const char *name" 60*2fe8fb19SBen Gras.Sh DESCRIPTION 61*2fe8fb19SBen GrasThese functions set, unset and fetch environment variables from the 62*2fe8fb19SBen Grashost 63*2fe8fb19SBen Gras.Em environment list . 64*2fe8fb19SBen GrasFor compatibility with differing environment conventions, 65*2fe8fb19SBen Grasthe 66*2fe8fb19SBen Gras.Fn getenv 67*2fe8fb19SBen Grasor 68*2fe8fb19SBen Gras.Fn getenv_r 69*2fe8fb19SBen Grasgiven argument 70*2fe8fb19SBen Gras.Ar name 71*2fe8fb19SBen Grasmay be appended with an equal sign 72*2fe8fb19SBen Gras.Dq Li \&= . 73*2fe8fb19SBen Gras.Pp 74*2fe8fb19SBen GrasThe 75*2fe8fb19SBen Gras.Fn getenv 76*2fe8fb19SBen Grasfunction obtains the current value of the environment variable 77*2fe8fb19SBen Gras.Ar name . 78*2fe8fb19SBen GrasIf the variable 79*2fe8fb19SBen Gras.Ar name 80*2fe8fb19SBen Grasis not in the current environment, a 81*2fe8fb19SBen Gras.Dv NULL 82*2fe8fb19SBen Graspointer is returned. 83*2fe8fb19SBen Gras.Pp 84*2fe8fb19SBen GrasThe 85*2fe8fb19SBen Gras.Fn getenv_r 86*2fe8fb19SBen Grasfunction obtains the current value of the environment variable 87*2fe8fb19SBen Gras.Fa name 88*2fe8fb19SBen Grasand copies it to 89*2fe8fb19SBen Gras.Fa buf . 90*2fe8fb19SBen GrasIf 91*2fe8fb19SBen Gras.Fa name 92*2fe8fb19SBen Grasis not in the current environment, or the string length of the value of 93*2fe8fb19SBen Gras.Fa name 94*2fe8fb19SBen Grasis longer than 95*2fe8fb19SBen Gras.Fa len 96*2fe8fb19SBen Grascharacters, then \-1 is returned and 97*2fe8fb19SBen Gras.Va errno 98*2fe8fb19SBen Grasis set to indicate the error. 99*2fe8fb19SBen Gras.Pp 100*2fe8fb19SBen GrasThe 101*2fe8fb19SBen Gras.Fn setenv 102*2fe8fb19SBen Grasfunction inserts or resets the environment variable 103*2fe8fb19SBen Gras.Ar name 104*2fe8fb19SBen Grasin the current environment list. 105*2fe8fb19SBen GrasIf the variable 106*2fe8fb19SBen Gras.Ar name 107*2fe8fb19SBen Grasdoes not exist in the list, 108*2fe8fb19SBen Grasit is inserted with the given 109*2fe8fb19SBen Gras.Ar value . 110*2fe8fb19SBen GrasIf the variable does exist, the argument 111*2fe8fb19SBen Gras.Ar overwrite 112*2fe8fb19SBen Grasis tested; if 113*2fe8fb19SBen Gras.Ar overwrite is 114*2fe8fb19SBen Graszero, the 115*2fe8fb19SBen Grasvariable is not reset, otherwise it is reset 116*2fe8fb19SBen Grasto the given 117*2fe8fb19SBen Gras.Ar value . 118*2fe8fb19SBen Gras.Pp 119*2fe8fb19SBen GrasThe 120*2fe8fb19SBen Gras.Fn putenv 121*2fe8fb19SBen Grasfunction takes an argument of the form 122*2fe8fb19SBen Gras.Dq name=value 123*2fe8fb19SBen Grasand it will set the environment variable 124*2fe8fb19SBen Gras.Dq name 125*2fe8fb19SBen Grasequal to 126*2fe8fb19SBen Gras.Dq value 127*2fe8fb19SBen Grasby altering an existing entry, or creating a new one if an existing 128*2fe8fb19SBen Grasone does not exist. 129*2fe8fb19SBen GrasThe actual string argument passed to 130*2fe8fb19SBen Gras.Fn putenv 131*2fe8fb19SBen Graswill become part of the environment. 132*2fe8fb19SBen GrasIf one changes the string, the environment will also change. 133*2fe8fb19SBen Gras.Pp 134*2fe8fb19SBen GrasThe 135*2fe8fb19SBen Gras.Fn unsetenv 136*2fe8fb19SBen Grasfunction 137*2fe8fb19SBen Grasdeletes all instances of the variable name pointed to by 138*2fe8fb19SBen Gras.Fa name 139*2fe8fb19SBen Grasfrom the list. 140*2fe8fb19SBen Gras.Sh RETURN VALUES 141*2fe8fb19SBen GrasThe functions 142*2fe8fb19SBen Gras.Fn getenv_r , 143*2fe8fb19SBen Gras.Fn setenv , 144*2fe8fb19SBen Gras.Fn putenv , 145*2fe8fb19SBen Grasand 146*2fe8fb19SBen Gras.Fn unsetenv 147*2fe8fb19SBen Grasreturn zero if successful; otherwise the global variable 148*2fe8fb19SBen Gras.Va errno 149*2fe8fb19SBen Grasis set to indicate the error and a 150*2fe8fb19SBen Gras\-1 is returned. 151*2fe8fb19SBen Gras.Pp 152*2fe8fb19SBen GrasIf 153*2fe8fb19SBen Gras.Fn getenv 154*2fe8fb19SBen Grasis successful, the string returned should be considered read-only. 155*2fe8fb19SBen Gras.Sh ERRORS 156*2fe8fb19SBen Gras.Bl -tag -width Er 157*2fe8fb19SBen Gras.It Bq Er EINVAL 158*2fe8fb19SBen GrasThe 159*2fe8fb19SBen Gras.Fa name 160*2fe8fb19SBen Grasargument to 161*2fe8fb19SBen Gras.Fn setenv 162*2fe8fb19SBen Grasor 163*2fe8fb19SBen Gras.Fn unsetenv 164*2fe8fb19SBen Grasis a null pointer, points to an empty string, or points to a string 165*2fe8fb19SBen Grascontaining an 166*2fe8fb19SBen Gras.Dq Li \&= 167*2fe8fb19SBen Grascharacter. 168*2fe8fb19SBen GrasThe 169*2fe8fb19SBen Gras.Fa value 170*2fe8fb19SBen Grasargument to 171*2fe8fb19SBen Gras.Fn setenv 172*2fe8fb19SBen Grasis a null pointer. 173*2fe8fb19SBen GrasThe 174*2fe8fb19SBen Gras.Fa string 175*2fe8fb19SBen Grasargument to 176*2fe8fb19SBen Gras.Fn putenv 177*2fe8fb19SBen Grasis a null pointer, or points to a string that either starts with a 178*2fe8fb19SBen Gras.Dq Li \&= 179*2fe8fb19SBen Grascharacter or does not contain one at all. 180*2fe8fb19SBen Gras.It Bq Er ENOMEM 181*2fe8fb19SBen GrasThe function 182*2fe8fb19SBen Gras.Fn setenv 183*2fe8fb19SBen Grasor 184*2fe8fb19SBen Gras.Fn putenv 185*2fe8fb19SBen Grasfailed because they were unable to allocate memory for the environment. 186*2fe8fb19SBen Gras.El 187*2fe8fb19SBen Gras.Pp 188*2fe8fb19SBen GrasThe function 189*2fe8fb19SBen Gras.Fn getenv_r 190*2fe8fb19SBen Grascan return the following errors: 191*2fe8fb19SBen Gras.Bl -tag -width Er 192*2fe8fb19SBen Gras.It Bq Er ENOENT 193*2fe8fb19SBen GrasThe variable 194*2fe8fb19SBen Gras.Fa name 195*2fe8fb19SBen Graswas not found in the environment. 196*2fe8fb19SBen Gras.It Bq Er ERANGE 197*2fe8fb19SBen GrasThe value of the named variable is too long to fit in the supplied buffer. 198*2fe8fb19SBen Gras.El 199*2fe8fb19SBen Gras.Sh SEE ALSO 200*2fe8fb19SBen Gras.Xr csh 1 , 201*2fe8fb19SBen Gras.Xr sh 1 , 202*2fe8fb19SBen Gras.Xr execve 2 , 203*2fe8fb19SBen Gras.Xr environ 7 204*2fe8fb19SBen Gras.Sh STANDARDS 205*2fe8fb19SBen GrasThe 206*2fe8fb19SBen Gras.Fn getenv 207*2fe8fb19SBen Grasfunction conforms to 208*2fe8fb19SBen Gras.St -ansiC . 209*2fe8fb19SBen GrasThe 210*2fe8fb19SBen Gras.Fn putenv 211*2fe8fb19SBen Grasfunction conforms to 212*2fe8fb19SBen Gras.St -xpg4 . 213*2fe8fb19SBen GrasThe 214*2fe8fb19SBen Gras.Fn unsetenv 215*2fe8fb19SBen Grasfunction conforms to 216*2fe8fb19SBen Gras.St -p1003.1-2001 . 217*2fe8fb19SBen Gras.Sh HISTORY 218*2fe8fb19SBen GrasThe functions 219*2fe8fb19SBen Gras.Fn setenv 220*2fe8fb19SBen Grasand 221*2fe8fb19SBen Gras.Fn unsetenv 222*2fe8fb19SBen Grasappeared in 223*2fe8fb19SBen Gras.At v7 . 224*2fe8fb19SBen GrasThe 225*2fe8fb19SBen Gras.Fn putenv 226*2fe8fb19SBen Grasfunction appeared in 227*2fe8fb19SBen Gras.Bx 4.3 Reno . 228