From f1c4c6a1c198f83079092f9026823689823c55d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ojeda=20B=C3=A4r?= Date: Mon, 23 Sep 2019 18:29:12 +0200 Subject: [PATCH 1/3] Add Filename.null --- stdlib/filename.ml | 4 ++++ stdlib/filename.mli | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/stdlib/filename.ml b/stdlib/filename.ml index 2ac74a954..b0dd5c219 100644 --- a/stdlib/filename.ml +++ b/stdlib/filename.ml @@ -70,6 +70,7 @@ let generic_dirname is_dir_sep current_dir_name name = else trailing_sep (String.length name - 1) module type SYSDEPS = sig + val null : string val current_dir_name : string val parent_dir_name : string val dir_sep : string @@ -88,6 +89,7 @@ module type SYSDEPS = sig end module Unix : SYSDEPS = struct + let null = "/dev/null" let current_dir_name = "." let parent_dir_name = ".." let dir_sep = "/" @@ -128,6 +130,7 @@ module Unix : SYSDEPS = struct end module Win32 : SYSDEPS = struct + let null = "NUL" let current_dir_name = "." let parent_dir_name = ".." let dir_sep = "\\" @@ -261,6 +264,7 @@ Quoting commands for execution by cmd.exe is difficult. end module Cygwin : SYSDEPS = struct + let null = "/dev/null" let current_dir_name = "." let parent_dir_name = ".." let dir_sep = "/" diff --git a/stdlib/filename.mli b/stdlib/filename.mli index 052301931..9e032b862 100644 --- a/stdlib/filename.mli +++ b/stdlib/filename.mli @@ -118,6 +118,10 @@ val dirname : string -> string This function conforms to the specification of POSIX.1-2008 for the [dirname] utility. *) +val null : string +(** [null] is ["/dev/null"] on POSIX and ["NUL"] on Windows. It represents a + file on the OS that discards all writes and returns end of file on reads. *) + val temp_file : ?temp_dir: string -> string -> string -> string (** [temp_file prefix suffix] returns the name of a fresh temporary file in the temporary directory. From e606e49b1619684c71d416fd8052cc8b02c5f48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ojeda=20B=C3=A4r?= Date: Mon, 23 Sep 2019 18:30:56 +0200 Subject: [PATCH 2/3] Add test --- testsuite/tests/lib-filename/null.ml | 8 ++++++++ testsuite/tests/lib-unix/common/process_pid.ml | 8 +------- 2 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 testsuite/tests/lib-filename/null.ml diff --git a/testsuite/tests/lib-filename/null.ml b/testsuite/tests/lib-filename/null.ml new file mode 100644 index 000000000..048e36622 --- /dev/null +++ b/testsuite/tests/lib-filename/null.ml @@ -0,0 +1,8 @@ +(* TEST +*) + +let () = + let ic = open_in Filename.null in + match input_char ic with + | exception End_of_file -> close_in ic + | _ -> assert false diff --git a/testsuite/tests/lib-unix/common/process_pid.ml b/testsuite/tests/lib-unix/common/process_pid.ml index 6df536bf2..8d8852f6d 100644 --- a/testsuite/tests/lib-unix/common/process_pid.ml +++ b/testsuite/tests/lib-unix/common/process_pid.ml @@ -5,17 +5,11 @@ include unix ** native *) -let null = - if Sys.win32 then - "NUL" - else - "/dev/null" - let () = let ic, _ as process = (* Redirect to null to avoid "The process tried to write to a nonexistent pipe." on Windows *) - Printf.ksprintf Unix.open_process "echo toto > %s" null + Printf.ksprintf Unix.open_process "echo toto > %s" Filename.null in assert (Unix.process_pid process = Unix.process_pid process); From 91407069a436ff737fd8ada3c0638297816cf11d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ojeda=20B=C3=A4r?= Date: Mon, 23 Sep 2019 18:35:06 +0200 Subject: [PATCH 3/3] Changes --- Changes | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Changes b/Changes index d167598fb..63581d0cb 100644 --- a/Changes +++ b/Changes @@ -195,6 +195,9 @@ Working version (Guillaume Munch-Maccagnoni, review by David Allsopp, Damien Doligez and Gabriel Scherer) +- #8971: Add `Filename.null`, the conventional name of the "null" device. + (Nicolás Ojeda Bär, review by Xavier Leroy and Alain Frisch) + ### Other libraries: - #1939, #2023: Implement Unix.truncate and Unix.ftruncate on Windows.