Skip to content

Commit

Permalink
use correct integer type for windows BOOL
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Oct 15, 2017
1 parent 3c19883 commit a98373f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 30 deletions.
2 changes: 1 addition & 1 deletion std/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 14 additions & 10 deletions std/os/child_process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -82,6 +83,7 @@ pub const ChildProcess = struct {
.argv = argv,
.pid = undefined,
.handle = undefined,
.thread_handle = undefined,
.err_pipe = undefined,
.llnode = undefined,
.term = null,
Expand Down Expand Up @@ -210,14 +212,15 @@ 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)}
}
});

os.windowsClose(self.handle);
os.windowsClose(self.thread_handle);
self.cleanupStreams();
return result;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -582,7 +586,6 @@ pub const ChildProcess = struct {
else => error.Unexpected,
};
}
os.windowsClose(piProcInfo.hThread);

if (stdin_ptr) |outstream| {
*outstream = io.OutStream {
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions std/os/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion std/os/path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 10 additions & 7 deletions std/os/windows/index.zig
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,15 +28,15 @@ 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;

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;

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

Expand Down
10 changes: 5 additions & 5 deletions std/os/windows/util.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
}
Expand Down

0 comments on commit a98373f

Please sign in to comment.