Skip to content

Commit

Permalink
os: add os.path_devnull (containing /dev/null on POSIX and \\.\nul …
Browse files Browse the repository at this point in the history
…on Windows) (#20439)
  • Loading branch information
spytheman committed Jan 8, 2024
1 parent 34da4c9 commit 252540a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
15 changes: 15 additions & 0 deletions vlib/os/file_test.v
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions vlib/os/os.js.v
Expand Up @@ -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 = ['']
Expand Down
9 changes: 9 additions & 0 deletions vlib/os/os_nix.c.v
Expand Up @@ -9,9 +9,18 @@ import strings
#include <sys/types.h>
#include <utime.h>

// 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
Expand Down
15 changes: 12 additions & 3 deletions vlib/os/os_windows.c.v
Expand Up @@ -6,6 +6,18 @@ import strings
#include <process.h>
#include <sys/utime.h>

// 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

Expand All @@ -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
Expand Down

0 comments on commit 252540a

Please sign in to comment.