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