设置环境、取消设置环境、放置环境

发布于 2024-12-08 13:04:11 字数 1785 浏览 1 评论 0原文

我正在为系统编程课程开发自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星光不落少年眉 2024-12-15 13:04:11

在检查命令行之前,您似乎无条件地调用 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, copy envp into a data structure under my full control, and then feed that back into execve. This is partially because I'm a control freak, and partially because it's nigh-impossible to do anything complicated with setenv and/or putenv and not have a memory leak. See this older SO question for gory details.

那伤。 2024-12-15 13:04:11

是什么让您认为它不起作用?我在下面写了一个简单的测试用例......并且它按预期工作。

确保在同一进程中调用 setevn 和 prientevn。

#include <stdlib.h>
#include <assert.h>
int main()
{


 char * s= "stack=overflow";

 int ret =  putenv(s);

 assert(ret == 0);

 //printout all the env
 extern char **environ;
        int i;
        for(i = 0; environ[i] != NULL; i++){
            printf("%s\n",environ[i]);
        }


 return 0; 

}
pierr@ubuntu:~/workspace/so/c/env$ ./test | grep stack
stack=overflow

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.

#include <stdlib.h>
#include <assert.h>
int main()
{


 char * s= "stack=overflow";

 int ret =  putenv(s);

 assert(ret == 0);

 //printout all the env
 extern char **environ;
        int i;
        for(i = 0; environ[i] != NULL; i++){
            printf("%s\n",environ[i]);
        }


 return 0; 

}
pierr@ubuntu:~/workspace/so/c/env$ ./test | grep stack
stack=overflow
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文