1 # &open3: Marc Horowitz <marc@mit.edu>
2 # derived mostly from &open2 by tom christiansen, <tchrist@convex.com>
4 # $Id: open3.pl,v 1.1 1993/11/23 06:26:15 marc Exp $
6 # usage: $pid = open3('wtr', 'rdr', 'err' 'some cmd and args', 'optarg', ...);
8 # spawn the given $cmd and connect rdr for
9 # reading, wtr for writing, and err for errors.
10 # if err is '', or the same as rdr, then stdout and
11 # stderr of the child are on the same fh. returns pid
12 # of child, or 0 on failure.
15 # if wtr begins with '>&', then wtr will be closed in the parent, and
16 # the child will read from it directly. if rdr or err begins with
17 # '>&', then the child will send output directly to that fd. In both
18 # cases, there will be a dup() instead of a pipe() made.
21 # WARNING: this is dangerous, as you may block forever
22 # unless you are very careful.
24 # $wtr is left unbuffered.
28 # pipe or fork or exec fails
32 $fh = 'FHOPEN000'; # package static in case called more than once
36 local($dad_wtr, $dad_rdr, $dad_err, @cmd) = @_;
37 local($dup_wtr, $dup_rdr, $dup_err);
39 $dad_wtr || die "open3: wtr should not be null";
40 $dad_rdr || die "open3: rdr should not be null";
41 $dad_err = $dad_rdr if ($dad_err eq '');
43 $dup_wtr = ($dad_wtr =~ s/^\>\&//);
44 $dup_rdr = ($dad_rdr =~ s/^\>\&//);
45 $dup_err = ($dad_err =~ s/^\>\&//);
47 # force unqualified filehandles into callers' package
48 local($package) = caller;
49 $dad_wtr =~ s/^[^']+$/$package'$&/;
50 $dad_rdr =~ s/^[^']+$/$package'$&/;
51 $dad_err =~ s/^[^']+$/$package'$&/;
53 local($kid_rdr) = ++$fh;
54 local($kid_wtr) = ++$fh;
55 local($kid_err) = ++$fh;
58 pipe($kid_rdr, $dad_wtr) || die "open3: pipe 1 (stdin) failed: $!";
61 pipe($dad_rdr, $kid_wtr) || die "open3: pipe 2 (stdout) failed: $!";
63 if ($dad_err ne $dad_rdr && !$dup_err) {
64 pipe($dad_err, $kid_err) || die "open3: pipe 3 (stderr) failed: $!";
67 if (($kidpid = fork) < 0) {
68 die "open2: fork failed: $!";
69 } elsif ($kidpid == 0) {
71 open(STDIN, ">&$dad_wtr") if (fileno(STDIN) != fileno($dad_wtr));
74 open(STDIN, ">&$kid_rdr");
77 open(STDOUT, ">&$dad_rdr") if (fileno(STDOUT) != fileno($dad_rdr));
80 open(STDOUT, ">&$kid_wtr");
82 if ($dad_rdr ne $dad_err) {
84 open(STDERR, ">&$dad_err")
85 if (fileno(STDERR) != fileno($dad_err));
88 open(STDERR, ">&$kid_err");
91 open(STDERR, ">&STDOUT") if (fileno(STDERR) != fileno(STDOUT));
95 die "open2: exec of @cmd failed";
98 close $kid_rdr; close $kid_wtr; close $kid_err;
103 select((select($dad_wtr), $| = 1)[0]); # unbuffer pipe
106 1; # so require is happy