Commit | Line | Data |
---|---|---|
e9e23e23 | 1 | /* Utility and Unix shadow routines for GNU Emacs on the Microsoft W32 API. |
0b5538bd | 2 | Copyright (C) 1994, 1995, 2000, 2001, 2002, 2003, 2004, |
8cabe764 | 3 | 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
95ed0025 | 4 | |
3b7ad313 EN |
5 | This file is part of GNU Emacs. |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
3b7ad313 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
3b7ad313 EN |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
95ed0025 | 19 | |
9ec0b715 | 20 | /* |
95ed0025 RS |
21 | Geoff Voelker (voelker@cs.washington.edu) 7-29-94 |
22 | */ | |
76b3903d | 23 | #include <stddef.h> /* for offsetof */ |
95ed0025 RS |
24 | #include <stdlib.h> |
25 | #include <stdio.h> | |
26 | #include <io.h> | |
480b0c5b | 27 | #include <errno.h> |
95ed0025 RS |
28 | #include <fcntl.h> |
29 | #include <ctype.h> | |
480b0c5b | 30 | #include <signal.h> |
b3308d2e | 31 | #include <sys/file.h> |
480b0c5b | 32 | #include <sys/time.h> |
16bb7578 | 33 | #include <sys/utime.h> |
22189f79 | 34 | #include <mbstring.h> /* for _mbspbrk */ |
480b0c5b GV |
35 | |
36 | /* must include CRT headers *before* config.h */ | |
4838e624 PJ |
37 | |
38 | #ifdef HAVE_CONFIG_H | |
39 | #include <config.h> | |
40 | #endif | |
41 | ||
480b0c5b GV |
42 | #undef access |
43 | #undef chdir | |
44 | #undef chmod | |
45 | #undef creat | |
46 | #undef ctime | |
47 | #undef fopen | |
48 | #undef link | |
49 | #undef mkdir | |
50 | #undef mktemp | |
51 | #undef open | |
52 | #undef rename | |
53 | #undef rmdir | |
54 | #undef unlink | |
55 | ||
56 | #undef close | |
57 | #undef dup | |
58 | #undef dup2 | |
59 | #undef pipe | |
60 | #undef read | |
61 | #undef write | |
95ed0025 | 62 | |
d8fcc1b9 AI |
63 | #undef strerror |
64 | ||
95ed0025 | 65 | #include "lisp.h" |
95ed0025 RS |
66 | |
67 | #include <pwd.h> | |
3d19b645 | 68 | #include <grp.h> |
95ed0025 | 69 | |
971bce75 AI |
70 | #ifdef __GNUC__ |
71 | #define _ANONYMOUS_UNION | |
72 | #define _ANONYMOUS_STRUCT | |
73 | #endif | |
480b0c5b | 74 | #include <windows.h> |
634d3003 | 75 | #include <lmcons.h> |
2d5324c5 | 76 | #include <shlobj.h> |
00b3b7b3 | 77 | |
480b0c5b GV |
78 | #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */ |
79 | #include <sys/socket.h> | |
80 | #undef socket | |
81 | #undef bind | |
82 | #undef connect | |
83 | #undef htons | |
84 | #undef ntohs | |
85 | #undef inet_addr | |
86 | #undef gethostname | |
87 | #undef gethostbyname | |
88 | #undef getservbyname | |
ecd270eb | 89 | #undef getpeername |
380961a6 | 90 | #undef shutdown |
962955c5 JR |
91 | #undef setsockopt |
92 | #undef listen | |
93 | #undef getsockname | |
94 | #undef accept | |
95 | #undef recvfrom | |
96 | #undef sendto | |
480b0c5b | 97 | #endif |
00b3b7b3 | 98 | |
489f9371 | 99 | #include "w32.h" |
480b0c5b | 100 | #include "ndir.h" |
489f9371 | 101 | #include "w32heap.h" |
253574a6 JR |
102 | #include "systime.h" |
103 | ||
2d5324c5 JR |
104 | typedef HRESULT (WINAPI * ShGetFolderPath_fn) |
105 | (IN HWND, IN int, IN HANDLE, IN DWORD, OUT char *); | |
106 | ||
9785d95b | 107 | void globals_of_w32 (); |
8aaaec6b | 108 | static DWORD get_rid (PSID); |
9785d95b | 109 | |
76b3903d GV |
110 | extern Lisp_Object Vw32_downcase_file_names; |
111 | extern Lisp_Object Vw32_generate_fake_inodes; | |
112 | extern Lisp_Object Vw32_get_true_file_attributes; | |
ac0af113 JR |
113 | /* Defined in process.c for its own purpose. */ |
114 | extern Lisp_Object Qlocal; | |
115 | ||
e0c181dd | 116 | extern int w32_num_mouse_buttons; |
76b3903d | 117 | |
18e070ac | 118 | \f |
9d95a291 EZ |
119 | /* Initialization states. |
120 | ||
121 | WARNING: If you add any more such variables for additional APIs, | |
122 | you MUST add initialization for them to globals_of_w32 | |
123 | below. This is because these variables might get set | |
124 | to non-NULL values during dumping, but the dumped Emacs | |
125 | cannot reuse those values, because it could be run on a | |
126 | different version of the OS, where API addresses are | |
127 | different. */ | |
9785d95b BK |
128 | static BOOL g_b_init_is_windows_9x; |
129 | static BOOL g_b_init_open_process_token; | |
130 | static BOOL g_b_init_get_token_information; | |
131 | static BOOL g_b_init_lookup_account_sid; | |
132 | static BOOL g_b_init_get_sid_identifier_authority; | |
c617afce EZ |
133 | static BOOL g_b_init_get_sid_sub_authority; |
134 | static BOOL g_b_init_get_sid_sub_authority_count; | |
8aaaec6b EZ |
135 | static BOOL g_b_init_get_file_security; |
136 | static BOOL g_b_init_get_security_descriptor_owner; | |
137 | static BOOL g_b_init_get_security_descriptor_group; | |
138 | static BOOL g_b_init_is_valid_sid; | |
9785d95b | 139 | |
f60ae425 BK |
140 | /* |
141 | BEGIN: Wrapper functions around OpenProcessToken | |
142 | and other functions in advapi32.dll that are only | |
143 | supported in Windows NT / 2k / XP | |
144 | */ | |
145 | /* ** Function pointer typedefs ** */ | |
146 | typedef BOOL (WINAPI * OpenProcessToken_Proc) ( | |
147 | HANDLE ProcessHandle, | |
148 | DWORD DesiredAccess, | |
149 | PHANDLE TokenHandle); | |
150 | typedef BOOL (WINAPI * GetTokenInformation_Proc) ( | |
151 | HANDLE TokenHandle, | |
152 | TOKEN_INFORMATION_CLASS TokenInformationClass, | |
153 | LPVOID TokenInformation, | |
154 | DWORD TokenInformationLength, | |
155 | PDWORD ReturnLength); | |
74258518 JR |
156 | typedef BOOL (WINAPI * GetProcessTimes_Proc) ( |
157 | HANDLE process_handle, | |
158 | LPFILETIME creation_time, | |
159 | LPFILETIME exit_time, | |
160 | LPFILETIME kernel_time, | |
161 | LPFILETIME user_time); | |
162 | ||
163 | GetProcessTimes_Proc get_process_times_fn = NULL; | |
164 | ||
f60ae425 BK |
165 | #ifdef _UNICODE |
166 | const char * const LookupAccountSid_Name = "LookupAccountSidW"; | |
8aaaec6b | 167 | const char * const GetFileSecurity_Name = "GetFileSecurityW"; |
f60ae425 BK |
168 | #else |
169 | const char * const LookupAccountSid_Name = "LookupAccountSidA"; | |
8aaaec6b | 170 | const char * const GetFileSecurity_Name = "GetFileSecurityA"; |
f60ae425 BK |
171 | #endif |
172 | typedef BOOL (WINAPI * LookupAccountSid_Proc) ( | |
173 | LPCTSTR lpSystemName, | |
174 | PSID Sid, | |
175 | LPTSTR Name, | |
176 | LPDWORD cbName, | |
177 | LPTSTR DomainName, | |
178 | LPDWORD cbDomainName, | |
179 | PSID_NAME_USE peUse); | |
180 | typedef PSID_IDENTIFIER_AUTHORITY (WINAPI * GetSidIdentifierAuthority_Proc) ( | |
181 | PSID pSid); | |
c617afce EZ |
182 | typedef PDWORD (WINAPI * GetSidSubAuthority_Proc) ( |
183 | PSID pSid, | |
184 | DWORD n); | |
185 | typedef PUCHAR (WINAPI * GetSidSubAuthorityCount_Proc) ( | |
186 | PSID pSid); | |
8aaaec6b EZ |
187 | typedef BOOL (WINAPI * GetFileSecurity_Proc) ( |
188 | LPCTSTR lpFileName, | |
189 | SECURITY_INFORMATION RequestedInformation, | |
190 | PSECURITY_DESCRIPTOR pSecurityDescriptor, | |
191 | DWORD nLength, | |
192 | LPDWORD lpnLengthNeeded); | |
193 | typedef BOOL (WINAPI * GetSecurityDescriptorOwner_Proc) ( | |
194 | PSECURITY_DESCRIPTOR pSecurityDescriptor, | |
195 | PSID *pOwner, | |
196 | LPBOOL lpbOwnerDefaulted); | |
197 | typedef BOOL (WINAPI * GetSecurityDescriptorGroup_Proc) ( | |
198 | PSECURITY_DESCRIPTOR pSecurityDescriptor, | |
199 | PSID *pGroup, | |
200 | LPBOOL lpbGroupDefaulted); | |
201 | typedef BOOL (WINAPI * IsValidSid_Proc) ( | |
202 | PSID sid); | |
f60ae425 BK |
203 | |
204 | /* ** A utility function ** */ | |
9bfb11f9 KS |
205 | static BOOL |
206 | is_windows_9x () | |
f60ae425 | 207 | { |
9785d95b | 208 | static BOOL s_b_ret=0; |
f60ae425 | 209 | OSVERSIONINFO os_ver; |
9785d95b | 210 | if (g_b_init_is_windows_9x == 0) |
f60ae425 | 211 | { |
9785d95b BK |
212 | g_b_init_is_windows_9x = 1; |
213 | ZeroMemory(&os_ver, sizeof(OSVERSIONINFO)); | |
214 | os_ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
215 | if (GetVersionEx (&os_ver)) | |
216 | { | |
217 | s_b_ret = (os_ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS); | |
218 | } | |
f60ae425 | 219 | } |
9785d95b | 220 | return s_b_ret; |
f60ae425 BK |
221 | } |
222 | ||
74258518 JR |
223 | /* Get total user and system times for get-internal-run-time. |
224 | Returns a list of three integers if the times are provided by the OS | |
225 | (NT derivatives), otherwise it returns the result of current-time. */ | |
226 | Lisp_Object | |
227 | w32_get_internal_run_time () | |
228 | { | |
229 | if (get_process_times_fn) | |
230 | { | |
231 | FILETIME create, exit, kernel, user; | |
232 | HANDLE proc = GetCurrentProcess(); | |
233 | if ((*get_process_times_fn) (proc, &create, &exit, &kernel, &user)) | |
234 | { | |
235 | LARGE_INTEGER user_int, kernel_int, total; | |
236 | int microseconds; | |
237 | user_int.LowPart = user.dwLowDateTime; | |
238 | user_int.HighPart = user.dwHighDateTime; | |
239 | kernel_int.LowPart = kernel.dwLowDateTime; | |
240 | kernel_int.HighPart = kernel.dwHighDateTime; | |
241 | total.QuadPart = user_int.QuadPart + kernel_int.QuadPart; | |
242 | /* FILETIME is 100 nanosecond increments, Emacs only wants | |
243 | microsecond resolution. */ | |
244 | total.QuadPart /= 10; | |
245 | microseconds = total.QuadPart % 1000000; | |
246 | total.QuadPart /= 1000000; | |
247 | ||
248 | /* Sanity check to make sure we can represent the result. */ | |
249 | if (total.HighPart == 0) | |
250 | { | |
251 | int secs = total.LowPart; | |
252 | ||
253 | return list3 (make_number ((secs >> 16) & 0xffff), | |
254 | make_number (secs & 0xffff), | |
255 | make_number (microseconds)); | |
256 | } | |
257 | } | |
258 | } | |
259 | ||
260 | return Fcurrent_time (); | |
261 | } | |
262 | ||
f60ae425 BK |
263 | /* ** The wrapper functions ** */ |
264 | ||
265 | BOOL WINAPI open_process_token ( | |
266 | HANDLE ProcessHandle, | |
267 | DWORD DesiredAccess, | |
268 | PHANDLE TokenHandle) | |
269 | { | |
9785d95b | 270 | static OpenProcessToken_Proc s_pfn_Open_Process_Token = NULL; |
f60ae425 BK |
271 | HMODULE hm_advapi32 = NULL; |
272 | if (is_windows_9x () == TRUE) | |
273 | { | |
274 | return FALSE; | |
275 | } | |
9785d95b BK |
276 | if (g_b_init_open_process_token == 0) |
277 | { | |
278 | g_b_init_open_process_token = 1; | |
279 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
280 | s_pfn_Open_Process_Token = | |
281 | (OpenProcessToken_Proc) GetProcAddress (hm_advapi32, "OpenProcessToken"); | |
282 | } | |
283 | if (s_pfn_Open_Process_Token == NULL) | |
f60ae425 BK |
284 | { |
285 | return FALSE; | |
286 | } | |
287 | return ( | |
9785d95b | 288 | s_pfn_Open_Process_Token ( |
f60ae425 BK |
289 | ProcessHandle, |
290 | DesiredAccess, | |
291 | TokenHandle) | |
292 | ); | |
293 | } | |
294 | ||
295 | BOOL WINAPI get_token_information ( | |
296 | HANDLE TokenHandle, | |
297 | TOKEN_INFORMATION_CLASS TokenInformationClass, | |
298 | LPVOID TokenInformation, | |
299 | DWORD TokenInformationLength, | |
300 | PDWORD ReturnLength) | |
301 | { | |
9785d95b | 302 | static GetTokenInformation_Proc s_pfn_Get_Token_Information = NULL; |
f60ae425 BK |
303 | HMODULE hm_advapi32 = NULL; |
304 | if (is_windows_9x () == TRUE) | |
305 | { | |
306 | return FALSE; | |
307 | } | |
9785d95b BK |
308 | if (g_b_init_get_token_information == 0) |
309 | { | |
310 | g_b_init_get_token_information = 1; | |
311 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
312 | s_pfn_Get_Token_Information = | |
313 | (GetTokenInformation_Proc) GetProcAddress (hm_advapi32, "GetTokenInformation"); | |
314 | } | |
315 | if (s_pfn_Get_Token_Information == NULL) | |
f60ae425 BK |
316 | { |
317 | return FALSE; | |
318 | } | |
319 | return ( | |
9785d95b | 320 | s_pfn_Get_Token_Information ( |
f60ae425 BK |
321 | TokenHandle, |
322 | TokenInformationClass, | |
323 | TokenInformation, | |
324 | TokenInformationLength, | |
325 | ReturnLength) | |
326 | ); | |
327 | } | |
328 | ||
329 | BOOL WINAPI lookup_account_sid ( | |
330 | LPCTSTR lpSystemName, | |
331 | PSID Sid, | |
332 | LPTSTR Name, | |
333 | LPDWORD cbName, | |
334 | LPTSTR DomainName, | |
335 | LPDWORD cbDomainName, | |
336 | PSID_NAME_USE peUse) | |
337 | { | |
9785d95b | 338 | static LookupAccountSid_Proc s_pfn_Lookup_Account_Sid = NULL; |
f60ae425 BK |
339 | HMODULE hm_advapi32 = NULL; |
340 | if (is_windows_9x () == TRUE) | |
341 | { | |
342 | return FALSE; | |
343 | } | |
9785d95b BK |
344 | if (g_b_init_lookup_account_sid == 0) |
345 | { | |
346 | g_b_init_lookup_account_sid = 1; | |
347 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
348 | s_pfn_Lookup_Account_Sid = | |
349 | (LookupAccountSid_Proc) GetProcAddress (hm_advapi32, LookupAccountSid_Name); | |
350 | } | |
351 | if (s_pfn_Lookup_Account_Sid == NULL) | |
f60ae425 BK |
352 | { |
353 | return FALSE; | |
354 | } | |
355 | return ( | |
9785d95b | 356 | s_pfn_Lookup_Account_Sid ( |
f60ae425 BK |
357 | lpSystemName, |
358 | Sid, | |
359 | Name, | |
360 | cbName, | |
361 | DomainName, | |
362 | cbDomainName, | |
363 | peUse) | |
364 | ); | |
365 | } | |
366 | ||
367 | PSID_IDENTIFIER_AUTHORITY WINAPI get_sid_identifier_authority ( | |
368 | PSID pSid) | |
369 | { | |
9785d95b | 370 | static GetSidIdentifierAuthority_Proc s_pfn_Get_Sid_Identifier_Authority = NULL; |
f60ae425 BK |
371 | HMODULE hm_advapi32 = NULL; |
372 | if (is_windows_9x () == TRUE) | |
373 | { | |
374 | return NULL; | |
375 | } | |
9785d95b BK |
376 | if (g_b_init_get_sid_identifier_authority == 0) |
377 | { | |
378 | g_b_init_get_sid_identifier_authority = 1; | |
379 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
380 | s_pfn_Get_Sid_Identifier_Authority = | |
381 | (GetSidIdentifierAuthority_Proc) GetProcAddress ( | |
382 | hm_advapi32, "GetSidIdentifierAuthority"); | |
383 | } | |
384 | if (s_pfn_Get_Sid_Identifier_Authority == NULL) | |
f60ae425 BK |
385 | { |
386 | return NULL; | |
387 | } | |
9785d95b | 388 | return (s_pfn_Get_Sid_Identifier_Authority (pSid)); |
f60ae425 BK |
389 | } |
390 | ||
c617afce EZ |
391 | PDWORD WINAPI get_sid_sub_authority ( |
392 | PSID pSid, | |
393 | DWORD n) | |
394 | { | |
395 | static GetSidSubAuthority_Proc s_pfn_Get_Sid_Sub_Authority = NULL; | |
6811b9f4 | 396 | static DWORD zero = 0U; |
c617afce EZ |
397 | HMODULE hm_advapi32 = NULL; |
398 | if (is_windows_9x () == TRUE) | |
399 | { | |
6811b9f4 | 400 | return &zero; |
c617afce EZ |
401 | } |
402 | if (g_b_init_get_sid_sub_authority == 0) | |
403 | { | |
404 | g_b_init_get_sid_sub_authority = 1; | |
405 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
406 | s_pfn_Get_Sid_Sub_Authority = | |
407 | (GetSidSubAuthority_Proc) GetProcAddress ( | |
408 | hm_advapi32, "GetSidSubAuthority"); | |
409 | } | |
410 | if (s_pfn_Get_Sid_Sub_Authority == NULL) | |
411 | { | |
6811b9f4 | 412 | return &zero; |
c617afce EZ |
413 | } |
414 | return (s_pfn_Get_Sid_Sub_Authority (pSid, n)); | |
415 | } | |
416 | ||
417 | PUCHAR WINAPI get_sid_sub_authority_count ( | |
418 | PSID pSid) | |
419 | { | |
420 | static GetSidSubAuthorityCount_Proc s_pfn_Get_Sid_Sub_Authority_Count = NULL; | |
6811b9f4 | 421 | static UCHAR zero = 0U; |
c617afce EZ |
422 | HMODULE hm_advapi32 = NULL; |
423 | if (is_windows_9x () == TRUE) | |
424 | { | |
6811b9f4 | 425 | return &zero; |
c617afce EZ |
426 | } |
427 | if (g_b_init_get_sid_sub_authority_count == 0) | |
428 | { | |
429 | g_b_init_get_sid_sub_authority_count = 1; | |
430 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
431 | s_pfn_Get_Sid_Sub_Authority_Count = | |
432 | (GetSidSubAuthorityCount_Proc) GetProcAddress ( | |
433 | hm_advapi32, "GetSidSubAuthorityCount"); | |
434 | } | |
435 | if (s_pfn_Get_Sid_Sub_Authority_Count == NULL) | |
436 | { | |
6811b9f4 | 437 | return &zero; |
c617afce EZ |
438 | } |
439 | return (s_pfn_Get_Sid_Sub_Authority_Count (pSid)); | |
440 | } | |
441 | ||
8aaaec6b EZ |
442 | BOOL WINAPI get_file_security ( |
443 | LPCTSTR lpFileName, | |
444 | SECURITY_INFORMATION RequestedInformation, | |
445 | PSECURITY_DESCRIPTOR pSecurityDescriptor, | |
446 | DWORD nLength, | |
447 | LPDWORD lpnLengthNeeded) | |
448 | { | |
449 | static GetFileSecurity_Proc s_pfn_Get_File_Security = NULL; | |
450 | HMODULE hm_advapi32 = NULL; | |
451 | if (is_windows_9x () == TRUE) | |
452 | { | |
453 | return FALSE; | |
454 | } | |
455 | if (g_b_init_get_file_security == 0) | |
456 | { | |
457 | g_b_init_get_file_security = 1; | |
458 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
459 | s_pfn_Get_File_Security = | |
460 | (GetFileSecurity_Proc) GetProcAddress ( | |
461 | hm_advapi32, GetFileSecurity_Name); | |
462 | } | |
463 | if (s_pfn_Get_File_Security == NULL) | |
464 | { | |
465 | return FALSE; | |
466 | } | |
467 | return (s_pfn_Get_File_Security (lpFileName, RequestedInformation, | |
468 | pSecurityDescriptor, nLength, | |
469 | lpnLengthNeeded)); | |
470 | } | |
471 | ||
472 | BOOL WINAPI get_security_descriptor_owner ( | |
473 | PSECURITY_DESCRIPTOR pSecurityDescriptor, | |
474 | PSID *pOwner, | |
475 | LPBOOL lpbOwnerDefaulted) | |
476 | { | |
477 | static GetSecurityDescriptorOwner_Proc s_pfn_Get_Security_Descriptor_Owner = NULL; | |
478 | HMODULE hm_advapi32 = NULL; | |
479 | if (is_windows_9x () == TRUE) | |
480 | { | |
481 | return FALSE; | |
482 | } | |
483 | if (g_b_init_get_security_descriptor_owner == 0) | |
484 | { | |
485 | g_b_init_get_security_descriptor_owner = 1; | |
486 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
487 | s_pfn_Get_Security_Descriptor_Owner = | |
488 | (GetSecurityDescriptorOwner_Proc) GetProcAddress ( | |
489 | hm_advapi32, "GetSecurityDescriptorOwner"); | |
490 | } | |
491 | if (s_pfn_Get_Security_Descriptor_Owner == NULL) | |
492 | { | |
493 | return FALSE; | |
494 | } | |
495 | return (s_pfn_Get_Security_Descriptor_Owner (pSecurityDescriptor, pOwner, | |
496 | lpbOwnerDefaulted)); | |
497 | } | |
498 | ||
499 | BOOL WINAPI get_security_descriptor_group ( | |
500 | PSECURITY_DESCRIPTOR pSecurityDescriptor, | |
501 | PSID *pGroup, | |
502 | LPBOOL lpbGroupDefaulted) | |
503 | { | |
504 | static GetSecurityDescriptorGroup_Proc s_pfn_Get_Security_Descriptor_Group = NULL; | |
505 | HMODULE hm_advapi32 = NULL; | |
506 | if (is_windows_9x () == TRUE) | |
507 | { | |
508 | return FALSE; | |
509 | } | |
510 | if (g_b_init_get_security_descriptor_group == 0) | |
511 | { | |
512 | g_b_init_get_security_descriptor_group = 1; | |
513 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
514 | s_pfn_Get_Security_Descriptor_Group = | |
515 | (GetSecurityDescriptorGroup_Proc) GetProcAddress ( | |
516 | hm_advapi32, "GetSecurityDescriptorGroup"); | |
517 | } | |
518 | if (s_pfn_Get_Security_Descriptor_Group == NULL) | |
519 | { | |
520 | return FALSE; | |
521 | } | |
522 | return (s_pfn_Get_Security_Descriptor_Group (pSecurityDescriptor, pGroup, | |
523 | lpbGroupDefaulted)); | |
524 | } | |
525 | ||
526 | BOOL WINAPI is_valid_sid ( | |
527 | PSID sid) | |
528 | { | |
529 | static IsValidSid_Proc s_pfn_Is_Valid_Sid = NULL; | |
530 | HMODULE hm_advapi32 = NULL; | |
531 | if (is_windows_9x () == TRUE) | |
532 | { | |
533 | return FALSE; | |
534 | } | |
535 | if (g_b_init_is_valid_sid == 0) | |
536 | { | |
537 | g_b_init_is_valid_sid = 1; | |
538 | hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
539 | s_pfn_Is_Valid_Sid = | |
540 | (IsValidSid_Proc) GetProcAddress ( | |
541 | hm_advapi32, "IsValidSid"); | |
542 | } | |
543 | if (s_pfn_Is_Valid_Sid == NULL) | |
544 | { | |
545 | return FALSE; | |
546 | } | |
547 | return (s_pfn_Is_Valid_Sid (sid)); | |
548 | } | |
549 | ||
f60ae425 BK |
550 | /* |
551 | END: Wrapper functions around OpenProcessToken | |
552 | and other functions in advapi32.dll that are only | |
553 | supported in Windows NT / 2k / XP | |
554 | */ | |
555 | ||
556 | \f | |
18e070ac AI |
557 | /* Equivalent of strerror for W32 error codes. */ |
558 | char * | |
559 | w32_strerror (int error_no) | |
560 | { | |
561 | static char buf[500]; | |
562 | ||
563 | if (error_no == 0) | |
564 | error_no = GetLastError (); | |
565 | ||
566 | buf[0] = '\0'; | |
567 | if (!FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, | |
568 | error_no, | |
569 | 0, /* choose most suitable language */ | |
570 | buf, sizeof (buf), NULL)) | |
571 | sprintf (buf, "w32 error %u", error_no); | |
572 | return buf; | |
573 | } | |
574 | ||
ed91b2ad EZ |
575 | /* Return 1 if P is a valid pointer to an object of size SIZE. Return |
576 | 0 if P is NOT a valid pointer. Return -1 if we cannot validate P. | |
577 | ||
578 | This is called from alloc.c:valid_pointer_p. */ | |
579 | int | |
580 | w32_valid_pointer_p (void *p, int size) | |
581 | { | |
582 | SIZE_T done; | |
583 | HANDLE h = OpenProcess (PROCESS_VM_READ, FALSE, GetCurrentProcessId ()); | |
584 | ||
585 | if (h) | |
586 | { | |
587 | unsigned char *buf = alloca (size); | |
588 | int retval = ReadProcessMemory (h, p, buf, size, &done); | |
589 | ||
590 | CloseHandle (h); | |
591 | return retval; | |
592 | } | |
593 | else | |
594 | return -1; | |
595 | } | |
596 | ||
76b3903d | 597 | static char startup_dir[MAXPATHLEN]; |
00b3b7b3 | 598 | |
95ed0025 | 599 | /* Get the current working directory. */ |
480b0c5b | 600 | char * |
95ed0025 RS |
601 | getwd (char *dir) |
602 | { | |
76b3903d | 603 | #if 0 |
480b0c5b GV |
604 | if (GetCurrentDirectory (MAXPATHLEN, dir) > 0) |
605 | return dir; | |
606 | return NULL; | |
76b3903d GV |
607 | #else |
608 | /* Emacs doesn't actually change directory itself, and we want to | |
609 | force our real wd to be where emacs.exe is to avoid unnecessary | |
610 | conflicts when trying to rename or delete directories. */ | |
611 | strcpy (dir, startup_dir); | |
612 | return dir; | |
613 | #endif | |
95ed0025 RS |
614 | } |
615 | ||
480b0c5b | 616 | #ifndef HAVE_SOCKETS |
95ed0025 RS |
617 | /* Emulate gethostname. */ |
618 | int | |
619 | gethostname (char *buffer, int size) | |
620 | { | |
177c0ea7 | 621 | /* NT only allows small host names, so the buffer is |
95ed0025 RS |
622 | certainly large enough. */ |
623 | return !GetComputerName (buffer, &size); | |
624 | } | |
480b0c5b | 625 | #endif /* HAVE_SOCKETS */ |
95ed0025 RS |
626 | |
627 | /* Emulate getloadavg. */ | |
628 | int | |
629 | getloadavg (double loadavg[], int nelem) | |
630 | { | |
631 | int i; | |
632 | ||
633 | /* A faithful emulation is going to have to be saved for a rainy day. */ | |
177c0ea7 | 634 | for (i = 0; i < nelem; i++) |
95ed0025 RS |
635 | { |
636 | loadavg[i] = 0.0; | |
637 | } | |
638 | return i; | |
639 | } | |
640 | ||
480b0c5b | 641 | /* Emulate getpwuid, getpwnam and others. */ |
95ed0025 | 642 | |
051fe60d GV |
643 | #define PASSWD_FIELD_SIZE 256 |
644 | ||
07f7980a EZ |
645 | static char dflt_passwd_name[PASSWD_FIELD_SIZE]; |
646 | static char dflt_passwd_passwd[PASSWD_FIELD_SIZE]; | |
647 | static char dflt_passwd_gecos[PASSWD_FIELD_SIZE]; | |
648 | static char dflt_passwd_dir[PASSWD_FIELD_SIZE]; | |
649 | static char dflt_passwd_shell[PASSWD_FIELD_SIZE]; | |
95ed0025 | 650 | |
07f7980a | 651 | static struct passwd dflt_passwd = |
95ed0025 | 652 | { |
07f7980a EZ |
653 | dflt_passwd_name, |
654 | dflt_passwd_passwd, | |
95ed0025 RS |
655 | 0, |
656 | 0, | |
657 | 0, | |
07f7980a EZ |
658 | dflt_passwd_gecos, |
659 | dflt_passwd_dir, | |
660 | dflt_passwd_shell, | |
95ed0025 RS |
661 | }; |
662 | ||
07f7980a EZ |
663 | static char dflt_group_name[GNLEN+1]; |
664 | ||
665 | static struct group dflt_group = | |
3d19b645 | 666 | { |
07f7980a EZ |
667 | /* When group information is not available, we return this as the |
668 | group for all files. */ | |
669 | dflt_group_name, | |
670 | 0, | |
3d19b645 LH |
671 | }; |
672 | ||
177c0ea7 JB |
673 | int |
674 | getuid () | |
675 | { | |
07f7980a | 676 | return dflt_passwd.pw_uid; |
480b0c5b GV |
677 | } |
678 | ||
177c0ea7 JB |
679 | int |
680 | geteuid () | |
681 | { | |
480b0c5b GV |
682 | /* I could imagine arguing for checking to see whether the user is |
683 | in the Administrators group and returning a UID of 0 for that | |
684 | case, but I don't know how wise that would be in the long run. */ | |
177c0ea7 | 685 | return getuid (); |
480b0c5b GV |
686 | } |
687 | ||
177c0ea7 JB |
688 | int |
689 | getgid () | |
690 | { | |
07f7980a | 691 | return dflt_passwd.pw_gid; |
480b0c5b GV |
692 | } |
693 | ||
177c0ea7 JB |
694 | int |
695 | getegid () | |
696 | { | |
480b0c5b GV |
697 | return getgid (); |
698 | } | |
699 | ||
95ed0025 RS |
700 | struct passwd * |
701 | getpwuid (int uid) | |
702 | { | |
07f7980a EZ |
703 | if (uid == dflt_passwd.pw_uid) |
704 | return &dflt_passwd; | |
480b0c5b | 705 | return NULL; |
95ed0025 RS |
706 | } |
707 | ||
3d19b645 LH |
708 | struct group * |
709 | getgrgid (gid_t gid) | |
710 | { | |
07f7980a | 711 | return &dflt_group; |
3d19b645 LH |
712 | } |
713 | ||
95ed0025 RS |
714 | struct passwd * |
715 | getpwnam (char *name) | |
716 | { | |
717 | struct passwd *pw; | |
177c0ea7 | 718 | |
95ed0025 RS |
719 | pw = getpwuid (getuid ()); |
720 | if (!pw) | |
721 | return pw; | |
722 | ||
05131107 | 723 | if (xstrcasecmp (name, pw->pw_name)) |
95ed0025 RS |
724 | return NULL; |
725 | ||
726 | return pw; | |
727 | } | |
728 | ||
480b0c5b GV |
729 | void |
730 | init_user_info () | |
95ed0025 | 731 | { |
480b0c5b GV |
732 | /* Find the user's real name by opening the process token and |
733 | looking up the name associated with the user-sid in that token. | |
734 | ||
735 | Use the relative portion of the identifier authority value from | |
736 | the user-sid as the user id value (same for group id using the | |
737 | primary group sid from the process token). */ | |
738 | ||
07f7980a EZ |
739 | char uname[UNLEN+1], gname[GNLEN+1], domain[1025]; |
740 | DWORD ulength = sizeof (uname), dlength = sizeof (domain), trash; | |
741 | DWORD glength = sizeof (gname); | |
10aabbf9 JB |
742 | HANDLE token = NULL; |
743 | SID_NAME_USE user_type; | |
634d3003 EZ |
744 | unsigned char buf[1024]; |
745 | TOKEN_USER user_token; | |
746 | TOKEN_PRIMARY_GROUP group_token; | |
10aabbf9 JB |
747 | |
748 | if (open_process_token (GetCurrentProcess (), TOKEN_QUERY, &token) | |
749 | && get_token_information (token, TokenUser, | |
634d3003 EZ |
750 | (PVOID)buf, sizeof (buf), &trash) |
751 | && (memcpy (&user_token, buf, sizeof (user_token)), | |
07f7980a | 752 | lookup_account_sid (NULL, user_token.User.Sid, uname, &ulength, |
634d3003 | 753 | domain, &dlength, &user_type))) |
d1c1c3d2 | 754 | { |
07f7980a | 755 | strcpy (dflt_passwd.pw_name, uname); |
c617afce | 756 | /* Determine a reasonable uid value. */ |
05131107 | 757 | if (xstrcasecmp ("administrator", uname) == 0) |
480b0c5b | 758 | { |
07f7980a EZ |
759 | dflt_passwd.pw_uid = 500; /* well-known Administrator uid */ |
760 | dflt_passwd.pw_gid = 513; /* well-known None gid */ | |
480b0c5b GV |
761 | } |
762 | else | |
763 | { | |
ce0ee994 EZ |
764 | /* Use the last sub-authority value of the RID, the relative |
765 | portion of the SID, as user/group ID. */ | |
8aaaec6b | 766 | dflt_passwd.pw_uid = get_rid (user_token.User.Sid); |
480b0c5b | 767 | |
8aaaec6b | 768 | /* Get group id and name. */ |
f60ae425 | 769 | if (get_token_information (token, TokenPrimaryGroup, |
634d3003 | 770 | (PVOID)buf, sizeof (buf), &trash)) |
480b0c5b | 771 | { |
634d3003 | 772 | memcpy (&group_token, buf, sizeof (group_token)); |
8aaaec6b | 773 | dflt_passwd.pw_gid = get_rid (group_token.PrimaryGroup); |
07f7980a EZ |
774 | dlength = sizeof (domain); |
775 | if (lookup_account_sid (NULL, group_token.PrimaryGroup, | |
776 | gname, &glength, NULL, &dlength, | |
777 | &user_type)) | |
778 | strcpy (dflt_group_name, gname); | |
480b0c5b GV |
779 | } |
780 | else | |
07f7980a | 781 | dflt_passwd.pw_gid = dflt_passwd.pw_uid; |
480b0c5b GV |
782 | } |
783 | } | |
784 | /* If security calls are not supported (presumably because we | |
10aabbf9 | 785 | are running under Windows 95), fallback to this. */ |
07f7980a | 786 | else if (GetUserName (uname, &ulength)) |
480b0c5b | 787 | { |
07f7980a | 788 | strcpy (dflt_passwd.pw_name, uname); |
05131107 | 789 | if (xstrcasecmp ("administrator", uname) == 0) |
07f7980a | 790 | dflt_passwd.pw_uid = 0; |
480b0c5b | 791 | else |
07f7980a EZ |
792 | dflt_passwd.pw_uid = 123; |
793 | dflt_passwd.pw_gid = dflt_passwd.pw_uid; | |
480b0c5b GV |
794 | } |
795 | else | |
796 | { | |
07f7980a EZ |
797 | strcpy (dflt_passwd.pw_name, "unknown"); |
798 | dflt_passwd.pw_uid = 123; | |
799 | dflt_passwd.pw_gid = 123; | |
d1c1c3d2 | 800 | } |
07f7980a | 801 | dflt_group.gr_gid = dflt_passwd.pw_gid; |
95ed0025 | 802 | |
480b0c5b GV |
803 | /* Ensure HOME and SHELL are defined. */ |
804 | if (getenv ("HOME") == NULL) | |
ca149beb | 805 | abort (); |
480b0c5b | 806 | if (getenv ("SHELL") == NULL) |
ca149beb | 807 | abort (); |
95ed0025 | 808 | |
480b0c5b | 809 | /* Set dir and shell from environment variables. */ |
07f7980a EZ |
810 | strcpy (dflt_passwd.pw_dir, getenv ("HOME")); |
811 | strcpy (dflt_passwd.pw_shell, getenv ("SHELL")); | |
bd4a449f | 812 | |
480b0c5b GV |
813 | if (token) |
814 | CloseHandle (token); | |
95ed0025 RS |
815 | } |
816 | ||
95ed0025 | 817 | int |
480b0c5b | 818 | random () |
95ed0025 | 819 | { |
480b0c5b GV |
820 | /* rand () on NT gives us 15 random bits...hack together 30 bits. */ |
821 | return ((rand () << 15) | rand ()); | |
95ed0025 RS |
822 | } |
823 | ||
95ed0025 | 824 | void |
480b0c5b | 825 | srandom (int seed) |
95ed0025 | 826 | { |
480b0c5b | 827 | srand (seed); |
95ed0025 RS |
828 | } |
829 | ||
76b3903d | 830 | |
cbe39279 RS |
831 | /* Normalize filename by converting all path separators to |
832 | the specified separator. Also conditionally convert upper | |
833 | case path name components to lower case. */ | |
834 | ||
835 | static void | |
836 | normalize_filename (fp, path_sep) | |
837 | register char *fp; | |
838 | char path_sep; | |
839 | { | |
840 | char sep; | |
841 | char *elem; | |
842 | ||
5162ffce MB |
843 | /* Always lower-case drive letters a-z, even if the filesystem |
844 | preserves case in filenames. | |
845 | This is so filenames can be compared by string comparison | |
846 | functions that are case-sensitive. Even case-preserving filesystems | |
847 | do not distinguish case in drive letters. */ | |
848 | if (fp[1] == ':' && *fp >= 'A' && *fp <= 'Z') | |
849 | { | |
850 | *fp += 'a' - 'A'; | |
851 | fp += 2; | |
852 | } | |
853 | ||
fbd6baed | 854 | if (NILP (Vw32_downcase_file_names)) |
cbe39279 RS |
855 | { |
856 | while (*fp) | |
857 | { | |
858 | if (*fp == '/' || *fp == '\\') | |
859 | *fp = path_sep; | |
860 | fp++; | |
861 | } | |
862 | return; | |
863 | } | |
864 | ||
865 | sep = path_sep; /* convert to this path separator */ | |
866 | elem = fp; /* start of current path element */ | |
867 | ||
868 | do { | |
869 | if (*fp >= 'a' && *fp <= 'z') | |
870 | elem = 0; /* don't convert this element */ | |
871 | ||
872 | if (*fp == 0 || *fp == ':') | |
873 | { | |
874 | sep = *fp; /* restore current separator (or 0) */ | |
875 | *fp = '/'; /* after conversion of this element */ | |
876 | } | |
877 | ||
878 | if (*fp == '/' || *fp == '\\') | |
879 | { | |
880 | if (elem && elem != fp) | |
881 | { | |
882 | *fp = 0; /* temporary end of string */ | |
883 | _strlwr (elem); /* while we convert to lower case */ | |
884 | } | |
885 | *fp = sep; /* convert (or restore) path separator */ | |
886 | elem = fp + 1; /* next element starts after separator */ | |
887 | sep = path_sep; | |
888 | } | |
889 | } while (*fp++); | |
890 | } | |
891 | ||
480b0c5b | 892 | /* Destructively turn backslashes into slashes. */ |
95ed0025 | 893 | void |
480b0c5b GV |
894 | dostounix_filename (p) |
895 | register char *p; | |
95ed0025 | 896 | { |
cbe39279 | 897 | normalize_filename (p, '/'); |
95ed0025 RS |
898 | } |
899 | ||
480b0c5b | 900 | /* Destructively turn slashes into backslashes. */ |
95ed0025 | 901 | void |
480b0c5b GV |
902 | unixtodos_filename (p) |
903 | register char *p; | |
95ed0025 | 904 | { |
cbe39279 | 905 | normalize_filename (p, '\\'); |
95ed0025 RS |
906 | } |
907 | ||
480b0c5b GV |
908 | /* Remove all CR's that are followed by a LF. |
909 | (From msdos.c...probably should figure out a way to share it, | |
910 | although this code isn't going to ever change.) */ | |
35f0d482 | 911 | int |
480b0c5b GV |
912 | crlf_to_lf (n, buf) |
913 | register int n; | |
914 | register unsigned char *buf; | |
35f0d482 | 915 | { |
480b0c5b GV |
916 | unsigned char *np = buf; |
917 | unsigned char *startp = buf; | |
918 | unsigned char *endp = buf + n; | |
35f0d482 | 919 | |
480b0c5b GV |
920 | if (n == 0) |
921 | return n; | |
922 | while (buf < endp - 1) | |
95ed0025 | 923 | { |
480b0c5b GV |
924 | if (*buf == 0x0d) |
925 | { | |
926 | if (*(++buf) != 0x0a) | |
927 | *np++ = 0x0d; | |
928 | } | |
929 | else | |
930 | *np++ = *buf++; | |
95ed0025 | 931 | } |
480b0c5b GV |
932 | if (buf < endp) |
933 | *np++ = *buf++; | |
934 | return np - startp; | |
95ed0025 RS |
935 | } |
936 | ||
76b3903d GV |
937 | /* Parse the root part of file name, if present. Return length and |
938 | optionally store pointer to char after root. */ | |
939 | static int | |
940 | parse_root (char * name, char ** pPath) | |
941 | { | |
942 | char * start = name; | |
943 | ||
944 | if (name == NULL) | |
945 | return 0; | |
946 | ||
947 | /* find the root name of the volume if given */ | |
948 | if (isalpha (name[0]) && name[1] == ':') | |
949 | { | |
950 | /* skip past drive specifier */ | |
951 | name += 2; | |
952 | if (IS_DIRECTORY_SEP (name[0])) | |
953 | name++; | |
954 | } | |
955 | else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1])) | |
956 | { | |
957 | int slashes = 2; | |
958 | name += 2; | |
959 | do | |
960 | { | |
961 | if (IS_DIRECTORY_SEP (*name) && --slashes == 0) | |
962 | break; | |
963 | name++; | |
964 | } | |
965 | while ( *name ); | |
966 | if (IS_DIRECTORY_SEP (name[0])) | |
967 | name++; | |
968 | } | |
969 | ||
970 | if (pPath) | |
971 | *pPath = name; | |
972 | ||
973 | return name - start; | |
974 | } | |
975 | ||
976 | /* Get long base name for name; name is assumed to be absolute. */ | |
977 | static int | |
978 | get_long_basename (char * name, char * buf, int size) | |
979 | { | |
980 | WIN32_FIND_DATA find_data; | |
981 | HANDLE dir_handle; | |
982 | int len = 0; | |
983 | ||
9ab8560d | 984 | /* must be valid filename, no wild cards or other invalid characters */ |
22189f79 | 985 | if (_mbspbrk (name, "*?|<>\"")) |
bb1584c8 RS |
986 | return 0; |
987 | ||
76b3903d GV |
988 | dir_handle = FindFirstFile (name, &find_data); |
989 | if (dir_handle != INVALID_HANDLE_VALUE) | |
990 | { | |
991 | if ((len = strlen (find_data.cFileName)) < size) | |
992 | memcpy (buf, find_data.cFileName, len + 1); | |
993 | else | |
994 | len = 0; | |
995 | FindClose (dir_handle); | |
996 | } | |
997 | return len; | |
998 | } | |
999 | ||
1000 | /* Get long name for file, if possible (assumed to be absolute). */ | |
1001 | BOOL | |
1002 | w32_get_long_filename (char * name, char * buf, int size) | |
1003 | { | |
1004 | char * o = buf; | |
1005 | char * p; | |
1006 | char * q; | |
1007 | char full[ MAX_PATH ]; | |
1008 | int len; | |
1009 | ||
1010 | len = strlen (name); | |
1011 | if (len >= MAX_PATH) | |
1012 | return FALSE; | |
1013 | ||
1014 | /* Use local copy for destructive modification. */ | |
1015 | memcpy (full, name, len+1); | |
1016 | unixtodos_filename (full); | |
1017 | ||
1018 | /* Copy root part verbatim. */ | |
1019 | len = parse_root (full, &p); | |
1020 | memcpy (o, full, len); | |
1021 | o += len; | |
4f8ac0b2 | 1022 | *o = '\0'; |
76b3903d GV |
1023 | size -= len; |
1024 | ||
4f8ac0b2 | 1025 | while (p != NULL && *p) |
76b3903d GV |
1026 | { |
1027 | q = p; | |
1028 | p = strchr (q, '\\'); | |
1029 | if (p) *p = '\0'; | |
1030 | len = get_long_basename (full, o, size); | |
1031 | if (len > 0) | |
1032 | { | |
1033 | o += len; | |
1034 | size -= len; | |
1035 | if (p != NULL) | |
1036 | { | |
1037 | *p++ = '\\'; | |
1038 | if (size < 2) | |
1039 | return FALSE; | |
1040 | *o++ = '\\'; | |
1041 | size--; | |
1042 | *o = '\0'; | |
1043 | } | |
1044 | } | |
1045 | else | |
1046 | return FALSE; | |
1047 | } | |
76b3903d GV |
1048 | |
1049 | return TRUE; | |
1050 | } | |
1051 | ||
9d3355d1 GV |
1052 | int |
1053 | is_unc_volume (const char *filename) | |
1054 | { | |
1055 | const char *ptr = filename; | |
1056 | ||
1057 | if (!IS_DIRECTORY_SEP (ptr[0]) || !IS_DIRECTORY_SEP (ptr[1]) || !ptr[2]) | |
1058 | return 0; | |
1059 | ||
22189f79 | 1060 | if (_mbspbrk (ptr + 2, "*?|<>\"\\/")) |
9d3355d1 GV |
1061 | return 0; |
1062 | ||
1063 | return 1; | |
1064 | } | |
76b3903d | 1065 | |
95ed0025 RS |
1066 | /* Routines that are no-ops on NT but are defined to get Emacs to compile. */ |
1067 | ||
177c0ea7 JB |
1068 | int |
1069 | sigsetmask (int signal_mask) | |
1070 | { | |
95ed0025 RS |
1071 | return 0; |
1072 | } | |
1073 | ||
177c0ea7 JB |
1074 | int |
1075 | sigmask (int sig) | |
1076 | { | |
8f900f6e AI |
1077 | return 0; |
1078 | } | |
1079 | ||
177c0ea7 JB |
1080 | int |
1081 | sigblock (int sig) | |
1082 | { | |
95ed0025 RS |
1083 | return 0; |
1084 | } | |
1085 | ||
177c0ea7 JB |
1086 | int |
1087 | sigunblock (int sig) | |
1088 | { | |
8f900f6e AI |
1089 | return 0; |
1090 | } | |
1091 | ||
177c0ea7 JB |
1092 | int |
1093 | setpgrp (int pid, int gid) | |
1094 | { | |
95ed0025 RS |
1095 | return 0; |
1096 | } | |
1097 | ||
177c0ea7 JB |
1098 | int |
1099 | alarm (int seconds) | |
1100 | { | |
95ed0025 RS |
1101 | return 0; |
1102 | } | |
1103 | ||
480b0c5b | 1104 | #define REG_ROOT "SOFTWARE\\GNU\\Emacs" |
f332b293 | 1105 | |
177c0ea7 | 1106 | LPBYTE |
fbd6baed | 1107 | w32_get_resource (key, lpdwtype) |
f332b293 GV |
1108 | char *key; |
1109 | LPDWORD lpdwtype; | |
1110 | { | |
1111 | LPBYTE lpvalue; | |
1112 | HKEY hrootkey = NULL; | |
1113 | DWORD cbData; | |
177c0ea7 JB |
1114 | |
1115 | /* Check both the current user and the local machine to see if | |
f332b293 | 1116 | we have any resources. */ |
177c0ea7 | 1117 | |
f332b293 GV |
1118 | if (RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS) |
1119 | { | |
1120 | lpvalue = NULL; | |
1121 | ||
177c0ea7 JB |
1122 | if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS |
1123 | && (lpvalue = (LPBYTE) xmalloc (cbData)) != NULL | |
f332b293 GV |
1124 | && RegQueryValueEx (hrootkey, key, NULL, lpdwtype, lpvalue, &cbData) == ERROR_SUCCESS) |
1125 | { | |
4da4d9bb | 1126 | RegCloseKey (hrootkey); |
f332b293 GV |
1127 | return (lpvalue); |
1128 | } | |
1129 | ||
1130 | if (lpvalue) xfree (lpvalue); | |
177c0ea7 | 1131 | |
f332b293 | 1132 | RegCloseKey (hrootkey); |
177c0ea7 JB |
1133 | } |
1134 | ||
f332b293 GV |
1135 | if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS) |
1136 | { | |
1137 | lpvalue = NULL; | |
177c0ea7 | 1138 | |
76b3903d GV |
1139 | if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS |
1140 | && (lpvalue = (LPBYTE) xmalloc (cbData)) != NULL | |
1141 | && RegQueryValueEx (hrootkey, key, NULL, lpdwtype, lpvalue, &cbData) == ERROR_SUCCESS) | |
f332b293 | 1142 | { |
4da4d9bb | 1143 | RegCloseKey (hrootkey); |
f332b293 GV |
1144 | return (lpvalue); |
1145 | } | |
177c0ea7 | 1146 | |
f332b293 | 1147 | if (lpvalue) xfree (lpvalue); |
177c0ea7 | 1148 | |
f332b293 | 1149 | RegCloseKey (hrootkey); |
177c0ea7 JB |
1150 | } |
1151 | ||
f332b293 GV |
1152 | return (NULL); |
1153 | } | |
1154 | ||
75b08edb GV |
1155 | char *get_emacs_configuration (void); |
1156 | extern Lisp_Object Vsystem_configuration; | |
1157 | ||
f332b293 | 1158 | void |
aa7b87b0 | 1159 | init_environment (char ** argv) |
f332b293 | 1160 | { |
b3308d2e KH |
1161 | static const char * const tempdirs[] = { |
1162 | "$TMPDIR", "$TEMP", "$TMP", "c:/" | |
1163 | }; | |
2d5324c5 | 1164 | |
b3308d2e | 1165 | int i; |
2d5324c5 | 1166 | |
b3308d2e KH |
1167 | const int imax = sizeof (tempdirs) / sizeof (tempdirs[0]); |
1168 | ||
1169 | /* Make sure they have a usable $TMPDIR. Many Emacs functions use | |
1170 | temporary files and assume "/tmp" if $TMPDIR is unset, which | |
1171 | will break on DOS/Windows. Refuse to work if we cannot find | |
1172 | a directory, not even "c:/", usable for that purpose. */ | |
1173 | for (i = 0; i < imax ; i++) | |
1174 | { | |
1175 | const char *tmp = tempdirs[i]; | |
1176 | ||
1177 | if (*tmp == '$') | |
1178 | tmp = getenv (tmp + 1); | |
1179 | /* Note that `access' can lie to us if the directory resides on a | |
1180 | read-only filesystem, like CD-ROM or a write-protected floppy. | |
1181 | The only way to be really sure is to actually create a file and | |
1182 | see if it succeeds. But I think that's too much to ask. */ | |
a302c7ae | 1183 | if (tmp && _access (tmp, D_OK) == 0) |
b3308d2e KH |
1184 | { |
1185 | char * var = alloca (strlen (tmp) + 8); | |
1186 | sprintf (var, "TMPDIR=%s", tmp); | |
aca583b2 | 1187 | _putenv (strdup (var)); |
b3308d2e KH |
1188 | break; |
1189 | } | |
1190 | } | |
1191 | if (i >= imax) | |
1192 | cmd_error_internal | |
1193 | (Fcons (Qerror, | |
1194 | Fcons (build_string ("no usable temporary directories found!!"), | |
1195 | Qnil)), | |
1196 | "While setting TMPDIR: "); | |
1197 | ||
ca149beb AI |
1198 | /* Check for environment variables and use registry settings if they |
1199 | don't exist. Fallback on default values where applicable. */ | |
f332b293 | 1200 | { |
480b0c5b GV |
1201 | int i; |
1202 | LPBYTE lpval; | |
1203 | DWORD dwType; | |
69fb0241 | 1204 | char locale_name[32]; |
2d5324c5 JR |
1205 | struct stat ignored; |
1206 | char default_home[MAX_PATH]; | |
f332b293 | 1207 | |
e00b99c8 | 1208 | static const struct env_entry |
ca149beb AI |
1209 | { |
1210 | char * name; | |
1211 | char * def_value; | |
e00b99c8 | 1212 | } dflt_envvars[] = |
ca149beb AI |
1213 | { |
1214 | {"HOME", "C:/"}, | |
1215 | {"PRELOAD_WINSOCK", NULL}, | |
1216 | {"emacs_dir", "C:/emacs"}, | |
cc14250a | 1217 | {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"}, |
ca149beb AI |
1218 | {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"}, |
1219 | {"EMACSDATA", "%emacs_dir%/etc"}, | |
1220 | {"EMACSPATH", "%emacs_dir%/bin"}, | |
76b3903d | 1221 | /* We no longer set INFOPATH because Info-default-directory-list |
ca149beb AI |
1222 | is then ignored. */ |
1223 | /* {"INFOPATH", "%emacs_dir%/info"}, */ | |
1224 | {"EMACSDOC", "%emacs_dir%/etc"}, | |
69fb0241 JR |
1225 | {"TERM", "cmd"}, |
1226 | {"LANG", NULL}, | |
480b0c5b GV |
1227 | }; |
1228 | ||
e00b99c8 EZ |
1229 | #define N_ENV_VARS sizeof(dflt_envvars)/sizeof(dflt_envvars[0]) |
1230 | ||
1231 | /* We need to copy dflt_envvars[] and work on the copy because we | |
1232 | don't want the dumped Emacs to inherit the values of | |
1233 | environment variables we saw during dumping (which could be on | |
1234 | a different system). The defaults above must be left intact. */ | |
1235 | struct env_entry env_vars[N_ENV_VARS]; | |
1236 | ||
1237 | for (i = 0; i < N_ENV_VARS; i++) | |
1238 | env_vars[i] = dflt_envvars[i]; | |
1239 | ||
2d5324c5 JR |
1240 | /* For backwards compatibility, check if a .emacs file exists in C:/ |
1241 | If not, then we can try to default to the appdata directory under the | |
1242 | user's profile, which is more likely to be writable. */ | |
1243 | if (stat ("C:/.emacs", &ignored) < 0) | |
1244 | { | |
1245 | HRESULT profile_result; | |
1246 | /* Dynamically load ShGetFolderPath, as it won't exist on versions | |
1247 | of Windows 95 and NT4 that have not been updated to include | |
1248 | MSIE 5. Also we don't link with shell32.dll by default. */ | |
1249 | HMODULE shell32_dll; | |
1250 | ShGetFolderPath_fn get_folder_path; | |
1251 | shell32_dll = GetModuleHandle ("shell32.dll"); | |
1252 | get_folder_path = (ShGetFolderPath_fn) | |
1253 | GetProcAddress (shell32_dll, "SHGetFolderPathA"); | |
1254 | ||
1255 | if (get_folder_path != NULL) | |
1256 | { | |
1257 | profile_result = get_folder_path (NULL, CSIDL_APPDATA, NULL, | |
1258 | 0, default_home); | |
1259 | ||
1260 | /* If we can't get the appdata dir, revert to old behaviour. */ | |
1261 | if (profile_result == S_OK) | |
1262 | env_vars[0].def_value = default_home; | |
1263 | } | |
1264 | ||
1265 | /* Unload shell32.dll, it is not needed anymore. */ | |
1266 | FreeLibrary (shell32_dll); | |
1267 | } | |
1268 | ||
69fb0241 JR |
1269 | /* Get default locale info and use it for LANG. */ |
1270 | if (GetLocaleInfo (LOCALE_USER_DEFAULT, | |
1271 | LOCALE_SABBREVLANGNAME | LOCALE_USE_CP_ACP, | |
1272 | locale_name, sizeof (locale_name))) | |
1273 | { | |
e00b99c8 | 1274 | for (i = 0; i < N_ENV_VARS; i++) |
69fb0241 JR |
1275 | { |
1276 | if (strcmp (env_vars[i].name, "LANG") == 0) | |
1277 | { | |
1278 | env_vars[i].def_value = locale_name; | |
1279 | break; | |
1280 | } | |
1281 | } | |
1282 | } | |
1283 | ||
ca149beb AI |
1284 | #define SET_ENV_BUF_SIZE (4 * MAX_PATH) /* to cover EMACSLOADPATH */ |
1285 | ||
1286 | /* Treat emacs_dir specially: set it unconditionally based on our | |
1287 | location, if it appears that we are running from the bin subdir | |
1288 | of a standard installation. */ | |
1289 | { | |
1290 | char *p; | |
1291 | char modname[MAX_PATH]; | |
1292 | ||
1293 | if (!GetModuleFileName (NULL, modname, MAX_PATH)) | |
1294 | abort (); | |
1295 | if ((p = strrchr (modname, '\\')) == NULL) | |
1296 | abort (); | |
1297 | *p = 0; | |
1298 | ||
05131107 | 1299 | if ((p = strrchr (modname, '\\')) && xstrcasecmp (p, "\\bin") == 0) |
ca149beb AI |
1300 | { |
1301 | char buf[SET_ENV_BUF_SIZE]; | |
1302 | ||
1303 | *p = 0; | |
1304 | for (p = modname; *p; p++) | |
1305 | if (*p == '\\') *p = '/'; | |
177c0ea7 | 1306 | |
ca149beb | 1307 | _snprintf (buf, sizeof(buf)-1, "emacs_dir=%s", modname); |
a302c7ae | 1308 | _putenv (strdup (buf)); |
ca149beb | 1309 | } |
950090be JR |
1310 | /* Handle running emacs from the build directory: src/oo-spd/i386/ */ |
1311 | ||
1312 | /* FIXME: should use substring of get_emacs_configuration (). | |
1313 | But I don't think the Windows build supports alpha, mips etc | |
1314 | anymore, so have taken the easy option for now. */ | |
05131107 | 1315 | else if (p && xstrcasecmp (p, "\\i386") == 0) |
950090be JR |
1316 | { |
1317 | *p = 0; | |
1318 | p = strrchr (modname, '\\'); | |
1319 | if (p != NULL) | |
1320 | { | |
1321 | *p = 0; | |
1322 | p = strrchr (modname, '\\'); | |
05131107 | 1323 | if (p && xstrcasecmp (p, "\\src") == 0) |
950090be JR |
1324 | { |
1325 | char buf[SET_ENV_BUF_SIZE]; | |
1326 | ||
1327 | *p = 0; | |
1328 | for (p = modname; *p; p++) | |
1329 | if (*p == '\\') *p = '/'; | |
1330 | ||
1331 | _snprintf (buf, sizeof(buf)-1, "emacs_dir=%s", modname); | |
1332 | _putenv (strdup (buf)); | |
1333 | } | |
1334 | } | |
1335 | } | |
ca149beb AI |
1336 | } |
1337 | ||
e00b99c8 | 1338 | for (i = 0; i < N_ENV_VARS; i++) |
f332b293 | 1339 | { |
ca149beb | 1340 | if (!getenv (env_vars[i].name)) |
480b0c5b | 1341 | { |
ca149beb | 1342 | int dont_free = 0; |
480b0c5b | 1343 | |
aa5ee2a3 JB |
1344 | if ((lpval = w32_get_resource (env_vars[i].name, &dwType)) == NULL |
1345 | /* Also ignore empty environment variables. */ | |
1346 | || *lpval == 0) | |
ca149beb | 1347 | { |
14c71d8b | 1348 | if (lpval) xfree (lpval); |
ca149beb AI |
1349 | lpval = env_vars[i].def_value; |
1350 | dwType = REG_EXPAND_SZ; | |
1351 | dont_free = 1; | |
480b0c5b | 1352 | } |
ca149beb AI |
1353 | |
1354 | if (lpval) | |
480b0c5b | 1355 | { |
892eb237 | 1356 | char buf1[SET_ENV_BUF_SIZE], buf2[SET_ENV_BUF_SIZE]; |
ca149beb | 1357 | |
892eb237 EZ |
1358 | if (dwType == REG_EXPAND_SZ) |
1359 | ExpandEnvironmentStrings ((LPSTR) lpval, buf1, sizeof(buf1)); | |
ca149beb | 1360 | else if (dwType == REG_SZ) |
892eb237 EZ |
1361 | strcpy (buf1, lpval); |
1362 | if (dwType == REG_EXPAND_SZ || dwType == REG_SZ) | |
ca149beb | 1363 | { |
892eb237 EZ |
1364 | _snprintf (buf2, sizeof(buf2)-1, "%s=%s", env_vars[i].name, |
1365 | buf1); | |
1366 | _putenv (strdup (buf2)); | |
ca149beb | 1367 | } |
f332b293 | 1368 | |
ca149beb AI |
1369 | if (!dont_free) |
1370 | xfree (lpval); | |
1371 | } | |
480b0c5b GV |
1372 | } |
1373 | } | |
1374 | } | |
1375 | ||
75b08edb GV |
1376 | /* Rebuild system configuration to reflect invoking system. */ |
1377 | Vsystem_configuration = build_string (EMACS_CONFIGURATION); | |
1378 | ||
76b3903d GV |
1379 | /* Another special case: on NT, the PATH variable is actually named |
1380 | "Path" although cmd.exe (perhaps NT itself) arranges for | |
1381 | environment variable lookup and setting to be case insensitive. | |
1382 | However, Emacs assumes a fully case sensitive environment, so we | |
1383 | need to change "Path" to "PATH" to match the expectations of | |
1384 | various elisp packages. We do this by the sneaky method of | |
1385 | modifying the string in the C runtime environ entry. | |
1386 | ||
1387 | The same applies to COMSPEC. */ | |
1388 | { | |
1389 | char ** envp; | |
1390 | ||
1391 | for (envp = environ; *envp; envp++) | |
1392 | if (_strnicmp (*envp, "PATH=", 5) == 0) | |
1393 | memcpy (*envp, "PATH=", 5); | |
1394 | else if (_strnicmp (*envp, "COMSPEC=", 8) == 0) | |
1395 | memcpy (*envp, "COMSPEC=", 8); | |
1396 | } | |
1397 | ||
1398 | /* Remember the initial working directory for getwd, then make the | |
1399 | real wd be the location of emacs.exe to avoid conflicts when | |
1400 | renaming or deleting directories. (We also don't call chdir when | |
1401 | running subprocesses for the same reason.) */ | |
1402 | if (!GetCurrentDirectory (MAXPATHLEN, startup_dir)) | |
1403 | abort (); | |
1404 | ||
1405 | { | |
1406 | char *p; | |
aa7b87b0 | 1407 | static char modname[MAX_PATH]; |
76b3903d GV |
1408 | |
1409 | if (!GetModuleFileName (NULL, modname, MAX_PATH)) | |
1410 | abort (); | |
1411 | if ((p = strrchr (modname, '\\')) == NULL) | |
1412 | abort (); | |
1413 | *p = 0; | |
1414 | ||
1415 | SetCurrentDirectory (modname); | |
aa7b87b0 AI |
1416 | |
1417 | /* Ensure argv[0] has the full path to Emacs. */ | |
1418 | *p = '\\'; | |
1419 | argv[0] = modname; | |
76b3903d GV |
1420 | } |
1421 | ||
20af4831 JR |
1422 | /* Determine if there is a middle mouse button, to allow parse_button |
1423 | to decide whether right mouse events should be mouse-2 or | |
1424 | mouse-3. */ | |
e0c181dd | 1425 | w32_num_mouse_buttons = GetSystemMetrics (SM_CMOUSEBUTTONS); |
20af4831 | 1426 | |
480b0c5b GV |
1427 | init_user_info (); |
1428 | } | |
1429 | ||
bf794306 EZ |
1430 | char * |
1431 | emacs_root_dir (void) | |
1432 | { | |
1433 | static char root_dir[FILENAME_MAX]; | |
1434 | const char *p; | |
1435 | ||
1436 | p = getenv ("emacs_dir"); | |
1437 | if (p == NULL) | |
1438 | abort (); | |
1439 | strcpy (root_dir, p); | |
1440 | root_dir[parse_root (root_dir, NULL)] = '\0'; | |
1441 | dostounix_filename (root_dir); | |
1442 | return root_dir; | |
1443 | } | |
1444 | ||
480b0c5b GV |
1445 | /* We don't have scripts to automatically determine the system configuration |
1446 | for Emacs before it's compiled, and we don't want to have to make the | |
1447 | user enter it, so we define EMACS_CONFIGURATION to invoke this runtime | |
1448 | routine. */ | |
1449 | ||
480b0c5b GV |
1450 | char * |
1451 | get_emacs_configuration (void) | |
1452 | { | |
1453 | char *arch, *oem, *os; | |
c5247da2 | 1454 | int build_num; |
a302c7ae | 1455 | static char configuration_buffer[32]; |
480b0c5b GV |
1456 | |
1457 | /* Determine the processor type. */ | |
177c0ea7 | 1458 | switch (get_processor_type ()) |
480b0c5b GV |
1459 | { |
1460 | ||
1461 | #ifdef PROCESSOR_INTEL_386 | |
1462 | case PROCESSOR_INTEL_386: | |
1463 | case PROCESSOR_INTEL_486: | |
1464 | case PROCESSOR_INTEL_PENTIUM: | |
1465 | arch = "i386"; | |
1466 | break; | |
1467 | #endif | |
1468 | ||
480b0c5b GV |
1469 | #ifdef PROCESSOR_MIPS_R2000 |
1470 | case PROCESSOR_MIPS_R2000: | |
1471 | case PROCESSOR_MIPS_R3000: | |
1472 | case PROCESSOR_MIPS_R4000: | |
1473 | arch = "mips"; | |
1474 | break; | |
1475 | #endif | |
1476 | ||
1477 | #ifdef PROCESSOR_ALPHA_21064 | |
1478 | case PROCESSOR_ALPHA_21064: | |
1479 | arch = "alpha"; | |
1480 | break; | |
1481 | #endif | |
1482 | ||
1483 | default: | |
1484 | arch = "unknown"; | |
1485 | break; | |
f332b293 | 1486 | } |
480b0c5b | 1487 | |
a302c7ae AI |
1488 | /* Use the OEM field to reflect the compiler/library combination. */ |
1489 | #ifdef _MSC_VER | |
1490 | #define COMPILER_NAME "msvc" | |
1491 | #else | |
1492 | #ifdef __GNUC__ | |
1493 | #define COMPILER_NAME "mingw" | |
1494 | #else | |
1495 | #define COMPILER_NAME "unknown" | |
1496 | #endif | |
1497 | #endif | |
1498 | oem = COMPILER_NAME; | |
480b0c5b | 1499 | |
c5247da2 GV |
1500 | switch (osinfo_cache.dwPlatformId) { |
1501 | case VER_PLATFORM_WIN32_NT: | |
1502 | os = "nt"; | |
1503 | build_num = osinfo_cache.dwBuildNumber; | |
1504 | break; | |
1505 | case VER_PLATFORM_WIN32_WINDOWS: | |
1506 | if (osinfo_cache.dwMinorVersion == 0) { | |
1507 | os = "windows95"; | |
1508 | } else { | |
1509 | os = "windows98"; | |
1510 | } | |
1511 | build_num = LOWORD (osinfo_cache.dwBuildNumber); | |
1512 | break; | |
1513 | case VER_PLATFORM_WIN32s: | |
1514 | /* Not supported, should not happen. */ | |
1515 | os = "windows32s"; | |
1516 | build_num = LOWORD (osinfo_cache.dwBuildNumber); | |
1517 | break; | |
1518 | default: | |
1519 | os = "unknown"; | |
1520 | build_num = 0; | |
1521 | break; | |
1522 | } | |
1523 | ||
1524 | if (osinfo_cache.dwPlatformId == VER_PLATFORM_WIN32_NT) { | |
1525 | sprintf (configuration_buffer, "%s-%s-%s%d.%d.%d", arch, oem, os, | |
1526 | get_w32_major_version (), get_w32_minor_version (), build_num); | |
1527 | } else { | |
1528 | sprintf (configuration_buffer, "%s-%s-%s.%d", arch, oem, os, build_num); | |
1529 | } | |
480b0c5b | 1530 | |
480b0c5b | 1531 | return configuration_buffer; |
f332b293 GV |
1532 | } |
1533 | ||
a302c7ae AI |
1534 | char * |
1535 | get_emacs_configuration_options (void) | |
1536 | { | |
1537 | static char options_buffer[256]; | |
1538 | ||
1539 | /* Work out the effective configure options for this build. */ | |
1540 | #ifdef _MSC_VER | |
1541 | #define COMPILER_VERSION "--with-msvc (%d.%02d)", _MSC_VER / 100, _MSC_VER % 100 | |
1542 | #else | |
1543 | #ifdef __GNUC__ | |
1544 | #define COMPILER_VERSION "--with-gcc (%d.%d)", __GNUC__, __GNUC_MINOR__ | |
1545 | #else | |
1546 | #define COMPILER_VERSION "" | |
1547 | #endif | |
1548 | #endif | |
1549 | ||
1550 | sprintf (options_buffer, COMPILER_VERSION); | |
1551 | #ifdef EMACSDEBUG | |
1552 | strcat (options_buffer, " --no-opt"); | |
1553 | #endif | |
1554 | #ifdef USER_CFLAGS | |
1555 | strcat (options_buffer, " --cflags"); | |
1556 | strcat (options_buffer, USER_CFLAGS); | |
1557 | #endif | |
1558 | #ifdef USER_LDFLAGS | |
1559 | strcat (options_buffer, " --ldflags"); | |
1560 | strcat (options_buffer, USER_LDFLAGS); | |
1561 | #endif | |
1562 | return options_buffer; | |
1563 | } | |
1564 | ||
1565 | ||
35f0d482 KH |
1566 | #include <sys/timeb.h> |
1567 | ||
1568 | /* Emulate gettimeofday (Ulrich Leodolter, 1/11/95). */ | |
177c0ea7 | 1569 | void |
35f0d482 KH |
1570 | gettimeofday (struct timeval *tv, struct timezone *tz) |
1571 | { | |
6e602566 | 1572 | struct _timeb tb; |
35f0d482 KH |
1573 | _ftime (&tb); |
1574 | ||
1575 | tv->tv_sec = tb.time; | |
1576 | tv->tv_usec = tb.millitm * 1000L; | |
177c0ea7 | 1577 | if (tz) |
35f0d482 KH |
1578 | { |
1579 | tz->tz_minuteswest = tb.timezone; /* minutes west of Greenwich */ | |
1580 | tz->tz_dsttime = tb.dstflag; /* type of dst correction */ | |
1581 | } | |
1582 | } | |
35f0d482 | 1583 | |
480b0c5b | 1584 | /* ------------------------------------------------------------------------- */ |
fbd6baed | 1585 | /* IO support and wrapper functions for W32 API. */ |
480b0c5b | 1586 | /* ------------------------------------------------------------------------- */ |
95ed0025 | 1587 | |
480b0c5b | 1588 | /* Place a wrapper around the MSVC version of ctime. It returns NULL |
177c0ea7 | 1589 | on network directories, so we handle that case here. |
480b0c5b GV |
1590 | (Ulrich Leodolter, 1/11/95). */ |
1591 | char * | |
1592 | sys_ctime (const time_t *t) | |
1593 | { | |
1594 | char *str = (char *) ctime (t); | |
1595 | return (str ? str : "Sun Jan 01 00:00:00 1970"); | |
1596 | } | |
1597 | ||
1598 | /* Emulate sleep...we could have done this with a define, but that | |
1599 | would necessitate including windows.h in the files that used it. | |
1600 | This is much easier. */ | |
1601 | void | |
1602 | sys_sleep (int seconds) | |
1603 | { | |
1604 | Sleep (seconds * 1000); | |
1605 | } | |
1606 | ||
76b3903d | 1607 | /* Internal MSVC functions for low-level descriptor munging */ |
480b0c5b GV |
1608 | extern int __cdecl _set_osfhnd (int fd, long h); |
1609 | extern int __cdecl _free_osfhnd (int fd); | |
1610 | ||
1611 | /* parallel array of private info on file handles */ | |
1612 | filedesc fd_info [ MAXDESC ]; | |
1613 | ||
76b3903d GV |
1614 | typedef struct volume_info_data { |
1615 | struct volume_info_data * next; | |
1616 | ||
1617 | /* time when info was obtained */ | |
1618 | DWORD timestamp; | |
1619 | ||
1620 | /* actual volume info */ | |
1621 | char * root_dir; | |
480b0c5b GV |
1622 | DWORD serialnum; |
1623 | DWORD maxcomp; | |
1624 | DWORD flags; | |
76b3903d GV |
1625 | char * name; |
1626 | char * type; | |
1627 | } volume_info_data; | |
1628 | ||
1629 | /* Global referenced by various functions. */ | |
1630 | static volume_info_data volume_info; | |
1631 | ||
1632 | /* Vector to indicate which drives are local and fixed (for which cached | |
1633 | data never expires). */ | |
1634 | static BOOL fixed_drives[26]; | |
1635 | ||
1636 | /* Consider cached volume information to be stale if older than 10s, | |
1637 | at least for non-local drives. Info for fixed drives is never stale. */ | |
1638 | #define DRIVE_INDEX( c ) ( (c) <= 'Z' ? (c) - 'A' : (c) - 'a' ) | |
1639 | #define VOLINFO_STILL_VALID( root_dir, info ) \ | |
1640 | ( ( isalpha (root_dir[0]) && \ | |
1641 | fixed_drives[ DRIVE_INDEX (root_dir[0]) ] ) \ | |
1642 | || GetTickCount () - info->timestamp < 10000 ) | |
1643 | ||
1644 | /* Cache support functions. */ | |
1645 | ||
1646 | /* Simple linked list with linear search is sufficient. */ | |
1647 | static volume_info_data *volume_cache = NULL; | |
1648 | ||
1649 | static volume_info_data * | |
1650 | lookup_volume_info (char * root_dir) | |
1651 | { | |
1652 | volume_info_data * info; | |
1653 | ||
1654 | for (info = volume_cache; info; info = info->next) | |
05131107 | 1655 | if (xstrcasecmp (info->root_dir, root_dir) == 0) |
76b3903d GV |
1656 | break; |
1657 | return info; | |
1658 | } | |
1659 | ||
1660 | static void | |
1661 | add_volume_info (char * root_dir, volume_info_data * info) | |
1662 | { | |
a302c7ae | 1663 | info->root_dir = xstrdup (root_dir); |
76b3903d GV |
1664 | info->next = volume_cache; |
1665 | volume_cache = info; | |
1666 | } | |
1667 | ||
1668 | ||
1669 | /* Wrapper for GetVolumeInformation, which uses caching to avoid | |
1670 | performance penalty (~2ms on 486 for local drives, 7.5ms for local | |
1671 | cdrom drive, ~5-10ms or more for remote drives on LAN). */ | |
1672 | volume_info_data * | |
1673 | GetCachedVolumeInformation (char * root_dir) | |
1674 | { | |
1675 | volume_info_data * info; | |
1676 | char default_root[ MAX_PATH ]; | |
1677 | ||
1678 | /* NULL for root_dir means use root from current directory. */ | |
1679 | if (root_dir == NULL) | |
1680 | { | |
1681 | if (GetCurrentDirectory (MAX_PATH, default_root) == 0) | |
1682 | return NULL; | |
1683 | parse_root (default_root, &root_dir); | |
1684 | *root_dir = 0; | |
1685 | root_dir = default_root; | |
1686 | } | |
1687 | ||
1688 | /* Local fixed drives can be cached permanently. Removable drives | |
1689 | cannot be cached permanently, since the volume name and serial | |
1690 | number (if nothing else) can change. Remote drives should be | |
1691 | treated as if they are removable, since there is no sure way to | |
1692 | tell whether they are or not. Also, the UNC association of drive | |
1693 | letters mapped to remote volumes can be changed at any time (even | |
1694 | by other processes) without notice. | |
177c0ea7 | 1695 | |
76b3903d GV |
1696 | As a compromise, so we can benefit from caching info for remote |
1697 | volumes, we use a simple expiry mechanism to invalidate cache | |
1698 | entries that are more than ten seconds old. */ | |
1699 | ||
1700 | #if 0 | |
1701 | /* No point doing this, because WNetGetConnection is even slower than | |
1702 | GetVolumeInformation, consistently taking ~50ms on a 486 (FWIW, | |
1703 | GetDriveType is about the only call of this type which does not | |
1704 | involve network access, and so is extremely quick). */ | |
1705 | ||
1706 | /* Map drive letter to UNC if remote. */ | |
1707 | if ( isalpha( root_dir[0] ) && !fixed[ DRIVE_INDEX( root_dir[0] ) ] ) | |
1708 | { | |
1709 | char remote_name[ 256 ]; | |
1710 | char drive[3] = { root_dir[0], ':' }; | |
1711 | ||
1712 | if (WNetGetConnection (drive, remote_name, sizeof (remote_name)) | |
1713 | == NO_ERROR) | |
1714 | /* do something */ ; | |
1715 | } | |
1716 | #endif | |
1717 | ||
1718 | info = lookup_volume_info (root_dir); | |
1719 | ||
1720 | if (info == NULL || ! VOLINFO_STILL_VALID (root_dir, info)) | |
1721 | { | |
1722 | char name[ 256 ]; | |
1723 | DWORD serialnum; | |
1724 | DWORD maxcomp; | |
1725 | DWORD flags; | |
1726 | char type[ 256 ]; | |
1727 | ||
1728 | /* Info is not cached, or is stale. */ | |
1729 | if (!GetVolumeInformation (root_dir, | |
1730 | name, sizeof (name), | |
1731 | &serialnum, | |
1732 | &maxcomp, | |
1733 | &flags, | |
1734 | type, sizeof (type))) | |
1735 | return NULL; | |
1736 | ||
1737 | /* Cache the volume information for future use, overwriting existing | |
1738 | entry if present. */ | |
1739 | if (info == NULL) | |
1740 | { | |
1741 | info = (volume_info_data *) xmalloc (sizeof (volume_info_data)); | |
1742 | add_volume_info (root_dir, info); | |
1743 | } | |
1744 | else | |
1745 | { | |
a302c7ae AI |
1746 | xfree (info->name); |
1747 | xfree (info->type); | |
76b3903d GV |
1748 | } |
1749 | ||
a302c7ae | 1750 | info->name = xstrdup (name); |
76b3903d GV |
1751 | info->serialnum = serialnum; |
1752 | info->maxcomp = maxcomp; | |
1753 | info->flags = flags; | |
a302c7ae | 1754 | info->type = xstrdup (type); |
76b3903d GV |
1755 | info->timestamp = GetTickCount (); |
1756 | } | |
1757 | ||
1758 | return info; | |
1759 | } | |
480b0c5b GV |
1760 | |
1761 | /* Get information on the volume where name is held; set path pointer to | |
1762 | start of pathname in name (past UNC header\volume header if present). */ | |
1763 | int | |
1764 | get_volume_info (const char * name, const char ** pPath) | |
95ed0025 | 1765 | { |
480b0c5b GV |
1766 | char temp[MAX_PATH]; |
1767 | char *rootname = NULL; /* default to current volume */ | |
76b3903d | 1768 | volume_info_data * info; |
480b0c5b GV |
1769 | |
1770 | if (name == NULL) | |
1771 | return FALSE; | |
1772 | ||
1773 | /* find the root name of the volume if given */ | |
1774 | if (isalpha (name[0]) && name[1] == ':') | |
1775 | { | |
1776 | rootname = temp; | |
1777 | temp[0] = *name++; | |
1778 | temp[1] = *name++; | |
1779 | temp[2] = '\\'; | |
1780 | temp[3] = 0; | |
1781 | } | |
1782 | else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1])) | |
95ed0025 | 1783 | { |
480b0c5b GV |
1784 | char *str = temp; |
1785 | int slashes = 4; | |
1786 | rootname = temp; | |
1787 | do | |
1788 | { | |
1789 | if (IS_DIRECTORY_SEP (*name) && --slashes == 0) | |
1790 | break; | |
1791 | *str++ = *name++; | |
1792 | } | |
1793 | while ( *name ); | |
1794 | ||
480b0c5b GV |
1795 | *str++ = '\\'; |
1796 | *str = 0; | |
95ed0025 | 1797 | } |
480b0c5b GV |
1798 | |
1799 | if (pPath) | |
1800 | *pPath = name; | |
177c0ea7 | 1801 | |
76b3903d GV |
1802 | info = GetCachedVolumeInformation (rootname); |
1803 | if (info != NULL) | |
95ed0025 | 1804 | { |
76b3903d GV |
1805 | /* Set global referenced by other functions. */ |
1806 | volume_info = *info; | |
480b0c5b | 1807 | return TRUE; |
95ed0025 | 1808 | } |
480b0c5b GV |
1809 | return FALSE; |
1810 | } | |
1811 | ||
1812 | /* Determine if volume is FAT format (ie. only supports short 8.3 | |
1813 | names); also set path pointer to start of pathname in name. */ | |
1814 | int | |
1815 | is_fat_volume (const char * name, const char ** pPath) | |
1816 | { | |
1817 | if (get_volume_info (name, pPath)) | |
1818 | return (volume_info.maxcomp == 12); | |
1819 | return FALSE; | |
1820 | } | |
1821 | ||
4d90eee4 | 1822 | /* Map filename to a valid 8.3 name if necessary. */ |
480b0c5b | 1823 | const char * |
fbd6baed | 1824 | map_w32_filename (const char * name, const char ** pPath) |
480b0c5b GV |
1825 | { |
1826 | static char shortname[MAX_PATH]; | |
1827 | char * str = shortname; | |
1828 | char c; | |
480b0c5b | 1829 | char * path; |
76b3903d | 1830 | const char * save_name = name; |
480b0c5b | 1831 | |
ca149beb AI |
1832 | if (strlen (name) >= MAX_PATH) |
1833 | { | |
1834 | /* Return a filename which will cause callers to fail. */ | |
1835 | strcpy (shortname, "?"); | |
1836 | return shortname; | |
1837 | } | |
1838 | ||
a302c7ae | 1839 | if (is_fat_volume (name, (const char **)&path)) /* truncate to 8.3 */ |
95ed0025 | 1840 | { |
480b0c5b GV |
1841 | register int left = 8; /* maximum number of chars in part */ |
1842 | register int extn = 0; /* extension added? */ | |
1843 | register int dots = 2; /* maximum number of dots allowed */ | |
1844 | ||
1845 | while (name < path) | |
1846 | *str++ = *name++; /* skip past UNC header */ | |
1847 | ||
1848 | while ((c = *name++)) | |
1849 | { | |
1850 | switch ( c ) | |
1851 | { | |
1852 | case '\\': | |
1853 | case '/': | |
1854 | *str++ = '\\'; | |
1855 | extn = 0; /* reset extension flags */ | |
1856 | dots = 2; /* max 2 dots */ | |
1857 | left = 8; /* max length 8 for main part */ | |
1858 | break; | |
1859 | case ':': | |
1860 | *str++ = ':'; | |
1861 | extn = 0; /* reset extension flags */ | |
1862 | dots = 2; /* max 2 dots */ | |
1863 | left = 8; /* max length 8 for main part */ | |
1864 | break; | |
1865 | case '.': | |
1866 | if ( dots ) | |
1867 | { | |
1868 | /* Convert path components of the form .xxx to _xxx, | |
1869 | but leave . and .. as they are. This allows .emacs | |
1870 | to be read as _emacs, for example. */ | |
1871 | ||
1872 | if (! *name || | |
1873 | *name == '.' || | |
1874 | IS_DIRECTORY_SEP (*name)) | |
1875 | { | |
1876 | *str++ = '.'; | |
1877 | dots--; | |
1878 | } | |
1879 | else | |
1880 | { | |
1881 | *str++ = '_'; | |
1882 | left--; | |
1883 | dots = 0; | |
1884 | } | |
1885 | } | |
1886 | else if ( !extn ) | |
1887 | { | |
1888 | *str++ = '.'; | |
1889 | extn = 1; /* we've got an extension */ | |
1890 | left = 3; /* 3 chars in extension */ | |
1891 | } | |
1892 | else | |
1893 | { | |
1894 | /* any embedded dots after the first are converted to _ */ | |
1895 | *str++ = '_'; | |
1896 | } | |
1897 | break; | |
1898 | case '~': | |
1899 | case '#': /* don't lose these, they're important */ | |
1900 | if ( ! left ) | |
1901 | str[-1] = c; /* replace last character of part */ | |
1902 | /* FALLTHRU */ | |
1903 | default: | |
1904 | if ( left ) | |
1905 | { | |
1906 | *str++ = tolower (c); /* map to lower case (looks nicer) */ | |
1907 | left--; | |
1908 | dots = 0; /* started a path component */ | |
1909 | } | |
1910 | break; | |
1911 | } | |
1912 | } | |
1913 | *str = '\0'; | |
fc85cb29 RS |
1914 | } |
1915 | else | |
1916 | { | |
1917 | strcpy (shortname, name); | |
1918 | unixtodos_filename (shortname); | |
95ed0025 | 1919 | } |
480b0c5b GV |
1920 | |
1921 | if (pPath) | |
76b3903d | 1922 | *pPath = shortname + (path - save_name); |
480b0c5b | 1923 | |
fc85cb29 | 1924 | return shortname; |
480b0c5b GV |
1925 | } |
1926 | ||
b3308d2e KH |
1927 | static int |
1928 | is_exec (const char * name) | |
1929 | { | |
1930 | char * p = strrchr (name, '.'); | |
1931 | return | |
1932 | (p != NULL | |
05131107 JR |
1933 | && (xstrcasecmp (p, ".exe") == 0 || |
1934 | xstrcasecmp (p, ".com") == 0 || | |
1935 | xstrcasecmp (p, ".bat") == 0 || | |
1936 | xstrcasecmp (p, ".cmd") == 0)); | |
b3308d2e KH |
1937 | } |
1938 | ||
177c0ea7 | 1939 | /* Emulate the Unix directory procedures opendir, closedir, |
76b3903d GV |
1940 | and readdir. We can't use the procedures supplied in sysdep.c, |
1941 | so we provide them here. */ | |
1942 | ||
1943 | struct direct dir_static; /* simulated directory contents */ | |
1944 | static HANDLE dir_find_handle = INVALID_HANDLE_VALUE; | |
1945 | static int dir_is_fat; | |
1946 | static char dir_pathname[MAXPATHLEN+1]; | |
1947 | static WIN32_FIND_DATA dir_find_data; | |
1948 | ||
9d3355d1 GV |
1949 | /* Support shares on a network resource as subdirectories of a read-only |
1950 | root directory. */ | |
1951 | static HANDLE wnet_enum_handle = INVALID_HANDLE_VALUE; | |
e0c181dd | 1952 | HANDLE open_unc_volume (const char *); |
9d3355d1 GV |
1953 | char *read_unc_volume (HANDLE, char *, int); |
1954 | void close_unc_volume (HANDLE); | |
1955 | ||
76b3903d GV |
1956 | DIR * |
1957 | opendir (char *filename) | |
1958 | { | |
1959 | DIR *dirp; | |
1960 | ||
1961 | /* Opening is done by FindFirstFile. However, a read is inherent to | |
1962 | this operation, so we defer the open until read time. */ | |
1963 | ||
76b3903d GV |
1964 | if (dir_find_handle != INVALID_HANDLE_VALUE) |
1965 | return NULL; | |
9d3355d1 GV |
1966 | if (wnet_enum_handle != INVALID_HANDLE_VALUE) |
1967 | return NULL; | |
1968 | ||
1969 | if (is_unc_volume (filename)) | |
1970 | { | |
1971 | wnet_enum_handle = open_unc_volume (filename); | |
1972 | if (wnet_enum_handle == INVALID_HANDLE_VALUE) | |
1973 | return NULL; | |
1974 | } | |
1975 | ||
1976 | if (!(dirp = (DIR *) malloc (sizeof (DIR)))) | |
1977 | return NULL; | |
76b3903d GV |
1978 | |
1979 | dirp->dd_fd = 0; | |
1980 | dirp->dd_loc = 0; | |
1981 | dirp->dd_size = 0; | |
1982 | ||
1983 | strncpy (dir_pathname, map_w32_filename (filename, NULL), MAXPATHLEN); | |
1984 | dir_pathname[MAXPATHLEN] = '\0'; | |
1985 | dir_is_fat = is_fat_volume (filename, NULL); | |
1986 | ||
1987 | return dirp; | |
1988 | } | |
1989 | ||
1990 | void | |
1991 | closedir (DIR *dirp) | |
1992 | { | |
1993 | /* If we have a find-handle open, close it. */ | |
1994 | if (dir_find_handle != INVALID_HANDLE_VALUE) | |
1995 | { | |
1996 | FindClose (dir_find_handle); | |
1997 | dir_find_handle = INVALID_HANDLE_VALUE; | |
1998 | } | |
9d3355d1 GV |
1999 | else if (wnet_enum_handle != INVALID_HANDLE_VALUE) |
2000 | { | |
2001 | close_unc_volume (wnet_enum_handle); | |
2002 | wnet_enum_handle = INVALID_HANDLE_VALUE; | |
2003 | } | |
76b3903d GV |
2004 | xfree ((char *) dirp); |
2005 | } | |
2006 | ||
2007 | struct direct * | |
2008 | readdir (DIR *dirp) | |
2009 | { | |
b07103dc EZ |
2010 | int downcase = !NILP (Vw32_downcase_file_names); |
2011 | ||
9d3355d1 GV |
2012 | if (wnet_enum_handle != INVALID_HANDLE_VALUE) |
2013 | { | |
177c0ea7 JB |
2014 | if (!read_unc_volume (wnet_enum_handle, |
2015 | dir_find_data.cFileName, | |
9d3355d1 GV |
2016 | MAX_PATH)) |
2017 | return NULL; | |
2018 | } | |
76b3903d | 2019 | /* If we aren't dir_finding, do a find-first, otherwise do a find-next. */ |
9d3355d1 | 2020 | else if (dir_find_handle == INVALID_HANDLE_VALUE) |
76b3903d GV |
2021 | { |
2022 | char filename[MAXNAMLEN + 3]; | |
2023 | int ln; | |
2024 | ||
2025 | strcpy (filename, dir_pathname); | |
2026 | ln = strlen (filename) - 1; | |
2027 | if (!IS_DIRECTORY_SEP (filename[ln])) | |
2028 | strcat (filename, "\\"); | |
2029 | strcat (filename, "*"); | |
2030 | ||
2031 | dir_find_handle = FindFirstFile (filename, &dir_find_data); | |
2032 | ||
2033 | if (dir_find_handle == INVALID_HANDLE_VALUE) | |
2034 | return NULL; | |
2035 | } | |
2036 | else | |
2037 | { | |
2038 | if (!FindNextFile (dir_find_handle, &dir_find_data)) | |
2039 | return NULL; | |
2040 | } | |
177c0ea7 | 2041 | |
76b3903d GV |
2042 | /* Emacs never uses this value, so don't bother making it match |
2043 | value returned by stat(). */ | |
2044 | dir_static.d_ino = 1; | |
177c0ea7 | 2045 | |
b07103dc EZ |
2046 | strcpy (dir_static.d_name, dir_find_data.cFileName); |
2047 | ||
2048 | /* If the file name in cFileName[] includes `?' characters, it means | |
2049 | the original file name used characters that cannot be represented | |
2050 | by the current ANSI codepage. To avoid total lossage, retrieve | |
2051 | the short 8+3 alias of the long file name. */ | |
2052 | if (_mbspbrk (dir_static.d_name, "?")) | |
2053 | { | |
2054 | strcpy (dir_static.d_name, dir_find_data.cAlternateFileName); | |
2055 | downcase = 1; /* 8+3 aliases are returned in all caps */ | |
2056 | } | |
2057 | dir_static.d_namlen = strlen (dir_static.d_name); | |
76b3903d GV |
2058 | dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3 + |
2059 | dir_static.d_namlen - dir_static.d_namlen % 4; | |
177c0ea7 | 2060 | |
192788d7 EZ |
2061 | /* If the file name in cFileName[] includes `?' characters, it means |
2062 | the original file name used characters that cannot be represented | |
2063 | by the current ANSI codepage. To avoid total lossage, retrieve | |
2064 | the short 8+3 alias of the long file name. */ | |
2065 | if (_mbspbrk (dir_find_data.cFileName, "?")) | |
2066 | { | |
2067 | strcpy (dir_static.d_name, dir_find_data.cAlternateFileName); | |
2068 | /* 8+3 aliases are returned in all caps, which could break | |
2069 | various alists that look at filenames' extensions. */ | |
2070 | downcase = 1; | |
2071 | } | |
2072 | else | |
2073 | strcpy (dir_static.d_name, dir_find_data.cFileName); | |
2074 | dir_static.d_namlen = strlen (dir_static.d_name); | |
76b3903d GV |
2075 | if (dir_is_fat) |
2076 | _strlwr (dir_static.d_name); | |
b07103dc | 2077 | else if (downcase) |
76b3903d GV |
2078 | { |
2079 | register char *p; | |
2080 | for (p = dir_static.d_name; *p; p++) | |
2081 | if (*p >= 'a' && *p <= 'z') | |
2082 | break; | |
2083 | if (!*p) | |
2084 | _strlwr (dir_static.d_name); | |
2085 | } | |
177c0ea7 | 2086 | |
76b3903d GV |
2087 | return &dir_static; |
2088 | } | |
2089 | ||
9d3355d1 | 2090 | HANDLE |
e0c181dd | 2091 | open_unc_volume (const char *path) |
9d3355d1 | 2092 | { |
177c0ea7 | 2093 | NETRESOURCE nr; |
9d3355d1 GV |
2094 | HANDLE henum; |
2095 | int result; | |
2096 | ||
177c0ea7 JB |
2097 | nr.dwScope = RESOURCE_GLOBALNET; |
2098 | nr.dwType = RESOURCETYPE_DISK; | |
2099 | nr.dwDisplayType = RESOURCEDISPLAYTYPE_SERVER; | |
2100 | nr.dwUsage = RESOURCEUSAGE_CONTAINER; | |
2101 | nr.lpLocalName = NULL; | |
6e602566 | 2102 | nr.lpRemoteName = (LPSTR)map_w32_filename (path, NULL); |
177c0ea7 JB |
2103 | nr.lpComment = NULL; |
2104 | nr.lpProvider = NULL; | |
9d3355d1 | 2105 | |
177c0ea7 | 2106 | result = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, |
9d3355d1 GV |
2107 | RESOURCEUSAGE_CONNECTABLE, &nr, &henum); |
2108 | ||
2109 | if (result == NO_ERROR) | |
2110 | return henum; | |
2111 | else | |
2112 | return INVALID_HANDLE_VALUE; | |
2113 | } | |
2114 | ||
2115 | char * | |
2116 | read_unc_volume (HANDLE henum, char *readbuf, int size) | |
2117 | { | |
a302c7ae | 2118 | DWORD count; |
9d3355d1 | 2119 | int result; |
a302c7ae | 2120 | DWORD bufsize = 512; |
9d3355d1 GV |
2121 | char *buffer; |
2122 | char *ptr; | |
2123 | ||
2124 | count = 1; | |
2125 | buffer = alloca (bufsize); | |
2126 | result = WNetEnumResource (wnet_enum_handle, &count, buffer, &bufsize); | |
2127 | if (result != NO_ERROR) | |
2128 | return NULL; | |
2129 | ||
2130 | /* WNetEnumResource returns \\resource\share...skip forward to "share". */ | |
2131 | ptr = ((LPNETRESOURCE) buffer)->lpRemoteName; | |
2132 | ptr += 2; | |
2133 | while (*ptr && !IS_DIRECTORY_SEP (*ptr)) ptr++; | |
2134 | ptr++; | |
2135 | ||
2136 | strncpy (readbuf, ptr, size); | |
2137 | return readbuf; | |
2138 | } | |
2139 | ||
2140 | void | |
2141 | close_unc_volume (HANDLE henum) | |
2142 | { | |
2143 | if (henum != INVALID_HANDLE_VALUE) | |
2144 | WNetCloseEnum (henum); | |
2145 | } | |
2146 | ||
2147 | DWORD | |
e0c181dd | 2148 | unc_volume_file_attributes (const char *path) |
9d3355d1 GV |
2149 | { |
2150 | HANDLE henum; | |
2151 | DWORD attrs; | |
2152 | ||
2153 | henum = open_unc_volume (path); | |
2154 | if (henum == INVALID_HANDLE_VALUE) | |
2155 | return -1; | |
2156 | ||
2157 | attrs = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_DIRECTORY; | |
2158 | ||
2159 | close_unc_volume (henum); | |
2160 | ||
2161 | return attrs; | |
2162 | } | |
2163 | ||
302d7d54 JR |
2164 | /* Ensure a network connection is authenticated. */ |
2165 | static void | |
2166 | logon_network_drive (const char *path) | |
2167 | { | |
2168 | NETRESOURCE resource; | |
2169 | char share[MAX_PATH]; | |
2170 | int i, n_slashes; | |
40a339c8 JR |
2171 | char drive[4]; |
2172 | ||
2173 | sprintf (drive, "%c:\\", path[0]); | |
302d7d54 JR |
2174 | |
2175 | /* Only logon to networked drives. */ | |
40a339c8 JR |
2176 | if ((!IS_DIRECTORY_SEP (path[0]) || !IS_DIRECTORY_SEP (path[1])) |
2177 | && GetDriveType (drive) != DRIVE_REMOTE) | |
302d7d54 | 2178 | return; |
40a339c8 | 2179 | |
302d7d54 JR |
2180 | n_slashes = 2; |
2181 | strncpy (share, path, MAX_PATH); | |
2182 | /* Truncate to just server and share name. */ | |
2183 | for (i = 2; i < MAX_PATH; i++) | |
2184 | { | |
2185 | if (IS_DIRECTORY_SEP (share[i]) && ++n_slashes > 3) | |
2186 | { | |
2187 | share[i] = '\0'; | |
2188 | break; | |
2189 | } | |
2190 | } | |
2191 | ||
2192 | resource.dwType = RESOURCETYPE_DISK; | |
2193 | resource.lpLocalName = NULL; | |
2194 | resource.lpRemoteName = share; | |
2195 | resource.lpProvider = NULL; | |
2196 | ||
2197 | WNetAddConnection2 (&resource, NULL, NULL, CONNECT_INTERACTIVE); | |
2198 | } | |
480b0c5b GV |
2199 | |
2200 | /* Shadow some MSVC runtime functions to map requests for long filenames | |
2201 | to reasonable short names if necessary. This was originally added to | |
177c0ea7 | 2202 | permit running Emacs on NT 3.1 on a FAT partition, which doesn't support |
480b0c5b GV |
2203 | long file names. */ |
2204 | ||
2205 | int | |
2206 | sys_access (const char * path, int mode) | |
2207 | { | |
b3308d2e KH |
2208 | DWORD attributes; |
2209 | ||
2210 | /* MSVC implementation doesn't recognize D_OK. */ | |
2211 | path = map_w32_filename (path, NULL); | |
9d3355d1 GV |
2212 | if (is_unc_volume (path)) |
2213 | { | |
2214 | attributes = unc_volume_file_attributes (path); | |
2215 | if (attributes == -1) { | |
2216 | errno = EACCES; | |
2217 | return -1; | |
2218 | } | |
2219 | } | |
2220 | else if ((attributes = GetFileAttributes (path)) == -1) | |
b3308d2e KH |
2221 | { |
2222 | /* Should try mapping GetLastError to errno; for now just indicate | |
2223 | that path doesn't exist. */ | |
2224 | errno = EACCES; | |
2225 | return -1; | |
2226 | } | |
2227 | if ((mode & X_OK) != 0 && !is_exec (path)) | |
2228 | { | |
2229 | errno = EACCES; | |
2230 | return -1; | |
2231 | } | |
2232 | if ((mode & W_OK) != 0 && (attributes & FILE_ATTRIBUTE_READONLY) != 0) | |
2233 | { | |
2234 | errno = EACCES; | |
2235 | return -1; | |
2236 | } | |
2237 | if ((mode & D_OK) != 0 && (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0) | |
2238 | { | |
2239 | errno = EACCES; | |
2240 | return -1; | |
2241 | } | |
2242 | return 0; | |
480b0c5b GV |
2243 | } |
2244 | ||
2245 | int | |
2246 | sys_chdir (const char * path) | |
2247 | { | |
fbd6baed | 2248 | return _chdir (map_w32_filename (path, NULL)); |
480b0c5b GV |
2249 | } |
2250 | ||
2251 | int | |
2252 | sys_chmod (const char * path, int mode) | |
2253 | { | |
fbd6baed | 2254 | return _chmod (map_w32_filename (path, NULL), mode); |
480b0c5b GV |
2255 | } |
2256 | ||
2d5ab4bf EZ |
2257 | int |
2258 | sys_chown (const char *path, uid_t owner, gid_t group) | |
2259 | { | |
e3b88685 | 2260 | if (sys_chmod (path, S_IREAD) == -1) /* check if file exists */ |
2d5ab4bf EZ |
2261 | return -1; |
2262 | return 0; | |
2263 | } | |
2264 | ||
480b0c5b GV |
2265 | int |
2266 | sys_creat (const char * path, int mode) | |
2267 | { | |
fbd6baed | 2268 | return _creat (map_w32_filename (path, NULL), mode); |
480b0c5b GV |
2269 | } |
2270 | ||
2271 | FILE * | |
2272 | sys_fopen(const char * path, const char * mode) | |
2273 | { | |
2274 | int fd; | |
2275 | int oflag; | |
2276 | const char * mode_save = mode; | |
2277 | ||
2278 | /* Force all file handles to be non-inheritable. This is necessary to | |
2279 | ensure child processes don't unwittingly inherit handles that might | |
2280 | prevent future file access. */ | |
2281 | ||
2282 | if (mode[0] == 'r') | |
2283 | oflag = O_RDONLY; | |
2284 | else if (mode[0] == 'w' || mode[0] == 'a') | |
2285 | oflag = O_WRONLY | O_CREAT | O_TRUNC; | |
95ed0025 | 2286 | else |
480b0c5b GV |
2287 | return NULL; |
2288 | ||
2289 | /* Only do simplistic option parsing. */ | |
2290 | while (*++mode) | |
2291 | if (mode[0] == '+') | |
2292 | { | |
2293 | oflag &= ~(O_RDONLY | O_WRONLY); | |
2294 | oflag |= O_RDWR; | |
2295 | } | |
2296 | else if (mode[0] == 'b') | |
2297 | { | |
2298 | oflag &= ~O_TEXT; | |
2299 | oflag |= O_BINARY; | |
2300 | } | |
2301 | else if (mode[0] == 't') | |
2302 | { | |
2303 | oflag &= ~O_BINARY; | |
2304 | oflag |= O_TEXT; | |
2305 | } | |
2306 | else break; | |
2307 | ||
fbd6baed | 2308 | fd = _open (map_w32_filename (path, NULL), oflag | _O_NOINHERIT, 0644); |
480b0c5b GV |
2309 | if (fd < 0) |
2310 | return NULL; | |
2311 | ||
76b3903d | 2312 | return _fdopen (fd, mode_save); |
95ed0025 | 2313 | } |
480b0c5b | 2314 | |
76b3903d | 2315 | /* This only works on NTFS volumes, but is useful to have. */ |
480b0c5b | 2316 | int |
76b3903d | 2317 | sys_link (const char * old, const char * new) |
480b0c5b | 2318 | { |
76b3903d GV |
2319 | HANDLE fileh; |
2320 | int result = -1; | |
2321 | char oldname[MAX_PATH], newname[MAX_PATH]; | |
2322 | ||
2323 | if (old == NULL || new == NULL) | |
2324 | { | |
2325 | errno = ENOENT; | |
2326 | return -1; | |
2327 | } | |
2328 | ||
2329 | strcpy (oldname, map_w32_filename (old, NULL)); | |
2330 | strcpy (newname, map_w32_filename (new, NULL)); | |
2331 | ||
2332 | fileh = CreateFile (oldname, 0, 0, NULL, OPEN_EXISTING, | |
2333 | FILE_FLAG_BACKUP_SEMANTICS, NULL); | |
2334 | if (fileh != INVALID_HANDLE_VALUE) | |
2335 | { | |
2336 | int wlen; | |
2337 | ||
2338 | /* Confusingly, the "alternate" stream name field does not apply | |
2339 | when restoring a hard link, and instead contains the actual | |
2340 | stream data for the link (ie. the name of the link to create). | |
2341 | The WIN32_STREAM_ID structure before the cStreamName field is | |
2342 | the stream header, which is then immediately followed by the | |
2343 | stream data. */ | |
2344 | ||
2345 | struct { | |
2346 | WIN32_STREAM_ID wid; | |
2347 | WCHAR wbuffer[MAX_PATH]; /* extra space for link name */ | |
2348 | } data; | |
2349 | ||
2350 | wlen = MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, newname, -1, | |
2351 | data.wid.cStreamName, MAX_PATH); | |
2352 | if (wlen > 0) | |
2353 | { | |
2354 | LPVOID context = NULL; | |
2355 | DWORD wbytes = 0; | |
2356 | ||
2357 | data.wid.dwStreamId = BACKUP_LINK; | |
2358 | data.wid.dwStreamAttributes = 0; | |
2359 | data.wid.Size.LowPart = wlen * sizeof(WCHAR); | |
2360 | data.wid.Size.HighPart = 0; | |
2361 | data.wid.dwStreamNameSize = 0; | |
2362 | ||
2363 | if (BackupWrite (fileh, (LPBYTE)&data, | |
2364 | offsetof (WIN32_STREAM_ID, cStreamName) | |
2365 | + data.wid.Size.LowPart, | |
2366 | &wbytes, FALSE, FALSE, &context) | |
2367 | && BackupWrite (fileh, NULL, 0, &wbytes, TRUE, FALSE, &context)) | |
2368 | { | |
2369 | /* succeeded */ | |
2370 | result = 0; | |
2371 | } | |
2372 | else | |
2373 | { | |
2374 | /* Should try mapping GetLastError to errno; for now just | |
2375 | indicate a general error (eg. links not supported). */ | |
2376 | errno = EINVAL; // perhaps EMLINK? | |
2377 | } | |
2378 | } | |
2379 | ||
2380 | CloseHandle (fileh); | |
2381 | } | |
2382 | else | |
2383 | errno = ENOENT; | |
2384 | ||
2385 | return result; | |
480b0c5b GV |
2386 | } |
2387 | ||
2388 | int | |
2389 | sys_mkdir (const char * path) | |
2390 | { | |
fbd6baed | 2391 | return _mkdir (map_w32_filename (path, NULL)); |
480b0c5b GV |
2392 | } |
2393 | ||
9d1778b1 RS |
2394 | /* Because of long name mapping issues, we need to implement this |
2395 | ourselves. Also, MSVC's _mktemp returns NULL when it can't generate | |
2396 | a unique name, instead of setting the input template to an empty | |
2397 | string. | |
2398 | ||
2399 | Standard algorithm seems to be use pid or tid with a letter on the | |
2400 | front (in place of the 6 X's) and cycle through the letters to find a | |
2401 | unique name. We extend that to allow any reasonable character as the | |
2402 | first of the 6 X's. */ | |
480b0c5b GV |
2403 | char * |
2404 | sys_mktemp (char * template) | |
2405 | { | |
9d1778b1 RS |
2406 | char * p; |
2407 | int i; | |
2408 | unsigned uid = GetCurrentThreadId (); | |
2409 | static char first_char[] = "abcdefghijklmnopqrstuvwyz0123456789!%-_@#"; | |
2410 | ||
2411 | if (template == NULL) | |
2412 | return NULL; | |
2413 | p = template + strlen (template); | |
2414 | i = 5; | |
2415 | /* replace up to the last 5 X's with uid in decimal */ | |
2416 | while (--p >= template && p[0] == 'X' && --i >= 0) | |
2417 | { | |
2418 | p[0] = '0' + uid % 10; | |
2419 | uid /= 10; | |
2420 | } | |
2421 | ||
2422 | if (i < 0 && p[0] == 'X') | |
2423 | { | |
2424 | i = 0; | |
2425 | do | |
2426 | { | |
2427 | int save_errno = errno; | |
2428 | p[0] = first_char[i]; | |
2429 | if (sys_access (template, 0) < 0) | |
2430 | { | |
2431 | errno = save_errno; | |
2432 | return template; | |
2433 | } | |
2434 | } | |
2435 | while (++i < sizeof (first_char)); | |
2436 | } | |
2437 | ||
2438 | /* Template is badly formed or else we can't generate a unique name, | |
2439 | so return empty string */ | |
2440 | template[0] = 0; | |
2441 | return template; | |
480b0c5b GV |
2442 | } |
2443 | ||
2444 | int | |
2445 | sys_open (const char * path, int oflag, int mode) | |
2446 | { | |
302f0b29 GM |
2447 | const char* mpath = map_w32_filename (path, NULL); |
2448 | /* Try to open file without _O_CREAT, to be able to write to hidden | |
2449 | and system files. Force all file handles to be | |
2450 | non-inheritable. */ | |
2451 | int res = _open (mpath, (oflag & ~_O_CREAT) | _O_NOINHERIT, mode); | |
2452 | if (res >= 0) | |
2453 | return res; | |
2454 | return _open (mpath, oflag | _O_NOINHERIT, mode); | |
480b0c5b GV |
2455 | } |
2456 | ||
2457 | int | |
2458 | sys_rename (const char * oldname, const char * newname) | |
2459 | { | |
cfb5e855 | 2460 | BOOL result; |
b3308d2e | 2461 | char temp[MAX_PATH]; |
480b0c5b | 2462 | |
e9e23e23 | 2463 | /* MoveFile on Windows 95 doesn't correctly change the short file name |
5162ffce MB |
2464 | alias in a number of circumstances (it is not easy to predict when |
2465 | just by looking at oldname and newname, unfortunately). In these | |
2466 | cases, renaming through a temporary name avoids the problem. | |
2467 | ||
e9e23e23 | 2468 | A second problem on Windows 95 is that renaming through a temp name when |
5162ffce MB |
2469 | newname is uppercase fails (the final long name ends up in |
2470 | lowercase, although the short alias might be uppercase) UNLESS the | |
2471 | long temp name is not 8.3. | |
2472 | ||
e9e23e23 | 2473 | So, on Windows 95 we always rename through a temp name, and we make sure |
5162ffce | 2474 | the temp name has a long extension to ensure correct renaming. */ |
480b0c5b | 2475 | |
fbd6baed | 2476 | strcpy (temp, map_w32_filename (oldname, NULL)); |
480b0c5b | 2477 | |
76b3903d | 2478 | if (os_subtype == OS_WIN95) |
480b0c5b | 2479 | { |
b3308d2e | 2480 | char * o; |
480b0c5b | 2481 | char * p; |
b3308d2e KH |
2482 | int i = 0; |
2483 | ||
2484 | oldname = map_w32_filename (oldname, NULL); | |
2485 | if (o = strrchr (oldname, '\\')) | |
2486 | o++; | |
2487 | else | |
2488 | o = (char *) oldname; | |
480b0c5b | 2489 | |
480b0c5b GV |
2490 | if (p = strrchr (temp, '\\')) |
2491 | p++; | |
2492 | else | |
2493 | p = temp; | |
b3308d2e KH |
2494 | |
2495 | do | |
2496 | { | |
2497 | /* Force temp name to require a manufactured 8.3 alias - this | |
2498 | seems to make the second rename work properly. */ | |
f313ee82 | 2499 | sprintf (p, "_.%s.%u", o, i); |
b3308d2e | 2500 | i++; |
58f0cb7e | 2501 | result = rename (oldname, temp); |
b3308d2e KH |
2502 | } |
2503 | /* This loop must surely terminate! */ | |
cfb5e855 | 2504 | while (result < 0 && errno == EEXIST); |
58f0cb7e | 2505 | if (result < 0) |
480b0c5b GV |
2506 | return -1; |
2507 | } | |
2508 | ||
2509 | /* Emulate Unix behaviour - newname is deleted if it already exists | |
5162ffce | 2510 | (at least if it is a file; don't do this for directories). |
76b3903d | 2511 | |
b3308d2e KH |
2512 | Since we mustn't do this if we are just changing the case of the |
2513 | file name (we would end up deleting the file we are trying to | |
2514 | rename!), we let rename detect if the destination file already | |
2515 | exists - that way we avoid the possible pitfalls of trying to | |
2516 | determine ourselves whether two names really refer to the same | |
2517 | file, which is not always possible in the general case. (Consider | |
2518 | all the permutations of shared or subst'd drives, etc.) */ | |
2519 | ||
2520 | newname = map_w32_filename (newname, NULL); | |
eb9ea53f | 2521 | result = rename (temp, newname); |
b3308d2e KH |
2522 | |
2523 | if (result < 0 | |
cfb5e855 | 2524 | && errno == EEXIST |
b3308d2e KH |
2525 | && _chmod (newname, 0666) == 0 |
2526 | && _unlink (newname) == 0) | |
2527 | result = rename (temp, newname); | |
480b0c5b | 2528 | |
eb9ea53f | 2529 | return result; |
480b0c5b GV |
2530 | } |
2531 | ||
2532 | int | |
2533 | sys_rmdir (const char * path) | |
2534 | { | |
fbd6baed | 2535 | return _rmdir (map_w32_filename (path, NULL)); |
480b0c5b GV |
2536 | } |
2537 | ||
2538 | int | |
2539 | sys_unlink (const char * path) | |
2540 | { | |
16bb7578 GV |
2541 | path = map_w32_filename (path, NULL); |
2542 | ||
2543 | /* On Unix, unlink works without write permission. */ | |
2544 | _chmod (path, 0666); | |
2545 | return _unlink (path); | |
480b0c5b GV |
2546 | } |
2547 | ||
2548 | static FILETIME utc_base_ft; | |
2549 | static long double utc_base; | |
2550 | static int init = 0; | |
2551 | ||
2552 | static time_t | |
2553 | convert_time (FILETIME ft) | |
2554 | { | |
2555 | long double ret; | |
2556 | ||
2557 | if (!init) | |
2558 | { | |
2559 | /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */ | |
2560 | SYSTEMTIME st; | |
2561 | ||
2562 | st.wYear = 1970; | |
2563 | st.wMonth = 1; | |
2564 | st.wDay = 1; | |
2565 | st.wHour = 0; | |
2566 | st.wMinute = 0; | |
2567 | st.wSecond = 0; | |
2568 | st.wMilliseconds = 0; | |
2569 | ||
2570 | SystemTimeToFileTime (&st, &utc_base_ft); | |
2571 | utc_base = (long double) utc_base_ft.dwHighDateTime | |
69b72317 | 2572 | * 4096.0L * 1024.0L * 1024.0L + utc_base_ft.dwLowDateTime; |
480b0c5b GV |
2573 | init = 1; |
2574 | } | |
2575 | ||
2576 | if (CompareFileTime (&ft, &utc_base_ft) < 0) | |
2577 | return 0; | |
2578 | ||
69b72317 EZ |
2579 | ret = (long double) ft.dwHighDateTime |
2580 | * 4096.0L * 1024.0L * 1024.0L + ft.dwLowDateTime; | |
480b0c5b | 2581 | ret -= utc_base; |
69b72317 | 2582 | return (time_t) (ret * 1e-7L); |
480b0c5b GV |
2583 | } |
2584 | ||
480b0c5b GV |
2585 | void |
2586 | convert_from_time_t (time_t time, FILETIME * pft) | |
2587 | { | |
2588 | long double tmp; | |
2589 | ||
2590 | if (!init) | |
2591 | { | |
2592 | /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */ | |
2593 | SYSTEMTIME st; | |
2594 | ||
2595 | st.wYear = 1970; | |
2596 | st.wMonth = 1; | |
2597 | st.wDay = 1; | |
2598 | st.wHour = 0; | |
2599 | st.wMinute = 0; | |
2600 | st.wSecond = 0; | |
2601 | st.wMilliseconds = 0; | |
2602 | ||
2603 | SystemTimeToFileTime (&st, &utc_base_ft); | |
2604 | utc_base = (long double) utc_base_ft.dwHighDateTime | |
2605 | * 4096 * 1024 * 1024 + utc_base_ft.dwLowDateTime; | |
2606 | init = 1; | |
2607 | } | |
2608 | ||
2609 | /* time in 100ns units since 1-Jan-1601 */ | |
2610 | tmp = (long double) time * 1e7 + utc_base; | |
2611 | pft->dwHighDateTime = (DWORD) (tmp / (4096.0 * 1024 * 1024)); | |
16bb7578 | 2612 | pft->dwLowDateTime = (DWORD) (tmp - (4096.0 * 1024 * 1024) * pft->dwHighDateTime); |
480b0c5b | 2613 | } |
480b0c5b | 2614 | |
76b3903d GV |
2615 | #if 0 |
2616 | /* No reason to keep this; faking inode values either by hashing or even | |
2617 | using the file index from GetInformationByHandle, is not perfect and | |
2618 | so by default Emacs doesn't use the inode values on Windows. | |
2619 | Instead, we now determine file-truename correctly (except for | |
2620 | possible drive aliasing etc). */ | |
2621 | ||
2622 | /* Modified version of "PJW" algorithm (see the "Dragon" compiler book). */ | |
480b0c5b | 2623 | static unsigned |
76b3903d | 2624 | hashval (const unsigned char * str) |
480b0c5b GV |
2625 | { |
2626 | unsigned h = 0; | |
480b0c5b GV |
2627 | while (*str) |
2628 | { | |
2629 | h = (h << 4) + *str++; | |
76b3903d | 2630 | h ^= (h >> 28); |
480b0c5b GV |
2631 | } |
2632 | return h; | |
2633 | } | |
2634 | ||
2635 | /* Return the hash value of the canonical pathname, excluding the | |
2636 | drive/UNC header, to get a hopefully unique inode number. */ | |
76b3903d | 2637 | static DWORD |
480b0c5b GV |
2638 | generate_inode_val (const char * name) |
2639 | { | |
2640 | char fullname[ MAX_PATH ]; | |
2641 | char * p; | |
2642 | unsigned hash; | |
2643 | ||
76b3903d GV |
2644 | /* Get the truly canonical filename, if it exists. (Note: this |
2645 | doesn't resolve aliasing due to subst commands, or recognise hard | |
2646 | links. */ | |
2647 | if (!w32_get_long_filename ((char *)name, fullname, MAX_PATH)) | |
2648 | abort (); | |
2649 | ||
2650 | parse_root (fullname, &p); | |
fbd6baed | 2651 | /* Normal W32 filesystems are still case insensitive. */ |
480b0c5b | 2652 | _strlwr (p); |
76b3903d | 2653 | return hashval (p); |
480b0c5b GV |
2654 | } |
2655 | ||
76b3903d GV |
2656 | #endif |
2657 | ||
8aaaec6b EZ |
2658 | static PSECURITY_DESCRIPTOR |
2659 | get_file_security_desc (const char *fname) | |
2660 | { | |
2661 | PSECURITY_DESCRIPTOR psd = NULL; | |
2662 | DWORD sd_len, err; | |
2663 | SECURITY_INFORMATION si = OWNER_SECURITY_INFORMATION | |
2664 | | GROUP_SECURITY_INFORMATION /* | DACL_SECURITY_INFORMATION */ ; | |
2665 | ||
2666 | if (!get_file_security (fname, si, psd, 0, &sd_len)) | |
2667 | { | |
2668 | err = GetLastError (); | |
2669 | if (err != ERROR_INSUFFICIENT_BUFFER) | |
2670 | return NULL; | |
2671 | } | |
2672 | ||
2673 | psd = xmalloc (sd_len); | |
2674 | if (!get_file_security (fname, si, psd, sd_len, &sd_len)) | |
2675 | { | |
2676 | xfree (psd); | |
2677 | return NULL; | |
2678 | } | |
2679 | ||
2680 | return psd; | |
2681 | } | |
2682 | ||
2683 | static DWORD | |
2684 | get_rid (PSID sid) | |
2685 | { | |
2686 | unsigned n_subauthorities; | |
2687 | ||
2688 | /* Use the last sub-authority value of the RID, the relative | |
2689 | portion of the SID, as user/group ID. */ | |
2690 | n_subauthorities = *get_sid_sub_authority_count (sid); | |
2691 | if (n_subauthorities < 1) | |
2692 | return 0; /* the "World" RID */ | |
2693 | return *get_sid_sub_authority (sid, n_subauthorities - 1); | |
2694 | } | |
2695 | ||
2696 | #define UID 1 | |
2697 | #define GID 2 | |
2698 | ||
2699 | static int | |
2700 | get_name_and_id (PSECURITY_DESCRIPTOR psd, const char *fname, | |
2701 | int *id, char *nm, int what) | |
2702 | { | |
2703 | PSID sid = NULL; | |
2704 | char machine[MAX_COMPUTERNAME_LENGTH+1]; | |
2705 | BOOL dflt; | |
2706 | SID_NAME_USE ignore; | |
2707 | char name[UNLEN+1]; | |
2708 | DWORD name_len = sizeof (name); | |
2709 | char domain[1024]; | |
2710 | DWORD domain_len = sizeof(domain); | |
2711 | char *mp = NULL; | |
2712 | int use_dflt = 0; | |
2713 | int result; | |
2714 | ||
2715 | if (what == UID) | |
2716 | result = get_security_descriptor_owner (psd, &sid, &dflt); | |
2717 | else if (what == GID) | |
2718 | result = get_security_descriptor_group (psd, &sid, &dflt); | |
2719 | else | |
2720 | result = 0; | |
2721 | ||
2722 | if (!result || !is_valid_sid (sid)) | |
2723 | use_dflt = 1; | |
2724 | else | |
2725 | { | |
2726 | /* If FNAME is a UNC, we need to lookup account on the | |
2727 | specified machine. */ | |
2728 | if (IS_DIRECTORY_SEP (fname[0]) && IS_DIRECTORY_SEP (fname[1]) | |
2729 | && fname[2] != '\0') | |
2730 | { | |
2731 | const char *s; | |
2732 | char *p; | |
2733 | ||
2734 | for (s = fname + 2, p = machine; | |
2735 | *s && !IS_DIRECTORY_SEP (*s); s++, p++) | |
2736 | *p = *s; | |
2737 | *p = '\0'; | |
2738 | mp = machine; | |
2739 | } | |
2740 | ||
2741 | if (!lookup_account_sid (mp, sid, name, &name_len, | |
2742 | domain, &domain_len, &ignore) | |
2743 | || name_len > UNLEN+1) | |
2744 | use_dflt = 1; | |
2745 | else | |
2746 | { | |
2747 | *id = get_rid (sid); | |
2748 | strcpy (nm, name); | |
2749 | } | |
2750 | } | |
2751 | return use_dflt; | |
2752 | } | |
2753 | ||
2754 | static void | |
2755 | get_file_owner_and_group ( | |
2756 | PSECURITY_DESCRIPTOR psd, | |
2757 | const char *fname, | |
2758 | struct stat *st) | |
2759 | { | |
2760 | int dflt_usr = 0, dflt_grp = 0; | |
2761 | ||
2762 | if (!psd) | |
2763 | { | |
2764 | dflt_usr = 1; | |
2765 | dflt_grp = 1; | |
2766 | } | |
2767 | else | |
2768 | { | |
2769 | if (get_name_and_id (psd, fname, &st->st_uid, st->st_uname, UID)) | |
2770 | dflt_usr = 1; | |
2771 | if (get_name_and_id (psd, fname, &st->st_gid, st->st_gname, GID)) | |
2772 | dflt_grp = 1; | |
2773 | } | |
2774 | /* Consider files to belong to current user/group, if we cannot get | |
2775 | more accurate information. */ | |
2776 | if (dflt_usr) | |
2777 | { | |
2778 | st->st_uid = dflt_passwd.pw_uid; | |
2779 | strcpy (st->st_uname, dflt_passwd.pw_name); | |
2780 | } | |
2781 | if (dflt_grp) | |
2782 | { | |
2783 | st->st_gid = dflt_passwd.pw_gid; | |
2784 | strcpy (st->st_gname, dflt_group.gr_name); | |
2785 | } | |
2786 | } | |
2787 | ||
480b0c5b GV |
2788 | /* MSVC stat function can't cope with UNC names and has other bugs, so |
2789 | replace it with our own. This also allows us to calculate consistent | |
2790 | inode values without hacks in the main Emacs code. */ | |
2791 | int | |
2792 | stat (const char * path, struct stat * buf) | |
2793 | { | |
eb9ea53f | 2794 | char *name, *r; |
480b0c5b GV |
2795 | WIN32_FIND_DATA wfd; |
2796 | HANDLE fh; | |
e3b88685 | 2797 | unsigned __int64 fake_inode; |
480b0c5b GV |
2798 | int permission; |
2799 | int len; | |
2800 | int rootdir = FALSE; | |
8aaaec6b | 2801 | PSECURITY_DESCRIPTOR psd = NULL; |
480b0c5b GV |
2802 | |
2803 | if (path == NULL || buf == NULL) | |
2804 | { | |
2805 | errno = EFAULT; | |
2806 | return -1; | |
2807 | } | |
2808 | ||
fbd6baed | 2809 | name = (char *) map_w32_filename (path, &path); |
22189f79 EZ |
2810 | /* Must be valid filename, no wild cards or other invalid |
2811 | characters. We use _mbspbrk to support multibyte strings that | |
2812 | might look to strpbrk as if they included literal *, ?, and other | |
2813 | characters mentioned below that are disallowed by Windows | |
2814 | filesystems. */ | |
2815 | if (_mbspbrk (name, "*?|<>\"")) | |
480b0c5b GV |
2816 | { |
2817 | errno = ENOENT; | |
2818 | return -1; | |
2819 | } | |
2820 | ||
eb9ea53f GV |
2821 | /* If name is "c:/.." or "/.." then stat "c:/" or "/". */ |
2822 | r = IS_DEVICE_SEP (name[1]) ? &name[2] : name; | |
2823 | if (IS_DIRECTORY_SEP (r[0]) && r[1] == '.' && r[2] == '.' && r[3] == '\0') | |
2824 | { | |
2825 | r[1] = r[2] = '\0'; | |
2826 | } | |
2827 | ||
480b0c5b GV |
2828 | /* Remove trailing directory separator, unless name is the root |
2829 | directory of a drive or UNC volume in which case ensure there | |
2830 | is a trailing separator. */ | |
2831 | len = strlen (name); | |
2832 | rootdir = (path >= name + len - 1 | |
2833 | && (IS_DIRECTORY_SEP (*path) || *path == 0)); | |
2834 | name = strcpy (alloca (len + 2), name); | |
2835 | ||
9d3355d1 GV |
2836 | if (is_unc_volume (name)) |
2837 | { | |
2838 | DWORD attrs = unc_volume_file_attributes (name); | |
2839 | ||
2840 | if (attrs == -1) | |
2841 | return -1; | |
2842 | ||
2843 | memset (&wfd, 0, sizeof (wfd)); | |
2844 | wfd.dwFileAttributes = attrs; | |
2845 | wfd.ftCreationTime = utc_base_ft; | |
2846 | wfd.ftLastAccessTime = utc_base_ft; | |
2847 | wfd.ftLastWriteTime = utc_base_ft; | |
2848 | strcpy (wfd.cFileName, name); | |
2849 | } | |
2850 | else if (rootdir) | |
480b0c5b GV |
2851 | { |
2852 | if (!IS_DIRECTORY_SEP (name[len-1])) | |
2853 | strcat (name, "\\"); | |
2854 | if (GetDriveType (name) < 2) | |
2855 | { | |
2856 | errno = ENOENT; | |
2857 | return -1; | |
2858 | } | |
2859 | memset (&wfd, 0, sizeof (wfd)); | |
2860 | wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY; | |
2861 | wfd.ftCreationTime = utc_base_ft; | |
2862 | wfd.ftLastAccessTime = utc_base_ft; | |
2863 | wfd.ftLastWriteTime = utc_base_ft; | |
2864 | strcpy (wfd.cFileName, name); | |
2865 | } | |
2866 | else | |
2867 | { | |
2868 | if (IS_DIRECTORY_SEP (name[len-1])) | |
2869 | name[len - 1] = 0; | |
76b3903d GV |
2870 | |
2871 | /* (This is hacky, but helps when doing file completions on | |
2872 | network drives.) Optimize by using information available from | |
2873 | active readdir if possible. */ | |
b19cc00c GV |
2874 | len = strlen (dir_pathname); |
2875 | if (IS_DIRECTORY_SEP (dir_pathname[len-1])) | |
2876 | len--; | |
76b3903d | 2877 | if (dir_find_handle != INVALID_HANDLE_VALUE |
b19cc00c | 2878 | && strnicmp (name, dir_pathname, len) == 0 |
76b3903d | 2879 | && IS_DIRECTORY_SEP (name[len]) |
05131107 | 2880 | && xstrcasecmp (name + len + 1, dir_static.d_name) == 0) |
480b0c5b | 2881 | { |
76b3903d GV |
2882 | /* This was the last entry returned by readdir. */ |
2883 | wfd = dir_find_data; | |
2884 | } | |
2885 | else | |
2886 | { | |
513feaa5 | 2887 | logon_network_drive (name); |
302d7d54 | 2888 | |
76b3903d GV |
2889 | fh = FindFirstFile (name, &wfd); |
2890 | if (fh == INVALID_HANDLE_VALUE) | |
2891 | { | |
2892 | errno = ENOENT; | |
2893 | return -1; | |
2894 | } | |
2895 | FindClose (fh); | |
480b0c5b | 2896 | } |
480b0c5b GV |
2897 | } |
2898 | ||
8aaaec6b EZ |
2899 | if (!(NILP (Vw32_get_true_file_attributes) |
2900 | || (EQ (Vw32_get_true_file_attributes, Qlocal) && | |
2a85f0c2 | 2901 | GetDriveType (name) != DRIVE_FIXED)) |
93e0f0da JR |
2902 | /* No access rights required to get info. */ |
2903 | && (fh = CreateFile (name, 0, 0, NULL, OPEN_EXISTING, | |
2904 | FILE_FLAG_BACKUP_SEMANTICS, NULL)) | |
2905 | != INVALID_HANDLE_VALUE) | |
480b0c5b | 2906 | { |
480b0c5b | 2907 | /* This is more accurate in terms of gettting the correct number |
aa5ee2a3 | 2908 | of links, but is quite slow (it is noticeable when Emacs is |
480b0c5b GV |
2909 | making a list of file name completions). */ |
2910 | BY_HANDLE_FILE_INFORMATION info; | |
2911 | ||
480b0c5b GV |
2912 | if (GetFileInformationByHandle (fh, &info)) |
2913 | { | |
480b0c5b | 2914 | buf->st_nlink = info.nNumberOfLinks; |
76b3903d GV |
2915 | /* Might as well use file index to fake inode values, but this |
2916 | is not guaranteed to be unique unless we keep a handle open | |
2917 | all the time (even then there are situations where it is | |
2918 | not unique). Reputedly, there are at most 48 bits of info | |
2919 | (on NTFS, presumably less on FAT). */ | |
e3b88685 EZ |
2920 | fake_inode = info.nFileIndexHigh; |
2921 | fake_inode <<= 32; | |
2922 | fake_inode += info.nFileIndexLow; | |
480b0c5b GV |
2923 | } |
2924 | else | |
2925 | { | |
01f31dfb AI |
2926 | buf->st_nlink = 1; |
2927 | fake_inode = 0; | |
2928 | } | |
2929 | ||
93e0f0da | 2930 | if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
01f31dfb | 2931 | { |
e3b88685 | 2932 | buf->st_mode = S_IFDIR; |
93e0f0da JR |
2933 | } |
2934 | else | |
2935 | { | |
2936 | switch (GetFileType (fh)) | |
2937 | { | |
2938 | case FILE_TYPE_DISK: | |
e3b88685 | 2939 | buf->st_mode = S_IFREG; |
93e0f0da JR |
2940 | break; |
2941 | case FILE_TYPE_PIPE: | |
e3b88685 | 2942 | buf->st_mode = S_IFIFO; |
93e0f0da JR |
2943 | break; |
2944 | case FILE_TYPE_CHAR: | |
2945 | case FILE_TYPE_UNKNOWN: | |
2946 | default: | |
e3b88685 | 2947 | buf->st_mode = S_IFCHR; |
93e0f0da | 2948 | } |
480b0c5b | 2949 | } |
01f31dfb | 2950 | CloseHandle (fh); |
8aaaec6b EZ |
2951 | psd = get_file_security_desc (name); |
2952 | get_file_owner_and_group (psd, name, buf); | |
76b3903d GV |
2953 | } |
2954 | else | |
2955 | { | |
2956 | /* Don't bother to make this information more accurate. */ | |
93e0f0da | 2957 | buf->st_mode = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? |
e3b88685 | 2958 | S_IFDIR : S_IFREG; |
480b0c5b | 2959 | buf->st_nlink = 1; |
76b3903d | 2960 | fake_inode = 0; |
8aaaec6b EZ |
2961 | |
2962 | get_file_owner_and_group (NULL, name, buf); | |
76b3903d | 2963 | } |
8aaaec6b EZ |
2964 | if (psd) |
2965 | xfree (psd); | |
76b3903d GV |
2966 | |
2967 | #if 0 | |
2968 | /* Not sure if there is any point in this. */ | |
2969 | if (!NILP (Vw32_generate_fake_inodes)) | |
2970 | fake_inode = generate_inode_val (name); | |
2971 | else if (fake_inode == 0) | |
2972 | { | |
2973 | /* For want of something better, try to make everything unique. */ | |
2974 | static DWORD gen_num = 0; | |
2975 | fake_inode = ++gen_num; | |
480b0c5b | 2976 | } |
76b3903d GV |
2977 | #endif |
2978 | ||
2979 | /* MSVC defines _ino_t to be short; other libc's might not. */ | |
2980 | if (sizeof (buf->st_ino) == 2) | |
2981 | buf->st_ino = fake_inode ^ (fake_inode >> 16); | |
2982 | else | |
2983 | buf->st_ino = fake_inode; | |
480b0c5b | 2984 | |
fbd6baed | 2985 | /* volume_info is set indirectly by map_w32_filename */ |
480b0c5b GV |
2986 | buf->st_dev = volume_info.serialnum; |
2987 | buf->st_rdev = volume_info.serialnum; | |
2988 | ||
480b0c5b | 2989 | |
8aaaec6b EZ |
2990 | buf->st_size = wfd.nFileSizeHigh; |
2991 | buf->st_size <<= 32; | |
2992 | buf->st_size += wfd.nFileSizeLow; | |
480b0c5b GV |
2993 | |
2994 | /* Convert timestamps to Unix format. */ | |
2995 | buf->st_mtime = convert_time (wfd.ftLastWriteTime); | |
2996 | buf->st_atime = convert_time (wfd.ftLastAccessTime); | |
2997 | if (buf->st_atime == 0) buf->st_atime = buf->st_mtime; | |
2998 | buf->st_ctime = convert_time (wfd.ftCreationTime); | |
2999 | if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime; | |
3000 | ||
3001 | /* determine rwx permissions */ | |
3002 | if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) | |
e3b88685 | 3003 | permission = S_IREAD; |
480b0c5b | 3004 | else |
e3b88685 | 3005 | permission = S_IREAD | S_IWRITE; |
177c0ea7 | 3006 | |
480b0c5b | 3007 | if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
e3b88685 | 3008 | permission |= S_IEXEC; |
b3308d2e | 3009 | else if (is_exec (name)) |
e3b88685 | 3010 | permission |= S_IEXEC; |
480b0c5b GV |
3011 | |
3012 | buf->st_mode |= permission | (permission >> 3) | (permission >> 6); | |
3013 | ||
3014 | return 0; | |
3015 | } | |
3016 | ||
16bb7578 GV |
3017 | /* Provide fstat and utime as well as stat for consistent handling of |
3018 | file timestamps. */ | |
3019 | int | |
3020 | fstat (int desc, struct stat * buf) | |
3021 | { | |
3022 | HANDLE fh = (HANDLE) _get_osfhandle (desc); | |
3023 | BY_HANDLE_FILE_INFORMATION info; | |
e3b88685 | 3024 | unsigned __int64 fake_inode; |
16bb7578 GV |
3025 | int permission; |
3026 | ||
3027 | switch (GetFileType (fh) & ~FILE_TYPE_REMOTE) | |
3028 | { | |
3029 | case FILE_TYPE_DISK: | |
e3b88685 | 3030 | buf->st_mode = S_IFREG; |
16bb7578 GV |
3031 | if (!GetFileInformationByHandle (fh, &info)) |
3032 | { | |
3033 | errno = EACCES; | |
3034 | return -1; | |
3035 | } | |
3036 | break; | |
3037 | case FILE_TYPE_PIPE: | |
e3b88685 | 3038 | buf->st_mode = S_IFIFO; |
16bb7578 GV |
3039 | goto non_disk; |
3040 | case FILE_TYPE_CHAR: | |
3041 | case FILE_TYPE_UNKNOWN: | |
3042 | default: | |
e3b88685 | 3043 | buf->st_mode = S_IFCHR; |
16bb7578 GV |
3044 | non_disk: |
3045 | memset (&info, 0, sizeof (info)); | |
3046 | info.dwFileAttributes = 0; | |
3047 | info.ftCreationTime = utc_base_ft; | |
3048 | info.ftLastAccessTime = utc_base_ft; | |
3049 | info.ftLastWriteTime = utc_base_ft; | |
3050 | } | |
3051 | ||
3052 | if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) | |
e3b88685 | 3053 | buf->st_mode = S_IFDIR; |
93e0f0da JR |
3054 | |
3055 | buf->st_nlink = info.nNumberOfLinks; | |
3056 | /* Might as well use file index to fake inode values, but this | |
3057 | is not guaranteed to be unique unless we keep a handle open | |
3058 | all the time (even then there are situations where it is | |
3059 | not unique). Reputedly, there are at most 48 bits of info | |
3060 | (on NTFS, presumably less on FAT). */ | |
e3b88685 EZ |
3061 | fake_inode = info.nFileIndexHigh; |
3062 | fake_inode <<= 32; | |
3063 | fake_inode += info.nFileIndexLow; | |
16bb7578 GV |
3064 | |
3065 | /* MSVC defines _ino_t to be short; other libc's might not. */ | |
3066 | if (sizeof (buf->st_ino) == 2) | |
3067 | buf->st_ino = fake_inode ^ (fake_inode >> 16); | |
3068 | else | |
3069 | buf->st_ino = fake_inode; | |
3070 | ||
8aaaec6b EZ |
3071 | /* Consider files to belong to current user. |
3072 | FIXME: this should use GetSecurityInfo API, but it is only | |
3073 | available for _WIN32_WINNT >= 0x501. */ | |
07f7980a EZ |
3074 | buf->st_uid = dflt_passwd.pw_uid; |
3075 | buf->st_gid = dflt_passwd.pw_gid; | |
8aaaec6b EZ |
3076 | strcpy (buf->st_uname, dflt_passwd.pw_name); |
3077 | strcpy (buf->st_gname, dflt_group.gr_name); | |
16bb7578 GV |
3078 | |
3079 | buf->st_dev = info.dwVolumeSerialNumber; | |
3080 | buf->st_rdev = info.dwVolumeSerialNumber; | |
3081 | ||
8aaaec6b EZ |
3082 | buf->st_size = info.nFileSizeHigh; |
3083 | buf->st_size <<= 32; | |
3084 | buf->st_size += info.nFileSizeLow; | |
16bb7578 GV |
3085 | |
3086 | /* Convert timestamps to Unix format. */ | |
3087 | buf->st_mtime = convert_time (info.ftLastWriteTime); | |
3088 | buf->st_atime = convert_time (info.ftLastAccessTime); | |
3089 | if (buf->st_atime == 0) buf->st_atime = buf->st_mtime; | |
3090 | buf->st_ctime = convert_time (info.ftCreationTime); | |
3091 | if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime; | |
3092 | ||
3093 | /* determine rwx permissions */ | |
3094 | if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) | |
e3b88685 | 3095 | permission = S_IREAD; |
16bb7578 | 3096 | else |
e3b88685 | 3097 | permission = S_IREAD | S_IWRITE; |
177c0ea7 | 3098 | |
16bb7578 | 3099 | if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
e3b88685 | 3100 | permission |= S_IEXEC; |
16bb7578 GV |
3101 | else |
3102 | { | |
3103 | #if 0 /* no way of knowing the filename */ | |
3104 | char * p = strrchr (name, '.'); | |
3105 | if (p != NULL && | |
05131107 JR |
3106 | (xstrcasecmp (p, ".exe") == 0 || |
3107 | xstrcasecmp (p, ".com") == 0 || | |
3108 | xstrcasecmp (p, ".bat") == 0 || | |
3109 | xstrcasecmp (p, ".cmd") == 0)) | |
e3b88685 | 3110 | permission |= S_IEXEC; |
16bb7578 GV |
3111 | #endif |
3112 | } | |
3113 | ||
3114 | buf->st_mode |= permission | (permission >> 3) | (permission >> 6); | |
3115 | ||
3116 | return 0; | |
3117 | } | |
3118 | ||
3119 | int | |
3120 | utime (const char *name, struct utimbuf *times) | |
3121 | { | |
3122 | struct utimbuf deftime; | |
3123 | HANDLE fh; | |
3124 | FILETIME mtime; | |
3125 | FILETIME atime; | |
3126 | ||
3127 | if (times == NULL) | |
3128 | { | |
3129 | deftime.modtime = deftime.actime = time (NULL); | |
3130 | times = &deftime; | |
3131 | } | |
3132 | ||
3133 | /* Need write access to set times. */ | |
3134 | fh = CreateFile (name, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, | |
3135 | 0, OPEN_EXISTING, 0, NULL); | |
3136 | if (fh) | |
3137 | { | |
3138 | convert_from_time_t (times->actime, &atime); | |
3139 | convert_from_time_t (times->modtime, &mtime); | |
3140 | if (!SetFileTime (fh, NULL, &atime, &mtime)) | |
3141 | { | |
3142 | CloseHandle (fh); | |
3143 | errno = EACCES; | |
3144 | return -1; | |
3145 | } | |
3146 | CloseHandle (fh); | |
3147 | } | |
3148 | else | |
3149 | { | |
3150 | errno = EINVAL; | |
3151 | return -1; | |
3152 | } | |
3153 | return 0; | |
3154 | } | |
3155 | ||
480b0c5b GV |
3156 | #ifdef HAVE_SOCKETS |
3157 | ||
3158 | /* Wrappers for winsock functions to map between our file descriptors | |
3159 | and winsock's handles; also set h_errno for convenience. | |
3160 | ||
3161 | To allow Emacs to run on systems which don't have winsock support | |
3162 | installed, we dynamically link to winsock on startup if present, and | |
3163 | otherwise provide the minimum necessary functionality | |
3164 | (eg. gethostname). */ | |
3165 | ||
3166 | /* function pointers for relevant socket functions */ | |
3167 | int (PASCAL *pfn_WSAStartup) (WORD wVersionRequired, LPWSADATA lpWSAData); | |
3168 | void (PASCAL *pfn_WSASetLastError) (int iError); | |
3169 | int (PASCAL *pfn_WSAGetLastError) (void); | |
26fb7bc4 | 3170 | int (PASCAL *pfn_WSAEventSelect) (SOCKET s, HANDLE hEventObject, long lNetworkEvents); |
64570b36 KS |
3171 | HANDLE (PASCAL *pfn_WSACreateEvent) (void); |
3172 | int (PASCAL *pfn_WSACloseEvent) (HANDLE hEvent); | |
480b0c5b GV |
3173 | int (PASCAL *pfn_socket) (int af, int type, int protocol); |
3174 | int (PASCAL *pfn_bind) (SOCKET s, const struct sockaddr *addr, int namelen); | |
3175 | int (PASCAL *pfn_connect) (SOCKET s, const struct sockaddr *addr, int namelen); | |
3176 | int (PASCAL *pfn_ioctlsocket) (SOCKET s, long cmd, u_long *argp); | |
3177 | int (PASCAL *pfn_recv) (SOCKET s, char * buf, int len, int flags); | |
3178 | int (PASCAL *pfn_send) (SOCKET s, const char * buf, int len, int flags); | |
3179 | int (PASCAL *pfn_closesocket) (SOCKET s); | |
3180 | int (PASCAL *pfn_shutdown) (SOCKET s, int how); | |
3181 | int (PASCAL *pfn_WSACleanup) (void); | |
3182 | ||
3183 | u_short (PASCAL *pfn_htons) (u_short hostshort); | |
3184 | u_short (PASCAL *pfn_ntohs) (u_short netshort); | |
3185 | unsigned long (PASCAL *pfn_inet_addr) (const char * cp); | |
3186 | int (PASCAL *pfn_gethostname) (char * name, int namelen); | |
3187 | struct hostent * (PASCAL *pfn_gethostbyname) (const char * name); | |
3188 | struct servent * (PASCAL *pfn_getservbyname) (const char * name, const char * proto); | |
ecd270eb | 3189 | int (PASCAL *pfn_getpeername) (SOCKET s, struct sockaddr *addr, int * namelen); |
962955c5 JR |
3190 | int (PASCAL *pfn_setsockopt) (SOCKET s, int level, int optname, |
3191 | const char * optval, int optlen); | |
3192 | int (PASCAL *pfn_listen) (SOCKET s, int backlog); | |
3193 | int (PASCAL *pfn_getsockname) (SOCKET s, struct sockaddr * name, | |
3194 | int * namelen); | |
3195 | SOCKET (PASCAL *pfn_accept) (SOCKET s, struct sockaddr * addr, int * addrlen); | |
3196 | int (PASCAL *pfn_recvfrom) (SOCKET s, char * buf, int len, int flags, | |
3197 | struct sockaddr * from, int * fromlen); | |
3198 | int (PASCAL *pfn_sendto) (SOCKET s, const char * buf, int len, int flags, | |
3199 | const struct sockaddr * to, int tolen); | |
3200 | ||
f1614061 RS |
3201 | /* SetHandleInformation is only needed to make sockets non-inheritable. */ |
3202 | BOOL (WINAPI *pfn_SetHandleInformation) (HANDLE object, DWORD mask, DWORD flags); | |
3203 | #ifndef HANDLE_FLAG_INHERIT | |
3204 | #define HANDLE_FLAG_INHERIT 1 | |
3205 | #endif | |
480b0c5b | 3206 | |
f249a012 RS |
3207 | HANDLE winsock_lib; |
3208 | static int winsock_inuse; | |
480b0c5b | 3209 | |
f249a012 | 3210 | BOOL |
480b0c5b GV |
3211 | term_winsock (void) |
3212 | { | |
f249a012 | 3213 | if (winsock_lib != NULL && winsock_inuse == 0) |
480b0c5b | 3214 | { |
f249a012 RS |
3215 | /* Not sure what would cause WSAENETDOWN, or even if it can happen |
3216 | after WSAStartup returns successfully, but it seems reasonable | |
3217 | to allow unloading winsock anyway in that case. */ | |
3218 | if (pfn_WSACleanup () == 0 || | |
3219 | pfn_WSAGetLastError () == WSAENETDOWN) | |
3220 | { | |
3221 | if (FreeLibrary (winsock_lib)) | |
3222 | winsock_lib = NULL; | |
3223 | return TRUE; | |
3224 | } | |
480b0c5b | 3225 | } |
f249a012 | 3226 | return FALSE; |
480b0c5b GV |
3227 | } |
3228 | ||
f249a012 RS |
3229 | BOOL |
3230 | init_winsock (int load_now) | |
480b0c5b GV |
3231 | { |
3232 | WSADATA winsockData; | |
3233 | ||
f249a012 RS |
3234 | if (winsock_lib != NULL) |
3235 | return TRUE; | |
f1614061 RS |
3236 | |
3237 | pfn_SetHandleInformation = NULL; | |
3238 | pfn_SetHandleInformation | |
3239 | = (void *) GetProcAddress (GetModuleHandle ("kernel32.dll"), | |
3240 | "SetHandleInformation"); | |
3241 | ||
64570b36 | 3242 | winsock_lib = LoadLibrary ("Ws2_32.dll"); |
480b0c5b GV |
3243 | |
3244 | if (winsock_lib != NULL) | |
3245 | { | |
3246 | /* dynamically link to socket functions */ | |
3247 | ||
3248 | #define LOAD_PROC(fn) \ | |
3249 | if ((pfn_##fn = (void *) GetProcAddress (winsock_lib, #fn)) == NULL) \ | |
3250 | goto fail; | |
3251 | ||
3252 | LOAD_PROC( WSAStartup ); | |
3253 | LOAD_PROC( WSASetLastError ); | |
3254 | LOAD_PROC( WSAGetLastError ); | |
26fb7bc4 | 3255 | LOAD_PROC( WSAEventSelect ); |
64570b36 KS |
3256 | LOAD_PROC( WSACreateEvent ); |
3257 | LOAD_PROC( WSACloseEvent ); | |
480b0c5b GV |
3258 | LOAD_PROC( socket ); |
3259 | LOAD_PROC( bind ); | |
3260 | LOAD_PROC( connect ); | |
3261 | LOAD_PROC( ioctlsocket ); | |
3262 | LOAD_PROC( recv ); | |
3263 | LOAD_PROC( send ); | |
3264 | LOAD_PROC( closesocket ); | |
3265 | LOAD_PROC( shutdown ); | |
3266 | LOAD_PROC( htons ); | |
3267 | LOAD_PROC( ntohs ); | |
3268 | LOAD_PROC( inet_addr ); | |
3269 | LOAD_PROC( gethostname ); | |
3270 | LOAD_PROC( gethostbyname ); | |
3271 | LOAD_PROC( getservbyname ); | |
ecd270eb | 3272 | LOAD_PROC( getpeername ); |
480b0c5b | 3273 | LOAD_PROC( WSACleanup ); |
962955c5 JR |
3274 | LOAD_PROC( setsockopt ); |
3275 | LOAD_PROC( listen ); | |
3276 | LOAD_PROC( getsockname ); | |
3277 | LOAD_PROC( accept ); | |
3278 | LOAD_PROC( recvfrom ); | |
3279 | LOAD_PROC( sendto ); | |
f249a012 RS |
3280 | #undef LOAD_PROC |
3281 | ||
480b0c5b GV |
3282 | /* specify version 1.1 of winsock */ |
3283 | if (pfn_WSAStartup (0x101, &winsockData) == 0) | |
3284 | { | |
f249a012 RS |
3285 | if (winsockData.wVersion != 0x101) |
3286 | goto fail; | |
3287 | ||
3288 | if (!load_now) | |
3289 | { | |
3290 | /* Report that winsock exists and is usable, but leave | |
3291 | socket functions disabled. I am assuming that calling | |
3292 | WSAStartup does not require any network interaction, | |
3293 | and in particular does not cause or require a dial-up | |
3294 | connection to be established. */ | |
3295 | ||
3296 | pfn_WSACleanup (); | |
3297 | FreeLibrary (winsock_lib); | |
3298 | winsock_lib = NULL; | |
3299 | } | |
3300 | winsock_inuse = 0; | |
3301 | return TRUE; | |
480b0c5b GV |
3302 | } |
3303 | ||
3304 | fail: | |
3305 | FreeLibrary (winsock_lib); | |
f249a012 | 3306 | winsock_lib = NULL; |
480b0c5b | 3307 | } |
f249a012 RS |
3308 | |
3309 | return FALSE; | |
480b0c5b GV |
3310 | } |
3311 | ||
3312 | ||
3313 | int h_errno = 0; | |
3314 | ||
3315 | /* function to set h_errno for compatability; map winsock error codes to | |
3316 | normal system codes where they overlap (non-overlapping definitions | |
3317 | are already in <sys/socket.h> */ | |
9bfb11f9 KS |
3318 | static void |
3319 | set_errno () | |
480b0c5b | 3320 | { |
f249a012 | 3321 | if (winsock_lib == NULL) |
480b0c5b GV |
3322 | h_errno = EINVAL; |
3323 | else | |
3324 | h_errno = pfn_WSAGetLastError (); | |
3325 | ||
3326 | switch (h_errno) | |
3327 | { | |
3328 | case WSAEACCES: h_errno = EACCES; break; | |
3329 | case WSAEBADF: h_errno = EBADF; break; | |
3330 | case WSAEFAULT: h_errno = EFAULT; break; | |
3331 | case WSAEINTR: h_errno = EINTR; break; | |
3332 | case WSAEINVAL: h_errno = EINVAL; break; | |
3333 | case WSAEMFILE: h_errno = EMFILE; break; | |
3334 | case WSAENAMETOOLONG: h_errno = ENAMETOOLONG; break; | |
3335 | case WSAENOTEMPTY: h_errno = ENOTEMPTY; break; | |
3336 | } | |
3337 | errno = h_errno; | |
3338 | } | |
3339 | ||
9bfb11f9 KS |
3340 | static void |
3341 | check_errno () | |
480b0c5b | 3342 | { |
f249a012 | 3343 | if (h_errno == 0 && winsock_lib != NULL) |
480b0c5b GV |
3344 | pfn_WSASetLastError (0); |
3345 | } | |
3346 | ||
d8fcc1b9 AI |
3347 | /* Extend strerror to handle the winsock-specific error codes. */ |
3348 | struct { | |
3349 | int errnum; | |
3350 | char * msg; | |
3351 | } _wsa_errlist[] = { | |
3352 | WSAEINTR , "Interrupted function call", | |
3353 | WSAEBADF , "Bad file descriptor", | |
3354 | WSAEACCES , "Permission denied", | |
3355 | WSAEFAULT , "Bad address", | |
3356 | WSAEINVAL , "Invalid argument", | |
3357 | WSAEMFILE , "Too many open files", | |
177c0ea7 | 3358 | |
d8fcc1b9 AI |
3359 | WSAEWOULDBLOCK , "Resource temporarily unavailable", |
3360 | WSAEINPROGRESS , "Operation now in progress", | |
3361 | WSAEALREADY , "Operation already in progress", | |
3362 | WSAENOTSOCK , "Socket operation on non-socket", | |
3363 | WSAEDESTADDRREQ , "Destination address required", | |
3364 | WSAEMSGSIZE , "Message too long", | |
3365 | WSAEPROTOTYPE , "Protocol wrong type for socket", | |
3366 | WSAENOPROTOOPT , "Bad protocol option", | |
3367 | WSAEPROTONOSUPPORT , "Protocol not supported", | |
3368 | WSAESOCKTNOSUPPORT , "Socket type not supported", | |
3369 | WSAEOPNOTSUPP , "Operation not supported", | |
3370 | WSAEPFNOSUPPORT , "Protocol family not supported", | |
3371 | WSAEAFNOSUPPORT , "Address family not supported by protocol family", | |
3372 | WSAEADDRINUSE , "Address already in use", | |
3373 | WSAEADDRNOTAVAIL , "Cannot assign requested address", | |
3374 | WSAENETDOWN , "Network is down", | |
3375 | WSAENETUNREACH , "Network is unreachable", | |
3376 | WSAENETRESET , "Network dropped connection on reset", | |
3377 | WSAECONNABORTED , "Software caused connection abort", | |
3378 | WSAECONNRESET , "Connection reset by peer", | |
3379 | WSAENOBUFS , "No buffer space available", | |
3380 | WSAEISCONN , "Socket is already connected", | |
3381 | WSAENOTCONN , "Socket is not connected", | |
3382 | WSAESHUTDOWN , "Cannot send after socket shutdown", | |
3383 | WSAETOOMANYREFS , "Too many references", /* not sure */ | |
3384 | WSAETIMEDOUT , "Connection timed out", | |
3385 | WSAECONNREFUSED , "Connection refused", | |
3386 | WSAELOOP , "Network loop", /* not sure */ | |
3387 | WSAENAMETOOLONG , "Name is too long", | |
3388 | WSAEHOSTDOWN , "Host is down", | |
3389 | WSAEHOSTUNREACH , "No route to host", | |
3390 | WSAENOTEMPTY , "Buffer not empty", /* not sure */ | |
3391 | WSAEPROCLIM , "Too many processes", | |
3392 | WSAEUSERS , "Too many users", /* not sure */ | |
3393 | WSAEDQUOT , "Double quote in host name", /* really not sure */ | |
3394 | WSAESTALE , "Data is stale", /* not sure */ | |
3395 | WSAEREMOTE , "Remote error", /* not sure */ | |
177c0ea7 | 3396 | |
d8fcc1b9 AI |
3397 | WSASYSNOTREADY , "Network subsystem is unavailable", |
3398 | WSAVERNOTSUPPORTED , "WINSOCK.DLL version out of range", | |
3399 | WSANOTINITIALISED , "Winsock not initialized successfully", | |
3400 | WSAEDISCON , "Graceful shutdown in progress", | |
3401 | #ifdef WSAENOMORE | |
3402 | WSAENOMORE , "No more operations allowed", /* not sure */ | |
3403 | WSAECANCELLED , "Operation cancelled", /* not sure */ | |
3404 | WSAEINVALIDPROCTABLE , "Invalid procedure table from service provider", | |
3405 | WSAEINVALIDPROVIDER , "Invalid service provider version number", | |
3406 | WSAEPROVIDERFAILEDINIT , "Unable to initialize a service provider", | |
aa5ee2a3 | 3407 | WSASYSCALLFAILURE , "System call failure", |
d8fcc1b9 AI |
3408 | WSASERVICE_NOT_FOUND , "Service not found", /* not sure */ |
3409 | WSATYPE_NOT_FOUND , "Class type not found", | |
3410 | WSA_E_NO_MORE , "No more resources available", /* really not sure */ | |
3411 | WSA_E_CANCELLED , "Operation already cancelled", /* really not sure */ | |
3412 | WSAEREFUSED , "Operation refused", /* not sure */ | |
3413 | #endif | |
177c0ea7 | 3414 | |
d8fcc1b9 AI |
3415 | WSAHOST_NOT_FOUND , "Host not found", |
3416 | WSATRY_AGAIN , "Authoritative host not found during name lookup", | |
3417 | WSANO_RECOVERY , "Non-recoverable error during name lookup", | |
3418 | WSANO_DATA , "Valid name, no data record of requested type", | |
3419 | ||
3420 | -1, NULL | |
3421 | }; | |
3422 | ||
3423 | char * | |
3424 | sys_strerror(int error_no) | |
3425 | { | |
3426 | int i; | |
3427 | static char unknown_msg[40]; | |
3428 | ||
a302c7ae AI |
3429 | if (error_no >= 0 && error_no < sys_nerr) |
3430 | return sys_errlist[error_no]; | |
d8fcc1b9 AI |
3431 | |
3432 | for (i = 0; _wsa_errlist[i].errnum >= 0; i++) | |
3433 | if (_wsa_errlist[i].errnum == error_no) | |
3434 | return _wsa_errlist[i].msg; | |
3435 | ||
3436 | sprintf(unknown_msg, "Unidentified error: %d", error_no); | |
3437 | return unknown_msg; | |
3438 | } | |
3439 | ||
480b0c5b GV |
3440 | /* [andrewi 3-May-96] I've had conflicting results using both methods, |
3441 | but I believe the method of keeping the socket handle separate (and | |
3442 | insuring it is not inheritable) is the correct one. */ | |
3443 | ||
3444 | //#define SOCK_REPLACE_HANDLE | |
3445 | ||
3446 | #ifdef SOCK_REPLACE_HANDLE | |
3447 | #define SOCK_HANDLE(fd) ((SOCKET) _get_osfhandle (fd)) | |
3448 | #else | |
3449 | #define SOCK_HANDLE(fd) ((SOCKET) fd_info[fd].hnd) | |
3450 | #endif | |
3451 | ||
962955c5 JR |
3452 | int socket_to_fd (SOCKET s); |
3453 | ||
480b0c5b GV |
3454 | int |
3455 | sys_socket(int af, int type, int protocol) | |
3456 | { | |
962955c5 | 3457 | SOCKET s; |
480b0c5b | 3458 | |
f249a012 | 3459 | if (winsock_lib == NULL) |
480b0c5b GV |
3460 | { |
3461 | h_errno = ENETDOWN; | |
3462 | return INVALID_SOCKET; | |
3463 | } | |
3464 | ||
3465 | check_errno (); | |
3466 | ||
3467 | /* call the real socket function */ | |
962955c5 | 3468 | s = pfn_socket (af, type, protocol); |
177c0ea7 | 3469 | |
480b0c5b | 3470 | if (s != INVALID_SOCKET) |
962955c5 | 3471 | return socket_to_fd (s); |
480b0c5b | 3472 | |
962955c5 JR |
3473 | set_errno (); |
3474 | return -1; | |
3475 | } | |
3476 | ||
3477 | /* Convert a SOCKET to a file descriptor. */ | |
3478 | int | |
3479 | socket_to_fd (SOCKET s) | |
3480 | { | |
3481 | int fd; | |
3482 | child_process * cp; | |
3483 | ||
3484 | /* Although under NT 3.5 _open_osfhandle will accept a socket | |
3485 | handle, if opened with SO_OPENTYPE == SO_SYNCHRONOUS_NONALERT, | |
3486 | that does not work under NT 3.1. However, we can get the same | |
3487 | effect by using a backdoor function to replace an existing | |
3488 | descriptor handle with the one we want. */ | |
3489 | ||
3490 | /* allocate a file descriptor (with appropriate flags) */ | |
3491 | fd = _open ("NUL:", _O_RDWR); | |
3492 | if (fd >= 0) | |
3493 | { | |
480b0c5b | 3494 | #ifdef SOCK_REPLACE_HANDLE |
962955c5 JR |
3495 | /* now replace handle to NUL with our socket handle */ |
3496 | CloseHandle ((HANDLE) _get_osfhandle (fd)); | |
3497 | _free_osfhnd (fd); | |
3498 | _set_osfhnd (fd, s); | |
3499 | /* setmode (fd, _O_BINARY); */ | |
480b0c5b | 3500 | #else |
962955c5 JR |
3501 | /* Make a non-inheritable copy of the socket handle. Note |
3502 | that it is possible that sockets aren't actually kernel | |
3503 | handles, which appears to be the case on Windows 9x when | |
3504 | the MS Proxy winsock client is installed. */ | |
3505 | { | |
3506 | /* Apparently there is a bug in NT 3.51 with some service | |
3507 | packs, which prevents using DuplicateHandle to make a | |
3508 | socket handle non-inheritable (causes WSACleanup to | |
3509 | hang). The work-around is to use SetHandleInformation | |
3510 | instead if it is available and implemented. */ | |
3511 | if (pfn_SetHandleInformation) | |
480b0c5b | 3512 | { |
962955c5 JR |
3513 | pfn_SetHandleInformation ((HANDLE) s, HANDLE_FLAG_INHERIT, 0); |
3514 | } | |
3515 | else | |
3516 | { | |
3517 | HANDLE parent = GetCurrentProcess (); | |
3518 | HANDLE new_s = INVALID_HANDLE_VALUE; | |
3519 | ||
3520 | if (DuplicateHandle (parent, | |
3521 | (HANDLE) s, | |
3522 | parent, | |
3523 | &new_s, | |
3524 | 0, | |
3525 | FALSE, | |
3526 | DUPLICATE_SAME_ACCESS)) | |
f1614061 | 3527 | { |
962955c5 JR |
3528 | /* It is possible that DuplicateHandle succeeds even |
3529 | though the socket wasn't really a kernel handle, | |
3530 | because a real handle has the same value. So | |
3531 | test whether the new handle really is a socket. */ | |
3532 | long nonblocking = 0; | |
3533 | if (pfn_ioctlsocket ((SOCKET) new_s, FIONBIO, &nonblocking) == 0) | |
ca149beb | 3534 | { |
962955c5 JR |
3535 | pfn_closesocket (s); |
3536 | s = (SOCKET) new_s; | |
3537 | } | |
3538 | else | |
3539 | { | |
3540 | CloseHandle (new_s); | |
3541 | } | |
177c0ea7 | 3542 | } |
480b0c5b | 3543 | } |
962955c5 JR |
3544 | } |
3545 | fd_info[fd].hnd = (HANDLE) s; | |
480b0c5b GV |
3546 | #endif |
3547 | ||
962955c5 JR |
3548 | /* set our own internal flags */ |
3549 | fd_info[fd].flags = FILE_SOCKET | FILE_BINARY | FILE_READ | FILE_WRITE; | |
480b0c5b | 3550 | |
962955c5 JR |
3551 | cp = new_child (); |
3552 | if (cp) | |
3553 | { | |
3554 | cp->fd = fd; | |
3555 | cp->status = STATUS_READ_ACKNOWLEDGED; | |
480b0c5b | 3556 | |
962955c5 JR |
3557 | /* attach child_process to fd_info */ |
3558 | if (fd_info[ fd ].cp != NULL) | |
3559 | { | |
3560 | DebPrint (("sys_socket: fd_info[%d] apparently in use!\n", fd)); | |
3561 | abort (); | |
480b0c5b GV |
3562 | } |
3563 | ||
962955c5 JR |
3564 | fd_info[ fd ].cp = cp; |
3565 | ||
3566 | /* success! */ | |
3567 | winsock_inuse++; /* count open sockets */ | |
3568 | return fd; | |
480b0c5b | 3569 | } |
480b0c5b | 3570 | |
962955c5 JR |
3571 | /* clean up */ |
3572 | _close (fd); | |
3573 | } | |
3574 | pfn_closesocket (s); | |
3575 | h_errno = EMFILE; | |
480b0c5b GV |
3576 | return -1; |
3577 | } | |
3578 | ||
3579 | ||
3580 | int | |
3581 | sys_bind (int s, const struct sockaddr * addr, int namelen) | |
3582 | { | |
f249a012 | 3583 | if (winsock_lib == NULL) |
480b0c5b GV |
3584 | { |
3585 | h_errno = ENOTSOCK; | |
3586 | return SOCKET_ERROR; | |
3587 | } | |
3588 | ||
3589 | check_errno (); | |
3590 | if (fd_info[s].flags & FILE_SOCKET) | |
3591 | { | |
3592 | int rc = pfn_bind (SOCK_HANDLE (s), addr, namelen); | |
3593 | if (rc == SOCKET_ERROR) | |
3594 | set_errno (); | |
3595 | return rc; | |
3596 | } | |
3597 | h_errno = ENOTSOCK; | |
3598 | return SOCKET_ERROR; | |
3599 | } | |
3600 | ||
3601 | ||
3602 | int | |
3603 | sys_connect (int s, const struct sockaddr * name, int namelen) | |
3604 | { | |
f249a012 | 3605 | if (winsock_lib == NULL) |
480b0c5b GV |
3606 | { |
3607 | h_errno = ENOTSOCK; | |
3608 | return SOCKET_ERROR; | |
3609 | } | |
3610 | ||
3611 | check_errno (); | |
3612 | if (fd_info[s].flags & FILE_SOCKET) | |
3613 | { | |
3614 | int rc = pfn_connect (SOCK_HANDLE (s), name, namelen); | |
3615 | if (rc == SOCKET_ERROR) | |
3616 | set_errno (); | |
3617 | return rc; | |
3618 | } | |
3619 | h_errno = ENOTSOCK; | |
3620 | return SOCKET_ERROR; | |
3621 | } | |
3622 | ||
3623 | u_short | |
3624 | sys_htons (u_short hostshort) | |
3625 | { | |
f249a012 | 3626 | return (winsock_lib != NULL) ? |
480b0c5b GV |
3627 | pfn_htons (hostshort) : hostshort; |
3628 | } | |
3629 | ||
3630 | u_short | |
3631 | sys_ntohs (u_short netshort) | |
3632 | { | |
f249a012 | 3633 | return (winsock_lib != NULL) ? |
480b0c5b GV |
3634 | pfn_ntohs (netshort) : netshort; |
3635 | } | |
3636 | ||
3637 | unsigned long | |
3638 | sys_inet_addr (const char * cp) | |
3639 | { | |
f249a012 | 3640 | return (winsock_lib != NULL) ? |
480b0c5b GV |
3641 | pfn_inet_addr (cp) : INADDR_NONE; |
3642 | } | |
3643 | ||
3644 | int | |
3645 | sys_gethostname (char * name, int namelen) | |
3646 | { | |
f249a012 | 3647 | if (winsock_lib != NULL) |
480b0c5b GV |
3648 | return pfn_gethostname (name, namelen); |
3649 | ||
3650 | if (namelen > MAX_COMPUTERNAME_LENGTH) | |
a302c7ae | 3651 | return !GetComputerName (name, (DWORD *)&namelen); |
480b0c5b GV |
3652 | |
3653 | h_errno = EFAULT; | |
3654 | return SOCKET_ERROR; | |
3655 | } | |
3656 | ||
3657 | struct hostent * | |
3658 | sys_gethostbyname(const char * name) | |
3659 | { | |
3660 | struct hostent * host; | |
3661 | ||
f249a012 | 3662 | if (winsock_lib == NULL) |
480b0c5b GV |
3663 | { |
3664 | h_errno = ENETDOWN; | |
3665 | return NULL; | |
3666 | } | |
3667 | ||
3668 | check_errno (); | |
3669 | host = pfn_gethostbyname (name); | |
3670 | if (!host) | |
3671 | set_errno (); | |
3672 | return host; | |
3673 | } | |
3674 | ||
3675 | struct servent * | |
3676 | sys_getservbyname(const char * name, const char * proto) | |
3677 | { | |
3678 | struct servent * serv; | |
3679 | ||
f249a012 | 3680 | if (winsock_lib == NULL) |
480b0c5b GV |
3681 | { |
3682 | h_errno = ENETDOWN; | |
3683 | return NULL; | |
3684 | } | |
3685 | ||
3686 | check_errno (); | |
3687 | serv = pfn_getservbyname (name, proto); | |
3688 | if (!serv) | |
3689 | set_errno (); | |
3690 | return serv; | |
3691 | } | |
3692 | ||
ecd270eb JR |
3693 | int |
3694 | sys_getpeername (int s, struct sockaddr *addr, int * namelen) | |
3695 | { | |
3696 | if (winsock_lib == NULL) | |
3697 | { | |
3698 | h_errno = ENETDOWN; | |
3699 | return SOCKET_ERROR; | |
3700 | } | |
3701 | ||
3702 | check_errno (); | |
3703 | if (fd_info[s].flags & FILE_SOCKET) | |
3704 | { | |
3705 | int rc = pfn_getpeername (SOCK_HANDLE (s), addr, namelen); | |
3706 | if (rc == SOCKET_ERROR) | |
3707 | set_errno (); | |
3708 | return rc; | |
3709 | } | |
3710 | h_errno = ENOTSOCK; | |
3711 | return SOCKET_ERROR; | |
3712 | } | |
3713 | ||
3714 | ||
380961a6 GV |
3715 | int |
3716 | sys_shutdown (int s, int how) | |
3717 | { | |
380961a6 GV |
3718 | if (winsock_lib == NULL) |
3719 | { | |
3720 | h_errno = ENETDOWN; | |
3721 | return SOCKET_ERROR; | |
3722 | } | |
3723 | ||
3724 | check_errno (); | |
3725 | if (fd_info[s].flags & FILE_SOCKET) | |
3726 | { | |
3727 | int rc = pfn_shutdown (SOCK_HANDLE (s), how); | |
962955c5 JR |
3728 | if (rc == SOCKET_ERROR) |
3729 | set_errno (); | |
3730 | return rc; | |
3731 | } | |
3732 | h_errno = ENOTSOCK; | |
3733 | return SOCKET_ERROR; | |
3734 | } | |
3735 | ||
3736 | int | |
a5a389bb | 3737 | sys_setsockopt (int s, int level, int optname, const void * optval, int optlen) |
962955c5 JR |
3738 | { |
3739 | if (winsock_lib == NULL) | |
3740 | { | |
3741 | h_errno = ENETDOWN; | |
3742 | return SOCKET_ERROR; | |
3743 | } | |
3744 | ||
3745 | check_errno (); | |
3746 | if (fd_info[s].flags & FILE_SOCKET) | |
3747 | { | |
3748 | int rc = pfn_setsockopt (SOCK_HANDLE (s), level, optname, | |
a5a389bb | 3749 | (const char *)optval, optlen); |
962955c5 JR |
3750 | if (rc == SOCKET_ERROR) |
3751 | set_errno (); | |
3752 | return rc; | |
3753 | } | |
3754 | h_errno = ENOTSOCK; | |
177c0ea7 | 3755 | return SOCKET_ERROR; |
962955c5 JR |
3756 | } |
3757 | ||
3758 | int | |
3759 | sys_listen (int s, int backlog) | |
3760 | { | |
3761 | if (winsock_lib == NULL) | |
3762 | { | |
3763 | h_errno = ENETDOWN; | |
3764 | return SOCKET_ERROR; | |
3765 | } | |
3766 | ||
3767 | check_errno (); | |
3768 | if (fd_info[s].flags & FILE_SOCKET) | |
3769 | { | |
3770 | int rc = pfn_listen (SOCK_HANDLE (s), backlog); | |
3771 | if (rc == SOCKET_ERROR) | |
3772 | set_errno (); | |
26fb7bc4 | 3773 | else |
64570b36 | 3774 | fd_info[s].flags |= FILE_LISTEN; |
962955c5 JR |
3775 | return rc; |
3776 | } | |
3777 | h_errno = ENOTSOCK; | |
177c0ea7 | 3778 | return SOCKET_ERROR; |
962955c5 JR |
3779 | } |
3780 | ||
3781 | int | |
3782 | sys_getsockname (int s, struct sockaddr * name, int * namelen) | |
3783 | { | |
3784 | if (winsock_lib == NULL) | |
3785 | { | |
3786 | h_errno = ENETDOWN; | |
3787 | return SOCKET_ERROR; | |
3788 | } | |
3789 | ||
3790 | check_errno (); | |
3791 | if (fd_info[s].flags & FILE_SOCKET) | |
3792 | { | |
3793 | int rc = pfn_getsockname (SOCK_HANDLE (s), name, namelen); | |
3794 | if (rc == SOCKET_ERROR) | |
3795 | set_errno (); | |
3796 | return rc; | |
3797 | } | |
3798 | h_errno = ENOTSOCK; | |
177c0ea7 | 3799 | return SOCKET_ERROR; |
962955c5 JR |
3800 | } |
3801 | ||
3802 | int | |
3803 | sys_accept (int s, struct sockaddr * addr, int * addrlen) | |
3804 | { | |
3805 | if (winsock_lib == NULL) | |
3806 | { | |
3807 | h_errno = ENETDOWN; | |
3808 | return -1; | |
3809 | } | |
3810 | ||
3811 | check_errno (); | |
26fb7bc4 | 3812 | if (fd_info[s].flags & FILE_LISTEN) |
962955c5 | 3813 | { |
a0ad1860 | 3814 | SOCKET t = pfn_accept (SOCK_HANDLE (s), addr, addrlen); |
64570b36 KS |
3815 | int fd = -1; |
3816 | if (t == INVALID_SOCKET) | |
3817 | set_errno (); | |
3818 | else | |
3819 | fd = socket_to_fd (t); | |
962955c5 | 3820 | |
64570b36 KS |
3821 | fd_info[s].cp->status = STATUS_READ_ACKNOWLEDGED; |
3822 | ResetEvent (fd_info[s].cp->char_avail); | |
3823 | return fd; | |
962955c5 JR |
3824 | } |
3825 | h_errno = ENOTSOCK; | |
3826 | return -1; | |
3827 | } | |
3828 | ||
3829 | int | |
3830 | sys_recvfrom (int s, char * buf, int len, int flags, | |
3831 | struct sockaddr * from, int * fromlen) | |
3832 | { | |
3833 | if (winsock_lib == NULL) | |
3834 | { | |
3835 | h_errno = ENETDOWN; | |
3836 | return SOCKET_ERROR; | |
3837 | } | |
3838 | ||
3839 | check_errno (); | |
3840 | if (fd_info[s].flags & FILE_SOCKET) | |
3841 | { | |
3842 | int rc = pfn_recvfrom (SOCK_HANDLE (s), buf, len, flags, from, fromlen); | |
3843 | if (rc == SOCKET_ERROR) | |
3844 | set_errno (); | |
3845 | return rc; | |
3846 | } | |
3847 | h_errno = ENOTSOCK; | |
3848 | return SOCKET_ERROR; | |
3849 | } | |
3850 | ||
3851 | int | |
3852 | sys_sendto (int s, const char * buf, int len, int flags, | |
3853 | const struct sockaddr * to, int tolen) | |
3854 | { | |
3855 | if (winsock_lib == NULL) | |
3856 | { | |
3857 | h_errno = ENETDOWN; | |
3858 | return SOCKET_ERROR; | |
3859 | } | |
3860 | ||
3861 | check_errno (); | |
3862 | if (fd_info[s].flags & FILE_SOCKET) | |
3863 | { | |
3864 | int rc = pfn_sendto (SOCK_HANDLE (s), buf, len, flags, to, tolen); | |
380961a6 GV |
3865 | if (rc == SOCKET_ERROR) |
3866 | set_errno (); | |
3867 | return rc; | |
3868 | } | |
3869 | h_errno = ENOTSOCK; | |
3870 | return SOCKET_ERROR; | |
3871 | } | |
3872 | ||
ecd270eb JR |
3873 | /* Windows does not have an fcntl function. Provide an implementation |
3874 | solely for making sockets non-blocking. */ | |
3875 | int | |
3876 | fcntl (int s, int cmd, int options) | |
3877 | { | |
3878 | if (winsock_lib == NULL) | |
3879 | { | |
3880 | h_errno = ENETDOWN; | |
3881 | return -1; | |
3882 | } | |
3883 | ||
3884 | check_errno (); | |
3885 | if (fd_info[s].flags & FILE_SOCKET) | |
3886 | { | |
3887 | if (cmd == F_SETFL && options == O_NDELAY) | |
3888 | { | |
3889 | unsigned long nblock = 1; | |
3890 | int rc = pfn_ioctlsocket (SOCK_HANDLE (s), FIONBIO, &nblock); | |
3891 | if (rc == SOCKET_ERROR) | |
3892 | set_errno(); | |
3893 | /* Keep track of the fact that we set this to non-blocking. */ | |
3894 | fd_info[s].flags |= FILE_NDELAY; | |
3895 | return rc; | |
3896 | } | |
3897 | else | |
3898 | { | |
3899 | h_errno = EINVAL; | |
3900 | return SOCKET_ERROR; | |
3901 | } | |
3902 | } | |
3903 | h_errno = ENOTSOCK; | |
3904 | return SOCKET_ERROR; | |
3905 | } | |
3906 | ||
480b0c5b GV |
3907 | #endif /* HAVE_SOCKETS */ |
3908 | ||
3909 | ||
3910 | /* Shadow main io functions: we need to handle pipes and sockets more | |
3911 | intelligently, and implement non-blocking mode as well. */ | |
3912 | ||
3913 | int | |
3914 | sys_close (int fd) | |
3915 | { | |
3916 | int rc; | |
3917 | ||
7559f399 | 3918 | if (fd < 0) |
480b0c5b GV |
3919 | { |
3920 | errno = EBADF; | |
3921 | return -1; | |
3922 | } | |
3923 | ||
7559f399 | 3924 | if (fd < MAXDESC && fd_info[fd].cp) |
480b0c5b GV |
3925 | { |
3926 | child_process * cp = fd_info[fd].cp; | |
3927 | ||
3928 | fd_info[fd].cp = NULL; | |
3929 | ||
3930 | if (CHILD_ACTIVE (cp)) | |
3931 | { | |
3932 | /* if last descriptor to active child_process then cleanup */ | |
3933 | int i; | |
3934 | for (i = 0; i < MAXDESC; i++) | |
3935 | { | |
3936 | if (i == fd) | |
3937 | continue; | |
3938 | if (fd_info[i].cp == cp) | |
3939 | break; | |
3940 | } | |
3941 | if (i == MAXDESC) | |
3942 | { | |
f249a012 | 3943 | #ifdef HAVE_SOCKETS |
480b0c5b GV |
3944 | if (fd_info[fd].flags & FILE_SOCKET) |
3945 | { | |
f249a012 RS |
3946 | #ifndef SOCK_REPLACE_HANDLE |
3947 | if (winsock_lib == NULL) abort (); | |
480b0c5b GV |
3948 | |
3949 | pfn_shutdown (SOCK_HANDLE (fd), 2); | |
3950 | rc = pfn_closesocket (SOCK_HANDLE (fd)); | |
f249a012 RS |
3951 | #endif |
3952 | winsock_inuse--; /* count open sockets */ | |
480b0c5b GV |
3953 | } |
3954 | #endif | |
3955 | delete_child (cp); | |
3956 | } | |
3957 | } | |
3958 | } | |
3959 | ||
3960 | /* Note that sockets do not need special treatment here (at least on | |
e9e23e23 | 3961 | NT and Windows 95 using the standard tcp/ip stacks) - it appears that |
480b0c5b GV |
3962 | closesocket is equivalent to CloseHandle, which is to be expected |
3963 | because socket handles are fully fledged kernel handles. */ | |
3964 | rc = _close (fd); | |
3965 | ||
7559f399 | 3966 | if (rc == 0 && fd < MAXDESC) |
480b0c5b GV |
3967 | fd_info[fd].flags = 0; |
3968 | ||
3969 | return rc; | |
3970 | } | |
3971 | ||
3972 | int | |
3973 | sys_dup (int fd) | |
3974 | { | |
3975 | int new_fd; | |
3976 | ||
3977 | new_fd = _dup (fd); | |
7559f399 | 3978 | if (new_fd >= 0 && new_fd < MAXDESC) |
480b0c5b GV |
3979 | { |
3980 | /* duplicate our internal info as well */ | |
3981 | fd_info[new_fd] = fd_info[fd]; | |
3982 | } | |
3983 | return new_fd; | |
3984 | } | |
3985 | ||
3986 | ||
3987 | int | |
3988 | sys_dup2 (int src, int dst) | |
3989 | { | |
3990 | int rc; | |
3991 | ||
3992 | if (dst < 0 || dst >= MAXDESC) | |
3993 | { | |
3994 | errno = EBADF; | |
3995 | return -1; | |
3996 | } | |
3997 | ||
3998 | /* make sure we close the destination first if it's a pipe or socket */ | |
3999 | if (src != dst && fd_info[dst].flags != 0) | |
4000 | sys_close (dst); | |
177c0ea7 | 4001 | |
480b0c5b GV |
4002 | rc = _dup2 (src, dst); |
4003 | if (rc == 0) | |
4004 | { | |
4005 | /* duplicate our internal info as well */ | |
4006 | fd_info[dst] = fd_info[src]; | |
4007 | } | |
4008 | return rc; | |
4009 | } | |
4010 | ||
480b0c5b GV |
4011 | /* Unix pipe() has only one arg */ |
4012 | int | |
4013 | sys_pipe (int * phandles) | |
4014 | { | |
4015 | int rc; | |
4016 | unsigned flags; | |
480b0c5b | 4017 | |
76b3903d GV |
4018 | /* make pipe handles non-inheritable; when we spawn a child, we |
4019 | replace the relevant handle with an inheritable one. Also put | |
4020 | pipes into binary mode; we will do text mode translation ourselves | |
4021 | if required. */ | |
4022 | rc = _pipe (phandles, 0, _O_NOINHERIT | _O_BINARY); | |
480b0c5b GV |
4023 | |
4024 | if (rc == 0) | |
4025 | { | |
cb72110d JR |
4026 | /* Protect against overflow, since Windows can open more handles than |
4027 | our fd_info array has room for. */ | |
4028 | if (phandles[0] >= MAXDESC || phandles[1] >= MAXDESC) | |
4029 | { | |
4030 | _close (phandles[0]); | |
4031 | _close (phandles[1]); | |
4032 | rc = -1; | |
4033 | } | |
4034 | else | |
4035 | { | |
4036 | flags = FILE_PIPE | FILE_READ | FILE_BINARY; | |
4037 | fd_info[phandles[0]].flags = flags; | |
480b0c5b | 4038 | |
cb72110d JR |
4039 | flags = FILE_PIPE | FILE_WRITE | FILE_BINARY; |
4040 | fd_info[phandles[1]].flags = flags; | |
4041 | } | |
480b0c5b GV |
4042 | } |
4043 | ||
4044 | return rc; | |
4045 | } | |
4046 | ||
f7554349 | 4047 | /* From ntproc.c */ |
78806724 | 4048 | extern int w32_pipe_read_delay; |
f7554349 | 4049 | |
480b0c5b GV |
4050 | /* Function to do blocking read of one byte, needed to implement |
4051 | select. It is only allowed on sockets and pipes. */ | |
4052 | int | |
4053 | _sys_read_ahead (int fd) | |
4054 | { | |
4055 | child_process * cp; | |
4056 | int rc; | |
4057 | ||
4058 | if (fd < 0 || fd >= MAXDESC) | |
4059 | return STATUS_READ_ERROR; | |
4060 | ||
4061 | cp = fd_info[fd].cp; | |
4062 | ||
4063 | if (cp == NULL || cp->fd != fd || cp->status != STATUS_READ_READY) | |
4064 | return STATUS_READ_ERROR; | |
4065 | ||
4066 | if ((fd_info[fd].flags & (FILE_PIPE | FILE_SOCKET)) == 0 | |
4067 | || (fd_info[fd].flags & FILE_READ) == 0) | |
4068 | { | |
4069 | DebPrint (("_sys_read_ahead: internal error: fd %d is not a pipe or socket!\n", fd)); | |
4070 | abort (); | |
4071 | } | |
177c0ea7 | 4072 | |
480b0c5b | 4073 | cp->status = STATUS_READ_IN_PROGRESS; |
177c0ea7 | 4074 | |
480b0c5b | 4075 | if (fd_info[fd].flags & FILE_PIPE) |
f7554349 | 4076 | { |
f7554349 KH |
4077 | rc = _read (fd, &cp->chr, sizeof (char)); |
4078 | ||
4079 | /* Give subprocess time to buffer some more output for us before | |
e9e23e23 | 4080 | reporting that input is available; we need this because Windows 95 |
f7554349 KH |
4081 | connects DOS programs to pipes by making the pipe appear to be |
4082 | the normal console stdout - as a result most DOS programs will | |
4083 | write to stdout without buffering, ie. one character at a | |
fbd6baed | 4084 | time. Even some W32 programs do this - "dir" in a command |
f7554349 KH |
4085 | shell on NT is very slow if we don't do this. */ |
4086 | if (rc > 0) | |
4087 | { | |
78806724 | 4088 | int wait = w32_pipe_read_delay; |
f7554349 KH |
4089 | |
4090 | if (wait > 0) | |
4091 | Sleep (wait); | |
4092 | else if (wait < 0) | |
4093 | while (++wait <= 0) | |
4094 | /* Yield remainder of our time slice, effectively giving a | |
4095 | temporary priority boost to the child process. */ | |
4096 | Sleep (0); | |
4097 | } | |
4098 | } | |
480b0c5b GV |
4099 | #ifdef HAVE_SOCKETS |
4100 | else if (fd_info[fd].flags & FILE_SOCKET) | |
ecd270eb JR |
4101 | { |
4102 | unsigned long nblock = 0; | |
4103 | /* We always want this to block, so temporarily disable NDELAY. */ | |
4104 | if (fd_info[fd].flags & FILE_NDELAY) | |
4105 | pfn_ioctlsocket (SOCK_HANDLE (fd), FIONBIO, &nblock); | |
4106 | ||
4107 | rc = pfn_recv (SOCK_HANDLE (fd), &cp->chr, sizeof (char), 0); | |
4108 | ||
4109 | if (fd_info[fd].flags & FILE_NDELAY) | |
4110 | { | |
4111 | nblock = 1; | |
4112 | pfn_ioctlsocket (SOCK_HANDLE (fd), FIONBIO, &nblock); | |
4113 | } | |
4114 | } | |
480b0c5b | 4115 | #endif |
177c0ea7 | 4116 | |
480b0c5b GV |
4117 | if (rc == sizeof (char)) |
4118 | cp->status = STATUS_READ_SUCCEEDED; | |
4119 | else | |
4120 | cp->status = STATUS_READ_FAILED; | |
4121 | ||
4122 | return cp->status; | |
4123 | } | |
4124 | ||
9bfb11f9 KS |
4125 | int |
4126 | _sys_wait_accept (int fd) | |
64570b36 KS |
4127 | { |
4128 | HANDLE hEv; | |
4129 | child_process * cp; | |
4130 | int rc; | |
4131 | ||
4132 | if (fd < 0 || fd >= MAXDESC) | |
4133 | return STATUS_READ_ERROR; | |
4134 | ||
4135 | cp = fd_info[fd].cp; | |
4136 | ||
4137 | if (cp == NULL || cp->fd != fd || cp->status != STATUS_READ_READY) | |
4138 | return STATUS_READ_ERROR; | |
4139 | ||
4140 | cp->status = STATUS_READ_FAILED; | |
4141 | ||
4142 | hEv = pfn_WSACreateEvent (); | |
4143 | rc = pfn_WSAEventSelect (SOCK_HANDLE (fd), hEv, FD_ACCEPT); | |
4144 | if (rc != SOCKET_ERROR) | |
4145 | { | |
4146 | rc = WaitForSingleObject (hEv, INFINITE); | |
4147 | pfn_WSAEventSelect (SOCK_HANDLE (fd), NULL, 0); | |
64570b36 KS |
4148 | if (rc == WAIT_OBJECT_0) |
4149 | cp->status = STATUS_READ_SUCCEEDED; | |
4150 | } | |
7046f191 | 4151 | pfn_WSACloseEvent (hEv); |
64570b36 KS |
4152 | |
4153 | return cp->status; | |
4154 | } | |
4155 | ||
480b0c5b GV |
4156 | int |
4157 | sys_read (int fd, char * buffer, unsigned int count) | |
4158 | { | |
4159 | int nchars; | |
480b0c5b GV |
4160 | int to_read; |
4161 | DWORD waiting; | |
76b3903d | 4162 | char * orig_buffer = buffer; |
480b0c5b | 4163 | |
7559f399 | 4164 | if (fd < 0) |
480b0c5b GV |
4165 | { |
4166 | errno = EBADF; | |
4167 | return -1; | |
4168 | } | |
4169 | ||
7559f399 | 4170 | if (fd < MAXDESC && fd_info[fd].flags & (FILE_PIPE | FILE_SOCKET)) |
480b0c5b GV |
4171 | { |
4172 | child_process *cp = fd_info[fd].cp; | |
4173 | ||
4174 | if ((fd_info[fd].flags & FILE_READ) == 0) | |
4175 | { | |
4176 | errno = EBADF; | |
4177 | return -1; | |
4178 | } | |
4179 | ||
76b3903d GV |
4180 | nchars = 0; |
4181 | ||
4182 | /* re-read CR carried over from last read */ | |
4183 | if (fd_info[fd].flags & FILE_LAST_CR) | |
4184 | { | |
4185 | if (fd_info[fd].flags & FILE_BINARY) abort (); | |
4186 | *buffer++ = 0x0d; | |
4187 | count--; | |
4188 | nchars++; | |
f52eb3ef | 4189 | fd_info[fd].flags &= ~FILE_LAST_CR; |
76b3903d GV |
4190 | } |
4191 | ||
480b0c5b GV |
4192 | /* presence of a child_process structure means we are operating in |
4193 | non-blocking mode - otherwise we just call _read directly. | |
4194 | Note that the child_process structure might be missing because | |
4195 | reap_subprocess has been called; in this case the pipe is | |
4196 | already broken, so calling _read on it is okay. */ | |
4197 | if (cp) | |
4198 | { | |
4199 | int current_status = cp->status; | |
4200 | ||
4201 | switch (current_status) | |
4202 | { | |
4203 | case STATUS_READ_FAILED: | |
4204 | case STATUS_READ_ERROR: | |
f52eb3ef GV |
4205 | /* report normal EOF if nothing in buffer */ |
4206 | if (nchars <= 0) | |
4207 | fd_info[fd].flags |= FILE_AT_EOF; | |
4208 | return nchars; | |
480b0c5b GV |
4209 | |
4210 | case STATUS_READ_READY: | |
4211 | case STATUS_READ_IN_PROGRESS: | |
4212 | DebPrint (("sys_read called when read is in progress\n")); | |
4213 | errno = EWOULDBLOCK; | |
4214 | return -1; | |
4215 | ||
4216 | case STATUS_READ_SUCCEEDED: | |
4217 | /* consume read-ahead char */ | |
4218 | *buffer++ = cp->chr; | |
4219 | count--; | |
76b3903d | 4220 | nchars++; |
480b0c5b GV |
4221 | cp->status = STATUS_READ_ACKNOWLEDGED; |
4222 | ResetEvent (cp->char_avail); | |
4223 | ||
4224 | case STATUS_READ_ACKNOWLEDGED: | |
4225 | break; | |
4226 | ||
4227 | default: | |
4228 | DebPrint (("sys_read: bad status %d\n", current_status)); | |
4229 | errno = EBADF; | |
4230 | return -1; | |
4231 | } | |
4232 | ||
4233 | if (fd_info[fd].flags & FILE_PIPE) | |
4234 | { | |
4235 | PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, &waiting, NULL); | |
4236 | to_read = min (waiting, (DWORD) count); | |
f52eb3ef GV |
4237 | |
4238 | if (to_read > 0) | |
4239 | nchars += _read (fd, buffer, to_read); | |
480b0c5b GV |
4240 | } |
4241 | #ifdef HAVE_SOCKETS | |
4242 | else /* FILE_SOCKET */ | |
4243 | { | |
f249a012 | 4244 | if (winsock_lib == NULL) abort (); |
480b0c5b GV |
4245 | |
4246 | /* do the equivalent of a non-blocking read */ | |
4247 | pfn_ioctlsocket (SOCK_HANDLE (fd), FIONREAD, &waiting); | |
76b3903d | 4248 | if (waiting == 0 && nchars == 0) |
480b0c5b GV |
4249 | { |
4250 | h_errno = errno = EWOULDBLOCK; | |
4251 | return -1; | |
4252 | } | |
4253 | ||
480b0c5b GV |
4254 | if (waiting) |
4255 | { | |
4256 | /* always use binary mode for sockets */ | |
76b3903d GV |
4257 | int res = pfn_recv (SOCK_HANDLE (fd), buffer, count, 0); |
4258 | if (res == SOCKET_ERROR) | |
480b0c5b GV |
4259 | { |
4260 | DebPrint(("sys_read.recv failed with error %d on socket %ld\n", | |
4261 | pfn_WSAGetLastError (), SOCK_HANDLE (fd))); | |
76b3903d GV |
4262 | set_errno (); |
4263 | return -1; | |
480b0c5b | 4264 | } |
76b3903d | 4265 | nchars += res; |
480b0c5b GV |
4266 | } |
4267 | } | |
4268 | #endif | |
4269 | } | |
4270 | else | |
f52eb3ef GV |
4271 | { |
4272 | int nread = _read (fd, buffer, count); | |
4273 | if (nread >= 0) | |
4274 | nchars += nread; | |
4275 | else if (nchars == 0) | |
4276 | nchars = nread; | |
4277 | } | |
76b3903d | 4278 | |
f52eb3ef GV |
4279 | if (nchars <= 0) |
4280 | fd_info[fd].flags |= FILE_AT_EOF; | |
76b3903d | 4281 | /* Perform text mode translation if required. */ |
f52eb3ef | 4282 | else if ((fd_info[fd].flags & FILE_BINARY) == 0) |
76b3903d GV |
4283 | { |
4284 | nchars = crlf_to_lf (nchars, orig_buffer); | |
4285 | /* If buffer contains only CR, return that. To be absolutely | |
4286 | sure we should attempt to read the next char, but in | |
4287 | practice a CR to be followed by LF would not appear by | |
4288 | itself in the buffer. */ | |
4289 | if (nchars > 1 && orig_buffer[nchars - 1] == 0x0d) | |
4290 | { | |
4291 | fd_info[fd].flags |= FILE_LAST_CR; | |
4292 | nchars--; | |
4293 | } | |
76b3903d | 4294 | } |
480b0c5b GV |
4295 | } |
4296 | else | |
4297 | nchars = _read (fd, buffer, count); | |
4298 | ||
76b3903d | 4299 | return nchars; |
480b0c5b GV |
4300 | } |
4301 | ||
4302 | /* For now, don't bother with a non-blocking mode */ | |
4303 | int | |
4304 | sys_write (int fd, const void * buffer, unsigned int count) | |
4305 | { | |
4306 | int nchars; | |
4307 | ||
7559f399 | 4308 | if (fd < 0) |
480b0c5b GV |
4309 | { |
4310 | errno = EBADF; | |
4311 | return -1; | |
4312 | } | |
4313 | ||
7559f399 | 4314 | if (fd < MAXDESC && fd_info[fd].flags & (FILE_PIPE | FILE_SOCKET)) |
76b3903d GV |
4315 | { |
4316 | if ((fd_info[fd].flags & FILE_WRITE) == 0) | |
4317 | { | |
4318 | errno = EBADF; | |
4319 | return -1; | |
4320 | } | |
4321 | ||
4322 | /* Perform text mode translation if required. */ | |
4323 | if ((fd_info[fd].flags & FILE_BINARY) == 0) | |
4324 | { | |
4325 | char * tmpbuf = alloca (count * 2); | |
4326 | unsigned char * src = (void *)buffer; | |
4327 | unsigned char * dst = tmpbuf; | |
4328 | int nbytes = count; | |
4329 | ||
4330 | while (1) | |
4331 | { | |
4332 | unsigned char *next; | |
4333 | /* copy next line or remaining bytes */ | |
4334 | next = _memccpy (dst, src, '\n', nbytes); | |
4335 | if (next) | |
4336 | { | |
4337 | /* copied one line ending with '\n' */ | |
4338 | int copied = next - dst; | |
4339 | nbytes -= copied; | |
4340 | src += copied; | |
4341 | /* insert '\r' before '\n' */ | |
4342 | next[-1] = '\r'; | |
4343 | next[0] = '\n'; | |
4344 | dst = next + 1; | |
4345 | count++; | |
177c0ea7 | 4346 | } |
76b3903d GV |
4347 | else |
4348 | /* copied remaining partial line -> now finished */ | |
4349 | break; | |
4350 | } | |
4351 | buffer = tmpbuf; | |
4352 | } | |
4353 | } | |
4354 | ||
480b0c5b | 4355 | #ifdef HAVE_SOCKETS |
7559f399 | 4356 | if (fd < MAXDESC && fd_info[fd].flags & FILE_SOCKET) |
480b0c5b | 4357 | { |
30a32e0e | 4358 | unsigned long nblock = 0; |
f249a012 | 4359 | if (winsock_lib == NULL) abort (); |
30a32e0e JR |
4360 | |
4361 | /* TODO: implement select() properly so non-blocking I/O works. */ | |
4362 | /* For now, make sure the write blocks. */ | |
4363 | if (fd_info[fd].flags & FILE_NDELAY) | |
4364 | pfn_ioctlsocket (SOCK_HANDLE (fd), FIONBIO, &nblock); | |
4365 | ||
480b0c5b | 4366 | nchars = pfn_send (SOCK_HANDLE (fd), buffer, count, 0); |
30a32e0e JR |
4367 | |
4368 | /* Set the socket back to non-blocking if it was before, | |
4369 | for other operations that support it. */ | |
4370 | if (fd_info[fd].flags & FILE_NDELAY) | |
4371 | { | |
4372 | nblock = 1; | |
4373 | pfn_ioctlsocket (SOCK_HANDLE (fd), FIONBIO, &nblock); | |
4374 | } | |
4375 | ||
480b0c5b GV |
4376 | if (nchars == SOCKET_ERROR) |
4377 | { | |
670773af | 4378 | DebPrint(("sys_write.send failed with error %d on socket %ld\n", |
480b0c5b GV |
4379 | pfn_WSAGetLastError (), SOCK_HANDLE (fd))); |
4380 | set_errno (); | |
4381 | } | |
4382 | } | |
4383 | else | |
4384 | #endif | |
4385 | nchars = _write (fd, buffer, count); | |
4386 | ||
4387 | return nchars; | |
4388 | } | |
4389 | ||
f52eb3ef GV |
4390 | static void |
4391 | check_windows_init_file () | |
4392 | { | |
4393 | extern int noninteractive, inhibit_window_system; | |
4394 | ||
4395 | /* A common indication that Emacs is not installed properly is when | |
4396 | it cannot find the Windows installation file. If this file does | |
4397 | not exist in the expected place, tell the user. */ | |
4398 | ||
177c0ea7 | 4399 | if (!noninteractive && !inhibit_window_system) |
d54abccd GV |
4400 | { |
4401 | extern Lisp_Object Vwindow_system, Vload_path, Qfile_exists_p; | |
a0b9c838 | 4402 | Lisp_Object objs[2]; |
96ef7d42 | 4403 | Lisp_Object full_load_path; |
d54abccd GV |
4404 | Lisp_Object init_file; |
4405 | int fd; | |
f52eb3ef | 4406 | |
a0b9c838 GV |
4407 | objs[0] = Vload_path; |
4408 | objs[1] = decode_env_path (0, (getenv ("EMACSLOADPATH"))); | |
4409 | full_load_path = Fappend (2, objs); | |
d54abccd | 4410 | init_file = build_string ("term/w32-win"); |
c50a2aa6 | 4411 | fd = openp (full_load_path, init_file, Fget_load_suffixes (), NULL, Qnil); |
177c0ea7 | 4412 | if (fd < 0) |
d54abccd | 4413 | { |
96ef7d42 | 4414 | Lisp_Object load_path_print = Fprin1_to_string (full_load_path, Qnil); |
d5db4077 KR |
4415 | char *init_file_name = SDATA (init_file); |
4416 | char *load_path = SDATA (load_path_print); | |
acc23b87 KS |
4417 | char *buffer = alloca (1024 |
4418 | + strlen (init_file_name) | |
4419 | + strlen (load_path)); | |
d54abccd | 4420 | |
177c0ea7 | 4421 | sprintf (buffer, |
d54abccd GV |
4422 | "The Emacs Windows initialization file \"%s.el\" " |
4423 | "could not be found in your Emacs installation. " | |
4424 | "Emacs checked the following directories for this file:\n" | |
4425 | "\n%s\n\n" | |
4426 | "When Emacs cannot find this file, it usually means that it " | |
4427 | "was not installed properly, or its distribution file was " | |
4428 | "not unpacked properly.\nSee the README.W32 file in the " | |
4429 | "top-level Emacs directory for more information.", | |
4430 | init_file_name, load_path); | |
4431 | MessageBox (NULL, | |
4432 | buffer, | |
4433 | "Emacs Abort Dialog", | |
4434 | MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL); | |
f52eb3ef GV |
4435 | /* Use the low-level Emacs abort. */ |
4436 | #undef abort | |
d54abccd GV |
4437 | abort (); |
4438 | } | |
4439 | else | |
4440 | { | |
a302c7ae | 4441 | _close (fd); |
d54abccd | 4442 | } |
f52eb3ef | 4443 | } |
f52eb3ef | 4444 | } |
480b0c5b GV |
4445 | |
4446 | void | |
4447 | term_ntproc () | |
4448 | { | |
4449 | #ifdef HAVE_SOCKETS | |
4450 | /* shutdown the socket interface if necessary */ | |
4451 | term_winsock (); | |
4452 | #endif | |
52c7f9ee JR |
4453 | |
4454 | term_w32select (); | |
480b0c5b GV |
4455 | } |
4456 | ||
4457 | void | |
4458 | init_ntproc () | |
4459 | { | |
4460 | #ifdef HAVE_SOCKETS | |
f249a012 RS |
4461 | /* Initialise the socket interface now if available and requested by |
4462 | the user by defining PRELOAD_WINSOCK; otherwise loading will be | |
fbd6baed | 4463 | delayed until open-network-stream is called (w32-has-winsock can |
f249a012 RS |
4464 | also be used to dynamically load or reload winsock). |
4465 | ||
4466 | Conveniently, init_environment is called before us, so | |
4467 | PRELOAD_WINSOCK can be set in the registry. */ | |
4468 | ||
4469 | /* Always initialize this correctly. */ | |
4470 | winsock_lib = NULL; | |
4471 | ||
4472 | if (getenv ("PRELOAD_WINSOCK") != NULL) | |
4473 | init_winsock (TRUE); | |
480b0c5b GV |
4474 | #endif |
4475 | ||
4476 | /* Initial preparation for subprocess support: replace our standard | |
4477 | handles with non-inheritable versions. */ | |
4478 | { | |
4479 | HANDLE parent; | |
4480 | HANDLE stdin_save = INVALID_HANDLE_VALUE; | |
4481 | HANDLE stdout_save = INVALID_HANDLE_VALUE; | |
4482 | HANDLE stderr_save = INVALID_HANDLE_VALUE; | |
4483 | ||
4484 | parent = GetCurrentProcess (); | |
4485 | ||
4486 | /* ignore errors when duplicating and closing; typically the | |
4487 | handles will be invalid when running as a gui program. */ | |
177c0ea7 JB |
4488 | DuplicateHandle (parent, |
4489 | GetStdHandle (STD_INPUT_HANDLE), | |
480b0c5b | 4490 | parent, |
177c0ea7 JB |
4491 | &stdin_save, |
4492 | 0, | |
4493 | FALSE, | |
480b0c5b | 4494 | DUPLICATE_SAME_ACCESS); |
177c0ea7 | 4495 | |
480b0c5b GV |
4496 | DuplicateHandle (parent, |
4497 | GetStdHandle (STD_OUTPUT_HANDLE), | |
4498 | parent, | |
4499 | &stdout_save, | |
4500 | 0, | |
4501 | FALSE, | |
4502 | DUPLICATE_SAME_ACCESS); | |
177c0ea7 | 4503 | |
480b0c5b GV |
4504 | DuplicateHandle (parent, |
4505 | GetStdHandle (STD_ERROR_HANDLE), | |
4506 | parent, | |
4507 | &stderr_save, | |
4508 | 0, | |
4509 | FALSE, | |
4510 | DUPLICATE_SAME_ACCESS); | |
177c0ea7 | 4511 | |
480b0c5b GV |
4512 | fclose (stdin); |
4513 | fclose (stdout); | |
4514 | fclose (stderr); | |
4515 | ||
4516 | if (stdin_save != INVALID_HANDLE_VALUE) | |
4517 | _open_osfhandle ((long) stdin_save, O_TEXT); | |
4518 | else | |
76b3903d GV |
4519 | _open ("nul", O_TEXT | O_NOINHERIT | O_RDONLY); |
4520 | _fdopen (0, "r"); | |
480b0c5b GV |
4521 | |
4522 | if (stdout_save != INVALID_HANDLE_VALUE) | |
4523 | _open_osfhandle ((long) stdout_save, O_TEXT); | |
4524 | else | |
76b3903d GV |
4525 | _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY); |
4526 | _fdopen (1, "w"); | |
480b0c5b GV |
4527 | |
4528 | if (stderr_save != INVALID_HANDLE_VALUE) | |
4529 | _open_osfhandle ((long) stderr_save, O_TEXT); | |
4530 | else | |
76b3903d GV |
4531 | _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY); |
4532 | _fdopen (2, "w"); | |
480b0c5b GV |
4533 | } |
4534 | ||
4535 | /* unfortunately, atexit depends on implementation of malloc */ | |
4536 | /* atexit (term_ntproc); */ | |
4537 | signal (SIGABRT, term_ntproc); | |
76b3903d GV |
4538 | |
4539 | /* determine which drives are fixed, for GetCachedVolumeInformation */ | |
4540 | { | |
4541 | /* GetDriveType must have trailing backslash. */ | |
4542 | char drive[] = "A:\\"; | |
4543 | ||
4544 | /* Loop over all possible drive letters */ | |
4545 | while (*drive <= 'Z') | |
4546 | { | |
4547 | /* Record if this drive letter refers to a fixed drive. */ | |
177c0ea7 | 4548 | fixed_drives[DRIVE_INDEX (*drive)] = |
76b3903d GV |
4549 | (GetDriveType (drive) == DRIVE_FIXED); |
4550 | ||
4551 | (*drive)++; | |
4552 | } | |
a302c7ae AI |
4553 | |
4554 | /* Reset the volume info cache. */ | |
4555 | volume_cache = NULL; | |
76b3903d | 4556 | } |
177c0ea7 | 4557 | |
d54abccd GV |
4558 | /* Check to see if Emacs has been installed correctly. */ |
4559 | check_windows_init_file (); | |
480b0c5b GV |
4560 | } |
4561 | ||
a8c3a596 JR |
4562 | /* |
4563 | shutdown_handler ensures that buffers' autosave files are | |
4564 | up to date when the user logs off, or the system shuts down. | |
4565 | */ | |
4566 | BOOL WINAPI shutdown_handler(DWORD type) | |
4567 | { | |
4568 | /* Ctrl-C and Ctrl-Break are already suppressed, so don't handle them. */ | |
4569 | if (type == CTRL_CLOSE_EVENT /* User closes console window. */ | |
4570 | || type == CTRL_LOGOFF_EVENT /* User logs off. */ | |
4571 | || type == CTRL_SHUTDOWN_EVENT) /* User shutsdown. */ | |
4572 | { | |
4573 | /* Shut down cleanly, making sure autosave files are up to date. */ | |
4574 | shut_down_emacs (0, 0, Qnil); | |
4575 | } | |
4576 | ||
7046f191 | 4577 | /* Allow other handlers to handle this signal. */ |
a8c3a596 JR |
4578 | return FALSE; |
4579 | } | |
4580 | ||
9785d95b BK |
4581 | /* |
4582 | globals_of_w32 is used to initialize those global variables that | |
4583 | must always be initialized on startup even when the global variable | |
4584 | initialized is non zero (see the function main in emacs.c). | |
4585 | */ | |
9bfb11f9 KS |
4586 | void |
4587 | globals_of_w32 () | |
9785d95b | 4588 | { |
74258518 JR |
4589 | HMODULE kernel32 = GetModuleHandle ("kernel32.dll"); |
4590 | ||
4591 | get_process_times_fn = (GetProcessTimes_Proc) | |
4592 | GetProcAddress (kernel32, "GetProcessTimes"); | |
4593 | ||
9785d95b BK |
4594 | g_b_init_is_windows_9x = 0; |
4595 | g_b_init_open_process_token = 0; | |
4596 | g_b_init_get_token_information = 0; | |
4597 | g_b_init_lookup_account_sid = 0; | |
4598 | g_b_init_get_sid_identifier_authority = 0; | |
9d95a291 EZ |
4599 | g_b_init_get_sid_sub_authority = 0; |
4600 | g_b_init_get_sid_sub_authority_count = 0; | |
8aaaec6b EZ |
4601 | g_b_init_get_file_security = 0; |
4602 | g_b_init_get_security_descriptor_owner = 0; | |
4603 | g_b_init_get_security_descriptor_group = 0; | |
4604 | g_b_init_is_valid_sid = 0; | |
a8c3a596 JR |
4605 | /* The following sets a handler for shutdown notifications for |
4606 | console apps. This actually applies to Emacs in both console and | |
4607 | GUI modes, since we had to fool windows into thinking emacs is a | |
4608 | console application to get console mode to work. */ | |
4609 | SetConsoleCtrlHandler(shutdown_handler, TRUE); | |
8aaaec6b EZ |
4610 | |
4611 | /* "None" is the default group name on standalone workstations. */ | |
4612 | strcpy (dflt_group_name, "None"); | |
9785d95b BK |
4613 | } |
4614 | ||
aa5ee2a3 | 4615 | /* end of w32.c */ |
ab5796a9 MB |
4616 | |
4617 | /* arch-tag: 90442dd3-37be-482b-b272-ac752e3049f1 | |
4618 | (do not change this comment) */ |