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 8603577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:21:25+00:00 2026-06-12T02:21:25+00:00

I am so close to figuring out a program I have been writing for

  • 0

I am so close to figuring out a program I have been writing for linux shell written in C. I have been wanting to get this working for a while now, and I decided to pick it up again and have been tinkering with it for the past few weeks.

For the following code, keep in mind that the array called arrayOfCommands is dynamically filled. My code fills the arrayOfCommands with the current command being run. For the sake of my example, we will be running the command ls -l | wc and arrayOfCommands is filled with the following, depending on which time through the loop it is:

//Pass #1
arrayOfCommands[]= ("ls", "-l", NULL)

//Pass #2
arrayOfCommands[]= ("wc", NULL)

Here is what I have so far:

//PIPING
int do_command(char **args, int pipes) {
    // pipes is the number of pipes in the command
    // (In our example, one)


    // The number of commands is one more than the
    // number of pipes (In our example, two)
    const int commands = pipes + 1;  //Ex: 2
    int i = 0;

    // Set up the pipes
    int pipefds[2*pipes];

    for(i = 0; i < pipes; i++){
        if(pipe(pipefds + i*2) < 0) {
            perror("Couldn't Pipe");
            exit(EXIT_FAILURE);
        }
    }

    // Variables
    int pid;
    int status;
    char *str_ptr;

    int j = 0;
    for (i = 0; i < commands; ++i) {

        // A magic function that updates arrayOfCommands with
        // the current command goes here.  It doesn't make 
        // sense in the context of the code, so just believe me! :)

        // Ex: The contents will be "ls -l" or "wc" depending on 
        // which time through the loop we are

        pid = fork();

        if(pid == 0) {
            //if not last command
            if(i < commands){
                if(dup2(pipefds[j + 1], 1) < 0){
                    perror("dup2");
                    exit(EXIT_FAILURE);
                }
            }

            //if not first command&& j!= 2*pipes
            if(j != 0 ){
                if(dup2(pipefds[j-2], 0) < 0){
                    perror("dup2");
                    exit(EXIT_FAILURE);
                }
            }

            for(i = 0; i < 2*pipes; i++){
                    close(pipefds[i]);
            }

            // Should any of the below inputs be *arrayOfCommands or 
            // **arrayOfCommands or &arrayOfCommands?
            // I'm REALLY bad with pointers

            if( execvp(arrayOfCommands, arrayOfCommands) < 0 ){
                    perror(arrayOfCommands);
                    exit(EXIT_FAILURE);
            }
        }
        else if(pid < 0){
            perror("error");
            exit(EXIT_FAILURE);
        }

        j+=2;
    }

    for(i = 0; i < 2 * pipes; i++){
        close(pipefds[i]);
    }

    for(i = 0; i < pipes + 1; i++){
    }
        wait(&status);
}

When I run this, I get a couple of errors:

  • dup2: Bad file descriptor
  • ls: |: No such file or directory
  • ls: wc: No such file or directory

Could someone help me figure out the following two things:

  1. Why am I getting these errors?
  2. In the execvp function, what kinds if pointers am I looking for? arrayOfCommands was initialized as a char *arrayOfArgs[]
  • 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-12T02:21:26+00:00Added an answer on June 12, 2026 at 2:21 am

    First thing:

    //if not last command
    if(i < commands)
    

    should be

    if(i < commands -1)
    

    since igoes from 0 to commands -1
    that should solve dup2: Bad file descriptor

    ls: |: No such file or directory

    ls: wc: No such file or directory

    are caused by malformed arrayOfCommands. It has to be initialized by

    char * arrayOfCommands[] = {"ls", "-l", NULL};
    

    and

    char * arrayOfCommands[] = {"wc", NULL};
    

    respectively and invoked via execvp(arrayOfCommands[0], arrayOfCommands)

    essentially arrayOfCommands has to be of the same format that your argument vector (typically argv) of an int main(int argc, char** argv).

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

Sidebar

Related Questions

Ok, I have been having a VERY hard time figuring this out. I guess
I'm so close to figuring this out, i think. I'm new to javascript, but
I know I'm close to figuring this out but need a little help. What
I'm very close to figuring this out, but can't seem to find the missing
I need help figuring out an error in my SQL statement. I have been
I've been staring at this for quite a while and can't figure out how
http://jsfiddle.net/nicktheandroid/ZSvHK/ I need help figuring out how to get this right, problems listed below.
I'm close to figuring out the correct way to do this but I'm all
I cant get around on how to express this is PHP. I have 100
I’m having a little trouble figuring this one out. I’m fairly new to C#,

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.