771
|
1 /* Utility and Unix shadow routines under MS Windows (WIN32_NATIVE defined).
|
428
|
2 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
|
2367
|
3 Copyright (C) 2000, 2001, 2002, 2004 Ben Wing.
|
428
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to the Free
|
|
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
20 02111-1307, USA.
|
|
21
|
771
|
22 */
|
428
|
23
|
771
|
24 /* Authorship:
|
428
|
25
|
771
|
26 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
|
|
27 Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au>
|
|
28 Sync'ed with Emacs 19.34.6 by Marc Paquette <marcpa@cam.org>
|
|
29 (Note: Sync messages from Marc Paquette may indicate
|
|
30 incomplete synching, so beware.)
|
|
31 Synched (completely!) with Emacs 20.6 by Ben Wing, 6-23-00.
|
|
32 Largely rewritten by Ben Wing for XEmacs Mule support.
|
2526
|
33 Synched (completely!) with Emacs 21.0.103 by Ben Wing, 6-13-01.
|
771
|
34 */
|
|
35
|
|
36 /* This file Mule-ized by Ben Wing, 6-23-00. */
|
428
|
37
|
|
38 #include <config.h>
|
|
39 #include "lisp.h"
|
|
40
|
592
|
41 #include "buffer.h"
|
872
|
42 #include "process.h"
|
592
|
43
|
859
|
44 #include "sysdir.h"
|
|
45 #include "sysfile.h"
|
428
|
46 #include "sysproc.h"
|
442
|
47 #include "syspwd.h"
|
859
|
48 #include "syssignal.h"
|
|
49 #include "systime.h"
|
428
|
50
|
442
|
51 #include "syswindows.h"
|
428
|
52
|
771
|
53 /* Control whether stat() attempts to determine file type and link count
|
|
54 exactly, at the expense of slower operation. Since true hard links
|
|
55 are supported on NTFS volumes, this is only relevant on NT. */
|
|
56 Lisp_Object Vmswindows_get_true_file_attributes;
|
428
|
57
|
771
|
58 /* Vmswindows_generate_fake_inodes; deleted */
|
|
59
|
|
60 Fixnum mswindows_fake_unix_uid;
|
428
|
61
|
|
62 /* Emulate getpwuid, getpwnam and others. */
|
|
63
|
771
|
64 static struct passwd the_passwd =
|
428
|
65 {
|
771
|
66 "",
|
|
67 "",
|
428
|
68 0,
|
|
69 0,
|
|
70 0,
|
771
|
71 "",
|
|
72 "",
|
|
73 "",
|
428
|
74 };
|
|
75
|
|
76 uid_t
|
442
|
77 getuid (void)
|
440
|
78 {
|
771
|
79 return mswindows_fake_unix_uid;
|
428
|
80 }
|
|
81
|
|
82 uid_t
|
442
|
83 geteuid (void)
|
428
|
84 {
|
771
|
85 /* Emacs 20.6 says: [[I could imagine arguing for checking to see
|
|
86 whether the user is in the Administrators group and returning a
|
|
87 UID of 0 for that case, but I don't know how wise that would be
|
|
88 in the long run.]] */
|
|
89 return mswindows_fake_unix_uid;
|
428
|
90 }
|
|
91
|
|
92 gid_t
|
442
|
93 getgid (void)
|
428
|
94 {
|
|
95 return the_passwd.pw_gid;
|
|
96 }
|
|
97
|
|
98 gid_t
|
442
|
99 getegid (void)
|
428
|
100 {
|
|
101 return getgid ();
|
|
102 }
|
|
103
|
|
104 struct passwd *
|
|
105 getpwuid (uid_t uid)
|
|
106 {
|
771
|
107 if (uid == mswindows_fake_unix_uid)
|
440
|
108 {
|
|
109 the_passwd.pw_gid = the_passwd.pw_uid = uid;
|
|
110 return &the_passwd;
|
|
111 }
|
|
112 else
|
|
113 return NULL;
|
428
|
114 }
|
|
115
|
|
116 struct passwd *
|
867
|
117 getpwnam (const Ibyte *name)
|
428
|
118 {
|
|
119 struct passwd *pw;
|
|
120
|
|
121 pw = getpwuid (getuid ());
|
|
122 if (!pw)
|
|
123 return pw;
|
|
124
|
1204
|
125 if (qxestrcasecmp_i18n (name, (Ibyte *) pw->pw_name))
|
428
|
126 return NULL;
|
|
127
|
|
128 return pw;
|
|
129 }
|
|
130
|
771
|
131 static void
|
442
|
132 init_user_info (void)
|
428
|
133 {
|
440
|
134 /* This code is pretty much of ad hoc nature. There is no unix-like
|
|
135 UIDs under Windows NT. There is no concept of root user, because
|
|
136 all security is ACL-based. Instead, let's use a simple variable,
|
|
137 nt-fake-unix-uid, which would allow the user to have a uid of
|
|
138 choice. --kkm, 02/03/2000 */
|
|
139 #if 0
|
428
|
140 /* Find the user's real name by opening the process token and
|
|
141 looking up the name associated with the user-sid in that token.
|
|
142
|
|
143 Use the relative portion of the identifier authority value from
|
|
144 the user-sid as the user id value (same for group id using the
|
|
145 primary group sid from the process token). */
|
|
146
|
771
|
147 TOKEN_USER sidinfo;
|
|
148 Extbyte name[256], domain[256];
|
|
149 Charcount length = sizeof (name) / XETCHAR_SIZE;
|
|
150 Charcount dlength = sizeof (domain) / XETCHAR_SIZE;
|
|
151 DWORD trash;
|
|
152 HANDLE token = NULL;
|
|
153 SID_NAME_USE user_type;
|
428
|
154
|
|
155 if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token)
|
771
|
156 && GetTokenInformation (token, TokenUser, &sidinfo, sizeof (sidinfo),
|
|
157 &trash)
|
|
158 && qxeLookupAccountSid (NULL, sidinfo.User.Sid, name, &length,
|
|
159 domain, &dlength, &user_type))
|
428
|
160 {
|
771
|
161 TSTR_TO_C_STRING_MALLOC (name, the_passwd.pw_name);
|
428
|
162 /* Determine a reasonable uid value. */
|
771
|
163 if (qxestrcasecmp ("administrator", the_passwd.pw_name) == 0)
|
428
|
164 {
|
|
165 the_passwd.pw_uid = 0;
|
|
166 the_passwd.pw_gid = 0;
|
|
167 }
|
|
168 else
|
|
169 {
|
|
170 SID_IDENTIFIER_AUTHORITY * pSIA;
|
771
|
171 TOKEN_PRIMARY_GROUP group;
|
428
|
172
|
771
|
173 pSIA = GetSidIdentifierAuthority (sidinfo.User.Sid);
|
428
|
174 /* I believe the relative portion is the last 4 bytes (of 6)
|
|
175 with msb first. */
|
|
176 the_passwd.pw_uid = ((pSIA->Value[2] << 24) +
|
|
177 (pSIA->Value[3] << 16) +
|
|
178 (pSIA->Value[4] << 8) +
|
|
179 (pSIA->Value[5] << 0));
|
|
180 /* restrict to conventional uid range for normal users */
|
|
181 the_passwd.pw_uid = the_passwd.pw_uid % 60001;
|
|
182
|
|
183 /* Get group id */
|
|
184 if (GetTokenInformation (token, TokenPrimaryGroup,
|
771
|
185 &group, sizeof (group), &trash))
|
428
|
186 {
|
|
187 SID_IDENTIFIER_AUTHORITY * pSIA;
|
|
188
|
771
|
189 pSIA = GetSidIdentifierAuthority (group.PrimaryGroup);
|
428
|
190 the_passwd.pw_gid = ((pSIA->Value[2] << 24) +
|
|
191 (pSIA->Value[3] << 16) +
|
|
192 (pSIA->Value[4] << 8) +
|
|
193 (pSIA->Value[5] << 0));
|
|
194 /* I don't know if this is necessary, but for safety... */
|
|
195 the_passwd.pw_gid = the_passwd.pw_gid % 60001;
|
|
196 }
|
|
197 else
|
|
198 the_passwd.pw_gid = the_passwd.pw_uid;
|
|
199 }
|
|
200 }
|
|
201 /* If security calls are not supported (presumably because we
|
|
202 are running under Windows 95), fallback to this. */
|
771
|
203 else if (qxeGetUserName (name, &length))
|
428
|
204 {
|
771
|
205 TSTR_TO_C_STRING_MALLOC (name, the_passwd.pw_name);
|
|
206 if (qxestrcasecmp ("administrator", the_passwd.pw_name) == 0)
|
428
|
207 the_passwd.pw_uid = 0;
|
|
208 else
|
|
209 the_passwd.pw_uid = 123;
|
|
210 the_passwd.pw_gid = the_passwd.pw_uid;
|
|
211 }
|
|
212 else
|
|
213 {
|
771
|
214 the_passwd.pw_name = "unknown";
|
428
|
215 the_passwd.pw_uid = 123;
|
|
216 the_passwd.pw_gid = 123;
|
|
217 }
|
|
218
|
440
|
219 if (token)
|
|
220 CloseHandle (token);
|
|
221 #else
|
|
222 /* Obtain only logon id here, uid part is moved to getuid */
|
771
|
223 DWORD length = UNLEN + 1;
|
|
224 Extbyte name[MAX_XETCHAR_SIZE * (UNLEN + 1)];
|
|
225 if (qxeGetUserName (name, &length))
|
|
226 TSTR_TO_C_STRING_MALLOC (name, the_passwd.pw_name);
|
440
|
227 else
|
771
|
228 the_passwd.pw_name = "unknown";
|
440
|
229 #endif
|
|
230
|
771
|
231 #if 0
|
428
|
232 /* Ensure HOME and SHELL are defined. */
|
|
233 /*
|
|
234 * With XEmacs, setting $HOME is deprecated.
|
|
235 */
|
771
|
236 if (egetenv ("HOME") == NULL)
|
|
237 eputenv ("HOME=c:/");
|
428
|
238 #endif
|
|
239
|
611
|
240 /* Set dir from environment variables. */
|
771
|
241 the_passwd.pw_dir = (char *) qxestrdup (get_home_directory ());
|
611
|
242 /* We used to set pw_shell here, but the order is wrong (SHELL gets
|
853
|
243 initted in process.c, called later in the init process) and pw_shell
|
611
|
244 is not used anywhere. */
|
428
|
245 }
|
|
246
|
771
|
247 /* Parse the root part of file name, if present. Return length and
|
867
|
248 optionally store pointer to Ibyte after root. */
|
771
|
249 static Bytecount
|
867
|
250 parse_root (Ibyte *name, Ibyte **pPath)
|
428
|
251 {
|
867
|
252 Ibyte *start = name;
|
428
|
253
|
|
254 if (name == NULL)
|
|
255 return 0;
|
|
256
|
|
257 /* find the root name of the volume if given */
|
|
258 if (isalpha (name[0]) && name[1] == ':')
|
|
259 {
|
|
260 /* skip past drive specifier */
|
|
261 name += 2;
|
|
262 if (IS_DIRECTORY_SEP (name[0]))
|
|
263 name++;
|
|
264 }
|
|
265 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
|
|
266 {
|
|
267 int slashes = 2;
|
|
268 name += 2;
|
|
269 do
|
|
270 {
|
|
271 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
|
|
272 break;
|
|
273 name++;
|
|
274 }
|
771
|
275 while (*name);
|
428
|
276 if (IS_DIRECTORY_SEP (name[0]))
|
|
277 name++;
|
|
278 }
|
|
279
|
|
280 if (pPath)
|
|
281 *pPath = name;
|
|
282
|
|
283 return name - start;
|
|
284 }
|
|
285
|
|
286 /* Get long base name for name; name is assumed to be absolute. */
|
867
|
287 static Ibyte *
|
|
288 get_long_basename (Ibyte *name)
|
428
|
289 {
|
771
|
290 WIN32_FIND_DATAW find_data;
|
428
|
291 HANDLE dir_handle;
|
771
|
292 Extbyte *nameext;
|
428
|
293
|
771
|
294 /* must be valid filename, no wild cards or other invalid characters */
|
|
295 if (qxestrpbrk (name, "*?|<>\""))
|
|
296 return 0;
|
428
|
297
|
2526
|
298 PATHNAME_CONVERT_OUT (name, nameext);
|
771
|
299 dir_handle = qxeFindFirstFile (nameext, &find_data);
|
428
|
300 if (dir_handle != INVALID_HANDLE_VALUE)
|
|
301 {
|
867
|
302 Ibyte *fileint;
|
771
|
303
|
|
304 TSTR_TO_C_STRING_MALLOC (find_data.cFileName, fileint);
|
428
|
305 FindClose (dir_handle);
|
771
|
306 return fileint;
|
428
|
307 }
|
771
|
308 return 0;
|
428
|
309 }
|
|
310
|
|
311 /* Get long name for file, if possible (assumed to be absolute). */
|
867
|
312 Ibyte *
|
|
313 mswindows_get_long_filename (Ibyte *name)
|
428
|
314 {
|
867
|
315 Ibyte *full = mswindows_canonicalize_filename (name);
|
|
316 Ibyte *p;
|
|
317 Ibyte *q;
|
771
|
318 DECLARE_EISTRING (o);
|
|
319 Bytecount len;
|
428
|
320
|
|
321 /* Copy root part verbatim. */
|
|
322 len = parse_root (full, &p);
|
771
|
323 eicpy_raw (o, full, len);
|
428
|
324
|
771
|
325 while (p != NULL && *p)
|
428
|
326 {
|
867
|
327 Ibyte *component;
|
771
|
328
|
428
|
329 q = p;
|
771
|
330 p = qxestrchr (q, '\\');
|
428
|
331 if (p) *p = '\0';
|
771
|
332 component = get_long_basename (full);
|
|
333 if (component)
|
428
|
334 {
|
771
|
335 eicat_rawz (o, component);
|
428
|
336 if (p != NULL)
|
|
337 {
|
|
338 *p++ = '\\';
|
771
|
339 eicat_ch (o, '\\');
|
428
|
340 }
|
1726
|
341 xfree (component, Ibyte *);
|
428
|
342 }
|
|
343 else
|
771
|
344 {
|
1726
|
345 xfree (full, Ibyte *);
|
771
|
346 return 0;
|
|
347 }
|
428
|
348 }
|
|
349
|
1726
|
350 xfree (full, Ibyte *);
|
771
|
351 return eicpyout_malloc (o, 0);
|
428
|
352 }
|
|
353
|
771
|
354 static int
|
867
|
355 is_unc_volume (const Ibyte *filename)
|
771
|
356 {
|
867
|
357 const Ibyte *ptr = filename;
|
428
|
358
|
771
|
359 if (!IS_DIRECTORY_SEP (ptr[0]) || !IS_DIRECTORY_SEP (ptr[1]) || !ptr[2])
|
|
360 return 0;
|
|
361
|
|
362 if (qxestrpbrk (ptr + 2, "*?|<>\"\\/"))
|
|
363 return 0;
|
|
364
|
|
365 return 1;
|
428
|
366 }
|
|
367
|
771
|
368 /* NOTE: Value returned is still in external format. Callers need to
|
|
369 convert. */
|
707
|
370 #define REG_ROOT "SOFTWARE\\XEmacs\\XEmacs"
|
428
|
371
|
771
|
372 static LPBYTE
|
867
|
373 nt_get_resource (Ibyte *key, LPDWORD lpdwtype)
|
428
|
374 {
|
|
375 LPBYTE lpvalue;
|
|
376 HKEY hrootkey = NULL;
|
|
377 DWORD cbData;
|
771
|
378 Extbyte *keyext;
|
|
379
|
|
380 C_STRING_TO_TSTR (key, keyext);
|
428
|
381
|
|
382 /* Check both the current user and the local machine to see if
|
|
383 we have any resources. */
|
|
384
|
771
|
385 if (qxeRegOpenKeyEx (HKEY_CURRENT_USER, XETEXT (REG_ROOT), 0, KEY_READ,
|
|
386 &hrootkey) == ERROR_SUCCESS)
|
428
|
387 {
|
|
388 lpvalue = NULL;
|
|
389
|
771
|
390 if (qxeRegQueryValueEx (hrootkey, keyext, NULL, NULL, NULL,
|
|
391 &cbData) == ERROR_SUCCESS
|
2367
|
392 && (lpvalue = xnew_array (BYTE, cbData)) != NULL
|
771
|
393 && qxeRegQueryValueEx (hrootkey, keyext, NULL, lpdwtype, lpvalue,
|
|
394 &cbData) == ERROR_SUCCESS)
|
|
395 return (lpvalue);
|
428
|
396
|
1726
|
397 if (lpvalue)
|
|
398 xfree (lpvalue, LPBYTE);
|
428
|
399
|
|
400 RegCloseKey (hrootkey);
|
|
401 }
|
|
402
|
771
|
403 if (qxeRegOpenKeyEx (HKEY_LOCAL_MACHINE, XETEXT (REG_ROOT), 0, KEY_READ,
|
|
404 &hrootkey) == ERROR_SUCCESS)
|
428
|
405 {
|
|
406 lpvalue = NULL;
|
|
407
|
771
|
408 if (qxeRegQueryValueEx (hrootkey, keyext, NULL, NULL, NULL,
|
|
409 &cbData) == ERROR_SUCCESS &&
|
2367
|
410 (lpvalue = xnew_array (BYTE, cbData)) != NULL &&
|
771
|
411 qxeRegQueryValueEx (hrootkey, keyext, NULL, lpdwtype, lpvalue,
|
|
412 &cbData) == ERROR_SUCCESS)
|
|
413 return (lpvalue);
|
428
|
414
|
1726
|
415 if (lpvalue)
|
|
416 xfree (lpvalue, LPBYTE);
|
428
|
417
|
|
418 RegCloseKey (hrootkey);
|
|
419 }
|
|
420
|
|
421 return (NULL);
|
|
422 }
|
|
423
|
|
424 void
|
814
|
425 init_mswindows_environment (void)
|
428
|
426 {
|
|
427 /* Check for environment variables and use registry if they don't exist */
|
771
|
428 /* Emacs 20.6 sets default values for these; not necessary here because
|
|
429 we already supply them. (except SHELL, which is set in init_user_info().)
|
|
430 Emacs 20.6 messes with TMPDIR; not necessary here. */
|
428
|
431 {
|
|
432 int i;
|
|
433 LPBYTE lpval;
|
|
434 DWORD dwType;
|
|
435
|
2367
|
436 static Ascbyte *env_vars[] =
|
428
|
437 {
|
|
438 "HOME",
|
|
439 "EMACSLOADPATH",
|
|
440 "EMACSDEBUGPATHS",
|
|
441 "SHELL",
|
|
442 "CMDPROXY",
|
|
443 "EMACSDATA",
|
|
444 "EMACSPATH",
|
|
445 "EMACSPACKAGEPATH",
|
771
|
446 "EMACSLOCKMETHOD",
|
428
|
447 "INFOPATH"
|
|
448 };
|
771
|
449 #if defined (HEAP_IN_DATA) && !defined (PDUMP)
|
430
|
450 cache_system_info ();
|
|
451 #endif
|
771
|
452
|
|
453 #if 0 /* FSF 21.1 */
|
|
454 /* !!#### i think i already do the equivalent elsewhere.
|
|
455 delete when i'm sure i do.
|
|
456 (but maybe i should be playing with LANG when the user changes
|
|
457 the locale, so that subprocesses get it right.) */
|
|
458 /* Get default locale info and use it for LANG. */
|
|
459 if (GetLocaleInfo (LOCALE_USER_DEFAULT,
|
|
460 LOCALE_SABBREVLANGNAME | LOCALE_USE_CP_ACP,
|
|
461 locale_name, sizeof (locale_name)))
|
|
462 {
|
|
463 for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
|
|
464 {
|
|
465 if (strcmp (env_vars[i].name, "LANG") == 0)
|
|
466 {
|
|
467 env_vars[i].def_value = locale_name;
|
|
468 break;
|
|
469 }
|
|
470 }
|
|
471 }
|
|
472 #endif /* 0 */
|
|
473
|
428
|
474 for (i = 0; i < countof (env_vars); i++)
|
|
475 {
|
771
|
476 if (!egetenv (env_vars[i]) &&
|
1204
|
477 (lpval = nt_get_resource ((Ibyte *) env_vars[i], &dwType)) != NULL)
|
428
|
478 {
|
|
479 if (dwType == REG_EXPAND_SZ)
|
|
480 {
|
771
|
481 Extbyte *buf = NULL;
|
867
|
482 Ibyte *envval;
|
771
|
483 Charcount cch;
|
428
|
484
|
771
|
485 cch = qxeExpandEnvironmentStrings ((Extbyte *) lpval, buf, 0);
|
2367
|
486 buf = alloca_extbytes (cch * XETCHAR_SIZE);
|
771
|
487 qxeExpandEnvironmentStrings ((Extbyte *) lpval, buf, cch);
|
|
488 TSTR_TO_C_STRING (buf, envval);
|
1204
|
489 eputenv (env_vars[i], (CIbyte *) envval);
|
428
|
490 }
|
|
491 else if (dwType == REG_SZ)
|
|
492 {
|
867
|
493 Ibyte *envval;
|
771
|
494
|
|
495 TSTR_TO_C_STRING (lpval, envval);
|
1204
|
496 eputenv (env_vars[i], (CIbyte *) envval);
|
428
|
497 }
|
|
498
|
1726
|
499 xfree (lpval, LPBYTE);
|
428
|
500 }
|
|
501 }
|
|
502 }
|
|
503
|
|
504 /* Another special case: on NT, the PATH variable is actually named
|
|
505 "Path" although cmd.exe (perhaps NT itself) arranges for
|
|
506 environment variable lookup and setting to be case insensitive.
|
|
507 However, Emacs assumes a fully case sensitive environment, so we
|
|
508 need to change "Path" to "PATH" to match the expectations of
|
771
|
509 various elisp packages.
|
428
|
510
|
|
511 The same applies to COMSPEC. */
|
|
512 {
|
2367
|
513 EXTERNAL_LIST_LOOP_2 (str, Vprocess_environment)
|
771
|
514 {
|
|
515 if (STRINGP (str))
|
|
516 {
|
867
|
517 Ibyte *dat = XSTRING_DATA (str);
|
2367
|
518 if (qxestrncasecmp_ascii (dat, "PATH=", 5) == 0)
|
771
|
519 memcpy (dat, "PATH=", 5);
|
2367
|
520 else if (qxestrncasecmp_ascii (dat, "COMSPEC=", 8) == 0)
|
771
|
521 memcpy (dat, "COMSPEC=", 8);
|
|
522 }
|
|
523 }
|
428
|
524 }
|
|
525
|
|
526 init_user_info ();
|
|
527 }
|
|
528
|
771
|
529 /* Emacs 20.6 contains a routine get_emacs_configuration() here to set
|
|
530 EMACS_CONFIGURATION. */
|
428
|
531 #ifndef HAVE_X_WINDOWS
|
|
532 /* X11R6 on NT provides the single parameter version of this command. */
|
|
533
|
|
534 #include <sys/timeb.h>
|
|
535
|
|
536 /* Emulate gettimeofday (Ulrich Leodolter, 1/11/95). */
|
|
537 void
|
|
538 gettimeofday (struct timeval *tv, struct timezone *tz)
|
|
539 {
|
|
540 struct _timeb tb;
|
|
541 _ftime (&tb);
|
|
542
|
|
543 tv->tv_sec = tb.time;
|
|
544 tv->tv_usec = tb.millitm * 1000L;
|
|
545 if (tz)
|
|
546 {
|
|
547 tz->tz_minuteswest = tb.timezone; /* minutes west of Greenwich */
|
|
548 tz->tz_dsttime = tb.dstflag; /* type of dst correction */
|
|
549 }
|
|
550 }
|
|
551
|
|
552 #endif /* HAVE_X_WINDOWS */
|
|
553
|
771
|
554
|
428
|
555 /* ------------------------------------------------------------------------- */
|
771
|
556 /* IO support and wrapper functions for Win32 API. */
|
428
|
557 /* ------------------------------------------------------------------------- */
|
|
558
|
771
|
559 typedef struct volume_info_data
|
428
|
560 {
|
771
|
561 struct volume_info_data *next;
|
428
|
562
|
|
563 /* time when info was obtained */
|
771
|
564 DWORD timestamp;
|
428
|
565
|
|
566 /* actual volume info */
|
867
|
567 Ibyte *root_dir;
|
771
|
568 DWORD serialnum;
|
|
569 DWORD maxcomp;
|
|
570 DWORD flags;
|
867
|
571 Ibyte *name;
|
|
572 Ibyte *type;
|
428
|
573 } volume_info_data;
|
|
574
|
|
575 /* Global referenced by various functions. */
|
|
576 static volume_info_data volume_info;
|
|
577
|
|
578 /* Vector to indicate which drives are local and fixed (for which cached
|
|
579 data never expires). */
|
|
580 static BOOL fixed_drives[26];
|
|
581
|
|
582 /* Consider cached volume information to be stale if older than 10s,
|
|
583 at least for non-local drives. Info for fixed drives is never stale. */
|
|
584 #define DRIVE_INDEX( c ) ( (c) <= 'Z' ? (c) - 'A' : (c) - 'a' )
|
|
585 #define VOLINFO_STILL_VALID( root_dir, info ) \
|
|
586 ( ( isalpha (root_dir[0]) && \
|
|
587 fixed_drives[ DRIVE_INDEX (root_dir[0]) ] ) \
|
|
588 || GetTickCount () - info->timestamp < 10000 )
|
|
589
|
|
590 /* Cache support functions. */
|
|
591
|
|
592 /* Simple linked list with linear search is sufficient. */
|
|
593 static volume_info_data *volume_cache = NULL;
|
|
594
|
|
595 static volume_info_data *
|
867
|
596 lookup_volume_info (Ibyte *root_dir)
|
428
|
597 {
|
771
|
598 volume_info_data *info;
|
428
|
599
|
|
600 for (info = volume_cache; info; info = info->next)
|
771
|
601 if (qxestrcasecmp_i18n (info->root_dir, root_dir) == 0)
|
428
|
602 break;
|
|
603 return info;
|
|
604 }
|
|
605
|
|
606 static void
|
867
|
607 add_volume_info (Ibyte *root_dir, volume_info_data *info)
|
428
|
608 {
|
771
|
609 info->root_dir = qxestrdup (root_dir);
|
428
|
610 info->next = volume_cache;
|
|
611 volume_cache = info;
|
|
612 }
|
|
613
|
|
614
|
|
615 /* Wrapper for GetVolumeInformation, which uses caching to avoid
|
|
616 performance penalty (~2ms on 486 for local drives, 7.5ms for local
|
|
617 cdrom drive, ~5-10ms or more for remote drives on LAN). */
|
771
|
618 static volume_info_data *
|
867
|
619 get_cached_volume_information (Ibyte *root_dir)
|
428
|
620 {
|
771
|
621 volume_info_data *info;
|
867
|
622 Ibyte *default_root;
|
428
|
623
|
|
624 /* NULL for root_dir means use root from current directory. */
|
|
625 if (root_dir == NULL)
|
|
626 {
|
771
|
627 Charcount nchars = qxeGetCurrentDirectory (0, NULL);
|
|
628 Extbyte *rootext;
|
|
629
|
|
630 if (!nchars)
|
428
|
631 return NULL;
|
771
|
632 rootext = alloca_extbytes (nchars * XETCHAR_SIZE);
|
|
633 if (!qxeGetCurrentDirectory (nchars, rootext))
|
|
634 return NULL;
|
|
635 TSTR_TO_C_STRING (rootext, default_root);
|
428
|
636 parse_root (default_root, &root_dir);
|
|
637 *root_dir = 0;
|
|
638 root_dir = default_root;
|
|
639 }
|
|
640
|
|
641 /* Local fixed drives can be cached permanently. Removable drives
|
|
642 cannot be cached permanently, since the volume name and serial
|
|
643 number (if nothing else) can change. Remote drives should be
|
|
644 treated as if they are removable, since there is no sure way to
|
|
645 tell whether they are or not. Also, the UNC association of drive
|
|
646 letters mapped to remote volumes can be changed at any time (even
|
|
647 by other processes) without notice.
|
|
648
|
|
649 As a compromise, so we can benefit from caching info for remote
|
|
650 volumes, we use a simple expiry mechanism to invalidate cache
|
|
651 entries that are more than ten seconds old. */
|
|
652
|
|
653 #if 0
|
|
654 /* No point doing this, because WNetGetConnection is even slower than
|
|
655 GetVolumeInformation, consistently taking ~50ms on a 486 (FWIW,
|
|
656 GetDriveType is about the only call of this type which does not
|
|
657 involve network access, and so is extremely quick). */
|
|
658
|
|
659 /* Map drive letter to UNC if remote. */
|
771
|
660 if (isalpha (root_dir[0]) && !fixed [DRIVE_INDEX (root_dir[0])])
|
428
|
661 {
|
771
|
662 Extbyte remote_name[256 * XETCHAR_SIZE];
|
867
|
663 Ibyte drive[3] = { root_dir[0], ':' };
|
771
|
664 Extbyte *driveext;
|
428
|
665
|
2526
|
666 PATHNAME_CONVERT_OUT (drive, driveext);
|
771
|
667 if (qxeWNetGetConnection (driveext, remote_name,
|
|
668 sizeof (remote_name) / XETCHAR_SIZE)
|
428
|
669 == NO_ERROR)
|
|
670 /* do something */ ;
|
|
671 }
|
|
672 #endif
|
|
673
|
|
674 info = lookup_volume_info (root_dir);
|
|
675
|
|
676 if (info == NULL || ! VOLINFO_STILL_VALID (root_dir, info))
|
771
|
677 {
|
|
678 Extbyte name[256 * MAX_XETCHAR_SIZE];
|
|
679 DWORD serialnum;
|
|
680 DWORD maxcomp;
|
|
681 DWORD flags;
|
|
682 Extbyte type[256 * MAX_XETCHAR_SIZE];
|
1204
|
683 Extbyte *rootdirext;
|
|
684
|
2526
|
685 PATHNAME_CONVERT_OUT (root_dir, rootdirext);
|
428
|
686
|
771
|
687 /* Info is not cached, or is stale. */
|
1204
|
688 if (!qxeGetVolumeInformation (rootdirext,
|
771
|
689 name, sizeof (name) / XETCHAR_SIZE,
|
|
690 &serialnum,
|
|
691 &maxcomp,
|
|
692 &flags,
|
|
693 type, sizeof (type) / XETCHAR_SIZE))
|
|
694 return NULL;
|
428
|
695
|
771
|
696 /* Cache the volume information for future use, overwriting existing
|
|
697 entry if present. */
|
|
698 if (info == NULL)
|
|
699 {
|
2367
|
700 info = xnew (volume_info_data);
|
771
|
701 add_volume_info (root_dir, info);
|
|
702 }
|
|
703 else
|
|
704 {
|
1726
|
705 xfree (info->name, Ibyte *);
|
|
706 xfree (info->type, Ibyte *);
|
771
|
707 }
|
428
|
708
|
771
|
709 TSTR_TO_C_STRING_MALLOC (name, info->name);
|
|
710 info->serialnum = serialnum;
|
|
711 info->maxcomp = maxcomp;
|
|
712 info->flags = flags;
|
|
713 TSTR_TO_C_STRING_MALLOC (type, info->type);
|
|
714 info->timestamp = GetTickCount ();
|
|
715 }
|
428
|
716
|
|
717 return info;
|
|
718 }
|
|
719
|
|
720 /* Get information on the volume where name is held; set path pointer to
|
|
721 start of pathname in name (past UNC header\volume header if present). */
|
771
|
722 static int
|
867
|
723 get_volume_info (const Ibyte *name, const Ibyte **pPath)
|
428
|
724 {
|
771
|
725 /* We probably only need a couple of bytes, but let's be generous in
|
|
726 case this function gets changed */
|
2367
|
727 Ibyte *temp = alloca_ibytes (qxestrlen (name) + 10);
|
867
|
728 Ibyte *rootname = NULL; /* default to current volume */
|
771
|
729 volume_info_data *info;
|
428
|
730
|
|
731 if (name == NULL)
|
|
732 return FALSE;
|
|
733
|
|
734 /* find the root name of the volume if given */
|
|
735 if (isalpha (name[0]) && name[1] == ':')
|
|
736 {
|
|
737 rootname = temp;
|
|
738 temp[0] = *name++;
|
|
739 temp[1] = *name++;
|
|
740 temp[2] = '\\';
|
|
741 temp[3] = 0;
|
|
742 }
|
|
743 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
|
|
744 {
|
867
|
745 Ibyte *str = temp;
|
428
|
746 int slashes = 4;
|
|
747 rootname = temp;
|
|
748 do
|
|
749 {
|
|
750 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
|
|
751 break;
|
|
752 *str++ = *name++;
|
|
753 }
|
771
|
754 while (*name);
|
428
|
755
|
|
756 *str++ = '\\';
|
|
757 *str = 0;
|
|
758 }
|
|
759
|
|
760 if (pPath)
|
|
761 *pPath = name;
|
|
762
|
771
|
763 info = get_cached_volume_information (rootname);
|
428
|
764 if (info != NULL)
|
|
765 {
|
|
766 /* Set global referenced by other functions. */
|
|
767 volume_info = *info;
|
|
768 return TRUE;
|
|
769 }
|
|
770 return FALSE;
|
|
771 }
|
|
772
|
771
|
773 /* XEmacs: Everything referring to map_win32_filename() aka map_w32_filename()
|
|
774 removed; it was only for NT 3.1, which we hereby do not support. (NT 3.5
|
|
775 predates Windows 95!) */
|
428
|
776
|
1204
|
777 int
|
|
778 mswindows_is_executable (const Ibyte *name)
|
771
|
779 {
|
867
|
780 Ibyte *p = qxestrrchr (name, '.');
|
2367
|
781 return (p != NULL && (qxestrcasecmp_ascii (p, ".exe") == 0 ||
|
|
782 qxestrcasecmp_ascii (p, ".com") == 0 ||
|
|
783 qxestrcasecmp_ascii (p, ".bat") == 0 ||
|
|
784 qxestrcasecmp_ascii (p, ".cmd") == 0));
|
428
|
785 }
|
|
786
|
|
787 /* Emulate the Unix directory procedures opendir, closedir,
|
|
788 and readdir. We can't use the procedures supplied in sysdep.c,
|
|
789 so we provide them here. */
|
|
790
|
|
791 struct direct dir_static; /* simulated directory contents */
|
|
792 static HANDLE dir_find_handle = INVALID_HANDLE_VALUE;
|
771
|
793 /* dir_is_fat deleted */
|
867
|
794 static Ibyte *dir_pathname;
|
771
|
795 static WIN32_FIND_DATAW dir_find_data;
|
|
796
|
|
797 /* Support shares on a network resource as subdirectories of a read-only
|
|
798 root directory. */
|
|
799 static HANDLE wnet_enum_handle = INVALID_HANDLE_VALUE;
|
867
|
800 static HANDLE open_unc_volume (const Ibyte *);
|
|
801 static Ibyte *read_unc_volume (HANDLE);
|
771
|
802 static int close_unc_volume (HANDLE);
|
428
|
803
|
|
804 DIR *
|
867
|
805 mswindows_opendir (const Ibyte *filename)
|
428
|
806 {
|
|
807 DIR *dirp;
|
|
808
|
|
809 /* Opening is done by FindFirstFile. However, a read is inherent to
|
|
810 this operation, so we defer the open until read time. */
|
|
811
|
771
|
812 if (dir_find_handle != INVALID_HANDLE_VALUE)
|
428
|
813 return NULL;
|
771
|
814 if (wnet_enum_handle != INVALID_HANDLE_VALUE)
|
428
|
815 return NULL;
|
|
816
|
771
|
817 if (is_unc_volume (filename))
|
|
818 {
|
|
819 wnet_enum_handle = open_unc_volume (filename);
|
|
820 if (wnet_enum_handle == INVALID_HANDLE_VALUE)
|
|
821 return NULL;
|
|
822 }
|
428
|
823
|
771
|
824 if (!(dirp = xnew_and_zero (DIR)))
|
|
825 return NULL;
|
|
826
|
|
827 if (dir_pathname)
|
1726
|
828 xfree (dir_pathname, Ibyte *);
|
771
|
829 dir_pathname = qxestrdup (filename);
|
428
|
830
|
|
831 return dirp;
|
|
832 }
|
|
833
|
442
|
834 int
|
771
|
835 mswindows_closedir (DIR *dirp)
|
428
|
836 {
|
771
|
837 int retval;
|
442
|
838
|
428
|
839 /* If we have a find-handle open, close it. */
|
|
840 if (dir_find_handle != INVALID_HANDLE_VALUE)
|
|
841 {
|
771
|
842 retval = FindClose (dir_find_handle) ? 0 : -1;
|
428
|
843 dir_find_handle = INVALID_HANDLE_VALUE;
|
|
844 }
|
771
|
845 else if (wnet_enum_handle != INVALID_HANDLE_VALUE)
|
|
846 {
|
|
847 retval = close_unc_volume (wnet_enum_handle);
|
|
848 wnet_enum_handle = INVALID_HANDLE_VALUE;
|
|
849 }
|
1726
|
850 xfree (dirp, DIR *);
|
771
|
851
|
|
852 return retval;
|
|
853 }
|
|
854
|
|
855 struct direct *
|
2286
|
856 mswindows_readdir (DIR *UNUSED (dirp))
|
771
|
857 {
|
867
|
858 Ibyte *val;
|
771
|
859 int need_to_free = 0;
|
|
860
|
|
861 if (wnet_enum_handle != INVALID_HANDLE_VALUE)
|
|
862 {
|
|
863 if (!(val = read_unc_volume (wnet_enum_handle)))
|
|
864 return NULL;
|
|
865 need_to_free = 1;
|
|
866 }
|
|
867 /* If we aren't dir_finding, do a find-first, otherwise do a find-next. */
|
|
868 else if (dir_find_handle == INVALID_HANDLE_VALUE)
|
|
869 {
|
|
870 DECLARE_EISTRING (filename);
|
867
|
871 Ichar lastch;
|
2526
|
872 Extbyte *fileext;
|
771
|
873
|
|
874 eicpy_rawz (filename, dir_pathname);
|
|
875 lastch = eigetch_char (filename, eicharlen (filename) - 1);
|
|
876 if (!IS_DIRECTORY_SEP (lastch))
|
|
877 eicat_ch (filename, '\\');
|
|
878 eicat_ch (filename, '*');
|
2526
|
879 PATHNAME_CONVERT_OUT (eidata (filename), fileext);
|
771
|
880
|
2526
|
881 dir_find_handle = qxeFindFirstFile (fileext, &dir_find_data);
|
771
|
882
|
|
883 if (dir_find_handle == INVALID_HANDLE_VALUE)
|
|
884 return NULL;
|
|
885 TSTR_TO_C_STRING (dir_find_data.cFileName, val);
|
|
886 }
|
|
887 else
|
|
888 {
|
|
889 if (!qxeFindNextFile (dir_find_handle, &dir_find_data))
|
|
890 return NULL;
|
|
891 TSTR_TO_C_STRING (dir_find_data.cFileName, val);
|
|
892 }
|
|
893
|
|
894 /* XEmacs never uses this value, so don't bother making it match
|
|
895 value returned by qxe_stat(). */
|
|
896 dir_static.d_ino = 1;
|
|
897
|
|
898 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3 +
|
|
899 dir_static.d_namlen - dir_static.d_namlen % 4;
|
|
900
|
|
901 {
|
|
902 DECLARE_EISTRING (found);
|
|
903 Bytecount namlen;
|
|
904
|
2526
|
905 if (mswindows_shortcuts_are_symlinks)
|
|
906 {
|
|
907 int len = qxestrlen (val);
|
|
908 if (len > 4 && !qxestrcasecmp_ascii (val + len - 4, ".LNK"))
|
|
909 {
|
|
910 /* If we've found a valid link, then chop off the .LNK ending */
|
|
911 DECLARE_EISTRING (linkname);
|
|
912 Ichar lastch;
|
|
913 Ibyte *resolved;
|
|
914
|
|
915 /* First check if link is valid */
|
|
916 PATHNAME_RESOLVE_LINKS (dir_pathname, resolved);
|
|
917 eicpy_rawz (linkname, resolved);
|
|
918 lastch = eigetch_char (linkname, eicharlen (linkname) - 1);
|
|
919 if (!IS_DIRECTORY_SEP (lastch))
|
|
920 eicat_ch (linkname, '\\');
|
|
921 eicat_rawz (linkname, val);
|
|
922 resolved = mswindows_read_link (eidata (linkname));
|
|
923 if (resolved)
|
|
924 {
|
|
925 xfree (resolved, Ibyte *);
|
|
926 len -= 4;
|
|
927 val[len] = '\0';
|
|
928 }
|
|
929 }
|
|
930 }
|
|
931
|
771
|
932 eicpy_rawz (found, val);
|
|
933 if (need_to_free)
|
1726
|
934 xfree (val, Ibyte *);
|
771
|
935
|
|
936 if (!NILP (Vmswindows_downcase_file_names))
|
|
937 eilwr (found);
|
|
938
|
|
939 namlen = min (eilen (found), sizeof (dir_static.d_name) - 1);
|
|
940 strncpy (dir_static.d_name, (char *) eidata (found), namlen);
|
|
941 dir_static.d_name[namlen] = '\0';
|
|
942 dir_static.d_namlen = (unsigned short) namlen;
|
|
943 }
|
|
944
|
|
945 return &dir_static;
|
|
946 }
|
|
947
|
|
948 static HANDLE
|
867
|
949 open_unc_volume (const Ibyte *path)
|
771
|
950 {
|
|
951 NETRESOURCEW nr;
|
|
952 HANDLE henum;
|
|
953 int result;
|
|
954
|
|
955 nr.dwScope = RESOURCE_GLOBALNET;
|
|
956 nr.dwType = RESOURCETYPE_DISK;
|
|
957 nr.dwDisplayType = RESOURCEDISPLAYTYPE_SERVER;
|
|
958 nr.dwUsage = RESOURCEUSAGE_CONTAINER;
|
|
959 nr.lpLocalName = NULL;
|
2526
|
960 PATHNAME_CONVERT_OUT (path, nr.lpRemoteName);
|
771
|
961 nr.lpComment = NULL;
|
|
962 nr.lpProvider = NULL;
|
|
963
|
|
964 result = qxeWNetOpenEnum (RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
|
|
965 RESOURCEUSAGE_CONNECTABLE, &nr, &henum);
|
|
966
|
|
967 if (result == NO_ERROR)
|
|
968 return henum;
|
|
969 else
|
|
970 return INVALID_HANDLE_VALUE;
|
|
971 }
|
|
972
|
867
|
973 static Ibyte *
|
2286
|
974 read_unc_volume (HANDLE UNUSED (henum))
|
771
|
975 {
|
1204
|
976 DWORD count;
|
771
|
977 int result;
|
|
978 Extbyte buf[16384];
|
867
|
979 Ibyte *ptr;
|
1204
|
980 DWORD bufsize = sizeof (buf);
|
771
|
981
|
|
982 count = 1;
|
|
983 /* #### we should just be querying the size and then allocating the
|
|
984 right amount, like for all similar API's. but the docs say this ?!
|
|
985
|
|
986 An application cannot set the lpBuffer parameter to NULL and
|
|
987 retrieve the required buffer size from the lpBufferSize
|
|
988 parameter. Instead, the application should allocate a buffer of a
|
|
989 reasonable size -- 16 kilobytes (K) is typical -- and use the value
|
|
990 of lpBufferSize for error detection.
|
|
991 */
|
|
992
|
|
993 result = qxeWNetEnumResource (wnet_enum_handle, &count, buf, &bufsize);
|
|
994 if (result != NO_ERROR)
|
|
995 return NULL;
|
|
996
|
|
997 /* WNetEnumResource returns \\resource\share...skip forward to "share". */
|
|
998 TSTR_TO_C_STRING (((LPNETRESOURCEW) buf)->lpRemoteName, ptr);
|
867
|
999 INC_IBYTEPTR (ptr);
|
|
1000 INC_IBYTEPTR (ptr);
|
|
1001 while (*ptr && !IS_DIRECTORY_SEP (itext_ichar (ptr)))
|
|
1002 INC_IBYTEPTR (ptr);
|
|
1003 INC_IBYTEPTR (ptr);
|
771
|
1004
|
|
1005 return qxestrdup (ptr);
|
|
1006 }
|
|
1007
|
|
1008 static int
|
|
1009 close_unc_volume (HANDLE henum)
|
|
1010 {
|
|
1011 if (henum != INVALID_HANDLE_VALUE)
|
|
1012 return WNetCloseEnum (henum) == NO_ERROR ? 0 : -1;
|
442
|
1013 else
|
|
1014 return -1;
|
428
|
1015 }
|
|
1016
|
771
|
1017 static DWORD
|
867
|
1018 unc_volume_file_attributes (const Ibyte *path)
|
428
|
1019 {
|
771
|
1020 HANDLE henum;
|
|
1021 DWORD attrs;
|
|
1022
|
|
1023 henum = open_unc_volume (path);
|
|
1024 if (henum == INVALID_HANDLE_VALUE)
|
|
1025 return -1;
|
|
1026
|
|
1027 attrs = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_DIRECTORY;
|
|
1028
|
|
1029 close_unc_volume (henum);
|
428
|
1030
|
771
|
1031 return attrs;
|
|
1032 }
|
|
1033
|
|
1034 int
|
867
|
1035 mswindows_access (const Ibyte *path, int mode)
|
771
|
1036 {
|
|
1037 DWORD attributes;
|
428
|
1038
|
771
|
1039 /* MSVC implementation doesn't recognize D_OK. */
|
|
1040 if (is_unc_volume (path))
|
|
1041 {
|
|
1042 attributes = unc_volume_file_attributes (path);
|
|
1043 if (attributes == -1)
|
|
1044 {
|
|
1045 errno = EACCES;
|
|
1046 return -1;
|
|
1047 }
|
428
|
1048 }
|
|
1049 else
|
|
1050 {
|
771
|
1051 Extbyte *pathext;
|
|
1052
|
2526
|
1053 PATHNAME_CONVERT_OUT (path, pathext);
|
771
|
1054 if ((attributes = qxeGetFileAttributes (pathext)) == -1)
|
|
1055 {
|
|
1056 /* Should try mapping GetLastError to errno; for now just indicate
|
|
1057 that path doesn't exist. */
|
|
1058 errno = EACCES;
|
|
1059 return -1;
|
|
1060 }
|
428
|
1061 }
|
1204
|
1062 if ((mode & X_OK) != 0 && !mswindows_is_executable (path))
|
771
|
1063 {
|
|
1064 errno = EACCES;
|
|
1065 return -1;
|
|
1066 }
|
|
1067 if ((mode & W_OK) != 0 && (attributes & FILE_ATTRIBUTE_READONLY) != 0)
|
428
|
1068 {
|
771
|
1069 errno = EACCES;
|
|
1070 return -1;
|
428
|
1071 }
|
771
|
1072 if ((mode & D_OK) != 0 && (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
|
1073 {
|
|
1074 errno = EACCES;
|
|
1075 return -1;
|
|
1076 }
|
|
1077 return 0;
|
428
|
1078 }
|
|
1079
|
771
|
1080 /* This only works on NTFS volumes, but is useful to have. */
|
|
1081 /* #### NT 5.0 has a function CreateHardLink to do this directly,
|
|
1082 and it may do more things. */
|
428
|
1083 int
|
867
|
1084 mswindows_link (const Ibyte *old, const Ibyte *new)
|
428
|
1085 {
|
771
|
1086 HANDLE fileh;
|
|
1087 int result = -1;
|
1204
|
1088 Extbyte *oldext;
|
771
|
1089
|
|
1090 if (old == NULL || new == NULL)
|
|
1091 {
|
|
1092 errno = ENOENT;
|
|
1093 return -1;
|
|
1094 }
|
|
1095
|
2526
|
1096 PATHNAME_CONVERT_OUT (old, oldext);
|
1204
|
1097 fileh = qxeCreateFile (oldext, 0, 0, NULL, OPEN_EXISTING,
|
771
|
1098 FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
|
1099 if (fileh != INVALID_HANDLE_VALUE)
|
|
1100 {
|
|
1101 int wlen;
|
|
1102 WCHAR *newuni;
|
|
1103
|
|
1104 /* Confusingly, the "alternate" stream name field does not apply
|
|
1105 when restoring a hard link, and instead contains the actual
|
|
1106 stream data for the link (ie. the name of the link to create).
|
|
1107 The WIN32_STREAM_ID structure before the cStreamName field is
|
|
1108 the stream header, which is then immediately followed by the
|
|
1109 stream data. */
|
|
1110
|
|
1111 struct
|
|
1112 {
|
|
1113 WIN32_STREAM_ID wid;
|
2421
|
1114 WCHAR wbuffer[_MAX_PATH]; /* extra space for link name */
|
771
|
1115 } data;
|
|
1116
|
|
1117 TO_EXTERNAL_FORMAT (C_STRING, new,
|
|
1118 ALLOCA, (newuni, wlen), Qmswindows_unicode);
|
2421
|
1119 if (wlen / sizeof (WCHAR) < _MAX_PATH)
|
771
|
1120 {
|
|
1121 LPVOID context = NULL;
|
|
1122 DWORD wbytes = 0;
|
428
|
1123
|
771
|
1124 wcscpy (data.wid.cStreamName, newuni);
|
|
1125 data.wid.dwStreamId = BACKUP_LINK;
|
|
1126 data.wid.dwStreamAttributes = 0;
|
|
1127 data.wid.Size.LowPart = wlen; /* in bytes, not chars! */
|
|
1128 data.wid.Size.HighPart = 0;
|
|
1129 data.wid.dwStreamNameSize = 0;
|
|
1130
|
|
1131 if (BackupWrite (fileh, (LPBYTE)&data,
|
|
1132 offsetof (WIN32_STREAM_ID, cStreamName)
|
|
1133 + data.wid.Size.LowPart,
|
|
1134 &wbytes, FALSE, FALSE, &context)
|
|
1135 && BackupWrite (fileh, NULL, 0, &wbytes, TRUE, FALSE, &context))
|
|
1136 {
|
|
1137 /* succeeded */
|
|
1138 result = 0;
|
|
1139 }
|
|
1140 else
|
|
1141 {
|
|
1142 /* Should try mapping GetLastError to errno; for now just
|
|
1143 indicate a general error (eg. links not supported). */
|
1242
|
1144 errno = EINVAL; /* perhaps EMLINK? */
|
771
|
1145 }
|
|
1146 }
|
|
1147
|
|
1148 CloseHandle (fileh);
|
|
1149 }
|
|
1150 else
|
|
1151 errno = ENOENT;
|
|
1152
|
|
1153 return result;
|
|
1154 }
|
|
1155
|
|
1156 /* sys_open() merged into sysdep.c sys_open() */
|
|
1157
|
|
1158 int
|
867
|
1159 mswindows_rename (const Ibyte *oldname, const Ibyte *newname)
|
771
|
1160 {
|
|
1161 int result;
|
867
|
1162 Ibyte *temp;
|
771
|
1163
|
|
1164 /* MoveFile on Windows 95 doesn't correctly change the short file name
|
428
|
1165 alias in a number of circumstances (it is not easy to predict when
|
|
1166 just by looking at oldname and newname, unfortunately). In these
|
|
1167 cases, renaming through a temporary name avoids the problem.
|
|
1168
|
771
|
1169 A second problem on Windows 95 is that renaming through a temp name when
|
428
|
1170 newname is uppercase fails (the final long name ends up in
|
|
1171 lowercase, although the short alias might be uppercase) UNLESS the
|
|
1172 long temp name is not 8.3.
|
|
1173
|
771
|
1174 So, on Windows 95 we always rename through a temp name, and we make sure
|
428
|
1175 the temp name has a long extension to ensure correct renaming. */
|
|
1176
|
771
|
1177 /* XEmacs: We sprintf() part of OLDNAME into part of OLDNAME + a number,
|
|
1178 so the following calculation should certainly be enough. */
|
428
|
1179
|
867
|
1180 temp = qxestrcpy (alloca_ibytes (2 * qxestrlen (oldname) + 100), oldname);
|
771
|
1181
|
|
1182 if (mswindows_windows9x_p)
|
428
|
1183 {
|
867
|
1184 Ibyte *o;
|
|
1185 Ibyte *p;
|
771
|
1186 int i = 0;
|
428
|
1187
|
771
|
1188 if (o = qxestrrchr (oldname, '\\'))
|
|
1189 o++;
|
|
1190 else
|
867
|
1191 o = (Ibyte *) oldname;
|
771
|
1192
|
|
1193 if (p = qxestrrchr (temp, '\\'))
|
428
|
1194 p++;
|
|
1195 else
|
|
1196 p = temp;
|
771
|
1197
|
|
1198 do
|
|
1199 {
|
|
1200 Extbyte *oldext, *tempext;
|
|
1201 /* Force temp name to require a manufactured 8.3 alias - this
|
|
1202 seems to make the second rename work properly. */
|
|
1203 qxesprintf (p, "_.%s.%u", o, i);
|
|
1204 i++;
|
2526
|
1205 PATHNAME_CONVERT_OUT (oldname, oldext);
|
|
1206 PATHNAME_CONVERT_OUT (temp, tempext);
|
771
|
1207 result = rename (oldext, tempext);
|
|
1208 }
|
|
1209 /* This loop must surely terminate! */
|
|
1210 while (result < 0 && errno == EEXIST);
|
|
1211 if (result < 0)
|
428
|
1212 return -1;
|
|
1213 }
|
|
1214
|
771
|
1215 /* Emulate Unix behaviour - newname is deleted if it already exists
|
428
|
1216 (at least if it is a file; don't do this for directories).
|
771
|
1217
|
|
1218 Since we mustn't do this if we are just changing the case of the
|
|
1219 file name (we would end up deleting the file we are trying to
|
|
1220 rename!), we let rename detect if the destination file already
|
|
1221 exists - that way we avoid the possible pitfalls of trying to
|
|
1222 determine ourselves whether two names really refer to the same
|
|
1223 file, which is not always possible in the general case. (Consider
|
|
1224 all the permutations of shared or subst'd drives, etc.) */
|
|
1225 {
|
|
1226 Extbyte *newext, *tempext;
|
|
1227
|
2526
|
1228 PATHNAME_CONVERT_OUT (newname, newext);
|
|
1229 PATHNAME_CONVERT_OUT (temp, tempext);
|
|
1230 if (XEUNICODE_P)
|
|
1231 {
|
|
1232 result = _wrename ((const wchar_t *) tempext,
|
|
1233 (const wchar_t *) newext);
|
|
1234 if (result < 0
|
|
1235 && (errno == EEXIST || errno == EACCES)
|
|
1236 && _wchmod ((const wchar_t *) newext, 0666) == 0
|
|
1237 && _wunlink ((const wchar_t *) newext) == 0)
|
|
1238 result = _wrename ((const wchar_t *) tempext,
|
|
1239 (const wchar_t *) newext);
|
|
1240 }
|
|
1241 else
|
|
1242 {
|
|
1243 result = rename (tempext, newext);
|
|
1244 if (result < 0
|
|
1245 && (errno == EEXIST || errno == EACCES)
|
|
1246 && _chmod (newext, 0666) == 0
|
|
1247 && _unlink (newext) == 0)
|
|
1248 result = rename (tempext, newext);
|
|
1249 }
|
771
|
1250 }
|
|
1251
|
|
1252 return result;
|
|
1253 }
|
428
|
1254
|
771
|
1255 int
|
867
|
1256 mswindows_unlink (const Ibyte *path)
|
771
|
1257 {
|
|
1258 Extbyte *pathout;
|
|
1259
|
2526
|
1260 PATHNAME_CONVERT_OUT (path, pathout);
|
771
|
1261 /* On Unix, unlink works without write permission. */
|
2526
|
1262 if (XEUNICODE_P)
|
|
1263 {
|
|
1264 _wchmod ((const wchar_t *) pathout, 0666);
|
|
1265 return _wunlink ((const wchar_t *) pathout);
|
|
1266 }
|
|
1267 else
|
|
1268 {
|
|
1269 _chmod (pathout, 0666);
|
|
1270 return _unlink (pathout);
|
|
1271 }
|
428
|
1272 }
|
|
1273
|
|
1274 static FILETIME utc_base_ft;
|
592
|
1275 static long double utc_base;
|
440
|
1276 static int init = 0;
|
771
|
1277 static LARGE_INTEGER utc_base_li;
|
440
|
1278
|
771
|
1279 /* XEmacs: We seem to have a new definition of
|
|
1280 mswindows_convert_time(), although I'm not sure why. --ben */
|
428
|
1281
|
|
1282 time_t
|
771
|
1283 mswindows_convert_time (FILETIME uft)
|
440
|
1284 {
|
|
1285 time_t ret;
|
|
1286 #ifndef MAXLONGLONG
|
|
1287 SYSTEMTIME st;
|
|
1288 struct tm t;
|
|
1289 FILETIME ft;
|
|
1290 TIME_ZONE_INFORMATION tzi;
|
|
1291 DWORD tzid;
|
|
1292 #else
|
|
1293 LARGE_INTEGER lft;
|
|
1294 #endif
|
|
1295
|
|
1296 if (!init)
|
|
1297 {
|
|
1298 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
|
|
1299 SYSTEMTIME st;
|
|
1300
|
|
1301 st.wYear = 1970;
|
|
1302 st.wMonth = 1;
|
|
1303 st.wDay = 1;
|
|
1304 st.wHour = 0;
|
|
1305 st.wMinute = 0;
|
|
1306 st.wSecond = 0;
|
|
1307 st.wMilliseconds = 0;
|
|
1308
|
|
1309 SystemTimeToFileTime (&st, &utc_base_ft);
|
|
1310
|
|
1311 utc_base_li.LowPart = utc_base_ft.dwLowDateTime;
|
|
1312 utc_base_li.HighPart = utc_base_ft.dwHighDateTime;
|
|
1313
|
|
1314 init = 1;
|
|
1315 }
|
|
1316
|
|
1317 #ifdef MAXLONGLONG
|
|
1318
|
|
1319 /* On a compiler that supports long integers, do it the easy way */
|
|
1320 lft.LowPart = uft.dwLowDateTime;
|
|
1321 lft.HighPart = uft.dwHighDateTime;
|
|
1322 ret = (time_t) ((lft.QuadPart - utc_base_li.QuadPart) / 10000000);
|
|
1323
|
|
1324 #else
|
|
1325
|
|
1326 /* Do it the hard way using mktime. */
|
|
1327 FileTimeToLocalFileTime(&uft, &ft);
|
|
1328 FileTimeToSystemTime (&ft, &st);
|
|
1329 tzid = GetTimeZoneInformation (&tzi);
|
|
1330 t.tm_year = st.wYear - 1900;
|
|
1331 t.tm_mon = st.wMonth - 1;
|
|
1332 t.tm_mday = st.wDay;
|
|
1333 t.tm_hour = st.wHour;
|
|
1334 t.tm_min = st.wMinute;
|
|
1335 t.tm_sec = st.wSecond;
|
|
1336 t.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT);
|
|
1337 /* st.wMilliseconds not applicable */
|
|
1338 ret = mktime(&t);
|
|
1339 if (ret == -1)
|
|
1340 {
|
|
1341 ret = 0;
|
|
1342 }
|
|
1343
|
|
1344 #endif
|
|
1345
|
|
1346 return ret;
|
|
1347 }
|
428
|
1348
|
771
|
1349 static void
|
428
|
1350 convert_from_time_t (time_t time, FILETIME * pft)
|
|
1351 {
|
|
1352 long double tmp;
|
|
1353
|
|
1354 if (!init)
|
|
1355 {
|
|
1356 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
|
|
1357 SYSTEMTIME st;
|
|
1358
|
|
1359 st.wYear = 1970;
|
|
1360 st.wMonth = 1;
|
|
1361 st.wDay = 1;
|
|
1362 st.wHour = 0;
|
|
1363 st.wMinute = 0;
|
|
1364 st.wSecond = 0;
|
|
1365 st.wMilliseconds = 0;
|
|
1366
|
|
1367 SystemTimeToFileTime (&st, &utc_base_ft);
|
|
1368 utc_base = (long double) utc_base_ft.dwHighDateTime
|
|
1369 * 4096 * 1024 * 1024 + utc_base_ft.dwLowDateTime;
|
|
1370 init = 1;
|
|
1371 }
|
|
1372
|
|
1373 /* time in 100ns units since 1-Jan-1601 */
|
|
1374 tmp = (long double) time * 1e7 + utc_base;
|
|
1375 pft->dwHighDateTime = (DWORD) (tmp / (4096.0 * 1024 * 1024));
|
771
|
1376 pft->dwLowDateTime = (DWORD) (tmp - (4096.0 * 1024 * 1024) *
|
|
1377 pft->dwHighDateTime);
|
428
|
1378 }
|
|
1379
|
|
1380 #if 0
|
771
|
1381 /* A comment from Emacs 20.6:
|
|
1382
|
|
1383 No reason to keep this; faking inode values either by hashing or even
|
428
|
1384 using the file index from GetInformationByHandle, is not perfect and
|
|
1385 so by default Emacs doesn't use the inode values on Windows.
|
|
1386 Instead, we now determine file-truename correctly (except for
|
|
1387 possible drive aliasing etc). */
|
|
1388
|
771
|
1389 /* XEmacs: Removed the fake-inodes code here, which was if 0'd out.
|
|
1390 If you want it, look in w32.c in Emacs 20.6. */
|
428
|
1391 #endif
|
|
1392
|
442
|
1393 /* #### aichner@ecf.teradyne.com reported that with the library
|
|
1394 provided stat/fstat, (file-exist "d:\\tmp\\") =>> nil,
|
|
1395 (file-exist "d:\\tmp") =>> t, when d:\tmp exists. Whenever
|
|
1396 we opt to use non-encapsulated stat(), this should serve as
|
|
1397 a compatibility test. --kkm */
|
440
|
1398
|
771
|
1399 /* Provide fstat and utime as well as stat for consistent handling of
|
|
1400 file timestamps. */
|
442
|
1401 int
|
771
|
1402 mswindows_fstat (int desc, struct stat *buf)
|
432
|
1403 {
|
448
|
1404 HANDLE fh = (HANDLE) _get_osfhandle (desc);
|
|
1405 BY_HANDLE_FILE_INFORMATION info;
|
|
1406 DWORD fake_inode;
|
|
1407 int permission;
|
|
1408
|
|
1409 switch (GetFileType (fh) & ~FILE_TYPE_REMOTE)
|
432
|
1410 {
|
448
|
1411 case FILE_TYPE_DISK:
|
|
1412 buf->st_mode = _S_IFREG;
|
|
1413 if (!GetFileInformationByHandle (fh, &info))
|
|
1414 {
|
|
1415 errno = EACCES;
|
|
1416 return -1;
|
|
1417 }
|
|
1418 break;
|
|
1419 case FILE_TYPE_PIPE:
|
|
1420 buf->st_mode = _S_IFIFO;
|
|
1421 goto non_disk;
|
|
1422 case FILE_TYPE_CHAR:
|
|
1423 case FILE_TYPE_UNKNOWN:
|
|
1424 default:
|
|
1425 buf->st_mode = _S_IFCHR;
|
|
1426 non_disk:
|
|
1427 memset (&info, 0, sizeof (info));
|
|
1428 info.dwFileAttributes = 0;
|
|
1429 info.ftCreationTime = utc_base_ft;
|
|
1430 info.ftLastAccessTime = utc_base_ft;
|
|
1431 info.ftLastWriteTime = utc_base_ft;
|
|
1432 }
|
|
1433
|
|
1434 if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
1435 {
|
|
1436 buf->st_mode = _S_IFDIR;
|
|
1437 buf->st_nlink = 2; /* doesn't really matter */
|
|
1438 fake_inode = 0; /* this doesn't either I think */
|
432
|
1439 }
|
|
1440 else
|
|
1441 {
|
462
|
1442 buf->st_nlink = (short) info.nNumberOfLinks;
|
448
|
1443 /* Might as well use file index to fake inode values, but this
|
|
1444 is not guaranteed to be unique unless we keep a handle open
|
|
1445 all the time (even then there are situations where it is
|
|
1446 not unique). Reputedly, there are at most 48 bits of info
|
|
1447 (on NTFS, presumably less on FAT). */
|
|
1448 fake_inode = info.nFileIndexLow ^ info.nFileIndexHigh;
|
432
|
1449 }
|
448
|
1450
|
|
1451 /* MSVC defines _ino_t to be short; other libc's might not. */
|
|
1452 if (sizeof (buf->st_ino) == 2)
|
462
|
1453 buf->st_ino = (unsigned short) (fake_inode ^ (fake_inode >> 16));
|
448
|
1454 else
|
462
|
1455 buf->st_ino = (unsigned short) fake_inode;
|
448
|
1456
|
|
1457 /* consider files to belong to current user */
|
|
1458 buf->st_uid = 0;
|
|
1459 buf->st_gid = 0;
|
|
1460
|
|
1461 buf->st_dev = info.dwVolumeSerialNumber;
|
|
1462 buf->st_rdev = info.dwVolumeSerialNumber;
|
|
1463
|
|
1464 buf->st_size = info.nFileSizeLow;
|
|
1465
|
|
1466 /* Convert timestamps to Unix format. */
|
771
|
1467 buf->st_mtime = mswindows_convert_time (info.ftLastWriteTime);
|
|
1468 buf->st_atime = mswindows_convert_time (info.ftLastAccessTime);
|
448
|
1469 if (buf->st_atime == 0) buf->st_atime = buf->st_mtime;
|
771
|
1470 buf->st_ctime = mswindows_convert_time (info.ftCreationTime);
|
448
|
1471 if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime;
|
|
1472
|
|
1473 /* determine rwx permissions */
|
|
1474 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
|
|
1475 permission = _S_IREAD;
|
|
1476 else
|
|
1477 permission = _S_IREAD | _S_IWRITE;
|
|
1478
|
|
1479 if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
1480 permission |= _S_IEXEC;
|
771
|
1481 else
|
|
1482 {
|
|
1483 #if 0 /* no way of knowing the filename */
|
1204
|
1484 if (mswindows_is_executable (name))
|
771
|
1485 permission |= _S_IEXEC;
|
|
1486 #endif
|
|
1487 }
|
448
|
1488
|
|
1489 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
|
|
1490
|
|
1491 return 0;
|
432
|
1492 }
|
|
1493
|
428
|
1494 /* MSVC stat function can't cope with UNC names and has other bugs, so
|
|
1495 replace it with our own. This also allows us to calculate consistent
|
|
1496 inode values without hacks in the main Emacs code. */
|
|
1497 int
|
867
|
1498 mswindows_stat (const Ibyte *path, struct stat *buf)
|
428
|
1499 {
|
867
|
1500 Ibyte *name, *r;
|
771
|
1501 WIN32_FIND_DATAW wfd;
|
428
|
1502 HANDLE fh;
|
|
1503 DWORD fake_inode;
|
|
1504 int permission;
|
771
|
1505 Bytecount len;
|
428
|
1506 int rootdir = FALSE;
|
771
|
1507 Extbyte *nameext;
|
819
|
1508 int errm;
|
428
|
1509
|
|
1510 if (path == NULL || buf == NULL)
|
|
1511 {
|
|
1512 errno = EFAULT;
|
|
1513 return -1;
|
|
1514 }
|
|
1515
|
867
|
1516 name = qxestrcpy (alloca_ibytes (qxestrlen (path) + 10), path);
|
819
|
1517 errm = SetErrorMode (SEM_FAILCRITICALERRORS
|
|
1518 | SEM_NOOPENFILEERRORBOX);
|
771
|
1519
|
|
1520 get_volume_info (name, &path);
|
|
1521 /* must be valid filename, no wild cards or other invalid characters */
|
|
1522 if (qxestrpbrk (name, "*?|<>\""))
|
428
|
1523 {
|
|
1524 errno = ENOENT;
|
|
1525 return -1;
|
|
1526 }
|
|
1527
|
771
|
1528 /* If name is "c:/.." or "/.." then stat "c:/" or "/". */
|
|
1529 r = IS_DEVICE_SEP (name[1]) ? &name[2] : name;
|
|
1530 if (IS_DIRECTORY_SEP (r[0]) && r[1] == '.' && r[2] == '.' && r[3] == '\0')
|
|
1531 {
|
|
1532 r[1] = r[2] = '\0';
|
|
1533 }
|
|
1534
|
428
|
1535 /* Remove trailing directory separator, unless name is the root
|
|
1536 directory of a drive or UNC volume in which case ensure there
|
|
1537 is a trailing separator. */
|
771
|
1538 len = qxestrlen (name);
|
428
|
1539 rootdir = (path >= name + len - 1
|
|
1540 && (IS_DIRECTORY_SEP (*path) || *path == 0));
|
771
|
1541
|
|
1542 if (is_unc_volume (name))
|
|
1543 {
|
|
1544 DWORD attrs = unc_volume_file_attributes (name);
|
|
1545
|
|
1546 if (attrs == -1)
|
|
1547 return -1;
|
428
|
1548
|
771
|
1549 memset (&wfd, 0, sizeof (wfd));
|
|
1550 wfd.dwFileAttributes = attrs;
|
|
1551 wfd.ftCreationTime = utc_base_ft;
|
|
1552 wfd.ftLastAccessTime = utc_base_ft;
|
|
1553 wfd.ftLastWriteTime = utc_base_ft;
|
|
1554 /* XEmacs deleted: strcpy (wfd.cFileName, name);
|
|
1555 Not used later on. */
|
|
1556 }
|
|
1557 else if (rootdir)
|
428
|
1558 {
|
|
1559 if (!IS_DIRECTORY_SEP (name[len-1]))
|
867
|
1560 qxestrcat (name, (Ibyte *) "\\");
|
2526
|
1561 /* File has already been resolved and we don't want to do it again
|
|
1562 in case of lstat() */
|
771
|
1563 C_STRING_TO_TSTR (name, nameext);
|
|
1564 if (qxeGetDriveType (nameext) < 2)
|
428
|
1565 {
|
819
|
1566 SetErrorMode (errm);
|
428
|
1567 errno = ENOENT;
|
|
1568 return -1;
|
|
1569 }
|
|
1570 memset (&wfd, 0, sizeof (wfd));
|
|
1571 wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
|
|
1572 wfd.ftCreationTime = utc_base_ft;
|
|
1573 wfd.ftLastAccessTime = utc_base_ft;
|
|
1574 wfd.ftLastWriteTime = utc_base_ft;
|
771
|
1575 /* XEmacs deleted: strcpy (wfd.cFileName, name);
|
|
1576 Not used later on. */
|
428
|
1577 }
|
|
1578 else
|
|
1579 {
|
|
1580 if (IS_DIRECTORY_SEP (name[len-1]))
|
|
1581 name[len - 1] = 0;
|
|
1582
|
|
1583 /* (This is hacky, but helps when doing file completions on
|
|
1584 network drives.) Optimize by using information available from
|
|
1585 active readdir if possible. */
|
771
|
1586 if (dir_pathname)
|
|
1587 {
|
|
1588 len = qxestrlen (dir_pathname);
|
|
1589 if (len && IS_DIRECTORY_SEP (dir_pathname[len-1]))
|
|
1590 len--;
|
|
1591 }
|
|
1592 if (dir_find_handle != INVALID_HANDLE_VALUE
|
|
1593 && dir_pathname
|
801
|
1594 && qxestrncasecmp_i18n (dir_pathname, name, len) == 0
|
771
|
1595 && IS_DIRECTORY_SEP (name[len])
|
|
1596 && qxestrcasecmp_i18n (name + len + 1,
|
867
|
1597 (Ibyte *) dir_static.d_name) == 0)
|
428
|
1598 {
|
|
1599 /* This was the last entry returned by readdir. */
|
|
1600 wfd = dir_find_data;
|
|
1601 }
|
|
1602 else
|
|
1603 {
|
771
|
1604 C_STRING_TO_TSTR (name, nameext);
|
|
1605 fh = qxeFindFirstFile (nameext, &wfd);
|
|
1606 if (fh == INVALID_HANDLE_VALUE)
|
|
1607 {
|
819
|
1608 SetErrorMode (errm);
|
771
|
1609 errno = ENOENT;
|
|
1610 return -1;
|
|
1611 }
|
|
1612 FindClose (fh);
|
|
1613 /* XEmacs: Don't need to convert wfd.cFileName because
|
|
1614 not used later on. */
|
428
|
1615 }
|
|
1616 }
|
|
1617
|
|
1618 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
1619 {
|
|
1620 buf->st_mode = _S_IFDIR;
|
|
1621 buf->st_nlink = 2; /* doesn't really matter */
|
|
1622 fake_inode = 0; /* this doesn't either I think */
|
|
1623 }
|
771
|
1624 else
|
428
|
1625 {
|
771
|
1626 if (!NILP (Vmswindows_get_true_file_attributes))
|
2526
|
1627 /* File has already been resolved and we don't want to do it again
|
|
1628 in case of lstat() */
|
771
|
1629 C_STRING_TO_TSTR (name, nameext);
|
|
1630 if (!NILP (Vmswindows_get_true_file_attributes)
|
|
1631 /* No access rights required to get info. */
|
|
1632 && (fh = qxeCreateFile (nameext, 0, 0, NULL, OPEN_EXISTING, 0, NULL))
|
|
1633 != INVALID_HANDLE_VALUE)
|
|
1634 {
|
2526
|
1635 /* This is more accurate in terms of getting the correct number
|
771
|
1636 of links, but is quite slow (it is noticable when Emacs is
|
|
1637 making a list of file name completions). */
|
|
1638 BY_HANDLE_FILE_INFORMATION info;
|
428
|
1639
|
771
|
1640 if (GetFileInformationByHandle (fh, &info))
|
|
1641 {
|
|
1642 buf->st_nlink = (short) info.nNumberOfLinks;
|
|
1643 /* Might as well use file index to fake inode values, but this
|
|
1644 is not guaranteed to be unique unless we keep a handle open
|
|
1645 all the time (even then there are situations where it is
|
|
1646 not unique). Reputedly, there are at most 48 bits of info
|
|
1647 (on NTFS, presumably less on FAT). */
|
|
1648 fake_inode = info.nFileIndexLow ^ info.nFileIndexHigh;
|
|
1649 }
|
|
1650 else
|
|
1651 {
|
|
1652 buf->st_nlink = 1;
|
|
1653 fake_inode = 0;
|
|
1654 }
|
428
|
1655
|
|
1656 switch (GetFileType (fh))
|
|
1657 {
|
|
1658 case FILE_TYPE_DISK:
|
|
1659 buf->st_mode = _S_IFREG;
|
|
1660 break;
|
|
1661 case FILE_TYPE_PIPE:
|
|
1662 buf->st_mode = _S_IFIFO;
|
|
1663 break;
|
|
1664 case FILE_TYPE_CHAR:
|
|
1665 case FILE_TYPE_UNKNOWN:
|
|
1666 default:
|
|
1667 buf->st_mode = _S_IFCHR;
|
|
1668 }
|
|
1669 CloseHandle (fh);
|
|
1670 }
|
|
1671 else
|
|
1672 {
|
771
|
1673 /* Don't bother to make this information more accurate. */
|
|
1674 buf->st_mode = _S_IFREG;
|
|
1675 buf->st_nlink = 1;
|
|
1676 fake_inode = 0;
|
428
|
1677 }
|
2526
|
1678
|
|
1679 if (mswindows_shortcuts_are_symlinks &&
|
|
1680 buf->st_mode == _S_IFREG)
|
|
1681 {
|
|
1682 len = qxestrlen (name);
|
|
1683 if (len > 4 && !qxestrcasecmp_ascii (name + len - 4, ".LNK"))
|
|
1684 {
|
|
1685 /* check if link is valid */
|
|
1686 Ibyte *resolved = mswindows_read_link (name);
|
|
1687 if (resolved)
|
|
1688 {
|
|
1689 xfree (resolved, Ibyte *);
|
|
1690 buf->st_mode = S_IFLNK;
|
|
1691 }
|
|
1692 }
|
|
1693 }
|
428
|
1694 }
|
|
1695
|
819
|
1696 SetErrorMode (errm);
|
|
1697
|
428
|
1698 #if 0
|
771
|
1699 /* XEmacs: Removed the fake-inodes code here, which was if 0'd out.
|
|
1700 If you want it, look in w32.c in Emacs 20.6. */
|
428
|
1701 #endif
|
|
1702
|
771
|
1703 /* MSVC defines _ino_t to be short; other libc's might not. */
|
|
1704 if (sizeof (buf->st_ino) == 2)
|
|
1705 buf->st_ino = (unsigned short) (fake_inode ^ (fake_inode >> 16));
|
|
1706 else
|
|
1707 buf->st_ino = (unsigned short) fake_inode;
|
428
|
1708
|
|
1709 /* consider files to belong to current user */
|
771
|
1710 buf->st_uid = the_passwd.pw_uid;
|
|
1711 buf->st_gid = the_passwd.pw_gid;
|
428
|
1712
|
771
|
1713 /* volume_info is set by get_volume_info */
|
428
|
1714 buf->st_dev = volume_info.serialnum;
|
|
1715 buf->st_rdev = volume_info.serialnum;
|
|
1716
|
771
|
1717
|
428
|
1718 buf->st_size = wfd.nFileSizeLow;
|
|
1719
|
|
1720 /* Convert timestamps to Unix format. */
|
771
|
1721 buf->st_mtime = mswindows_convert_time (wfd.ftLastWriteTime);
|
|
1722 buf->st_atime = mswindows_convert_time (wfd.ftLastAccessTime);
|
428
|
1723 if (buf->st_atime == 0) buf->st_atime = buf->st_mtime;
|
771
|
1724 buf->st_ctime = mswindows_convert_time (wfd.ftCreationTime);
|
428
|
1725 if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime;
|
|
1726
|
|
1727 /* determine rwx permissions */
|
|
1728 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
|
|
1729 permission = _S_IREAD;
|
|
1730 else
|
|
1731 permission = _S_IREAD | _S_IWRITE;
|
|
1732
|
|
1733 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
1734 permission |= _S_IEXEC;
|
1204
|
1735 else if (mswindows_is_executable (name))
|
771
|
1736 permission |= _S_IEXEC;
|
428
|
1737
|
|
1738 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
|
|
1739
|
|
1740 return 0;
|
|
1741 }
|
|
1742
|
|
1743 int
|
771
|
1744 mswindows_utime (Lisp_Object path, struct utimbuf *times)
|
428
|
1745 {
|
771
|
1746 /* #### Supposedly we're providing this because standard utime()
|
|
1747 might not work; or at the very least to get consistent results
|
|
1748 since we replace other time-handling routines in stat. But out
|
|
1749 replacement doesn't seem to work, probably due to some subtle bug
|
|
1750 in this routine, which should be investigated eventually. So for
|
|
1751 the moment, we just use utime(), which conceivably might be
|
|
1752 slightly off in comparison with our own routines? Seems strange,
|
|
1753 and so far no problems seen. --ben */
|
428
|
1754
|
771
|
1755 struct utimbuf deftime;
|
|
1756 #if 0
|
|
1757 HANDLE fh;
|
|
1758 #endif
|
|
1759 static FILETIME mtime;
|
|
1760 static FILETIME atime;
|
|
1761 Extbyte *filename;
|
428
|
1762
|
771
|
1763 if (times == NULL)
|
428
|
1764 {
|
771
|
1765 deftime.modtime = deftime.actime = time (NULL);
|
|
1766 times = &deftime;
|
428
|
1767 }
|
|
1768
|
2526
|
1769 LISP_PATHNAME_CONVERT_OUT (path, filename);
|
771
|
1770 /* APA: SetFileTime fails to set mtime correctly (always 1-Jan-1970) */
|
|
1771 #if 0
|
|
1772 /* Need write access to set times. */
|
|
1773 fh = qxeCreateFile (filename, GENERIC_WRITE,
|
|
1774 FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
1775 0, OPEN_EXISTING, 0, NULL);
|
|
1776 if (fh)
|
|
1777 {
|
|
1778 convert_from_time_t (times->actime, &atime);
|
|
1779 convert_from_time_t (times->modtime, &mtime);
|
|
1780 if (!SetFileTime (fh, NULL, &atime, &mtime))
|
|
1781 {
|
|
1782 CloseHandle (fh);
|
|
1783 errno = EACCES;
|
|
1784 return -1;
|
|
1785 }
|
|
1786 CloseHandle (fh);
|
|
1787 }
|
|
1788 else
|
|
1789 {
|
|
1790 errno = EINVAL;
|
|
1791 return -1;
|
|
1792 }
|
|
1793 return 0;
|
|
1794 #else
|
|
1795 {
|
|
1796 struct _utimbuf newtimes;
|
|
1797
|
|
1798 newtimes.actime = times->actime;
|
|
1799 newtimes.modtime = times->modtime;
|
|
1800
|
|
1801 if (XEUNICODE_P)
|
|
1802 return _wutime ((const wchar_t *) filename, &newtimes);
|
|
1803 else
|
|
1804 return _utime (filename, &newtimes);
|
|
1805 }
|
|
1806 #endif
|
|
1807 }
|
|
1808
|
867
|
1809 Ibyte *
|
771
|
1810 mswindows_getdcwd (int drivelet)
|
|
1811 {
|
|
1812 Extbyte *cwdext;
|
867
|
1813 Ibyte *cwd;
|
771
|
1814
|
|
1815 if (XEUNICODE_P)
|
|
1816 cwdext = (Extbyte *) _wgetdcwd (drivelet, NULL, 0);
|
|
1817 else
|
|
1818 cwdext = _getdcwd (drivelet, NULL, 0);
|
|
1819 TSTR_TO_C_STRING_MALLOC (cwdext, cwd);
|
1726
|
1820 xfree (cwdext, Extbyte *);
|
771
|
1821 return cwd;
|
428
|
1822 }
|
|
1823
|
442
|
1824
|
|
1825 /*--------------------------------------------------------------------*/
|
|
1826 /* Memory-mapped files */
|
|
1827 /*--------------------------------------------------------------------*/
|
|
1828
|
428
|
1829 int
|
867
|
1830 open_input_file (file_data *p_file, const Ibyte *filename)
|
428
|
1831 {
|
442
|
1832 /* Synched with FSF 20.6. We fixed some warnings. */
|
428
|
1833 HANDLE file;
|
|
1834 HANDLE file_mapping;
|
771
|
1835 void *file_base;
|
428
|
1836 DWORD size, upper_size;
|
771
|
1837 Extbyte *fileext;
|
428
|
1838
|
2526
|
1839 PATHNAME_CONVERT_OUT (filename, fileext);
|
771
|
1840
|
|
1841 file = qxeCreateFile (fileext, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
1842 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
428
|
1843 if (file == INVALID_HANDLE_VALUE)
|
|
1844 return FALSE;
|
|
1845
|
|
1846 size = GetFileSize (file, &upper_size);
|
771
|
1847 file_mapping = qxeCreateFileMapping (file, NULL, PAGE_READONLY,
|
|
1848 0, size, NULL);
|
428
|
1849 if (!file_mapping)
|
|
1850 return FALSE;
|
|
1851
|
|
1852 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
|
|
1853 if (file_base == 0)
|
|
1854 return FALSE;
|
|
1855
|
771
|
1856 p_file->name = filename;
|
442
|
1857 p_file->size = size;
|
|
1858 p_file->file = file;
|
|
1859 p_file->file_mapping = file_mapping;
|
771
|
1860 p_file->file_base = file_base;
|
442
|
1861
|
|
1862 return TRUE;
|
|
1863 }
|
|
1864
|
|
1865 int
|
867
|
1866 open_output_file (file_data *p_file, const Ibyte *filename,
|
771
|
1867 unsigned long size)
|
442
|
1868 {
|
|
1869 /* Synched with FSF 20.6. We fixed some warnings. */
|
|
1870 HANDLE file;
|
|
1871 HANDLE file_mapping;
|
771
|
1872 void *file_base;
|
|
1873 Extbyte *fileext;
|
442
|
1874
|
2526
|
1875 PATHNAME_CONVERT_OUT (filename, fileext);
|
771
|
1876
|
|
1877 file = qxeCreateFile (fileext, GENERIC_READ | GENERIC_WRITE, 0, NULL,
|
|
1878 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
442
|
1879 if (file == INVALID_HANDLE_VALUE)
|
|
1880 return FALSE;
|
|
1881
|
771
|
1882 file_mapping = qxeCreateFileMapping (file, NULL, PAGE_READWRITE,
|
|
1883 0, size, NULL);
|
442
|
1884 if (!file_mapping)
|
|
1885 return FALSE;
|
|
1886
|
|
1887 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
|
|
1888 if (file_base == NULL)
|
|
1889 return FALSE;
|
|
1890
|
|
1891 p_file->name = filename;
|
428
|
1892 p_file->size = size;
|
|
1893 p_file->file = file;
|
|
1894 p_file->file_mapping = file_mapping;
|
771
|
1895 p_file->file_base = file_base;
|
428
|
1896
|
|
1897 return TRUE;
|
|
1898 }
|
|
1899
|
442
|
1900 #if 1 /* !defined(MINGW) */
|
|
1901 /* Return pointer to section header for section containing the given
|
|
1902 relative virtual address. */
|
|
1903 static IMAGE_SECTION_HEADER *
|
771
|
1904 rva_to_section (DWORD rva, IMAGE_NT_HEADERS *nt_header)
|
442
|
1905 {
|
|
1906 /* Synched with FSF 20.6. We added MINGW stuff. */
|
|
1907 PIMAGE_SECTION_HEADER section;
|
|
1908 int i;
|
|
1909
|
|
1910 section = IMAGE_FIRST_SECTION (nt_header);
|
|
1911
|
|
1912 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
|
|
1913 {
|
|
1914 /* Some linkers (eg. the NT SDK linker I believe) swapped the
|
|
1915 meaning of these two values - or rather, they ignored
|
|
1916 VirtualSize entirely and always set it to zero. This affects
|
|
1917 some very old exes (eg. gzip dated Dec 1993). Since
|
|
1918 mswindows_executable_type relies on this function to work reliably,
|
|
1919 we need to cope with this. */
|
|
1920 DWORD real_size = max (section->SizeOfRawData,
|
|
1921 section->Misc.VirtualSize);
|
|
1922 if (rva >= section->VirtualAddress
|
|
1923 && rva < section->VirtualAddress + real_size)
|
|
1924 return section;
|
|
1925 section++;
|
|
1926 }
|
|
1927 return NULL;
|
|
1928 }
|
|
1929 #endif
|
|
1930
|
|
1931 void
|
867
|
1932 mswindows_executable_type (const Ibyte *filename, int *is_dos_app,
|
771
|
1933 int *is_cygnus_app)
|
442
|
1934 {
|
|
1935 /* Synched with FSF 20.6. We added MINGW stuff and casts. */
|
|
1936 file_data executable;
|
867
|
1937 Ibyte *p;
|
442
|
1938
|
|
1939 /* Default values in case we can't tell for sure. */
|
|
1940 *is_dos_app = FALSE;
|
|
1941 *is_cygnus_app = FALSE;
|
|
1942
|
|
1943 if (!open_input_file (&executable, filename))
|
|
1944 return;
|
|
1945
|
771
|
1946 p = qxestrrchr (filename, '.');
|
442
|
1947
|
|
1948 /* We can only identify DOS .com programs from the extension. */
|
2367
|
1949 if (p && qxestrcasecmp_ascii (p, ".com") == 0)
|
442
|
1950 *is_dos_app = TRUE;
|
2367
|
1951 else if (p && (qxestrcasecmp_ascii (p, ".bat") == 0 ||
|
|
1952 qxestrcasecmp_ascii (p, ".cmd") == 0))
|
442
|
1953 {
|
|
1954 /* A DOS shell script - it appears that CreateProcess is happy to
|
|
1955 accept this (somewhat surprisingly); presumably it looks at
|
|
1956 COMSPEC to determine what executable to actually invoke.
|
|
1957 Therefore, we have to do the same here as well. */
|
|
1958 /* Actually, I think it uses the program association for that
|
|
1959 extension, which is defined in the registry. */
|
|
1960 p = egetenv ("COMSPEC");
|
|
1961 if (p)
|
|
1962 mswindows_executable_type (p, is_dos_app, is_cygnus_app);
|
|
1963 }
|
|
1964 else
|
|
1965 {
|
|
1966 /* Look for DOS .exe signature - if found, we must also check that
|
|
1967 it isn't really a 16- or 32-bit Windows exe, since both formats
|
|
1968 start with a DOS program stub. Note that 16-bit Windows
|
|
1969 executables use the OS/2 1.x format. */
|
|
1970
|
|
1971 #if 0 /* defined( MINGW ) */
|
771
|
1972 /* mingw doesn't have enough headers to detect cygwin
|
442
|
1973 apps, just do what we can. */
|
771
|
1974 FILHDR *exe_header;
|
442
|
1975
|
771
|
1976 exe_header = (FILHDR *) executable.file_base;
|
442
|
1977 if (exe_header->e_magic != DOSMAGIC)
|
|
1978 goto unwind;
|
|
1979
|
771
|
1980 if ((char *) exe_header->e_lfanew > (char *) executable.size)
|
442
|
1981 {
|
|
1982 /* Some dos headers (pkunzip) have bogus e_lfanew fields. */
|
|
1983 *is_dos_app = TRUE;
|
|
1984 }
|
|
1985 else if (exe_header->nt_signature != NT_SIGNATURE)
|
|
1986 {
|
|
1987 *is_dos_app = TRUE;
|
|
1988 }
|
|
1989 #else
|
771
|
1990 IMAGE_DOS_HEADER *dos_header;
|
|
1991 IMAGE_NT_HEADERS *nt_header;
|
442
|
1992
|
|
1993 dos_header = (PIMAGE_DOS_HEADER) executable.file_base;
|
|
1994 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
|
|
1995 goto unwind;
|
|
1996
|
771
|
1997 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header +
|
|
1998 dos_header->e_lfanew);
|
442
|
1999
|
771
|
2000 if ((char *) nt_header > (char *) dos_header + executable.size)
|
442
|
2001 {
|
|
2002 /* Some dos headers (pkunzip) have bogus e_lfanew fields. */
|
|
2003 *is_dos_app = TRUE;
|
|
2004 }
|
|
2005 else if (nt_header->Signature != IMAGE_NT_SIGNATURE &&
|
|
2006 LOWORD (nt_header->Signature) != IMAGE_OS2_SIGNATURE)
|
|
2007 {
|
|
2008 *is_dos_app = TRUE;
|
|
2009 }
|
|
2010 else if (nt_header->Signature == IMAGE_NT_SIGNATURE)
|
|
2011 {
|
|
2012 /* Look for cygwin.dll in DLL import list. */
|
|
2013 IMAGE_DATA_DIRECTORY import_dir =
|
771
|
2014 nt_header->OptionalHeader.
|
|
2015 DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
|
|
2016 IMAGE_IMPORT_DESCRIPTOR *imports;
|
|
2017 IMAGE_SECTION_HEADER *section;
|
442
|
2018
|
|
2019 section = rva_to_section (import_dir.VirtualAddress, nt_header);
|
771
|
2020 imports =
|
|
2021 (IMAGE_IMPORT_DESCRIPTOR *) RVA_TO_PTR (import_dir.VirtualAddress,
|
|
2022 section, executable);
|
|
2023
|
442
|
2024 for ( ; imports->Name; imports++)
|
|
2025 {
|
771
|
2026 Extbyte *dllname_ext =
|
|
2027 (Extbyte *) RVA_TO_PTR (imports->Name, section, executable);
|
867
|
2028 Ibyte *dllname;
|
771
|
2029
|
|
2030 EXTERNAL_TO_C_STRING (dllname_ext, dllname, Qbinary);
|
442
|
2031
|
|
2032 /* The exact name of the cygwin dll has changed with
|
|
2033 various releases, but hopefully this will be reasonably
|
|
2034 future proof. */
|
867
|
2035 if (qxestrncasecmp (dllname, (Ibyte *) "cygwin", 6) == 0)
|
442
|
2036 {
|
|
2037 *is_cygnus_app = TRUE;
|
|
2038 break;
|
|
2039 }
|
|
2040 }
|
|
2041 }
|
|
2042 #endif
|
|
2043 }
|
|
2044
|
|
2045 unwind:
|
|
2046 close_file_data (&executable);
|
|
2047 }
|
|
2048
|
428
|
2049 /* Close the system structures associated with the given file. */
|
|
2050 void
|
|
2051 close_file_data (file_data *p_file)
|
|
2052 {
|
611
|
2053 UnmapViewOfFile (p_file->file_base);
|
|
2054 CloseHandle (p_file->file_mapping);
|
|
2055 CloseHandle (p_file->file);
|
428
|
2056 }
|
|
2057
|
771
|
2058
|
|
2059 /* Some miscellaneous functions that are Windows specific, but not GUI
|
|
2060 specific (ie. are applicable in terminal or batch mode as well). */
|
|
2061
|
|
2062 DEFUN ("mswindows-short-file-name", Fmswindows_short_file_name, 1, 1, "", /*
|
|
2063 Return the short file name version (8.3) of the full path of FILENAME.
|
|
2064 If FILENAME does not exist, return nil.
|
|
2065 All path elements in FILENAME are converted to their short names.
|
|
2066 */
|
|
2067 (filename))
|
|
2068 {
|
2421
|
2069 Extbyte shortname[PATH_MAX_EXTERNAL];
|
771
|
2070 Extbyte *fileext;
|
867
|
2071 Ibyte *shortint;
|
771
|
2072
|
|
2073 CHECK_STRING (filename);
|
|
2074
|
|
2075 /* first expand it. */
|
|
2076 filename = Fexpand_file_name (filename, Qnil);
|
|
2077
|
2526
|
2078 LISP_PATHNAME_CONVERT_OUT (filename, fileext);
|
771
|
2079 /* luckily, this returns the short version of each element in the path. */
|
|
2080 if (qxeGetShortPathName (fileext, shortname,
|
|
2081 sizeof (shortname) / XETCHAR_SIZE) == 0)
|
|
2082 return Qnil;
|
|
2083
|
|
2084 TSTR_TO_C_STRING (shortname, shortint);
|
|
2085 MSWINDOWS_NORMALIZE_FILENAME (shortint);
|
|
2086
|
1204
|
2087 return build_intstring (shortint);
|
771
|
2088 }
|
|
2089
|
|
2090
|
|
2091 DEFUN ("mswindows-long-file-name", Fmswindows_long_file_name, 1, 1, "", /*
|
|
2092 Return the long file name version of the full path of FILENAME.
|
|
2093 If FILENAME does not exist, return nil.
|
|
2094 All path elements in FILENAME are converted to their long names.
|
|
2095 */
|
|
2096 (filename))
|
|
2097 {
|
867
|
2098 Ibyte *longname, *canon;
|
771
|
2099 Lisp_Object ret;
|
|
2100
|
|
2101 CHECK_STRING (filename);
|
|
2102
|
|
2103 /* first expand it. */
|
|
2104 filename = Fexpand_file_name (filename, Qnil);
|
|
2105
|
|
2106 if (!(longname = mswindows_get_long_filename (XSTRING_DATA (filename))))
|
|
2107 return Qnil;
|
|
2108
|
|
2109 canon = mswindows_canonicalize_filename (longname);
|
1204
|
2110 ret = build_intstring (canon);
|
1726
|
2111 xfree (canon, Ibyte *);
|
|
2112 xfree (longname, Ibyte *);
|
771
|
2113 return ret;
|
|
2114 }
|
|
2115
|
814
|
2116
|
|
2117 void
|
|
2118 init_nt (void)
|
|
2119 {
|
|
2120 /* Initial preparation for subprocess support: replace our standard
|
|
2121 handles with non-inheritable versions.
|
|
2122
|
|
2123 #### Do we still need this? This is left over from the old process
|
|
2124 support. */
|
|
2125 {
|
|
2126 HANDLE parent;
|
|
2127 HANDLE stdin_save = INVALID_HANDLE_VALUE;
|
|
2128 HANDLE stdout_save = INVALID_HANDLE_VALUE;
|
|
2129 HANDLE stderr_save = INVALID_HANDLE_VALUE;
|
|
2130
|
|
2131 parent = GetCurrentProcess ();
|
|
2132
|
|
2133 /* ignore errors when duplicating and closing; typically the
|
|
2134 handles will be invalid when running as a gui program. */
|
|
2135 DuplicateHandle (parent,
|
|
2136 GetStdHandle (STD_INPUT_HANDLE),
|
|
2137 parent,
|
|
2138 &stdin_save,
|
|
2139 0,
|
|
2140 FALSE,
|
|
2141 DUPLICATE_SAME_ACCESS);
|
|
2142
|
|
2143 DuplicateHandle (parent,
|
|
2144 GetStdHandle (STD_OUTPUT_HANDLE),
|
|
2145 parent,
|
|
2146 &stdout_save,
|
|
2147 0,
|
|
2148 FALSE,
|
|
2149 DUPLICATE_SAME_ACCESS);
|
|
2150
|
|
2151 DuplicateHandle (parent,
|
|
2152 GetStdHandle (STD_ERROR_HANDLE),
|
|
2153 parent,
|
|
2154 &stderr_save,
|
|
2155 0,
|
|
2156 FALSE,
|
|
2157 DUPLICATE_SAME_ACCESS);
|
|
2158
|
|
2159 retry_fclose (stdin);
|
|
2160 retry_fclose (stdout);
|
|
2161 retry_fclose (stderr);
|
|
2162
|
|
2163 if (stdin_save != INVALID_HANDLE_VALUE)
|
|
2164 _open_osfhandle ((long) stdin_save, O_TEXT);
|
|
2165 else
|
|
2166 _open ("nul", O_TEXT | O_NOINHERIT | O_RDONLY);
|
|
2167 _fdopen (0, "r");
|
|
2168
|
|
2169 if (stdout_save != INVALID_HANDLE_VALUE)
|
|
2170 _open_osfhandle ((long) stdout_save, O_TEXT);
|
|
2171 else
|
|
2172 _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY);
|
|
2173 _fdopen (1, "w");
|
|
2174
|
|
2175 if (stderr_save != INVALID_HANDLE_VALUE)
|
|
2176 _open_osfhandle ((long) stderr_save, O_TEXT);
|
|
2177 else
|
|
2178 _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY);
|
|
2179 _fdopen (2, "w");
|
|
2180 }
|
|
2181
|
|
2182 /* determine which drives are fixed, for get_cached_volume_information */
|
|
2183 {
|
|
2184 /* GetDriveType must have trailing backslash. */
|
867
|
2185 Ibyte drive[] = "A:\\";
|
814
|
2186
|
|
2187 /* Loop over all possible drive letters */
|
|
2188 while (*drive <= 'Z')
|
|
2189 {
|
|
2190 Extbyte *driveext;
|
|
2191
|
2526
|
2192 PATHNAME_CONVERT_OUT (drive, driveext);
|
814
|
2193
|
|
2194 /* Record if this drive letter refers to a fixed drive. */
|
|
2195 fixed_drives[DRIVE_INDEX (*drive)] =
|
|
2196 (qxeGetDriveType (driveext) == DRIVE_FIXED);
|
|
2197
|
|
2198 (*drive)++;
|
|
2199 }
|
|
2200
|
|
2201 /* Reset the volume info cache. */
|
|
2202 volume_cache = NULL;
|
|
2203 }
|
|
2204 }
|
|
2205
|
771
|
2206 void
|
|
2207 syms_of_nt (void)
|
|
2208 {
|
|
2209 DEFSUBR (Fmswindows_short_file_name);
|
|
2210 DEFSUBR (Fmswindows_long_file_name);
|
|
2211 }
|
|
2212
|
440
|
2213 void
|
|
2214 vars_of_nt (void)
|
|
2215 {
|
771
|
2216 DEFVAR_INT ("mswindows-fake-unix-uid", &mswindows_fake_unix_uid /*
|
440
|
2217 *Set uid returned by `user-uid' and `user-real-uid'.
|
771
|
2218 Under NT and 9x, there are no uids, and even no almighty user called root.
|
|
2219 By setting this variable, you can have any uid of choice. Default is 0.
|
440
|
2220 Changes to this variable take effect immediately.
|
|
2221 */ );
|
771
|
2222 mswindows_fake_unix_uid = 0;
|
|
2223
|
|
2224 DEFVAR_LISP ("mswindows-get-true-file-attributes", &Vmswindows_get_true_file_attributes /*
|
|
2225 Non-nil means determine accurate link count in file-attributes.
|
|
2226 This option slows down file-attributes noticeably, so is disabled by
|
|
2227 default. Note that it is only useful for files on NTFS volumes,
|
|
2228 where hard links are supported.
|
|
2229 */ );
|
|
2230 Vmswindows_get_true_file_attributes = Qnil;
|
440
|
2231 }
|
|
2232
|
428
|
2233 /* end of nt.c */
|