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