home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / tahoe / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-18  |  1.6 KB  |  73 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifdef notdef
  14. static char sccsid[] = "@(#)popen.c    5.4 (Berkeley) 2/18/88";
  15. #endif /* notdef */
  16.  
  17. #include <stdio.h>
  18. #include <sys/signal.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <errno.h>
  22.  
  23. #define    tst(a,b)    (*mode == 'r'? (b) : (a))
  24. #define    RDR    0
  25. #define    WTR    1
  26. static    int    popen_pid[20];
  27.  
  28. FILE *
  29. popen(cmd,mode)
  30. char    *cmd;
  31. char    *mode;
  32. {
  33.     int p[2];
  34.     register myside, hisside, pid;
  35.  
  36.     if(pipe(p) < 0)
  37.         return NULL;
  38.     myside = tst(p[WTR], p[RDR]);
  39.     hisside = tst(p[RDR], p[WTR]);
  40.     if((pid = vfork()) == 0) {
  41.         /* myside and hisside reverse roles in child */
  42.         close(myside);
  43.         dup2(hisside, tst(0, 1));
  44.         close(hisside);
  45.         execl("/bin/csh", "sh", "-c", cmd, 0);
  46.         _exit(1);
  47.     }
  48.     if(pid == -1)
  49.         return NULL;
  50.     popen_pid[myside] = pid;
  51.     close(hisside);
  52.     return(fdopen(myside, mode));
  53. }
  54.  
  55. pclose(ptr)
  56. FILE *ptr;
  57. {
  58.     register f, r;
  59.     int omask;
  60.     union wait status;
  61.     extern int errno;
  62.  
  63.     f = fileno(ptr);
  64.     fclose(ptr);
  65.     omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
  66.     while((r = wait(&status)) != popen_pid[f] && r != -1 && errno != EINTR)
  67.         ;
  68.     if(r == -1)
  69.         status.w_status = -1;
  70.     sigsetmask(omask);
  71.     return (status.w_status);
  72. }
  73.