Thread: Input and output

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    Input and output

    Hi,

    I wrote a shell in C that accepts commands.
    Now I need to implement input output redirection.

    My shell accepts a string and breaks it down to command and file name if input output redirection is requested.

    So for ex:
    If the user writes: wc -l < document.txt
    My shell will store the command wc-l in a string
    and the file name document.txt in another string.
    str1[] = "wc - l"
    str2[] = "document.txt"

    For output redirection it's the same
    For ex:
    If the user writes ls - l > document.txt
    str1[] = "ls - l"
    and
    str2[] = "document.txt"

    Now I want to know how do i tell the shell to do the input output redirection
    What instruction should I write before execvp?
    if I have str1[] and str2[].

    Thanks.
    Thanks.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Dunno if it works, but have you tried putting "<" and "document.txt" as arguments in the execvp function? Like:
    Code:
    args = {"-l", "<", "document.txt"}
    execvp("myTest.exe", args)

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    It's a little unclear as to what you want, here's a shot though.

    Code:
       if( freopen(argv[1], "a", stdout) == NULL ) {
            fprintf(stdout,"Error with freopen(): ");
            perror("");
            return 0;
        }
    Pretty basic explanation.. freopen takes the filedescripter of stdout, and gives it to argv[1]. Flushing of stdout is performed before doing so.

    Couple more system calls (though not standard c) are dup and dup2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Bad output -- possible input buffer problems
    By jake123 in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 03:36 PM
  3. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM