Thread: Redirection Issue

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Redirection Issue

    Hi everyone, I was making a little program that, for the sake of simplicity, receives its arguments from a file through the command-line redirecting the standard input to a file... But it simply doesn't work.
    I type:
    [yada@yada]$ ./program < input
    And there's absolutely nothing in the argument vector (argv). Redirecting stdout to a file works, though.
    Any help?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Redirection is OS or shell dependent.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    I'm using fedora 10, do you know if that's the intended behaviour? Am I Doomed? Shall I pray three days in a row?
    Last edited by Oblivious88; 04-07-2009 at 05:59 PM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Oblivious88 View Post
    Hi everyone, I was making a little program that, for the sake of simplicity, receives its arguments from a file through the command-line redirecting the standard input to a file... But it simply doesn't work.
    I type:
    [yada@yada]$ ./program < input
    And there's absolutely nothing in the argument vector (argv). Redirecting stdout to a file works, though.
    Any help?
    Why would there be anything in argv? You have not passed any parameters.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Quote Originally Posted by brewbuck View Post
    Why would there be anything in argv? You have not passed any parameters.
    That's precisely my question, how redirecting in that case works and where are the contents of the file?

    EDIT:
    Thanks to your question, I did a little experimentation and now I've seen the behaviour of '<'. The problem is fixed. Any advices regarding this are appreciated (one never stops learning).
    Last edited by Oblivious88; 04-07-2009 at 06:22 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, the redirection happens at shell level, before your program actually starts. Something like this:
    Code:
        if ((pid = fork()) == 0)
        {
            // Child process. 
            if (redirect_input)  // redirect input gets set by the parser if it sees "< filename"
            {
                int fd;
                fd = open(input_file_name, ...);
                if (fd < 0)
                {
                    // Issue error message and quit. 
                }
                close(STDIN_HANDLE);
                dup2(STDIN_HANDLE, fd);   // Make fd -> STDIN_HANDLE. 
                close(fd);  
                exec(command);   // Execute your program
            }
         }
    You can (in some OS's) figure out if a handle is "console" or not, if you want to behave differently for input from console than from a file (e.g. you want to prompt for the data in console mode, but not if the input is from a file).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux file redirection buffering issue !!!!
    By AirSeb in forum Linux Programming
    Replies: 8
    Last Post: 03-14-2009, 05:32 PM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  4. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM
  5. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM