158f0484fSRodney W. Grimes.\" Copyright (c) 1988, 1991, 1993 258f0484fSRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 358f0484fSRodney W. Grimes.\" 458f0484fSRodney W. Grimes.\" This code is derived from software contributed to Berkeley by 558f0484fSRodney W. Grimes.\" the American National Standards Committee X3, on Information 658f0484fSRodney W. Grimes.\" Processing Systems. 758f0484fSRodney W. Grimes.\" 858f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 958f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions 1058f0484fSRodney W. Grimes.\" are met: 1158f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 1258f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 1358f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 1458f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 1558f0484fSRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 163fb3b97cSEd Maste.\" 3. Neither the name of the University nor the names of its contributors 1758f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 1858f0484fSRodney W. Grimes.\" without specific prior written permission. 1958f0484fSRodney W. Grimes.\" 2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3058f0484fSRodney W. Grimes.\" SUCH DAMAGE. 3158f0484fSRodney W. Grimes.\" 32*873420caSDag-Erling Smørgrav.Dd April 22, 2025 3358f0484fSRodney W. Grimes.Dt GETENV 3 3458f0484fSRodney W. Grimes.Os 3558f0484fSRodney W. Grimes.Sh NAME 36597b0267SMariusz Zaborski.Nm clearenv , 3758f0484fSRodney W. Grimes.Nm getenv , 38*873420caSDag-Erling Smørgrav.Nm getenv_r , 3958f0484fSRodney W. Grimes.Nm putenv , 40adeca214Slucy.Nm secure_getenv , 4158f0484fSRodney W. Grimes.Nm setenv , 4258f0484fSRodney W. Grimes.Nm unsetenv 4358f0484fSRodney W. Grimes.Nd environment variable functions 4425bb73e0SAlexey Zelkin.Sh LIBRARY 4525bb73e0SAlexey Zelkin.Lb libc 4658f0484fSRodney W. Grimes.Sh SYNOPSIS 478aefde06SJeroen Ruigrok van der Werven.In stdlib.h 48597b0267SMariusz Zaborski.Ft int 49597b0267SMariusz Zaborski.Fn clearenv "void" 5058f0484fSRodney W. Grimes.Ft char * 5158f0484fSRodney W. Grimes.Fn getenv "const char *name" 52*873420caSDag-Erling Smørgrav.Ft int 53*873420caSDag-Erling Smørgrav.Fn getenv_r "const char *name" "char *buf" "size_t len" 54adeca214Slucy.Ft char * 55adeca214Slucy.Fn secure_getenv "const char *name" 5658f0484fSRodney W. Grimes.Ft int 5758f0484fSRodney W. Grimes.Fn setenv "const char *name" "const char *value" "int overwrite" 5858f0484fSRodney W. Grimes.Ft int 592966d28cSSean Farley.Fn putenv "char *string" 602966d28cSSean Farley.Ft int 6158f0484fSRodney W. Grimes.Fn unsetenv "const char *name" 6258f0484fSRodney W. Grimes.Sh DESCRIPTION 6358f0484fSRodney W. GrimesThese functions set, unset and fetch environment variables from the 6458f0484fSRodney W. Grimeshost 6558f0484fSRodney W. Grimes.Em environment list . 6658f0484fSRodney W. Grimes.Pp 6758f0484fSRodney W. GrimesThe 68597b0267SMariusz Zaborski.Fn clearenv 69597b0267SMariusz Zaborskifunction clears all environment variables. 70597b0267SMariusz ZaborskiNew variables can be added using 71597b0267SMariusz Zaborski.Fn setenv 72597b0267SMariusz Zaborskiand 73597b0267SMariusz Zaborski.Fn putenv . 74597b0267SMariusz Zaborski.Pp 75597b0267SMariusz ZaborskiThe 7658f0484fSRodney W. Grimes.Fn getenv 77*873420caSDag-Erling Smørgravfunction obtains the current value of the environment variable 78*873420caSDag-Erling Smørgravdesignated by 795c564baeSRuslan Ermilov.Fa name . 8079d9a182STom RhodesThe application should not modify the string pointed 8179d9a182STom Rhodesto by the 8279d9a182STom Rhodes.Fn getenv 8379d9a182STom Rhodesfunction. 8458f0484fSRodney W. Grimes.Pp 85d5df2685SWarner LoshThe 86*873420caSDag-Erling Smørgrav.Fn getenv_r 87*873420caSDag-Erling Smørgravfunction obtains the current value of the environment variable 88*873420caSDag-Erling Smørgravdesignated by 89*873420caSDag-Erling Smørgrav.Fa name 90*873420caSDag-Erling Smørgravand copies it into the buffer 91*873420caSDag-Erling Smørgrav.Fa buf 92*873420caSDag-Erling Smørgravof length 93*873420caSDag-Erling Smørgrav.Fa len . 94*873420caSDag-Erling Smørgrav.Pp 95*873420caSDag-Erling SmørgravThe 96adeca214Slucy.Fn secure_getenv 97d5df2685SWarner Loshreturns 98d5df2685SWarner Losh.Va NULL 99d5df2685SWarner Loshwhen the environment cannot be trusted, otherwise it acts like 100d5df2685SWarner Losh.Fn getenv . 101d5df2685SWarner LoshThe environment currently is not trusted when 102f5f37c3fSJens Schweikhardt.Xr issetugid 2 103d5df2685SWarner Loshreturns a non-zero value, but other conditions may be added 104d5df2685SWarner Loshin the future. 105adeca214Slucy.Pp 10658f0484fSRodney W. GrimesThe 10758f0484fSRodney W. Grimes.Fn setenv 10858f0484fSRodney W. Grimesfunction inserts or resets the environment variable 1095c564baeSRuslan Ermilov.Fa name 11058f0484fSRodney W. Grimesin the current environment list. 11158f0484fSRodney W. GrimesIf the variable 1125c564baeSRuslan Ermilov.Fa name 11358f0484fSRodney W. Grimesdoes not exist in the list, 11458f0484fSRodney W. Grimesit is inserted with the given 1155c564baeSRuslan Ermilov.Fa value . 11658f0484fSRodney W. GrimesIf the variable does exist, the argument 1175c564baeSRuslan Ermilov.Fa overwrite 11858f0484fSRodney W. Grimesis tested; if 1195c564baeSRuslan Ermilov.Fa overwrite 1200b0ea948STom Rhodesis zero, the 12158f0484fSRodney W. Grimesvariable is not reset, otherwise it is reset 12258f0484fSRodney W. Grimesto the given 1235c564baeSRuslan Ermilov.Fa value . 12458f0484fSRodney W. Grimes.Pp 12558f0484fSRodney W. GrimesThe 12658f0484fSRodney W. Grimes.Fn putenv 1272966d28cSSean Farleyfunction takes an argument of the form ``name=value'' and 1282966d28cSSean Farleyputs it directly into the current environment, 1292966d28cSSean Farleyso altering the argument shall change the environment. 1302966d28cSSean FarleyIf the variable 1312966d28cSSean Farley.Fa name 1322966d28cSSean Farleydoes not exist in the list, 1332966d28cSSean Farleyit is inserted with the given 1342966d28cSSean Farley.Fa value . 1352966d28cSSean FarleyIf the variable 1362966d28cSSean Farley.Fa name 1372966d28cSSean Farleydoes exist, it is reset to the given 1382966d28cSSean Farley.Fa value . 13958f0484fSRodney W. Grimes.Pp 14058f0484fSRodney W. GrimesThe 14158f0484fSRodney W. Grimes.Fn unsetenv 14258f0484fSRodney W. Grimesfunction 14358f0484fSRodney W. Grimesdeletes all instances of the variable name pointed to by 14458f0484fSRodney W. Grimes.Fa name 14558f0484fSRodney W. Grimesfrom the list. 146ef6ccfb5SSean Farley.Pp 147ef6ccfb5SSean FarleyIf corruption (e.g., a name without a value) is detected while making a copy of 148ef6ccfb5SSean Farleyenviron for internal usage, then 149ef6ccfb5SSean Farley.Fn setenv , 150ef6ccfb5SSean Farley.Fn unsetenv 151ef6ccfb5SSean Farleyand 152ef6ccfb5SSean Farley.Fn putenv 153ef6ccfb5SSean Farleywill output a warning to stderr about the issue, drop the corrupt entry and 154ef6ccfb5SSean Farleycomplete the task without error. 15558f0484fSRodney W. Grimes.Sh RETURN VALUES 156af73aa7cSHiten PandyaThe 157af73aa7cSHiten Pandya.Fn getenv 1582410103cSRuslan Ermilovfunction returns the value of the environment variable as a 1592410103cSRuslan Ermilov.Dv NUL Ns 1602410103cSRuslan Ermilov-terminated string. 161af73aa7cSHiten PandyaIf the variable 162af73aa7cSHiten Pandya.Fa name 163af73aa7cSHiten Pandyais not in the current environment, 164af73aa7cSHiten Pandya.Dv NULL 165af73aa7cSHiten Pandyais returned. 166af73aa7cSHiten Pandya.Pp 167adeca214SlucyThe 168adeca214Slucy.Fn secure_getenv 169adeca214Slucyfunction returns 170adeca214Slucy.Dv NULL 171adeca214Slucyif the process is in "secure execution," otherwise it will call 172adeca214Slucy.Fn getenv . 173adeca214Slucy.Pp 174*873420caSDag-Erling Smørgrav.Rv -std clearenv getenv_r setenv putenv unsetenv 17558f0484fSRodney W. Grimes.Sh ERRORS 176274b6244SAlexey Zelkin.Bl -tag -width Er 1772966d28cSSean Farley.It Bq Er EINVAL 1782966d28cSSean FarleyThe function 1792966d28cSSean Farley.Fn getenv , 180*873420caSDag-Erling Smørgrav.Fn getenv_r , 1812966d28cSSean Farley.Fn setenv 1822966d28cSSean Farleyor 1832966d28cSSean Farley.Fn unsetenv 1842966d28cSSean Farleyfailed because the 1852966d28cSSean Farley.Fa name 1862966d28cSSean Farleyis a 1872966d28cSSean Farley.Dv NULL 1882966d28cSSean Farleypointer, points to an empty string, or points to a string containing an 1892966d28cSSean Farley.Dq Li \&= 1902966d28cSSean Farleycharacter. 1912966d28cSSean Farley.Pp 1922966d28cSSean FarleyThe function 1932966d28cSSean Farley.Fn putenv 1942966d28cSSean Farleyfailed because 1952966d28cSSean Farley.Fa string 1962966d28cSSean Farleyis a 1972966d28cSSean Farley.Dv NULL 1982966d28cSSean Farleypointer, 199a3fb6da9SGlen Barber.Fa string 200a3fb6da9SGlen Barberis without an 2012966d28cSSean Farley.Dq Li \&= 2022966d28cSSean Farleycharacter or 2032966d28cSSean Farley.Dq Li \&= 2042966d28cSSean Farleyis the first character in 2052966d28cSSean Farley.Fa string . 2062966d28cSSean FarleyThis does not follow the 2072966d28cSSean Farley.Tn POSIX 2082966d28cSSean Farleyspecification. 209*873420caSDag-Erling Smørgrav.It Bq Er ENOENT 210*873420caSDag-Erling SmørgravThe function 211*873420caSDag-Erling Smørgrav.Fn getenv_r 212*873420caSDag-Erling Smørgravfailed because the requested variable was not found in the 213*873420caSDag-Erling Smørgravenvironment. 21458f0484fSRodney W. Grimes.It Bq Er ENOMEM 21558f0484fSRodney W. GrimesThe function 2162966d28cSSean Farley.Fn setenv , 2172966d28cSSean Farley.Fn unsetenv 21858f0484fSRodney W. Grimesor 21958f0484fSRodney W. Grimes.Fn putenv 22058f0484fSRodney W. Grimesfailed because they were unable to allocate memory for the environment. 221*873420caSDag-Erling Smørgrav.It Bq Er ERANGE 222*873420caSDag-Erling SmørgravThe function 223*873420caSDag-Erling Smørgrav.Fn getenv_r 224*873420caSDag-Erling Smørgravfailed because the value of the requested variable was too long to fit 225*873420caSDag-Erling Smørgravin the provided buffer. 22658f0484fSRodney W. Grimes.El 22758f0484fSRodney W. Grimes.Sh SEE ALSO 22858f0484fSRodney W. Grimes.Xr csh 1 , 22958f0484fSRodney W. Grimes.Xr sh 1 , 23058f0484fSRodney W. Grimes.Xr execve 2 , 23158f0484fSRodney W. Grimes.Xr environ 7 23258f0484fSRodney W. Grimes.Sh STANDARDS 23358f0484fSRodney W. GrimesThe 23458f0484fSRodney W. Grimes.Fn getenv 23558f0484fSRodney W. Grimesfunction conforms to 236588a200cSRuslan Ermilov.St -isoC . 2372966d28cSSean FarleyThe 2382966d28cSSean Farley.Fn setenv , 2392966d28cSSean Farley.Fn putenv 2402966d28cSSean Farleyand 2412966d28cSSean Farley.Fn unsetenv 2422966d28cSSean Farleyfunctions conforms to 2432966d28cSSean Farley.St -p1003.1-2001 . 244d5df2685SWarner LoshThe 245d5df2685SWarner Losh.Fn secure_getenv 246dfa0ac74SEd Mastefunction conforms to 247dfa0ac74SEd Maste.St -p1003.1-2024 . 24824a0682cSRuslan Ermilov.Sh HISTORY 24924a0682cSRuslan ErmilovThe functions 25024a0682cSRuslan Ermilov.Fn setenv 25124a0682cSRuslan Ermilovand 25224a0682cSRuslan Ermilov.Fn unsetenv 25324a0682cSRuslan Ermilovappeared in 25424a0682cSRuslan Ermilov.At v7 . 25524a0682cSRuslan ErmilovThe 25624a0682cSRuslan Ermilov.Fn putenv 25724a0682cSRuslan Ermilovfunction appeared in 25824a0682cSRuslan Ermilov.Bx 4.3 Reno . 2592966d28cSSean Farley.Pp 2602966d28cSSean FarleyUntil 2612966d28cSSean Farley.Fx 7.0 , 2622966d28cSSean Farley.Fn putenv 2632966d28cSSean Farleywould make a copy of 2642966d28cSSean Farley.Fa string 2652966d28cSSean Farleyand insert it into the environment using 2662966d28cSSean Farley.Fn setenv . 2672966d28cSSean FarleyThis was changed to use 2682966d28cSSean Farley.Fa string 2692966d28cSSean Farleyas the memory location of the ``name=value'' pair to follow the 2702966d28cSSean Farley.Tn POSIX 2712966d28cSSean Farleyspecification. 272597b0267SMariusz Zaborski.Pp 273597b0267SMariusz ZaborskiThe 274597b0267SMariusz Zaborski.Fn clearenv 275d5df2685SWarner Loshand 276d5df2685SWarner Losh.Fn secure_getenv 277d5df2685SWarner Loshfunctions were added in 278597b0267SMariusz Zaborski.Fx 14 . 279*873420caSDag-Erling Smørgrav.Pp 280*873420caSDag-Erling SmørgravThe 281*873420caSDag-Erling Smørgrav.Fn getenv_r 282*873420caSDag-Erling Smørgravfunction first appeared in 283*873420caSDag-Erling Smørgrav.Nx 4.0 284*873420caSDag-Erling Smørgravand was added in 285*873420caSDag-Erling Smørgrav.Fx 15 . 2865059d6a6SArchie Cobbs.Sh BUGS 2875059d6a6SArchie CobbsSuccessive calls to 2885059d6a6SArchie Cobbs.Fn setenv 2892966d28cSSean Farleythat assign a larger-sized 2905c564baeSRuslan Ermilov.Fa value 2912966d28cSSean Farleythan any previous value to the same 2925c564baeSRuslan Ermilov.Fa name 2931a0a9345SRuslan Ermilovwill result in a memory leak. 2941a0a9345SRuslan ErmilovThe 295b5c508fbSRuslan Ermilov.Fx 2962966d28cSSean Farleysemantics for this function 2975059d6a6SArchie Cobbs(namely, that the contents of 2985c564baeSRuslan Ermilov.Fa value 2995059d6a6SArchie Cobbsare copied and that old values remain accessible indefinitely) make this 3001a0a9345SRuslan Ermilovbug unavoidable. 3011a0a9345SRuslan ErmilovFuture versions may eliminate one or both of these 3025059d6a6SArchie Cobbssemantic guarantees in order to fix the bug. 303