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