Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4059374
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:12:03+00:00 2026-05-20T15:12:03+00:00

Continuing on this problem , but I’ll reiterate: For a homework assignment I have

  • 0

Continuing on this problem, but I’ll reiterate:

For a homework assignment I have to write a basic shell including redirection. The program uses readline to prompt for input, parses the input string, and breaks it down into the executable name, the arguments, and the input/output file(s), if applicable. After parsing the string, it forks and the child execv()’s to the executable that was passed in. I’m using dup2() to change the file descriptors after the fork and before the execv, but am having a problem once the program has execv’d to the new executable. If in my shell I run ls > foo.out, I get: ls: write error: Bad file descriptor

Here is the code for my child process (this is after fork()):

int _child(struct command *c){
    int ret;
    /* When given `ls > foo.out`, these next two lines output:
    ** c->infile is (null)
    ** c->outfile is foo.out
    */
    printf("c->infile is %s\n",c->infile);
    printf("c->outfile is %s\n",c->outfile);
    if(c->infile){
        int fd = open( c->infile, O_RDONLY);
        int _fd_dup = dup2(fd,0);
        close(0);
        if(_fd_dup != 0){
            fprintf(stderr, "Failed to redirect command input.\n");
            return 0;
        }
    }
    if(c->outfile){
        int fd = open( c->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
        int _fd_dup = dup2(fd,1);
        close(1);
        if(_fd_dup != 1){
            fprintf(stderr, "Failed to redirect command output.\n");
            return 0;
        }
    }

I do not get the “Failed to redirect command output.” error. As I mentioned, this is a homework assignment so I’m not looking for anyone to fix this, but rather point me in the right direction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:12:03+00:00Added an answer on May 20, 2026 at 3:12 pm

    The problem is in this bit of code:

        int _fd_dup = dup2(fd,1);
        close(1);
    

    You should be closing fd, not 1. You have the same problem in the fd 0 case, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing my problem from yesterday, the Silverlight datagrid I have from this issue is
I have several files containing this line Release: X I want to increment X
I have a plain text file looking like this: some text containing line breaks
I have DataTemplate containing a TextBox . I'm setting this template to a listbox
I have an HBITMAP containing alpha channel data. I can successfully render this using
I have a c++ header file containing a class. I want to use this
I'm trying to do this: cmd.exe /C C:\Program Files\Somewhere\SomeProgram.exe > C:\temp\Folder Containing Spaces\SomeProgram.out However,
I have a big problem, at least for me. I have a Table with
I've got this python dictionary mydict, containing arrays, here's what it looks like :
Continuing from my previous question , is there a comprehensive document that lists all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.