简体   繁体   中英

Input and output redirection in shell

I am trying to implement input and output redirection is C. I read all of the answers to this question, but it does not work! It goes to the last if ( execvp ).

I want something like this:

output redirection: ls -al > output.txt

input redirection: sort < input.txt

Here is the code that I wrote:

int io_redirection(char * args[], char* inputFile, char* outputFile, int option){

    int err = -1;

    int fileDescriptor = -1; // between 0 and 19
    pid_t pid = -10;
    pid = fork();

    if(pid==-1){
        printf("Child process could not be created\n");
        return;
    }
    if(pid==0){
        // Option 0: output redirection
        if (option == 0){
            // I also tried fileDescriptor = creat(outputFile, 0644);
            fileDescriptor = open(outputFile, O_CREAT | O_TRUNC | O_WRONLY, 0600); 
            dup2(fileDescriptor, STDOUT_FILENO); 
            close(fileDescriptor);


        }
         else if (option == 1){
            // Option 1: input redirection

            fileDescriptor = open(inputFile, O_RDONLY, 0600);  
            dup2(fileDescriptor, STDIN_FILENO);
            close(fileDescriptor);

        }



        if (execvp(args[0],args)==err){
            printf("err");
            kill(getpid(),SIGTERM);
        }        
    }
    waitpid(pid,NULL,0);
}

for the output redirection args contains ls and -al . And for the input one it contains sort .

execvp requires a NULL as the last paramter. I am guessing your command synthesis ignores that, the rest works as intended.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/wait.h>


void print_child_status (int status) {
  if (WIFEXITED (status)) {
    fprintf (stdout, "Child exited with status %d\n", WEXITSTATUS (status));
  } else if (WIFSTOPPED (status)) {
    fprintf (stdout, "Child stopped by signal %d (%s)\n", WSTOPSIG (status), strsignal (WSTOPSIG (status)));
  } else if (WIFSIGNALED (status)) {
    fprintf (stdout, "Child killed by signal %d (%s)\n", WTERMSIG (status), strsignal (WTERMSIG (status)));
  } else {
    fprintf (stdout, "Unknown child status\n");
  }
}

int io_redirection(char * args[], char* inputFile, char* outputFile, int option){

    int err = -1;

    int fileDescriptor = -1; // between 0 and 19
    pid_t pid = -10;
    pid = fork();

    if(pid==-1){
        printf("Child process could not be created\n");
        return -1;
    }
    else if(pid == 0){
        // Option 0: output redirection
        if (option == 0){
            // I also tried fileDescriptor = creat(outputFile, 0644);
            fileDescriptor = open(outputFile, O_CREAT | O_TRUNC | O_WRONLY, 0600); 
            dup2(fileDescriptor, STDOUT_FILENO); 
            close(fileDescriptor);


        }
         else if (option == 1) {
            // Option 1: input redirection
            fileDescriptor = open(inputFile, O_RDONLY);
             printf("Input redirection %d\n", fileDescriptor);
            dup2(fileDescriptor, STDIN_FILENO);
            close(fileDescriptor);

        }
        err = execvp(args[0], args);

        if (err){
            printf("err %d\n", err);
        }
        return err;
    } else {
        int status;
        waitpid(pid, &status,0);
        print_child_status(status);
    }
    return 0;

}

int main() {
    char * args[] = {"ls", "-l", NULL};
    io_redirection(args, NULL, "out.txt", 0);
    char * args1[] = {"grep", "main", NULL};
    io_redirection(args1, "./test.c", NULL, 1);
    return 0;
}

Maybe your test wrong, I use the following code test is right.

int main() {
  char* argv[] = {"ls", "-al", NULL};
  io_redirection(argv, NULL, "test", 0);

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM