I am working on a custom shell for a system programming class. We were instructed to implement built-in commands setenv()and unsetenv()with a hint about checking man pages for putenv().
My problem is that setenv(char*, char*, int)and putenv(char*)does not seem to work at all. My code to execute the entered command is as follows:
pid = fork();
if(pid == 0){
if(!strcmp(_simpleCommands[0]->_arguments[0],"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")){
char * p = _simpleCommands[0]->_arguments[1];
char * s = _simpleCommands[0]->_arguments[2];
char param[strlen(p) + strlen(s) + 1];
strcat(param,p);
strcat(param,"=");
strcat(param,s);
putenv(param);
exit(0);
}
if(!strcmp(_simpleCommands[0]->_arguments[0],"unsetenv")){
unsetenv(_simpleCommands[0]->_arguments[0]);
exit(0);
}
execvp(_simpleCommands[0]->_arguments[0],_simpleCommands->_arguments);
perror("-myshell");
_exit(1);
}
If I run printenvit works correctly, but if I try to set a new variable with putenv()or setenv()my command printenv()returns exactly the same thing, so it does not work.
, , , , (* ?), , , .