0

I want to redirect stdout and stdin in a specific file which would be given in argv array.

For instance when I enter a command like - ./shell ls > test

it should be redirected to the "test" file, now I am bit confuse because without writing any code it automatically redirects to that file, I want to do it manually, secondly, when I enter a command like- ./shell ls < test, the stdin should be redirected. I tried to find a file name and ">" or "<" sign using argv[argc-1] and argv[argc-2], but it seems that when I use ">" and a filename afterwards, the output prints(the arguments before ">" "<" sing) in that file instead of getting that name and a sign.

Basically, I am creating a shell command using execvp() and fork().

Here is my code, I am able to redirect stdout in a static file.

void call_system(char *argv[],int argc)
    {
    int pid;
    int status=0;
    signal(SIGCHLD, SIG_IGN);
    int background;
    /*two process are created*/
    pid=fork();
    background = 0;

    if(pid<0)
    {
        fprintf(stderr,"unsuccessful fork /n");
         exit(EXIT_SUCCESS);
    }
    else if(pid==0)
    {
        //system(argv[1]);
        /*argument will be executed*/

                freopen("CON","w",stdout);
                    char *bname;
                    char *path2 = strdup(*argv);
                    bname = basename(path2);


                        execvp(bname, argv);
                         fclose (stdout);
    }
    else if(pid>0)
    {
    /*it will wait untill the child process doesn't finish*/
    //waitpid(pid,&status,0);

    wait(&status);

    //int tempid;
    //tempid=waitpid(pid,&status,WNOHANG);
    //while(tempid!= pid);// no blocking wait


    if(!WIFEXITED(status) || WEXITSTATUS(status))
    printf("error");



                         exit(EXIT_SUCCESS);

    }
    }
Ravi Vyas
  • 137
  • 1
  • 4
  • 19

2 Answers2

1

Try using dup() or dup2() or dup3().

The dup() system call creates a copy of the file descriptor oldfd, using the lowest-numbered unused descriptor for the new descriptor.

File *fp=fopen(argv[1],"r");
int fd=fileno(fp);

dup2(fd,0); //dup2(fd,STDIN_FILENO) redirect file stream to input stream
scanf("%s",buff); //reading from file.

Similarly output can also be redirected.From manual these informations may be useful

On program startup, the integer file descriptors associated with the
       streams stdin, stdout, and stderr are 0, 1, and 2, respectively.  The
       preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO
       are defined with these values in <unistd.h>.

Suppose you want to redirect stdout to this file.

dup2(fd,1);//dup2(fd,STDOUT_FILENO)
printf("%s",buff); //this will write it to the file.
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

stdio redirection is handled by the shell, not the launched program. The relevant syscalls are pipe, open and dup2, the later of the two is used to redirect the stdio filedescriptors into the pipe or file to be read from or written to.

datenwolf
  • 159,371
  • 13
  • 185
  • 298