From cc06fe7ae65d9df967f9bd508a69266d8a1f1579 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 16 Jul 2019 01:57:03 +0200 Subject: [PATCH] os.dir(); fix vroot on Windows --- compiler/main.v | 2 +- vlib/os/os.v | 10 ++++++++++ vlib/os/os_nix.v | 4 ++++ vlib/os/os_test.v | 9 +++++++++ vlib/os/os_win.v | 4 ++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/compiler/main.v b/compiler/main.v index ebf225a4ae..0492a0736a 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -933,7 +933,7 @@ fn new_v(args[]string) *V { 'option.v', ] // Location of all vlib files - vroot := os.executable().all_before_last('/') + vroot := os.dir(os.executable()) println('VROOT=$vroot') // v.exe's parent directory should contain vlib if os.dir_exists(vroot) && os.dir_exists(vroot + '/vlib/builtin') { diff --git a/vlib/os/os.v b/vlib/os/os.v index 5c0627d200..827f2d725e 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -382,6 +382,16 @@ pub fn ext(path string) string { return path.right(pos) } + +// dir returns all but the last element of path, typically the path's directory. +pub fn dir(path string) string { + pos := path.last_index(PathSeparator) + if pos == -1 { + return '.' + } + return path.left(pos) +} + fn path_sans_ext(path string) string { pos := path.last_index('.') if pos == -1 { diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index 3d553e42d7..0c0c793e0d 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -1,3 +1,7 @@ module os #include + +const ( + PathSeparator = '/' +) diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index a50f48649e..6fb5f30956 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -34,6 +34,15 @@ fn test_write_and_read_string_to_file() { os.rm(filename) } +fn test_dir() { + $if windows { + assert os.dir('C:\a\b\c') == 'C:\a\b' + + } $else { + assert os.dir('/var/tmp/foo') == '/var/tmp' + } +} + //fn test_fork() { // pid := os.fork() // if pid == 0 { diff --git a/vlib/os/os_win.v b/vlib/os/os_win.v index de3664c306..03026ac061 100644 --- a/vlib/os/os_win.v +++ b/vlib/os/os_win.v @@ -1,5 +1,9 @@ module os +const ( + PathSeparator = '/' +) + // Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types // A handle to an object. type HANDLE voidptr