home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / ucbmail / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-03  |  2.0 KB  |  107 lines

  1. /*
  2.  *  P O P E N . C 
  3.  *
  4.  *  EE/CIS Computer Lab
  5.  *  Department of Computer and Information Sciences
  6.  *  Department of Electrical Engineering
  7.  *  University of Delaware
  8.  *
  9.  *  REVISION HISTORY:
  10.  *
  11.  *  $Revision: 1.3 $
  12.  *
  13.  *  $Log:    popen.c,v $
  14.  * Revision 1.3  85/11/16  15:19:15  galvin
  15.  * Added define for sigmask for backward compatibility from 4.3bsd to 4.2bsd.
  16.  * 
  17.  * Revision 1.2  85/11/16  14:43:38  galvin
  18.  * Added comment header for revision history.
  19.  * 
  20.  *
  21.  */
  22.  
  23. /*
  24.  * Copyright (c) 1980 Regents of the University of California.
  25.  * All rights reserved.  The Berkeley software License Agreement
  26.  * specifies the terms and conditions for redistribution.
  27.  */
  28.  
  29. #ifndef lint
  30. static char *sccsid = "@(#)popen.c    5.2 (Berkeley) 6/21/85";
  31. #endif not lint
  32.  
  33. #include <stdio.h>
  34. #include <signal.h>
  35. #include <errno.h>
  36. #include "./sigretro.h"
  37. #define    tst(a,b)    (*mode == 'r'? (b) : (a))
  38. #define    RDR    0
  39. #define    WTR    1
  40. static    int    popen_pid[20];
  41.  
  42. #ifndef VMUNIX
  43. #define vfork    fork
  44. #endif VMUNIX
  45. #ifndef    SIGRETRO
  46. #define    sigchild()
  47. #endif
  48.  
  49. FILE *
  50. popen(cmd,mode)
  51. char    *cmd;
  52. char    *mode;
  53. {
  54.     int p[2];
  55.     register myside, hisside, pid;
  56.  
  57.     if(pipe(p) < 0)
  58.         return NULL;
  59.     myside = tst(p[WTR], p[RDR]);
  60.     hisside = tst(p[RDR], p[WTR]);
  61.     if((pid = vfork()) == 0) {
  62.         /* myside and hisside reverse roles in child */
  63.         sigchild();
  64.         close(myside);
  65.         dup2(hisside, tst(0, 1));
  66.         close(hisside);
  67.         execl("/bin/csh", "sh", "-c", cmd, 0);
  68.         _exit(1);
  69.     }
  70.     if(pid == -1)
  71.         return NULL;
  72.     popen_pid[myside] = pid;
  73.     close(hisside);
  74.     return(fdopen(myside, mode));
  75. }
  76.  
  77. pclose(ptr)
  78. FILE *ptr;
  79. {
  80.     register f, r;
  81.     int status, omask;
  82.     extern int errno;
  83.  
  84.     f = fileno(ptr);
  85.     fclose(ptr);
  86. # ifdef V4_2BSD
  87.     omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
  88. # endif V4_2BSD
  89. # ifdef V4_1BSD
  90.     sighold(SIGINT);
  91.     sighold(SIGQUIT);
  92.     sighold(SIGHUP);
  93. # endif V4_2BSD
  94.     while((r = wait(&status)) != popen_pid[f] && r != -1 && errno != EINTR)
  95.         ;
  96.     if(r == -1)
  97.         status = -1;
  98. # ifdef V4_2BSD
  99.     sigsetmask(omask);
  100. # endif V4_2BSD
  101. # ifdef V4_1BSD
  102.     sigrelse(SIGINT);
  103.     sigrelse(SIGQUIT);
  104.     sigrelse(SIGHUP);
  105. # endif V4_2BSD
  106. }
  107.