1: /*	@(#)popen.c	2.1	SCCS id keyword	*/
   2: #include <whoami.h>
   3: #include <stdio.h>
   4: #include <signal.h>
   5: #define tst(a,b)    (*mode == 'r'? (b) : (a))
   6: #define RDR 0
   7: #define WTR 1
   8: static  int popen_pid[20];
   9: 
  10: FILE *
  11: popen(cmd,mode)
  12: char    *cmd;
  13: char    *mode;
  14: {
  15:     int p[2];
  16:     register myside, hisside, pid;
  17: 
  18:     if(pipe(p) < 0)
  19:         return NULL;
  20:     myside = tst(p[WTR], p[RDR]);
  21:     hisside = tst(p[RDR], p[WTR]);
  22: #ifdef  VIRUS_VFORK
  23:     if((pid = vfork()) == 0)
  24: #else
  25:     if((pid = fork()) == 0)
  26: #endif
  27:     {
  28:         /* myside and hisside reverse roles in child */
  29:         close(myside);
  30:         dup2(hisside, tst(0, 1));
  31:         close(hisside);
  32:         execl("/bin/sh", "sh", "-c", cmd, 0);
  33:         _exit(1);
  34:     }
  35:     if(pid == -1) {
  36:         close(myside);
  37:         close(hisside);
  38:         return NULL;
  39:     }
  40:     popen_pid[myside] = pid;
  41:     close(hisside);
  42:     return(fdopen(myside, mode));
  43: }
  44: 
  45: pclose(ptr)
  46: FILE *ptr;
  47: {
  48:     register f, r, (*hstat)(), (*istat)(), (*qstat)();
  49:     int status;
  50: 
  51:     f = fileno(ptr);
  52:     fclose(ptr);
  53:     istat = signal(SIGINT, SIG_IGN);
  54:     qstat = signal(SIGQUIT, SIG_IGN);
  55:     hstat = signal(SIGHUP, SIG_IGN);
  56:     while((r = wait(&status)) != popen_pid[f] && r != -1)
  57:         ;
  58:     if(r == -1)
  59:         status = -1;
  60:     signal(SIGINT, istat);
  61:     signal(SIGQUIT, qstat);
  62:     signal(SIGHUP, hstat);
  63:     return(status);
  64: }

Defined functions

Defined variables

popen_pid defined in line 8; used 2 times

Defined macros

RDR defined in line 6; used 2 times
WTR defined in line 7; used 2 times
tst defined in line 5; used 3 times
Last modified: 1983-07-30
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 810
Valid CSS Valid XHTML 1.0 Strict