authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-08 21:40:33-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-08 21:44:24-04:00
log9d5f15fe3da40813527d19c75b3633dcf2d00b0b
tree06cc475a5440708b2fd00fd1b0994dc39ca75a75
parent7cfab2fb5f0f0dce81feab9dd8ad3d3e1b435044

implement os.getCwd for windows


3 files changed, 27 insertions(+), 23 deletions(-)

std/os/index.zig+17-4
...@@ -476,10 +476,23 @@ pub const args = struct {...@@ -476,10 +476,23 @@ pub const args = struct {
476pub fn getCwd(allocator: &Allocator) -> %[]u8 {476pub fn getCwd(allocator: &Allocator) -> %[]u8 {
477 switch (builtin.os) {477 switch (builtin.os) {
478 Os.windows => {478 Os.windows => {
479 @panic("implement getCwd for windows");479 var buf = %return allocator.alloc(u8, 256);
480 //if (windows.GetCurrentDirectoryA(buf_len(out_cwd), buf_ptr(out_cwd)) == 0) {480 %defer allocator.free(buf);
481 // zig_panic("GetCurrentDirectory failed");481
482 //}482 while (true) {
483 const result = windows.GetCurrentDirectoryA(windows.WORD(buf.len), buf.ptr);
484
485 if (result == 0) {
486 return error.Unexpected;
487 }
488
489 if (result > buf.len) {
490 buf = %return allocator.realloc(u8, buf, result);
491 continue;
492 }
493
494 return buf[0..result];
495 }
483 },496 },
484 else => {497 else => {
485 var buf = %return allocator.alloc(u8, 1024);498 var buf = %return allocator.alloc(u8, 1024);
std/os/path.zig+6-18
...@@ -176,25 +176,13 @@ test "os.path.networkShare" {...@@ -176,25 +176,13 @@ test "os.path.networkShare" {
176 assert(networkShare("\\\\a\\") == null);176 assert(networkShare("\\\\a\\") == null);
177}177}
178178
179pub fn root(path: []const u8) -> []const u8 {179pub fn diskDesignator(path: []const u8) -> []const u8 {
180 if (is_windows) {180 if (!is_windows)
181 return rootWindows(path);181 return "";
182 } else {
183 return rootPosix(path);
184 }
185}
186182
187pub fn rootWindows(path: []const u8) -> []const u8 {
188 return drive(path) ?? (networkShare(path) ?? []u8{});183 return drive(path) ?? (networkShare(path) ?? []u8{});
189}184}
190185
191pub fn rootPosix(path: []const u8) -> []const u8 {
192 if (path.len == 0 or path[0] != '/')
193 return []u8{};
194
195 return path[0..1];
196}
197
198// TODO ASCII is wrong, we actually need full unicode support to compare paths.186// TODO ASCII is wrong, we actually need full unicode support to compare paths.
199fn networkShareServersEql(ns1: []const u8, ns2: []const u8) -> bool {187fn networkShareServersEql(ns1: []const u8, ns2: []const u8) -> bool {
200 const sep1 = ns1[0];188 const sep1 = ns1[0];
...@@ -346,7 +334,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8...@@ -346,7 +334,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8
346 mem.copy(u8, result, cwd);334 mem.copy(u8, result, cwd);
347 result_index += cwd.len;335 result_index += cwd.len;
348336
349 root_slice = rootWindows(result[0..result_index]);337 root_slice = diskDesignator(result[0..result_index]);
350 }338 }
351 %defer allocator.free(result);339 %defer allocator.free(result);
352340
...@@ -362,7 +350,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8...@@ -362,7 +350,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8
362 continue;350 continue;
363 }351 }
364 }352 }
365 var it = mem.split(p[rootWindows(p).len..], "/\\");353 var it = mem.split(p[diskDesignator(p).len..], "/\\");
366 while (it.next()) |component| {354 while (it.next()) |component| {
367 if (mem.eql(u8, component, ".")) {355 if (mem.eql(u8, component, ".")) {
368 continue;356 continue;
...@@ -517,7 +505,7 @@ pub fn dirnameWindows(path: []const u8) -> []const u8 {...@@ -517,7 +505,7 @@ pub fn dirnameWindows(path: []const u8) -> []const u8 {
517 if (path.len == 0)505 if (path.len == 0)
518 return path[0..0];506 return path[0..0];
519507
520 const root_slice = rootWindows(path);508 const root_slice = diskDesignator(path);
521 if (path.len == root_slice.len)509 if (path.len == root_slice.len)
522 return path;510 return path;
523511
std/os/windows/index.zig+4-1
...@@ -13,6 +13,8 @@ pub extern "kernel32" stdcallcc fn GetCommandLine() -> LPTSTR;...@@ -13,6 +13,8 @@ pub extern "kernel32" stdcallcc fn GetCommandLine() -> LPTSTR;
1313
14pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: &DWORD) -> bool;14pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: &DWORD) -> bool;
1515
16pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPTSTR) -> DWORD;
17
16/// Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis.18/// Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis.
17/// Multiple threads do not overwrite each other's last-error code.19/// Multiple threads do not overwrite each other's last-error code.
18pub extern "kernel32" stdcallcc fn GetLastError() -> DWORD;20pub extern "kernel32" stdcallcc fn GetLastError() -> DWORD;
...@@ -50,7 +52,7 @@ pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lp...@@ -50,7 +52,7 @@ pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lp
50pub const PROV_RSA_FULL = 1;52pub const PROV_RSA_FULL = 1;
5153
52pub const UNICODE = false;54pub const UNICODE = false;
53pub const LPTSTR = if (unicode) LPWSTR else LPSTR;55pub const LPTSTR = if (UNICODE) LPWSTR else LPSTR;
54pub const LPWSTR = &WCHAR;56pub const LPWSTR = &WCHAR;
55pub const LPSTR = &CHAR;57pub const LPSTR = &CHAR;
56pub const CHAR = u8;58pub const CHAR = u8;
...@@ -59,6 +61,7 @@ pub const SIZE_T = usize;...@@ -59,6 +61,7 @@ pub const SIZE_T = usize;
5961
60pub const BOOL = bool;62pub const BOOL = bool;
61pub const BYTE = u8;63pub const BYTE = u8;
64pub const WORD = u16;
62pub const DWORD = u32;65pub const DWORD = u32;
63pub const FLOAT = f32;66pub const FLOAT = f32;
64pub const HANDLE = &c_void;67pub const HANDLE = &c_void;