Lely core libraries  2.2.5
io.c
Go to the documentation of this file.
1 
24 #include "io.h"
25 
26 #if _WIN32
27 
28 #include <lely/io2/sys/io.h>
29 
30 #include <assert.h>
31 
32 #include <windows.h>
33 
34 LPFN_RTLNTSTATUSTODOSERROR lpfnRtlNtStatusToDosError;
35 
36 static size_t io_init_refcnt = 0;
37 
38 int
39 io_init(void)
40 {
41  if (io_init_refcnt++)
42  return 0;
43 
44  DWORD dwErrCode = 0;
45 
46  if (io_win32_ntdll_init() == -1) {
47  dwErrCode = GetLastError();
48  goto error_ntdll_init;
49  }
50 
51  if (io_win32_sigset_init() == -1) {
52  dwErrCode = GetLastError();
53  goto error_sigset_init;
54  }
55 
56  return 0;
57 
58  // io_win32_sigset_fini();
59 error_sigset_init:
60  io_win32_ntdll_fini();
61 error_ntdll_init:
62  SetLastError(dwErrCode);
63  io_init_refcnt--;
64  return -1;
65 }
66 
67 void
68 io_fini(void)
69 {
70  assert(io_init_refcnt);
71 
72  if (--io_init_refcnt)
73  return;
74 
75  io_win32_sigset_fini();
76  io_win32_ntdll_fini();
77 }
78 
79 int
80 io_win32_ntdll_init(void)
81 {
82  HMODULE hLibModule = GetModuleHandleA("ntdll.dll");
83  if (!hLibModule)
84  goto error_ntdll;
85 
86 #if __GNUC__ >= 8
87 #pragma GCC diagnostic push
88 #pragma GCC diagnostic ignored "-Wcast-function-type"
89 #endif
90  lpfnRtlNtStatusToDosError = (LPFN_RTLNTSTATUSTODOSERROR)GetProcAddress(
91  hLibModule, "RtlNtStatusToDosError");
92  if (!lpfnRtlNtStatusToDosError)
93  goto error_RtlNtStatusToDosError;
94 #if __GNUC__ >= 8
95 #pragma GCC diagnostic pop
96 #endif
97 
98  return 0;
99 
100  // lpfnRtlNtStatusToDosError = NULL;
101 error_RtlNtStatusToDosError:
102 error_ntdll:
103  return -1;
104 }
105 
106 void
107 io_win32_ntdll_fini(void)
108 {
109  lpfnRtlNtStatusToDosError = NULL;
110 }
111 
112 #endif // _WIN32
This header file is part of the I/O library; it contains system-dependent I/O initialization/finaliza...
void io_fini(void)
Finalizes the I/O library and terminates the availability of the I/O functions.
Definition: io.c:38
int io_init(void)
Initializes the I/O library and makes the I/O functions available for use.
Definition: io.c:31
This is the internal header file of the Windows-specific I/O declarations.