Common Linux C functions-Environment Variables

Source: Internet
Author: User
Common Linux C functions-environment variables-general Linux technology-Linux programming and kernel information. For more information, see the following section. Getenv (get environment variable content)
Putenv, setenv, unsetenv

Header file # include

Define the function char * getenv (const char * name );

Function Description getenv () is used to obtain the content of the parameter name environment variable. The parameter name is the name of the environment variable. If the variable exists, a pointer to the content is returned. The environment variable format is name = value.

If the returned value is successfully executed, a pointer to the content is returned. If no conforming environment variable name is found, NULL is returned.

Example # include
Mian ()
{
Char * p;
If (p = getenv ("USER ")))
Printf ("USER = % s \ n", p );
}

Run USER = root

 




Putenv (change or add environment variables)
Related functions: getenv, setenv, and unsetenv

Header file # include4

Defines the int putenv (const char * string) function );

Function Description putenv () is used to change or add environment variables. The format of the parameter string is name = value. If the environment variable originally exists, the content of the variable changes according to the string parameter. Otherwise, the content of this parameter becomes a new environment variable.

If the return value is successfully executed, 0 is returned. If an error occurs,-1 is returned.

The error code ENOMEM memory is insufficient and a new environment variable space cannot be configured.

Example # include
Main ()
{
Char * p;
If (p = getenv ("USER ")))
Printf ("USER = % s \ n", p );
Putenv ("USER = test ");
Printf ("USER + 5s \ n", getenv ("USER "));
}

Run USER = root
USER = root

 




Setenv (change or add environment variables)
Related functions: getenv, putenv, and unsetenv

Header file # include

Defines the function int setenv (const char * name, const char * value, int overwrite );

Function Description: setenv () is used to change or add environment variables. The parameter name is an environment variable name string.

The value parameter is the content of the variable. overwrite is used to determine whether to change the existing environment variable. If the value of overwrite is not 0 and the environment variable already exists, the original content will be changed to the variable content referred to by the parameter value. If overwrite is 0 and the environment variable already contains content, the parameter value is ignored.

If the return value is successfully executed, 0 is returned. If an error occurs,-1 is returned.

Error code: insufficient ENOMEM memory, unable to configure new environment variable space

Example # include
Main ()
{
Char * p;
If (p = getenv ("USER ")))
Printf ("USER = % s \ n", p );
Setenv ("USER", "test", 1 );
Printf ("USER = % s \ n", getenv ("USEr "));
Unsetenv ("USER ");
Printf ("USER = % s \ n", getenv ("USER "));
}

Run USER = root
USER = test
USER = (null)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.