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