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