botched copyright
[unix-history] / usr / src / usr.bin / mail / popen.c
... / ...
CommitLineData
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
8static char *sccsid = "@(#)popen.c 5.2 (Berkeley) %G%";
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
17static int popen_pid[20];
18
19#ifndef VMUNIX
20#define vfork fork
21#endif VMUNIX
22#ifndef SIGRETRO
23#define sigchild()
24#endif
25
26FILE *
27popen(cmd,mode)
28char *cmd;
29char *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
54pclose(ptr)
55FILE *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}