]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/winapi.c
moved g_platform_id to winapi
[bacula/bacula] / bacula / src / lib / winapi.c
1 /*
2  * Windows APIs that are different for each system.
3  *   We use pointers to the entry points so that a
4  *   single binary will run on all Windows systems.
5  *
6  *     Kern Sibbald MMIII
7  */
8 /*
9    Copyright (C) 2000-2005 Kern Sibbald
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29
30 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
31
32 #include "winapi.h"
33
34 // init with win9x, but maybe set to NT in InitWinAPI
35 DWORD  g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
36
37
38 /* API Pointers */
39
40 t_OpenProcessToken      p_OpenProcessToken = NULL;
41 t_AdjustTokenPrivileges p_AdjustTokenPrivileges = NULL;
42 t_LookupPrivilegeValue  p_LookupPrivilegeValue = NULL;
43
44 t_SetProcessShutdownParameters p_SetProcessShutdownParameters = NULL;
45
46 t_CreateFileA   p_CreateFileA = NULL;
47 t_CreateFileW   p_CreateFileW = NULL;
48
49 t_wunlink p_wunlink = NULL;
50 t_wmkdir p_wmkdir = NULL;
51 t_wopen p_wopen = NULL;
52 t_GetFileAttributesA    p_GetFileAttributesA = NULL;
53 t_GetFileAttributesW    p_GetFileAttributesW = NULL;
54
55 t_GetFileAttributesExA  p_GetFileAttributesExA = NULL;
56 t_GetFileAttributesExW  p_GetFileAttributesExW = NULL;
57
58 t_SetFileAttributesA    p_SetFileAttributesA = NULL;
59 t_SetFileAttributesW    p_SetFileAttributesW = NULL;
60 t_BackupRead            p_BackupRead = NULL;
61 t_BackupWrite           p_BackupWrite = NULL;
62 t_WideCharToMultiByte p_WideCharToMultiByte = NULL;
63 t_MultiByteToWideChar p_MultiByteToWideChar = NULL;
64
65 t_FindFirstFileA p_FindFirstFileA = NULL;
66 t_FindFirstFileW p_FindFirstFileW = NULL;
67
68 t_FindNextFileA p_FindNextFileA = NULL;
69 t_FindNextFileW p_FindNextFileW = NULL;
70
71 t_SetCurrentDirectoryA p_SetCurrentDirectoryA = NULL;
72 t_SetCurrentDirectoryW p_SetCurrentDirectoryW = NULL;
73
74 t_GetCurrentDirectoryA p_GetCurrentDirectoryA = NULL;
75 t_GetCurrentDirectoryW p_GetCurrentDirectoryW = NULL;
76
77
78 void 
79 InitWinAPIWrapper() 
80 {
81    HMODULE hLib = LoadLibraryA("KERNEL32.DLL");
82    if (hLib) {
83       /* create file calls */
84       p_CreateFileA = (t_CreateFileA)
85           GetProcAddress(hLib, "CreateFileA");
86       p_CreateFileW = (t_CreateFileW)
87           GetProcAddress(hLib, "CreateFileW");      
88
89       /* attribute calls */
90       p_GetFileAttributesA = (t_GetFileAttributesA)
91           GetProcAddress(hLib, "GetFileAttributesA");
92       p_GetFileAttributesW = (t_GetFileAttributesW)
93           GetProcAddress(hLib, "GetFileAttributesW");
94       p_GetFileAttributesExA = (t_GetFileAttributesExA)
95           GetProcAddress(hLib, "GetFileAttributesExA");
96       p_GetFileAttributesExW = (t_GetFileAttributesExW)
97           GetProcAddress(hLib, "GetFileAttributesExW");
98       p_SetFileAttributesA = (t_SetFileAttributesA)
99           GetProcAddress(hLib, "SetFileAttributesA");
100       p_SetFileAttributesW = (t_SetFileAttributesW)
101           GetProcAddress(hLib, "SetFileAttributesW");
102       /* process calls */
103       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
104           GetProcAddress(hLib, "SetProcessShutdownParameters");
105       /* backup calls */
106       p_BackupRead = (t_BackupRead)
107           GetProcAddress(hLib, "BackupRead");
108       p_BackupWrite = (t_BackupWrite)
109           GetProcAddress(hLib, "BackupWrite");
110       /* char conversion calls */
111       p_WideCharToMultiByte = (t_WideCharToMultiByte)
112           GetProcAddress(hLib, "WideCharToMultiByte");
113       p_MultiByteToWideChar = (t_MultiByteToWideChar)
114           GetProcAddress(hLib, "MultiByteToWideChar");
115
116       /* find files */
117       p_FindFirstFileA = (t_FindFirstFileA)
118           GetProcAddress(hLib, "FindFirstFileA"); 
119       p_FindFirstFileW = (t_FindFirstFileW)
120           GetProcAddress(hLib, "FindFirstFileW");       
121       p_FindNextFileA = (t_FindNextFileA)
122           GetProcAddress(hLib, "FindNextFileA");
123       p_FindNextFileW = (t_FindNextFileW)
124           GetProcAddress(hLib, "FindNextFileW");
125       /* set and get directory */
126       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
127           GetProcAddress(hLib, "SetCurrentDirectoryA");
128       p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
129           GetProcAddress(hLib, "SetCurrentDirectoryW");       
130       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
131           GetProcAddress(hLib, "GetCurrentDirectoryA");
132       p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
133           GetProcAddress(hLib, "GetCurrentDirectoryW");      
134       FreeLibrary(hLib);
135    }
136    
137    hLib = LoadLibraryA("MSVCRT.DLL");
138    if (hLib) {
139       /* unlink */
140       p_wunlink = (t_wunlink)
141       GetProcAddress(hLib, "_wunlink");
142       /* wmkdir */
143       p_wmkdir = (t_wmkdir)
144       GetProcAddress(hLib, "_wmkdir");
145       /* wopen */
146       p_wopen = (t_wopen)
147       GetProcAddress(hLib, "_wopen");
148
149       FreeLibrary(hLib);
150    }
151    
152    hLib = LoadLibraryA("ADVAPI32.DLL");
153    if (hLib) {
154       p_OpenProcessToken = (t_OpenProcessToken)
155          GetProcAddress(hLib, "OpenProcessToken");
156       p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
157          GetProcAddress(hLib, "AdjustTokenPrivileges");
158       p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
159          GetProcAddress(hLib, "LookupPrivilegeValueA");
160       FreeLibrary(hLib);
161    }
162
163    // do we run on win 9x ???
164    OSVERSIONINFO osversioninfo;
165    osversioninfo.dwOSVersionInfoSize = sizeof(osversioninfo);
166
167    // Get the current OS version
168    if (!GetVersionEx(&osversioninfo)) {
169       g_platform_id = 0;
170    } else {
171       g_platform_id = osversioninfo.dwPlatformId;
172    }
173
174    if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS) {
175       p_BackupRead = NULL;
176       p_BackupWrite = NULL;
177
178       p_CreateFileW = NULL;          
179       p_GetFileAttributesW = NULL;          
180       p_GetFileAttributesExW = NULL;
181           
182       p_SetFileAttributesW = NULL;
183                 
184       p_FindFirstFileW = NULL;
185       p_FindNextFileW = NULL;
186       p_SetCurrentDirectoryW = NULL;
187       p_GetCurrentDirectoryW = NULL;
188
189       p_wunlink = NULL;
190       p_wmkdir = NULL;
191       p_wopen = NULL;
192    }   
193 }
194 #endif