From a98373f144b19cae1fd943a3c1ab655a8970b1a6 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 15 Oct 2017 14:01:55 -0400 Subject: [PATCH] use correct integer type for windows BOOL --- std/io.zig | 2 +- std/os/child_process.zig | 24 ++++++++++++++---------- std/os/index.zig | 12 ++++++------ std/os/path.zig | 2 +- std/os/windows/index.zig | 17 ++++++++++------- std/os/windows/util.zig | 10 +++++----- 6 files changed, 37 insertions(+), 30 deletions(-) diff --git a/std/io.zig b/std/io.zig index 001f638f39..dc396834e9 100644 --- a/std/io.zig +++ b/std/io.zig @@ -305,7 +305,7 @@ pub const InStream = struct { while (index < buf.len) { const want_read_count = system.DWORD(math.min(system.DWORD(@maxValue(system.DWORD)), buf.len - index)); var amt_read: system.DWORD = undefined; - if (!system.ReadFile(handle, @ptrCast(&c_void, &buf[index]), want_read_count, &amt_read, null)) { + if (system.ReadFile(handle, @ptrCast(&c_void, &buf[index]), want_read_count, &amt_read, null) == 0) { const err = system.GetLastError(); return switch (err) { system.ERROR.OPERATION_ABORTED => continue, diff --git a/std/os/child_process.zig b/std/os/child_process.zig index 4fdfb1c37d..7e4348fb1b 100644 --- a/std/os/child_process.zig +++ b/std/os/child_process.zig @@ -24,6 +24,7 @@ const is_windows = builtin.os == Os.windows; pub const ChildProcess = struct { pub pid: if (is_windows) void else i32, pub handle: if (is_windows) windows.HANDLE else void, + pub thread_handle: if (is_windows) windows.HANDLE else void, pub allocator: &mem.Allocator, @@ -82,6 +83,7 @@ pub const ChildProcess = struct { .argv = argv, .pid = undefined, .handle = undefined, + .thread_handle = undefined, .err_pipe = undefined, .llnode = undefined, .term = null, @@ -210,7 +212,7 @@ pub const ChildProcess = struct { self.term = (%Term)({ var exit_code: windows.DWORD = undefined; - if (!windows.GetExitCodeProcess(self.handle, &exit_code)) { + if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) { Term.Unknown{0} } else { Term.Exited {@bitCast(i32, exit_code)} @@ -218,6 +220,7 @@ pub const ChildProcess = struct { }); os.windowsClose(self.handle); + os.windowsClose(self.thread_handle); self.cleanupStreams(); return result; } @@ -430,10 +433,11 @@ pub const ChildProcess = struct { } fn spawnWindows(self: &ChildProcess) -> %void { - var saAttr: windows.SECURITY_ATTRIBUTES = undefined; - saAttr.nLength = @sizeOf(windows.SECURITY_ATTRIBUTES); - saAttr.bInheritHandle = true; - saAttr.lpSecurityDescriptor = null; + var saAttr = windows.SECURITY_ATTRIBUTES { + .nLength = @sizeOf(windows.SECURITY_ATTRIBUTES), + .bInheritHandle = windows.TRUE, + .lpSecurityDescriptor = null, + }; const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or @@ -571,9 +575,9 @@ pub const ChildProcess = struct { defer if (maybe_envp_buf) |envp_buf| self.allocator.free(envp_buf); const envp_ptr = if (maybe_envp_buf) |envp_buf| envp_buf.ptr else null; - if (!windows.CreateProcessA(app_name.ptr, cmd_line.ptr, null, null, true, 0, + if (windows.CreateProcessA(app_name.ptr, cmd_line.ptr, null, null, windows.TRUE, 0, @ptrCast(?&c_void, envp_ptr), - cwd_ptr, &siStartInfo, &piProcInfo)) + cwd_ptr, &siStartInfo, &piProcInfo) == windows.FALSE) { const err = windows.GetLastError(); return switch (err) { @@ -582,7 +586,6 @@ pub const ChildProcess = struct { else => error.Unexpected, }; } - os.windowsClose(piProcInfo.hThread); if (stdin_ptr) |outstream| { *outstream = io.OutStream { @@ -609,6 +612,7 @@ pub const ChildProcess = struct { } self.handle = piProcInfo.hProcess; + self.thread_handle = piProcInfo.hThread; self.term = null; self.stdin = stdin_ptr; self.stdout = stdout_ptr; @@ -672,7 +676,7 @@ fn windowsDestroyPipe(rd: ?windows.HANDLE, wr: ?windows.HANDLE) { } fn windowsMakePipe(rd: &windows.HANDLE, wr: &windows.HANDLE, sattr: &windows.SECURITY_ATTRIBUTES) -> %void { - if (!windows.CreatePipe(rd, wr, sattr, 0)) { + if (windows.CreatePipe(rd, wr, sattr, 0) == 0) { const err = windows.GetLastError(); return switch (err) { else => error.Unexpected, @@ -681,7 +685,7 @@ fn windowsMakePipe(rd: &windows.HANDLE, wr: &windows.HANDLE, sattr: &windows.SEC } fn windowsSetHandleInfo(h: windows.HANDLE, mask: windows.DWORD, flags: windows.DWORD) -> %void { - if (!windows.SetHandleInformation(h, mask, flags)) { + if (windows.SetHandleInformation(h, mask, flags) == 0) { const err = windows.GetLastError(); return switch (err) { else => error.Unexpected, diff --git a/std/os/index.zig b/std/os/index.zig index 1eb422b57b..5eb3c1b8ef 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -95,12 +95,12 @@ pub fn getRandomBytes(buf: []u8) -> %void { }, Os.windows => { var hCryptProv: windows.HCRYPTPROV = undefined; - if (!windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0)) { + if (windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0) == 0) { return error.Unexpected; } defer _ = windows.CryptReleaseContext(hCryptProv, 0); - if (!windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr)) { + if (windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr) == 0) { return error.Unexpected; } }, @@ -417,7 +417,7 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap { if (is_windows) { const ptr = windows.GetEnvironmentStringsA() ?? return error.OutOfMemory; - defer assert(windows.FreeEnvironmentStringsA(ptr)); + defer assert(windows.FreeEnvironmentStringsA(ptr) != 0); var i: usize = 0; while (true) { @@ -657,7 +657,7 @@ pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void mem.copy(u8, buf, file_path); buf[file_path.len] = 0; - if (!windows.DeleteFileA(buf.ptr)) { + if (windows.DeleteFileA(buf.ptr) == 0) { const err = windows.GetLastError(); return switch (err) { windows.ERROR.FILE_NOT_FOUND => error.FileNotFound, @@ -740,7 +740,7 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8) if (is_windows) { const flags = windows.MOVEFILE_REPLACE_EXISTING|windows.MOVEFILE_WRITE_THROUGH; - if (!windows.MoveFileExA(old_buf.ptr, new_buf.ptr, flags)) { + if (windows.MoveFileExA(old_buf.ptr, new_buf.ptr, flags) == 0) { const err = windows.GetLastError(); return switch (err) { else => return error.Unexpected, @@ -783,7 +783,7 @@ pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) -> %void { const path_buf = %return cstr.addNullByte(allocator, dir_path); defer allocator.free(path_buf); - if (!windows.CreateDirectoryA(path_buf.ptr, null)) { + if (windows.CreateDirectoryA(path_buf.ptr, null) == 0) { const err = windows.GetLastError(); return switch (err) { windows.ERROR.ALREADY_EXISTS => error.PathAlreadyExists, diff --git a/std/os/path.zig b/std/os/path.zig index 878e4e9e53..93b7bc017c 100644 --- a/std/os/path.zig +++ b/std/os/path.zig @@ -941,7 +941,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { else => error.Unexpected, }; } - defer assert(windows.CloseHandle(h_file)); + defer os.windowsClose(h_file); var buf = %return allocator.alloc(u8, 256); %defer allocator.free(buf); while (true) { diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig index bad7eaf19c..290953a5da 100644 --- a/std/os/windows/index.zig +++ b/std/os/windows/index.zig @@ -1,11 +1,11 @@ pub const ERROR = @import("error.zig"); pub extern "advapi32" stdcallcc fn CryptAcquireContextA(phProv: &HCRYPTPROV, pszContainer: ?LPCSTR, - pszProvider: ?LPCSTR, dwProvType: DWORD, dwFlags: DWORD) -> bool; + pszProvider: ?LPCSTR, dwProvType: DWORD, dwFlags: DWORD) -> BOOL; -pub extern "advapi32" stdcallcc fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlags: DWORD) -> bool; +pub extern "advapi32" stdcallcc fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlags: DWORD) -> BOOL; -pub extern "advapi32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) -> bool; +pub extern "advapi32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) -> BOOL; pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) -> BOOL; @@ -28,7 +28,7 @@ pub extern "kernel32" stdcallcc fn CreateProcessA(lpApplicationName: ?LPCSTR, lp pub extern "kernel32" stdcallcc fn CreateSymbolicLinkA(lpSymlinkFileName: LPCSTR, lpTargetFileName: LPCSTR, dwFlags: DWORD) -> BOOLEAN; -pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: LPCSTR) -> bool; +pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: LPCSTR) -> BOOL; pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) -> noreturn; @@ -36,7 +36,7 @@ pub extern "kernel32" stdcallcc fn FreeEnvironmentStringsA(penv: LPCH) -> BOOL; pub extern "kernel32" stdcallcc fn GetCommandLineA() -> LPSTR; -pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: &DWORD) -> bool; +pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: &DWORD) -> BOOL; pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) -> DWORD; @@ -50,7 +50,7 @@ pub extern "kernel32" stdcallcc fn GetLastError() -> DWORD; pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(in_hFile: HANDLE, in_FileInformationClass: FILE_INFO_BY_HANDLE_CLASS, out_lpFileInformation: &c_void, - in_dwBufferSize: DWORD) -> bool; + in_dwBufferSize: DWORD) -> BOOL; pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA(hFile: HANDLE, lpszFilePath: LPSTR, cchFilePath: DWORD, dwFlags: DWORD) -> DWORD; @@ -86,7 +86,7 @@ pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lp pub const PROV_RSA_FULL = 1; -pub const BOOL = bool; +pub const BOOL = c_int; pub const BOOLEAN = BYTE; pub const BYTE = u8; pub const CHAR = u8; @@ -116,6 +116,9 @@ pub const UNICODE = false; pub const WCHAR = u16; pub const WORD = u16; +pub const TRUE = 1; +pub const FALSE = 0; + /// The standard input device. Initially, this is the console input buffer, CONIN$. pub const STD_INPUT_HANDLE = @maxValue(DWORD) - 10 + 1; diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig index 99a6dd2a48..84eceb81af 100644 --- a/std/os/windows/util.zig +++ b/std/os/windows/util.zig @@ -22,7 +22,7 @@ pub fn windowsWaitSingle(handle: windows.HANDLE, milliseconds: windows.DWORD) -> } pub fn windowsClose(handle: windows.HANDLE) { - assert(windows.CloseHandle(handle)); + assert(windows.CloseHandle(handle) != 0); } error SystemResources; @@ -31,7 +31,7 @@ error IoPending; error BrokenPipe; pub fn windowsWrite(handle: windows.HANDLE, bytes: []const u8) -> %void { - if (!windows.WriteFile(handle, @ptrCast(&const c_void, bytes.ptr), u32(bytes.len), null, null)) { + if (windows.WriteFile(handle, @ptrCast(&const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) { return switch (windows.GetLastError()) { windows.ERROR.INVALID_USER_BUFFER => error.SystemResources, windows.ERROR.NOT_ENOUGH_MEMORY => error.SystemResources, @@ -49,15 +49,15 @@ pub fn windowsIsTty(handle: windows.HANDLE) -> bool { return true; var out: windows.DWORD = undefined; - return windows.GetConsoleMode(handle, &out); + return windows.GetConsoleMode(handle, &out) != 0; } pub fn windowsIsCygwinPty(handle: windows.HANDLE) -> bool { const size = @sizeOf(windows.FILE_NAME_INFO); var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = []u8{0} ** (size + windows.MAX_PATH); - if (!windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, - @ptrCast(&c_void, &name_info_bytes[0]), u32(name_info_bytes.len))) + if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, + @ptrCast(&c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0) { return true; }