Each process receives an environment table when it is started. The environment table consists of an array of character pointers, each containing the address of a null-terminated string, and the global variable Environ contains the address of the array of pointers:
extern char **environ;
In Linux, environment variables consist of strings such as name=value, an environment table structure consisting of 5 environment variables:
If you need to use the Environ variable to view the entire environment, you can use the getenv and PUCENV functions if you only read a variable.
Related functions:
#include <stdlib.h>char *getenv (const char *name);
Description
The return value of the function is the address of the value string in Name=value and returns null if it is not found.
#include <stdlib.h>int putenv (char *str); int setenv (const char *name, const char *value, int rewrite); int unsetenv (c Onst Char *name);
Description
function return value: Returns 0 if successful, and returns a value other than 0 if the error occurs.
The putenv operation is to put the Name=value string into the environment table, and if name exists, first delete its original definition.
Setenv set name to value. If name exists in the environment, when rewrite is not 0, its existing definition is first deleted. When rewrite is 0, its existing definition is not deleted (name is not set to the new value, and there is no error);
Unsetenv Delete the definition of name. There is no error even if name does not exist.
The difference between putenv and setenv:
Setenv must allocate a store to create a Name=value string based on its parameters. At the same time, putenv does not need to put the parameter string passed to it directly into the environment.
Note: When using putenv, an error occurs when a string stored in the stack is passed as a parameter to the function, because the storage area that its stack frame occupies may be reused when returning from the current function.
Actions when modifying the environment table:
Environment tables and environment strings in a process are typically placed at the top of the process storage space. Deleting a string is as simple as deleting a string in the environment table and moving its subsequent pointer to the top order of the environment table, but adding or modifying a string is different because the environment table usually occupies the top of the process address space and cannot be extended to the high address direction (up). Also cannot move a stack frame below it, so it cannot be extended to the low address direction. The combination of the two makes the length of the space not increasing.
To modify an existing name:
If the length of the new value is less than or equal to the length of the existing value, just modify its source content directly. If the length of the new value is greater than the source length, you must call malloc to allocate space for the new string, and then point the pointer for name to the new string.
Add a new environment variable:
If you add an environment variable for the first time, you will need to copy the environment table to the new allocation area after allocating space for the new environment variable, that is, allocating a space to the environment table again, then storing the address of the new environment variable in the footer of the environment table, and finally adding a null pointer at the end of the environment table. Of course, you need to point environ to the new environment table.
If the environment variable is not added for the first time, only the address of the new environment variable is added to the end of the environment table, and a new null pointer is added at the end of the environment table.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux Programming: Environment table