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