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

  • SEARCH
  • Home
  • 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 8716959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:12:15+00:00 2026-06-13T06:12:15+00:00

I am writing my own shell for a homework assignment, and am running into

  • 0

I am writing my own shell for a homework assignment, and am running into this issue:

Whenever I enter a command to redirect output to a file (i.e. ls -al > output.txt) my shell should handle the command and redirect the output. However, I keep getting this message displayed: ls: write error: Bad file descriptor

I saw on a few other forums that it might be in reference to exceeding the amount of memory, but I don’t see how that could be the issue. Here’s a bit of my code (if you need more for clarity, feel free to ask):

loc[0] = argv[0];                 //used for execution

while(argv[count] != 0){                    //loop through commands
    if(strcmp(argv[count], "<") == 0)    //and test for certain flags
        inFlag = 1;
    else if(strcmp(argv[count], ">") == 0){
        argv[count] = argv[count+1] = 0;
        outFlag = 1;
    }
    else if(strcmp(argv[count], "&") == 0)
        bgFlag == 1;
    else if(strcmp(argv[count], "|") == 0){
        argv[count] = 0;
        loc[pipes+1] = argv[count+1];
        pipes++;
    }
        count++;
}

for(k = 0; k <= pipes; k++){
    if(j < pipes){
        pipe(r_tube);
        j++;
    }

    pid = fork();

    if(pid > 0){
        if(j > 0){
            close(l_tube[0]);
            close(l_tube[1]);
        }
        l_tube[0] = r_tube[0];
        l_tube[1] = r_tube[1];
    }
    else if(pid == 0){
        if((k == 0) && (inFlag == 1)){
            int n = open("input.txt", "r");
            close (0);
            dup (n);
            close (n);
        }
        else if((k > 0) && (k < pipes)){

        }
        else if((k == pipes) && (outFlag == 1)){   //<-----issue
            int out = open("output.txt", 0666);
            close (1);
            dup (out);
            close (out);
        }
        else if(k == pipes){

        }
        execvp(argv[loc[k]], &argv[loc[k]]);
  • 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-06-13T06:12:16+00:00Added an answer on June 13, 2026 at 6:12 am

    Try this:

    count = 0;
    
    loc[0] = argv[0];                 //used for execution
    
    count++;
    pipes++;
    
    char* outFile = NULL;
    char* inFile = NULL;
    
    while(argv[count] != 0)
    { 
        if(strcmp(argv[count], "<") == 0)
        { 
            inFile = strdup(argv[count+1]);
            inFlag = 1;
            count++;
        }
        else if(strcmp(argv[count], ">") == 0)
        {
            outFile = strdup(argv[count+1]);
            outFlag = 1;
            count++;
        }
        else if(strcmp(argv[count], "&") == 0)
        {
            bgFlag == 1;
        }
        else if(strcmp(argv[count], "|") == 0)
        {
            argv[count] = 0;
            loc[pipes] = argv[count+1];
            pipes++;
        }
    
        count++;
    }
    
    int current_out = STDOUT_FILENO;
    int current_in = STDIN_FILENO;
    int next_in = STDIN_FILENO;
    
    for(k = 0; k <= pipes; k++)
    {
        int cur_pipes[2];
    
        int last_out = current_out;
        int last_in = current_in;
    
        if(k < pipes)
        {
            pipe(cur_pipes);
            current_out = cur_pipes[1];
            current_in = next_in;
            next_in = cur_pipes[0];
        }
    
        else if (k == pipes)
        {
            current_out = STDOUT_FILENO;
            current_in = next_in;
        }
    
        pid = fork();
    
        if(pid > 0)
        {
            if(k > 0)
            {
                close(last_out);
                close(last_in);
            }
        }
        else if(pid == 0)
        {
            if((k == 0) && (inFlag == 1))
            {
                int n = open(inFile, O_RDONLY);
                if (n == -1) 
                {
                    printf("Could not open input file!\n");
                    exit(1);
                }
                current_in = n;
            }
    
            if((k == pipes) && (outFlag == 1))
            {
                int n = open(outFile, O_WRONLY | O_CREAT, 0666);
                if (n == -1) {
                    printf("Could not open output file!\n");
                    exit(1);
                }
    
                current_out = n;
            }       
    
            if (current_in != STDIN_FILENO)
            {
                close (STDIN_FILENO);
                dup (current_in);
                close (current_in);
            }
    
            if(current_out != STDOUT_FILENO)
            {
                close(STDOUT_FILENO);
                dup(current_out);
                close(current_out);
            }
    
            execvp(argv[loc[k]], &argv[loc[k]]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently writing my own shell program. This simple shell can just execute
I am writing my own shell program. I am currently implementing the cd command
I am writing my own unix shell as a part of my assignment and
I am writing my own shell as part of course assignment. So I need
When writing my own flat file databases I try and keep the file sizes
I'm currently writing my own shell as a project for a class, and have
In one of my courses we're writing our own shell (basically from scratch). We've
I'm writing my own shell in C and I need to detect EOF (for
I've tried finding this out on my own but to no avail. I'm writing
I am writing my own shell and need to implement a history feature where

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.