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 {
476476pub fn getCwd(allocator: &Allocator) -> %[]u8 {
477477 switch (builtin.os) {
478478 Os.windows => {
479 @panic("implement getCwd for windows");
480 //if (windows.GetCurrentDirectoryA(buf_len(out_cwd), buf_ptr(out_cwd)) == 0) {
481 // zig_panic("GetCurrentDirectory failed");
482 //}
479 var buf = %return allocator.alloc(u8, 256);
480 %defer allocator.free(buf);
481
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 }
483496 },
484497 else => {
485498 var buf = %return allocator.alloc(u8, 1024);
std/os/path.zig+6-18
......@@ -176,25 +176,13 @@ test "os.path.networkShare" {
176176 assert(networkShare("\\\\a\\") == null);
177177}
178178
179pub fn root(path: []const u8) -> []const u8 {
180 if (is_windows) {
181 return rootWindows(path);
182 } else {
183 return rootPosix(path);
184 }
185}
179pub fn diskDesignator(path: []const u8) -> []const u8 {
180 if (!is_windows)
181 return "";
186182
187pub fn rootWindows(path: []const u8) -> []const u8 {
188183 return drive(path) ?? (networkShare(path) ?? []u8{});
189184}
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
198186// TODO ASCII is wrong, we actually need full unicode support to compare paths.
199187fn networkShareServersEql(ns1: []const u8, ns2: []const u8) -> bool {
200188 const sep1 = ns1[0];
......@@ -346,7 +334,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8
346334 mem.copy(u8, result, cwd);
347335 result_index += cwd.len;
348336
349 root_slice = rootWindows(result[0..result_index]);
337 root_slice = diskDesignator(result[0..result_index]);
350338 }
351339 %defer allocator.free(result);
352340
......@@ -362,7 +350,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8
362350 continue;
363351 }
364352 }
365 var it = mem.split(p[rootWindows(p).len..], "/\\");
353 var it = mem.split(p[diskDesignator(p).len..], "/\\");
366354 while (it.next()) |component| {
367355 if (mem.eql(u8, component, ".")) {
368356 continue;
......@@ -517,7 +505,7 @@ pub fn dirnameWindows(path: []const u8) -> []const u8 {
517505 if (path.len == 0)
518506 return path[0..0];
519507
520 const root_slice = rootWindows(path);
508 const root_slice = diskDesignator(path);
521509 if (path.len == root_slice.len)
522510 return path;
523511
std/os/windows/index.zig+4-1
......@@ -13,6 +13,8 @@ pub extern "kernel32" stdcallcc fn GetCommandLine() -> LPTSTR;
1313
1414pub 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
1618/// Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis.
1719/// Multiple threads do not overwrite each other's last-error code.
1820pub extern "kernel32" stdcallcc fn GetLastError() -> DWORD;
......@@ -50,7 +52,7 @@ pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lp
5052pub const PROV_RSA_FULL = 1;
5153
5254pub const UNICODE = false;
53pub const LPTSTR = if (unicode) LPWSTR else LPSTR;
55pub const LPTSTR = if (UNICODE) LPWSTR else LPSTR;
5456pub const LPWSTR = &WCHAR;
5557pub const LPSTR = &CHAR;
5658pub const CHAR = u8;
......@@ -59,6 +61,7 @@ pub const SIZE_T = usize;
5961
6062pub const BOOL = bool;
6163pub const BYTE = u8;
64pub const WORD = u16;
6265pub const DWORD = u32;
6366pub const FLOAT = f32;
6467pub const HANDLE = &c_void;