设置环境、取消设置环境、放置环境
我正在为系统编程课程开发自定义 shell。我们被指示实现内置的 setenv()
和 unsetenv()
命令,并提示检查 putenv() 的手册页
代码>。
我的问题是 setenv(char*, char*, int)
和 putenv(char*)
似乎根本不起作用。我用于执行输入命令的代码如下:
//... skipping past stuff for IO redirection
pid = fork();
if(pid == 0){
//child
if(!strcmp(_simpleCommands[0]->_arguments[0],"printenv")){
//check if command is "printenv"
extern char **environ;
int i;
for(i = 0; environ[i] != NULL; i++){
printf("%s\n",environ[i]);
}
exit(0);
}
if(!strcmp(_simpleCommands[0]->_arguments[0],"setenv")){
//if command is "setenv" get parameters char* A, char* B
char * p = _simpleCommands[0]->_arguments[1];
char * s = _simpleCommands[0]->_arguments[2];
//putenv(char* s) needs to be formatted A=B; A is variable B is value
char param[strlen(p) + strlen(s) + 1];
strcat(param,p);
strcat(param,"=");
strcat(param,s);
putenv(param);
//setenv(p,s,1);
exit(0);
}
if(!strcmp(_simpleCommands[0]->_arguments[0],"unsetenv")){
//remove environment variable
unsetenv(_simpleCommands[0]->_arguments[0]);
exit(0);
}
//execute command
execvp(_simpleCommands[0]->_arguments[0],_simpleCommands->_arguments);
perror("-myshell");
_exit(1);
}
//omitting restore IO defaults...
如果我运行 printenv
它可以正常工作,但是如果我尝试使用 putenv()
或 设置新变量>setenv()
我的 printenv()
命令返回完全相同的内容,因此它似乎不起作用。
附带说明一下,问题可能不在于函数或我如何调用它们,因为我的 shell 正在执行命令,就好像它必须格式化通配符(*
或 ?) 我不确定是否会发生。
I am working on a custom shell for a systems programming class. We were instructed to implement the built-in setenv()
and unsetenv()
commands with a hint of check man pages for putenv()
.
My issue is that setenv(char*, char*, int)
and putenv(char*)
do not seem to be working at all. My code for executing a command entered is as follows:
//... skipping past stuff for IO redirection
pid = fork();
if(pid == 0){
//child
if(!strcmp(_simpleCommands[0]->_arguments[0],"printenv")){
//check if command is "printenv"
extern char **environ;
int i;
for(i = 0; environ[i] != NULL; i++){
printf("%s\n",environ[i]);
}
exit(0);
}
if(!strcmp(_simpleCommands[0]->_arguments[0],"setenv")){
//if command is "setenv" get parameters char* A, char* B
char * p = _simpleCommands[0]->_arguments[1];
char * s = _simpleCommands[0]->_arguments[2];
//putenv(char* s) needs to be formatted A=B; A is variable B is value
char param[strlen(p) + strlen(s) + 1];
strcat(param,p);
strcat(param,"=");
strcat(param,s);
putenv(param);
//setenv(p,s,1);
exit(0);
}
if(!strcmp(_simpleCommands[0]->_arguments[0],"unsetenv")){
//remove environment variable
unsetenv(_simpleCommands[0]->_arguments[0]);
exit(0);
}
//execute command
execvp(_simpleCommands[0]->_arguments[0],_simpleCommands->_arguments);
perror("-myshell");
_exit(1);
}
//omitting restore IO defaults...
If I run printenv
it works properly, but if I try to set a new variable using either putenv()
or setenv()
my printenv()
command returns the exact same thing, so it does not appear to be working.
As a side note, the problem may not be with the functions or how I called them, because my shell is executing the commands as though it had to format a wildcard (*
or ?
) which I am not sure should happen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在检查命令行之前,您似乎无条件地调用
fork
。但某些 shell 内置命令需要在父进程中运行,以便其效果持续存在。所有操纵环境的内置函数都属于这一类。顺便说一句,如果我正在编写 shell,我不会尝试使用 C 库的环境操作函数。我将使用三参数
main
,将envp
复制到我完全控制的数据结构中,然后将其反馈回execve
。这部分是因为我是一个控制狂,部分是因为用setenv
和/或putenv
做任何复杂的事情几乎不可能而不发生内存泄漏。请参阅这个较旧的问题了解详细信息。You appear to be calling
fork
unconditionally before examining the command line. But some shell built-in commands need to run in the parent process, so that their effect persists. All the built-ins that manipulate the environment fall in this category.As an aside, I wouldn't try to use the C library's environment manipulation functions if I were writing a shell. I'd use three-argument
main
, copyenvp
into a data structure under my full control, and then feed that back intoexecve
. This is partially because I'm a control freak, and partially because it's nigh-impossible to do anything complicated withsetenv
and/orputenv
and not have a memory leak. See this older SO question for gory details.是什么让您认为它不起作用?我在下面写了一个简单的测试用例......并且它按预期工作。
确保在同一进程中调用 setevn 和 prientevn。
What make you think it is not working? I wrote a simple test case below...and it worked as expected.
Making sure you setevn and prientevn are called in the same process.