Skip to content

Commit

Permalink
Move win32 libc bindings from winbase.cr to approriate files (#10771)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Jun 4, 2021
1 parent bf809ac commit 93a65e3
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 73 deletions.
2 changes: 2 additions & 0 deletions src/crystal/system/win32/dir.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "crystal/system/windows"
require "c/winbase"
require "c/direct"
require "c/handleapi"
require "c/processenv"

module Crystal::System::Dir
private class DirHandle
Expand Down
1 change: 1 addition & 0 deletions src/crystal/system/win32/env.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "crystal/system/windows"
require "c/winbase"
require "c/processenv"

module Crystal::System::Env
# Sets an environment variable or unsets it if *value* is `nil`.
Expand Down
1 change: 1 addition & 0 deletions src/crystal/system/win32/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require "c/fileapi"
require "c/sys/utime"
require "c/sys/stat"
require "c/winbase"
require "c/handleapi"

module Crystal::System::File
def self.open(filename : String, mode : String, perm : Int32 | ::File::Permissions) : LibC::Int
Expand Down
1 change: 1 addition & 0 deletions src/crystal/system/win32/process.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "c/processthreadsapi"
require "c/handleapi"
require "process/shell"

struct Crystal::System::Process
Expand Down
2 changes: 2 additions & 0 deletions src/crystal/system/win32/time.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "c/winbase"
require "c/timezoneapi"
require "c/windows"
require "./zone_names"

module Crystal::System::Time
Expand Down
6 changes: 6 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/errhandlingapi.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "c/int_safe"

lib LibC
fun GetLastError : DWORD
fun SetLastError(dwErrCode : DWORD)
end
10 changes: 10 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/fileapi.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "c/winnt"
require "c/basetsd"
require "c/wtypesbase"

lib LibC
fun GetFullPathNameW(lpFileName : LPWSTR, nBufferLength : DWORD, lpBuffer : LPWSTR, lpFilePart : LPWSTR*) : DWORD
Expand All @@ -12,6 +13,15 @@ lib LibC

fun GetFileType(hFile : HANDLE) : DWORD

struct WIN32_FILE_ATTRIBUTE_DATA
dwFileAttributes : DWORD
ftCreationTime : FILETIME
ftLastAccessTime : FILETIME
ftLastWriteTime : FILETIME
nFileSizeHigh : DWORD
nFileSizeLow : DWORD
end

struct BY_HANDLE_FILE_INFORMATION
dwFileAttributes : DWORD
ftCreationTime : FILETIME
Expand Down
11 changes: 11 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/handleapi.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "c/winnt"

lib LibC
INVALID_HANDLE_VALUE = HANDLE.new(-1)

fun CloseHandle(hObject : HANDLE) : BOOL

fun DuplicateHandle(hSourceProcessHandle : HANDLE, hSourceHandle : HANDLE,
hTargetProcessHandle : HANDLE, lpTargetHandle : HANDLE*,
dwDesiredAccess : DWORD, bInheritHandle : BOOL, dwOptions : DWORD) : BOOL
end
26 changes: 26 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/minwinbase.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# part of winbase
lib LibC
struct FILETIME
dwLowDateTime : DWORD
dwHighDateTime : DWORD
end

struct SYSTEMTIME
wYear : WORD
wMonth : WORD
wDayOfWeek : WORD
wDay : WORD
wHour : WORD
wMinute : WORD
wSecond : WORD
wMilliseconds : WORD
end

enum GET_FILEEX_INFO_LEVELS
GetFileExInfoStandard
GetFileExMaxInfoLevel
end

STATUS_PENDING = 0x103
STILL_ACTIVE = STATUS_PENDING
end
11 changes: 11 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/processenv.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "c/winnt"

lib LibC
fun GetCurrentDirectoryW(nBufferLength : DWORD, lpBuffer : LPWSTR) : DWORD
fun SetCurrentDirectoryW(lpPathname : LPWSTR) : BOOL

fun GetEnvironmentVariableW(lpName : LPWSTR, lpBuffer : LPWSTR, nSize : DWORD) : DWORD
fun GetEnvironmentStringsW : LPWCH
fun FreeEnvironmentStringsW(lpszEnvironmentBlock : LPWCH) : BOOL
fun SetEnvironmentVariableW(lpName : LPWSTR, lpValue : LPWSTR) : BOOL
end
1 change: 1 addition & 0 deletions src/lib_c/x86_64-windows-msvc/c/processthreadsapi.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "./basetsd"
require "c/wtypesbase"

lib LibC
CREATE_UNICODE_ENVIRONMENT = 0x00000400
Expand Down
6 changes: 6 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/profileapi.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# part of windows

lib LibC
fun QueryPerformanceCounter(lpPerformanceCount : LARGE_INTEGER*) : BOOL
fun QueryPerformanceFrequency(lpFrequency : LARGE_INTEGER*) : BOOL
end
7 changes: 4 additions & 3 deletions src/lib_c/x86_64-windows-msvc/c/sysinfoapi.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "c/winnt"
require "c/win_def"
require "c/int_safe"
require "c/winbase"

lib LibC
fun GetSystemTimeAsFileTime(time : FILETIME*)
fun GetSystemTimePreciseAsFileTime(time : FILETIME*)

fun GetNativeSystemInfo(system_info : SYSTEM_INFO*)

struct PROCESSOR_INFO
Expand Down
21 changes: 21 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/timezoneapi.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "c/winnt"
require "c/winbase"

lib LibC
struct TIME_ZONE_INFORMATION
bias : LONG
standardName : StaticArray(WCHAR, 32)
standardDate : SYSTEMTIME
standardBias : LONG
daylightName : StaticArray(WCHAR, 32)
daylightDate : SYSTEMTIME
daylightBias : LONG
end

TIME_TONE_ID_INVALID = 0xffffffff_u32
TIME_ZONE_ID_UNKNOWN = 0_u32
TIME_ZONE_ID_STANDARD = 1_u32
TIME_ZONE_ID_DAYLIGHT = 2_u32

fun GetTimeZoneInformation(tz_info : TIME_ZONE_INFORMATION*) : DWORD
end
71 changes: 1 addition & 70 deletions src/lib_c/x86_64-windows-msvc/c/winbase.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require "c/winnt"
require "c/win_def"
require "c/int_safe"
require "c/minwinbase"

lib LibC
fun GetLastError : DWORD
fun SetLastError(dwErrCode : DWORD)

FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100_u32
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200_u32
FORMAT_MESSAGE_FROM_STRING = 0x00000400_u32
Expand All @@ -17,91 +15,24 @@ lib LibC
fun FormatMessageW(dwFlags : DWORD, lpSource : Void*, dwMessageId : DWORD, dwLanguageId : DWORD,
lpBuffer : LPWSTR, nSize : DWORD, arguments : Void*) : DWORD

struct FILETIME
dwLowDateTime : DWORD
dwHighDateTime : DWORD
end

struct SYSTEMTIME
wYear : WORD
wMonth : WORD
wDayOfWeek : WORD
wDay : WORD
wHour : WORD
wMinute : WORD
wSecond : WORD
wMilliseconds : WORD
end

struct TIME_ZONE_INFORMATION
bias : LONG
standardName : StaticArray(WCHAR, 32)
standardDate : SYSTEMTIME
standardBias : LONG
daylightName : StaticArray(WCHAR, 32)
daylightDate : SYSTEMTIME
daylightBias : LONG
end

TIME_ZONE_ID_UNKNOWN = 0_u32
TIME_ZONE_ID_STANDARD = 1_u32
TIME_ZONE_ID_DAYLIGHT = 2_u32

fun GetTimeZoneInformation(tz_info : TIME_ZONE_INFORMATION*) : DWORD
fun GetSystemTimeAsFileTime(time : FILETIME*)
fun GetSystemTimePreciseAsFileTime(time : FILETIME*)

fun QueryPerformanceCounter(performance_count : Int64*) : BOOL
fun QueryPerformanceFrequency(frequency : Int64*) : BOOL

fun GetCurrentDirectoryW(nBufferLength : DWORD, lpBuffer : LPWSTR) : DWORD
fun SetCurrentDirectoryW(lpPathname : LPWSTR) : BOOL

SYMBOLIC_LINK_FLAG_DIRECTORY = 0x1
SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE = 0x2

fun CreateHardLinkW(lpFileName : LPWSTR, lpExistingFileName : LPWSTR, lpSecurityAttributes : Void*) : BOOL
fun CreateSymbolicLinkW(lpSymlinkFileName : LPWSTR, lpTargetFileName : LPWSTR, dwFlags : DWORD) : BOOLEAN

struct WIN32_FILE_ATTRIBUTE_DATA
dwFileAttributes : DWORD
ftCreationTime : FILETIME
ftLastAccessTime : FILETIME
ftLastWriteTime : FILETIME
nFileSizeHigh : DWORD
nFileSizeLow : DWORD
end

enum GET_FILEEX_INFO_LEVELS
GetFileExInfoStandard
GetFileExMaxInfoLevel
end

struct SECURITY_ATTRIBUTES
nLength : DWORD
lpSecurityDescriptor : Void*
bInheritHandle : BOOL
end

INVALID_HANDLE_VALUE = HANDLE.new(-1)

fun CloseHandle(hObject : HANDLE) : BOOL

fun GetEnvironmentVariableW(lpName : LPWSTR, lpBuffer : LPWSTR, nSize : DWORD) : DWORD
fun GetEnvironmentStringsW : LPWCH
fun FreeEnvironmentStringsW(lpszEnvironmentBlock : LPWCH) : BOOL
fun SetEnvironmentVariableW(lpName : LPWSTR, lpValue : LPWSTR) : BOOL

INFINITE = 0xFFFFFFFF

STILL_ACTIVE = 0x103

STARTF_USESTDHANDLES = 0x00000100

fun DuplicateHandle(hSourceProcessHandle : HANDLE, hSourceHandle : HANDLE,
hTargetProcessHandle : HANDLE, lpTargetHandle : HANDLE*,
dwDesiredAccess : DWORD, bInheritHandle : BOOL, dwOptions : DWORD) : BOOL

MOVEFILE_REPLACE_EXISTING = 0x1_u32
MOVEFILE_COPY_ALLOWED = 0x2_u32
MOVEFILE_DELAY_UNTIL_REBOOT = 0x4_u32
Expand Down
3 changes: 3 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/windows.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "c/winnt"

require "c/profileapi"
3 changes: 3 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/winnt.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require "c/int_safe"

lib LibC
alias BOOLEAN = BYTE
alias LONG = Int32
alias LARGE_INTEGER = Int64

alias CHAR = UChar
alias WCHAR = UInt16
Expand Down
9 changes: 9 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/wtypesbase.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "c/winnt"

lib LibC
struct SECURITY_ATTRIBUTES
nLength : DWORD
lpSecurityDescriptor : Void*
bInheritHandle : BOOL
end
end
1 change: 1 addition & 0 deletions src/winerror.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% if flag?(:win32) %}
require "c/winbase"
require "c/errhandlingapi"
{% end %}

# `WinError` represents Windows' [System Error Codes](https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes#system-error-codes-1).
Expand Down

0 comments on commit 93a65e3

Please sign in to comment.