I'm having trouble building in a pipe, i have program

reads in from argv using fgets
spilts up using strtok into command,cmd and cmd2.

then i have a if statement

if(strcmp(cmd, "|")==0)

then i open the file for writing

strcmp(filename, cmd2);
file2=open(filename, O_WRONLY);

now im confussed where to go. I been reading examples using dup2, something like

dup2(file2,0);

which writes it to stdin
but how to get it from stdin to a file?
im jut generally confused,

i do now i need to execute the command . i.e ls
then change the output of that from stdout (1) to a file.
just how?

Well it's the right idea, but file descriptor 0 is stdin, so the file you opened should be opened for reading, not writing.

Here's an example:
But first - create a text file mydata and put some text in it..

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char**argv)
{
	char ch[100];
	int infile, outfile;

	infile = open("mydata", O_RDONLY, 0666);
	if (infile < 0)
	{
		fputs("could not open infile!\n", stdout);
		exit(EXIT_FAILURE);
	}

	outfile = open("myresult", O_WRONLY|O_CREAT, 0666);
	if (outfile < 0)
	{
		fputs("could not open outfile!\n", stdout);
		exit(EXIT_FAILURE);
	}

	dup2(outfile, 1);
	dup2(infile, 0);

	while (fgets(ch, 100, stdin))/*read from infile - mydata*/
	{
		fputs(ch, stdout);/*write to outfile - myresult*/
	}
	

	close(outfile);
	close(infile);
	exit(EXIT_SUCCESS);
}

Yes i understand that,
problems im having are :

1)my input isnt a text file its a command, i.e ls,

2) using fputs to take output from stdout, to a file of my choice.

So how about posting something approaching a complete program, instead of random lines in the hope we can guess all the parts you missed out.

Yes i understand that,
problems im having are :

1)my input isnt a text file its a command, i.e ls,

And what's the problem? The value is stored in the command line arguments? It has to be stored somewhere..

Short of writing the code for you, I can't think of any more examples

For this to work - create a text file mydata and put 5 lines of text in it.

usage:

./pipe cat<mydata sort


pipe.c

#include <stdlib.h>
#include <unistd.h>

enum PIPES {READ, WRITE};

int main(int argc, char**argv)
{
	int hpipe[2];
	pipe(hpipe);

	if (fork())
	{
		close(hpipe[WRITE]);
		dup2(hpipe[READ], 0);
		close(hpipe[READ]);
		execlp(argv[2], argv[2], NULL);
	}
	else
	{
		close(hpipe[READ]);
		dup2(hpipe[WRITE], 1);
		close(hpipe[WRITE]);
		execlp(argv[1], argv[1], NULL);
	}
	exit(EXIT_SUCCESS);
}

So how about posting something approaching a complete program, instead of random lines in the hope we can guess all the parts you missed out.

http://www.daniweb.com/forums/thread265834.html

is link the to rest of my file.

And believe me i do not want you to write the code for me ,
and as picking and annoying as i may seem,
i do really appreciate the help .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.