1 /* System description file for Windows NT.
2
3 Copyright (C) 1993-1995, 2001-2023 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
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
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
19
20 /* Define symbols to identify the version of Unix this is.
21 Define all the symbols that apply correctly. */
22
23 #ifndef WINDOWSNT
24 #define WINDOWSNT
25 #endif
26
27 #include <mingw_time.h>
28
29 /* MinGW-w64 gcc does not automotically define a macro for
30 differentiating it from MinGW gcc. We need to test the presence of
31 __MINGW64_VERSION_MAJOR in _mingw.h: */
32 #ifdef __MINGW32__
33 # include <_mingw.h>
34 # ifdef __MINGW64_VERSION_MAJOR
35 # define MINGW_W64
36 # endif
37 # if defined __MINGW32_VERSION && __MINGW32_VERSION >= 5001000L
38 /* Avoid warnings about gettimeofday being deprecated. */
39 # undef __POSIX_2008_DEPRECATED
40 # define __POSIX_2008_DEPRECATED
41 # endif
42 /* Old versions of MinGW don't have these in the w32api headers, and
43 Gnulib uses them in some files. */
44 # ifndef _WIN32_WINNT_WIN2K
45 # define _WIN32_WINNT_WIN2K 0x0500
46 # endif
47 # ifndef _WIN32_WINNT_WINXP
48 # define _WIN32_WINNT_WINXP 0x0501
49 # endif
50 # ifndef _WIN32_WINNT_WS03
51 # define _WIN32_WINNT_WS03 0x0502
52 # endif
53 # ifndef _WIN32_WINNT_VISTA
54 # define _WIN32_WINNT_VISTA 0x0600
55 # endif
56 # ifndef _WIN32_WINNT_WIN7
57 # define _WIN32_WINNT_WIN7 0x0601
58 # endif
59 # ifndef _WIN32_WINNT_WIN8
60 # define _WIN32_WINNT_WIN8 0x0602
61 # endif
62 # ifndef _WIN32_WINNT_WINBLUE
63 # define _WIN32_WINNT_WINBLUE 0x0603
64 # endif
65 # ifndef _WIN32_WINNT_WIN10
66 # define _WIN32_WINNT_WIN10 0x0A00
67 # endif
68 #endif
69
70 /* #undef const */
71
72 /* Number of chars of output in the buffer of a stdio stream. */
73 #ifdef __GNU_LIBRARY__
74 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
75 #else
76 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
77 #endif
78
79 /* If you are compiling with a non-C calling convention but need to
80 declare vararg routines differently, put it here. */
81 #define _VARARGS_ __cdecl
82
83 /* If you are providing a function to something that will call the
84 function back (like a signal handler and signal, or main) its calling
85 convention must be whatever standard the libraries expect. */
86 #define _CALLBACK_ __cdecl
87
88 /* Define HAVE_TIMEVAL if the system supports the BSD style clock values.
89 Look in <sys/time.h> for a timeval structure. */
90 #define HAVE_TIMEVAL 1
91
92 /* Our select emulation does 1-byte read-ahead waiting for received
93 packets, so datagrams are broken. */
94 #define BROKEN_DATAGRAM_SOCKETS 1
95
96 #define MAIL_USE_SYSTEM_LOCK 1
97
98 /* Define to 1 if GCC-style __attribute__ ((__aligned__ (expr))) works. */
99 #ifdef __GNUC__
100 #define HAVE_ATTRIBUTE_ALIGNED 1
101 #endif
102
103 /* Define to 1 if strtold conforms to C99. */
104 #ifdef __GNUC__
105 #define HAVE_C99_STRTOLD 1
106 #endif
107
108 #if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
109 # ifndef HAVE___BUILTIN_UNWIND_INIT
110 # define HAVE___BUILTIN_UNWIND_INIT 1
111 # endif
112 #endif
113
114 /* This isn't perfect, as some systems might have the page file in
115 another place. Also, I suspect that the time stamp of that file
116 might also change when Windows enlarges the file due to
117 insufficient VM. Still, this seems to be the most reliable way;
118 the alternative (of using GetSystemTimes) won't work on laptops
119 that hibernate, because the system clock is stopped then. Other
120 possibility would be to run "net statistics workstation" and parse
121 the output, but that's gross. So this should do; if the file is
122 not there, the boot time will be returned as zero, and filelock.c
123 already handles that. */
124 #define BOOT_TIME_FILE "C:/pagefile.sys"
125
126 /* ============================================================ */
127
128 /* Here, add any special hacks needed to make Emacs work on this
129 system. For example, you might define certain system call names
130 that don't exist on your system, or that do different things on
131 your system and must be used only through an encapsulation (which
132 you should place, by convention, in sysdep.c). */
133
134 #ifdef __GNUC__
135 #ifndef __cplusplus
136 #undef inline
137 #endif
138 #else /* MSVC */
139 #define inline __inline
140 #endif
141
142 #ifdef __GNUC__
143 /* config.h may have defined already. */
144 # ifndef restrict
145 # define restrict __restrict__
146 # endif
147 #else
148 /* FIXME: should we define to __restrict, which MSVC supports? */
149 # define restrict
150 #endif
151
152 /* `mode_t' is not defined for MSVC. Define. */
153 #ifdef _MSC_VER
154 typedef unsigned short mode_t;
155 #endif
156
157 /* A va_copy replacement for MSVC. */
158 #ifdef _MSC_VER
159 # ifdef _WIN64
160 # ifndef va_copy /* Need to be checked (?) */
161 # define va_copy(d,s) ((d) = (s))
162 # endif
163 # else /* not _WIN64 */
164 # define va_copy(d,s) ((d) = (s))
165 # endif /* not _WIN64 */
166 #endif /* _MSC_VER */
167
168 #ifndef WINDOWSNT
169 /* Some of the files of Emacs which are intended for use with other
170 programs assume that if you have a config.h file, you must declare
171 the type of getenv. */
172 extern char *getenv ();
173 #endif
174
175 /* Prevent accidental use of features unavailable in older Windows
176 versions we still support. MinGW64 defines this to a higher value
177 in its system headers, and is not really compatible with values
178 lower than 0x0500, so leave it alone. */
179 #ifndef MINGW_W64
180 # undef _WIN32_WINNT
181 # define _WIN32_WINNT 0x0400
182 #endif
183
184 /* Make a leaner executable. */
185 #define WIN32_LEAN_AND_MEAN
186
187 #include <sys/types.h>
188
189 #ifndef MAXPATHLEN
190 #define MAXPATHLEN _MAX_PATH
191 #endif
192
193 /* This is used to hold UTF-8 encoded file names. */
194 #define MAX_UTF8_PATH (MAXPATHLEN * 4)
195
196 #ifdef HAVE_NTGUI
197 # ifndef HAVE_WINDOW_SYSTEM
198 # define HAVE_WINDOW_SYSTEM 1
199 # define POLL_FOR_INPUT 1
200 # endif
201 #endif
202
203 /* Get some redefinitions in place. */
204
205 #ifdef emacs
206
207 #ifdef MINGW_W64
208 /* MinGW64 specific stuff. */
209 /* Make sure 'struct timespec' and 'struct timezone' are defined. */
210 #include <sys/types.h>
211 #include <time.h>
212 /* This prototype avoids MinGW64 compiler warnings due to the fact
213 that time.h is included before localtime is redirected to
214 sys_localtime below. */
215 extern struct tm * sys_localtime (const time_t *);
216 /* MinGW64 uses a 2-argument _setjmp, and setjmp is a macro defined to
217 supply the 2nd arg correctly, so don't use _setjmp directly in that
218 case. */
219 #undef HAVE__SETJMP
220
221 /* Unlike MS and mingw.org, MinGW64 doesn't define gai_strerror as an
222 inline function in a system header file, and instead seems to
223 require to link against ws2_32.a. But we don't want to link with
224 -lws2_32, as that would make Emacs dependent on the respective DLL.
225 So MinGW64 is amply punished here by the following: */
226 #undef HAVE_GAI_STRERROR
227 #endif
228
229 /* The following is needed for recovery from C stack overflows. */
230 #include <setjmp.h>
231 typedef jmp_buf sigjmp_buf;
232 #ifdef MINGW_W64
233 /* Evidently, MinGW64's longjmp crashes when invoked from an exception
234 handler, see https://sourceforge.net/p/mingw-w64/mailman/message/32421953/.
235 This seems to be an unsolved problem in the MinGW64 runtime. So we
236 use the GCC intrinsics instead. FIXME. */
237 #define sigsetjmp(j,m) __builtin_setjmp(j)
238 #else
239 #define sigsetjmp(j,m) setjmp(j)
240 #endif
241 extern void w32_reset_stack_overflow_guard (void);
242
243 #ifdef _MSC_VER
244 #include <sys/timeb.h>
245 #include <sys/stat.h>
246 #include <signal.h>
247
248 /* MSVC gets link-time errors without these redirections. */
249 #define fstat(a, b) sys_fstat(a, b)
250 #define stat(a, b) sys_stat(a, b)
251 #define utime sys_utime
252 #endif
253
254 /* Calls that are emulated or shadowed. */
255 #undef chdir
256 #define chdir sys_chdir
257 #undef chmod
258 #define chmod sys_chmod
259 #undef close
260 #define close sys_close
261 #undef creat
262 #define creat sys_creat
263 #define ctime sys_ctime
264 #undef dup
265 #define dup sys_dup
266 #undef dup2
267 #define dup2 sys_dup2
268 #define fopen sys_fopen
269 #define link sys_link
270 #define localtime sys_localtime
271 #undef read
272 #define read sys_read
273 #define rename sys_rename
274 #define rmdir sys_rmdir
275 #define select sys_select
276 #define pselect sys_select
277 #define sleep sys_sleep
278 #define strerror sys_strerror
279 #undef unlink
280 #define unlink sys_unlink
281 #undef opendir
282 #define opendir sys_opendir
283 #undef closedir
284 #define closedir sys_closedir
285 #undef readdir
286 #define readdir sys_readdir
287 #undef seekdir
288 #define seekdir sys_seekdir
289 /* This prototype is needed because some files include config.h
290 _after_ the standard headers, so sys_unlink gets no prototype from
291 stdio.h or io.h. */
292 extern int sys_unlink (const char *);
293 #undef write
294 #define write sys_write
295 #undef umask
296 #define umask sys_umask
297 extern int sys_umask (int);
298 #define clock sys_clock
299
300 /* Subprocess calls that are emulated. */
301 #define spawnve sys_spawnve
302 #define kill sys_kill
303 #define signal sys_signal
304
305 /* Internal signals. */
306 #define emacs_raise(sig) emacs_abort()
307
308 /* termcap.c calls that are emulated. */
309 #define tputs sys_tputs
310 #define tgetstr sys_tgetstr
311
312 /* cm.c calls that are emulated. */
313 #define chcheckmagic sys_chcheckmagic
314 #define cmcostinit sys_cmcostinit
315 #define cmgoto sys_cmgoto
316 #define cmputc sys_cmputc
317 #define Wcm_clear sys_Wcm_clear
318
319 #endif /* emacs */
320
321 /* Used both in Emacs, in lib-src, and in Gnulib. */
322 #undef open
323 #define open sys_open
324
325 /* Map to MSVC names. */
326 #define execlp _execlp
327 #define execvp _execvp
328 #include <stdint.h> /* for intptr_t */
329 extern intptr_t _execvp (const char *, char **);
330 #define tcdrain _commit
331 #define fdopen _fdopen
332 #define fsync _commit
333 #define ftruncate _chsize
334 #define getpid _getpid
335 #ifdef _MSC_VER
336 typedef int pid_t;
337 #define snprintf _snprintf
338 #define strtoll _strtoi64
339 #define copysign _copysign
340 #endif
341 #define isatty _isatty
342 #define _longjmp longjmp
343 /* MinGW64 defines lseek to invoke lseek64. */
344 #ifndef lseek
345 #define lseek _lseek
346 #endif
347 #define popen _popen
348 #define pclose _pclose
349 #define strdup _strdup
350 #define strupr _strupr
351 #define strnicmp _strnicmp
352 #define stricmp _stricmp
353 #define tzset _tzset
354
355 /* We cannot include system header process.h, since there's src/process.h. */
356 int _getpid (void);
357
358 /* Include time.h before redirecting tzname, since MSVC's time.h
359 defines _tzname to call a function, but also declares tzname a
360 2-element array. Having the redirection before including the
361 header thus has the effect of declaring a function that returns an
362 array, and triggers an error message. */
363 #include <time.h>
364 #define tzname _tzname
365
366 /* Required for functions in lib/time_r.c, since we don't use lib/time.h. */
367 extern struct tm *gmtime_r (time_t const * restrict, struct tm * restrict);
368 extern struct tm *localtime_r (time_t const * restrict, struct tm * restrict);
369
370 #ifdef _MSC_VER
371 /* This is hacky, but is necessary to avoid warnings about macro
372 redefinitions using the MSVC compilers, since, when __STDC__ is
373 undefined or zero, those compilers declare functions like fileno,
374 lseek, and chdir, for which we defined macros above. */
375 #ifndef __STDC__
376 #define __STDC__ 1
377 #define MUST_UNDEF__STDC__
378 #endif
379 #include <direct.h>
380 #include <io.h>
381 #include <stdio.h>
382 #ifdef MUST_UNDEF__STDC__
383 #undef __STDC__
384 #undef MUST_UNDEF__STDC__
385 #endif
386 #else /* !_MSC_VER */
387 #include <direct.h>
388 #include <io.h>
389 #include <stdio.h>
390 #endif /* !_MSC_VER */
391 #ifndef fileno
392 #define fileno _fileno
393 #endif
394
395 /* Defines that we need that aren't in the standard signal.h. */
396 #define SIGHUP 1 /* Hang up */
397 #define SIGQUIT 3 /* Quit process */
398 #define SIGTRAP 5 /* Trace trap */
399 #define SIGKILL 9 /* Die, die die */
400 #define SIGPIPE 13 /* Write on pipe with no readers */
401 #define SIGALRM 14 /* Alarm */
402 #define SIGCHLD 18 /* Death of child */
403 #define SIGPROF 19 /* Profiling */
404
405 #ifndef NSIG
406 #define NSIG 23
407 #endif
408
409 #ifndef ENOTSUP
410 #define ENOTSUP ENOSYS
411 #endif
412
413 /* In case lib/errno.h is not used. */
414 #ifndef EOPNOTSUPP
415 #define EOPNOTSUPP 130
416 #endif
417
418 #ifdef _MSC_VER
419 typedef int sigset_t;
420 typedef int ssize_t;
421 #endif
422
423 #ifdef MINGW_W64
424 #ifndef _POSIX
425 typedef _sigset_t sigset_t;
426 #endif
427 #endif
428
429 typedef void (_CALLBACK_ *signal_handler) (int);
430 extern signal_handler sys_signal (int, signal_handler);
431
432 struct sigaction {
433 int sa_flags;
434 void (_CALLBACK_ *sa_handler)(int);
435 sigset_t sa_mask;
436 };
437 #define SA_RESTART 0
438 #define SIG_BLOCK 1
439 #define SIG_SETMASK 2
440 #define SIG_UNBLOCK 3
441
442 extern int sigemptyset (sigset_t *);
443 extern int sigaddset (sigset_t *, int);
444 extern int sigfillset (sigset_t *);
445 extern int sigprocmask (int, const sigset_t *, sigset_t *);
446 /* MinGW64 defines pthread_sigmask as zero in its pthread_signal.h
447 header, but we have an implementation for that function in w32proc.c. */
448 #ifdef pthread_sigmask
449 #undef pthread_sigmask
450 #endif
451 extern int pthread_sigmask (int, const sigset_t *, sigset_t *);
452 extern int sigismember (const sigset_t *, int);
453 extern int setpgrp (int, int);
454 extern int sigaction (int, const struct sigaction *, struct sigaction *);
455 extern int alarm (int);
456
457 extern int sys_kill (pid_t, int);
458
459 extern void explicit_bzero (void *, size_t);
460
461 /* For integration with MSDOS support. */
462 #define getdisk() (_getdrive () - 1)
463 #ifdef emacs
464 #define getdefdir(_drv, _buf) ((_buf[0] = (_drv + 'A' - 1), _buf[1] = ':', _buf[2] = '/', _buf[3] = 0), 1)
465 #else
466 #define getdefdir(_drv, _buf) _getdcwd (_drv, _buf, MAXPATHLEN)
467 #endif
468
469 #ifndef EMACS_CONFIGURATION
470 extern char *get_emacs_configuration (void);
471 extern char *get_emacs_configuration_options (void);
472 #define EMACS_CONFIGURATION get_emacs_configuration ()
473 #define EMACS_CONFIG_OPTIONS get_emacs_configuration_options ()
474 #endif
475
476 /* Define this so that winsock.h definitions don't get included with
477 windows.h. For this to have proper effect, config.h must always be
478 included before windows.h. */
479 #define _WINSOCKAPI_ 1
480 #if !(defined __MINGW32_VERSION && __MINGW32_VERSION >= 5000002L)
481 /* mingw.org's MinGW 5.x changed how it includes winsock.h and time.h,
482 and now defining _WINSOCK_H skips the definition of struct timeval,
483 which we don't want. */
484 # define _WINSOCK_H
485 #endif
486
487 /* Defines size_t and alloca (). */
488 #include <stdlib.h>
489 #include <sys/stat.h>
490 #ifdef _MSC_VER
491 #define alloca _alloca
492 #else
493 #include <malloc.h>
494 #endif
495
496 /* Needed in Emacs and in Gnulib. */
497 /* This must be after including sys/stat.h, because we need mode_t. */
498 #undef mkdir
499 #define mkdir(d,f) sys_mkdir(d,f)
500 int sys_mkdir (const char *, mode_t);
501
502 #ifdef emacs
503
504 typedef void * (* malloc_fn)(size_t);
505 typedef void * (* realloc_fn)(void *, size_t);
506 typedef void (* free_fn)(void *);
507
508 extern void *malloc_before_dump(size_t);
509 extern void *realloc_before_dump(void *, size_t);
510 extern void free_before_dump(void *);
511 extern void *malloc_after_dump(size_t);
512 extern void *realloc_after_dump(void *, size_t);
513 extern void free_after_dump(void *);
514
515 extern void *malloc_after_dump_9x(size_t);
516 extern void *realloc_after_dump_9x(void *, size_t);
517 extern void free_after_dump_9x(void *);
518
519 extern void *sys_calloc(size_t, size_t);
520
521 extern malloc_fn the_malloc_fn;
522 extern realloc_fn the_realloc_fn;
523 extern free_fn the_free_fn;
524
525 #define malloc(size) (*the_malloc_fn)(size)
526 #define free(ptr) (*the_free_fn)(ptr)
527 #define realloc(ptr, size) (*the_realloc_fn)(ptr, size)
528 #define calloc(num, size) sys_calloc(num, size)
529
530 #endif
531
532 /* Define for those source files that do not include enough NT system files. */
533 #ifndef NULL
534 #ifdef __cplusplus
535 #define NULL 0
536 #else
537 #define NULL ((void *)0)
538 #endif
539 #endif
540
541 /* For proper declaration of environ. */
542 #ifndef sys_nerr
543 #define sys_nerr _sys_nerr
544 #endif
545
546 /* This must be after including stdlib.h, which defines putenv on MinGW. */
547 #ifdef putenv
548 # undef putenv
549 #endif
550 #define putenv sys_putenv
551 extern int sys_putenv (char *);
552
553 extern int getloadavg (double *, int);
554 extern int getpagesize (void);
555
556 extern void * memrchr (void const *, int, size_t);
557
558 /* Declared here, since we don't use Gnulib's stdlib.h. */
559 extern int mkostemp (char *, int);
560
561 #if defined (__MINGW32__)
562
563 /* Define to 1 if the system has the type `long long int'. */
564 # ifndef HAVE_LONG_LONG_INT
565 # define HAVE_LONG_LONG_INT 1
566 # endif
567
568 /* Define to 1 if the system has the type `unsigned long long int'. */
569 # ifndef HAVE_UNSIGNED_LONG_LONG_INT
570 # define HAVE_UNSIGNED_LONG_LONG_INT 1
571 # endif
572
573 #endif
574
575 #ifdef _MSC_VER
576 # if defined(_WIN64)
577 typedef __int64 EMACS_INT;
578 typedef unsigned __int64 EMACS_UINT;
579 # define EMACS_INT_MAX LLONG_MAX
580 # define PRIuMAX "llu"
581 # define pI "ll"
582 /* Fix a bug in MSVC headers : stdint.h */
583 # define _INTPTR 2
584 # elif defined(_WIN32)
585 /* Temporarily disable wider-than-pointer integers until they're tested more.
586 Build with CFLAGS='-DWIDE_EMACS_INT' to try them out. */
587
588 # ifdef WIDE_EMACS_INT
589
590 /* Use pre-C99-style 64-bit integers. */
591 typedef __int64 EMACS_INT;
592 typedef unsigned __int64 EMACS_UINT;
593 # define EMACS_INT_MAX LLONG_MAX
594 # define PRIuMAX "llu"
595 # define pI "I64"
596 # else
597 typedef int EMACS_INT;
598 typedef unsigned int EMACS_UINT;
599 # define EMACS_INT_MAX LONG_MAX
600 # define PRIuMAX "lu"
601 # define pI "l"
602 # endif
603 # endif
604 #endif
605
606 /* We need a little extra space, see ../../lisp/loadup.el. */
607 #define SYSTEM_PURESIZE_EXTRA 50000
608
609 #define DATA_START get_data_start ()
610
611 /* For unexec to work on Alpha systems, we need to put Emacs'
612 initialized data into a separate section from the CRT initialized
613 data (because the Alpha linker freely reorders data variables, even
614 across libraries, so our data and the CRT data get intermingled).
615
616 Starting with MSVC 5.0, we must also place the uninitialized data
617 into its own section. VC5 intermingles uninitialized data from the CRT
618 between Emacs' static uninitialized data and its public uninitialized
619 data. A separate .bss section for Emacs groups both static and
620 public uninitialized together.
621
622 Note that unexw32.c relies on this fact, and must be modified
623 accordingly if this section name is changed, or if this pragma is
624 removed. Also, obviously, all files that define initialized data
625 must include config.h to pick up this pragma. */
626
627 /* Names must be < 8 bytes. */
628 #ifdef _MSC_VER
629 #pragma data_seg("EMDATA")
630 #pragma bss_seg("EMBSS")
631 #endif
632
633 /* #define FULL_DEBUG */
634 /* #define EMACSDEBUG */
635
636 #ifdef _MSC_VER
637 #if _MSC_VER >= 800 && !defined(__cplusplus)
638 /* Unnamed type definition in parentheses.
639 A structure, union, or enumerated type with no name is defined in a
640 parenthetical expression. The type definition is meaningless. */
641 #pragma warning(disable:4116)
642 /* 'argument' : conversion from 'type1' to 'type2', possible loss of
643 data A floating point type was converted to an integer type. A
644 possible loss of data may have occurred. */
645 #pragma warning(disable:4244)
646 /* Negative integral constant converted to unsigned type.
647 An expression converts a negative integer constant to an unsigned type.
648 The result of the expression is probably meaningless. */
649 #pragma warning(disable:4308)
650 #endif
651 #endif
652
653 /* Event name for when emacsclient starts the Emacs daemon on Windows. */
654 #define W32_DAEMON_EVENT "EmacsServerEvent"
655
656 /* ============================================================ */