1 ;;;; This file contains Win32 support routines that SBCL needs to
2 ;;;; implement itself, in addition to those that apply to Win32 in
3 ;;;; unix.lisp. In theory, some of these functions might someday be
4 ;;;; useful to the end user.
6 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
15 (in-package "SB!WIN32")
17 ;;; Alien definitions for commonly used Win32 types. Woe unto whoever
18 ;;; tries to untangle this someday for 64-bit Windows.
20 ;;; FIXME: There used to be many more here, which are now groveled,
21 ;;; but groveling HANDLE makes it unsigned, which currently breaks the
22 ;;; build. --NS 2006-06-18
23 (define-alien-type handle int-ptr)
25 (define-alien-type lispbool (boolean 32))
27 (define-alien-type system-string
28 #!-sb-unicode c-string
29 #!+sb-unicode (c-string :external-format :ucs-2))
31 (defconstant default-environment-length 1024)
33 ;;; HANDLEs are actually pointers, but an invalid handle is -1 cast
35 (defconstant invalid-handle -1)
39 ;;; Retrieve the calling thread's last-error code value. The
40 ;;; last-error code is maintained on a per-thread basis.
41 (define-alien-routine ("GetLastError@0" get-last-error) dword)
43 ;;; Flag constants for FORMAT-MESSAGE.
44 (defconstant format-message-from-system #x1000)
46 ;;; Format an error message based on a lookup table. See MSDN for the
47 ;;; full meaning of the all options---most are not used when getting
48 ;;; system error codes.
49 (define-alien-routine ("FormatMessageA@28" format-message) dword
60 ;;; Get the operating system handle for a C file descriptor. Returns
61 ;;; INVALID-HANDLE on failure.
62 (define-alien-routine ("_get_osfhandle" get-osfhandle) handle
65 ;;; Read data from a file handle into a buffer. This may be used
66 ;;; synchronously or with "overlapped" (asynchronous) I/O.
67 (define-alien-routine ("ReadFile@20" read-file) bool
71 (bytes-read (* dword))
74 ;;; Write data from a buffer to a file handle. This may be used
75 ;;; synchronously or with "overlapped" (asynchronous) I/O.
76 (define-alien-routine ("WriteFile@20" write-file) bool
79 (bytes-to-write dword)
80 (bytes-written (* dword))
83 ;;; Copy data from a named or anonymous pipe into a buffer without
84 ;;; removing it from the pipe. BUFFER, BYTES-READ, BYTES-AVAIL, and
85 ;;; BYTES-LEFT-THIS-MESSAGE may be NULL if no data is to be read.
86 ;;; Return TRUE on success, FALSE on failure.
87 (define-alien-routine ("PeekNamedPipe@24" peek-named-pipe) bool
91 (bytes-read (* dword))
92 (bytes-avail (* dword))
93 (bytes-left-this-message (* dword)))
95 ;;; Flush the console input buffer if HANDLE is a console handle.
96 ;;; Returns true on success, false if the handle does not refer to a
98 (define-alien-routine ("FlushConsoleInputBuffer@4" flush-console-input-buffer) bool
101 ;;; Read data from the console input buffer without removing it,
102 ;;; without blocking. Buffer should be large enough for LENGTH *
103 ;;; INPUT-RECORD-SIZE bytes.
104 (define-alien-routine ("PeekConsoleInputA@16" peek-console-input) bool
110 ;;; Listen for input on a Windows file handle. Unlike UNIX, there
111 ;;; isn't a unified interface to do this---we have to know what sort
112 ;;; of handle we have. Of course, there's no way to actually
113 ;;; introspect it, so we have to try various things until we find
114 ;;; something that works. Returns true if there could be input
115 ;;; available, or false if there is not.
116 (defun handle-listen (handle)
117 (with-alien ((avail dword)
118 (buf (array char #.input-record-size)))
119 (unless (zerop (peek-named-pipe handle nil 0 nil (addr avail) nil))
120 (return-from handle-listen (plusp avail)))
122 (unless (zerop (peek-console-input handle
125 (return-from handle-listen (plusp avail)))
127 ;; FIXME-SOCKETS: Try again here with WSAEventSelect in case
128 ;; HANDLE is a socket.
131 ;;; Listen for input on a C runtime file handle. Returns true if
132 ;;; there could be input available, or false if there is not.
133 (defun fd-listen (fd)
134 (let ((handle (get-osfhandle fd)))
136 (handle-listen handle)
139 ;;; Clear all available input from a file handle.
140 (defun handle-clear-input (handle)
141 (flush-console-input-buffer handle)
142 (with-alien ((buf (array char 1024))
145 (unless (handle-listen handle)
147 (when (zerop (read-file handle (cast buf (* t)) 1024 (addr count) nil))
152 ;;; Clear all available input from a C runtime file handle.
153 (defun fd-clear-input (fd)
154 (let ((handle (get-osfhandle fd)))
156 (handle-clear-input handle))))
158 ;;;; System Functions
161 (define-alien-routine ("Sleep@4" millisleep) void
162 (milliseconds dword))
165 (defun sb!unix:nanosleep (sec nsec)
166 (let ((*allow-with-interrupts* *interrupts-enabled*))
168 (let ((timer (sb!impl::os-create-wtimer)))
169 (sb!impl::os-set-wtimer timer sec nsec)
171 (do () ((with-local-interrupts
172 (zerop (sb!impl::os-wait-for-wtimer timer)))))
173 (sb!impl::os-close-wtimer timer))))))
177 (defvar *ansi-codepage* nil)
178 (defvar *oem-codepage* nil)
179 (defvar *codepage-to-external-format* (make-hash-table)))
183 (cp '(;;037 IBM EBCDIC - U.S./Canada
184 (437 :CP437) ;; OEM - United States
185 ;;500 IBM EBCDIC - International
186 ;;708 Arabic - ASMO 708
187 ;;709 Arabic - ASMO 449+, BCON V4
188 ;;710 Arabic - Transparent Arabic
189 ;;720 Arabic - Transparent ASMO
190 ;;737 OEM - Greek (formerly 437G)
192 (850 :CP850) ;; OEM - Multilingual Latin I
193 (852 :CP852) ;; OEM - Latin II
194 (855 :CP855) ;; OEM - Cyrillic (primarily Russian)
195 (857 :CP857) ;; OEM - Turkish
196 ;;858 OEM - Multilingual Latin I + Euro symbol
197 (860 :CP860) ;; OEM - Portuguese
198 (861 :CP861) ;; OEM - Icelandic
199 (862 :CP862) ;; OEM - Hebrew
200 (863 :CP863) ;; OEM - Canadian-French
201 (864 :CP864) ;; OEM - Arabic
202 (865 :CP865) ;; OEM - Nordic
203 (866 :CP866) ;; OEM - Russian
204 (869 :CP869) ;; OEM - Modern Greek
205 ;;870 IBM EBCDIC - Multilingual/ROECE (Latin-2)
206 (874 :CP874) ;; ANSI/OEM - Thai (same as 28605, ISO 8859-15)
207 ;;875 IBM EBCDIC - Modern Greek
208 (932 :CP932) ;; ANSI/OEM - Japanese, Shift-JIS
209 ;;936 ANSI/OEM - Simplified Chinese (PRC, Singapore)
210 ;;949 ANSI/OEM - Korean (Unified Hangul Code)
211 ;;950 ANSI/OEM - Traditional Chinese (Taiwan; Hong Kong SAR, PRC)
212 ;;1026 IBM EBCDIC - Turkish (Latin-5)
213 ;;1047 IBM EBCDIC - Latin 1/Open System
214 ;;1140 IBM EBCDIC - U.S./Canada (037 + Euro symbol)
215 ;;1141 IBM EBCDIC - Germany (20273 + Euro symbol)
216 ;;1142 IBM EBCDIC - Denmark/Norway (20277 + Euro symbol)
217 ;;1143 IBM EBCDIC - Finland/Sweden (20278 + Euro symbol)
218 ;;1144 IBM EBCDIC - Italy (20280 + Euro symbol)
219 ;;1145 IBM EBCDIC - Latin America/Spain (20284 + Euro symbol)
220 ;;1146 IBM EBCDIC - United Kingdom (20285 + Euro symbol)
221 ;;1147 IBM EBCDIC - France (20297 + Euro symbol)
222 ;;1148 IBM EBCDIC - International (500 + Euro symbol)
223 ;;1149 IBM EBCDIC - Icelandic (20871 + Euro symbol)
224 (1200 :UCS-2LE) ;; Unicode UCS-2 Little-Endian (BMP of ISO 10646)
225 (1201 :UCS-2BE) ;; Unicode UCS-2 Big-Endian
226 (1250 :CP1250) ;; ANSI - Central European
227 (1251 :CP1251) ;; ANSI - Cyrillic
228 (1252 :CP1252) ;; ANSI - Latin I
229 (1253 :CP1253) ;; ANSI - Greek
230 (1254 :CP1254) ;; ANSI - Turkish
231 (1255 :CP1255) ;; ANSI - Hebrew
232 (1256 :CP1256) ;; ANSI - Arabic
233 (1257 :CP1257) ;; ANSI - Baltic
234 (1258 :CP1258) ;; ANSI/OEM - Vietnamese
235 ;;1361 Korean (Johab)
237 ;;10001 MAC - Japanese
238 ;;10002 MAC - Traditional Chinese (Big5)
242 ;;10006 MAC - Greek I
243 (10007 :X-MAC-CYRILLIC) ;; MAC - Cyrillic
244 ;;10008 MAC - Simplified Chinese (GB 2312)
245 ;;10010 MAC - Romania
246 ;;10017 MAC - Ukraine
248 ;;10029 MAC - Latin II
249 ;;10079 MAC - Icelandic
250 ;;10081 MAC - Turkish
251 ;;10082 MAC - Croatia
252 ;;12000 Unicode UCS-4 Little-Endian
253 ;;12001 Unicode UCS-4 Big-Endian
256 ;;20002 Eten - Taiwan
257 ;;20003 IBM5550 - Taiwan
258 ;;20004 TeleText - Taiwan
259 ;;20005 Wang - Taiwan
260 ;;20105 IA5 IRV International Alphabet No. 5 (7-bit)
261 ;;20106 IA5 German (7-bit)
262 ;;20107 IA5 Swedish (7-bit)
263 ;;20108 IA5 Norwegian (7-bit)
264 ;;20127 US-ASCII (7-bit)
266 ;;20269 ISO 6937 Non-Spacing Accent
267 ;;20273 IBM EBCDIC - Germany
268 ;;20277 IBM EBCDIC - Denmark/Norway
269 ;;20278 IBM EBCDIC - Finland/Sweden
270 ;;20280 IBM EBCDIC - Italy
271 ;;20284 IBM EBCDIC - Latin America/Spain
272 ;;20285 IBM EBCDIC - United Kingdom
273 ;;20290 IBM EBCDIC - Japanese Katakana Extended
274 ;;20297 IBM EBCDIC - France
275 ;;20420 IBM EBCDIC - Arabic
276 ;;20423 IBM EBCDIC - Greek
277 ;;20424 IBM EBCDIC - Hebrew
278 ;;20833 IBM EBCDIC - Korean Extended
279 ;;20838 IBM EBCDIC - Thai
280 (20866 :KOI8-R) ;; Russian - KOI8-R
281 ;;20871 IBM EBCDIC - Icelandic
282 ;;20880 IBM EBCDIC - Cyrillic (Russian)
283 ;;20905 IBM EBCDIC - Turkish
284 ;;20924 IBM EBCDIC - Latin-1/Open System (1047 + Euro symbol)
285 ;;20932 JIS X 0208-1990 & 0121-1990
286 ;;20936 Simplified Chinese (GB2312)
287 ;;21025 IBM EBCDIC - Cyrillic (Serbian, Bulgarian)
289 (21866 :KOI8-U) ;; Ukrainian (KOI8-U)
290 (28591 :LATIN-1) ;; ISO 8859-1 Latin I
291 (28592 :ISO-8859-2) ;; ISO 8859-2 Central Europe
292 (28593 :ISO-8859-3) ;; ISO 8859-3 Latin 3
293 (28594 :ISO-8859-4) ;; ISO 8859-4 Baltic
294 (28595 :ISO-8859-5) ;; ISO 8859-5 Cyrillic
295 (28596 :ISO-8859-6) ;; ISO 8859-6 Arabic
296 (28597 :ISO-8859-7) ;; ISO 8859-7 Greek
297 (28598 :ISO-8859-8) ;; ISO 8859-8 Hebrew
298 (28599 :ISO-8859-9) ;; ISO 8859-9 Latin 5
299 (28605 :LATIN-9) ;; ISO 8859-15 Latin 9
301 (38598 :ISO-8859-8) ;; ISO 8859-8 Hebrew
302 ;;50220 ISO 2022 Japanese with no halfwidth Katakana
303 ;;50221 ISO 2022 Japanese with halfwidth Katakana
304 ;;50222 ISO 2022 Japanese JIS X 0201-1989
305 ;;50225 ISO 2022 Korean
306 ;;50227 ISO 2022 Simplified Chinese
307 ;;50229 ISO 2022 Traditional Chinese
308 ;;50930 Japanese (Katakana) Extended
309 ;;50931 US/Canada and Japanese
310 ;;50933 Korean Extended and Korean
311 ;;50935 Simplified Chinese Extended and Simplified Chinese
312 ;;50936 Simplified Chinese
313 ;;50937 US/Canada and Traditional Chinese
314 ;;50939 Japanese (Latin) Extended and Japanese
315 (51932 :EUC-JP) ;; EUC - Japanese
316 ;;51936 EUC - Simplified Chinese
318 ;;51950 EUC - Traditional Chinese
319 ;;52936 HZ-GB2312 Simplified Chinese
320 ;;54936 Windows XP: GB18030 Simplified Chinese (4 Byte)
321 ;;57002 ISCII Devanagari
322 ;;57003 ISCII Bengali
325 ;;57006 ISCII Assamese
327 ;;57008 ISCII Kannada
328 ;;57009 ISCII Malayalam
329 ;;57010 ISCII Gujarati
330 ;;57011 ISCII Punjabi
331 ;;65000 Unicode UTF-7
332 (65001 :UTF8))) ;; Unicode UTF-8
333 (setf (gethash (car cp) *codepage-to-external-format*) (cadr cp)))
336 ;; FIXME: Something odd here: why are these two #+SB-UNICODE, whereas
337 ;; the console just behave differently?
339 (declaim (ftype (function () keyword) ansi-codepage))
340 (defun ansi-codepage ()
342 (setq *ansi-codepage*
343 (gethash (alien-funcall (extern-alien "GetACP@0" (function UINT)))
344 *codepage-to-external-format*
347 (declaim (ftype (function () keyword) oem-codepage))
348 (defun oem-codepage ()
351 (gethash (alien-funcall (extern-alien "GetOEMCP@0" (function UINT)))
352 *codepage-to-external-format*
355 ;; http://msdn.microsoft.com/library/en-us/dllproc/base/getconsolecp.asp
356 (declaim (ftype (function () keyword) console-input-codepage))
357 (defun console-input-codepage ()
359 (gethash (alien-funcall (extern-alien "GetConsoleCP@0" (function UINT)))
360 *codepage-to-external-format*)
363 ;; http://msdn.microsoft.com/library/en-us/dllproc/base/getconsoleoutputcp.asp
364 (declaim (ftype (function () keyword) console-output-codepage))
365 (defun console-output-codepage ()
367 (gethash (alien-funcall
368 (extern-alien "GetConsoleOutputCP@0" (function UINT)))
369 *codepage-to-external-format*)
372 (define-alien-routine ("LocalFree@4" local-free) void
375 (defmacro cast-and-free (value &key (type 'system-string)
376 (free-function 'free-alien))
377 `(prog1 (cast ,value ,type)
378 (,free-function ,value)))
380 (eval-when (:compile-toplevel :load-toplevel :execute)
381 (defmacro with-funcname ((name description) &body body)
383 ((,name (etypecase ,description
384 (string ,description)
385 (cons (destructuring-bind (s &optional (l 0) c) ,description
386 (format nil "~A~A~A" s
387 (if c #!-sb-unicode "A@" #!+sb-unicode "W@" "@")
391 (defmacro make-system-buffer (x)
392 `(make-alien char #!+sb-unicode (ash ,x 1) #!-sb-unicode ,x))
394 ;;; FIXME: The various FOO-SYSCALL-BAR macros, and perhaps some other
395 ;;; macros in this file, are only used in this file, and could be
396 ;;; implemented using SB!XC:DEFMACRO wrapped in EVAL-WHEN.
398 (defmacro syscall ((name ret-type &rest arg-types) success-form &rest args)
399 (with-funcname (sname name)
401 (declare (optimize (sb!c::float-accuracy 0)))
402 (let ((result (alien-funcall
404 (function ,ret-type ,@arg-types))
406 (declare (ignorable result))
409 ;;; This is like SYSCALL, but if it fails, signal an error instead of
410 ;;; returning error codes. Should only be used for syscalls that will
411 ;;; never really get an error.
412 (defmacro syscall* ((name &rest arg-types) success-form &rest args)
413 (with-funcname (sname name)
415 (declare (optimize (sb!c::float-accuracy 0)))
416 (let ((result (alien-funcall
417 (extern-alien ,sname (function bool ,@arg-types))
420 (win32-error ,sname))
423 (defmacro with-sysfun ((func name ret-type &rest arg-types) &body body)
424 (with-funcname (sname name)
425 `(with-alien ((,func (function ,ret-type ,@arg-types)
429 (defmacro void-syscall* ((name &rest arg-types) &rest args)
430 `(syscall* (,name ,@arg-types) (values t 0) ,@args))
432 (defun get-last-error-message (err)
433 "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/retrieving_the_last_error_code.asp"
434 (with-alien ((amsg (* char)))
435 (syscall (("FormatMessage" 28 t)
436 dword dword dword dword dword (* (* char)) dword dword)
437 (cast-and-free amsg :free-function local-free)
438 (logior FORMAT_MESSAGE_ALLOCATE_BUFFER FORMAT_MESSAGE_FROM_SYSTEM)
439 0 err 0 (addr amsg) 0 0)))
441 (defmacro win32-error (func-name &optional err)
442 `(let ((err-code ,(or err `(get-last-error))))
443 (declare (type (unsigned-byte 32) err-code))
444 (error "~%Win32 Error [~A] - ~A~%~A"
447 (get-last-error-message err-code))))
449 (defun get-folder-namestring (csidl)
450 "http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/functions/shgetfolderpath.asp"
451 (with-alien ((apath (* char) (make-system-buffer (1+ max_path))))
452 (syscall (("SHGetFolderPath" 20 t) int handle int handle dword (* char))
453 (concatenate 'string (cast-and-free apath) "\\")
456 (defun get-folder-pathname (csidl)
457 (parse-native-namestring (get-folder-namestring csidl)))
459 (defun sb!unix:posix-getcwd ()
460 (with-alien ((apath (* char) (make-system-buffer (1+ max_path))))
461 (with-sysfun (afunc ("GetCurrentDirectory" 8 t) dword dword (* char))
462 (let ((ret (alien-funcall afunc (1+ max_path) apath)))
464 (win32-error "GetCurrentDirectory"))
465 (when (> ret (1+ max_path))
467 (setf apath (make-system-buffer ret))
468 (alien-funcall afunc ret apath))
469 (cast-and-free apath)))))
471 (defun sb!unix:unix-mkdir (name mode)
472 (declare (type sb!unix:unix-pathname name)
473 (type sb!unix:unix-file-mode mode)
475 (void-syscall* (("CreateDirectory" 8 t) system-string dword) name 0))
477 (defun sb!unix:unix-rename (name1 name2)
478 (declare (type sb!unix:unix-pathname name1 name2))
479 (void-syscall* (("MoveFile" 8 t) system-string system-string) name1 name2))
481 (defun sb!unix::posix-getenv (name)
482 (declare (type simple-string name))
483 (with-alien ((aenv (* char) (make-system-buffer default-environment-length)))
484 (with-sysfun (afunc ("GetEnvironmentVariable" 12 t)
485 dword system-string (* char) dword)
486 (let ((ret (alien-funcall afunc name aenv default-environment-length)))
487 (when (> ret default-environment-length)
489 (setf aenv (make-system-buffer ret))
490 (alien-funcall afunc name aenv ret))
493 (free-alien aenv))))))
495 ;; GET-CURRENT-PROCESS
496 ;; The GetCurrentProcess function retrieves a pseudo handle for the current
499 ;; http://msdn.microsoft.com/library/en-us/dllproc/base/getcurrentprocess.asp
500 (declaim (inline get-current-process))
501 (define-alien-routine ("GetCurrentProcess@0" get-current-process) handle)
503 ;;;; Process time information
505 (defconstant 100ns-per-internal-time-unit
506 (/ 10000000 sb!xc:internal-time-units-per-second))
509 ;; The FILETIME structure is a 64-bit value representing the number of
510 ;; 100-nanosecond intervals since January 1, 1601 (UTC).
512 ;; http://msdn.microsoft.com/library/en-us/sysinfo/base/filetime_str.asp?
513 (define-alien-type FILETIME (sb!alien:unsigned 64))
515 (defmacro with-process-times ((creation-time exit-time kernel-time user-time)
517 `(with-alien ((,creation-time filetime)
518 (,exit-time filetime)
519 (,kernel-time filetime)
520 (,user-time filetime))
521 (syscall* (("GetProcessTimes" 20) handle (* filetime) (* filetime)
522 (* filetime) (* filetime))
524 (get-current-process)
525 (addr ,creation-time)
530 (declaim (inline system-internal-real-time))
533 (declare (unsigned-byte epoch))
534 ;; FIXME: For optimization ideas see the unix implementation.
535 (defun reinit-internal-real-time ()
537 epoch (get-internal-real-time)))
538 (defun get-internal-real-time ()
539 (- (with-alien ((system-time filetime))
540 (syscall (("GetSystemTimeAsFileTime" 4) void (* filetime))
541 (values (floor system-time 100ns-per-internal-time-unit))
545 (defun system-internal-run-time ()
546 (with-process-times (creation-time exit-time kernel-time user-time)
547 (values (floor (+ user-time kernel-time) 100ns-per-internal-time-unit))))
549 (define-alien-type hword (unsigned 16))
551 (define-alien-type systemtime
560 (millisecond hword)))
562 ;; Obtained with, but the XC can't deal with that -- but
563 ;; it's not like the value is ever going to change...
564 ;; (with-alien ((filetime filetime)
565 ;; (epoch systemtime))
566 ;; (setf (slot epoch 'year) 1970
567 ;; (slot epoch 'month) 1
568 ;; (slot epoch 'day) 1
569 ;; (slot epoch 'hour) 0
570 ;; (slot epoch 'minute) 0
571 ;; (slot epoch 'second) 0
572 ;; (slot epoch 'millisecond) 0)
573 ;; (syscall (("SystemTimeToFileTime" 8) void
574 ;; (* systemtime) (* filetime))
578 (defconstant +unix-epoch-filetime+ 116444736000000000)
581 (declaim (inline get-time-of-day))
582 (defun get-time-of-day ()
583 "Return the number of seconds and microseconds since the beginning of the
584 UNIX epoch: January 1st 1970."
585 (with-alien ((system-time filetime))
586 (syscall (("GetSystemTimeAsFileTime" 4) void (* filetime))
587 (multiple-value-bind (sec 100ns)
588 (floor (- system-time +unix-epoch-filetime+)
589 (* 100ns-per-internal-time-unit
590 internal-time-units-per-second))
591 (values sec (floor 100ns 10)))
592 (addr system-time))))
595 ;; The SetEnvironmentVariable function sets the contents of the specified
596 ;; environment variable for the current process.
598 ;; http://msdn.microsoft.com/library/en-us/dllproc/base/setenvironmentvariable.asp
599 (defun setenv (name value)
600 (declare (type simple-string name value))
601 (void-syscall* (("SetEnvironmentVariable" 8 t) system-string system-string)
604 (defmacro c-sizeof (s)
605 "translate alien size (in bits) to c-size (in bytes)"
606 `(/ (alien-size ,s) 8))
609 ;; The OSVERSIONINFO data structure contains operating system version
610 ;; information. The information includes major and minor version numbers,
611 ;; a build number, a platform identifier, and descriptive text about
612 ;; the operating system. This structure is used with the GetVersionEx function.
614 ;; http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
615 (define-alien-type nil
616 (struct OSVERSIONINFO
617 (dwOSVersionInfoSize dword)
618 (dwMajorVersion dword)
619 (dwMinorVersion dword)
620 (dwBuildNumber dword)
622 (szCSDVersion (array char #!-sb-unicode 128 #!+sb-unicode 256))))
624 (defun get-version-ex ()
625 (with-alien ((info (struct OSVERSIONINFO)))
626 (setf (slot info 'dwOSVersionInfoSize) (c-sizeof (struct OSVERSIONINFO)))
627 (syscall* (("GetVersionEx" 4 t) (* (struct OSVERSIONINFO)))
628 (values (slot info 'dwMajorVersion)
629 (slot info 'dwMinorVersion)
630 (slot info 'dwBuildNumber)
631 (slot info 'dwPlatformId)
632 (cast (slot info 'szCSDVersion) system-string))
636 ;; The GetComputerName function retrieves the NetBIOS name of the local
637 ;; computer. This name is established at system startup, when the system
638 ;; reads it from the registry.
640 ;; http://msdn.microsoft.com/library/en-us/sysinfo/base/getcomputername.asp
641 (declaim (ftype (function () simple-string) get-computer-name))
642 (defun get-computer-name ()
643 (with-alien ((aname (* char) (make-system-buffer (1+ MAX_COMPUTERNAME_LENGTH)))
644 (length dword (1+ MAX_COMPUTERNAME_LENGTH)))
645 (with-sysfun (afunc ("GetComputerName" 8 t) bool (* char) (* dword))
646 (when (zerop (alien-funcall afunc aname (addr length)))
647 (let ((err (get-last-error)))
648 (unless (= err ERROR_BUFFER_OVERFLOW)
649 (win32-error "GetComputerName" err))
651 (setf aname (make-system-buffer length))
652 (alien-funcall afunc aname (addr length))))
653 (cast-and-free aname))))
655 (define-alien-routine ("SetFilePointerEx" set-file-pointer-ex) lispbool
658 (new-position long-long :out)
661 ;; File mapping support routines
662 (define-alien-routine (#!+sb-unicode "CreateFileMappingW"
663 #!-sb-unicode "CreateFileMappingA"
667 (security-attributes (* t))
669 (maximum-size-high dword)
670 (maximum-size-low dword)
671 (name (c-string #!+sb-unicode #!+sb-unicode :external-format :ucs-2)))
673 (define-alien-routine ("MapViewOfFile" map-view-of-file)
675 (file-mapping handle)
676 (desired-access dword)
681 (define-alien-routine ("UnmapViewOfFile" unmap-view-of-file) bool
684 (define-alien-routine ("FlushViewOfFile" flush-view-of-file) bool
688 ;; Constants for CreateFile `disposition'.
689 (defconstant file-create-new 1)
690 (defconstant file-create-always 2)
691 (defconstant file-open-existing 3)
692 (defconstant file-open-always 4)
693 (defconstant file-truncate-existing 5)
696 (defconstant access-generic-read #x80000000)
697 (defconstant access-generic-write #x40000000)
698 (defconstant access-generic-execute #x20000000)
699 (defconstant access-generic-all #x10000000)
700 (defconstant access-file-append-data #x4)
703 (defconstant file-share-delete #x04)
704 (defconstant file-share-read #x01)
705 (defconstant file-share-write #x02)
707 ;; CreateFile (the real file-opening workhorse)
708 (define-alien-routine (#!+sb-unicode "CreateFileW"
709 #!-sb-unicode "CreateFileA"
712 (name (c-string #!+sb-unicode #!+sb-unicode :external-format :ucs-2))
713 (desired-access dword)
715 (security-attributes (* t))
716 (creation-disposition dword)
717 (flags-and-attributes dword)
718 (template-file handle))
720 (defconstant file-attribute-readonly #x1)
721 (defconstant file-attribute-hidden #x2)
722 (defconstant file-attribute-system #x4)
723 (defconstant file-attribute-directory #x10)
724 (defconstant file-attribute-archive #x20)
725 (defconstant file-attribute-device #x40)
726 (defconstant file-attribute-normal #x80)
727 (defconstant file-attribute-temporary #x100)
728 (defconstant file-attribute-sparse #x200)
729 (defconstant file-attribute-reparse-point #x400)
730 (defconstant file-attribute-reparse-compressed #x800)
731 (defconstant file-attribute-reparse-offline #x1000)
732 (defconstant file-attribute-not-content-indexed #x2000)
733 (defconstant file-attribute-encrypted #x4000)
735 (defconstant file-flag-overlapped #x40000000)
736 (defconstant file-flag-sequential-scan #x8000000)
738 ;; GetFileAttribute is like a tiny subset of fstat(),
739 ;; enough to distinguish directories from anything else.
740 (define-alien-routine (#!+sb-unicode "GetFileAttributesW"
741 #!-sb-unicode "GetFileAttributesA"
744 (name (c-string #!+sb-unicode #!+sb-unicode :external-format :ucs-2)))
746 (define-alien-routine ("CloseHandle" close-handle) bool
749 (define-alien-routine ("_open_osfhandle" open-osfhandle)
754 ;; Intended to be an imitation of sb!unix:unix-open based on
755 ;; CreateFile, as complete as possibly.
756 ;; FILE_FLAG_OVERLAPPED is a must for decent I/O.
758 (defun unixlike-open (path flags mode &optional revertable)
759 (declare (type sb!unix:unix-pathname path)
761 (type sb!unix:unix-file-mode mode)
763 (let* ((disposition-flags
765 (if (zerop (logand sb!unix:o_creat flags)) 0 #b100)
766 (if (zerop (logand sb!unix:o_excl flags)) 0 #b010)
767 (if (zerop (logand sb!unix:o_trunc flags)) 0 #b001)))
769 ;; there are 8 combinations of creat|excl|trunc, some of
770 ;; them are equivalent. Case stmt below maps them to 5
771 ;; dispositions (see CreateFile manual).
772 (case disposition-flags
773 ((#b110 #b111) file-create-new)
774 ((#b001 #b011) file-truncate-existing)
775 ((#b000 #b010) file-open-existing)
776 (#b100 file-open-always)
777 (#b101 file-create-always))))
781 (if revertable #x10000 0)
782 (if (plusp (logand sb!unix:o_append flags))
783 access-file-append-data
785 (ecase (logand 3 flags)
786 (0 FILE_GENERIC_READ)
787 (1 FILE_GENERIC_WRITE)
788 ((2 3) (logior FILE_GENERIC_READ
789 FILE_GENERIC_WRITE))))
790 (logior FILE_SHARE_READ
795 file-attribute-normal
797 file-flag-sequential-scan)
799 (if (eql handle invalid-handle)
801 (let ((error-code (get-last-error)))
803 (#.error_file_not_found
805 ((#.error_already_exists #.error_file_exists)
807 (otherwise (- error-code)))))
809 ;; FIXME: seeking to the end is not enough for real APPEND
810 ;; semantics, but it's better than nothing.
813 ;; On the other hand, the CL spec implies the "better than
814 ;; nothing" seek-once semantics implemented here, and our
815 ;; POSIX backend is incorrect in implementing :APPEND as
816 ;; O_APPEND. Other CL implementations get this right across
819 ;; Of course, it would be nice if we had :IF-EXISTS
820 ;; :ATOMICALLY-APPEND separately as an extension, and in
821 ;; that case, we will have to worry about supporting it
824 ;; I've tested this only very briefly (on XP and Windows 7),
825 ;; but my impression is that WriteFile (without documenting
826 ;; it?) is like ZwWriteFile, i.e. if we pass in -1 as the
827 ;; offset in our overlapped structure, WriteFile seeks to the
828 ;; end for us. Should we depend on that? How do we communicate
829 ;; our desire to do so to the runtime?
832 (set-file-pointer-ex handle 0 (if (plusp (logand sb!unix::o_append flags)) 2 0))
833 (let ((fd (open-osfhandle handle (logior sb!unix::o_binary flags))))
835 (values nil (sb!unix::get-errno))
838 (define-alien-routine ("closesocket" close-socket) int (handle handle))
839 (define-alien-routine ("shutdown" shutdown-socket) int (handle handle)
842 (define-alien-routine ("DuplicateHandle" duplicate-handle) lispbool
843 (from-process handle)
846 (to-handle handle :out)
851 (defconstant +handle-flag-inherit+ 1)
852 (defconstant +handle-flag-protect-from-close+ 2)
854 (define-alien-routine ("SetHandleInformation" set-handle-information) lispbool
859 (define-alien-routine ("GetHandleInformation" get-handle-information) lispbool
863 (define-alien-routine getsockopt int
867 (dataword int-ptr :in-out)
868 (socklen int :in-out))
870 (defconstant sol_socket #xFFFF)
871 (defconstant so_type #x1008)
873 (defun socket-handle-p (handle)
874 (zerop (getsockopt handle sol_socket so_type 0 (alien-size int :bytes))))
876 (defconstant ebadf 9)
878 ;;; For sockets, CloseHandle first and closesocket() afterwards is
879 ;;; legal: winsock tracks its handles separately (that's why we have
880 ;;; the problem with simple _close in the first place).
882 ;;; ...Seems to be the problem on some OSes, though. We could
883 ;;; duplicate a handle and attempt close-socket on a duplicated one,
884 ;;; but it also have some problems...
886 ;;; For now, we protect socket handle from close with SetHandleInformation,
887 ;;; then call CRT _close() that fails to close a handle but _gets rid of fd_,
888 ;;; and then we close a handle ourserves.
890 (defun unixlike-close (fd)
891 (let ((handle (get-osfhandle fd)))
892 (flet ((close-protection (enable)
893 (set-handle-information handle 2 (if enable 2 0))))
894 (if (= handle invalid-handle)
897 (when (and (socket-handle-p handle) (close-protection t))
898 (shutdown-socket handle 2)
899 (alien-funcall (extern-alien "_dup2" (function int int int)) 0 fd)
900 (close-protection nil)
901 (close-socket handle))
902 (sb!unix::void-syscall ("close" int) fd))))))