Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F133061831
D28223.id82485.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
D28223.id82485.diff
View Options
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 <src.opts.mk>
+ATF_TESTS_C+= clearenv_test
ATF_TESTS_C+= dynthr_test
ATF_TESTS_C+= heapsort_test
ATF_TESTS_C+= mergesort_test
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Oct 23, 2:33 PM (21 h, 40 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
24093398
Default Alt Text
D28223.id82485.diff (2 KB)
Attached To
Mode
D28223: libc: add cleanenv function
Attached
Detach File
Event Timeline
Log In to Comment