home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / fractal / kaos.lha / execlib / pipe_create.c < prev   
Encoding:
C/C++ Source or Header  |  1990-01-16  |  1.9 KB  |  62 lines

  1. /*
  2. ### create pipes ###
  3. */
  4. #include <stdio.h>
  5.  
  6. void pipe_create(s,pid,from,to)
  7. char s[];
  8. int *pid,*from,*to;
  9. {
  10.     int pipeto[2],pipefrom[2];
  11.     int c,numfds;
  12.     FILE *popen();
  13.     extern FILE *fp_fromchild,*fp_tochild;
  14.     
  15.     /* Create a pipe */
  16.     if(pipe(pipefrom) < 0) {
  17.                 perror("new process");
  18.         system_mess_proc(1,"Error: Pipe failed! Check the file name!");
  19.         return;
  20.     }
  21.         if(pipe(pipeto) < 0){
  22.                 perror("new process");
  23.         system_mess_proc(1,"Error: Pipe failed! Check the file name!");
  24.         return;
  25.     }
  26.  
  27.     /* Fork a child */
  28.         switch (*pid = fork()){
  29.                 case -1:
  30.                         perror("new process");
  31.                         exit(1);
  32.                 case 0:
  33.                         /* use dup2 to set the child's stdin and stdout to
  34.                         the pipe */
  35.                         dup2(pipeto[0],0);
  36.                         dup2(pipefrom[1],1);
  37.  
  38.                         /* close all other fds (except stderr) since child
  39.                         process doesn't know about or need them */
  40.                         numfds = getdtablesize();
  41.                         for (c = 3; c < numfds; c++)
  42.                                 close(c);
  43.                         (void) execl("/bin/csh","csh","-c",s,0);
  44.                         perror("new process: child");
  45.                         exit(1);
  46.                 default:
  47.                         close(pipeto[0]);
  48.                         close(pipefrom[1]);
  49.                         *to = pipeto[1];
  50.                         fp_tochild = fdopen(*to, "w");
  51.                         *from = pipefrom[0];
  52.                         fp_fromchild = fdopen(*from, "r");
  53.  
  54.                         /* The pipe should be unbuffered or "new process" 
  55.                         will not get any data until 1024 characters have
  56.                         been sent */
  57.                         setbuf(fp_tochild,NULL);
  58.                         setbuf(fp_fromchild,NULL);
  59.                         break;
  60.         }
  61. }
  62.