• C

Simulating Pipe in C.

Here is what I am working on:

If you execute:
% pipe <program1> <program2>

then the two programs <program1> and <program2> will be concurrently executed, with the standard output of <program1> attached to the standard input to <program2>.

The pipe program must support arguments of program1 and program2. For example:

% pipe "cat /etc/passwd" "wc -l"

This should be equivalent to the shell executing:

% cat /etc/passwd | wc l

I've been working on it for weeks now and can't figure it out. Any ideas or suggestions?
kiltgodAsked:
Who is Participating?
 
Infinity08Commented:
>> Now I can't figure out how exactly to pipe the output into another command.

Just popen the other command with "w" mode, and fprintf to that file pointer ...
0
 
grg99Commented:
we can't give you the whole answer, but see  "man popen"  for a really big hint.  
0
 
avizitCommented:

 you will probably need to do a

man fork
and man exec too
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

 
ravenplCommented:
man 2 pipe
man 2 fork
man 2 execlp
man 2 dup2
do the math by Yourself
0
 
kiltgodAuthor Commented:
This is what I have thus far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main(int arg, char *argv[]) {
   FILE *fpipe;
   char *command = argv[1];
   char line[256];

   if ( !(fpipe = (FILE*)popen(command,"r")) )
   {  // If fpipe is NULL
      perror("Problems with pipe");
      exit(1);
   }


   while ( fgets( line, sizeof line, fpipe))
   {
     printf("%s", line);
   } pclose(fpipe);

Now I can't figure out how exactly to pipe the output into another command. I've tried execve, but to no avial since it can't take argv[] as an argument. I am looking into raven's idea as well.
0
 
avizitCommented:
since you have two commands you will need to fork() to create a new process after you do a popen.

so in one process you close the read end of the pipe and in the other you close the write end
and then you can use the pipe for one way communication.
0
 
ravenplCommented:
> since you have two commands you will need to fork() to create a new process after you do a popen.
and duplicate fd obtained from popen to stdout(stdin)
0
 
Infinity08Commented:
>> since you have two commands you will need to fork() to create a new process after you do a popen.

If your popen implementation is standards compliant, popen should already do that for you, so there's no need for an extra fork.

However, using a combination of pipe, fork, read and write is a good alternative.
0
 
ravenplCommented:
Infinity08: You missed one point
pipe <prog1> <prog1>
You haveto replace Your application with prog1(and duplicate stdin/out from proper fds), or act as proxy between two popen()s - right?
0
 
Infinity08Commented:
>> Infinity08: You missed one point

I don't think I did ... you only have to set up a one-way stream from prog2's stdout to prog1's stdin, and that can easily be done with two popen's.

So, I'm not sure what I missed ?
0
 
ravenplCommented:
But You are executing from pipe program right? So You have two FILE structures and have to act as proxy. Right?
But there's much easier/lighter solution - pipe(), fork(), dup2()x2, exec()x2 - isn't it?
0
 
Infinity08Commented:
>> But there's much easier/lighter solution - pipe(), fork(), dup2()x2, exec()x2 - isn't it?

I would prefer it too, but it's not easier, because popen does the fork's and exec's for you - you just have to popen the two processes - one for reading and one for writing, and then loop over the data and forward ...
0
 
ravenplCommented:
Unless the following is allowed usage, then popen(one way pipe) is useless
cat /etc/passwd | pipe cat tail | cat
0
 
ravenplCommented:
> % pipe "cat /etc/passwd" "wc -l"
But I agree, that tokeninzing params for exec() usage is not easier at all.
BTW: system("argv[1] | argv[2]") // LOL ;)
0
 
Infinity08Commented:
>> Unless the following is allowed usage, then popen(one way pipe) is useless
>> cat /etc/passwd | pipe cat tail | cat

I'm not sure what you mean. The pipe application would not make use of stdin and stdout, so there's no sense in using it in another pipe. A one way pipe is not useless, because that's exactly what was asked ...

As a side note : what's that command supposed to do lol


>> But I agree, that tokeninzing params for exec() usage is not easier at all.

With popen's it's a 5-line application : 2 for the popen's, 1 for the loop, and 2 more for the pclose's. Can't get a lot simpler than that ;)


>> BTW: system("argv[1] | argv[2]") // LOL ;)

heh ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.