summaryrefslogtreecommitdiffstats
path: root/otherlibs/unix
diff options
context:
space:
mode:
authorDamien Doligez <damien.doligez-inria.fr>1999-10-14 15:41:19 +0000
committerDamien Doligez <damien.doligez-inria.fr>1999-10-14 15:41:19 +0000
commit615389e08cd83e699d750f98bc8366aa38dbdfec (patch)
treed529e9d409c6073b8948d85f8e7eb2f30f71504a /otherlibs/unix
parentd4d7744f79a0b7ab91e4faeb91d0d677f1cf7561 (diff)
Reecriture de perform_redirection pour mieux traiter les descripteurs en double et les descripteurs deja fermes
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@2433 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'otherlibs/unix')
-rw-r--r--otherlibs/unix/unix.ml33
1 files changed, 23 insertions, 10 deletions
diff --git a/otherlibs/unix/unix.ml b/otherlibs/unix/unix.ml
index 5d5fca081..011ac5d3a 100644
--- a/otherlibs/unix/unix.ml
+++ b/otherlibs/unix/unix.ml
@@ -5,7 +5,7 @@
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
-(* Automatique. Distributed only by permission. *)
+(* en Automatique. Distributed only by permission. *)
(* *)
(***********************************************************************)
@@ -525,17 +525,30 @@ let system cmd =
exit 127
| id -> snd(waitpid [] id)
-let perform_redirections new_stdin new_stdout new_stderr =
- if new_stdin <> stdin then begin
- dup2 new_stdin stdin; close new_stdin
- end;
- if new_stdout <> stdout then begin
- dup2 new_stdout stdout; close new_stdout
- end;
- if new_stderr <> stderr then begin
- dup2 new_stderr stderr; close new_stderr
+let rec safe_dup fd =
+ let new_fd = dup fd in
+ if new_fd >= 3 then
+ new_fd
+ else begin
+ let res = safe_dup fd in
+ close new_fd;
+ res
end
+let safe_close fd =
+ try close fd with Unix_error(_,_,_) -> ()
+
+let perform_redirections new_stdin new_stdout new_stderr =
+ let newnewstdin = safe_dup new_stdin in
+ let newnewstdout = safe_dup new_stdout in
+ let newnewstderr = safe_dup new_stderr in
+ safe_close new_stdin;
+ safe_close new_stdout;
+ safe_close new_stderr;
+ dup2 newnewstdin stdin; close newnewstdin;
+ dup2 newnewstdout stdout; close newnewstdout;
+ dup2 newnewstderr stderr; close newnewstderr
+
let create_process cmd args new_stdin new_stdout new_stderr =
match fork() with
0 ->