mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:27:44 +00:00
LibC: stdlib: Add clearenv() function
This commit is contained in:
parent
3436317c08
commit
853664bd3c
4 changed files with 46 additions and 1 deletions
34
Base/usr/share/man/man3/clearenv.md
Normal file
34
Base/usr/share/man/man3/clearenv.md
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
## Name
|
||||||
|
|
||||||
|
clearenv - clear the environment
|
||||||
|
|
||||||
|
## Synopsis
|
||||||
|
|
||||||
|
```**c++
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
clearenv();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
Clears all environment variables and sets the external
|
||||||
|
variable `environ` to NULL.
|
||||||
|
|
||||||
|
## Return value
|
||||||
|
|
||||||
|
The `clearenv()` function returns zero.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
```c++
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
clearenv();
|
||||||
|
putenv("PATH=/bin");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
|
@ -289,6 +289,16 @@ int unsetenv(const char* name)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int clearenv()
|
||||||
|
{
|
||||||
|
size_t environ_size = 0;
|
||||||
|
for (; environ[environ_size]; ++environ_size) {
|
||||||
|
environ[environ_size] = NULL;
|
||||||
|
}
|
||||||
|
*environ = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int setenv(const char* name, const char* value, int overwrite)
|
int setenv(const char* name, const char* value, int overwrite)
|
||||||
{
|
{
|
||||||
if (!overwrite && getenv(name))
|
if (!overwrite && getenv(name))
|
||||||
|
|
|
@ -47,6 +47,7 @@ __attribute__((alloc_size(2))) void* realloc(void* ptr, size_t);
|
||||||
char* getenv(const char* name);
|
char* getenv(const char* name);
|
||||||
int putenv(char*);
|
int putenv(char*);
|
||||||
int unsetenv(const char*);
|
int unsetenv(const char*);
|
||||||
|
int clearenv(void);
|
||||||
int setenv(const char* name, const char* value, int overwrite);
|
int setenv(const char* name, const char* value, int overwrite);
|
||||||
int atoi(const char*);
|
int atoi(const char*);
|
||||||
long atol(const char*);
|
long atol(const char*);
|
||||||
|
|
|
@ -41,7 +41,7 @@ int main(int argc, char** argv)
|
||||||
for (int idx = 1; idx < argc; ++idx) {
|
for (int idx = 1; idx < argc; ++idx) {
|
||||||
if (idx == 1) {
|
if (idx == 1) {
|
||||||
if (StringView { argv[idx] } == "-i" || StringView { argv[idx] } == "--ignore-environment") {
|
if (StringView { argv[idx] } == "-i" || StringView { argv[idx] } == "--ignore-environment") {
|
||||||
*environ = NULL;
|
clearenv();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue