Close stdin and stdout to use redirection with a unix shell

Posted on 2005-04-29
Last Modified: 2010-04-02
I am creating my own shell to work as a unix shell (bash or other). I have gotten it to work with things like ls and pwd using execvp(), but I also want to be able to handle redirection like ls > out.txt. I think that I have to first close stdout and open out.txt before i call fork, but using this code:
if (!strcmp(command[i], ">"))
I get error: STDOUT' undeclared (first use in this function).
I also tried stdout, but got the same error. Am I missing a header?
Hope someone can help me
Question by:v_wall78
    LVL 22

    Accepted Solution

    I think most software at the shell level just uses 0, 1, and 2 for the stdin, stdout, and stderr descriptor numbers.  I wouldn't feel bad about using the bare numbers since that convention is so ingrained in UNIX tradition.

    But, if you really want to use a #defined constant, STDOUT_FILENO and friends are defined in <unistd.h>.

    I think you might be wrong in the sequence of events for redirecting output into a file.  I believe it usually goes:
    int pid = fork();
    if (pid) { /* parent */
        don't mess with file descriptors
        wait() for child to finish
    } else { /* child */
        close all open descriptors except 0,1,2
        close & open file descriptors 0,1,2 to handle redirected input and/or output
        exec() the command
    LVL 86

    Expert Comment

    Instead of closing 'stdout', why not using

       /* Reassign "stdout" to "out.txt": */
       stream = freopen( "out.txt", "w", stdout );
    LVL 22

    Expert Comment

    In my experience, shells don't usually touch FILE * i/o, and stay in the realm of file descriptors.  The idea behind freopen() can be implemented by using open() and dup2().

    Author Comment

    I figured out how to do it:

    if (!strcmp(command[i], ">"))  {                                                                   // if input redirection
    if ((in = open(command[i+1], O_RDONLY)) == -1) {                                      // open input file specified
          fprintf(stderr, "Input file specified could not be opened.\n");
           return -1; }
    dup2 (in, fileno(stdin)); }                                                                            // change fileid 0 from stdin to the file specified by user

    if (!strcmp(command[i], ">"))  {                                                                  // if output redirection
       if ((out = open(command[i+1], O_WRONLY|O_CREAT,0644)) == -1) {        // open or create output file specified
           fprintf(stderr, "Output file specified could not be opened.\n");
            return -1; }
    dup2 (out, fileno(stdout)); }

    Thanks guys

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now