【发布时间】:2015-08-26 15:09:06
【问题描述】:
我知道这对于假设在堆栈溢出上发布的内容有点偏离主题,但我想知道这些示例是否做得很好。
setenv:
包括: #include <stdlib.h>
声明: int setenv(const char *v_name, const char *v_value, int overwrite);
返回:如果成功返回零,否则返回-1,并设置errno以指示错误原因。
getenv:
包括: #include <stdlib.h>
声明: char *getenv(const char *name)
返回:如果成功返回一个指向环境中值的指针,如果没有匹配则返回NULL。
示例
char *ppath 在以下示例中用作变量。
示例 1:此示例显示当 overwrite 参数为非零且 v_name 具有值时发生的情况。
ppath = getenv("PWD"); //puts the environment of $PWD into ppath
if(ppath == NULL) //error checking
perror("getenv");
cout << "$PWD = " << ppath << endl; //prints the environment of $PWD
这段代码的输出是 $PWD = /class/classes/username/CS100 这是因为 char 变量 ppath 正在获取
来自getenv("PWD") 的环境和$PWD 的环境是/class/classes/username/CS100。因此ppath 指向那个
环境。还有适当的错误检查以确保$PWD 有一个环境,如果它没有perror
被调用。
ppath = getenv("HOME"); //gets the environment of the $HOME
if(ppath == NULL) //error checking
perror("getenv");
cout << "$HOME = " << ppath << endl; //prints the environment of $PWD
这段代码的输出是 $HOME = /class/classes/username 这是因为 char 变量 ppath 正在获取
来自getenv("HOME") 的环境和$HOME 的环境是/class/classes/username。因此ppath 指向那个
环境。还有适当的错误检查以确保$HOME 有一个环境,如果它没有perror
被调用。
if(-1==setenv("PWD",ppath,1)) //since the overwrite parameter is non-zero it replaces environment
perror("setenv"); //of $PWD with the value of ppath which is defined by the environment
//of $HOME
这是为了更改$PWD 的环境,在这种情况下,它采用ppath 指向的值,在这种情况下是/class/classes/username,也是$HOME。 overwrite参数非零,所以将$PWD的环境替换为ppath
ppath = getenv("PWD"); //gets the environment of $PWD
if(ppath == NULL) //error checking
perror("getenv");
cout << "$PWD = " << ppath << endl; //the value should now be the same as the value of $HOME
这段代码的输出是 $PWD = /class/classes/username 这是因为 char 变量 ppath 正在获取
来自getenv("PWD") 的环境和$PWD 的环境是/class/classes/username。因此ppath 指向那个
环境。还有适当的错误检查以确保$PWD 有一个环境,如果它没有perror
叫做。示例 1 的完整输出如下。
输出 1:
$PWD = /class/classes/username/CS100
$HOME = /class/classes/username
$PWD = /class/classes/username
示例 2:此示例显示当 overwrite 参数为非零且 v_name 具有空值(如 v_name = "" 中的空字符串)时会发生什么情况。
ppath = getenv("PWD");
if(ppath == NULL)
perror("getenv");
cout << "$PWD = " << ppath << endl; //in this case ppath ="" because the environment of $PWD is not set
以下代码的输出将是$PWD = 这是因为$PWD 的环境在这种情况下设置为"",它使ppath 指向一个空字符串。因此它不会抛出错误的原因,但是如果 $PWD 是未定义的变量,则调用 perror。
ppath = getenv("HOME");
if(ppath == NULL)
perror("getenv");
cout << "$HOME = " << ppath << endl;
输出:这段代码是 $HOME = /class/classes/username 这是因为 char 变量 ppath 正在获取
来自getenv("HOME") 的环境和$HOME 的环境是/class/classes/username。因此ppath 指向那个
环境。还有适当的错误检查以确保$HOME 有一个环境,如果它没有perror
被调用。
if(-1==setenv("PWD",ppath,1)) //since the overwrite parameter is non-zero it replaces environment
perror("setenv"); //of $PWD with the value of ppath which is defined by the environment
//of $HOME
由于 overwrite 参数为 1,因此将 $PWD 设置为前面示例中定义的 ppath 并不重要。
ppath = getenv("PWD");
if(ppath == NULL)
perror("getenv");
cout << "$PWD = " << ppath << endl; //the value should now be the same as the value of $HOME
以下代码的输出是$PWD = /class/classes/username 这是因为setenv 将过去代码块中定义的$PWD 的值更改为ppath。
输出 2:
$PWD =
$HOME = /class/classes/username
$PWD = /class/classes/username
示例 3:此示例显示当 overwrite 参数为零且 v_name 确实有值时会发生什么。
ppath = getenv("PWD");
if(ppath == NULL)
perror("getenv");
cout << "$PWD = " << ppath << endl;
在这种情况下,ppath 设置为$PWD 的环境。这个代码块的输出是
$PWD = /class/classes/username/CS100.
ppath = getenv("HOME");
if(ppath == NULL)
perror("getenv");
cout << "$HOME = " << ppath << endl;
在此代码块中,ppath 设置为$HOME 的环境。以下代码块的输出将是
$HOME = /class/classes/username.
if(-1==setenv("PWD",ppath,0)) //since the overwrite parameter is zero it does not replaces
perror("setenv"); //environment of $PWD with ppath.
这里有一个overwrite 参数为零的新情况。在这种情况下,ppath 是什么并不重要,因为
零标志使得$PWD 不会被ppath 替换,除非$PWD 是未定义的变量,在这种情况下
$PWD 将被赋予 ppath 的值。
ppath = getenv("PWD");
if(ppath == NULL)
perror("getenv");
cout << "$PWD = " << ppath << endl; //the value should not be changed.
这个代码块的输出是$PWD = /class/classes/username/CS100。这是因为setenv 没有更改
$PWD 的环境,因为 overwrite 参数。
输出 3:
$PWD = /class/classes/username/CS100
$HOME = /class/classes/username
$PWD = /class/classes/username/CS100
示例 4:此示例显示当overwrite 参数为零或非零且v_name 是未在环境中定义的参数时发生的情况。
ppath = getenv("HOME");
if(ppath == NULL)
perror("getenv");
cout << "$HOME = " << ppath << endl;
在这种情况下 ppath 被赋予$HOME 的环境,还进行了适当的错误检查。代码的输出
块是$HOME = /class/classes/username。
if(-1==setenv("random_name",ppath,0)) //since the overwrite parameter is zero and the variable
perror("setenv"); //$random_name is undefined, setenv makes the environment of
//$random_variable is the value of ppath. If the case where
//there is a undefined variable the setenv behaves the
//same way regardless of a non-zero or zero overwrites parameter.
在这种情况下,overwrite 参数仍然为零,但 v_name 未声明,因此这是 overwrite 零参数有帮助的地方。在这种情况下,random_name 设置为ppath,它指向$HOME 的环境,即/class/classes/username。
ppath = getenv("random_name"); //gets the value of $PWD
if(ppath == NULL)
perror("getenv");
cout << "$random_name = " << ppath << endl; //the value should now be the same as the value of $HOME
ppath 包含$random_name 的环境,由前面代码块中的setenv 设置。输出
这个代码块是 $random_name = /class/classes/username
输出 4:
$HOME = /class/classes/username
$random_name = /class/classes/username
【问题讨论】:
-
Documentation for setenv 的可能副本
-
我已经在您的duplicate question 中解决了其中的一些问题。你似乎没有注意到太多。
-
实际上你可以注意到这两篇文章有非常不同的文档,因为在这篇文章中没有描述,只是示例和示例描述。
-
如果您提供了指向真实文档的链接,则无需重复任何内容,否则您当然应该为所有参数提供文档。不清楚你在问什么。
标签: c++ linux environment-variables system-calls