putenv ()- 更改/添加环境变量
格式
#include <stdlib.h>
int putenv(const char *varname);语言级别
XPG4
线程安全
是
作业 CCSID 接口
发送到此函数的所有字符数据都应该使用作业的 CCSID。 此函数返回的所有字符数据都使用作业的 CCSID。 请参阅 了解 CCSID 和语言环境 以获取更多信息。
描述
putenv() 函数通过改变现有变量或创建新变量来设置环境变量的值。 varname 参数指向格式为 var=x的字符串,其中 x 是环境变量 var的新值。
name 不能包含空白或等于 (=) 符号。 例如
PATH NAME=/my_lib/joe_user由于 PATH 与 NAME之间存在空白,因此无效。 类似的示例, PATH=NAME=/my_lib/joe_user由于 PATH 与 NAME之间的符号相等,因此无效。 系统将第一个等号后面的所有字符解释为环境变量的 值 。返回值
如果成功, putenv() 函数将返回 0。 如果 putenv() 失败,则返回 -1 并设置 errno 以指示错误。
示例
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *pathvar;
if (-1 == putenv("PATH=/:/home/userid")) {
printf("putenv failed \n");
return EXIT_FAILURE;
}
/* getting and printing the current environment path */
pathvar = getenv("PATH");
printf("The current path is: %s\n", pathvar);
return 0;
}
/**********************************************************
The output should be:
The current path is: /:/home/userid