Move DEFSYM to lisp.h and use everywhere.
[bpt/emacs.git] / src / w32proc.c
CommitLineData
e9e23e23 1/* Process support for GNU Emacs on the Microsoft W32 API.
73b0cd50 2 Copyright (C) 1992, 1995, 1999-2011 Free Software Foundation, Inc.
6cdfb6e6 3
3b7ad313
EN
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
3b7ad313 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
3b7ad313
EN
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
6cdfb6e6 18
9ec0b715 19/*
6cdfb6e6
RS
20 Drew Bliss Oct 14, 1993
21 Adapted from alarm.c by Tim Fleehart
22*/
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <io.h>
c519b5e1 28#include <fcntl.h>
6cdfb6e6 29#include <signal.h>
51f635c4 30#include <sys/file.h>
d7306fe6 31#include <setjmp.h>
6cdfb6e6 32
c519b5e1 33/* must include CRT headers *before* config.h */
4838e624 34#include <config.h>
4838e624 35
c519b5e1
GV
36#undef signal
37#undef wait
38#undef spawnve
39#undef select
40#undef kill
41
6cdfb6e6 42#include <windows.h>
42c95ffb
AI
43#ifdef __GNUC__
44/* This definition is missing from mingw32 headers. */
ed3751c8 45extern BOOL WINAPI IsValidLocale (LCID, DWORD);
42c95ffb 46#endif
6cdfb6e6 47
d613418b
EZ
48#ifdef HAVE_LANGINFO_CODESET
49#include <nl_types.h>
50#include <langinfo.h>
51#endif
52
6cdfb6e6 53#include "lisp.h"
489f9371 54#include "w32.h"
b2fc9f3d 55#include "w32heap.h"
6cdfb6e6 56#include "systime.h"
3d7eead0
GV
57#include "syswait.h"
58#include "process.h"
e7c15bba 59#include "syssignal.h"
ef79fbba 60#include "w32term.h"
f481eb31 61#include "dispextern.h" /* for xstrcasecmp */
b23077df 62#include "coding.h"
3d7eead0 63
8747ac3f
EZ
64#define RVA_TO_PTR(var,section,filedata) \
65 ((void *)((section)->PointerToRawData \
66 + ((DWORD)(var) - (section)->VirtualAddress) \
67 + (filedata).file_base))
68
b2fc9f3d 69Lisp_Object Qhigh, Qlow;
817abdf6 70
6cdfb6e6 71#ifdef EMACSDEBUG
b56ceb92
JB
72void
73_DebPrint (const char *fmt, ...)
6cdfb6e6 74{
c519b5e1 75 char buf[1024];
6cdfb6e6
RS
76 va_list args;
77
78 va_start (args, fmt);
79 vsprintf (buf, fmt, args);
80 va_end (args);
81 OutputDebugString (buf);
82}
83#endif
84
ed3751c8 85typedef void (_CALLBACK_ *signal_handler) (int);
6cdfb6e6
RS
86
87/* Signal handlers...SIG_DFL == 0 so this is initialized correctly. */
88static signal_handler sig_handlers[NSIG];
89
90/* Fake signal implementation to record the SIGCHLD handler. */
177c0ea7 91signal_handler
c519b5e1 92sys_signal (int sig, signal_handler handler)
6cdfb6e6
RS
93{
94 signal_handler old;
177c0ea7 95
6cdfb6e6
RS
96 if (sig != SIGCHLD)
97 {
98 errno = EINVAL;
99 return SIG_ERR;
100 }
101 old = sig_handlers[sig];
102 sig_handlers[sig] = handler;
103 return old;
104}
105
c519b5e1
GV
106/* Defined in <process.h> which conflicts with the local copy */
107#define _P_NOWAIT 1
108
109/* Child process management list. */
110int child_proc_count = 0;
111child_process child_procs[ MAX_CHILDREN ];
112child_process *dead_child = NULL;
113
24f981c9 114static DWORD WINAPI reader_thread (void *arg);
c519b5e1 115
6cdfb6e6 116/* Find an unused process slot. */
c519b5e1 117child_process *
6cdfb6e6
RS
118new_child (void)
119{
120 child_process *cp;
c519b5e1 121 DWORD id;
177c0ea7 122
9d4f32e8 123 for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
6cdfb6e6 124 if (!CHILD_ACTIVE (cp))
c519b5e1
GV
125 goto Initialise;
126 if (child_proc_count == MAX_CHILDREN)
127 return NULL;
128 cp = &child_procs[child_proc_count++];
129
130 Initialise:
ed3751c8 131 memset (cp, 0, sizeof (*cp));
c519b5e1
GV
132 cp->fd = -1;
133 cp->pid = -1;
134 cp->procinfo.hProcess = NULL;
135 cp->status = STATUS_READ_ERROR;
136
137 /* use manual reset event so that select() will function properly */
138 cp->char_avail = CreateEvent (NULL, TRUE, FALSE, NULL);
139 if (cp->char_avail)
140 {
141 cp->char_consumed = CreateEvent (NULL, FALSE, FALSE, NULL);
142 if (cp->char_consumed)
143 {
144 cp->thrd = CreateThread (NULL, 1024, reader_thread, cp, 0, &id);
145 if (cp->thrd)
146 return cp;
147 }
148 }
149 delete_child (cp);
150 return NULL;
151}
152
177c0ea7 153void
c519b5e1
GV
154delete_child (child_process *cp)
155{
156 int i;
157
158 /* Should not be deleting a child that is still needed. */
159 for (i = 0; i < MAXDESC; i++)
160 if (fd_info[i].cp == cp)
161 abort ();
162
163 if (!CHILD_ACTIVE (cp))
164 return;
165
166 /* reap thread if necessary */
167 if (cp->thrd)
168 {
169 DWORD rc;
170
171 if (GetExitCodeThread (cp->thrd, &rc) && rc == STILL_ACTIVE)
172 {
173 /* let the thread exit cleanly if possible */
174 cp->status = STATUS_READ_ERROR;
175 SetEvent (cp->char_consumed);
a017b515
JR
176#if 0
177 /* We used to forceably terminate the thread here, but it
178 is normally unnecessary, and in abnormal cases, the worst that
179 will happen is we have an extra idle thread hanging around
180 waiting for the zombie process. */
c519b5e1
GV
181 if (WaitForSingleObject (cp->thrd, 1000) != WAIT_OBJECT_0)
182 {
183 DebPrint (("delete_child.WaitForSingleObject (thread) failed "
184 "with %lu for fd %ld\n", GetLastError (), cp->fd));
185 TerminateThread (cp->thrd, 0);
186 }
a017b515 187#endif
c519b5e1
GV
188 }
189 CloseHandle (cp->thrd);
190 cp->thrd = NULL;
191 }
192 if (cp->char_avail)
193 {
194 CloseHandle (cp->char_avail);
195 cp->char_avail = NULL;
196 }
197 if (cp->char_consumed)
198 {
199 CloseHandle (cp->char_consumed);
200 cp->char_consumed = NULL;
201 }
202
203 /* update child_proc_count (highest numbered slot in use plus one) */
204 if (cp == child_procs + child_proc_count - 1)
205 {
206 for (i = child_proc_count-1; i >= 0; i--)
207 if (CHILD_ACTIVE (&child_procs[i]))
208 {
209 child_proc_count = i + 1;
210 break;
211 }
212 }
213 if (i < 0)
214 child_proc_count = 0;
6cdfb6e6
RS
215}
216
217/* Find a child by pid. */
218static child_process *
219find_child_pid (DWORD pid)
220{
221 child_process *cp;
c519b5e1 222
9d4f32e8 223 for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
6cdfb6e6
RS
224 if (CHILD_ACTIVE (cp) && pid == cp->pid)
225 return cp;
226 return NULL;
227}
228
6cdfb6e6 229
c519b5e1
GV
230/* Thread proc for child process and socket reader threads. Each thread
231 is normally blocked until woken by select() to check for input by
04bf5b65 232 reading one char. When the read completes, char_avail is signaled
c519b5e1 233 to wake up the select emulator and the thread blocks itself again. */
24f981c9 234static DWORD WINAPI
6cdfb6e6
RS
235reader_thread (void *arg)
236{
237 child_process *cp;
177c0ea7 238
6cdfb6e6
RS
239 /* Our identity */
240 cp = (child_process *)arg;
177c0ea7 241
6cdfb6e6 242 /* We have to wait for the go-ahead before we can start */
b2fc9f3d
GV
243 if (cp == NULL
244 || WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
c519b5e1
GV
245 return 1;
246
6cdfb6e6
RS
247 for (;;)
248 {
c519b5e1
GV
249 int rc;
250
f9125cde
KS
251 if (fd_info[cp->fd].flags & FILE_LISTEN)
252 rc = _sys_wait_accept (cp->fd);
253 else
254 rc = _sys_read_ahead (cp->fd);
c519b5e1
GV
255
256 /* The name char_avail is a misnomer - it really just means the
257 read-ahead has completed, whether successfully or not. */
6cdfb6e6
RS
258 if (!SetEvent (cp->char_avail))
259 {
260 DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
261 GetLastError (), cp->fd));
c519b5e1
GV
262 return 1;
263 }
264
265 if (rc == STATUS_READ_ERROR)
266 return 1;
177c0ea7 267
6cdfb6e6 268 /* If the read died, the child has died so let the thread die */
c519b5e1 269 if (rc == STATUS_READ_FAILED)
6cdfb6e6 270 break;
177c0ea7 271
6cdfb6e6
RS
272 /* Wait until our input is acknowledged before reading again */
273 if (WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
274 {
275 DebPrint (("reader_thread.WaitForSingleObject failed with "
276 "%lu for fd %ld\n", GetLastError (), cp->fd));
277 break;
278 }
279 }
280 return 0;
281}
282
b2fc9f3d
GV
283/* To avoid Emacs changing directory, we just record here the directory
284 the new process should start in. This is set just before calling
285 sys_spawnve, and is not generally valid at any other time. */
286static char * process_dir;
287
177c0ea7 288static BOOL
a55a5f3c 289create_child (char *exe, char *cmdline, char *env, int is_gui_app,
c519b5e1 290 int * pPid, child_process *cp)
6cdfb6e6 291{
6cdfb6e6
RS
292 STARTUPINFO start;
293 SECURITY_ATTRIBUTES sec_attrs;
42c95ffb 294#if 0
6cdfb6e6 295 SECURITY_DESCRIPTOR sec_desc;
42c95ffb 296#endif
82e7c0a9 297 DWORD flags;
b2fc9f3d 298 char dir[ MAXPATHLEN ];
177c0ea7 299
c519b5e1 300 if (cp == NULL) abort ();
177c0ea7 301
6cdfb6e6
RS
302 memset (&start, 0, sizeof (start));
303 start.cb = sizeof (start);
177c0ea7 304
58d4e829 305#ifdef HAVE_NTGUI
a55a5f3c 306 if (NILP (Vw32_start_process_show_window) && !is_gui_app)
0ecf7d36
RS
307 start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
308 else
309 start.dwFlags = STARTF_USESTDHANDLES;
58d4e829
GV
310 start.wShowWindow = SW_HIDE;
311
312 start.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
313 start.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
314 start.hStdError = GetStdHandle (STD_ERROR_HANDLE);
315#endif /* HAVE_NTGUI */
316
42c95ffb 317#if 0
6cdfb6e6
RS
318 /* Explicitly specify no security */
319 if (!InitializeSecurityDescriptor (&sec_desc, SECURITY_DESCRIPTOR_REVISION))
c519b5e1 320 goto EH_Fail;
6cdfb6e6 321 if (!SetSecurityDescriptorDacl (&sec_desc, TRUE, NULL, FALSE))
c519b5e1 322 goto EH_Fail;
42c95ffb 323#endif
6cdfb6e6 324 sec_attrs.nLength = sizeof (sec_attrs);
42c95ffb 325 sec_attrs.lpSecurityDescriptor = NULL /* &sec_desc */;
6cdfb6e6 326 sec_attrs.bInheritHandle = FALSE;
177c0ea7 327
b2fc9f3d
GV
328 strcpy (dir, process_dir);
329 unixtodos_filename (dir);
82e7c0a9
AI
330
331 flags = (!NILP (Vw32_start_process_share_console)
332 ? CREATE_NEW_PROCESS_GROUP
333 : CREATE_NEW_CONSOLE);
334 if (NILP (Vw32_start_process_inherit_error_mode))
335 flags |= CREATE_DEFAULT_ERROR_MODE;
6cdfb6e6 336 if (!CreateProcess (exe, cmdline, &sec_attrs, NULL, TRUE,
82e7c0a9 337 flags, env, dir, &start, &cp->procinfo))
c519b5e1
GV
338 goto EH_Fail;
339
340 cp->pid = (int) cp->procinfo.dwProcessId;
341
342 /* Hack for Windows 95, which assigns large (ie negative) pids */
343 if (cp->pid < 0)
344 cp->pid = -cp->pid;
345
346 /* pid must fit in a Lisp_Int */
acba5cae 347 cp->pid = cp->pid & INTMASK;
c519b5e1 348
c519b5e1 349 *pPid = cp->pid;
b2fc9f3d 350
6cdfb6e6 351 return TRUE;
b2fc9f3d 352
6cdfb6e6 353 EH_Fail:
ed3751c8 354 DebPrint (("create_child.CreateProcess failed: %ld\n", GetLastError ()););
6cdfb6e6
RS
355 return FALSE;
356}
357
358/* create_child doesn't know what emacs' file handle will be for waiting
359 on output from the child, so we need to make this additional call
360 to register the handle with the process
361 This way the select emulator knows how to match file handles with
362 entries in child_procs. */
177c0ea7 363void
6cdfb6e6
RS
364register_child (int pid, int fd)
365{
366 child_process *cp;
177c0ea7 367
6cdfb6e6
RS
368 cp = find_child_pid (pid);
369 if (cp == NULL)
370 {
371 DebPrint (("register_child unable to find pid %lu\n", pid));
372 return;
373 }
177c0ea7 374
6cdfb6e6
RS
375#ifdef FULL_DEBUG
376 DebPrint (("register_child registered fd %d with pid %lu\n", fd, pid));
377#endif
177c0ea7 378
6cdfb6e6 379 cp->fd = fd;
6cdfb6e6 380
c519b5e1
GV
381 /* thread is initially blocked until select is called; set status so
382 that select will release thread */
383 cp->status = STATUS_READ_ACKNOWLEDGED;
384
385 /* attach child_process to fd_info */
386 if (fd_info[fd].cp != NULL)
6cdfb6e6 387 {
c519b5e1
GV
388 DebPrint (("register_child: fd_info[%d] apparently in use!\n", fd));
389 abort ();
6cdfb6e6 390 }
c519b5e1
GV
391
392 fd_info[fd].cp = cp;
6cdfb6e6
RS
393}
394
395/* When a process dies its pipe will break so the reader thread will
396 signal failure to the select emulator.
397 The select emulator then calls this routine to clean up.
398 Since the thread signaled failure we can assume it is exiting. */
177c0ea7 399static void
c519b5e1 400reap_subprocess (child_process *cp)
6cdfb6e6 401{
c519b5e1 402 if (cp->procinfo.hProcess)
6cdfb6e6 403 {
c519b5e1 404 /* Reap the process */
b2fc9f3d
GV
405#ifdef FULL_DEBUG
406 /* Process should have already died before we are called. */
407 if (WaitForSingleObject (cp->procinfo.hProcess, 0) != WAIT_OBJECT_0)
408 DebPrint (("reap_subprocess: child fpr fd %d has not died yet!", cp->fd));
409#endif
c519b5e1
GV
410 CloseHandle (cp->procinfo.hProcess);
411 cp->procinfo.hProcess = NULL;
412 CloseHandle (cp->procinfo.hThread);
413 cp->procinfo.hThread = NULL;
6cdfb6e6 414 }
c519b5e1
GV
415
416 /* For asynchronous children, the child_proc resources will be freed
417 when the last pipe read descriptor is closed; for synchronous
418 children, we must explicitly free the resources now because
419 register_child has not been called. */
420 if (cp->fd == -1)
421 delete_child (cp);
6cdfb6e6
RS
422}
423
424/* Wait for any of our existing child processes to die
425 When it does, close its handle
426 Return the pid and fill in the status if non-NULL. */
22759c72 427
177c0ea7 428int
c519b5e1 429sys_wait (int *status)
6cdfb6e6
RS
430{
431 DWORD active, retval;
432 int nh;
c519b5e1 433 int pid;
6cdfb6e6
RS
434 child_process *cp, *cps[MAX_CHILDREN];
435 HANDLE wait_hnd[MAX_CHILDREN];
177c0ea7 436
6cdfb6e6
RS
437 nh = 0;
438 if (dead_child != NULL)
439 {
440 /* We want to wait for a specific child */
c519b5e1 441 wait_hnd[nh] = dead_child->procinfo.hProcess;
6cdfb6e6 442 cps[nh] = dead_child;
c519b5e1 443 if (!wait_hnd[nh]) abort ();
6cdfb6e6 444 nh++;
b2fc9f3d
GV
445 active = 0;
446 goto get_result;
6cdfb6e6
RS
447 }
448 else
449 {
9d4f32e8 450 for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
c519b5e1 451 /* some child_procs might be sockets; ignore them */
3ac04ed0
CY
452 if (CHILD_ACTIVE (cp) && cp->procinfo.hProcess
453 && (cp->fd < 0 || (fd_info[cp->fd].flags & FILE_AT_EOF) != 0))
6cdfb6e6 454 {
c519b5e1 455 wait_hnd[nh] = cp->procinfo.hProcess;
6cdfb6e6
RS
456 cps[nh] = cp;
457 nh++;
458 }
459 }
177c0ea7 460
6cdfb6e6
RS
461 if (nh == 0)
462 {
463 /* Nothing to wait on, so fail */
464 errno = ECHILD;
465 return -1;
466 }
b2fc9f3d
GV
467
468 do
469 {
470 /* Check for quit about once a second. */
471 QUIT;
472 active = WaitForMultipleObjects (nh, wait_hnd, FALSE, 1000);
473 } while (active == WAIT_TIMEOUT);
474
6cdfb6e6
RS
475 if (active == WAIT_FAILED)
476 {
477 errno = EBADF;
478 return -1;
479 }
b2fc9f3d
GV
480 else if (active >= WAIT_OBJECT_0
481 && active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS)
6cdfb6e6
RS
482 {
483 active -= WAIT_OBJECT_0;
484 }
b2fc9f3d
GV
485 else if (active >= WAIT_ABANDONED_0
486 && active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS)
6cdfb6e6
RS
487 {
488 active -= WAIT_ABANDONED_0;
489 }
b2fc9f3d
GV
490 else
491 abort ();
492
493get_result:
6cdfb6e6
RS
494 if (!GetExitCodeProcess (wait_hnd[active], &retval))
495 {
496 DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
497 GetLastError ()));
498 retval = 1;
499 }
500 if (retval == STILL_ACTIVE)
501 {
502 /* Should never happen */
503 DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
504 errno = EINVAL;
505 return -1;
506 }
bc69349b
RS
507
508 /* Massage the exit code from the process to match the format expected
8e6208c5 509 by the WIFSTOPPED et al macros in syswait.h. Only WIFSIGNALED and
bc69349b
RS
510 WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT. */
511
512 if (retval == STATUS_CONTROL_C_EXIT)
513 retval = SIGINT;
514 else
515 retval <<= 8;
177c0ea7 516
6cdfb6e6 517 cp = cps[active];
c519b5e1
GV
518 pid = cp->pid;
519#ifdef FULL_DEBUG
520 DebPrint (("Wait signaled with process pid %d\n", cp->pid));
521#endif
22759c72 522
6cdfb6e6
RS
523 if (status)
524 {
22759c72
KH
525 *status = retval;
526 }
527 else if (synch_process_alive)
528 {
529 synch_process_alive = 0;
22759c72 530
3d7eead0
GV
531 /* Report the status of the synchronous process. */
532 if (WIFEXITED (retval))
533 synch_process_retcode = WRETCODE (retval);
534 else if (WIFSIGNALED (retval))
535 {
536 int code = WTERMSIG (retval);
68c45bf0
PE
537 char *signame;
538
ca9c0567 539 synchronize_system_messages_locale ();
68c45bf0
PE
540 signame = strsignal (code);
541
3d7eead0
GV
542 if (signame == 0)
543 signame = "unknown";
544
545 synch_process_death = signame;
546 }
c519b5e1
GV
547
548 reap_subprocess (cp);
6cdfb6e6 549 }
b2fc9f3d
GV
550
551 reap_subprocess (cp);
177c0ea7 552
c519b5e1 553 return pid;
6cdfb6e6
RS
554}
555
75be5258
EZ
556/* Old versions of w32api headers don't have separate 32-bit and
557 64-bit defines, but the one they have matches the 32-bit variety. */
558#ifndef IMAGE_NT_OPTIONAL_HDR32_MAGIC
559# define IMAGE_NT_OPTIONAL_HDR32_MAGIC IMAGE_NT_OPTIONAL_HDR_MAGIC
560# define IMAGE_OPTIONAL_HEADER32 IMAGE_OPTIONAL_HEADER
561#endif
562
24f981c9 563static void
b56ceb92
JB
564w32_executable_type (char * filename,
565 int * is_dos_app,
566 int * is_cygnus_app,
567 int * is_gui_app)
817abdf6 568{
b2fc9f3d
GV
569 file_data executable;
570 char * p;
177c0ea7 571
b2fc9f3d
GV
572 /* Default values in case we can't tell for sure. */
573 *is_dos_app = FALSE;
574 *is_cygnus_app = FALSE;
a55a5f3c 575 *is_gui_app = FALSE;
b2fc9f3d
GV
576
577 if (!open_input_file (&executable, filename))
578 return;
817abdf6 579
b2fc9f3d 580 p = strrchr (filename, '.');
177c0ea7 581
b2fc9f3d 582 /* We can only identify DOS .com programs from the extension. */
05131107 583 if (p && xstrcasecmp (p, ".com") == 0)
b2fc9f3d 584 *is_dos_app = TRUE;
05131107
JR
585 else if (p && (xstrcasecmp (p, ".bat") == 0
586 || xstrcasecmp (p, ".cmd") == 0))
b2fc9f3d
GV
587 {
588 /* A DOS shell script - it appears that CreateProcess is happy to
589 accept this (somewhat surprisingly); presumably it looks at
590 COMSPEC to determine what executable to actually invoke.
591 Therefore, we have to do the same here as well. */
592 /* Actually, I think it uses the program association for that
593 extension, which is defined in the registry. */
594 p = egetenv ("COMSPEC");
595 if (p)
a55a5f3c 596 w32_executable_type (p, is_dos_app, is_cygnus_app, is_gui_app);
b2fc9f3d
GV
597 }
598 else
817abdf6 599 {
b2fc9f3d
GV
600 /* Look for DOS .exe signature - if found, we must also check that
601 it isn't really a 16- or 32-bit Windows exe, since both formats
602 start with a DOS program stub. Note that 16-bit Windows
603 executables use the OS/2 1.x format. */
817abdf6 604
b2fc9f3d
GV
605 IMAGE_DOS_HEADER * dos_header;
606 IMAGE_NT_HEADERS * nt_header;
607
608 dos_header = (PIMAGE_DOS_HEADER) executable.file_base;
609 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
610 goto unwind;
611
612 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
613
177c0ea7 614 if ((char *) nt_header > (char *) dos_header + executable.size)
817abdf6 615 {
b2fc9f3d
GV
616 /* Some dos headers (pkunzip) have bogus e_lfanew fields. */
617 *is_dos_app = TRUE;
177c0ea7 618 }
b2fc9f3d
GV
619 else if (nt_header->Signature != IMAGE_NT_SIGNATURE
620 && LOWORD (nt_header->Signature) != IMAGE_OS2_SIGNATURE)
621 {
622 *is_dos_app = TRUE;
623 }
624 else if (nt_header->Signature == IMAGE_NT_SIGNATURE)
625 {
2b6e2f4d
JR
626 IMAGE_DATA_DIRECTORY *data_dir = NULL;
627 if (nt_header->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
628 {
629 /* Ensure we are using the 32 bit structure. */
630 IMAGE_OPTIONAL_HEADER32 *opt
631 = (IMAGE_OPTIONAL_HEADER32*) &(nt_header->OptionalHeader);
632 data_dir = opt->DataDirectory;
633 *is_gui_app = (opt->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI);
634 }
635 /* MingW 3.12 has the required 64 bit structs, but in case older
636 versions don't, only check 64 bit exes if we know how. */
637#ifdef IMAGE_NT_OPTIONAL_HDR64_MAGIC
638 else if (nt_header->OptionalHeader.Magic
639 == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
640 {
641 IMAGE_OPTIONAL_HEADER64 *opt
642 = (IMAGE_OPTIONAL_HEADER64*) &(nt_header->OptionalHeader);
643 data_dir = opt->DataDirectory;
644 *is_gui_app = (opt->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI);
645 }
646#endif
647 if (data_dir)
648 {
649 /* Look for cygwin.dll in DLL import list. */
650 IMAGE_DATA_DIRECTORY import_dir =
651 data_dir[IMAGE_DIRECTORY_ENTRY_IMPORT];
652 IMAGE_IMPORT_DESCRIPTOR * imports;
653 IMAGE_SECTION_HEADER * section;
654
655 section = rva_to_section (import_dir.VirtualAddress, nt_header);
656 imports = RVA_TO_PTR (import_dir.VirtualAddress, section,
657 executable);
658
659 for ( ; imports->Name; imports++)
660 {
661 char * dllname = RVA_TO_PTR (imports->Name, section,
662 executable);
35f36d65 663
2b6e2f4d
JR
664 /* The exact name of the cygwin dll has changed with
665 various releases, but hopefully this will be reasonably
666 future proof. */
667 if (strncmp (dllname, "cygwin", 6) == 0)
668 {
669 *is_cygnus_app = TRUE;
670 break;
671 }
672 }
673 }
b2fc9f3d 674 }
817abdf6 675 }
177c0ea7 676
b2fc9f3d
GV
677unwind:
678 close_file_data (&executable);
817abdf6
KH
679}
680
24f981c9 681static int
42c95ffb 682compare_env (const void *strp1, const void *strp2)
d9709fde 683{
42c95ffb 684 const char *str1 = *(const char **)strp1, *str2 = *(const char **)strp2;
d9709fde
GV
685
686 while (*str1 && *str2 && *str1 != '=' && *str2 != '=')
687 {
11c22fff
AI
688 /* Sort order in command.com/cmd.exe is based on uppercasing
689 names, so do the same here. */
690 if (toupper (*str1) > toupper (*str2))
d9709fde 691 return 1;
11c22fff 692 else if (toupper (*str1) < toupper (*str2))
d9709fde
GV
693 return -1;
694 str1++, str2++;
695 }
696
697 if (*str1 == '=' && *str2 == '=')
698 return 0;
699 else if (*str1 == '=')
700 return -1;
701 else
702 return 1;
703}
704
24f981c9 705static void
d9709fde
GV
706merge_and_sort_env (char **envp1, char **envp2, char **new_envp)
707{
708 char **optr, **nptr;
709 int num;
710
711 nptr = new_envp;
712 optr = envp1;
713 while (*optr)
714 *nptr++ = *optr++;
715 num = optr - envp1;
716
717 optr = envp2;
718 while (*optr)
719 *nptr++ = *optr++;
720 num += optr - envp2;
721
722 qsort (new_envp, num, sizeof (char *), compare_env);
723
724 *nptr = NULL;
725}
6cdfb6e6
RS
726
727/* When a new child process is created we need to register it in our list,
728 so intercept spawn requests. */
177c0ea7 729int
c519b5e1 730sys_spawnve (int mode, char *cmdname, char **argv, char **envp)
6cdfb6e6 731{
0a4de642 732 Lisp_Object program, full;
6cdfb6e6 733 char *cmdline, *env, *parg, **targ;
d9709fde 734 int arglen, numenv;
c519b5e1
GV
735 int pid;
736 child_process *cp;
a55a5f3c 737 int is_dos_app, is_cygnus_app, is_gui_app;
b2fc9f3d
GV
738 int do_quoting = 0;
739 char escape_char;
d9709fde
GV
740 /* We pass our process ID to our children by setting up an environment
741 variable in their environment. */
742 char ppid_env_var_buffer[64];
743 char *extra_env[] = {ppid_env_var_buffer, NULL};
0a7a6051
JR
744 /* These are the characters that cause an argument to need quoting.
745 Arguments with whitespace characters need quoting to prevent the
746 argument being split into two or more. Arguments with wildcards
747 are also quoted, for consistency with posix platforms, where wildcards
748 are not expanded if we run the program directly without a shell.
749 Some extra whitespace characters need quoting in Cygwin programs,
750 so this list is conditionally modified below. */
751 char *sepchars = " \t*?";
d9709fde 752
c519b5e1
GV
753 /* We don't care about the other modes */
754 if (mode != _P_NOWAIT)
755 {
756 errno = EINVAL;
757 return -1;
758 }
0a4de642
RS
759
760 /* Handle executable names without an executable suffix. */
761 program = make_string (cmdname, strlen (cmdname));
762 if (NILP (Ffile_executable_p (program)))
763 {
764 struct gcpro gcpro1;
177c0ea7 765
0a4de642
RS
766 full = Qnil;
767 GCPRO1 (program);
44c7a526 768 openp (Vexec_path, program, Vexec_suffixes, &full, make_number (X_OK));
0a4de642
RS
769 UNGCPRO;
770 if (NILP (full))
771 {
772 errno = EINVAL;
773 return -1;
774 }
b2fc9f3d 775 program = full;
0a4de642
RS
776 }
777
b2fc9f3d 778 /* make sure argv[0] and cmdname are both in DOS format */
d5db4077 779 cmdname = SDATA (program);
c519b5e1
GV
780 unixtodos_filename (cmdname);
781 argv[0] = cmdname;
817abdf6 782
ef79fbba 783 /* Determine whether program is a 16-bit DOS executable, or a w32
b2fc9f3d
GV
784 executable that is implicitly linked to the Cygnus dll (implying it
785 was compiled with the Cygnus GNU toolchain and hence relies on
786 cygwin.dll to parse the command line - we use this to decide how to
a55a5f3c
AI
787 escape quote chars in command line args that must be quoted).
788
789 Also determine whether it is a GUI app, so that we don't hide its
790 initial window unless specifically requested. */
791 w32_executable_type (cmdname, &is_dos_app, &is_cygnus_app, &is_gui_app);
b2fc9f3d
GV
792
793 /* On Windows 95, if cmdname is a DOS app, we invoke a helper
794 application to start it by specifying the helper app as cmdname,
795 while leaving the real app name as argv[0]. */
796 if (is_dos_app)
817abdf6 797 {
b2fc9f3d
GV
798 cmdname = alloca (MAXPATHLEN);
799 if (egetenv ("CMDPROXY"))
800 strcpy (cmdname, egetenv ("CMDPROXY"));
801 else
802 {
d5db4077 803 strcpy (cmdname, SDATA (Vinvocation_directory));
b2fc9f3d
GV
804 strcat (cmdname, "cmdproxy.exe");
805 }
806 unixtodos_filename (cmdname);
817abdf6 807 }
177c0ea7 808
6cdfb6e6
RS
809 /* we have to do some conjuring here to put argv and envp into the
810 form CreateProcess wants... argv needs to be a space separated/null
811 terminated list of parameters, and envp is a null
812 separated/double-null terminated list of parameters.
c519b5e1 813
b2fc9f3d
GV
814 Additionally, zero-length args and args containing whitespace or
815 quote chars need to be wrapped in double quotes - for this to work,
816 embedded quotes need to be escaped as well. The aim is to ensure
817 the child process reconstructs the argv array we start with
818 exactly, so we treat quotes at the beginning and end of arguments
819 as embedded quotes.
820
ef79fbba 821 The w32 GNU-based library from Cygnus doubles quotes to escape
b2fc9f3d
GV
822 them, while MSVC uses backslash for escaping. (Actually the MSVC
823 startup code does attempt to recognise doubled quotes and accept
824 them, but gets it wrong and ends up requiring three quotes to get a
825 single embedded quote!) So by default we decide whether to use
826 quote or backslash as the escape character based on whether the
827 binary is apparently a Cygnus compiled app.
828
829 Note that using backslash to escape embedded quotes requires
830 additional special handling if an embedded quote is already
97610156 831 preceded by backslash, or if an arg requiring quoting ends with
b2fc9f3d
GV
832 backslash. In such cases, the run of escape characters needs to be
833 doubled. For consistency, we apply this special handling as long
834 as the escape character is not quote.
835
836 Since we have no idea how large argv and envp are likely to be we
837 figure out list lengths on the fly and allocate them. */
838
839 if (!NILP (Vw32_quote_process_args))
840 {
841 do_quoting = 1;
842 /* Override escape char by binding w32-quote-process-args to
843 desired character, or use t for auto-selection. */
844 if (INTEGERP (Vw32_quote_process_args))
845 escape_char = XINT (Vw32_quote_process_args);
846 else
847 escape_char = is_cygnus_app ? '"' : '\\';
848 }
177c0ea7 849
9d4f32e8 850 /* Cygwin apps needs quoting a bit more often. */
dbb70029
GM
851 if (escape_char == '"')
852 sepchars = "\r\n\t\f '";
853
6cdfb6e6
RS
854 /* do argv... */
855 arglen = 0;
856 targ = argv;
857 while (*targ)
858 {
c519b5e1 859 char * p = *targ;
b2fc9f3d
GV
860 int need_quotes = 0;
861 int escape_char_run = 0;
c519b5e1
GV
862
863 if (*p == 0)
b2fc9f3d
GV
864 need_quotes = 1;
865 for ( ; *p; p++)
866 {
dbb70029
GM
867 if (escape_char == '"' && *p == '\\')
868 /* If it's a Cygwin app, \ needs to be escaped. */
869 arglen++;
870 else if (*p == '"')
b2fc9f3d
GV
871 {
872 /* allow for embedded quotes to be escaped */
873 arglen++;
874 need_quotes = 1;
875 /* handle the case where the embedded quote is already escaped */
876 if (escape_char_run > 0)
877 {
878 /* To preserve the arg exactly, we need to double the
879 preceding escape characters (plus adding one to
880 escape the quote character itself). */
881 arglen += escape_char_run;
882 }
883 }
dbb70029 884 else if (strchr (sepchars, *p) != NULL)
b2fc9f3d
GV
885 {
886 need_quotes = 1;
887 }
888
889 if (*p == escape_char && escape_char != '"')
890 escape_char_run++;
891 else
892 escape_char_run = 0;
893 }
894 if (need_quotes)
895 {
896 arglen += 2;
897 /* handle the case where the arg ends with an escape char - we
898 must not let the enclosing quote be escaped. */
899 if (escape_char_run > 0)
900 arglen += escape_char_run;
901 }
6cdfb6e6
RS
902 arglen += strlen (*targ++) + 1;
903 }
c519b5e1 904 cmdline = alloca (arglen);
6cdfb6e6
RS
905 targ = argv;
906 parg = cmdline;
907 while (*targ)
908 {
c519b5e1 909 char * p = *targ;
b2fc9f3d 910 int need_quotes = 0;
c519b5e1
GV
911
912 if (*p == 0)
b2fc9f3d 913 need_quotes = 1;
93fdf2f8 914
b2fc9f3d 915 if (do_quoting)
93fdf2f8 916 {
93fdf2f8 917 for ( ; *p; p++)
dbb70029 918 if ((strchr (sepchars, *p) != NULL) || *p == '"')
b2fc9f3d 919 need_quotes = 1;
93fdf2f8 920 }
b2fc9f3d 921 if (need_quotes)
c519b5e1 922 {
b2fc9f3d 923 int escape_char_run = 0;
c519b5e1
GV
924 char * first;
925 char * last;
926
927 p = *targ;
928 first = p;
929 last = p + strlen (p) - 1;
930 *parg++ = '"';
b2fc9f3d
GV
931#if 0
932 /* This version does not escape quotes if they occur at the
933 beginning or end of the arg - this could lead to incorrect
fffa137c 934 behavior when the arg itself represents a command line
b2fc9f3d
GV
935 containing quoted args. I believe this was originally done
936 as a hack to make some things work, before
937 `w32-quote-process-args' was added. */
c519b5e1
GV
938 while (*p)
939 {
940 if (*p == '"' && p > first && p < last)
b2fc9f3d 941 *parg++ = escape_char; /* escape embedded quotes */
c519b5e1
GV
942 *parg++ = *p++;
943 }
b2fc9f3d
GV
944#else
945 for ( ; *p; p++)
946 {
947 if (*p == '"')
948 {
949 /* double preceding escape chars if any */
950 while (escape_char_run > 0)
951 {
952 *parg++ = escape_char;
953 escape_char_run--;
954 }
955 /* escape all quote chars, even at beginning or end */
956 *parg++ = escape_char;
957 }
dbb70029
GM
958 else if (escape_char == '"' && *p == '\\')
959 *parg++ = '\\';
b2fc9f3d
GV
960 *parg++ = *p;
961
962 if (*p == escape_char && escape_char != '"')
963 escape_char_run++;
964 else
965 escape_char_run = 0;
966 }
967 /* double escape chars before enclosing quote */
968 while (escape_char_run > 0)
969 {
970 *parg++ = escape_char;
971 escape_char_run--;
972 }
973#endif
c519b5e1
GV
974 *parg++ = '"';
975 }
976 else
977 {
978 strcpy (parg, *targ);
979 parg += strlen (*targ);
980 }
6cdfb6e6 981 *parg++ = ' ';
c519b5e1 982 targ++;
6cdfb6e6
RS
983 }
984 *--parg = '\0';
177c0ea7 985
6cdfb6e6
RS
986 /* and envp... */
987 arglen = 1;
988 targ = envp;
d9709fde 989 numenv = 1; /* for end null */
6cdfb6e6
RS
990 while (*targ)
991 {
992 arglen += strlen (*targ++) + 1;
d9709fde 993 numenv++;
6cdfb6e6 994 }
d9709fde 995 /* extra env vars... */
177c0ea7 996 sprintf (ppid_env_var_buffer, "EM_PARENT_PROCESS_ID=%d",
6cdfb6e6
RS
997 GetCurrentProcessId ());
998 arglen += strlen (ppid_env_var_buffer) + 1;
d9709fde 999 numenv++;
6cdfb6e6 1000
d9709fde
GV
1001 /* merge env passed in and extra env into one, and sort it. */
1002 targ = (char **) alloca (numenv * sizeof (char *));
1003 merge_and_sort_env (envp, extra_env, targ);
1004
1005 /* concatenate env entries. */
c519b5e1 1006 env = alloca (arglen);
6cdfb6e6
RS
1007 parg = env;
1008 while (*targ)
1009 {
1010 strcpy (parg, *targ);
1011 parg += strlen (*targ++);
1012 *parg++ = '\0';
1013 }
6cdfb6e6
RS
1014 *parg++ = '\0';
1015 *parg = '\0';
c519b5e1
GV
1016
1017 cp = new_child ();
1018 if (cp == NULL)
1019 {
1020 errno = EAGAIN;
1021 return -1;
1022 }
177c0ea7 1023
6cdfb6e6 1024 /* Now create the process. */
a55a5f3c 1025 if (!create_child (cmdname, cmdline, env, is_gui_app, &pid, cp))
6cdfb6e6 1026 {
c519b5e1 1027 delete_child (cp);
6cdfb6e6 1028 errno = ENOEXEC;
c519b5e1 1029 return -1;
6cdfb6e6 1030 }
177c0ea7 1031
c519b5e1 1032 return pid;
6cdfb6e6
RS
1033}
1034
1035/* Emulate the select call
1036 Wait for available input on any of the given rfds, or timeout if
1037 a timeout is given and no input is detected
b2fc9f3d
GV
1038 wfds and efds are not supported and must be NULL.
1039
1040 For simplicity, we detect the death of child processes here and
1041 synchronously call the SIGCHLD handler. Since it is possible for
1042 children to be created without a corresponding pipe handle from which
1043 to read output, we wait separately on the process handles as well as
1044 the char_avail events for each process pipe. We only call
86143765
RS
1045 wait/reap_process when the process actually terminates.
1046
1047 To reduce the number of places in which Emacs can be hung such that
1048 C-g is not able to interrupt it, we always wait on interrupt_handle
04bf5b65 1049 (which is signaled by the input thread when C-g is detected). If we
86143765
RS
1050 detect that we were woken up by C-g, we return -1 with errno set to
1051 EINTR as on Unix. */
6cdfb6e6 1052
7684e57b 1053/* From w32console.c */
6cdfb6e6 1054extern HANDLE keyboard_handle;
86143765
RS
1055
1056/* From w32xfns.c */
1057extern HANDLE interrupt_handle;
1058
6cdfb6e6
RS
1059/* From process.c */
1060extern int proc_buffered_char[];
1061
177c0ea7 1062int
22759c72
KH
1063sys_select (int nfds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
1064 EMACS_TIME *timeout)
6cdfb6e6
RS
1065{
1066 SELECT_TYPE orfds;
b2fc9f3d
GV
1067 DWORD timeout_ms, start_time;
1068 int i, nh, nc, nr;
6cdfb6e6 1069 DWORD active;
b2fc9f3d
GV
1070 child_process *cp, *cps[MAX_CHILDREN];
1071 HANDLE wait_hnd[MAXDESC + MAX_CHILDREN];
c519b5e1 1072 int fdindex[MAXDESC]; /* mapping from wait handles back to descriptors */
177c0ea7 1073
b2fc9f3d
GV
1074 timeout_ms = timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFINITE;
1075
6cdfb6e6 1076 /* If the descriptor sets are NULL but timeout isn't, then just Sleep. */
177c0ea7 1077 if (rfds == NULL && wfds == NULL && efds == NULL && timeout != NULL)
6cdfb6e6 1078 {
b2fc9f3d 1079 Sleep (timeout_ms);
6cdfb6e6
RS
1080 return 0;
1081 }
1082
1083 /* Otherwise, we only handle rfds, so fail otherwise. */
1084 if (rfds == NULL || wfds != NULL || efds != NULL)
1085 {
1086 errno = EINVAL;
1087 return -1;
1088 }
177c0ea7 1089
6cdfb6e6
RS
1090 orfds = *rfds;
1091 FD_ZERO (rfds);
1092 nr = 0;
86143765
RS
1093
1094 /* Always wait on interrupt_handle, to detect C-g (quit). */
1095 wait_hnd[0] = interrupt_handle;
1096 fdindex[0] = -1;
177c0ea7 1097
b2fc9f3d 1098 /* Build a list of pipe handles to wait on. */
86143765 1099 nh = 1;
6cdfb6e6
RS
1100 for (i = 0; i < nfds; i++)
1101 if (FD_ISSET (i, &orfds))
1102 {
1103 if (i == 0)
1104 {
c519b5e1
GV
1105 if (keyboard_handle)
1106 {
1107 /* Handle stdin specially */
1108 wait_hnd[nh] = keyboard_handle;
1109 fdindex[nh] = i;
1110 nh++;
1111 }
6cdfb6e6
RS
1112
1113 /* Check for any emacs-generated input in the queue since
1114 it won't be detected in the wait */
1115 if (detect_input_pending ())
1116 {
1117 FD_SET (i, rfds);
c519b5e1 1118 return 1;
6cdfb6e6
RS
1119 }
1120 }
1121 else
1122 {
c519b5e1
GV
1123 /* Child process and socket input */
1124 cp = fd_info[i].cp;
6cdfb6e6
RS
1125 if (cp)
1126 {
c519b5e1
GV
1127 int current_status = cp->status;
1128
1129 if (current_status == STATUS_READ_ACKNOWLEDGED)
1130 {
1131 /* Tell reader thread which file handle to use. */
1132 cp->fd = i;
1133 /* Wake up the reader thread for this process */
1134 cp->status = STATUS_READ_READY;
1135 if (!SetEvent (cp->char_consumed))
1136 DebPrint (("nt_select.SetEvent failed with "
1137 "%lu for fd %ld\n", GetLastError (), i));
1138 }
1139
1140#ifdef CHECK_INTERLOCK
1141 /* slightly crude cross-checking of interlock between threads */
1142
1143 current_status = cp->status;
1144 if (WaitForSingleObject (cp->char_avail, 0) == WAIT_OBJECT_0)
1145 {
04bf5b65 1146 /* char_avail has been signaled, so status (which may
c519b5e1
GV
1147 have changed) should indicate read has completed
1148 but has not been acknowledged. */
1149 current_status = cp->status;
b2fc9f3d
GV
1150 if (current_status != STATUS_READ_SUCCEEDED
1151 && current_status != STATUS_READ_FAILED)
c519b5e1
GV
1152 DebPrint (("char_avail set, but read not completed: status %d\n",
1153 current_status));
1154 }
1155 else
1156 {
04bf5b65 1157 /* char_avail has not been signaled, so status should
c519b5e1 1158 indicate that read is in progress; small possibility
04bf5b65 1159 that read has completed but event wasn't yet signaled
c519b5e1
GV
1160 when we tested it (because a context switch occurred
1161 or if running on separate CPUs). */
b2fc9f3d
GV
1162 if (current_status != STATUS_READ_READY
1163 && current_status != STATUS_READ_IN_PROGRESS
1164 && current_status != STATUS_READ_SUCCEEDED
1165 && current_status != STATUS_READ_FAILED)
c519b5e1
GV
1166 DebPrint (("char_avail reset, but read status is bad: %d\n",
1167 current_status));
1168 }
1169#endif
1170 wait_hnd[nh] = cp->char_avail;
1171 fdindex[nh] = i;
1172 if (!wait_hnd[nh]) abort ();
1173 nh++;
6cdfb6e6
RS
1174#ifdef FULL_DEBUG
1175 DebPrint (("select waiting on child %d fd %d\n",
1176 cp-child_procs, i));
1177#endif
6cdfb6e6
RS
1178 }
1179 else
1180 {
c519b5e1 1181 /* Unable to find something to wait on for this fd, skip */
ef79fbba
GV
1182
1183 /* Note that this is not a fatal error, and can in fact
1184 happen in unusual circumstances. Specifically, if
1185 sys_spawnve fails, eg. because the program doesn't
1186 exist, and debug-on-error is t so Fsignal invokes a
1187 nested input loop, then the process output pipe is
1188 still included in input_wait_mask with no child_proc
1189 associated with it. (It is removed when the debugger
1190 exits the nested input loop and the error is thrown.) */
1191
c519b5e1 1192 DebPrint (("sys_select: fd %ld is invalid! ignoring\n", i));
6cdfb6e6
RS
1193 }
1194 }
1195 }
b2fc9f3d
GV
1196
1197count_children:
1198 /* Add handles of child processes. */
1199 nc = 0;
9d4f32e8 1200 for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
ef79fbba
GV
1201 /* Some child_procs might be sockets; ignore them. Also some
1202 children may have died already, but we haven't finished reading
1203 the process output; ignore them too. */
1204 if (CHILD_ACTIVE (cp) && cp->procinfo.hProcess
1205 && (cp->fd < 0
1206 || (fd_info[cp->fd].flags & FILE_SEND_SIGCHLD) == 0
1207 || (fd_info[cp->fd].flags & FILE_AT_EOF) != 0)
1208 )
b2fc9f3d
GV
1209 {
1210 wait_hnd[nh + nc] = cp->procinfo.hProcess;
1211 cps[nc] = cp;
1212 nc++;
1213 }
177c0ea7 1214
6cdfb6e6 1215 /* Nothing to look for, so we didn't find anything */
177c0ea7 1216 if (nh + nc == 0)
6cdfb6e6 1217 {
22759c72 1218 if (timeout)
b2fc9f3d 1219 Sleep (timeout_ms);
6cdfb6e6
RS
1220 return 0;
1221 }
177c0ea7 1222
b2fc9f3d 1223 start_time = GetTickCount ();
8b031dcc 1224
04bf5b65 1225 /* Wait for input or child death to be signaled. If user input is
8b031dcc
AI
1226 allowed, then also accept window messages. */
1227 if (FD_ISSET (0, &orfds))
1228 active = MsgWaitForMultipleObjects (nh + nc, wait_hnd, FALSE, timeout_ms,
1229 QS_ALLINPUT);
1230 else
1231 active = WaitForMultipleObjects (nh + nc, wait_hnd, FALSE, timeout_ms);
c519b5e1 1232
6cdfb6e6
RS
1233 if (active == WAIT_FAILED)
1234 {
1235 DebPrint (("select.WaitForMultipleObjects (%d, %lu) failed with %lu\n",
b2fc9f3d 1236 nh + nc, timeout_ms, GetLastError ()));
d64b707c 1237 /* don't return EBADF - this causes wait_reading_process_output to
c519b5e1
GV
1238 abort; WAIT_FAILED is returned when single-stepping under
1239 Windows 95 after switching thread focus in debugger, and
1240 possibly at other times. */
1241 errno = EINTR;
6cdfb6e6
RS
1242 return -1;
1243 }
1244 else if (active == WAIT_TIMEOUT)
1245 {
1246 return 0;
1247 }
b2fc9f3d
GV
1248 else if (active >= WAIT_OBJECT_0
1249 && active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS)
6cdfb6e6
RS
1250 {
1251 active -= WAIT_OBJECT_0;
1252 }
b2fc9f3d
GV
1253 else if (active >= WAIT_ABANDONED_0
1254 && active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS)
6cdfb6e6
RS
1255 {
1256 active -= WAIT_ABANDONED_0;
1257 }
b2fc9f3d
GV
1258 else
1259 abort ();
6cdfb6e6 1260
c519b5e1 1261 /* Loop over all handles after active (now officially documented as
04bf5b65 1262 being the first signaled handle in the array). We do this to
c519b5e1
GV
1263 ensure fairness, so that all channels with data available will be
1264 processed - otherwise higher numbered channels could be starved. */
1265 do
6cdfb6e6 1266 {
8b031dcc
AI
1267 if (active == nh + nc)
1268 {
1269 /* There are messages in the lisp thread's queue; we must
1270 drain the queue now to ensure they are processed promptly,
1271 because if we don't do so, we will not be woken again until
1272 further messages arrive.
1273
1274 NB. If ever we allow window message procedures to callback
1275 into lisp, we will need to ensure messages are dispatched
1276 at a safe time for lisp code to be run (*), and we may also
1277 want to provide some hooks in the dispatch loop to cater
1278 for modeless dialogs created by lisp (ie. to register
1279 window handles to pass to IsDialogMessage).
1280
1281 (*) Note that MsgWaitForMultipleObjects above is an
1282 internal dispatch point for messages that are sent to
1283 windows created by this thread. */
1284 drain_message_queue ();
1285 }
1286 else if (active >= nh)
b2fc9f3d
GV
1287 {
1288 cp = cps[active - nh];
ef79fbba
GV
1289
1290 /* We cannot always signal SIGCHLD immediately; if we have not
1291 finished reading the process output, we must delay sending
1292 SIGCHLD until we do. */
1293
1294 if (cp->fd >= 0 && (fd_info[cp->fd].flags & FILE_AT_EOF) == 0)
1295 fd_info[cp->fd].flags |= FILE_SEND_SIGCHLD;
b2fc9f3d 1296 /* SIG_DFL for SIGCHLD is ignore */
ef79fbba
GV
1297 else if (sig_handlers[SIGCHLD] != SIG_DFL &&
1298 sig_handlers[SIGCHLD] != SIG_IGN)
b2fc9f3d
GV
1299 {
1300#ifdef FULL_DEBUG
1301 DebPrint (("select calling SIGCHLD handler for pid %d\n",
1302 cp->pid));
1303#endif
1304 dead_child = cp;
1305 sig_handlers[SIGCHLD] (SIGCHLD);
1306 dead_child = NULL;
1307 }
1308 }
86143765
RS
1309 else if (fdindex[active] == -1)
1310 {
1311 /* Quit (C-g) was detected. */
1312 errno = EINTR;
1313 return -1;
1314 }
b2fc9f3d 1315 else if (fdindex[active] == 0)
c519b5e1
GV
1316 {
1317 /* Keyboard input available */
1318 FD_SET (0, rfds);
6cdfb6e6 1319 nr++;
c519b5e1 1320 }
6cdfb6e6 1321 else
c519b5e1 1322 {
b2fc9f3d
GV
1323 /* must be a socket or pipe - read ahead should have
1324 completed, either succeeding or failing. */
c519b5e1
GV
1325 FD_SET (fdindex[active], rfds);
1326 nr++;
c519b5e1
GV
1327 }
1328
b2fc9f3d
GV
1329 /* Even though wait_reading_process_output only reads from at most
1330 one channel, we must process all channels here so that we reap
1331 all children that have died. */
1332 while (++active < nh + nc)
c519b5e1
GV
1333 if (WaitForSingleObject (wait_hnd[active], 0) == WAIT_OBJECT_0)
1334 break;
b2fc9f3d
GV
1335 } while (active < nh + nc);
1336
1337 /* If no input has arrived and timeout hasn't expired, wait again. */
1338 if (nr == 0)
1339 {
1340 DWORD elapsed = GetTickCount () - start_time;
1341
1342 if (timeout_ms > elapsed) /* INFINITE is MAX_UINT */
1343 {
1344 if (timeout_ms != INFINITE)
1345 timeout_ms -= elapsed;
1346 goto count_children;
1347 }
1348 }
c519b5e1 1349
6cdfb6e6
RS
1350 return nr;
1351}
1352
c519b5e1 1353/* Substitute for certain kill () operations */
b2fc9f3d
GV
1354
1355static BOOL CALLBACK
42c95ffb 1356find_child_console (HWND hwnd, LPARAM arg)
b2fc9f3d 1357{
42c95ffb 1358 child_process * cp = (child_process *) arg;
b2fc9f3d
GV
1359 DWORD thread_id;
1360 DWORD process_id;
1361
1362 thread_id = GetWindowThreadProcessId (hwnd, &process_id);
1363 if (process_id == cp->procinfo.dwProcessId)
1364 {
1365 char window_class[32];
1366
1367 GetClassName (hwnd, window_class, sizeof (window_class));
1368 if (strcmp (window_class,
1369 (os_subtype == OS_WIN95)
1370 ? "tty"
1371 : "ConsoleWindowClass") == 0)
1372 {
1373 cp->hwnd = hwnd;
1374 return FALSE;
1375 }
1376 }
1377 /* keep looking */
1378 return TRUE;
1379}
1380
177c0ea7 1381int
c519b5e1 1382sys_kill (int pid, int sig)
6cdfb6e6
RS
1383{
1384 child_process *cp;
c519b5e1
GV
1385 HANDLE proc_hand;
1386 int need_to_free = 0;
1387 int rc = 0;
177c0ea7 1388
6cdfb6e6
RS
1389 /* Only handle signals that will result in the process dying */
1390 if (sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP)
1391 {
1392 errno = EINVAL;
1393 return -1;
1394 }
c519b5e1 1395
6cdfb6e6
RS
1396 cp = find_child_pid (pid);
1397 if (cp == NULL)
1398 {
c519b5e1
GV
1399 proc_hand = OpenProcess (PROCESS_TERMINATE, 0, pid);
1400 if (proc_hand == NULL)
1401 {
1402 errno = EPERM;
1403 return -1;
1404 }
1405 need_to_free = 1;
1406 }
1407 else
1408 {
1409 proc_hand = cp->procinfo.hProcess;
1410 pid = cp->procinfo.dwProcessId;
b2fc9f3d
GV
1411
1412 /* Try to locate console window for process. */
1413 EnumWindows (find_child_console, (LPARAM) cp);
6cdfb6e6 1414 }
177c0ea7 1415
a55a5f3c 1416 if (sig == SIGINT || sig == SIGQUIT)
6cdfb6e6 1417 {
b2fc9f3d
GV
1418 if (NILP (Vw32_start_process_share_console) && cp && cp->hwnd)
1419 {
1420 BYTE control_scan_code = (BYTE) MapVirtualKey (VK_CONTROL, 0);
a55a5f3c
AI
1421 /* Fake Ctrl-C for SIGINT, and Ctrl-Break for SIGQUIT. */
1422 BYTE vk_break_code = (sig == SIGINT) ? 'C' : VK_CANCEL;
b2fc9f3d
GV
1423 BYTE break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
1424 HWND foreground_window;
1425
1426 if (break_scan_code == 0)
1427 {
a55a5f3c 1428 /* Fake Ctrl-C for SIGQUIT if we can't manage Ctrl-Break. */
b2fc9f3d
GV
1429 vk_break_code = 'C';
1430 break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
1431 }
1432
1433 foreground_window = GetForegroundWindow ();
f446016f 1434 if (foreground_window)
b2fc9f3d 1435 {
f446016f
AI
1436 /* NT 5.0, and apparently also Windows 98, will not allow
1437 a Window to be set to foreground directly without the
1438 user's involvement. The workaround is to attach
1439 ourselves to the thread that owns the foreground
1440 window, since that is the only thread that can set the
1441 foreground window. */
1442 DWORD foreground_thread, child_thread;
1443 foreground_thread =
1444 GetWindowThreadProcessId (foreground_window, NULL);
1445 if (foreground_thread == GetCurrentThreadId ()
1446 || !AttachThreadInput (GetCurrentThreadId (),
1447 foreground_thread, TRUE))
1448 foreground_thread = 0;
1449
1450 child_thread = GetWindowThreadProcessId (cp->hwnd, NULL);
1451 if (child_thread == GetCurrentThreadId ()
1452 || !AttachThreadInput (GetCurrentThreadId (),
1453 child_thread, TRUE))
1454 child_thread = 0;
1455
1456 /* Set the foreground window to the child. */
1457 if (SetForegroundWindow (cp->hwnd))
1458 {
1459 /* Generate keystrokes as if user had typed Ctrl-Break or
1460 Ctrl-C. */
1461 keybd_event (VK_CONTROL, control_scan_code, 0, 0);
1462 keybd_event (vk_break_code, break_scan_code,
1463 (vk_break_code == 'C' ? 0 : KEYEVENTF_EXTENDEDKEY), 0);
1464 keybd_event (vk_break_code, break_scan_code,
1465 (vk_break_code == 'C' ? 0 : KEYEVENTF_EXTENDEDKEY)
1466 | KEYEVENTF_KEYUP, 0);
1467 keybd_event (VK_CONTROL, control_scan_code,
1468 KEYEVENTF_KEYUP, 0);
1469
1470 /* Sleep for a bit to give time for Emacs frame to respond
1471 to focus change events (if Emacs was active app). */
1472 Sleep (100);
1473
1474 SetForegroundWindow (foreground_window);
1475 }
1476 /* Detach from the foreground and child threads now that
1477 the foreground switching is over. */
1478 if (foreground_thread)
1479 AttachThreadInput (GetCurrentThreadId (),
1480 foreground_thread, FALSE);
1481 if (child_thread)
1482 AttachThreadInput (GetCurrentThreadId (),
1483 child_thread, FALSE);
1484 }
1485 }
c519b5e1 1486 /* Ctrl-Break is NT equivalent of SIGINT. */
b2fc9f3d 1487 else if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, pid))
6cdfb6e6 1488 {
c519b5e1 1489 DebPrint (("sys_kill.GenerateConsoleCtrlEvent return %d "
6cdfb6e6
RS
1490 "for pid %lu\n", GetLastError (), pid));
1491 errno = EINVAL;
c519b5e1 1492 rc = -1;
80874ef7 1493 }
6cdfb6e6
RS
1494 }
1495 else
1496 {
b2fc9f3d
GV
1497 if (NILP (Vw32_start_process_share_console) && cp && cp->hwnd)
1498 {
1499#if 1
1500 if (os_subtype == OS_WIN95)
1501 {
1502/*
1503 Another possibility is to try terminating the VDM out-right by
1504 calling the Shell VxD (id 0x17) V86 interface, function #4
1505 "SHELL_Destroy_VM", ie.
1506
1507 mov edx,4
1508 mov ebx,vm_handle
1509 call shellapi
1510
1511 First need to determine the current VM handle, and then arrange for
1512 the shellapi call to be made from the system vm (by using
1513 Switch_VM_and_callback).
1514
1515 Could try to invoke DestroyVM through CallVxD.
1516
1517*/
ef79fbba
GV
1518#if 0
1519 /* On Win95, posting WM_QUIT causes the 16-bit subsystem
1520 to hang when cmdproxy is used in conjunction with
1521 command.com for an interactive shell. Posting
1522 WM_CLOSE pops up a dialog that, when Yes is selected,
1523 does the same thing. TerminateProcess is also less
1524 than ideal in that subprocesses tend to stick around
1525 until the machine is shutdown, but at least it
1526 doesn't freeze the 16-bit subsystem. */
b2fc9f3d 1527 PostMessage (cp->hwnd, WM_QUIT, 0xff, 0);
ef79fbba
GV
1528#endif
1529 if (!TerminateProcess (proc_hand, 0xff))
1530 {
1531 DebPrint (("sys_kill.TerminateProcess returned %d "
1532 "for pid %lu\n", GetLastError (), pid));
1533 errno = EINVAL;
1534 rc = -1;
1535 }
b2fc9f3d
GV
1536 }
1537 else
1538#endif
1539 PostMessage (cp->hwnd, WM_CLOSE, 0, 0);
1540 }
fbd6baed 1541 /* Kill the process. On W32 this doesn't kill child processes
8eae7766 1542 so it doesn't work very well for shells which is why it's not
b2fc9f3d
GV
1543 used in every case. */
1544 else if (!TerminateProcess (proc_hand, 0xff))
6cdfb6e6 1545 {
c519b5e1 1546 DebPrint (("sys_kill.TerminateProcess returned %d "
6cdfb6e6
RS
1547 "for pid %lu\n", GetLastError (), pid));
1548 errno = EINVAL;
c519b5e1 1549 rc = -1;
6cdfb6e6
RS
1550 }
1551 }
c519b5e1
GV
1552
1553 if (need_to_free)
1554 CloseHandle (proc_hand);
1555
1556 return rc;
6cdfb6e6
RS
1557}
1558
c519b5e1
GV
1559/* The following two routines are used to manipulate stdin, stdout, and
1560 stderr of our child processes.
1561
1562 Assuming that in, out, and err are *not* inheritable, we make them
1563 stdin, stdout, and stderr of the child as follows:
1564
1565 - Save the parent's current standard handles.
1566 - Set the std handles to inheritable duplicates of the ones being passed in.
1567 (Note that _get_osfhandle() is an io.h procedure that retrieves the
1568 NT file handle for a crt file descriptor.)
1569 - Spawn the child, which inherits in, out, and err as stdin,
1570 stdout, and stderr. (see Spawnve)
1571 - Close the std handles passed to the child.
1572 - Reset the parent's standard handles to the saved handles.
1573 (see reset_standard_handles)
1574 We assume that the caller closes in, out, and err after calling us. */
1575
1576void
1577prepare_standard_handles (int in, int out, int err, HANDLE handles[3])
6cdfb6e6 1578{
c519b5e1
GV
1579 HANDLE parent;
1580 HANDLE newstdin, newstdout, newstderr;
1581
1582 parent = GetCurrentProcess ();
1583
1584 handles[0] = GetStdHandle (STD_INPUT_HANDLE);
1585 handles[1] = GetStdHandle (STD_OUTPUT_HANDLE);
1586 handles[2] = GetStdHandle (STD_ERROR_HANDLE);
1587
1588 /* make inheritable copies of the new handles */
177c0ea7 1589 if (!DuplicateHandle (parent,
c519b5e1
GV
1590 (HANDLE) _get_osfhandle (in),
1591 parent,
177c0ea7
JB
1592 &newstdin,
1593 0,
1594 TRUE,
c519b5e1
GV
1595 DUPLICATE_SAME_ACCESS))
1596 report_file_error ("Duplicating input handle for child", Qnil);
177c0ea7 1597
c519b5e1
GV
1598 if (!DuplicateHandle (parent,
1599 (HANDLE) _get_osfhandle (out),
1600 parent,
1601 &newstdout,
1602 0,
1603 TRUE,
1604 DUPLICATE_SAME_ACCESS))
1605 report_file_error ("Duplicating output handle for child", Qnil);
177c0ea7 1606
c519b5e1
GV
1607 if (!DuplicateHandle (parent,
1608 (HANDLE) _get_osfhandle (err),
1609 parent,
1610 &newstderr,
1611 0,
1612 TRUE,
1613 DUPLICATE_SAME_ACCESS))
1614 report_file_error ("Duplicating error handle for child", Qnil);
1615
1616 /* and store them as our std handles */
1617 if (!SetStdHandle (STD_INPUT_HANDLE, newstdin))
1618 report_file_error ("Changing stdin handle", Qnil);
177c0ea7 1619
c519b5e1
GV
1620 if (!SetStdHandle (STD_OUTPUT_HANDLE, newstdout))
1621 report_file_error ("Changing stdout handle", Qnil);
1622
1623 if (!SetStdHandle (STD_ERROR_HANDLE, newstderr))
1624 report_file_error ("Changing stderr handle", Qnil);
1625}
1626
1627void
1628reset_standard_handles (int in, int out, int err, HANDLE handles[3])
1629{
1630 /* close the duplicated handles passed to the child */
1631 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
1632 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
1633 CloseHandle (GetStdHandle (STD_ERROR_HANDLE));
1634
1635 /* now restore parent's saved std handles */
1636 SetStdHandle (STD_INPUT_HANDLE, handles[0]);
1637 SetStdHandle (STD_OUTPUT_HANDLE, handles[1]);
1638 SetStdHandle (STD_ERROR_HANDLE, handles[2]);
6cdfb6e6 1639}
c519b5e1 1640
b2fc9f3d
GV
1641void
1642set_process_dir (char * dir)
1643{
1644 process_dir = dir;
1645}
1646
a11e68d0
RS
1647/* To avoid problems with winsock implementations that work over dial-up
1648 connections causing or requiring a connection to exist while Emacs is
1649 running, Emacs no longer automatically loads winsock on startup if it
1650 is present. Instead, it will be loaded when open-network-stream is
1651 first called.
1652
1653 To allow full control over when winsock is loaded, we provide these
1654 two functions to dynamically load and unload winsock. This allows
1655 dial-up users to only be connected when they actually need to use
1656 socket services. */
1657
7684e57b 1658/* From w32.c */
a11e68d0
RS
1659extern HANDLE winsock_lib;
1660extern BOOL term_winsock (void);
1661extern BOOL init_winsock (int load_now);
1662
fbd6baed 1663DEFUN ("w32-has-winsock", Fw32_has_winsock, Sw32_has_winsock, 0, 1, 0,
33f09670
JR
1664 doc: /* Test for presence of the Windows socket library `winsock'.
1665Returns non-nil if winsock support is present, nil otherwise.
1666
1667If the optional argument LOAD-NOW is non-nil, the winsock library is
1668also loaded immediately if not already loaded. If winsock is loaded,
1669the winsock local hostname is returned (since this may be different from
1670the value of `system-name' and should supplant it), otherwise t is
1671returned to indicate winsock support is present. */)
5842a27b 1672 (Lisp_Object load_now)
a11e68d0
RS
1673{
1674 int have_winsock;
1675
1676 have_winsock = init_winsock (!NILP (load_now));
1677 if (have_winsock)
1678 {
1679 if (winsock_lib != NULL)
1680 {
1681 /* Return new value for system-name. The best way to do this
1682 is to call init_system_name, saving and restoring the
1683 original value to avoid side-effects. */
1684 Lisp_Object orig_hostname = Vsystem_name;
1685 Lisp_Object hostname;
1686
1687 init_system_name ();
1688 hostname = Vsystem_name;
1689 Vsystem_name = orig_hostname;
1690 return hostname;
1691 }
1692 return Qt;
1693 }
1694 return Qnil;
1695}
1696
fbd6baed 1697DEFUN ("w32-unload-winsock", Fw32_unload_winsock, Sw32_unload_winsock,
a11e68d0 1698 0, 0, 0,
33f09670
JR
1699 doc: /* Unload the Windows socket library `winsock' if loaded.
1700This is provided to allow dial-up socket connections to be disconnected
1701when no longer needed. Returns nil without unloading winsock if any
1702socket connections still exist. */)
5842a27b 1703 (void)
a11e68d0
RS
1704{
1705 return term_winsock () ? Qt : Qnil;
1706}
1707
93fdf2f8 1708\f
b2fc9f3d
GV
1709/* Some miscellaneous functions that are Windows specific, but not GUI
1710 specific (ie. are applicable in terminal or batch mode as well). */
1711
b2fc9f3d 1712DEFUN ("w32-short-file-name", Fw32_short_file_name, Sw32_short_file_name, 1, 1, 0,
33f09670
JR
1713 doc: /* Return the short file name version (8.3) of the full path of FILENAME.
1714If FILENAME does not exist, return nil.
1715All path elements in FILENAME are converted to their short names. */)
5842a27b 1716 (Lisp_Object filename)
b2fc9f3d
GV
1717{
1718 char shortname[MAX_PATH];
1719
b7826503 1720 CHECK_STRING (filename);
b2fc9f3d
GV
1721
1722 /* first expand it. */
1723 filename = Fexpand_file_name (filename, Qnil);
1724
1725 /* luckily, this returns the short version of each element in the path. */
b23077df 1726 if (GetShortPathName (SDATA (ENCODE_FILE (filename)), shortname, MAX_PATH) == 0)
b2fc9f3d
GV
1727 return Qnil;
1728
087fc47a 1729 dostounix_filename (shortname);
b2fc9f3d
GV
1730
1731 return build_string (shortname);
1732}
1733
1734
1735DEFUN ("w32-long-file-name", Fw32_long_file_name, Sw32_long_file_name,
1736 1, 1, 0,
33f09670
JR
1737 doc: /* Return the long file name version of the full path of FILENAME.
1738If FILENAME does not exist, return nil.
1739All path elements in FILENAME are converted to their long names. */)
5842a27b 1740 (Lisp_Object filename)
b2fc9f3d
GV
1741{
1742 char longname[ MAX_PATH ];
8dcaeba2 1743 int drive_only = 0;
b2fc9f3d 1744
b7826503 1745 CHECK_STRING (filename);
b2fc9f3d 1746
8dcaeba2
JR
1747 if (SBYTES (filename) == 2
1748 && *(SDATA (filename) + 1) == ':')
1749 drive_only = 1;
1750
b2fc9f3d
GV
1751 /* first expand it. */
1752 filename = Fexpand_file_name (filename, Qnil);
1753
b23077df 1754 if (!w32_get_long_filename (SDATA (ENCODE_FILE (filename)), longname, MAX_PATH))
b2fc9f3d
GV
1755 return Qnil;
1756
087fc47a 1757 dostounix_filename (longname);
b2fc9f3d 1758
8dcaeba2
JR
1759 /* If we were passed only a drive, make sure that a slash is not appended
1760 for consistency with directories. Allow for drive mapping via SUBST
1761 in case expand-file-name is ever changed to expand those. */
1762 if (drive_only && longname[1] == ':' && longname[2] == '/' && !longname[3])
1763 longname[2] = '\0';
1764
b23077df 1765 return DECODE_FILE (build_string (longname));
b2fc9f3d
GV
1766}
1767
33f09670
JR
1768DEFUN ("w32-set-process-priority", Fw32_set_process_priority,
1769 Sw32_set_process_priority, 2, 2, 0,
1770 doc: /* Set the priority of PROCESS to PRIORITY.
1771If PROCESS is nil, the priority of Emacs is changed, otherwise the
1772priority of the process whose pid is PROCESS is changed.
1773PRIORITY should be one of the symbols high, normal, or low;
1774any other symbol will be interpreted as normal.
1775
1776If successful, the return value is t, otherwise nil. */)
5842a27b 1777 (Lisp_Object process, Lisp_Object priority)
b2fc9f3d
GV
1778{
1779 HANDLE proc_handle = GetCurrentProcess ();
1780 DWORD priority_class = NORMAL_PRIORITY_CLASS;
1781 Lisp_Object result = Qnil;
1782
b7826503 1783 CHECK_SYMBOL (priority);
b2fc9f3d
GV
1784
1785 if (!NILP (process))
1786 {
1787 DWORD pid;
1788 child_process *cp;
1789
b7826503 1790 CHECK_NUMBER (process);
b2fc9f3d
GV
1791
1792 /* Allow pid to be an internally generated one, or one obtained
1793 externally. This is necessary because real pids on Win95 are
1794 negative. */
1795
1796 pid = XINT (process);
1797 cp = find_child_pid (pid);
1798 if (cp != NULL)
1799 pid = cp->procinfo.dwProcessId;
1800
1801 proc_handle = OpenProcess (PROCESS_SET_INFORMATION, FALSE, pid);
1802 }
1803
1804 if (EQ (priority, Qhigh))
1805 priority_class = HIGH_PRIORITY_CLASS;
1806 else if (EQ (priority, Qlow))
1807 priority_class = IDLE_PRIORITY_CLASS;
1808
1809 if (proc_handle != NULL)
1810 {
1811 if (SetPriorityClass (proc_handle, priority_class))
1812 result = Qt;
1813 if (!NILP (process))
1814 CloseHandle (proc_handle);
1815 }
1816
1817 return result;
1818}
1819
d613418b
EZ
1820#ifdef HAVE_LANGINFO_CODESET
1821/* Emulation of nl_langinfo. Used in fns.c:Flocale_info. */
b56ceb92
JB
1822char *
1823nl_langinfo (nl_item item)
d613418b
EZ
1824{
1825 /* Conversion of Posix item numbers to their Windows equivalents. */
1826 static const LCTYPE w32item[] = {
1827 LOCALE_IDEFAULTANSICODEPAGE,
1828 LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
1829 LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6, LOCALE_SDAYNAME7,
1830 LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3,
1831 LOCALE_SMONTHNAME4, LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6,
1832 LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8, LOCALE_SMONTHNAME9,
1833 LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12
1834 };
1835
1836 static char *nl_langinfo_buf = NULL;
1837 static int nl_langinfo_len = 0;
1838
1839 if (nl_langinfo_len <= 0)
1840 nl_langinfo_buf = xmalloc (nl_langinfo_len = 1);
1841
1842 if (item < 0 || item >= _NL_NUM)
1843 nl_langinfo_buf[0] = 0;
1844 else
1845 {
1846 LCID cloc = GetThreadLocale ();
1847 int need_len = GetLocaleInfo (cloc, w32item[item] | LOCALE_USE_CP_ACP,
1848 NULL, 0);
1849
1850 if (need_len <= 0)
1851 nl_langinfo_buf[0] = 0;
1852 else
1853 {
1854 if (item == CODESET)
1855 {
1856 need_len += 2; /* for the "cp" prefix */
1857 if (need_len < 8) /* for the case we call GetACP */
1858 need_len = 8;
1859 }
1860 if (nl_langinfo_len <= need_len)
1861 nl_langinfo_buf = xrealloc (nl_langinfo_buf,
1862 nl_langinfo_len = need_len);
1863 if (!GetLocaleInfo (cloc, w32item[item] | LOCALE_USE_CP_ACP,
1864 nl_langinfo_buf, nl_langinfo_len))
1865 nl_langinfo_buf[0] = 0;
1866 else if (item == CODESET)
1867 {
1868 if (strcmp (nl_langinfo_buf, "0") == 0 /* CP_ACP */
1869 || strcmp (nl_langinfo_buf, "1") == 0) /* CP_OEMCP */
1870 sprintf (nl_langinfo_buf, "cp%u", GetACP ());
1871 else
1872 {
1873 memmove (nl_langinfo_buf + 2, nl_langinfo_buf,
1874 strlen (nl_langinfo_buf) + 1);
1875 nl_langinfo_buf[0] = 'c';
1876 nl_langinfo_buf[1] = 'p';
1877 }
1878 }
1879 }
1880 }
1881 return nl_langinfo_buf;
1882}
1883#endif /* HAVE_LANGINFO_CODESET */
b2fc9f3d 1884
33f09670
JR
1885DEFUN ("w32-get-locale-info", Fw32_get_locale_info,
1886 Sw32_get_locale_info, 1, 2, 0,
1887 doc: /* Return information about the Windows locale LCID.
1888By default, return a three letter locale code which encodes the default
35f36d65 1889language as the first two characters, and the country or regional variant
33f09670
JR
1890as the third letter. For example, ENU refers to `English (United States)',
1891while ENC means `English (Canadian)'.
1892
1893If the optional argument LONGFORM is t, the long form of the locale
1894name is returned, e.g. `English (United States)' instead; if LONGFORM
1895is a number, it is interpreted as an LCTYPE constant and the corresponding
1896locale information is returned.
1897
1898If LCID (a 16-bit number) is not a valid locale, the result is nil. */)
5842a27b 1899 (Lisp_Object lcid, Lisp_Object longform)
b2fc9f3d
GV
1900{
1901 int got_abbrev;
1902 int got_full;
1903 char abbrev_name[32] = { 0 };
1904 char full_name[256] = { 0 };
1905
b7826503 1906 CHECK_NUMBER (lcid);
b2fc9f3d
GV
1907
1908 if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
1909 return Qnil;
1910
1911 if (NILP (longform))
1912 {
1913 got_abbrev = GetLocaleInfo (XINT (lcid),
1914 LOCALE_SABBREVLANGNAME | LOCALE_USE_CP_ACP,
1915 abbrev_name, sizeof (abbrev_name));
1916 if (got_abbrev)
1917 return build_string (abbrev_name);
1918 }
0eaf5926 1919 else if (EQ (longform, Qt))
b2fc9f3d
GV
1920 {
1921 got_full = GetLocaleInfo (XINT (lcid),
1922 LOCALE_SLANGUAGE | LOCALE_USE_CP_ACP,
1923 full_name, sizeof (full_name));
1924 if (got_full)
011a0143 1925 return DECODE_SYSTEM (build_string (full_name));
b2fc9f3d 1926 }
0eaf5926
GV
1927 else if (NUMBERP (longform))
1928 {
1929 got_full = GetLocaleInfo (XINT (lcid),
1930 XINT (longform),
1931 full_name, sizeof (full_name));
1932 if (got_full)
1933 return make_unibyte_string (full_name, got_full);
1934 }
b2fc9f3d
GV
1935
1936 return Qnil;
1937}
1938
1939
33f09670
JR
1940DEFUN ("w32-get-current-locale-id", Fw32_get_current_locale_id,
1941 Sw32_get_current_locale_id, 0, 0, 0,
1942 doc: /* Return Windows locale id for current locale setting.
1943This is a numerical value; use `w32-get-locale-info' to convert to a
1944human-readable form. */)
5842a27b 1945 (void)
b2fc9f3d
GV
1946{
1947 return make_number (GetThreadLocale ());
1948}
1949
24f981c9 1950static DWORD
b56ceb92 1951int_from_hex (char * s)
ef79fbba
GV
1952{
1953 DWORD val = 0;
1954 static char hex[] = "0123456789abcdefABCDEF";
1955 char * p;
1956
ed3751c8 1957 while (*s && (p = strchr (hex, *s)) != NULL)
ef79fbba
GV
1958 {
1959 unsigned digit = p - hex;
1960 if (digit > 15)
1961 digit -= 6;
1962 val = val * 16 + digit;
1963 s++;
1964 }
1965 return val;
1966}
1967
1968/* We need to build a global list, since the EnumSystemLocale callback
1969 function isn't given a context pointer. */
1970Lisp_Object Vw32_valid_locale_ids;
1971
24f981c9 1972static BOOL CALLBACK
b56ceb92 1973enum_locale_fn (LPTSTR localeNum)
ef79fbba
GV
1974{
1975 DWORD id = int_from_hex (localeNum);
1976 Vw32_valid_locale_ids = Fcons (make_number (id), Vw32_valid_locale_ids);
1977 return TRUE;
1978}
1979
33f09670
JR
1980DEFUN ("w32-get-valid-locale-ids", Fw32_get_valid_locale_ids,
1981 Sw32_get_valid_locale_ids, 0, 0, 0,
1982 doc: /* Return list of all valid Windows locale ids.
1983Each id is a numerical value; use `w32-get-locale-info' to convert to a
1984human-readable form. */)
5842a27b 1985 (void)
ef79fbba
GV
1986{
1987 Vw32_valid_locale_ids = Qnil;
1988
1989 EnumSystemLocales (enum_locale_fn, LCID_SUPPORTED);
1990
1991 Vw32_valid_locale_ids = Fnreverse (Vw32_valid_locale_ids);
1992 return Vw32_valid_locale_ids;
1993}
1994
b2fc9f3d
GV
1995
1996DEFUN ("w32-get-default-locale-id", Fw32_get_default_locale_id, Sw32_get_default_locale_id, 0, 1, 0,
33f09670
JR
1997 doc: /* Return Windows locale id for default locale setting.
1998By default, the system default locale setting is returned; if the optional
1999parameter USERP is non-nil, the user default locale setting is returned.
2000This is a numerical value; use `w32-get-locale-info' to convert to a
2001human-readable form. */)
5842a27b 2002 (Lisp_Object userp)
b2fc9f3d
GV
2003{
2004 if (NILP (userp))
2005 return make_number (GetSystemDefaultLCID ());
2006 return make_number (GetUserDefaultLCID ());
2007}
2008
177c0ea7 2009
b2fc9f3d 2010DEFUN ("w32-set-current-locale", Fw32_set_current_locale, Sw32_set_current_locale, 1, 1, 0,
33f09670
JR
2011 doc: /* Make Windows locale LCID be the current locale setting for Emacs.
2012If successful, the new locale id is returned, otherwise nil. */)
5842a27b 2013 (Lisp_Object lcid)
b2fc9f3d 2014{
b7826503 2015 CHECK_NUMBER (lcid);
b2fc9f3d
GV
2016
2017 if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
2018 return Qnil;
2019
2020 if (!SetThreadLocale (XINT (lcid)))
2021 return Qnil;
2022
ef79fbba
GV
2023 /* Need to set input thread locale if present. */
2024 if (dwWindowsThreadId)
2025 /* Reply is not needed. */
2026 PostThreadMessage (dwWindowsThreadId, WM_EMACS_SETLOCALE, XINT (lcid), 0);
2027
b2fc9f3d
GV
2028 return make_number (GetThreadLocale ());
2029}
2030
0eaf5926
GV
2031
2032/* We need to build a global list, since the EnumCodePages callback
2033 function isn't given a context pointer. */
2034Lisp_Object Vw32_valid_codepages;
2035
24f981c9 2036static BOOL CALLBACK
b56ceb92 2037enum_codepage_fn (LPTSTR codepageNum)
0eaf5926
GV
2038{
2039 DWORD id = atoi (codepageNum);
2040 Vw32_valid_codepages = Fcons (make_number (id), Vw32_valid_codepages);
2041 return TRUE;
2042}
2043
33f09670
JR
2044DEFUN ("w32-get-valid-codepages", Fw32_get_valid_codepages,
2045 Sw32_get_valid_codepages, 0, 0, 0,
2046 doc: /* Return list of all valid Windows codepages. */)
5842a27b 2047 (void)
0eaf5926
GV
2048{
2049 Vw32_valid_codepages = Qnil;
2050
2051 EnumSystemCodePages (enum_codepage_fn, CP_SUPPORTED);
2052
2053 Vw32_valid_codepages = Fnreverse (Vw32_valid_codepages);
2054 return Vw32_valid_codepages;
2055}
2056
2057
33f09670
JR
2058DEFUN ("w32-get-console-codepage", Fw32_get_console_codepage,
2059 Sw32_get_console_codepage, 0, 0, 0,
2060 doc: /* Return current Windows codepage for console input. */)
5842a27b 2061 (void)
0eaf5926
GV
2062{
2063 return make_number (GetConsoleCP ());
2064}
2065
177c0ea7 2066
33f09670
JR
2067DEFUN ("w32-set-console-codepage", Fw32_set_console_codepage,
2068 Sw32_set_console_codepage, 1, 1, 0,
2069 doc: /* Make Windows codepage CP be the current codepage setting for Emacs.
2070The codepage setting affects keyboard input and display in tty mode.
2071If successful, the new CP is returned, otherwise nil. */)
5842a27b 2072 (Lisp_Object cp)
0eaf5926 2073{
b7826503 2074 CHECK_NUMBER (cp);
0eaf5926
GV
2075
2076 if (!IsValidCodePage (XINT (cp)))
2077 return Qnil;
2078
2079 if (!SetConsoleCP (XINT (cp)))
2080 return Qnil;
2081
2082 return make_number (GetConsoleCP ());
2083}
2084
2085
33f09670
JR
2086DEFUN ("w32-get-console-output-codepage", Fw32_get_console_output_codepage,
2087 Sw32_get_console_output_codepage, 0, 0, 0,
2088 doc: /* Return current Windows codepage for console output. */)
5842a27b 2089 (void)
0eaf5926
GV
2090{
2091 return make_number (GetConsoleOutputCP ());
2092}
2093
177c0ea7 2094
33f09670
JR
2095DEFUN ("w32-set-console-output-codepage", Fw32_set_console_output_codepage,
2096 Sw32_set_console_output_codepage, 1, 1, 0,
2097 doc: /* Make Windows codepage CP be the current codepage setting for Emacs.
2098The codepage setting affects keyboard input and display in tty mode.
2099If successful, the new CP is returned, otherwise nil. */)
5842a27b 2100 (Lisp_Object cp)
0eaf5926 2101{
b7826503 2102 CHECK_NUMBER (cp);
0eaf5926
GV
2103
2104 if (!IsValidCodePage (XINT (cp)))
2105 return Qnil;
2106
2107 if (!SetConsoleOutputCP (XINT (cp)))
2108 return Qnil;
2109
2110 return make_number (GetConsoleOutputCP ());
2111}
2112
2113
33f09670
JR
2114DEFUN ("w32-get-codepage-charset", Fw32_get_codepage_charset,
2115 Sw32_get_codepage_charset, 1, 1, 0,
2116 doc: /* Return charset of codepage CP.
2117Returns nil if the codepage is not valid. */)
5842a27b 2118 (Lisp_Object cp)
0eaf5926
GV
2119{
2120 CHARSETINFO info;
2121
b7826503 2122 CHECK_NUMBER (cp);
0eaf5926
GV
2123
2124 if (!IsValidCodePage (XINT (cp)))
2125 return Qnil;
2126
2127 if (TranslateCharsetInfo ((DWORD *) XINT (cp), &info, TCI_SRCCODEPAGE))
2128 return make_number (info.ciCharset);
2129
2130 return Qnil;
2131}
2132
2133
33f09670
JR
2134DEFUN ("w32-get-valid-keyboard-layouts", Fw32_get_valid_keyboard_layouts,
2135 Sw32_get_valid_keyboard_layouts, 0, 0, 0,
2136 doc: /* Return list of Windows keyboard languages and layouts.
2137The return value is a list of pairs of language id and layout id. */)
5842a27b 2138 (void)
0eaf5926
GV
2139{
2140 int num_layouts = GetKeyboardLayoutList (0, NULL);
2141 HKL * layouts = (HKL *) alloca (num_layouts * sizeof (HKL));
2142 Lisp_Object obj = Qnil;
2143
2144 if (GetKeyboardLayoutList (num_layouts, layouts) == num_layouts)
2145 {
2146 while (--num_layouts >= 0)
2147 {
2148 DWORD kl = (DWORD) layouts[num_layouts];
2149
2150 obj = Fcons (Fcons (make_number (kl & 0xffff),
2151 make_number ((kl >> 16) & 0xffff)),
2152 obj);
2153 }
2154 }
2155
2156 return obj;
2157}
2158
2159
33f09670
JR
2160DEFUN ("w32-get-keyboard-layout", Fw32_get_keyboard_layout,
2161 Sw32_get_keyboard_layout, 0, 0, 0,
2162 doc: /* Return current Windows keyboard language and layout.
2163The return value is the cons of the language id and the layout id. */)
5842a27b 2164 (void)
0eaf5926
GV
2165{
2166 DWORD kl = (DWORD) GetKeyboardLayout (dwWindowsThreadId);
2167
2168 return Fcons (make_number (kl & 0xffff),
2169 make_number ((kl >> 16) & 0xffff));
2170}
2171
177c0ea7 2172
33f09670
JR
2173DEFUN ("w32-set-keyboard-layout", Fw32_set_keyboard_layout,
2174 Sw32_set_keyboard_layout, 1, 1, 0,
2175 doc: /* Make LAYOUT be the current keyboard layout for Emacs.
2176The keyboard layout setting affects interpretation of keyboard input.
2177If successful, the new layout id is returned, otherwise nil. */)
5842a27b 2178 (Lisp_Object layout)
0eaf5926
GV
2179{
2180 DWORD kl;
2181
b7826503 2182 CHECK_CONS (layout);
f4532092
AI
2183 CHECK_NUMBER_CAR (layout);
2184 CHECK_NUMBER_CDR (layout);
0eaf5926 2185
8e713be6
KR
2186 kl = (XINT (XCAR (layout)) & 0xffff)
2187 | (XINT (XCDR (layout)) << 16);
0eaf5926
GV
2188
2189 /* Synchronize layout with input thread. */
2190 if (dwWindowsThreadId)
2191 {
2192 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_SETKEYBOARDLAYOUT,
2193 (WPARAM) kl, 0))
2194 {
2195 MSG msg;
2196 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
2197
2198 if (msg.wParam == 0)
2199 return Qnil;
2200 }
2201 }
2202 else if (!ActivateKeyboardLayout ((HKL) kl, 0))
2203 return Qnil;
2204
2205 return Fw32_get_keyboard_layout ();
2206}
2207
b2fc9f3d 2208\f
b56ceb92
JB
2209void
2210syms_of_ntproc (void)
93fdf2f8 2211{
51128692
JR
2212 DEFSYM (Qhigh, "high");
2213 DEFSYM (Qlow, "low");
b2fc9f3d 2214
fbd6baed
GV
2215 defsubr (&Sw32_has_winsock);
2216 defsubr (&Sw32_unload_winsock);
7d701334 2217
b2fc9f3d
GV
2218 defsubr (&Sw32_short_file_name);
2219 defsubr (&Sw32_long_file_name);
2220 defsubr (&Sw32_set_process_priority);
2221 defsubr (&Sw32_get_locale_info);
2222 defsubr (&Sw32_get_current_locale_id);
2223 defsubr (&Sw32_get_default_locale_id);
ef79fbba 2224 defsubr (&Sw32_get_valid_locale_ids);
b2fc9f3d 2225 defsubr (&Sw32_set_current_locale);
a11e68d0 2226
0eaf5926
GV
2227 defsubr (&Sw32_get_console_codepage);
2228 defsubr (&Sw32_set_console_codepage);
2229 defsubr (&Sw32_get_console_output_codepage);
2230 defsubr (&Sw32_set_console_output_codepage);
2231 defsubr (&Sw32_get_valid_codepages);
2232 defsubr (&Sw32_get_codepage_charset);
2233
2234 defsubr (&Sw32_get_valid_keyboard_layouts);
2235 defsubr (&Sw32_get_keyboard_layout);
2236 defsubr (&Sw32_set_keyboard_layout);
2237
29208e82 2238 DEFVAR_LISP ("w32-quote-process-args", Vw32_quote_process_args,
33f09670
JR
2239 doc: /* Non-nil enables quoting of process arguments to ensure correct parsing.
2240Because Windows does not directly pass argv arrays to child processes,
2241programs have to reconstruct the argv array by parsing the command
2242line string. For an argument to contain a space, it must be enclosed
2243in double quotes or it will be parsed as multiple arguments.
2244
2245If the value is a character, that character will be used to escape any
2246quote characters that appear, otherwise a suitable escape character
2247will be chosen based on the type of the program. */);
b2fc9f3d 2248 Vw32_quote_process_args = Qt;
817abdf6 2249
fbd6baed 2250 DEFVAR_LISP ("w32-start-process-show-window",
29208e82 2251 Vw32_start_process_show_window,
33f09670
JR
2252 doc: /* When nil, new child processes hide their windows.
2253When non-nil, they show their window in the method of their choice.
2254This variable doesn't affect GUI applications, which will never be hidden. */);
fbd6baed 2255 Vw32_start_process_show_window = Qnil;
0ecf7d36 2256
b2fc9f3d 2257 DEFVAR_LISP ("w32-start-process-share-console",
29208e82 2258 Vw32_start_process_share_console,
33f09670
JR
2259 doc: /* When nil, new child processes are given a new console.
2260When non-nil, they share the Emacs console; this has the limitation of
804d894a 2261allowing only one DOS subprocess to run at a time (whether started directly
33f09670
JR
2262or indirectly by Emacs), and preventing Emacs from cleanly terminating the
2263subprocess group, but may allow Emacs to interrupt a subprocess that doesn't
2264otherwise respond to interrupts from Emacs. */);
b2fc9f3d
GV
2265 Vw32_start_process_share_console = Qnil;
2266
82e7c0a9 2267 DEFVAR_LISP ("w32-start-process-inherit-error-mode",
29208e82 2268 Vw32_start_process_inherit_error_mode,
33f09670
JR
2269 doc: /* When nil, new child processes revert to the default error mode.
2270When non-nil, they inherit their error mode setting from Emacs, which stops
2271them blocking when trying to access unmounted drives etc. */);
82e7c0a9
AI
2272 Vw32_start_process_inherit_error_mode = Qt;
2273
29208e82 2274 DEFVAR_INT ("w32-pipe-read-delay", w32_pipe_read_delay,
33f09670
JR
2275 doc: /* Forced delay before reading subprocess output.
2276This is done to improve the buffering of subprocess output, by
2277avoiding the inefficiency of frequently reading small amounts of data.
2278
2279If positive, the value is the number of milliseconds to sleep before
2280reading the subprocess output. If negative, the magnitude is the number
2281of time slices to wait (effectively boosting the priority of the child
2282process temporarily). A value of zero disables waiting entirely. */);
5322f50b 2283 w32_pipe_read_delay = 50;
0c04091e 2284
29208e82 2285 DEFVAR_LISP ("w32-downcase-file-names", Vw32_downcase_file_names,
33f09670
JR
2286 doc: /* Non-nil means convert all-upper case file names to lower case.
2287This applies when performing completions and file name expansion.
2288Note that the value of this setting also affects remote file names,
2289so you probably don't want to set to non-nil if you use case-sensitive
177c0ea7 2290filesystems via ange-ftp. */);
fbd6baed 2291 Vw32_downcase_file_names = Qnil;
b2fc9f3d
GV
2292
2293#if 0
29208e82 2294 DEFVAR_LISP ("w32-generate-fake-inodes", Vw32_generate_fake_inodes,
33f09670
JR
2295 doc: /* Non-nil means attempt to fake realistic inode values.
2296This works by hashing the truename of files, and should detect
2297aliasing between long and short (8.3 DOS) names, but can have
2298false positives because of hash collisions. Note that determing
2299the truename of a file can be slow. */);
b2fc9f3d
GV
2300 Vw32_generate_fake_inodes = Qnil;
2301#endif
2302
29208e82 2303 DEFVAR_LISP ("w32-get-true-file-attributes", Vw32_get_true_file_attributes,
ed4c17bb
EZ
2304 doc: /* Non-nil means determine accurate file attributes in `file-attributes'.
2305This option controls whether to issue additional system calls to determine
017dab84 2306accurate link counts, file type, and ownership information. It is more
ed4c17bb 2307useful for files on NTFS volumes, where hard links and file security are
017dab84 2308supported, than on volumes of the FAT family.
ed4c17bb
EZ
2309
2310Without these system calls, link count will always be reported as 1 and file
2311ownership will be attributed to the current user.
2312The default value `local' means only issue these system calls for files
2313on local fixed drives. A value of nil means never issue them.
2314Any other non-nil value means do this even on remote and removable drives
2315where the performance impact may be noticeable even on modern hardware. */);
2fa4f090 2316 Vw32_get_true_file_attributes = Qlocal;
af621bc3
EZ
2317
2318 staticpro (&Vw32_valid_locale_ids);
2319 staticpro (&Vw32_valid_codepages);
93fdf2f8 2320}
42a7e7f1 2321/* end of w32proc.c */
ab5796a9 2322