exec_pipe
[Home]
[Dissertação]
[Biba]
[Linux]
[Conjugue]
[br.ispell]
[axw3]
[uplink]
/*
exec_pipe library function
--------------------------
by Ricardo Ueda Karpischek, Jan 5 1995
Starts a child command specifying args and environment. Note
that args[0] must be equal command. Returns in fildes two
pipe descriptors. All writes in fildes[1] will be read by
the child in its standard input. All child writes in its
standard output will be available for reading in fildes[0].
Returns 1 if successfull, 0 otherwise. If child could not
start command, it will exit with rc code.
*/
#include <stdio.h>
#include <stdlib.h>
int exec_pipe(command,args,environ,fildes,rc)
char *command,*args[],*environ[];
int *fildes,rc;
{
int i,fd[4],pid;
/* two pipes by filter */
pipe(fd);
pipe(fd+2);
/* duplicates process */
if ((pid = fork()) == 0)
{
/* Redirects standard input and output */
close(fd[1]);
close(fd[2]);
dup2(fd[0],0);
dup2(fd[3],1);
/* let's go */
execve(command,args,environ);
exit(rc);
}
/* close descriptors (child side) and returns */
else if (pid > 0)
{
close(fd[0]);
close(fd[3]);
fildes[0] = fd[2];
fildes[1] = fd[1];
return(1);
}
/* unable to start child */
else
{
for (i=0; i<4; ++i) close(fd[i]);
return(0);
}
}
/*
simple tests:
------------
int main()
{
char *cmd="/bin/pwd",*args[2],path[100];
int fd[2],n;
args[0] = cmd;
args[1] = NULL;
exec_pipe(cmd,args,NULL,fd,1);
n = read(fd[0],path,99);
path[n] = 0;
printf("%s",path);
exit(0);
}
int main()
{
char *cmd="/bin/cat",*args[2],path[100];
int fd[2],n;
args[0] = cmd;
args[1] = NULL;
exec_pipe(cmd,args,NULL,fd,1);
write(fd[1],"banana",6);
n = read(fd[0],path,99);
path[n] = 0;
printf("%s\n",path);
exit(0);
}
*/