Thread: Input and output redirection together !!

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    Input and output redirection together !!

    Hi,

    I am trying to implement redirections. I am able to get the result for input and output redirection, individually. However, when I do both together I see not o/p.


    file_input - Input file
    file_output - output file
    cmd - the command that will get exected
    argv - arguments
    symbol - tell which is the redirection

    Code:
    //Function that exectues commands involving redirection
    int execute_redirect_command(char file_input[], char file_output[], char cmd[], char **argv, char symbol[])
    {
        FILE *input_ptr, *output_ptr;
        int fd;
        if(fork() == 0)
        {
            if(file_input != NULL)
            {
                input_ptr = fopen(file_input,"r");
                fd = fileno(input_ptr);
                dup2(fd,0);
                close(fd);
            }
            if(file_output != NULL)
            {
                if(strcmp(symbol,">>") == 0)
                {
                    output_ptr = fopen(file_output,"a");
                }
                else
                {
                    output_ptr = fopen(file_output,"w");
                }
                fd = fileno(output_ptr);
                dup2(fd,1);
                close(fd);
            }
            execvp(cmd, argv);
        }
        else
        {
            wait(NULL);
        }
    }
    Above is my code. Also would like to know how to do error handling here.

    Thanks in advance.
    Last edited by prashant8809; 09-08-2012 at 02:27 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I see things here that have been declared in other piece of code,no here,such as fork,close,fileno,dup2,execvp... Also can you be a little more specific about what the problem is?
    About error handling i suggest you to read the return values of fopen and strcmp (write ref and the function's name in google) and assign this return value to a variable and have if-else structure in order to do error handling

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to post a complete (non-)working program.

    The only error I see in your code is that you ignore the function error returns.

    Examples:
    Code:
    pid = fork();
    if (pid == 0)
    else if (pid == -1)
        perror("fork");
    else
        wait(NULL);
    
    
    if (input_ptr == NULL) perror("fopen input");
    
    if (dup2(fd,0) == -1) perror("dup2 0");
    
    if (execvp(cmd, argv) == -1) perror("execvp");
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with input redirection - C -
    By abdullahkiran in forum C Programming
    Replies: 10
    Last Post: 04-21-2012, 12:17 PM
  2. input redirection <
    By bos1234 in forum C Programming
    Replies: 7
    Last Post: 03-27-2012, 06:10 AM
  3. input output redirection
    By chess2009 in forum C Programming
    Replies: 1
    Last Post: 02-26-2011, 06:46 PM
  4. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM