you will probably need to do a
man fork
and man exec too
Main Topics
Browse All TopicsHere 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?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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.
>> 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.
>> 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 ...
>> 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 ...
Business Accounts
Answer for Membership
by: grg99Posted on 2007-07-22 at 16:01:49ID: 19544048
we can't give you the whole answer, but see "man popen" for a really big hint.