100
|
1 /* Utility and Unix shadow routines for XEmacs on Windows NT.
|
|
2 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to the Free
|
|
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
19 02111-1307, USA.
|
|
20
|
|
21
|
|
22 Geoff Voelker (voelker@cs.washington.edu) 7-29-94 */
|
|
23
|
|
24 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
|
209
|
25 /* Sync'ed with Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
|
|
26
|
290
|
27 #include <config.h>
|
100
|
28
|
290
|
29 #undef signal
|
|
30 #define getwd _getwd
|
|
31 #include "lisp.h"
|
|
32 #undef getwd
|
|
33
|
223
|
34 #include "systime.h"
|
|
35 #include "syssignal.h"
|
239
|
36 #include "sysproc.h"
|
223
|
37
|
290
|
38 #include <ctype.h>
|
|
39 #include <direct.h>
|
|
40 #include <errno.h>
|
|
41 #include <fcntl.h>
|
|
42 #include <io.h>
|
100
|
43 #include <pwd.h>
|
290
|
44 #include <signal.h>
|
|
45 #include <stddef.h> /* for offsetof */
|
|
46 #include <string.h>
|
|
47 #include <stdlib.h>
|
|
48 #include <stdio.h>
|
100
|
49
|
|
50 #include <windows.h>
|
223
|
51 #include <mmsystem.h>
|
100
|
52
|
|
53 #include "nt.h"
|
|
54 #include <sys/dir.h>
|
|
55 #include "ntheap.h"
|
|
56
|
209
|
57
|
263
|
58 extern Lisp_Object Vmswindows_downcase_file_names;
|
|
59 #if 0
|
209
|
60 extern Lisp_Object Vwin32_generate_fake_inodes;
|
263
|
61 #endif
|
|
62 extern Lisp_Object Vmswindows_get_true_file_attributes;
|
209
|
63
|
288
|
64 extern char *get_home_directory(void);
|
|
65
|
209
|
66 static char startup_dir[ MAXPATHLEN ];
|
|
67
|
100
|
68 /* Get the current working directory. */
|
|
69 char *
|
|
70 getwd (char *dir)
|
|
71 {
|
209
|
72 #if 0
|
100
|
73 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
|
|
74 return dir;
|
|
75 return NULL;
|
209
|
76 #else
|
|
77 /* Emacs doesn't actually change directory itself, and we want to
|
|
78 force our real wd to be where emacs.exe is to avoid unnecessary
|
|
79 conflicts when trying to rename or delete directories. */
|
|
80 strcpy (dir, startup_dir);
|
|
81 return dir;
|
|
82 #endif
|
100
|
83 }
|
|
84
|
|
85 /* Emulate getloadavg. */
|
|
86 int
|
|
87 getloadavg (double loadavg[], int nelem)
|
|
88 {
|
|
89 int i;
|
|
90
|
|
91 /* A faithful emulation is going to have to be saved for a rainy day. */
|
|
92 for (i = 0; i < nelem; i++)
|
|
93 {
|
|
94 loadavg[i] = 0.0;
|
|
95 }
|
|
96 return i;
|
|
97 }
|
|
98
|
|
99 /* Emulate getpwuid, getpwnam and others. */
|
|
100
|
|
101 #define PASSWD_FIELD_SIZE 256
|
|
102
|
|
103 static char the_passwd_name[PASSWD_FIELD_SIZE];
|
|
104 static char the_passwd_passwd[PASSWD_FIELD_SIZE];
|
|
105 static char the_passwd_gecos[PASSWD_FIELD_SIZE];
|
|
106 static char the_passwd_dir[PASSWD_FIELD_SIZE];
|
|
107 static char the_passwd_shell[PASSWD_FIELD_SIZE];
|
|
108
|
|
109 static struct passwd the_passwd =
|
|
110 {
|
|
111 the_passwd_name,
|
|
112 the_passwd_passwd,
|
|
113 0,
|
|
114 0,
|
|
115 0,
|
|
116 the_passwd_gecos,
|
|
117 the_passwd_dir,
|
|
118 the_passwd_shell,
|
|
119 };
|
|
120
|
|
121 int
|
|
122 getuid ()
|
|
123 {
|
|
124 return the_passwd.pw_uid;
|
|
125 }
|
|
126
|
|
127 int
|
|
128 geteuid ()
|
|
129 {
|
|
130 /* I could imagine arguing for checking to see whether the user is
|
|
131 in the Administrators group and returning a UID of 0 for that
|
|
132 case, but I don't know how wise that would be in the long run. */
|
|
133 return getuid ();
|
|
134 }
|
|
135
|
|
136 int
|
|
137 getgid ()
|
|
138 {
|
|
139 return the_passwd.pw_gid;
|
|
140 }
|
|
141
|
|
142 int
|
|
143 getegid ()
|
|
144 {
|
|
145 return getgid ();
|
|
146 }
|
|
147
|
|
148 struct passwd *
|
|
149 getpwuid (int uid)
|
|
150 {
|
|
151 if (uid == the_passwd.pw_uid)
|
|
152 return &the_passwd;
|
|
153 return NULL;
|
|
154 }
|
|
155
|
|
156 struct passwd *
|
288
|
157 getpwnam (const char *name)
|
100
|
158 {
|
|
159 struct passwd *pw;
|
|
160
|
|
161 pw = getpwuid (getuid ());
|
|
162 if (!pw)
|
|
163 return pw;
|
|
164
|
|
165 if (stricmp (name, pw->pw_name))
|
|
166 return NULL;
|
|
167
|
|
168 return pw;
|
|
169 }
|
|
170
|
|
171 void
|
|
172 init_user_info ()
|
|
173 {
|
|
174 /* Find the user's real name by opening the process token and
|
|
175 looking up the name associated with the user-sid in that token.
|
|
176
|
|
177 Use the relative portion of the identifier authority value from
|
|
178 the user-sid as the user id value (same for group id using the
|
|
179 primary group sid from the process token). */
|
|
180
|
|
181 char user_sid[256], name[256], domain[256];
|
|
182 DWORD length = sizeof (name), dlength = sizeof (domain), trash;
|
|
183 HANDLE token = NULL;
|
|
184 SID_NAME_USE user_type;
|
|
185
|
|
186 if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token)
|
|
187 && GetTokenInformation (token, TokenUser,
|
|
188 (PVOID) user_sid, sizeof (user_sid), &trash)
|
|
189 && LookupAccountSid (NULL, *((PSID *) user_sid), name, &length,
|
|
190 domain, &dlength, &user_type))
|
|
191 {
|
|
192 strcpy (the_passwd.pw_name, name);
|
|
193 /* Determine a reasonable uid value. */
|
|
194 if (stricmp ("administrator", name) == 0)
|
|
195 {
|
|
196 the_passwd.pw_uid = 0;
|
|
197 the_passwd.pw_gid = 0;
|
|
198 }
|
|
199 else
|
|
200 {
|
|
201 SID_IDENTIFIER_AUTHORITY * pSIA;
|
|
202
|
|
203 pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
|
|
204 /* I believe the relative portion is the last 4 bytes (of 6)
|
|
205 with msb first. */
|
|
206 the_passwd.pw_uid = ((pSIA->Value[2] << 24) +
|
|
207 (pSIA->Value[3] << 16) +
|
|
208 (pSIA->Value[4] << 8) +
|
|
209 (pSIA->Value[5] << 0));
|
|
210 /* restrict to conventional uid range for normal users */
|
|
211 the_passwd.pw_uid = the_passwd.pw_uid % 60001;
|
|
212
|
|
213 /* Get group id */
|
|
214 if (GetTokenInformation (token, TokenPrimaryGroup,
|
|
215 (PVOID) user_sid, sizeof (user_sid), &trash))
|
|
216 {
|
|
217 SID_IDENTIFIER_AUTHORITY * pSIA;
|
|
218
|
|
219 pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
|
|
220 the_passwd.pw_gid = ((pSIA->Value[2] << 24) +
|
|
221 (pSIA->Value[3] << 16) +
|
|
222 (pSIA->Value[4] << 8) +
|
|
223 (pSIA->Value[5] << 0));
|
|
224 /* I don't know if this is necessary, but for safety... */
|
|
225 the_passwd.pw_gid = the_passwd.pw_gid % 60001;
|
|
226 }
|
|
227 else
|
|
228 the_passwd.pw_gid = the_passwd.pw_uid;
|
|
229 }
|
|
230 }
|
|
231 /* If security calls are not supported (presumably because we
|
|
232 are running under Windows 95), fallback to this. */
|
|
233 else if (GetUserName (name, &length))
|
|
234 {
|
|
235 strcpy (the_passwd.pw_name, name);
|
|
236 if (stricmp ("administrator", name) == 0)
|
|
237 the_passwd.pw_uid = 0;
|
|
238 else
|
|
239 the_passwd.pw_uid = 123;
|
|
240 the_passwd.pw_gid = the_passwd.pw_uid;
|
|
241 }
|
|
242 else
|
|
243 {
|
|
244 strcpy (the_passwd.pw_name, "unknown");
|
|
245 the_passwd.pw_uid = 123;
|
|
246 the_passwd.pw_gid = 123;
|
|
247 }
|
|
248
|
|
249 /* Ensure HOME and SHELL are defined. */
|
288
|
250 #if 0
|
|
251 /*
|
|
252 * With XEmacs, setting $HOME is deprecated.
|
|
253 */
|
100
|
254 if (getenv ("HOME") == NULL)
|
|
255 putenv ("HOME=c:/");
|
288
|
256 #endif
|
100
|
257 if (getenv ("SHELL") == NULL)
|
|
258 putenv ((GetVersion () & 0x80000000) ? "SHELL=command" : "SHELL=cmd");
|
|
259
|
|
260 /* Set dir and shell from environment variables. */
|
288
|
261 strcpy (the_passwd.pw_dir, get_home_directory());
|
100
|
262 strcpy (the_passwd.pw_shell, getenv ("SHELL"));
|
|
263
|
|
264 if (token)
|
|
265 CloseHandle (token);
|
|
266 }
|
|
267
|
|
268 /* Normalize filename by converting all path separators to
|
|
269 the specified separator. Also conditionally convert upper
|
|
270 case path name components to lower case. */
|
|
271
|
|
272 static void
|
|
273 normalize_filename (fp, path_sep)
|
203
|
274 REGISTER char *fp;
|
100
|
275 char path_sep;
|
|
276 {
|
|
277 char sep;
|
|
278 char *elem;
|
|
279
|
|
280 /* Always lower-case drive letters a-z, even if the filesystem
|
|
281 preserves case in filenames.
|
|
282 This is so filenames can be compared by string comparison
|
|
283 functions that are case-sensitive. Even case-preserving filesystems
|
|
284 do not distinguish case in drive letters. */
|
|
285 if (fp[1] == ':' && *fp >= 'A' && *fp <= 'Z')
|
|
286 {
|
|
287 *fp += 'a' - 'A';
|
|
288 fp += 2;
|
|
289 }
|
|
290
|
263
|
291 if (NILP (Vmswindows_downcase_file_names))
|
100
|
292 {
|
|
293 while (*fp)
|
|
294 {
|
|
295 if (*fp == '/' || *fp == '\\')
|
|
296 *fp = path_sep;
|
|
297 fp++;
|
|
298 }
|
|
299 return;
|
|
300 }
|
|
301
|
|
302 sep = path_sep; /* convert to this path separator */
|
|
303 elem = fp; /* start of current path element */
|
|
304
|
|
305 do {
|
|
306 if (*fp >= 'a' && *fp <= 'z')
|
|
307 elem = 0; /* don't convert this element */
|
|
308
|
|
309 if (*fp == 0 || *fp == ':')
|
|
310 {
|
|
311 sep = *fp; /* restore current separator (or 0) */
|
|
312 *fp = '/'; /* after conversion of this element */
|
|
313 }
|
|
314
|
|
315 if (*fp == '/' || *fp == '\\')
|
|
316 {
|
|
317 if (elem && elem != fp)
|
|
318 {
|
|
319 *fp = 0; /* temporary end of string */
|
|
320 _strlwr (elem); /* while we convert to lower case */
|
|
321 }
|
|
322 *fp = sep; /* convert (or restore) path separator */
|
|
323 elem = fp + 1; /* next element starts after separator */
|
|
324 sep = path_sep;
|
|
325 }
|
|
326 } while (*fp++);
|
|
327 }
|
|
328
|
|
329 /* Destructively turn backslashes into slashes. */
|
|
330 void
|
|
331 dostounix_filename (p)
|
203
|
332 REGISTER char *p;
|
100
|
333 {
|
|
334 normalize_filename (p, '/');
|
|
335 }
|
|
336
|
|
337 /* Destructively turn slashes into backslashes. */
|
|
338 void
|
|
339 unixtodos_filename (p)
|
203
|
340 REGISTER char *p;
|
100
|
341 {
|
|
342 normalize_filename (p, '\\');
|
|
343 }
|
|
344
|
|
345 /* Remove all CR's that are followed by a LF.
|
|
346 (From msdos.c...probably should figure out a way to share it,
|
|
347 although this code isn't going to ever change.) */
|
|
348 int
|
209
|
349 crlf_to_lf (n, buf, lf_count)
|
203
|
350 REGISTER int n;
|
|
351 REGISTER unsigned char *buf;
|
209
|
352 REGISTER unsigned *lf_count;
|
100
|
353 {
|
|
354 unsigned char *np = buf;
|
|
355 unsigned char *startp = buf;
|
|
356 unsigned char *endp = buf + n;
|
|
357
|
|
358 if (n == 0)
|
|
359 return n;
|
|
360 while (buf < endp - 1)
|
|
361 {
|
209
|
362 if (*buf == 0x0a)
|
|
363 (*lf_count)++;
|
100
|
364 if (*buf == 0x0d)
|
|
365 {
|
|
366 if (*(++buf) != 0x0a)
|
|
367 *np++ = 0x0d;
|
|
368 }
|
|
369 else
|
|
370 *np++ = *buf++;
|
|
371 }
|
|
372 if (buf < endp)
|
209
|
373 {
|
|
374 if (*buf == 0x0a)
|
|
375 (*lf_count)++;
|
100
|
376 *np++ = *buf++;
|
209
|
377 }
|
100
|
378 return np - startp;
|
|
379 }
|
|
380
|
209
|
381 /* Parse the root part of file name, if present. Return length and
|
|
382 optionally store pointer to char after root. */
|
|
383 static int
|
|
384 parse_root (char * name, char ** pPath)
|
|
385 {
|
|
386 char * start = name;
|
|
387
|
|
388 if (name == NULL)
|
|
389 return 0;
|
|
390
|
|
391 /* find the root name of the volume if given */
|
|
392 if (isalpha (name[0]) && name[1] == ':')
|
|
393 {
|
|
394 /* skip past drive specifier */
|
|
395 name += 2;
|
|
396 if (IS_DIRECTORY_SEP (name[0]))
|
|
397 name++;
|
|
398 }
|
|
399 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
|
|
400 {
|
|
401 int slashes = 2;
|
|
402 name += 2;
|
|
403 do
|
|
404 {
|
|
405 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
|
|
406 break;
|
|
407 name++;
|
|
408 }
|
|
409 while ( *name );
|
|
410 if (IS_DIRECTORY_SEP (name[0]))
|
|
411 name++;
|
|
412 }
|
|
413
|
|
414 if (pPath)
|
|
415 *pPath = name;
|
|
416
|
|
417 return name - start;
|
|
418 }
|
|
419
|
|
420 /* Get long base name for name; name is assumed to be absolute. */
|
|
421 static int
|
|
422 get_long_basename (char * name, char * buf, int size)
|
|
423 {
|
|
424 WIN32_FIND_DATA find_data;
|
|
425 HANDLE dir_handle;
|
|
426 int len = 0;
|
|
427 #ifdef PIGSFLY
|
|
428 char *p;
|
|
429
|
|
430 /* If the last component of NAME has a wildcard character,
|
|
431 return it as the basename. */
|
|
432 p = name + strlen (name);
|
|
433 while (*p != '\\' && *p != ':' && p > name) p--;
|
|
434 if (p > name) p++;
|
|
435 if (strchr (p, '*') || strchr (p, '?'))
|
|
436 {
|
|
437 if ((len = strlen (p)) < size)
|
|
438 memcpy (buf, p, len + 1);
|
|
439 else
|
|
440 len = 0;
|
|
441 return len;
|
|
442 }
|
|
443 #endif
|
|
444
|
|
445 dir_handle = FindFirstFile (name, &find_data);
|
|
446 if (dir_handle != INVALID_HANDLE_VALUE)
|
|
447 {
|
|
448 if ((len = strlen (find_data.cFileName)) < size)
|
|
449 memcpy (buf, find_data.cFileName, len + 1);
|
|
450 else
|
|
451 len = 0;
|
|
452 FindClose (dir_handle);
|
|
453 }
|
|
454 return len;
|
|
455 }
|
|
456
|
|
457 /* Get long name for file, if possible (assumed to be absolute). */
|
|
458 BOOL
|
|
459 win32_get_long_filename (char * name, char * buf, int size)
|
|
460 {
|
|
461 char * o = buf;
|
|
462 char * p;
|
|
463 char * q;
|
|
464 char full[ MAX_PATH ];
|
|
465 int len;
|
|
466
|
|
467 len = strlen (name);
|
|
468 if (len >= MAX_PATH)
|
|
469 return FALSE;
|
|
470
|
|
471 /* Use local copy for destructive modification. */
|
|
472 memcpy (full, name, len+1);
|
|
473 unixtodos_filename (full);
|
|
474
|
|
475 /* Copy root part verbatim. */
|
|
476 len = parse_root (full, &p);
|
|
477 memcpy (o, full, len);
|
|
478 o += len;
|
|
479 size -= len;
|
|
480
|
|
481 do
|
|
482 {
|
|
483 q = p;
|
|
484 p = strchr (q, '\\');
|
|
485 if (p) *p = '\0';
|
|
486 len = get_long_basename (full, o, size);
|
|
487 if (len > 0)
|
|
488 {
|
|
489 o += len;
|
|
490 size -= len;
|
|
491 if (p != NULL)
|
|
492 {
|
|
493 *p++ = '\\';
|
|
494 if (size < 2)
|
|
495 return FALSE;
|
|
496 *o++ = '\\';
|
|
497 size--;
|
|
498 *o = '\0';
|
|
499 }
|
|
500 }
|
|
501 else
|
|
502 return FALSE;
|
|
503 }
|
|
504 while (p != NULL && *p);
|
|
505
|
|
506 return TRUE;
|
|
507 }
|
|
508
|
|
509
|
100
|
510 /* Routines that are no-ops on NT but are defined to get Emacs to compile. */
|
|
511
|
239
|
512 #if 0 /* #### We do not need those, do we? -kkm */
|
100
|
513 int
|
|
514 unrequest_sigio (void)
|
|
515 {
|
|
516 return 0;
|
|
517 }
|
|
518
|
|
519 int
|
|
520 request_sigio (void)
|
|
521 {
|
|
522 return 0;
|
|
523 }
|
239
|
524 #endif /* 0 */
|
100
|
525
|
213
|
526 #define REG_ROOT "SOFTWARE\\GNU\\XEmacs"
|
100
|
527
|
|
528 LPBYTE
|
|
529 nt_get_resource (key, lpdwtype)
|
|
530 char *key;
|
|
531 LPDWORD lpdwtype;
|
|
532 {
|
|
533 LPBYTE lpvalue;
|
|
534 HKEY hrootkey = NULL;
|
|
535 DWORD cbData;
|
|
536 BOOL ok = FALSE;
|
|
537
|
|
538 /* Check both the current user and the local machine to see if
|
|
539 we have any resources. */
|
|
540
|
|
541 if (RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
|
|
542 {
|
|
543 lpvalue = NULL;
|
|
544
|
|
545 if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS
|
|
546 && (lpvalue = (LPBYTE) xmalloc (cbData)) != NULL
|
|
547 && RegQueryValueEx (hrootkey, key, NULL, lpdwtype, lpvalue, &cbData) == ERROR_SUCCESS)
|
|
548 {
|
|
549 return (lpvalue);
|
|
550 }
|
|
551
|
|
552 if (lpvalue) xfree (lpvalue);
|
|
553
|
|
554 RegCloseKey (hrootkey);
|
|
555 }
|
|
556
|
|
557 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
|
|
558 {
|
|
559 lpvalue = NULL;
|
|
560
|
|
561 if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS &&
|
|
562 (lpvalue = (LPBYTE) xmalloc (cbData)) != NULL &&
|
|
563 RegQueryValueEx (hrootkey, key, NULL, lpdwtype, lpvalue, &cbData) == ERROR_SUCCESS)
|
|
564 {
|
|
565 return (lpvalue);
|
|
566 }
|
|
567
|
|
568 if (lpvalue) xfree (lpvalue);
|
|
569
|
|
570 RegCloseKey (hrootkey);
|
|
571 }
|
|
572
|
|
573 return (NULL);
|
|
574 }
|
|
575
|
|
576 void
|
|
577 init_environment ()
|
|
578 {
|
|
579 /* Check for environment variables and use registry if they don't exist */
|
|
580 {
|
|
581 int i;
|
|
582 LPBYTE lpval;
|
|
583 DWORD dwType;
|
|
584
|
|
585 static char * env_vars[] =
|
|
586 {
|
|
587 "HOME",
|
|
588 "PRELOAD_WINSOCK",
|
|
589 "emacs_dir",
|
|
590 "EMACSLOADPATH",
|
|
591 "SHELL",
|
209
|
592 "CMDPROXY",
|
100
|
593 "EMACSDATA",
|
|
594 "EMACSPATH",
|
|
595 "EMACSLOCKDIR",
|
209
|
596 /* We no longer set INFOPATH because Info-default-directory-list
|
|
597 is then ignored. We use a hook in winnt.el instead. */
|
|
598 /* "INFOPATH", */
|
100
|
599 "EMACSDOC",
|
|
600 "TERM",
|
|
601 };
|
|
602
|
272
|
603 for (i = 0; i < countof (env_vars); i++)
|
100
|
604 {
|
|
605 if (!getenv (env_vars[i]) &&
|
|
606 (lpval = nt_get_resource (env_vars[i], &dwType)) != NULL)
|
|
607 {
|
|
608 if (dwType == REG_EXPAND_SZ)
|
|
609 {
|
|
610 char buf1[500], buf2[500];
|
|
611
|
|
612 ExpandEnvironmentStrings ((LPSTR) lpval, buf1, 500);
|
|
613 _snprintf (buf2, 499, "%s=%s", env_vars[i], buf1);
|
|
614 putenv (strdup (buf2));
|
|
615 }
|
|
616 else if (dwType == REG_SZ)
|
|
617 {
|
|
618 char buf[500];
|
|
619
|
|
620 _snprintf (buf, 499, "%s=%s", env_vars[i], lpval);
|
|
621 putenv (strdup (buf));
|
|
622 }
|
|
623
|
|
624 xfree (lpval);
|
|
625 }
|
|
626 }
|
|
627 }
|
|
628
|
209
|
629 /* Another special case: on NT, the PATH variable is actually named
|
|
630 "Path" although cmd.exe (perhaps NT itself) arranges for
|
|
631 environment variable lookup and setting to be case insensitive.
|
|
632 However, Emacs assumes a fully case sensitive environment, so we
|
|
633 need to change "Path" to "PATH" to match the expectations of
|
|
634 various elisp packages. We do this by the sneaky method of
|
|
635 modifying the string in the C runtime environ entry.
|
|
636
|
|
637 The same applies to COMSPEC. */
|
|
638 {
|
|
639 char ** envp;
|
|
640
|
|
641 for (envp = environ; *envp; envp++)
|
|
642 if (_strnicmp (*envp, "PATH=", 5) == 0)
|
|
643 memcpy (*envp, "PATH=", 5);
|
|
644 else if (_strnicmp (*envp, "COMSPEC=", 8) == 0)
|
|
645 memcpy (*envp, "COMSPEC=", 8);
|
|
646 }
|
|
647
|
|
648 /* Remember the initial working directory for getwd, then make the
|
|
649 real wd be the location of emacs.exe to avoid conflicts when
|
|
650 renaming or deleting directories. (We also don't call chdir when
|
|
651 running subprocesses for the same reason.) */
|
|
652 if (!GetCurrentDirectory (MAXPATHLEN, startup_dir))
|
|
653 abort ();
|
|
654
|
|
655 {
|
|
656 char *p;
|
|
657 char modname[MAX_PATH];
|
|
658
|
|
659 if (!GetModuleFileName (NULL, modname, MAX_PATH))
|
|
660 abort ();
|
|
661 if ((p = strrchr (modname, '\\')) == NULL)
|
|
662 abort ();
|
|
663 *p = 0;
|
|
664
|
|
665 SetCurrentDirectory (modname);
|
|
666 }
|
|
667
|
100
|
668 init_user_info ();
|
|
669 }
|
|
670
|
|
671 /* We don't have scripts to automatically determine the system configuration
|
|
672 for Emacs before it's compiled, and we don't want to have to make the
|
|
673 user enter it, so we define EMACS_CONFIGURATION to invoke this runtime
|
|
674 routine. */
|
|
675
|
|
676 static char configuration_buffer[32];
|
|
677
|
288
|
678 const char *
|
100
|
679 get_emacs_configuration (void)
|
|
680 {
|
|
681 char *arch, *oem, *os;
|
|
682
|
|
683 /* Determine the processor type. */
|
|
684 switch (get_processor_type ())
|
|
685 {
|
|
686
|
|
687 #ifdef PROCESSOR_INTEL_386
|
|
688 case PROCESSOR_INTEL_386:
|
|
689 case PROCESSOR_INTEL_486:
|
|
690 case PROCESSOR_INTEL_PENTIUM:
|
|
691 arch = "i386";
|
|
692 break;
|
|
693 #endif
|
|
694
|
|
695 #ifdef PROCESSOR_INTEL_860
|
|
696 case PROCESSOR_INTEL_860:
|
|
697 arch = "i860";
|
|
698 break;
|
|
699 #endif
|
|
700
|
|
701 #ifdef PROCESSOR_MIPS_R2000
|
|
702 case PROCESSOR_MIPS_R2000:
|
|
703 case PROCESSOR_MIPS_R3000:
|
|
704 case PROCESSOR_MIPS_R4000:
|
|
705 arch = "mips";
|
|
706 break;
|
|
707 #endif
|
|
708
|
|
709 #ifdef PROCESSOR_ALPHA_21064
|
|
710 case PROCESSOR_ALPHA_21064:
|
|
711 arch = "alpha";
|
|
712 break;
|
|
713 #endif
|
|
714
|
|
715 default:
|
|
716 arch = "unknown";
|
|
717 break;
|
|
718 }
|
|
719
|
|
720 /* Let oem be "*" until we figure out how to decode the OEM field. */
|
|
721 oem = "*";
|
|
722
|
|
723 os = (GetVersion () & 0x80000000) ? "win95" : "nt";
|
|
724
|
|
725 sprintf (configuration_buffer, "%s-%s-%s%d.%d", arch, oem, os,
|
|
726 get_nt_major_version (), get_nt_minor_version ());
|
|
727 return configuration_buffer;
|
|
728 }
|
|
729
|
|
730 #ifndef HAVE_X_WINDOWS
|
|
731 /* X11R6 on NT provides the single parameter version of this command. */
|
|
732
|
|
733 #include <sys/timeb.h>
|
|
734
|
|
735 /* Emulate gettimeofday (Ulrich Leodolter, 1/11/95). */
|
|
736 void
|
|
737 gettimeofday (struct timeval *tv, struct timezone *tz)
|
|
738 {
|
|
739 struct _timeb tb;
|
|
740 _ftime (&tb);
|
|
741
|
|
742 tv->tv_sec = tb.time;
|
|
743 tv->tv_usec = tb.millitm * 1000L;
|
|
744 if (tz)
|
|
745 {
|
|
746 tz->tz_minuteswest = tb.timezone; /* minutes west of Greenwich */
|
|
747 tz->tz_dsttime = tb.dstflag; /* type of dst correction */
|
|
748 }
|
|
749 }
|
|
750
|
|
751 #endif /* HAVE_X_WINDOWS */
|
|
752
|
|
753 /* ------------------------------------------------------------------------- */
|
|
754 /* IO support and wrapper functions for Win32 API. */
|
|
755 /* ------------------------------------------------------------------------- */
|
|
756
|
|
757 /* Place a wrapper around the MSVC version of ctime. It returns NULL
|
|
758 on network directories, so we handle that case here.
|
|
759 (Ulrich Leodolter, 1/11/95). */
|
|
760 char *
|
|
761 sys_ctime (const time_t *t)
|
|
762 {
|
|
763 char *str = (char *) ctime (t);
|
|
764 return (str ? str : "Sun Jan 01 00:00:00 1970");
|
|
765 }
|
|
766
|
|
767 /* Emulate sleep...we could have done this with a define, but that
|
|
768 would necessitate including windows.h in the files that used it.
|
|
769 This is much easier. */
|
|
770
|
|
771 #ifndef HAVE_X_WINDOWS
|
|
772 void
|
|
773 sys_sleep (int seconds)
|
|
774 {
|
|
775 Sleep (seconds * 1000);
|
|
776 }
|
|
777 #endif
|
|
778
|
239
|
779 /* #### This is an evil dirty hack. We must get rid of it.
|
|
780 Word "munging" is not in XEmacs lexicon. - kkm */
|
|
781
|
100
|
782 /* Internal MSVC data and functions for low-level descriptor munging */
|
|
783 #if (_MSC_VER == 900)
|
|
784 extern char _osfile[];
|
|
785 #endif
|
|
786 extern int __cdecl _set_osfhnd (int fd, long h);
|
|
787 extern int __cdecl _free_osfhnd (int fd);
|
|
788
|
|
789 /* parallel array of private info on file handles */
|
|
790 filedesc fd_info [ MAXDESC ];
|
|
791
|
209
|
792 typedef struct volume_info_data {
|
|
793 struct volume_info_data * next;
|
|
794
|
|
795 /* time when info was obtained */
|
|
796 DWORD timestamp;
|
|
797
|
|
798 /* actual volume info */
|
|
799 char * root_dir;
|
100
|
800 DWORD serialnum;
|
|
801 DWORD maxcomp;
|
|
802 DWORD flags;
|
209
|
803 char * name;
|
|
804 char * type;
|
|
805 } volume_info_data;
|
|
806
|
|
807 /* Global referenced by various functions. */
|
|
808 static volume_info_data volume_info;
|
|
809
|
|
810 /* Vector to indicate which drives are local and fixed (for which cached
|
|
811 data never expires). */
|
|
812 static BOOL fixed_drives[26];
|
|
813
|
|
814 /* Consider cached volume information to be stale if older than 10s,
|
|
815 at least for non-local drives. Info for fixed drives is never stale. */
|
|
816 #define DRIVE_INDEX( c ) ( (c) <= 'Z' ? (c) - 'A' : (c) - 'a' )
|
|
817 #define VOLINFO_STILL_VALID( root_dir, info ) \
|
|
818 ( ( isalpha (root_dir[0]) && \
|
|
819 fixed_drives[ DRIVE_INDEX (root_dir[0]) ] ) \
|
|
820 || GetTickCount () - info->timestamp < 10000 )
|
|
821
|
|
822 /* Cache support functions. */
|
|
823
|
|
824 /* Simple linked list with linear search is sufficient. */
|
|
825 static volume_info_data *volume_cache = NULL;
|
|
826
|
|
827 static volume_info_data *
|
|
828 lookup_volume_info (char * root_dir)
|
|
829 {
|
|
830 volume_info_data * info;
|
|
831
|
|
832 for (info = volume_cache; info; info = info->next)
|
|
833 if (stricmp (info->root_dir, root_dir) == 0)
|
|
834 break;
|
|
835 return info;
|
|
836 }
|
|
837
|
|
838 static void
|
|
839 add_volume_info (char * root_dir, volume_info_data * info)
|
|
840 {
|
|
841 info->root_dir = xstrdup (root_dir);
|
|
842 info->next = volume_cache;
|
|
843 volume_cache = info;
|
|
844 }
|
|
845
|
|
846
|
|
847 /* Wrapper for GetVolumeInformation, which uses caching to avoid
|
|
848 performance penalty (~2ms on 486 for local drives, 7.5ms for local
|
|
849 cdrom drive, ~5-10ms or more for remote drives on LAN). */
|
|
850 volume_info_data *
|
|
851 GetCachedVolumeInformation (char * root_dir)
|
|
852 {
|
|
853 volume_info_data * info;
|
|
854 char default_root[ MAX_PATH ];
|
|
855
|
|
856 /* NULL for root_dir means use root from current directory. */
|
|
857 if (root_dir == NULL)
|
|
858 {
|
|
859 if (GetCurrentDirectory (MAX_PATH, default_root) == 0)
|
|
860 return NULL;
|
|
861 parse_root (default_root, &root_dir);
|
|
862 *root_dir = 0;
|
|
863 root_dir = default_root;
|
|
864 }
|
|
865
|
|
866 /* Local fixed drives can be cached permanently. Removable drives
|
|
867 cannot be cached permanently, since the volume name and serial
|
|
868 number (if nothing else) can change. Remote drives should be
|
|
869 treated as if they are removable, since there is no sure way to
|
|
870 tell whether they are or not. Also, the UNC association of drive
|
|
871 letters mapped to remote volumes can be changed at any time (even
|
|
872 by other processes) without notice.
|
|
873
|
|
874 As a compromise, so we can benefit from caching info for remote
|
|
875 volumes, we use a simple expiry mechanism to invalidate cache
|
|
876 entries that are more than ten seconds old. */
|
|
877
|
|
878 #if 0
|
|
879 /* No point doing this, because WNetGetConnection is even slower than
|
|
880 GetVolumeInformation, consistently taking ~50ms on a 486 (FWIW,
|
|
881 GetDriveType is about the only call of this type which does not
|
|
882 involve network access, and so is extremely quick). */
|
|
883
|
|
884 /* Map drive letter to UNC if remote. */
|
|
885 if ( isalpha( root_dir[0] ) && !fixed[ DRIVE_INDEX( root_dir[0] ) ] )
|
|
886 {
|
|
887 char remote_name[ 256 ];
|
|
888 char drive[3] = { root_dir[0], ':' };
|
|
889
|
|
890 if (WNetGetConnection (drive, remote_name, sizeof (remote_name))
|
|
891 == NO_ERROR)
|
|
892 /* do something */ ;
|
|
893 }
|
|
894 #endif
|
|
895
|
|
896 info = lookup_volume_info (root_dir);
|
|
897
|
|
898 if (info == NULL || ! VOLINFO_STILL_VALID (root_dir, info))
|
|
899 {
|
|
900 char name[ 256 ];
|
|
901 DWORD serialnum;
|
|
902 DWORD maxcomp;
|
|
903 DWORD flags;
|
|
904 char type[ 256 ];
|
|
905
|
|
906 /* Info is not cached, or is stale. */
|
|
907 if (!GetVolumeInformation (root_dir,
|
|
908 name, sizeof (name),
|
|
909 &serialnum,
|
|
910 &maxcomp,
|
|
911 &flags,
|
|
912 type, sizeof (type)))
|
|
913 return NULL;
|
|
914
|
|
915 /* Cache the volume information for future use, overwriting existing
|
|
916 entry if present. */
|
|
917 if (info == NULL)
|
|
918 {
|
|
919 info = (volume_info_data *) xmalloc (sizeof (volume_info_data));
|
|
920 add_volume_info (root_dir, info);
|
|
921 }
|
|
922 else
|
|
923 {
|
|
924 free (info->name);
|
|
925 free (info->type);
|
|
926 }
|
|
927
|
|
928 info->name = xstrdup (name);
|
|
929 info->serialnum = serialnum;
|
|
930 info->maxcomp = maxcomp;
|
|
931 info->flags = flags;
|
|
932 info->type = xstrdup (type);
|
|
933 info->timestamp = GetTickCount ();
|
|
934 }
|
|
935
|
|
936 return info;
|
|
937 }
|
100
|
938
|
|
939 /* Get information on the volume where name is held; set path pointer to
|
|
940 start of pathname in name (past UNC header\volume header if present). */
|
|
941 int
|
|
942 get_volume_info (const char * name, const char ** pPath)
|
|
943 {
|
|
944 char temp[MAX_PATH];
|
|
945 char *rootname = NULL; /* default to current volume */
|
209
|
946 volume_info_data * info;
|
100
|
947
|
|
948 if (name == NULL)
|
|
949 return FALSE;
|
|
950
|
|
951 /* find the root name of the volume if given */
|
|
952 if (isalpha (name[0]) && name[1] == ':')
|
|
953 {
|
|
954 rootname = temp;
|
|
955 temp[0] = *name++;
|
|
956 temp[1] = *name++;
|
|
957 temp[2] = '\\';
|
|
958 temp[3] = 0;
|
|
959 }
|
|
960 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
|
|
961 {
|
|
962 char *str = temp;
|
|
963 int slashes = 4;
|
|
964 rootname = temp;
|
|
965 do
|
|
966 {
|
|
967 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
|
|
968 break;
|
|
969 *str++ = *name++;
|
|
970 }
|
|
971 while ( *name );
|
|
972
|
|
973 *str++ = '\\';
|
|
974 *str = 0;
|
|
975 }
|
|
976
|
|
977 if (pPath)
|
|
978 *pPath = name;
|
|
979
|
209
|
980 info = GetCachedVolumeInformation (rootname);
|
|
981 if (info != NULL)
|
100
|
982 {
|
209
|
983 /* Set global referenced by other functions. */
|
|
984 volume_info = *info;
|
100
|
985 return TRUE;
|
|
986 }
|
|
987 return FALSE;
|
|
988 }
|
|
989
|
|
990 /* Determine if volume is FAT format (ie. only supports short 8.3
|
|
991 names); also set path pointer to start of pathname in name. */
|
|
992 int
|
|
993 is_fat_volume (const char * name, const char ** pPath)
|
|
994 {
|
|
995 if (get_volume_info (name, pPath))
|
|
996 return (volume_info.maxcomp == 12);
|
|
997 return FALSE;
|
|
998 }
|
|
999
|
|
1000 /* Map filename to a legal 8.3 name if necessary. */
|
|
1001 const char *
|
|
1002 map_win32_filename (const char * name, const char ** pPath)
|
|
1003 {
|
|
1004 static char shortname[MAX_PATH];
|
|
1005 char * str = shortname;
|
|
1006 char c;
|
|
1007 char * path;
|
209
|
1008 const char * save_name = name;
|
100
|
1009
|
|
1010 if (is_fat_volume (name, &path)) /* truncate to 8.3 */
|
|
1011 {
|
203
|
1012 REGISTER int left = 8; /* maximum number of chars in part */
|
|
1013 REGISTER int extn = 0; /* extension added? */
|
|
1014 REGISTER int dots = 2; /* maximum number of dots allowed */
|
100
|
1015
|
|
1016 while (name < path)
|
|
1017 *str++ = *name++; /* skip past UNC header */
|
|
1018
|
|
1019 while ((c = *name++))
|
|
1020 {
|
|
1021 switch ( c )
|
|
1022 {
|
|
1023 case '\\':
|
|
1024 case '/':
|
|
1025 *str++ = '\\';
|
|
1026 extn = 0; /* reset extension flags */
|
|
1027 dots = 2; /* max 2 dots */
|
|
1028 left = 8; /* max length 8 for main part */
|
|
1029 break;
|
|
1030 case ':':
|
|
1031 *str++ = ':';
|
|
1032 extn = 0; /* reset extension flags */
|
|
1033 dots = 2; /* max 2 dots */
|
|
1034 left = 8; /* max length 8 for main part */
|
|
1035 break;
|
|
1036 case '.':
|
|
1037 if ( dots )
|
|
1038 {
|
|
1039 /* Convert path components of the form .xxx to _xxx,
|
|
1040 but leave . and .. as they are. This allows .emacs
|
|
1041 to be read as _emacs, for example. */
|
|
1042
|
|
1043 if (! *name ||
|
|
1044 *name == '.' ||
|
|
1045 IS_DIRECTORY_SEP (*name))
|
|
1046 {
|
|
1047 *str++ = '.';
|
|
1048 dots--;
|
|
1049 }
|
|
1050 else
|
|
1051 {
|
|
1052 *str++ = '_';
|
|
1053 left--;
|
|
1054 dots = 0;
|
|
1055 }
|
|
1056 }
|
|
1057 else if ( !extn )
|
|
1058 {
|
|
1059 *str++ = '.';
|
|
1060 extn = 1; /* we've got an extension */
|
|
1061 left = 3; /* 3 chars in extension */
|
|
1062 }
|
|
1063 else
|
|
1064 {
|
|
1065 /* any embedded dots after the first are converted to _ */
|
|
1066 *str++ = '_';
|
|
1067 }
|
|
1068 break;
|
|
1069 case '~':
|
|
1070 case '#': /* don't lose these, they're important */
|
|
1071 if ( ! left )
|
|
1072 str[-1] = c; /* replace last character of part */
|
|
1073 /* FALLTHRU */
|
|
1074 default:
|
|
1075 if ( left )
|
|
1076 {
|
|
1077 *str++ = tolower (c); /* map to lower case (looks nicer) */
|
|
1078 left--;
|
|
1079 dots = 0; /* started a path component */
|
|
1080 }
|
|
1081 break;
|
|
1082 }
|
|
1083 }
|
|
1084 *str = '\0';
|
|
1085 }
|
|
1086 else
|
|
1087 {
|
|
1088 strcpy (shortname, name);
|
|
1089 unixtodos_filename (shortname);
|
|
1090 }
|
|
1091
|
|
1092 if (pPath)
|
209
|
1093 *pPath = shortname + (path - save_name);
|
100
|
1094
|
|
1095 return shortname;
|
|
1096 }
|
|
1097
|
|
1098
|
209
|
1099 /* Emulate the Unix directory procedures opendir, closedir,
|
|
1100 and readdir. We can't use the procedures supplied in sysdep.c,
|
|
1101 so we provide them here. */
|
|
1102
|
|
1103 struct direct dir_static; /* simulated directory contents */
|
|
1104 static HANDLE dir_find_handle = INVALID_HANDLE_VALUE;
|
|
1105 static int dir_is_fat;
|
|
1106 static char dir_pathname[MAXPATHLEN+1];
|
|
1107 static WIN32_FIND_DATA dir_find_data;
|
|
1108
|
|
1109 DIR *
|
288
|
1110 opendir (const char *filename)
|
209
|
1111 {
|
|
1112 DIR *dirp;
|
|
1113
|
|
1114 /* Opening is done by FindFirstFile. However, a read is inherent to
|
|
1115 this operation, so we defer the open until read time. */
|
|
1116
|
|
1117 if (!(dirp = (DIR *) xmalloc (sizeof (DIR))))
|
|
1118 return NULL;
|
|
1119 if (dir_find_handle != INVALID_HANDLE_VALUE)
|
|
1120 return NULL;
|
|
1121
|
|
1122 dirp->dd_fd = 0;
|
|
1123 dirp->dd_loc = 0;
|
|
1124 dirp->dd_size = 0;
|
|
1125
|
|
1126 strncpy (dir_pathname, map_win32_filename (filename, NULL), MAXPATHLEN);
|
|
1127 dir_pathname[MAXPATHLEN] = '\0';
|
|
1128 dir_is_fat = is_fat_volume (filename, NULL);
|
|
1129
|
|
1130 return dirp;
|
|
1131 }
|
|
1132
|
|
1133 void
|
|
1134 closedir (DIR *dirp)
|
|
1135 {
|
|
1136 /* If we have a find-handle open, close it. */
|
|
1137 if (dir_find_handle != INVALID_HANDLE_VALUE)
|
|
1138 {
|
|
1139 FindClose (dir_find_handle);
|
|
1140 dir_find_handle = INVALID_HANDLE_VALUE;
|
|
1141 }
|
|
1142 xfree ((char *) dirp);
|
|
1143 }
|
|
1144
|
|
1145 struct direct *
|
|
1146 readdir (DIR *dirp)
|
|
1147 {
|
|
1148 /* If we aren't dir_finding, do a find-first, otherwise do a find-next. */
|
|
1149 if (dir_find_handle == INVALID_HANDLE_VALUE)
|
|
1150 {
|
|
1151 char filename[MAXNAMLEN + 3];
|
|
1152 int ln;
|
|
1153
|
|
1154 strcpy (filename, dir_pathname);
|
|
1155 ln = strlen (filename) - 1;
|
|
1156 if (!IS_DIRECTORY_SEP (filename[ln]))
|
|
1157 strcat (filename, "\\");
|
|
1158 strcat (filename, "*");
|
|
1159
|
|
1160 dir_find_handle = FindFirstFile (filename, &dir_find_data);
|
|
1161
|
|
1162 if (dir_find_handle == INVALID_HANDLE_VALUE)
|
|
1163 return NULL;
|
|
1164 }
|
|
1165 else
|
|
1166 {
|
|
1167 if (!FindNextFile (dir_find_handle, &dir_find_data))
|
|
1168 return NULL;
|
|
1169 }
|
|
1170
|
|
1171 /* Emacs never uses this value, so don't bother making it match
|
|
1172 value returned by stat(). */
|
|
1173 dir_static.d_ino = 1;
|
|
1174
|
|
1175 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3 +
|
|
1176 dir_static.d_namlen - dir_static.d_namlen % 4;
|
|
1177
|
|
1178 dir_static.d_namlen = strlen (dir_find_data.cFileName);
|
|
1179 strcpy (dir_static.d_name, dir_find_data.cFileName);
|
|
1180 if (dir_is_fat)
|
|
1181 _strlwr (dir_static.d_name);
|
263
|
1182 else if (!NILP (Vmswindows_downcase_file_names))
|
209
|
1183 {
|
|
1184 REGISTER char *p;
|
|
1185 for (p = dir_static.d_name; *p; p++)
|
|
1186 if (*p >= 'a' && *p <= 'z')
|
|
1187 break;
|
|
1188 if (!*p)
|
|
1189 _strlwr (dir_static.d_name);
|
|
1190 }
|
|
1191
|
|
1192 return &dir_static;
|
|
1193 }
|
|
1194
|
290
|
1195 #if 0
|
|
1196 /* #### Have to check if all that sad story about '95 is true - kkm */
|
100
|
1197 int
|
|
1198 sys_rename (const char * oldname, const char * newname)
|
|
1199 {
|
|
1200 char temp[MAX_PATH];
|
|
1201 DWORD attr;
|
|
1202
|
|
1203 /* MoveFile on Win95 doesn't correctly change the short file name
|
|
1204 alias in a number of circumstances (it is not easy to predict when
|
|
1205 just by looking at oldname and newname, unfortunately). In these
|
|
1206 cases, renaming through a temporary name avoids the problem.
|
|
1207
|
|
1208 A second problem on Win95 is that renaming through a temp name when
|
|
1209 newname is uppercase fails (the final long name ends up in
|
|
1210 lowercase, although the short alias might be uppercase) UNLESS the
|
|
1211 long temp name is not 8.3.
|
|
1212
|
|
1213 So, on Win95 we always rename through a temp name, and we make sure
|
|
1214 the temp name has a long extension to ensure correct renaming. */
|
|
1215
|
|
1216 strcpy (temp, map_win32_filename (oldname, NULL));
|
|
1217
|
|
1218 if (GetVersion () & 0x80000000)
|
|
1219 {
|
|
1220 char * p;
|
|
1221
|
|
1222 if (p = strrchr (temp, '\\'))
|
|
1223 p++;
|
|
1224 else
|
|
1225 p = temp;
|
|
1226 /* Force temp name to require a manufactured 8.3 alias - this
|
|
1227 seems to make the second rename work properly. */
|
209
|
1228 strcpy (p, "_rename_temp.XXXXXX");
|
|
1229 sys_mktemp (temp);
|
100
|
1230 if (rename (map_win32_filename (oldname, NULL), temp) < 0)
|
|
1231 return -1;
|
|
1232 }
|
|
1233
|
|
1234 /* Emulate Unix behaviour - newname is deleted if it already exists
|
|
1235 (at least if it is a file; don't do this for directories).
|
|
1236 However, don't do this if we are just changing the case of the file
|
|
1237 name - we will end up deleting the file we are trying to rename! */
|
|
1238 newname = map_win32_filename (newname, NULL);
|
209
|
1239
|
|
1240 /* TODO: Use GetInformationByHandle (on NT) to ensure newname and temp
|
|
1241 do not refer to the same file, eg. through share aliases. */
|
100
|
1242 if (stricmp (newname, temp) != 0
|
|
1243 && (attr = GetFileAttributes (newname)) != -1
|
|
1244 && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
|
1245 {
|
|
1246 _chmod (newname, 0666);
|
|
1247 _unlink (newname);
|
|
1248 }
|
|
1249
|
|
1250 return rename (temp, newname);
|
|
1251 }
|
290
|
1252 #endif /* 0 */
|
100
|
1253
|
|
1254 static FILETIME utc_base_ft;
|
|
1255 static long double utc_base;
|
|
1256 static int init = 0;
|
|
1257
|
263
|
1258 time_t
|
100
|
1259 convert_time (FILETIME ft)
|
|
1260 {
|
|
1261 long double ret;
|
|
1262
|
|
1263 if (!init)
|
|
1264 {
|
|
1265 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
|
|
1266 SYSTEMTIME st;
|
|
1267
|
|
1268 st.wYear = 1970;
|
|
1269 st.wMonth = 1;
|
|
1270 st.wDay = 1;
|
|
1271 st.wHour = 0;
|
|
1272 st.wMinute = 0;
|
|
1273 st.wSecond = 0;
|
|
1274 st.wMilliseconds = 0;
|
|
1275
|
|
1276 SystemTimeToFileTime (&st, &utc_base_ft);
|
|
1277 utc_base = (long double) utc_base_ft.dwHighDateTime
|
|
1278 * 4096 * 1024 * 1024 + utc_base_ft.dwLowDateTime;
|
|
1279 init = 1;
|
|
1280 }
|
|
1281
|
|
1282 if (CompareFileTime (&ft, &utc_base_ft) < 0)
|
|
1283 return 0;
|
|
1284
|
|
1285 ret = (long double) ft.dwHighDateTime * 4096 * 1024 * 1024 + ft.dwLowDateTime;
|
|
1286 ret -= utc_base;
|
|
1287 return (time_t) (ret * 1e-7);
|
|
1288 }
|
|
1289
|
|
1290 #if 0
|
|
1291 /* in case we ever have need of this */
|
|
1292 void
|
|
1293 convert_from_time_t (time_t time, FILETIME * pft)
|
|
1294 {
|
|
1295 long double tmp;
|
|
1296
|
|
1297 if (!init)
|
|
1298 {
|
|
1299 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
|
|
1300 SYSTEMTIME st;
|
|
1301
|
|
1302 st.wYear = 1970;
|
|
1303 st.wMonth = 1;
|
|
1304 st.wDay = 1;
|
|
1305 st.wHour = 0;
|
|
1306 st.wMinute = 0;
|
|
1307 st.wSecond = 0;
|
|
1308 st.wMilliseconds = 0;
|
|
1309
|
|
1310 SystemTimeToFileTime (&st, &utc_base_ft);
|
|
1311 utc_base = (long double) utc_base_ft.dwHighDateTime
|
|
1312 * 4096 * 1024 * 1024 + utc_base_ft.dwLowDateTime;
|
|
1313 init = 1;
|
|
1314 }
|
|
1315
|
|
1316 /* time in 100ns units since 1-Jan-1601 */
|
|
1317 tmp = (long double) time * 1e7 + utc_base;
|
|
1318 pft->dwHighDateTime = (DWORD) (tmp / (4096.0 * 1024 * 1024));
|
|
1319 pft->dwLowDateTime = (DWORD) (tmp - pft->dwHighDateTime);
|
|
1320 }
|
|
1321 #endif
|
|
1322
|
209
|
1323 #if 0
|
|
1324 /* No reason to keep this; faking inode values either by hashing or even
|
|
1325 using the file index from GetInformationByHandle, is not perfect and
|
|
1326 so by default Emacs doesn't use the inode values on Windows.
|
|
1327 Instead, we now determine file-truename correctly (except for
|
|
1328 possible drive aliasing etc). */
|
|
1329
|
|
1330 /* Modified version of "PJW" algorithm (see the "Dragon" compiler book). */
|
100
|
1331 static unsigned
|
209
|
1332 hashval (const unsigned char * str)
|
100
|
1333 {
|
|
1334 unsigned h = 0;
|
|
1335 while (*str)
|
|
1336 {
|
|
1337 h = (h << 4) + *str++;
|
209
|
1338 h ^= (h >> 28);
|
100
|
1339 }
|
|
1340 return h;
|
|
1341 }
|
|
1342
|
|
1343 /* Return the hash value of the canonical pathname, excluding the
|
|
1344 drive/UNC header, to get a hopefully unique inode number. */
|
209
|
1345 static DWORD
|
100
|
1346 generate_inode_val (const char * name)
|
|
1347 {
|
|
1348 char fullname[ MAX_PATH ];
|
|
1349 char * p;
|
|
1350 unsigned hash;
|
|
1351
|
209
|
1352 /* Get the truly canonical filename, if it exists. (Note: this
|
|
1353 doesn't resolve aliasing due to subst commands, or recognise hard
|
|
1354 links. */
|
|
1355 if (!win32_get_long_filename ((char *)name, fullname, MAX_PATH))
|
|
1356 abort ();
|
|
1357
|
|
1358 parse_root (fullname, &p);
|
100
|
1359 /* Normal Win32 filesystems are still case insensitive. */
|
|
1360 _strlwr (p);
|
209
|
1361 return hashval (p);
|
100
|
1362 }
|
|
1363
|
209
|
1364 #endif
|
|
1365
|
100
|
1366 /* MSVC stat function can't cope with UNC names and has other bugs, so
|
|
1367 replace it with our own. This also allows us to calculate consistent
|
|
1368 inode values without hacks in the main Emacs code. */
|
|
1369 int
|
|
1370 stat (const char * path, struct stat * buf)
|
|
1371 {
|
|
1372 char * name;
|
|
1373 WIN32_FIND_DATA wfd;
|
|
1374 HANDLE fh;
|
209
|
1375 DWORD fake_inode;
|
100
|
1376 int permission;
|
|
1377 int len;
|
|
1378 int rootdir = FALSE;
|
|
1379
|
|
1380 if (path == NULL || buf == NULL)
|
|
1381 {
|
|
1382 errno = EFAULT;
|
|
1383 return -1;
|
|
1384 }
|
|
1385
|
|
1386 name = (char *) map_win32_filename (path, &path);
|
|
1387 /* must be valid filename, no wild cards */
|
|
1388 if (strchr (name, '*') || strchr (name, '?'))
|
|
1389 {
|
|
1390 errno = ENOENT;
|
|
1391 return -1;
|
|
1392 }
|
|
1393
|
|
1394 /* Remove trailing directory separator, unless name is the root
|
|
1395 directory of a drive or UNC volume in which case ensure there
|
|
1396 is a trailing separator. */
|
|
1397 len = strlen (name);
|
|
1398 rootdir = (path >= name + len - 1
|
|
1399 && (IS_DIRECTORY_SEP (*path) || *path == 0));
|
|
1400 name = strcpy (alloca (len + 2), name);
|
|
1401
|
|
1402 if (rootdir)
|
|
1403 {
|
|
1404 if (!IS_DIRECTORY_SEP (name[len-1]))
|
|
1405 strcat (name, "\\");
|
|
1406 if (GetDriveType (name) < 2)
|
|
1407 {
|
|
1408 errno = ENOENT;
|
|
1409 return -1;
|
|
1410 }
|
|
1411 memset (&wfd, 0, sizeof (wfd));
|
|
1412 wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
|
|
1413 wfd.ftCreationTime = utc_base_ft;
|
|
1414 wfd.ftLastAccessTime = utc_base_ft;
|
|
1415 wfd.ftLastWriteTime = utc_base_ft;
|
|
1416 strcpy (wfd.cFileName, name);
|
|
1417 }
|
|
1418 else
|
|
1419 {
|
|
1420 if (IS_DIRECTORY_SEP (name[len-1]))
|
|
1421 name[len - 1] = 0;
|
209
|
1422
|
|
1423 /* (This is hacky, but helps when doing file completions on
|
|
1424 network drives.) Optimize by using information available from
|
|
1425 active readdir if possible. */
|
|
1426 if (dir_find_handle != INVALID_HANDLE_VALUE &&
|
|
1427 (len = strlen (dir_pathname)),
|
|
1428 strnicmp (name, dir_pathname, len) == 0 &&
|
|
1429 IS_DIRECTORY_SEP (name[len]) &&
|
|
1430 stricmp (name + len + 1, dir_static.d_name) == 0)
|
|
1431 {
|
|
1432 /* This was the last entry returned by readdir. */
|
|
1433 wfd = dir_find_data;
|
|
1434 }
|
|
1435 else
|
|
1436 {
|
100
|
1437 fh = FindFirstFile (name, &wfd);
|
|
1438 if (fh == INVALID_HANDLE_VALUE)
|
|
1439 {
|
|
1440 errno = ENOENT;
|
|
1441 return -1;
|
|
1442 }
|
|
1443 FindClose (fh);
|
|
1444 }
|
209
|
1445 }
|
100
|
1446
|
|
1447 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
1448 {
|
|
1449 buf->st_mode = _S_IFDIR;
|
|
1450 buf->st_nlink = 2; /* doesn't really matter */
|
209
|
1451 fake_inode = 0; /* this doesn't either I think */
|
100
|
1452 }
|
263
|
1453 else if (!NILP (Vmswindows_get_true_file_attributes))
|
100
|
1454 {
|
|
1455 /* This is more accurate in terms of gettting the correct number
|
|
1456 of links, but is quite slow (it is noticable when Emacs is
|
|
1457 making a list of file name completions). */
|
|
1458 BY_HANDLE_FILE_INFORMATION info;
|
|
1459
|
209
|
1460 /* No access rights required to get info. */
|
211
|
1461 fh = CreateFile (name, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
|
|
1462 OPEN_EXISTING, 0, NULL);
|
100
|
1463
|
|
1464 if (GetFileInformationByHandle (fh, &info))
|
|
1465 {
|
|
1466 switch (GetFileType (fh))
|
|
1467 {
|
|
1468 case FILE_TYPE_DISK:
|
|
1469 buf->st_mode = _S_IFREG;
|
|
1470 break;
|
|
1471 case FILE_TYPE_PIPE:
|
|
1472 buf->st_mode = _S_IFIFO;
|
|
1473 break;
|
|
1474 case FILE_TYPE_CHAR:
|
|
1475 case FILE_TYPE_UNKNOWN:
|
|
1476 default:
|
|
1477 buf->st_mode = _S_IFCHR;
|
|
1478 }
|
288
|
1479 buf->st_nlink = (short) info.nNumberOfLinks;
|
209
|
1480 /* Might as well use file index to fake inode values, but this
|
|
1481 is not guaranteed to be unique unless we keep a handle open
|
|
1482 all the time (even then there are situations where it is
|
|
1483 not unique). Reputedly, there are at most 48 bits of info
|
|
1484 (on NTFS, presumably less on FAT). */
|
|
1485 fake_inode = info.nFileIndexLow ^ info.nFileIndexHigh;
|
100
|
1486 CloseHandle (fh);
|
|
1487 }
|
|
1488 else
|
|
1489 {
|
|
1490 errno = EACCES;
|
|
1491 return -1;
|
|
1492 }
|
209
|
1493 }
|
|
1494 else
|
|
1495 {
|
|
1496 /* Don't bother to make this information more accurate. */
|
100
|
1497 buf->st_mode = _S_IFREG;
|
|
1498 buf->st_nlink = 1;
|
209
|
1499 fake_inode = 0;
|
|
1500 }
|
|
1501
|
|
1502 #if 0
|
|
1503 /* Not sure if there is any point in this. */
|
|
1504 if (!NILP (Vwin32_generate_fake_inodes))
|
|
1505 fake_inode = generate_inode_val (name);
|
|
1506 else if (fake_inode == 0)
|
|
1507 {
|
|
1508 /* For want of something better, try to make everything unique. */
|
|
1509 static DWORD gen_num = 0;
|
|
1510 fake_inode = ++gen_num;
|
|
1511 }
|
100
|
1512 #endif
|
209
|
1513
|
288
|
1514 /* #### MSVC defines _ino_t to be short; other libc's might not. */
|
|
1515 buf->st_ino = (unsigned short) (fake_inode ^ (fake_inode >> 16));
|
100
|
1516
|
|
1517 /* consider files to belong to current user */
|
|
1518 buf->st_uid = the_passwd.pw_uid;
|
|
1519 buf->st_gid = the_passwd.pw_gid;
|
|
1520
|
|
1521 /* volume_info is set indirectly by map_win32_filename */
|
|
1522 buf->st_dev = volume_info.serialnum;
|
|
1523 buf->st_rdev = volume_info.serialnum;
|
|
1524
|
|
1525
|
|
1526 buf->st_size = wfd.nFileSizeLow;
|
|
1527
|
|
1528 /* Convert timestamps to Unix format. */
|
|
1529 buf->st_mtime = convert_time (wfd.ftLastWriteTime);
|
|
1530 buf->st_atime = convert_time (wfd.ftLastAccessTime);
|
|
1531 if (buf->st_atime == 0) buf->st_atime = buf->st_mtime;
|
|
1532 buf->st_ctime = convert_time (wfd.ftCreationTime);
|
|
1533 if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime;
|
|
1534
|
|
1535 /* determine rwx permissions */
|
|
1536 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
|
|
1537 permission = _S_IREAD;
|
|
1538 else
|
|
1539 permission = _S_IREAD | _S_IWRITE;
|
|
1540
|
|
1541 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
1542 permission |= _S_IEXEC;
|
|
1543 else
|
|
1544 {
|
|
1545 char * p = strrchr (name, '.');
|
|
1546 if (p != NULL &&
|
|
1547 (stricmp (p, ".exe") == 0 ||
|
|
1548 stricmp (p, ".com") == 0 ||
|
|
1549 stricmp (p, ".bat") == 0 ||
|
|
1550 stricmp (p, ".cmd") == 0))
|
|
1551 permission |= _S_IEXEC;
|
|
1552 }
|
|
1553
|
|
1554 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
|
|
1555
|
|
1556 return 0;
|
|
1557 }
|
|
1558
|
|
1559 /* From callproc.c */
|
|
1560 extern Lisp_Object Vbinary_process_input;
|
|
1561 extern Lisp_Object Vbinary_process_output;
|
|
1562
|
|
1563 /* Unix pipe() has only one arg */
|
|
1564 int
|
|
1565 sys_pipe (int * phandles)
|
|
1566 {
|
|
1567 int rc;
|
|
1568 unsigned flags;
|
|
1569
|
209
|
1570 /* make pipe handles non-inheritable; when we spawn a child, we
|
|
1571 replace the relevant handle with an inheritable one. Also put
|
|
1572 pipes into binary mode; we will do text mode translation ourselves
|
|
1573 if required. */
|
|
1574 rc = _pipe (phandles, 0, _O_NOINHERIT | _O_BINARY);
|
100
|
1575
|
|
1576 if (rc == 0)
|
|
1577 {
|
|
1578 flags = FILE_PIPE | FILE_READ;
|
|
1579 if (!NILP (Vbinary_process_output))
|
|
1580 flags |= FILE_BINARY;
|
|
1581 fd_info[phandles[0]].flags = flags;
|
|
1582
|
|
1583 flags = FILE_PIPE | FILE_WRITE;
|
|
1584 if (!NILP (Vbinary_process_input))
|
|
1585 flags |= FILE_BINARY;
|
|
1586 fd_info[phandles[1]].flags = flags;
|
|
1587 }
|
|
1588
|
|
1589 return rc;
|
|
1590 }
|
|
1591
|
|
1592 /* From ntproc.c */
|
|
1593 extern Lisp_Object Vwin32_pipe_read_delay;
|
|
1594
|
|
1595 /* Function to do blocking read of one byte, needed to implement
|
|
1596 select. It is only allowed on sockets and pipes. */
|
|
1597 int
|
|
1598 _sys_read_ahead (int fd)
|
|
1599 {
|
|
1600 child_process * cp;
|
|
1601 int rc;
|
|
1602
|
|
1603 if (fd < 0 || fd >= MAXDESC)
|
|
1604 return STATUS_READ_ERROR;
|
|
1605
|
|
1606 cp = fd_info[fd].cp;
|
|
1607
|
|
1608 if (cp == NULL || cp->fd != fd || cp->status != STATUS_READ_READY)
|
|
1609 return STATUS_READ_ERROR;
|
|
1610
|
|
1611 if ((fd_info[fd].flags & (FILE_PIPE | FILE_SOCKET)) == 0
|
|
1612 || (fd_info[fd].flags & FILE_READ) == 0)
|
|
1613 {
|
288
|
1614 /* fd is not a pipe or socket */
|
100
|
1615 abort ();
|
|
1616 }
|
|
1617
|
|
1618 cp->status = STATUS_READ_IN_PROGRESS;
|
|
1619
|
|
1620 if (fd_info[fd].flags & FILE_PIPE)
|
|
1621 {
|
|
1622 rc = _read (fd, &cp->chr, sizeof (char));
|
|
1623
|
|
1624 /* Give subprocess time to buffer some more output for us before
|
|
1625 reporting that input is available; we need this because Win95
|
|
1626 connects DOS programs to pipes by making the pipe appear to be
|
|
1627 the normal console stdout - as a result most DOS programs will
|
|
1628 write to stdout without buffering, ie. one character at a
|
|
1629 time. Even some Win32 programs do this - "dir" in a command
|
|
1630 shell on NT is very slow if we don't do this. */
|
|
1631 if (rc > 0)
|
|
1632 {
|
|
1633 int wait = XINT (Vwin32_pipe_read_delay);
|
|
1634
|
|
1635 if (wait > 0)
|
|
1636 Sleep (wait);
|
|
1637 else if (wait < 0)
|
|
1638 while (++wait <= 0)
|
|
1639 /* Yield remainder of our time slice, effectively giving a
|
|
1640 temporary priority boost to the child process. */
|
|
1641 Sleep (0);
|
|
1642 }
|
|
1643 }
|
282
|
1644
|
100
|
1645 if (rc == sizeof (char))
|
|
1646 cp->status = STATUS_READ_SUCCEEDED;
|
|
1647 else
|
|
1648 cp->status = STATUS_READ_FAILED;
|
|
1649
|
|
1650 return cp->status;
|
|
1651 }
|
|
1652
|
|
1653 void
|
288
|
1654 term_ntproc (int unused)
|
100
|
1655 {
|
|
1656 }
|
|
1657
|
|
1658 void
|
|
1659 init_ntproc ()
|
|
1660 {
|
|
1661 /* Initial preparation for subprocess support: replace our standard
|
|
1662 handles with non-inheritable versions. */
|
|
1663 {
|
|
1664 HANDLE parent;
|
|
1665 HANDLE stdin_save = INVALID_HANDLE_VALUE;
|
|
1666 HANDLE stdout_save = INVALID_HANDLE_VALUE;
|
|
1667 HANDLE stderr_save = INVALID_HANDLE_VALUE;
|
|
1668
|
|
1669 parent = GetCurrentProcess ();
|
|
1670
|
|
1671 /* ignore errors when duplicating and closing; typically the
|
|
1672 handles will be invalid when running as a gui program. */
|
|
1673 DuplicateHandle (parent,
|
|
1674 GetStdHandle (STD_INPUT_HANDLE),
|
|
1675 parent,
|
|
1676 &stdin_save,
|
|
1677 0,
|
|
1678 FALSE,
|
|
1679 DUPLICATE_SAME_ACCESS);
|
|
1680
|
|
1681 DuplicateHandle (parent,
|
|
1682 GetStdHandle (STD_OUTPUT_HANDLE),
|
|
1683 parent,
|
|
1684 &stdout_save,
|
|
1685 0,
|
|
1686 FALSE,
|
|
1687 DUPLICATE_SAME_ACCESS);
|
|
1688
|
|
1689 DuplicateHandle (parent,
|
|
1690 GetStdHandle (STD_ERROR_HANDLE),
|
|
1691 parent,
|
|
1692 &stderr_save,
|
|
1693 0,
|
|
1694 FALSE,
|
|
1695 DUPLICATE_SAME_ACCESS);
|
|
1696
|
|
1697 fclose (stdin);
|
|
1698 fclose (stdout);
|
|
1699 fclose (stderr);
|
|
1700
|
|
1701 if (stdin_save != INVALID_HANDLE_VALUE)
|
|
1702 _open_osfhandle ((long) stdin_save, O_TEXT);
|
|
1703 else
|
|
1704 _open ("nul", O_TEXT | O_NOINHERIT | O_RDONLY);
|
209
|
1705 _fdopen (0, "r");
|
100
|
1706
|
|
1707 if (stdout_save != INVALID_HANDLE_VALUE)
|
|
1708 _open_osfhandle ((long) stdout_save, O_TEXT);
|
|
1709 else
|
|
1710 _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY);
|
209
|
1711 _fdopen (1, "w");
|
100
|
1712
|
|
1713 if (stderr_save != INVALID_HANDLE_VALUE)
|
|
1714 _open_osfhandle ((long) stderr_save, O_TEXT);
|
|
1715 else
|
|
1716 _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY);
|
209
|
1717 _fdopen (2, "w");
|
100
|
1718 }
|
|
1719
|
|
1720 /* unfortunately, atexit depends on implementation of malloc */
|
|
1721 /* atexit (term_ntproc); */
|
|
1722 signal (SIGABRT, term_ntproc);
|
209
|
1723
|
|
1724 /* determine which drives are fixed, for GetCachedVolumeInformation */
|
|
1725 {
|
|
1726 /* GetDriveType must have trailing backslash. */
|
|
1727 char drive[] = "A:\\";
|
|
1728
|
|
1729 /* Loop over all possible drive letters */
|
|
1730 while ( *drive <= 'Z' )
|
|
1731 {
|
|
1732 /* Record if this drive letter refers to a fixed drive. */
|
|
1733 fixed_drives[ DRIVE_INDEX (*drive) ] =
|
|
1734 (GetDriveType (drive) == DRIVE_FIXED);
|
|
1735
|
|
1736 (*drive)++;
|
|
1737 }
|
|
1738 }
|
100
|
1739 }
|
|
1740 #ifndef HAVE_TTY
|
|
1741 Lisp_Object Vstdio_str;
|
|
1742
|
|
1743 Lisp_Object
|
|
1744 tty_semi_canonicalize_console_connection (Lisp_Object connection,
|
|
1745 Error_behavior errb)
|
|
1746 {
|
|
1747 return Vstdio_str;
|
|
1748 }
|
|
1749
|
|
1750 Lisp_Object
|
|
1751 tty_canonicalize_console_connection (Lisp_Object connection,
|
|
1752 Error_behavior errb)
|
|
1753 {
|
|
1754 return Vstdio_str;
|
|
1755 }
|
|
1756
|
|
1757 Lisp_Object
|
|
1758 tty_semi_canonicalize_device_connection (Lisp_Object connection,
|
|
1759 Error_behavior errb)
|
|
1760 {
|
|
1761 return Vstdio_str;
|
|
1762 }
|
|
1763
|
|
1764 Lisp_Object
|
|
1765 tty_canonicalize_device_connection (Lisp_Object connection,
|
|
1766 Error_behavior errb)
|
|
1767 {
|
|
1768 return Vstdio_str;
|
|
1769 }
|
|
1770 #endif
|
|
1771
|
223
|
1772 /*--------------------------------------------------------------------*/
|
|
1773 /* Signal support */
|
|
1774 /*--------------------------------------------------------------------*/
|
|
1775
|
|
1776 /* We need MS-defined signal and raise here */
|
|
1777 #undef signal
|
|
1778 #undef raise
|
|
1779
|
|
1780 #define sigmask(nsig) (1U << nsig)
|
|
1781
|
|
1782 /* We can support as many signals as fit into word */
|
|
1783 #define SIG_MAX 32
|
|
1784
|
|
1785 /* Signal handlers. Initial value = 0 = SIG_DFL */
|
|
1786 static void (__cdecl *signal_handlers[SIG_MAX])(int) = {0};
|
|
1787
|
|
1788 /* Signal block mask: bit set to 1 means blocked */
|
|
1789 unsigned signal_block_mask = 0;
|
|
1790
|
|
1791 /* Signal pending mask: bit set to 1 means sig is pending */
|
|
1792 unsigned signal_pending_mask = 0;
|
|
1793
|
|
1794 msw_sighandler msw_sigset (int nsig, msw_sighandler handler)
|
|
1795 {
|
|
1796 /* We delegate some signals to the system function */
|
|
1797 if (nsig == SIGFPE || nsig == SIGABRT || nsig == SIGINT)
|
288
|
1798 return signal (nsig, handler);
|
223
|
1799
|
|
1800 if (nsig < 0 || nsig > SIG_MAX)
|
|
1801 {
|
|
1802 errno = EINVAL;
|
288
|
1803 return NULL;
|
223
|
1804 }
|
|
1805
|
|
1806 /* Store handler ptr */
|
288
|
1807 {
|
|
1808 msw_sighandler old_handler = signal_handlers[nsig];
|
|
1809 signal_handlers[nsig] = handler;
|
|
1810 return old_handler;
|
|
1811 }
|
223
|
1812 }
|
|
1813
|
|
1814 int msw_sighold (int nsig)
|
|
1815 {
|
|
1816 if (nsig < 0 || nsig > SIG_MAX)
|
|
1817 return errno = EINVAL;
|
|
1818
|
|
1819 signal_block_mask |= sigmask(nsig);
|
|
1820 return 0;
|
|
1821 }
|
|
1822
|
|
1823 int msw_sigrelse (int nsig)
|
|
1824 {
|
|
1825 if (nsig < 0 || nsig > SIG_MAX)
|
|
1826 return errno = EINVAL;
|
|
1827
|
|
1828 signal_block_mask &= ~sigmask(nsig);
|
|
1829
|
|
1830 if (signal_pending_mask & sigmask(nsig))
|
|
1831 msw_raise (nsig);
|
|
1832
|
|
1833 return 0;
|
|
1834 }
|
|
1835
|
|
1836 int msw_sigpause (int nsig)
|
|
1837 {
|
|
1838 /* This is currently not called, because the only
|
|
1839 call to sigpause inside XEmacs is with SIGCHLD
|
|
1840 parameter. Just in case, we put an assert here,
|
|
1841 so anyone who will add a call to sigpause will
|
|
1842 be surprised (or surprise someone else...) */
|
|
1843 assert (0);
|
|
1844 return 0;
|
|
1845 }
|
|
1846
|
|
1847 int msw_raise (int nsig)
|
|
1848 {
|
|
1849 /* We delegate some raises to the system routine */
|
|
1850 if (nsig == SIGFPE || nsig == SIGABRT || nsig == SIGINT)
|
|
1851 return raise (nsig);
|
|
1852
|
|
1853 if (nsig < 0 || nsig > SIG_MAX)
|
|
1854 return errno = EINVAL;
|
|
1855
|
|
1856 /* If the signal is blocked, remember to issue later */
|
|
1857 if (signal_block_mask & sigmask(nsig))
|
|
1858 {
|
|
1859 signal_pending_mask |= sigmask(nsig);
|
|
1860 return 0;
|
|
1861 }
|
|
1862
|
|
1863 if (signal_handlers[nsig] == SIG_IGN)
|
|
1864 return 0;
|
|
1865
|
|
1866 if (signal_handlers[nsig] != SIG_DFL)
|
|
1867 {
|
|
1868 (*signal_handlers[nsig])(nsig);
|
|
1869 return 0;
|
|
1870 }
|
|
1871
|
|
1872 /* Default signal actions */
|
|
1873 if (nsig == SIGALRM || nsig == SIGPROF)
|
|
1874 exit (3);
|
|
1875
|
|
1876 /* Other signals are ignored by default */
|
|
1877 }
|
|
1878
|
|
1879 /*--------------------------------------------------------------------*/
|
|
1880 /* Async timers */
|
|
1881 /*--------------------------------------------------------------------*/
|
|
1882
|
|
1883 /* We emulate two timers, one for SIGALRM, another for SIGPROF.
|
|
1884
|
|
1885 itimerproc() function has an implementation limitation: it does
|
|
1886 not allow to set *both* interval and period. If an attempt is
|
|
1887 made to set both, and then they are unequal, the function
|
|
1888 asserts.
|
|
1889
|
|
1890 Minimum timer resolution on Win32 systems varies, and is greater
|
|
1891 than or equal than 1 ms. The resolution is always wrapped not to
|
|
1892 attempt to get below the system defined limit.
|
|
1893 */
|
|
1894
|
|
1895 /* Timer precision, denominator of one fraction: for 100 ms
|
|
1896 interval, request 10 ms precision
|
|
1897 */
|
|
1898 const int timer_prec = 10;
|
|
1899
|
|
1900 /* Last itimevals, as set by calls to setitimer */
|
|
1901 static struct itimerval it_alarm;
|
|
1902 static struct itimerval it_prof;
|
|
1903
|
|
1904 /* Timer IDs as returned by MM */
|
|
1905 MMRESULT tid_alarm = 0;
|
|
1906 MMRESULT tid_prof = 0;
|
|
1907
|
|
1908 static void CALLBACK timer_proc (UINT uID, UINT uMsg, DWORD dwUser,
|
|
1909 DWORD dw1, DWORD dw2)
|
|
1910 {
|
|
1911 /* Just raise a signal indicated by dwUser parameter */
|
|
1912 msw_raise (dwUser);
|
|
1913 }
|
|
1914
|
|
1915 /* Divide time in ms specified by IT by DENOM. Return 1 ms
|
|
1916 if division results in zero */
|
|
1917 static UINT period (const struct itimerval* it, UINT denom)
|
|
1918 {
|
|
1919 static TIMECAPS time_caps;
|
|
1920
|
|
1921 UINT res;
|
|
1922 const struct timeval* tv =
|
|
1923 (it->it_value.tv_sec == 0 && it->it_value.tv_usec == 0)
|
|
1924 ? &it->it_interval : &it->it_value;
|
|
1925
|
|
1926 /* Zero means stop timer */
|
|
1927 if (tv->tv_sec == 0 && tv->tv_usec == 0)
|
|
1928 return 0;
|
|
1929
|
|
1930 /* Conver to ms and divide by denom */
|
|
1931 res = (tv->tv_sec * 1000 + (tv->tv_usec + 500) / 1000) / denom;
|
|
1932
|
|
1933 /* Converge to minimum timer resolution */
|
|
1934 if (time_caps.wPeriodMin == 0)
|
|
1935 timeGetDevCaps (&time_caps, sizeof(time_caps));
|
|
1936
|
|
1937 if (res < time_caps.wPeriodMin)
|
|
1938 res = time_caps.wPeriodMin;
|
|
1939
|
|
1940 return res;
|
|
1941 }
|
|
1942
|
|
1943 static int setitimer_helper (const struct itimerval* itnew,
|
|
1944 struct itimerval* itold, struct itimerval* itcurrent,
|
|
1945 MMRESULT* tid, DWORD sigkind)
|
|
1946 {
|
|
1947 UINT delay, resolution, event_type;
|
|
1948
|
|
1949 /* First stop the old timer */
|
|
1950 if (*tid)
|
|
1951 {
|
|
1952 timeKillEvent (*tid);
|
|
1953 timeEndPeriod (period (itcurrent, timer_prec));
|
|
1954 *tid = 0;
|
|
1955 }
|
|
1956
|
|
1957 /* Return old itimerval if requested */
|
|
1958 if (itold)
|
|
1959 *itold = *itcurrent;
|
|
1960
|
|
1961 *itcurrent = *itnew;
|
|
1962
|
|
1963 /* Determine if to start new timer */
|
|
1964 delay = period (itnew, 1);
|
|
1965 if (delay)
|
|
1966 {
|
|
1967 resolution = period (itnew, timer_prec);
|
|
1968 event_type = (itnew->it_value.tv_sec == 0 && itnew->it_value.tv_usec == 0)
|
|
1969 ? TIME_ONESHOT : TIME_PERIODIC;
|
|
1970 timeBeginPeriod (resolution);
|
|
1971 *tid = timeSetEvent (delay, resolution, timer_proc, sigkind, event_type);
|
|
1972 }
|
|
1973
|
|
1974 return !delay || *tid;
|
|
1975 }
|
|
1976
|
|
1977 int setitimer (int kind, const struct itimerval* itnew,
|
|
1978 struct itimerval* itold)
|
|
1979 {
|
|
1980 /* In this version, both interval and value are allowed
|
|
1981 only if they are equal. */
|
|
1982 assert ((itnew->it_value.tv_sec == 0 && itnew->it_value.tv_usec == 0)
|
|
1983 || (itnew->it_interval.tv_sec == 0 && itnew->it_interval.tv_usec == 0)
|
|
1984 || (itnew->it_value.tv_sec == itnew->it_interval.tv_sec &&
|
|
1985 itnew->it_value.tv_usec == itnew->it_interval.tv_usec));
|
|
1986
|
|
1987 if (kind == ITIMER_REAL)
|
|
1988 return setitimer_helper (itnew, itold, &it_alarm, &tid_alarm, SIGALRM);
|
|
1989 else if (kind == ITIMER_PROF)
|
|
1990 return setitimer_helper (itnew, itold, &it_prof, &tid_prof, SIGPROF);
|
|
1991 else
|
|
1992 return errno = EINVAL;
|
|
1993 }
|
|
1994
|
100
|
1995 /* end of nt.c */
|