2

I have implemented a shell which supports redirection but after the redirtection is done it gets of of my shell. How can I manage it in a way to get back to shell (stdout)?

 int i;
     for (i=1; args[i];i++)
         {
           if (strcmp(args[i],">")==0)
             {
               printf("argv[i] %s %d \n", args[i], i);
               int out = open(args[i+1], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
               close(1);
               int fdl=dup2(out,1);
               close(out);
               execvp(args[0],args);
            //   open(STDOUT, ">>", \$out); //doesn't work~!
             }

         }

Here's what happens when I execute my shell:

 ./basic_shell 
mysh> pwd > out_pwd
argv[i] > 1 
pwd: ignoring non-option arguments

and it creates out_pwd as expected and writes the pwd result into it. However when I try

mysh>ls > out_ls

I receive this error:

ls cannot access >: No such file or directory

Can you please give me some hints on how to fix it?

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408

2 Answers2

2

Instead of actually transforming file descriptors in your shell process, you want to simply save a representation of the mapping, and perform the replacements in the child after fork but before you exec the command.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Doing the I/O redirections in the child processes is basically correct. Pipes are slightly different; they have to be created before forking, but the redirection is still done after the fork. – Jonathan Leffler Sep 27 '13 at 04:09
0

First close the handle
Then call AllocConsole in kernel32 / or console api in win 7^ to create console

Ali Kherad
  • 834
  • 8
  • 25