Index: include/stdlib.h =================================================================== --- include/stdlib.h +++ include/stdlib.h @@ -271,6 +271,9 @@ size_t, int (^ _Nonnull)(const void *, const void *)); #endif char *getbsize(int *, long *); + +int clearenv(void); + /* getcap(3) functions */ char *cgetcap(char *, const char *, int); int cgetclose(void); Index: lib/libc/stdlib/Symbol.map =================================================================== --- lib/libc/stdlib/Symbol.map +++ lib/libc/stdlib/Symbol.map @@ -126,6 +126,7 @@ qsort_s; rand; srand; + clearenv; }; FBSDprivate_1.0 { Index: lib/libc/stdlib/getenv.3 =================================================================== --- lib/libc/stdlib/getenv.3 +++ lib/libc/stdlib/getenv.3 @@ -32,10 +32,11 @@ .\" @(#)getenv.3 8.2 (Berkeley) 12/11/93 .\" $FreeBSD$ .\" -.Dd June 20, 2007 +.Dd January 17, 2021 .Dt GETENV 3 .Os .Sh NAME +.Nm clearenv , .Nm getenv , .Nm putenv , .Nm setenv , @@ -45,6 +46,8 @@ .Lb libc .Sh SYNOPSIS .In stdlib.h +.Ft int +.Fn clearenv "void" .Ft char * .Fn getenv "const char *name" .Ft int @@ -59,6 +62,14 @@ .Em environment list . .Pp The +.Fn clearenv +function clears all environment variables. +New variables can be added using +.Fn setenv +and +.Fn getenv . +.Pp +The .Fn getenv function obtains the current value of the environment variable, .Fa name . @@ -128,7 +139,7 @@ .Dv NULL is returned. .Pp -.Rv -std setenv putenv unsetenv +.Rv -std clearenv setenv putenv unsetenv .Sh ERRORS .Bl -tag -width Er .It Bq Er EINVAL @@ -211,6 +222,13 @@ as the memory location of the ``name=value'' pair to follow the .Tn POSIX specification. +.Pp +The +.Fn clearenv +function first appeared in glibc. +It was never part of any standard. +Support for it was added in +.Fx 13 . .Sh BUGS Successive calls to .Fn setenv Index: lib/libc/stdlib/getenv.c =================================================================== --- lib/libc/stdlib/getenv.c +++ lib/libc/stdlib/getenv.c @@ -691,3 +691,28 @@ return (0); } + +/* + * Unset all variable by flagging it as inactive. No variable is + * ever freed. + */ +int +clearenv(void) +{ + int ndx; + + /* Initialize environment. */ + if (__merge_environ() == -1 || (envVars == NULL && __build_env() == -1)) + return (-1); + + /* Remove from the end to not shuffle memory too much. */ + for (ndx = envVarsTotal - 1; ndx >= 0; ndx--) { + envVars[ndx].active = false; + if (envVars[ndx].putenv) + __remove_putenv(ndx); + } + + __rebuild_environ(0); + + return (0); +} Index: lib/libc/tests/stdlib/Makefile =================================================================== --- lib/libc/tests/stdlib/Makefile +++ lib/libc/tests/stdlib/Makefile @@ -2,6 +2,7 @@ .include +ATF_TESTS_C+= clearenv_test ATF_TESTS_C+= dynthr_test ATF_TESTS_C+= heapsort_test ATF_TESTS_C+= mergesort_test