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