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