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