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