C Programming

Getenv Function Usage in C Programming

In the C programming language, several functions help the user acquire relevant information, such as the process name and id. Similarly, in this article, we will discuss information about the environment list that contains the variable name of the environment and then returns the value in the form of a pointer. If the function getenv() cannot find the environment, it returns NULL, and errno displays the error message.

Working on the Getenv in C

In the ecosystem of Linux, the environment variable can be set locally or globally. This variable provides the function to the end-user. That determines how the applications will run on the Linux operating system. There are many ways to categorize the environment variable, such as the environment variable can also be referred to as a shell variable either created at the global or local level.

The getenv() works In such a way that it searches the environment variable dynamically at the run time in every entry along with the specific name. When it is found, it returns a pointer to the value of a variable. The C source code or program must be written in such a way that it does not modify the string that is addressed by the pointer and returned by getenv(). Furthermore, the C programming language does not create a function that sets or modifies the environment variables or any other list or information the user may need.

Declaration of the Getenv() Function

Char *getenv(const char *name)

Return Value

The getenv() function returns the pointer to the c-string having a value of the environment variable that corresponds to env_var. A null pointer is returned when no match is found relevant to the request. These environment variables are useful for all the processes in the current system, either in multiprocessing. These variables contain information like paths of some executable files, a home directory, or a temp directory created to store some temporary files.

Features of Getenv()

The environment variables are found at the system level. There are many ways to utilize the environment variables in the Linux operating system.

Env: This command allows the user to execute the program in the custom environment. When the env command is used without the arguments as parameters, it will display the available options of variables in the system.

Printenv: This command functions to display all the variables in the environment system.

Set: This command works to set the environment variable in the Linux operating system. When we use this command without arguments, it prints all the variable options, shell variables, and functions in the system.

Export: The export command sets the environment variables. But it is temporary because it lasts only when the session lasts. If the session is logged out or closed, then the variable is no more.

All the description regarding getenv is mentioned in the manual of Linux:

$ man getenv

This will lead you to a manual page.

We will share some examples that will display the implementation of the getenv() function in the C programming language in the Linux environment. For all the examples, we will use a text editor of Linux to write codes and the Linux terminal to implement them.

Example 1

In the source code, we use a library <stdlib.h> to ensure the usage of this feature to access the environment. Then by using the getenv() function in the main program, we will get the path, home, and root of the current system.

For the implementation purpose, you can create a dummy user in a Linux environment. But to make it precise, we are using the default one:

Printf(" Path : %sn", getenv("Path"));

A similar format is used for the other two features home and root path:

Now, save the text file with the extension of c. If the information regarding the environment will be available, then it will be displayed. However, if it is not found, NULL will be returned.

Go to the Linux terminal and use a GCC compiler. This compiler is used to compile the code and then execute it:

$ gcc –o getenv getenv.c

$ ./getenv

Getenv.c is the name of a file, and “-o” is used to save the output in a particular file mentioned in the command. The output will display the whole path of the current file and the home directory name. As the root is unknown, so it is declared null.

Example 2

The previous example was quite simple in the case of arguments. This example deals with the pointer used to access the information of the environment. In the main program, the parameters contain the argument and a pointer. For loop is used to display all the information as the manual is displaying consumed time. Each memory slot will be accessed, and the information will be fetched. The loop lasts until the index value is not null:

See the resultant value on the compilation:

On execution, you will see that each minor detail from the start of creating the environment is displayed. You can see that the session manager will display the details of the virtual box configuration. This information will be very detailed that will end by showing the user name as mentioned in the attached picture:

Example 3

Another method of using getenv() is displayed here. A pointer will operate the function, and the return value will be stored in the pointer. First, introduce a pointer in the main program. Then, this pointer will be used with each information getting function.

Str = getenv("Home")) ! = NULL);

This statement shows that the home path will be displayed until the value becomes NULL. The same goes for the other information. Each value obtained will be printed later. Home, Path, and Log name will be displayed according to the default information. These all refer to the environment variable information. But unlike this, the fourth one is the user’s choice data.

On execution of the code, we will see the resultant values. The results for the home path will display the user name in the directory. The second part was for the path of the environment. The third line shows the log name or in actuality, the user name. Now, come toward the user choice, the resultant value is not present as it will not be found. So the null is returned:

Example 4

This example will also display the path only but through a different approach. First, the value is fetched through the function, and then, the third fetched path will be displayed by applying an if-statement to check if each information is displayed until the resultant in the pointer is not NULL.

Ptr_path = getenv("path");

Now, see the resultant value through the terminal. You will see the path is displayed and obtained through the source code:

Conclusion

The article “getenv function usage” in C programming language is explained with a couple of examples to assist the end-user as a user guide. Getenv brings the environment in the form of different ways. Each approach is explained that helps to bring the path, user name, and root of the default environment of the Linux operating system. We hope you found this article helpful. Check out more Linux Hint articles for useful tips and information.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.