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