Shell '>' operator implementation
Well I am doing asn assignment in which we have to in some way take shell commands and then execute them thru "execv".
I have done the part of that relating to pipes, i.e I have implemented shell commands which are something like this:
Shell@ A|B
now I have to implement the "A>B" command and I am getting trouble here, So far I have done the following:
int fds[2];
int status;
int pid;
char *tmpargv[3];
char comm1[]="/bin/";
pipe(fds);
if(strcmp(argv[2],"g")==0)
{
if(fork()==0)
{
close(0);
dup(fds[1]);
close(fds[0]);
close(fds[1]);
strcpy(tmpargv[0],comm1);
strcat(tmpargv[0],argv[1]);
tmpargv[1]=0;
execv( tmpargv[0], tmpargv );
perror("execv");
exit(1);
}
wait(&status);
if(fork()==0)
{
close(1);
dup(fds[0]);
// close(fds[0]);
close(fds[1]);
strcpy(tmpargv[0],comm1);
strcat(tmpargv[0],argv[3]);
tmpargv[1]=0;
execv( tmpargv[0], tmpargv );
exit(1);
}
wait(&status);
}
return 0;
}
Here 'g' is for '>', And the above code is only executing the first command and not the 2nd one.
Please Help!
|