1: /*
   2:  * Copyright (c) 1980 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #ifndef lint
   8: static char *sccsid = "@(#)popen.c	5.2 (Berkeley) 6/21/85";
   9: #endif not lint
  10: 
  11: #include <stdio.h>
  12: #include <signal.h>
  13: #include <errno.h>
  14: #define tst(a,b)    (*mode == 'r'? (b) : (a))
  15: #define RDR 0
  16: #define WTR 1
  17: static  int popen_pid[20];
  18: 
  19: #ifndef VMUNIX
  20: #define vfork   fork
  21: #endif VMUNIX
  22: #ifndef SIGRETRO
  23: #define sigchild()
  24: #endif
  25: 
  26: FILE *
  27: popen(cmd,mode)
  28: char    *cmd;
  29: char    *mode;
  30: {
  31:     int p[2];
  32:     register myside, hisside, pid;
  33: 
  34:     if(pipe(p) < 0)
  35:         return NULL;
  36:     myside = tst(p[WTR], p[RDR]);
  37:     hisside = tst(p[RDR], p[WTR]);
  38:     if((pid = vfork()) == 0) {
  39:         /* myside and hisside reverse roles in child */
  40:         sigchild();
  41:         close(myside);
  42:         dup2(hisside, tst(0, 1));
  43:         close(hisside);
  44:         execl("/bin/csh", "sh", "-c", cmd, 0);
  45:         _exit(1);
  46:     }
  47:     if(pid == -1)
  48:         return NULL;
  49:     popen_pid[myside] = pid;
  50:     close(hisside);
  51:     return(fdopen(myside, mode));
  52: }
  53: 
  54: pclose(ptr)
  55: FILE *ptr;
  56: {
  57:     register f, r;
  58:     int status, omask;
  59:     extern int errno;
  60: 
  61:     f = fileno(ptr);
  62:     fclose(ptr);
  63: # ifdef VMUNIX
  64:     omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
  65: # endif VMUNIX
  66:     while((r = wait(&status)) != popen_pid[f] && r != -1 && errno != EINTR)
  67:         ;
  68:     if(r == -1)
  69:         status = -1;
  70: # ifdef VMUNIX
  71:     sigsetmask(omask);
  72: # endif VMUNIX
  73:     return(status);
  74: }

Defined functions

pclose defined in line 54; used 3 times
popen defined in line 26; used 2 times

Defined variables

popen_pid defined in line 17; used 2 times
sccsid defined in line 8; never used

Defined macros

RDR defined in line 15; used 2 times
WTR defined in line 16; used 2 times
sigchild defined in line 23; used 1 times
  • in line 40
tst defined in line 14; used 3 times
vfork defined in line 20; used 1 times
  • in line 38
Last modified: 1985-06-22
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1077
Valid CSS Valid XHTML 1.0 Strict