From 252540a1ea4ed32ba69d85bf5e9dda92e4f5cf1f Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 8 Jan 2024 18:00:36 +0200 Subject: [PATCH] os: add `os.path_devnull` (containing /dev/null on POSIX and \\.\nul on Windows) (#20439) --- vlib/os/file_test.v | 15 +++++++++++++++ vlib/os/os.js.v | 2 ++ vlib/os/os_nix.c.v | 9 +++++++++ vlib/os/os_windows.c.v | 15 ++++++++++++--- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/vlib/os/file_test.v b/vlib/os/file_test.v index a4fe309d074471..764360019da421 100644 --- a/vlib/os/file_test.v +++ b/vlib/os/file_test.v @@ -477,3 +477,18 @@ fn test_open_file_crlf_binary_mode() { assert fcont_wb == teststr } + +fn test_path_devnull() { + dump(os.path_devnull) + content := os.read_file(os.path_devnull)! + // dump(content) + // dump(content.len) + + os.write_file(os.path_devnull, 'something')! + + content_after := os.read_file(os.path_devnull)! + // dump(content_after) + // dump(content_after.len) + assert content.len == 0 + assert content_after.len == 0 +} diff --git a/vlib/os/os.js.v b/vlib/os/os.js.v index ad177c161dc111..f9d9c735c667bf 100644 --- a/vlib/os/os.js.v +++ b/vlib/os/os.js.v @@ -8,6 +8,8 @@ $if js_node { pub const path_delimiter = get_path_delimiter() pub const path_separator = get_path_separator() +pub const path_devnull = '/dev/null' // TODO + pub const args = []string{} const executable_suffixes = [''] diff --git a/vlib/os/os_nix.c.v b/vlib/os/os_nix.c.v index 8906c2ee9cfb20..8fdc6492157022 100644 --- a/vlib/os/os_nix.c.v +++ b/vlib/os/os_nix.c.v @@ -9,9 +9,18 @@ import strings #include #include +// path_separator is the platform specific separator string, used between the folders +// and filenames in a path. It is '/' on POSIX, and '\\' on Windows. pub const path_separator = '/' + +// path_delimiter is the platform specific delimiter string, used between the paths +// in environment variables like PATH. It is ':' on POSIX, and ';' on Windows. pub const path_delimiter = ':' +// path_devnull is a platform-specific file path of the null device. +// It is '/dev/null' on POSIX, and r'\\.\nul' on Windows. +pub const path_devnull = '/dev/null' + const executable_suffixes = [''] const stdin_value = 0 diff --git a/vlib/os/os_windows.c.v b/vlib/os/os_windows.c.v index 8356771a38a0d1..3b7f39d3d30673 100644 --- a/vlib/os/os_windows.c.v +++ b/vlib/os/os_windows.c.v @@ -6,6 +6,18 @@ import strings #include #include +// path_separator is the platform specific separator string, used between the folders +// and filenames in a path. It is '/' on POSIX, and '\\' on Windows. +pub const path_separator = '\\' + +// path_delimiter is the platform specific delimiter string, used between the paths +// in environment variables like PATH. It is ':' on POSIX, and ';' on Windows. +pub const path_delimiter = ';' + +// path_devnull is a platform-specific file path of the null device. +// It is '/dev/null' on POSIX, and r'\\.\nul' on Windows. +pub const path_devnull = r'\\.\nul' + // See https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw fn C.CreateSymbolicLinkW(&u16, &u16, u32) int @@ -16,9 +28,6 @@ fn C._getpid() int const executable_suffixes = ['.exe', '.bat', '.cmd', ''] -pub const path_separator = '\\' -pub const path_delimiter = ';' - // Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types // A handle to an object. pub type HANDLE = voidptr