Talk C chestnuts together (137th back: C language instance -- view environment variables)
Hello, everyone. We talked about the exec series functions in the last time. This is an example of viewing environment variables. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
When talking about exec functions, we mentioned the running environment of the program. Some readers may not understand it. Let's take a look at the environment variables in the running environment. Environment variables are often used in programming, such as commonly used PATH environment variables. Sometimes the program cannot run because of an environment variable error.
Environment variables are a type of parameter provided by the operating system. A program can run these parameters in the operating system. They are essentially key-value pairs.
For example, SHELL =/bin/bash.
SHELL is the name of the environment variable, and its value is the content after the equal sign. It prompts that the shell program we run is/bin/bash. We can regard SHELL as a key and the content after the equal sign as a value. This is its essence: Key-value pairs.
We can use commands on the terminal to view the current environment variables. Common Commands include echo, set, and env.
There are many environment variables, such as the commonly used SHELL and PATH. You can use the echo command to view the value of a single environment variable, provided that
You need to know the name of the environment variable.
$ Echo $ SHELL // use the echo command to view the value of a single environment variable/bin/bash $ echo $ PATH/usr/local/sbin:/usr/local/bin: /usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
If you forget the name of the environment variable, you can use the set command to view the environment variable in the current terminal.
$ Set // run the set command BASH =/bin/bashHISTFILESIZE = 2000 HISTSIZE = 1000 HOME =/home/talk8HOSTNAME = talk8-PCHOSTTYPE = i686LOGNAME = talk8
Only some common environment variables are selected here, and other environment variables are not listed one by one.
In addition, the env command can also view environment variables, but it is more extensive than the set command. It can view all the environment variables in the current system.
$ Env // run env command LC_PAPER = zh_CN.UTF-8TERM = xtermSHELL =/bin/bashUSER = talk8USERNAME = talk8PATH =/usr/local/sbin:/usr/local/bin: /usr/sbin:/usr/bin:/sbin:/bin:/usr/gamesLANG = zh_CN.UTF-8HOME =/home/talk8gnome_topics top_session_id = this-is-deprecatedXDG_SESSION_DESKTOP = defaultLOGNAME = talk8
There are many environment variables in the system. Here, only some common environment variables are selected, and other environment variables are not listed one by one.
There are already many spectators in the console to test these commands, haha. However, do not forget that we are talkC su Zi. Apart from these commands, we can also view the environment variables in the C program.
The system provides the getenv function to view environment variables. The following is a prototype of the function:
char *getenv(const char *name);
A function has only one parameter, which is a character pointer used to receive the variable name. The function returns a value that matches the variable name. If the variable name does not exist or the variable does not have a value, a null pointer is returned;
The following is the core code of the program. For more information, see
char *env_key = "SHELL"; char *env_value = NULL; env_value = getenv(env_key); if(NULL != env_value) printf("the value of %s is %s \n",env_key,env_value); else printf("there is not any value of %s \n",env_key);
In the code, we use the getenv function to obtain the SHELL value of the environment variable and display it to the terminal. The following shows the program running result:
the value of SHELL is /bin/bash
As you can see, this result is exactly the same as the result we just obtained using the echo command.
As you may have noticed, you need to know the environment variable name before using this function. Like the echo command, is there any function that can view all the environment variables like the set and env commands. The answer is no. The audience has been stunned, haha. Do not be disappointed. If there is no function, you can write it by yourself? How to write? I am prompted that all the environment variables in the system are stored in a variable named environ, but this variable is a pointer to a string. It is defined as follows:
char **environ;
Do you understand how it works? We write faster than others. The code I wrote is as follows:
extern char ** environ; char ** env_array = NULL; env_array = environ; while(NULL != *env_array) printf("%s \n",*env_array++);
For the code, I should pay attention to the following points:
We need to declare the environ variable before using it. We can regard this variable as a string array, and the last element of this array is a null pointer. After understanding this, it is as easy to display environment variables as traversing arrays.
I will not list the running results of the program. I believe you have completed the program yourself. You can compare the running results of the program with the running results of the env command, and you will find that they are exactly the same.
The complete code is stored in my resources. You can download and use it.
Finally, let's summarize the methods for viewing environment variables:
Use Linux Command: echo, set and env. Use Function: getenv. This function returns the same result as echo. Use the variable "environ. The result of this variable is the same as that of the env command.
Let's talk about the example of viewing environment variables. I want to know what examples will be provided later, and I will try again.