LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ with system commands (https://www.linuxquestions.org/questions/programming-9/c-with-system-commands-161580/)

AquamaN 03-23-2004 07:43 PM

C++ with system commands
 
Hello everyone,
I am trying to write a program in c++ that will take in unix commands from the command line and from there it will split (using fork() ) for each command after that and then using pipes, will pass the data to the next command (using pipe() ). For example, I want to be able to type:

$ a.out "ls" "grep character" "sort"

and from there my program will take in each command, do them, then pass their input on to the next command... only I'm a little lost while doing this. I have been reading that I can use execlp but all the documentation on that is extremely confusing, any ideas?? and as always, thanks for your time.

-AquamaN

AquamaN 03-23-2004 08:25 PM

This is how I was thinking of doing it so far... Not sure if it is the best way... :newbie:

Code:

#include <iostream.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
        int x, y, pid, pipeName[2];
        char * argument;
        y = 1;
       
        for(x = 1;  x <= argc; x++)
        {
                pid = fork();
               
                if(pid == 0) /* child */
                {
                        argument = arg[y];
                        y++;
                        y++;  //these are there so it gets odd numbered commands
                        //execute command here

                        //pass data to parent with pipe
                }
                else  /* parent */
                {
                        y++;      // this is there so it gets the even numbered commands
                        argument = arg[y];
                        y++;

                        //recieve data with pipe

                        //execute command with child date
                }
        }

Thanks again!

aluser 03-24-2004 01:19 AM

This interested me so I went and wrote it myself. As usual the devil was in the details; the most annoying part turned out to be getting the initial process to wait until the others had finished before exiting (otherwise, you get the shell prompt followed by output, which is just plain ugly :) )

It's in my cvs thingy at http://alf.hopto.org/cgi-bin/viewcvs.cgi/pipe/

aluser 03-24-2004 01:29 AM

That could be too much of a spoiler, or it could be difficult to understand -- ask more specific questions and I can answer.

As you can see, I felt recursion was a more natural solution than what you posted: handle running the first command, and pipe its output into the same function which is now told to run the rest of the commands. Break when you get to the last command.

It could be that going backwards, that is having the first process run the last command in the line, would have avoided the timing problem I had with the process exiting and dumping the prompt before all the output prints.

AquamaN 03-24-2004 02:05 AM

Quote:

Originally posted by aluser
That could be too much of a spoiler, or it could be difficult to understand -- ask more specific questions and I can answer.

As you can see, I felt recursion was a more natural solution than what you posted: handle running the first command, and pipe its output into the same function which is now told to run the rest of the commands. Break when you get to the last command.

It could be that going backwards, that is having the first process run the last command in the line, would have avoided the timing problem I had with the process exiting and dumping the prompt before all the output prints.

woah.... that is truly impressive. A lot more than what I was looking for but I think I can learn from it. Thanks a lot aluser!!! You are awesome!!

-AquamaN

P.s. Are you a programmer by trade?

AquamaN 03-24-2004 02:12 AM

Seriously, this is impressive, I keep on going over it and looking at how long it took you to write it, I'm astonished, seriously.

-AquamaN

aluser 03-24-2004 12:17 PM

Quote:

P.s. Are you a programmer by trade?
Lol nope, just a bored college student. I was familiar with how these calls work so it wasn't that big a deal : )


I just updated the source in cvs a little: it now uses the fact that descriptors are closed when a process exits to implicitly close the pipe() fd opened in do_it(). This saves a fork and some passing around of fd through function arguments.


All times are GMT -5. The time now is 09:27 PM.