linux - Minishell problems with cd (C) -


i made simple minishell in c , works, except cd command. when try run nothing happens except creates child process never ends. example, after running cd in minishell need type exit twice exit minishell, not standard once. code: int debug=0;

void safequit(){     if (debug==1)       printf("info: parent process, pid=%d exiting.\n", getpid());     exit(0); }      int main(int argc,char** argv)      {     int pid, stat;     char * array_arg[50];      char command_line[200];//user input     if (argc>1)       if (strcmp(argv[1],"-debug")==0)         debug=1;     while (1){       printf("[minishell]> "+debug);       gets(command_line);       if (strcmp(command_line,"exit")==0)         safequit();       char * subcommand=strtok(command_line," ");  //divides string according spaces       int i=0;          while (subcommand!= null)//insert array       {         array_arg[i]=subcommand;         subcommand=strtok(null," ");         i++;       }    array_arg[i]='\0';       if (fork()==0){         if (debug==1)           printf("info: child process, pid = %d, executing command %s\n", getpid(), command_line);         execvp(array_arg[0],array_arg); //execution of cmd       }       else{         pid=wait(&stat);   }     } } 

cd shell built-in, not external utility. want change current working directory of current process (the shell itself), not of child process. call chdir instead of forking child process.

separately, check execvp errors , defensively terminate child after failed exec. you'd have seen informative error if had done so:

... (child) ... execvp(array_arg[0], array_arg); perror("error - exec failed"); // if here, did *not* replace process image exit(0); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -