1 /* Utility and Unix shadow routines for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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.
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
25 #include <stddef.h> /* for offsetof */
35 #include <sys/utime.h>
37 /* must include CRT headers *before* config.h */
66 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
67 #include <sys/socket.h>
86 #define min(x, y) (((x) < (y)) ? (x) : (y))
87 #define max(x, y) (((x) > (y)) ? (x) : (y))
89 extern Lisp_Object Vw32_downcase_file_names
;
90 extern Lisp_Object Vw32_generate_fake_inodes
;
91 extern Lisp_Object Vw32_get_true_file_attributes
;
93 static char startup_dir
[MAXPATHLEN
];
95 /* Get the current working directory. */
100 if (GetCurrentDirectory (MAXPATHLEN
, dir
) > 0)
104 /* Emacs doesn't actually change directory itself, and we want to
105 force our real wd to be where emacs.exe is to avoid unnecessary
106 conflicts when trying to rename or delete directories. */
107 strcpy (dir
, startup_dir
);
113 /* Emulate gethostname. */
115 gethostname (char *buffer
, int size
)
117 /* NT only allows small host names, so the buffer is
118 certainly large enough. */
119 return !GetComputerName (buffer
, &size
);
121 #endif /* HAVE_SOCKETS */
123 /* Emulate getloadavg. */
125 getloadavg (double loadavg
[], int nelem
)
129 /* A faithful emulation is going to have to be saved for a rainy day. */
130 for (i
= 0; i
< nelem
; i
++)
137 /* Emulate getpwuid, getpwnam and others. */
139 #define PASSWD_FIELD_SIZE 256
141 static char the_passwd_name
[PASSWD_FIELD_SIZE
];
142 static char the_passwd_passwd
[PASSWD_FIELD_SIZE
];
143 static char the_passwd_gecos
[PASSWD_FIELD_SIZE
];
144 static char the_passwd_dir
[PASSWD_FIELD_SIZE
];
145 static char the_passwd_shell
[PASSWD_FIELD_SIZE
];
147 static struct passwd the_passwd
=
162 return the_passwd
.pw_uid
;
168 /* I could imagine arguing for checking to see whether the user is
169 in the Administrators group and returning a UID of 0 for that
170 case, but I don't know how wise that would be in the long run. */
177 return the_passwd
.pw_gid
;
189 if (uid
== the_passwd
.pw_uid
)
195 getpwnam (char *name
)
199 pw
= getpwuid (getuid ());
203 if (stricmp (name
, pw
->pw_name
))
212 /* Find the user's real name by opening the process token and
213 looking up the name associated with the user-sid in that token.
215 Use the relative portion of the identifier authority value from
216 the user-sid as the user id value (same for group id using the
217 primary group sid from the process token). */
219 char user_sid
[256], name
[256], domain
[256];
220 DWORD length
= sizeof (name
), dlength
= sizeof (domain
), trash
;
222 SID_NAME_USE user_type
;
224 if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY
, &token
)
225 && GetTokenInformation (token
, TokenUser
,
226 (PVOID
) user_sid
, sizeof (user_sid
), &trash
)
227 && LookupAccountSid (NULL
, *((PSID
*) user_sid
), name
, &length
,
228 domain
, &dlength
, &user_type
))
230 strcpy (the_passwd
.pw_name
, name
);
231 /* Determine a reasonable uid value. */
232 if (stricmp ("administrator", name
) == 0)
234 the_passwd
.pw_uid
= 0;
235 the_passwd
.pw_gid
= 0;
239 SID_IDENTIFIER_AUTHORITY
* pSIA
;
241 pSIA
= GetSidIdentifierAuthority (*((PSID
*) user_sid
));
242 /* I believe the relative portion is the last 4 bytes (of 6)
244 the_passwd
.pw_uid
= ((pSIA
->Value
[2] << 24) +
245 (pSIA
->Value
[3] << 16) +
246 (pSIA
->Value
[4] << 8) +
247 (pSIA
->Value
[5] << 0));
248 /* restrict to conventional uid range for normal users */
249 the_passwd
.pw_uid
= the_passwd
.pw_uid
% 60001;
252 if (GetTokenInformation (token
, TokenPrimaryGroup
,
253 (PVOID
) user_sid
, sizeof (user_sid
), &trash
))
255 SID_IDENTIFIER_AUTHORITY
* pSIA
;
257 pSIA
= GetSidIdentifierAuthority (*((PSID
*) user_sid
));
258 the_passwd
.pw_gid
= ((pSIA
->Value
[2] << 24) +
259 (pSIA
->Value
[3] << 16) +
260 (pSIA
->Value
[4] << 8) +
261 (pSIA
->Value
[5] << 0));
262 /* I don't know if this is necessary, but for safety... */
263 the_passwd
.pw_gid
= the_passwd
.pw_gid
% 60001;
266 the_passwd
.pw_gid
= the_passwd
.pw_uid
;
269 /* If security calls are not supported (presumably because we
270 are running under Windows 95), fallback to this. */
271 else if (GetUserName (name
, &length
))
273 strcpy (the_passwd
.pw_name
, name
);
274 if (stricmp ("administrator", name
) == 0)
275 the_passwd
.pw_uid
= 0;
277 the_passwd
.pw_uid
= 123;
278 the_passwd
.pw_gid
= the_passwd
.pw_uid
;
282 strcpy (the_passwd
.pw_name
, "unknown");
283 the_passwd
.pw_uid
= 123;
284 the_passwd
.pw_gid
= 123;
287 /* Ensure HOME and SHELL are defined. */
288 if (getenv ("HOME") == NULL
)
290 if (getenv ("SHELL") == NULL
)
291 putenv (os_subtype
== OS_WIN95
? "SHELL=command" : "SHELL=cmd");
293 /* Set dir and shell from environment variables. */
294 strcpy (the_passwd
.pw_dir
, getenv ("HOME"));
295 strcpy (the_passwd
.pw_shell
, getenv ("SHELL"));
304 /* rand () on NT gives us 15 random bits...hack together 30 bits. */
305 return ((rand () << 15) | rand ());
315 /* Normalize filename by converting all path separators to
316 the specified separator. Also conditionally convert upper
317 case path name components to lower case. */
320 normalize_filename (fp
, path_sep
)
327 /* Always lower-case drive letters a-z, even if the filesystem
328 preserves case in filenames.
329 This is so filenames can be compared by string comparison
330 functions that are case-sensitive. Even case-preserving filesystems
331 do not distinguish case in drive letters. */
332 if (fp
[1] == ':' && *fp
>= 'A' && *fp
<= 'Z')
338 if (NILP (Vw32_downcase_file_names
))
342 if (*fp
== '/' || *fp
== '\\')
349 sep
= path_sep
; /* convert to this path separator */
350 elem
= fp
; /* start of current path element */
353 if (*fp
>= 'a' && *fp
<= 'z')
354 elem
= 0; /* don't convert this element */
356 if (*fp
== 0 || *fp
== ':')
358 sep
= *fp
; /* restore current separator (or 0) */
359 *fp
= '/'; /* after conversion of this element */
362 if (*fp
== '/' || *fp
== '\\')
364 if (elem
&& elem
!= fp
)
366 *fp
= 0; /* temporary end of string */
367 _strlwr (elem
); /* while we convert to lower case */
369 *fp
= sep
; /* convert (or restore) path separator */
370 elem
= fp
+ 1; /* next element starts after separator */
376 /* Destructively turn backslashes into slashes. */
378 dostounix_filename (p
)
381 normalize_filename (p
, '/');
384 /* Destructively turn slashes into backslashes. */
386 unixtodos_filename (p
)
389 normalize_filename (p
, '\\');
392 /* Remove all CR's that are followed by a LF.
393 (From msdos.c...probably should figure out a way to share it,
394 although this code isn't going to ever change.) */
398 register unsigned char *buf
;
400 unsigned char *np
= buf
;
401 unsigned char *startp
= buf
;
402 unsigned char *endp
= buf
+ n
;
406 while (buf
< endp
- 1)
410 if (*(++buf
) != 0x0a)
421 /* Parse the root part of file name, if present. Return length and
422 optionally store pointer to char after root. */
424 parse_root (char * name
, char ** pPath
)
431 /* find the root name of the volume if given */
432 if (isalpha (name
[0]) && name
[1] == ':')
434 /* skip past drive specifier */
436 if (IS_DIRECTORY_SEP (name
[0]))
439 else if (IS_DIRECTORY_SEP (name
[0]) && IS_DIRECTORY_SEP (name
[1]))
445 if (IS_DIRECTORY_SEP (*name
) && --slashes
== 0)
450 if (IS_DIRECTORY_SEP (name
[0]))
460 /* Get long base name for name; name is assumed to be absolute. */
462 get_long_basename (char * name
, char * buf
, int size
)
464 WIN32_FIND_DATA find_data
;
468 /* must be valid filename, no wild cards or other illegal characters */
469 if (strpbrk (name
, "*?|<>\""))
472 dir_handle
= FindFirstFile (name
, &find_data
);
473 if (dir_handle
!= INVALID_HANDLE_VALUE
)
475 if ((len
= strlen (find_data
.cFileName
)) < size
)
476 memcpy (buf
, find_data
.cFileName
, len
+ 1);
479 FindClose (dir_handle
);
484 /* Get long name for file, if possible (assumed to be absolute). */
486 w32_get_long_filename (char * name
, char * buf
, int size
)
491 char full
[ MAX_PATH
];
498 /* Use local copy for destructive modification. */
499 memcpy (full
, name
, len
+1);
500 unixtodos_filename (full
);
502 /* Copy root part verbatim. */
503 len
= parse_root (full
, &p
);
504 memcpy (o
, full
, len
);
511 p
= strchr (q
, '\\');
513 len
= get_long_basename (full
, o
, size
);
531 while (p
!= NULL
&& *p
);
537 is_unc_volume (const char *filename
)
539 const char *ptr
= filename
;
541 if (!IS_DIRECTORY_SEP (ptr
[0]) || !IS_DIRECTORY_SEP (ptr
[1]) || !ptr
[2])
544 if (strpbrk (ptr
+ 2, "*?|<>\"\\/"))
550 /* Routines that are no-ops on NT but are defined to get Emacs to compile. */
553 sigsetmask (int signal_mask
)
565 setpgrp (int pid
, int gid
)
577 unrequest_sigio (void)
588 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
591 w32_get_resource (key
, lpdwtype
)
596 HKEY hrootkey
= NULL
;
600 /* Check both the current user and the local machine to see if
601 we have any resources. */
603 if (RegOpenKeyEx (HKEY_CURRENT_USER
, REG_ROOT
, 0, KEY_READ
, &hrootkey
) == ERROR_SUCCESS
)
607 if (RegQueryValueEx (hrootkey
, key
, NULL
, NULL
, NULL
, &cbData
) == ERROR_SUCCESS
608 && (lpvalue
= (LPBYTE
) xmalloc (cbData
)) != NULL
609 && RegQueryValueEx (hrootkey
, key
, NULL
, lpdwtype
, lpvalue
, &cbData
) == ERROR_SUCCESS
)
614 if (lpvalue
) xfree (lpvalue
);
616 RegCloseKey (hrootkey
);
619 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE
, REG_ROOT
, 0, KEY_READ
, &hrootkey
) == ERROR_SUCCESS
)
623 if (RegQueryValueEx (hrootkey
, key
, NULL
, NULL
, NULL
, &cbData
) == ERROR_SUCCESS
624 && (lpvalue
= (LPBYTE
) xmalloc (cbData
)) != NULL
625 && RegQueryValueEx (hrootkey
, key
, NULL
, lpdwtype
, lpvalue
, &cbData
) == ERROR_SUCCESS
)
630 if (lpvalue
) xfree (lpvalue
);
632 RegCloseKey (hrootkey
);
638 char *get_emacs_configuration (void);
639 extern Lisp_Object Vsystem_configuration
;
645 static const char * const tempdirs
[] = {
646 "$TMPDIR", "$TEMP", "$TMP", "c:/"
649 const int imax
= sizeof (tempdirs
) / sizeof (tempdirs
[0]);
651 /* Make sure they have a usable $TMPDIR. Many Emacs functions use
652 temporary files and assume "/tmp" if $TMPDIR is unset, which
653 will break on DOS/Windows. Refuse to work if we cannot find
654 a directory, not even "c:/", usable for that purpose. */
655 for (i
= 0; i
< imax
; i
++)
657 const char *tmp
= tempdirs
[i
];
660 tmp
= getenv (tmp
+ 1);
661 /* Note that `access' can lie to us if the directory resides on a
662 read-only filesystem, like CD-ROM or a write-protected floppy.
663 The only way to be really sure is to actually create a file and
664 see if it succeeds. But I think that's too much to ask. */
665 if (tmp
&& access (tmp
, D_OK
) == 0)
667 char * var
= alloca (strlen (tmp
) + 8);
668 sprintf (var
, "TMPDIR=%s", tmp
);
676 Fcons (build_string ("no usable temporary directories found!!"),
678 "While setting TMPDIR: ");
680 /* Check for environment variables and use registry if they don't exist */
686 static char * env_vars
[] =
697 /* We no longer set INFOPATH because Info-default-directory-list
698 is then ignored. We use a hook in winnt.el instead. */
704 for (i
= 0; i
< (sizeof (env_vars
) / sizeof (env_vars
[0])); i
++)
706 if (!getenv (env_vars
[i
])
707 && (lpval
= w32_get_resource (env_vars
[i
], &dwType
)) != NULL
)
709 if (dwType
== REG_EXPAND_SZ
)
711 char buf1
[500], buf2
[500];
713 ExpandEnvironmentStrings ((LPSTR
) lpval
, buf1
, 500);
714 _snprintf (buf2
, 499, "%s=%s", env_vars
[i
], buf1
);
715 putenv (strdup (buf2
));
717 else if (dwType
== REG_SZ
)
721 _snprintf (buf
, 499, "%s=%s", env_vars
[i
], lpval
);
722 putenv (strdup (buf
));
730 /* Rebuild system configuration to reflect invoking system. */
731 Vsystem_configuration
= build_string (EMACS_CONFIGURATION
);
733 /* Another special case: on NT, the PATH variable is actually named
734 "Path" although cmd.exe (perhaps NT itself) arranges for
735 environment variable lookup and setting to be case insensitive.
736 However, Emacs assumes a fully case sensitive environment, so we
737 need to change "Path" to "PATH" to match the expectations of
738 various elisp packages. We do this by the sneaky method of
739 modifying the string in the C runtime environ entry.
741 The same applies to COMSPEC. */
745 for (envp
= environ
; *envp
; envp
++)
746 if (_strnicmp (*envp
, "PATH=", 5) == 0)
747 memcpy (*envp
, "PATH=", 5);
748 else if (_strnicmp (*envp
, "COMSPEC=", 8) == 0)
749 memcpy (*envp
, "COMSPEC=", 8);
752 /* Remember the initial working directory for getwd, then make the
753 real wd be the location of emacs.exe to avoid conflicts when
754 renaming or deleting directories. (We also don't call chdir when
755 running subprocesses for the same reason.) */
756 if (!GetCurrentDirectory (MAXPATHLEN
, startup_dir
))
761 char modname
[MAX_PATH
];
763 if (!GetModuleFileName (NULL
, modname
, MAX_PATH
))
765 if ((p
= strrchr (modname
, '\\')) == NULL
)
769 SetCurrentDirectory (modname
);
775 /* We don't have scripts to automatically determine the system configuration
776 for Emacs before it's compiled, and we don't want to have to make the
777 user enter it, so we define EMACS_CONFIGURATION to invoke this runtime
780 static char configuration_buffer
[32];
783 get_emacs_configuration (void)
785 char *arch
, *oem
, *os
;
787 /* Determine the processor type. */
788 switch (get_processor_type ())
791 #ifdef PROCESSOR_INTEL_386
792 case PROCESSOR_INTEL_386
:
793 case PROCESSOR_INTEL_486
:
794 case PROCESSOR_INTEL_PENTIUM
:
799 #ifdef PROCESSOR_INTEL_860
800 case PROCESSOR_INTEL_860
:
805 #ifdef PROCESSOR_MIPS_R2000
806 case PROCESSOR_MIPS_R2000
:
807 case PROCESSOR_MIPS_R3000
:
808 case PROCESSOR_MIPS_R4000
:
813 #ifdef PROCESSOR_ALPHA_21064
814 case PROCESSOR_ALPHA_21064
:
824 /* Let oem be "*" until we figure out how to decode the OEM field. */
827 os
= (GetVersion () & OS_WIN95
) ? "windows95" : "nt";
829 sprintf (configuration_buffer
, "%s-%s-%s%d.%d", arch
, oem
, os
,
830 get_w32_major_version (), get_w32_minor_version ());
831 return configuration_buffer
;
834 #include <sys/timeb.h>
836 /* Emulate gettimeofday (Ulrich Leodolter, 1/11/95). */
838 gettimeofday (struct timeval
*tv
, struct timezone
*tz
)
843 tv
->tv_sec
= tb
.time
;
844 tv
->tv_usec
= tb
.millitm
* 1000L;
847 tz
->tz_minuteswest
= tb
.timezone
; /* minutes west of Greenwich */
848 tz
->tz_dsttime
= tb
.dstflag
; /* type of dst correction */
852 /* ------------------------------------------------------------------------- */
853 /* IO support and wrapper functions for W32 API. */
854 /* ------------------------------------------------------------------------- */
856 /* Place a wrapper around the MSVC version of ctime. It returns NULL
857 on network directories, so we handle that case here.
858 (Ulrich Leodolter, 1/11/95). */
860 sys_ctime (const time_t *t
)
862 char *str
= (char *) ctime (t
);
863 return (str
? str
: "Sun Jan 01 00:00:00 1970");
866 /* Emulate sleep...we could have done this with a define, but that
867 would necessitate including windows.h in the files that used it.
868 This is much easier. */
870 sys_sleep (int seconds
)
872 Sleep (seconds
* 1000);
875 /* Internal MSVC functions for low-level descriptor munging */
876 extern int __cdecl
_set_osfhnd (int fd
, long h
);
877 extern int __cdecl
_free_osfhnd (int fd
);
879 /* parallel array of private info on file handles */
880 filedesc fd_info
[ MAXDESC
];
882 typedef struct volume_info_data
{
883 struct volume_info_data
* next
;
885 /* time when info was obtained */
888 /* actual volume info */
897 /* Global referenced by various functions. */
898 static volume_info_data volume_info
;
900 /* Vector to indicate which drives are local and fixed (for which cached
901 data never expires). */
902 static BOOL fixed_drives
[26];
904 /* Consider cached volume information to be stale if older than 10s,
905 at least for non-local drives. Info for fixed drives is never stale. */
906 #define DRIVE_INDEX( c ) ( (c) <= 'Z' ? (c) - 'A' : (c) - 'a' )
907 #define VOLINFO_STILL_VALID( root_dir, info ) \
908 ( ( isalpha (root_dir[0]) && \
909 fixed_drives[ DRIVE_INDEX (root_dir[0]) ] ) \
910 || GetTickCount () - info->timestamp < 10000 )
912 /* Cache support functions. */
914 /* Simple linked list with linear search is sufficient. */
915 static volume_info_data
*volume_cache
= NULL
;
917 static volume_info_data
*
918 lookup_volume_info (char * root_dir
)
920 volume_info_data
* info
;
922 for (info
= volume_cache
; info
; info
= info
->next
)
923 if (stricmp (info
->root_dir
, root_dir
) == 0)
929 add_volume_info (char * root_dir
, volume_info_data
* info
)
931 info
->root_dir
= strdup (root_dir
);
932 info
->next
= volume_cache
;
937 /* Wrapper for GetVolumeInformation, which uses caching to avoid
938 performance penalty (~2ms on 486 for local drives, 7.5ms for local
939 cdrom drive, ~5-10ms or more for remote drives on LAN). */
941 GetCachedVolumeInformation (char * root_dir
)
943 volume_info_data
* info
;
944 char default_root
[ MAX_PATH
];
946 /* NULL for root_dir means use root from current directory. */
947 if (root_dir
== NULL
)
949 if (GetCurrentDirectory (MAX_PATH
, default_root
) == 0)
951 parse_root (default_root
, &root_dir
);
953 root_dir
= default_root
;
956 /* Local fixed drives can be cached permanently. Removable drives
957 cannot be cached permanently, since the volume name and serial
958 number (if nothing else) can change. Remote drives should be
959 treated as if they are removable, since there is no sure way to
960 tell whether they are or not. Also, the UNC association of drive
961 letters mapped to remote volumes can be changed at any time (even
962 by other processes) without notice.
964 As a compromise, so we can benefit from caching info for remote
965 volumes, we use a simple expiry mechanism to invalidate cache
966 entries that are more than ten seconds old. */
969 /* No point doing this, because WNetGetConnection is even slower than
970 GetVolumeInformation, consistently taking ~50ms on a 486 (FWIW,
971 GetDriveType is about the only call of this type which does not
972 involve network access, and so is extremely quick). */
974 /* Map drive letter to UNC if remote. */
975 if ( isalpha( root_dir
[0] ) && !fixed
[ DRIVE_INDEX( root_dir
[0] ) ] )
977 char remote_name
[ 256 ];
978 char drive
[3] = { root_dir
[0], ':' };
980 if (WNetGetConnection (drive
, remote_name
, sizeof (remote_name
))
986 info
= lookup_volume_info (root_dir
);
988 if (info
== NULL
|| ! VOLINFO_STILL_VALID (root_dir
, info
))
996 /* Info is not cached, or is stale. */
997 if (!GetVolumeInformation (root_dir
,
1002 type
, sizeof (type
)))
1005 /* Cache the volume information for future use, overwriting existing
1006 entry if present. */
1009 info
= (volume_info_data
*) xmalloc (sizeof (volume_info_data
));
1010 add_volume_info (root_dir
, info
);
1018 info
->name
= strdup (name
);
1019 info
->serialnum
= serialnum
;
1020 info
->maxcomp
= maxcomp
;
1021 info
->flags
= flags
;
1022 info
->type
= strdup (type
);
1023 info
->timestamp
= GetTickCount ();
1029 /* Get information on the volume where name is held; set path pointer to
1030 start of pathname in name (past UNC header\volume header if present). */
1032 get_volume_info (const char * name
, const char ** pPath
)
1034 char temp
[MAX_PATH
];
1035 char *rootname
= NULL
; /* default to current volume */
1036 volume_info_data
* info
;
1041 /* find the root name of the volume if given */
1042 if (isalpha (name
[0]) && name
[1] == ':')
1050 else if (IS_DIRECTORY_SEP (name
[0]) && IS_DIRECTORY_SEP (name
[1]))
1057 if (IS_DIRECTORY_SEP (*name
) && --slashes
== 0)
1070 info
= GetCachedVolumeInformation (rootname
);
1073 /* Set global referenced by other functions. */
1074 volume_info
= *info
;
1080 /* Determine if volume is FAT format (ie. only supports short 8.3
1081 names); also set path pointer to start of pathname in name. */
1083 is_fat_volume (const char * name
, const char ** pPath
)
1085 if (get_volume_info (name
, pPath
))
1086 return (volume_info
.maxcomp
== 12);
1090 /* Map filename to a legal 8.3 name if necessary. */
1092 map_w32_filename (const char * name
, const char ** pPath
)
1094 static char shortname
[MAX_PATH
];
1095 char * str
= shortname
;
1098 const char * save_name
= name
;
1100 if (is_fat_volume (name
, &path
)) /* truncate to 8.3 */
1102 register int left
= 8; /* maximum number of chars in part */
1103 register int extn
= 0; /* extension added? */
1104 register int dots
= 2; /* maximum number of dots allowed */
1107 *str
++ = *name
++; /* skip past UNC header */
1109 while ((c
= *name
++))
1116 extn
= 0; /* reset extension flags */
1117 dots
= 2; /* max 2 dots */
1118 left
= 8; /* max length 8 for main part */
1122 extn
= 0; /* reset extension flags */
1123 dots
= 2; /* max 2 dots */
1124 left
= 8; /* max length 8 for main part */
1129 /* Convert path components of the form .xxx to _xxx,
1130 but leave . and .. as they are. This allows .emacs
1131 to be read as _emacs, for example. */
1135 IS_DIRECTORY_SEP (*name
))
1150 extn
= 1; /* we've got an extension */
1151 left
= 3; /* 3 chars in extension */
1155 /* any embedded dots after the first are converted to _ */
1160 case '#': /* don't lose these, they're important */
1162 str
[-1] = c
; /* replace last character of part */
1167 *str
++ = tolower (c
); /* map to lower case (looks nicer) */
1169 dots
= 0; /* started a path component */
1178 strcpy (shortname
, name
);
1179 unixtodos_filename (shortname
);
1183 *pPath
= shortname
+ (path
- save_name
);
1189 is_exec (const char * name
)
1191 char * p
= strrchr (name
, '.');
1194 && (stricmp (p
, ".exe") == 0 ||
1195 stricmp (p
, ".com") == 0 ||
1196 stricmp (p
, ".bat") == 0 ||
1197 stricmp (p
, ".cmd") == 0));
1200 /* Emulate the Unix directory procedures opendir, closedir,
1201 and readdir. We can't use the procedures supplied in sysdep.c,
1202 so we provide them here. */
1204 struct direct dir_static
; /* simulated directory contents */
1205 static HANDLE dir_find_handle
= INVALID_HANDLE_VALUE
;
1206 static int dir_is_fat
;
1207 static char dir_pathname
[MAXPATHLEN
+1];
1208 static WIN32_FIND_DATA dir_find_data
;
1210 /* Support shares on a network resource as subdirectories of a read-only
1212 static HANDLE wnet_enum_handle
= INVALID_HANDLE_VALUE
;
1213 HANDLE
open_unc_volume (char *);
1214 char *read_unc_volume (HANDLE
, char *, int);
1215 void close_unc_volume (HANDLE
);
1218 opendir (char *filename
)
1222 /* Opening is done by FindFirstFile. However, a read is inherent to
1223 this operation, so we defer the open until read time. */
1225 if (dir_find_handle
!= INVALID_HANDLE_VALUE
)
1227 if (wnet_enum_handle
!= INVALID_HANDLE_VALUE
)
1230 if (is_unc_volume (filename
))
1232 wnet_enum_handle
= open_unc_volume (filename
);
1233 if (wnet_enum_handle
== INVALID_HANDLE_VALUE
)
1237 if (!(dirp
= (DIR *) malloc (sizeof (DIR))))
1244 strncpy (dir_pathname
, map_w32_filename (filename
, NULL
), MAXPATHLEN
);
1245 dir_pathname
[MAXPATHLEN
] = '\0';
1246 dir_is_fat
= is_fat_volume (filename
, NULL
);
1252 closedir (DIR *dirp
)
1254 /* If we have a find-handle open, close it. */
1255 if (dir_find_handle
!= INVALID_HANDLE_VALUE
)
1257 FindClose (dir_find_handle
);
1258 dir_find_handle
= INVALID_HANDLE_VALUE
;
1260 else if (wnet_enum_handle
!= INVALID_HANDLE_VALUE
)
1262 close_unc_volume (wnet_enum_handle
);
1263 wnet_enum_handle
= INVALID_HANDLE_VALUE
;
1265 xfree ((char *) dirp
);
1271 if (wnet_enum_handle
!= INVALID_HANDLE_VALUE
)
1273 if (!read_unc_volume (wnet_enum_handle
,
1274 dir_find_data
.cFileName
,
1278 /* If we aren't dir_finding, do a find-first, otherwise do a find-next. */
1279 else if (dir_find_handle
== INVALID_HANDLE_VALUE
)
1281 char filename
[MAXNAMLEN
+ 3];
1284 strcpy (filename
, dir_pathname
);
1285 ln
= strlen (filename
) - 1;
1286 if (!IS_DIRECTORY_SEP (filename
[ln
]))
1287 strcat (filename
, "\\");
1288 strcat (filename
, "*");
1290 dir_find_handle
= FindFirstFile (filename
, &dir_find_data
);
1292 if (dir_find_handle
== INVALID_HANDLE_VALUE
)
1297 if (!FindNextFile (dir_find_handle
, &dir_find_data
))
1301 /* Emacs never uses this value, so don't bother making it match
1302 value returned by stat(). */
1303 dir_static
.d_ino
= 1;
1305 dir_static
.d_reclen
= sizeof (struct direct
) - MAXNAMLEN
+ 3 +
1306 dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
1308 dir_static
.d_namlen
= strlen (dir_find_data
.cFileName
);
1309 strcpy (dir_static
.d_name
, dir_find_data
.cFileName
);
1311 _strlwr (dir_static
.d_name
);
1312 else if (!NILP (Vw32_downcase_file_names
))
1315 for (p
= dir_static
.d_name
; *p
; p
++)
1316 if (*p
>= 'a' && *p
<= 'z')
1319 _strlwr (dir_static
.d_name
);
1326 open_unc_volume (char *path
)
1332 nr
.dwScope
= RESOURCE_GLOBALNET
;
1333 nr
.dwType
= RESOURCETYPE_DISK
;
1334 nr
.dwDisplayType
= RESOURCEDISPLAYTYPE_SERVER
;
1335 nr
.dwUsage
= RESOURCEUSAGE_CONTAINER
;
1336 nr
.lpLocalName
= NULL
;
1337 nr
.lpRemoteName
= map_w32_filename (path
, NULL
);
1338 nr
.lpComment
= NULL
;
1339 nr
.lpProvider
= NULL
;
1341 result
= WNetOpenEnum(RESOURCE_GLOBALNET
, RESOURCETYPE_DISK
,
1342 RESOURCEUSAGE_CONNECTABLE
, &nr
, &henum
);
1344 if (result
== NO_ERROR
)
1347 return INVALID_HANDLE_VALUE
;
1351 read_unc_volume (HANDLE henum
, char *readbuf
, int size
)
1360 buffer
= alloca (bufsize
);
1361 result
= WNetEnumResource (wnet_enum_handle
, &count
, buffer
, &bufsize
);
1362 if (result
!= NO_ERROR
)
1365 /* WNetEnumResource returns \\resource\share...skip forward to "share". */
1366 ptr
= ((LPNETRESOURCE
) buffer
)->lpRemoteName
;
1368 while (*ptr
&& !IS_DIRECTORY_SEP (*ptr
)) ptr
++;
1371 strncpy (readbuf
, ptr
, size
);
1376 close_unc_volume (HANDLE henum
)
1378 if (henum
!= INVALID_HANDLE_VALUE
)
1379 WNetCloseEnum (henum
);
1383 unc_volume_file_attributes (char *path
)
1388 henum
= open_unc_volume (path
);
1389 if (henum
== INVALID_HANDLE_VALUE
)
1392 attrs
= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_DIRECTORY
;
1394 close_unc_volume (henum
);
1400 /* Shadow some MSVC runtime functions to map requests for long filenames
1401 to reasonable short names if necessary. This was originally added to
1402 permit running Emacs on NT 3.1 on a FAT partition, which doesn't support
1406 sys_access (const char * path
, int mode
)
1410 /* MSVC implementation doesn't recognize D_OK. */
1411 path
= map_w32_filename (path
, NULL
);
1412 if (is_unc_volume (path
))
1414 attributes
= unc_volume_file_attributes (path
);
1415 if (attributes
== -1) {
1420 else if ((attributes
= GetFileAttributes (path
)) == -1)
1422 /* Should try mapping GetLastError to errno; for now just indicate
1423 that path doesn't exist. */
1427 if ((mode
& X_OK
) != 0 && !is_exec (path
))
1432 if ((mode
& W_OK
) != 0 && (attributes
& FILE_ATTRIBUTE_READONLY
) != 0)
1437 if ((mode
& D_OK
) != 0 && (attributes
& FILE_ATTRIBUTE_DIRECTORY
) == 0)
1446 sys_chdir (const char * path
)
1448 return _chdir (map_w32_filename (path
, NULL
));
1452 sys_chmod (const char * path
, int mode
)
1454 return _chmod (map_w32_filename (path
, NULL
), mode
);
1458 sys_creat (const char * path
, int mode
)
1460 return _creat (map_w32_filename (path
, NULL
), mode
);
1464 sys_fopen(const char * path
, const char * mode
)
1468 const char * mode_save
= mode
;
1470 /* Force all file handles to be non-inheritable. This is necessary to
1471 ensure child processes don't unwittingly inherit handles that might
1472 prevent future file access. */
1476 else if (mode
[0] == 'w' || mode
[0] == 'a')
1477 oflag
= O_WRONLY
| O_CREAT
| O_TRUNC
;
1481 /* Only do simplistic option parsing. */
1485 oflag
&= ~(O_RDONLY
| O_WRONLY
);
1488 else if (mode
[0] == 'b')
1493 else if (mode
[0] == 't')
1500 fd
= _open (map_w32_filename (path
, NULL
), oflag
| _O_NOINHERIT
, 0644);
1504 return _fdopen (fd
, mode_save
);
1507 /* This only works on NTFS volumes, but is useful to have. */
1509 sys_link (const char * old
, const char * new)
1513 char oldname
[MAX_PATH
], newname
[MAX_PATH
];
1515 if (old
== NULL
|| new == NULL
)
1521 strcpy (oldname
, map_w32_filename (old
, NULL
));
1522 strcpy (newname
, map_w32_filename (new, NULL
));
1524 fileh
= CreateFile (oldname
, 0, 0, NULL
, OPEN_EXISTING
,
1525 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
1526 if (fileh
!= INVALID_HANDLE_VALUE
)
1530 /* Confusingly, the "alternate" stream name field does not apply
1531 when restoring a hard link, and instead contains the actual
1532 stream data for the link (ie. the name of the link to create).
1533 The WIN32_STREAM_ID structure before the cStreamName field is
1534 the stream header, which is then immediately followed by the
1538 WIN32_STREAM_ID wid
;
1539 WCHAR wbuffer
[MAX_PATH
]; /* extra space for link name */
1542 wlen
= MultiByteToWideChar (CP_ACP
, MB_PRECOMPOSED
, newname
, -1,
1543 data
.wid
.cStreamName
, MAX_PATH
);
1546 LPVOID context
= NULL
;
1549 data
.wid
.dwStreamId
= BACKUP_LINK
;
1550 data
.wid
.dwStreamAttributes
= 0;
1551 data
.wid
.Size
.LowPart
= wlen
* sizeof(WCHAR
);
1552 data
.wid
.Size
.HighPart
= 0;
1553 data
.wid
.dwStreamNameSize
= 0;
1555 if (BackupWrite (fileh
, (LPBYTE
)&data
,
1556 offsetof (WIN32_STREAM_ID
, cStreamName
)
1557 + data
.wid
.Size
.LowPart
,
1558 &wbytes
, FALSE
, FALSE
, &context
)
1559 && BackupWrite (fileh
, NULL
, 0, &wbytes
, TRUE
, FALSE
, &context
))
1566 /* Should try mapping GetLastError to errno; for now just
1567 indicate a general error (eg. links not supported). */
1568 errno
= EINVAL
; // perhaps EMLINK?
1572 CloseHandle (fileh
);
1581 sys_mkdir (const char * path
)
1583 return _mkdir (map_w32_filename (path
, NULL
));
1586 /* Because of long name mapping issues, we need to implement this
1587 ourselves. Also, MSVC's _mktemp returns NULL when it can't generate
1588 a unique name, instead of setting the input template to an empty
1591 Standard algorithm seems to be use pid or tid with a letter on the
1592 front (in place of the 6 X's) and cycle through the letters to find a
1593 unique name. We extend that to allow any reasonable character as the
1594 first of the 6 X's. */
1596 sys_mktemp (char * template)
1600 unsigned uid
= GetCurrentThreadId ();
1601 static char first_char
[] = "abcdefghijklmnopqrstuvwyz0123456789!%-_@#";
1603 if (template == NULL
)
1605 p
= template + strlen (template);
1607 /* replace up to the last 5 X's with uid in decimal */
1608 while (--p
>= template && p
[0] == 'X' && --i
>= 0)
1610 p
[0] = '0' + uid
% 10;
1614 if (i
< 0 && p
[0] == 'X')
1619 int save_errno
= errno
;
1620 p
[0] = first_char
[i
];
1621 if (sys_access (template, 0) < 0)
1627 while (++i
< sizeof (first_char
));
1630 /* Template is badly formed or else we can't generate a unique name,
1631 so return empty string */
1637 sys_open (const char * path
, int oflag
, int mode
)
1639 /* Force all file handles to be non-inheritable. */
1640 return _open (map_w32_filename (path
, NULL
), oflag
| _O_NOINHERIT
, mode
);
1644 sys_rename (const char * oldname
, const char * newname
)
1647 char temp
[MAX_PATH
];
1649 /* MoveFile on Windows 95 doesn't correctly change the short file name
1650 alias in a number of circumstances (it is not easy to predict when
1651 just by looking at oldname and newname, unfortunately). In these
1652 cases, renaming through a temporary name avoids the problem.
1654 A second problem on Windows 95 is that renaming through a temp name when
1655 newname is uppercase fails (the final long name ends up in
1656 lowercase, although the short alias might be uppercase) UNLESS the
1657 long temp name is not 8.3.
1659 So, on Windows 95 we always rename through a temp name, and we make sure
1660 the temp name has a long extension to ensure correct renaming. */
1662 strcpy (temp
, map_w32_filename (oldname
, NULL
));
1664 if (os_subtype
== OS_WIN95
)
1670 oldname
= map_w32_filename (oldname
, NULL
);
1671 if (o
= strrchr (oldname
, '\\'))
1674 o
= (char *) oldname
;
1676 if (p
= strrchr (temp
, '\\'))
1683 /* Force temp name to require a manufactured 8.3 alias - this
1684 seems to make the second rename work properly. */
1685 sprintf (p
, "_.%s.%u", o
, i
);
1687 result
= rename (oldname
, temp
);
1689 /* This loop must surely terminate! */
1690 while (result
< 0 && (errno
== EEXIST
|| errno
== EACCES
));
1695 /* Emulate Unix behaviour - newname is deleted if it already exists
1696 (at least if it is a file; don't do this for directories).
1698 Since we mustn't do this if we are just changing the case of the
1699 file name (we would end up deleting the file we are trying to
1700 rename!), we let rename detect if the destination file already
1701 exists - that way we avoid the possible pitfalls of trying to
1702 determine ourselves whether two names really refer to the same
1703 file, which is not always possible in the general case. (Consider
1704 all the permutations of shared or subst'd drives, etc.) */
1706 newname
= map_w32_filename (newname
, NULL
);
1707 result
= rename (temp
, newname
);
1710 && (errno
== EEXIST
|| errno
== EACCES
)
1711 && _chmod (newname
, 0666) == 0
1712 && _unlink (newname
) == 0)
1713 result
= rename (temp
, newname
);
1719 sys_rmdir (const char * path
)
1721 return _rmdir (map_w32_filename (path
, NULL
));
1725 sys_unlink (const char * path
)
1727 path
= map_w32_filename (path
, NULL
);
1729 /* On Unix, unlink works without write permission. */
1730 _chmod (path
, 0666);
1731 return _unlink (path
);
1734 static FILETIME utc_base_ft
;
1735 static long double utc_base
;
1736 static int init
= 0;
1739 convert_time (FILETIME ft
)
1745 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
1754 st
.wMilliseconds
= 0;
1756 SystemTimeToFileTime (&st
, &utc_base_ft
);
1757 utc_base
= (long double) utc_base_ft
.dwHighDateTime
1758 * 4096 * 1024 * 1024 + utc_base_ft
.dwLowDateTime
;
1762 if (CompareFileTime (&ft
, &utc_base_ft
) < 0)
1765 ret
= (long double) ft
.dwHighDateTime
* 4096 * 1024 * 1024 + ft
.dwLowDateTime
;
1767 return (time_t) (ret
* 1e-7);
1771 convert_from_time_t (time_t time
, FILETIME
* pft
)
1777 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
1786 st
.wMilliseconds
= 0;
1788 SystemTimeToFileTime (&st
, &utc_base_ft
);
1789 utc_base
= (long double) utc_base_ft
.dwHighDateTime
1790 * 4096 * 1024 * 1024 + utc_base_ft
.dwLowDateTime
;
1794 /* time in 100ns units since 1-Jan-1601 */
1795 tmp
= (long double) time
* 1e7
+ utc_base
;
1796 pft
->dwHighDateTime
= (DWORD
) (tmp
/ (4096.0 * 1024 * 1024));
1797 pft
->dwLowDateTime
= (DWORD
) (tmp
- (4096.0 * 1024 * 1024) * pft
->dwHighDateTime
);
1801 /* No reason to keep this; faking inode values either by hashing or even
1802 using the file index from GetInformationByHandle, is not perfect and
1803 so by default Emacs doesn't use the inode values on Windows.
1804 Instead, we now determine file-truename correctly (except for
1805 possible drive aliasing etc). */
1807 /* Modified version of "PJW" algorithm (see the "Dragon" compiler book). */
1809 hashval (const unsigned char * str
)
1814 h
= (h
<< 4) + *str
++;
1820 /* Return the hash value of the canonical pathname, excluding the
1821 drive/UNC header, to get a hopefully unique inode number. */
1823 generate_inode_val (const char * name
)
1825 char fullname
[ MAX_PATH
];
1829 /* Get the truly canonical filename, if it exists. (Note: this
1830 doesn't resolve aliasing due to subst commands, or recognise hard
1832 if (!w32_get_long_filename ((char *)name
, fullname
, MAX_PATH
))
1835 parse_root (fullname
, &p
);
1836 /* Normal W32 filesystems are still case insensitive. */
1843 /* MSVC stat function can't cope with UNC names and has other bugs, so
1844 replace it with our own. This also allows us to calculate consistent
1845 inode values without hacks in the main Emacs code. */
1847 stat (const char * path
, struct stat
* buf
)
1850 WIN32_FIND_DATA wfd
;
1855 int rootdir
= FALSE
;
1857 if (path
== NULL
|| buf
== NULL
)
1863 name
= (char *) map_w32_filename (path
, &path
);
1864 /* must be valid filename, no wild cards or other illegal characters */
1865 if (strpbrk (name
, "*?|<>\""))
1871 /* If name is "c:/.." or "/.." then stat "c:/" or "/". */
1872 r
= IS_DEVICE_SEP (name
[1]) ? &name
[2] : name
;
1873 if (IS_DIRECTORY_SEP (r
[0]) && r
[1] == '.' && r
[2] == '.' && r
[3] == '\0')
1878 /* Remove trailing directory separator, unless name is the root
1879 directory of a drive or UNC volume in which case ensure there
1880 is a trailing separator. */
1881 len
= strlen (name
);
1882 rootdir
= (path
>= name
+ len
- 1
1883 && (IS_DIRECTORY_SEP (*path
) || *path
== 0));
1884 name
= strcpy (alloca (len
+ 2), name
);
1886 if (is_unc_volume (name
))
1888 DWORD attrs
= unc_volume_file_attributes (name
);
1893 memset (&wfd
, 0, sizeof (wfd
));
1894 wfd
.dwFileAttributes
= attrs
;
1895 wfd
.ftCreationTime
= utc_base_ft
;
1896 wfd
.ftLastAccessTime
= utc_base_ft
;
1897 wfd
.ftLastWriteTime
= utc_base_ft
;
1898 strcpy (wfd
.cFileName
, name
);
1902 if (!IS_DIRECTORY_SEP (name
[len
-1]))
1903 strcat (name
, "\\");
1904 if (GetDriveType (name
) < 2)
1909 memset (&wfd
, 0, sizeof (wfd
));
1910 wfd
.dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
1911 wfd
.ftCreationTime
= utc_base_ft
;
1912 wfd
.ftLastAccessTime
= utc_base_ft
;
1913 wfd
.ftLastWriteTime
= utc_base_ft
;
1914 strcpy (wfd
.cFileName
, name
);
1918 if (IS_DIRECTORY_SEP (name
[len
-1]))
1921 /* (This is hacky, but helps when doing file completions on
1922 network drives.) Optimize by using information available from
1923 active readdir if possible. */
1924 if (dir_find_handle
!= INVALID_HANDLE_VALUE
1925 && (len
= strlen (dir_pathname
)),
1926 strnicmp (name
, dir_pathname
, len
) == 0
1927 && IS_DIRECTORY_SEP (name
[len
])
1928 && stricmp (name
+ len
+ 1, dir_static
.d_name
) == 0)
1930 /* This was the last entry returned by readdir. */
1931 wfd
= dir_find_data
;
1935 fh
= FindFirstFile (name
, &wfd
);
1936 if (fh
== INVALID_HANDLE_VALUE
)
1945 if (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
1947 buf
->st_mode
= _S_IFDIR
;
1948 buf
->st_nlink
= 2; /* doesn't really matter */
1949 fake_inode
= 0; /* this doesn't either I think */
1951 else if (!NILP (Vw32_get_true_file_attributes
)
1952 /* No access rights required to get info. */
1953 && (fh
= CreateFile (name
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
))
1954 != INVALID_HANDLE_VALUE
)
1956 /* This is more accurate in terms of gettting the correct number
1957 of links, but is quite slow (it is noticable when Emacs is
1958 making a list of file name completions). */
1959 BY_HANDLE_FILE_INFORMATION info
;
1961 if (GetFileInformationByHandle (fh
, &info
))
1963 switch (GetFileType (fh
))
1965 case FILE_TYPE_DISK
:
1966 buf
->st_mode
= _S_IFREG
;
1968 case FILE_TYPE_PIPE
:
1969 buf
->st_mode
= _S_IFIFO
;
1971 case FILE_TYPE_CHAR
:
1972 case FILE_TYPE_UNKNOWN
:
1974 buf
->st_mode
= _S_IFCHR
;
1976 buf
->st_nlink
= info
.nNumberOfLinks
;
1977 /* Might as well use file index to fake inode values, but this
1978 is not guaranteed to be unique unless we keep a handle open
1979 all the time (even then there are situations where it is
1980 not unique). Reputedly, there are at most 48 bits of info
1981 (on NTFS, presumably less on FAT). */
1982 fake_inode
= info
.nFileIndexLow
^ info
.nFileIndexHigh
;
1993 /* Don't bother to make this information more accurate. */
1994 buf
->st_mode
= _S_IFREG
;
2000 /* Not sure if there is any point in this. */
2001 if (!NILP (Vw32_generate_fake_inodes
))
2002 fake_inode
= generate_inode_val (name
);
2003 else if (fake_inode
== 0)
2005 /* For want of something better, try to make everything unique. */
2006 static DWORD gen_num
= 0;
2007 fake_inode
= ++gen_num
;
2011 /* MSVC defines _ino_t to be short; other libc's might not. */
2012 if (sizeof (buf
->st_ino
) == 2)
2013 buf
->st_ino
= fake_inode
^ (fake_inode
>> 16);
2015 buf
->st_ino
= fake_inode
;
2017 /* consider files to belong to current user */
2018 buf
->st_uid
= the_passwd
.pw_uid
;
2019 buf
->st_gid
= the_passwd
.pw_gid
;
2021 /* volume_info is set indirectly by map_w32_filename */
2022 buf
->st_dev
= volume_info
.serialnum
;
2023 buf
->st_rdev
= volume_info
.serialnum
;
2026 buf
->st_size
= wfd
.nFileSizeLow
;
2028 /* Convert timestamps to Unix format. */
2029 buf
->st_mtime
= convert_time (wfd
.ftLastWriteTime
);
2030 buf
->st_atime
= convert_time (wfd
.ftLastAccessTime
);
2031 if (buf
->st_atime
== 0) buf
->st_atime
= buf
->st_mtime
;
2032 buf
->st_ctime
= convert_time (wfd
.ftCreationTime
);
2033 if (buf
->st_ctime
== 0) buf
->st_ctime
= buf
->st_mtime
;
2035 /* determine rwx permissions */
2036 if (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
)
2037 permission
= _S_IREAD
;
2039 permission
= _S_IREAD
| _S_IWRITE
;
2041 if (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2042 permission
|= _S_IEXEC
;
2043 else if (is_exec (name
))
2044 permission
|= _S_IEXEC
;
2046 buf
->st_mode
|= permission
| (permission
>> 3) | (permission
>> 6);
2051 /* Provide fstat and utime as well as stat for consistent handling of
2054 fstat (int desc
, struct stat
* buf
)
2056 HANDLE fh
= (HANDLE
) _get_osfhandle (desc
);
2057 BY_HANDLE_FILE_INFORMATION info
;
2061 switch (GetFileType (fh
) & ~FILE_TYPE_REMOTE
)
2063 case FILE_TYPE_DISK
:
2064 buf
->st_mode
= _S_IFREG
;
2065 if (!GetFileInformationByHandle (fh
, &info
))
2071 case FILE_TYPE_PIPE
:
2072 buf
->st_mode
= _S_IFIFO
;
2074 case FILE_TYPE_CHAR
:
2075 case FILE_TYPE_UNKNOWN
:
2077 buf
->st_mode
= _S_IFCHR
;
2079 memset (&info
, 0, sizeof (info
));
2080 info
.dwFileAttributes
= 0;
2081 info
.ftCreationTime
= utc_base_ft
;
2082 info
.ftLastAccessTime
= utc_base_ft
;
2083 info
.ftLastWriteTime
= utc_base_ft
;
2086 if (info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2088 buf
->st_mode
= _S_IFDIR
;
2089 buf
->st_nlink
= 2; /* doesn't really matter */
2090 fake_inode
= 0; /* this doesn't either I think */
2094 buf
->st_nlink
= info
.nNumberOfLinks
;
2095 /* Might as well use file index to fake inode values, but this
2096 is not guaranteed to be unique unless we keep a handle open
2097 all the time (even then there are situations where it is
2098 not unique). Reputedly, there are at most 48 bits of info
2099 (on NTFS, presumably less on FAT). */
2100 fake_inode
= info
.nFileIndexLow
^ info
.nFileIndexHigh
;
2103 /* MSVC defines _ino_t to be short; other libc's might not. */
2104 if (sizeof (buf
->st_ino
) == 2)
2105 buf
->st_ino
= fake_inode
^ (fake_inode
>> 16);
2107 buf
->st_ino
= fake_inode
;
2109 /* consider files to belong to current user */
2113 buf
->st_dev
= info
.dwVolumeSerialNumber
;
2114 buf
->st_rdev
= info
.dwVolumeSerialNumber
;
2116 buf
->st_size
= info
.nFileSizeLow
;
2118 /* Convert timestamps to Unix format. */
2119 buf
->st_mtime
= convert_time (info
.ftLastWriteTime
);
2120 buf
->st_atime
= convert_time (info
.ftLastAccessTime
);
2121 if (buf
->st_atime
== 0) buf
->st_atime
= buf
->st_mtime
;
2122 buf
->st_ctime
= convert_time (info
.ftCreationTime
);
2123 if (buf
->st_ctime
== 0) buf
->st_ctime
= buf
->st_mtime
;
2125 /* determine rwx permissions */
2126 if (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
)
2127 permission
= _S_IREAD
;
2129 permission
= _S_IREAD
| _S_IWRITE
;
2131 if (info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2132 permission
|= _S_IEXEC
;
2135 #if 0 /* no way of knowing the filename */
2136 char * p
= strrchr (name
, '.');
2138 (stricmp (p
, ".exe") == 0 ||
2139 stricmp (p
, ".com") == 0 ||
2140 stricmp (p
, ".bat") == 0 ||
2141 stricmp (p
, ".cmd") == 0))
2142 permission
|= _S_IEXEC
;
2146 buf
->st_mode
|= permission
| (permission
>> 3) | (permission
>> 6);
2152 utime (const char *name
, struct utimbuf
*times
)
2154 struct utimbuf deftime
;
2161 deftime
.modtime
= deftime
.actime
= time (NULL
);
2165 /* Need write access to set times. */
2166 fh
= CreateFile (name
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2167 0, OPEN_EXISTING
, 0, NULL
);
2170 convert_from_time_t (times
->actime
, &atime
);
2171 convert_from_time_t (times
->modtime
, &mtime
);
2172 if (!SetFileTime (fh
, NULL
, &atime
, &mtime
))
2190 /* Wrappers for winsock functions to map between our file descriptors
2191 and winsock's handles; also set h_errno for convenience.
2193 To allow Emacs to run on systems which don't have winsock support
2194 installed, we dynamically link to winsock on startup if present, and
2195 otherwise provide the minimum necessary functionality
2196 (eg. gethostname). */
2198 /* function pointers for relevant socket functions */
2199 int (PASCAL
*pfn_WSAStartup
) (WORD wVersionRequired
, LPWSADATA lpWSAData
);
2200 void (PASCAL
*pfn_WSASetLastError
) (int iError
);
2201 int (PASCAL
*pfn_WSAGetLastError
) (void);
2202 int (PASCAL
*pfn_socket
) (int af
, int type
, int protocol
);
2203 int (PASCAL
*pfn_bind
) (SOCKET s
, const struct sockaddr
*addr
, int namelen
);
2204 int (PASCAL
*pfn_connect
) (SOCKET s
, const struct sockaddr
*addr
, int namelen
);
2205 int (PASCAL
*pfn_ioctlsocket
) (SOCKET s
, long cmd
, u_long
*argp
);
2206 int (PASCAL
*pfn_recv
) (SOCKET s
, char * buf
, int len
, int flags
);
2207 int (PASCAL
*pfn_send
) (SOCKET s
, const char * buf
, int len
, int flags
);
2208 int (PASCAL
*pfn_closesocket
) (SOCKET s
);
2209 int (PASCAL
*pfn_shutdown
) (SOCKET s
, int how
);
2210 int (PASCAL
*pfn_WSACleanup
) (void);
2212 u_short (PASCAL
*pfn_htons
) (u_short hostshort
);
2213 u_short (PASCAL
*pfn_ntohs
) (u_short netshort
);
2214 unsigned long (PASCAL
*pfn_inet_addr
) (const char * cp
);
2215 int (PASCAL
*pfn_gethostname
) (char * name
, int namelen
);
2216 struct hostent
* (PASCAL
*pfn_gethostbyname
) (const char * name
);
2217 struct servent
* (PASCAL
*pfn_getservbyname
) (const char * name
, const char * proto
);
2219 /* SetHandleInformation is only needed to make sockets non-inheritable. */
2220 BOOL (WINAPI
*pfn_SetHandleInformation
) (HANDLE object
, DWORD mask
, DWORD flags
);
2221 #ifndef HANDLE_FLAG_INHERIT
2222 #define HANDLE_FLAG_INHERIT 1
2226 static int winsock_inuse
;
2231 if (winsock_lib
!= NULL
&& winsock_inuse
== 0)
2233 /* Not sure what would cause WSAENETDOWN, or even if it can happen
2234 after WSAStartup returns successfully, but it seems reasonable
2235 to allow unloading winsock anyway in that case. */
2236 if (pfn_WSACleanup () == 0 ||
2237 pfn_WSAGetLastError () == WSAENETDOWN
)
2239 if (FreeLibrary (winsock_lib
))
2248 init_winsock (int load_now
)
2250 WSADATA winsockData
;
2252 if (winsock_lib
!= NULL
)
2255 pfn_SetHandleInformation
= NULL
;
2256 pfn_SetHandleInformation
2257 = (void *) GetProcAddress (GetModuleHandle ("kernel32.dll"),
2258 "SetHandleInformation");
2260 winsock_lib
= LoadLibrary ("wsock32.dll");
2262 if (winsock_lib
!= NULL
)
2264 /* dynamically link to socket functions */
2266 #define LOAD_PROC(fn) \
2267 if ((pfn_##fn = (void *) GetProcAddress (winsock_lib, #fn)) == NULL) \
2270 LOAD_PROC( WSAStartup
);
2271 LOAD_PROC( WSASetLastError
);
2272 LOAD_PROC( WSAGetLastError
);
2273 LOAD_PROC( socket
);
2275 LOAD_PROC( connect
);
2276 LOAD_PROC( ioctlsocket
);
2279 LOAD_PROC( closesocket
);
2280 LOAD_PROC( shutdown
);
2283 LOAD_PROC( inet_addr
);
2284 LOAD_PROC( gethostname
);
2285 LOAD_PROC( gethostbyname
);
2286 LOAD_PROC( getservbyname
);
2287 LOAD_PROC( WSACleanup
);
2291 /* specify version 1.1 of winsock */
2292 if (pfn_WSAStartup (0x101, &winsockData
) == 0)
2294 if (winsockData
.wVersion
!= 0x101)
2299 /* Report that winsock exists and is usable, but leave
2300 socket functions disabled. I am assuming that calling
2301 WSAStartup does not require any network interaction,
2302 and in particular does not cause or require a dial-up
2303 connection to be established. */
2306 FreeLibrary (winsock_lib
);
2314 FreeLibrary (winsock_lib
);
2324 /* function to set h_errno for compatability; map winsock error codes to
2325 normal system codes where they overlap (non-overlapping definitions
2326 are already in <sys/socket.h> */
2327 static void set_errno ()
2329 if (winsock_lib
== NULL
)
2332 h_errno
= pfn_WSAGetLastError ();
2336 case WSAEACCES
: h_errno
= EACCES
; break;
2337 case WSAEBADF
: h_errno
= EBADF
; break;
2338 case WSAEFAULT
: h_errno
= EFAULT
; break;
2339 case WSAEINTR
: h_errno
= EINTR
; break;
2340 case WSAEINVAL
: h_errno
= EINVAL
; break;
2341 case WSAEMFILE
: h_errno
= EMFILE
; break;
2342 case WSAENAMETOOLONG
: h_errno
= ENAMETOOLONG
; break;
2343 case WSAENOTEMPTY
: h_errno
= ENOTEMPTY
; break;
2348 static void check_errno ()
2350 if (h_errno
== 0 && winsock_lib
!= NULL
)
2351 pfn_WSASetLastError (0);
2354 /* [andrewi 3-May-96] I've had conflicting results using both methods,
2355 but I believe the method of keeping the socket handle separate (and
2356 insuring it is not inheritable) is the correct one. */
2358 //#define SOCK_REPLACE_HANDLE
2360 #ifdef SOCK_REPLACE_HANDLE
2361 #define SOCK_HANDLE(fd) ((SOCKET) _get_osfhandle (fd))
2363 #define SOCK_HANDLE(fd) ((SOCKET) fd_info[fd].hnd)
2367 sys_socket(int af
, int type
, int protocol
)
2373 if (winsock_lib
== NULL
)
2376 return INVALID_SOCKET
;
2381 /* call the real socket function */
2382 s
= (long) pfn_socket (af
, type
, protocol
);
2384 if (s
!= INVALID_SOCKET
)
2386 /* Although under NT 3.5 _open_osfhandle will accept a socket
2387 handle, if opened with SO_OPENTYPE == SO_SYNCHRONOUS_NONALERT,
2388 that does not work under NT 3.1. However, we can get the same
2389 effect by using a backdoor function to replace an existing
2390 descriptor handle with the one we want. */
2392 /* allocate a file descriptor (with appropriate flags) */
2393 fd
= _open ("NUL:", _O_RDWR
);
2396 #ifdef SOCK_REPLACE_HANDLE
2397 /* now replace handle to NUL with our socket handle */
2398 CloseHandle ((HANDLE
) _get_osfhandle (fd
));
2400 _set_osfhnd (fd
, s
);
2401 /* setmode (fd, _O_BINARY); */
2403 /* Make a non-inheritable copy of the socket handle. */
2406 HANDLE new_s
= INVALID_HANDLE_VALUE
;
2408 parent
= GetCurrentProcess ();
2410 /* Apparently there is a bug in NT 3.51 with some service
2411 packs, which prevents using DuplicateHandle to make a
2412 socket handle non-inheritable (causes WSACleanup to
2413 hang). The work-around is to use SetHandleInformation
2414 instead if it is available and implemented. */
2415 if (!pfn_SetHandleInformation
2416 || !pfn_SetHandleInformation ((HANDLE
) s
,
2417 HANDLE_FLAG_INHERIT
,
2420 DuplicateHandle (parent
,
2426 DUPLICATE_SAME_ACCESS
);
2427 pfn_closesocket (s
);
2430 fd_info
[fd
].hnd
= (HANDLE
) s
;
2434 /* set our own internal flags */
2435 fd_info
[fd
].flags
= FILE_SOCKET
| FILE_BINARY
| FILE_READ
| FILE_WRITE
;
2441 cp
->status
= STATUS_READ_ACKNOWLEDGED
;
2443 /* attach child_process to fd_info */
2444 if (fd_info
[ fd
].cp
!= NULL
)
2446 DebPrint (("sys_socket: fd_info[%d] apparently in use!\n", fd
));
2450 fd_info
[ fd
].cp
= cp
;
2453 winsock_inuse
++; /* count open sockets */
2460 pfn_closesocket (s
);
2470 sys_bind (int s
, const struct sockaddr
* addr
, int namelen
)
2472 if (winsock_lib
== NULL
)
2475 return SOCKET_ERROR
;
2479 if (fd_info
[s
].flags
& FILE_SOCKET
)
2481 int rc
= pfn_bind (SOCK_HANDLE (s
), addr
, namelen
);
2482 if (rc
== SOCKET_ERROR
)
2487 return SOCKET_ERROR
;
2492 sys_connect (int s
, const struct sockaddr
* name
, int namelen
)
2494 if (winsock_lib
== NULL
)
2497 return SOCKET_ERROR
;
2501 if (fd_info
[s
].flags
& FILE_SOCKET
)
2503 int rc
= pfn_connect (SOCK_HANDLE (s
), name
, namelen
);
2504 if (rc
== SOCKET_ERROR
)
2509 return SOCKET_ERROR
;
2513 sys_htons (u_short hostshort
)
2515 return (winsock_lib
!= NULL
) ?
2516 pfn_htons (hostshort
) : hostshort
;
2520 sys_ntohs (u_short netshort
)
2522 return (winsock_lib
!= NULL
) ?
2523 pfn_ntohs (netshort
) : netshort
;
2527 sys_inet_addr (const char * cp
)
2529 return (winsock_lib
!= NULL
) ?
2530 pfn_inet_addr (cp
) : INADDR_NONE
;
2534 sys_gethostname (char * name
, int namelen
)
2536 if (winsock_lib
!= NULL
)
2537 return pfn_gethostname (name
, namelen
);
2539 if (namelen
> MAX_COMPUTERNAME_LENGTH
)
2540 return !GetComputerName (name
, &namelen
);
2543 return SOCKET_ERROR
;
2547 sys_gethostbyname(const char * name
)
2549 struct hostent
* host
;
2551 if (winsock_lib
== NULL
)
2558 host
= pfn_gethostbyname (name
);
2565 sys_getservbyname(const char * name
, const char * proto
)
2567 struct servent
* serv
;
2569 if (winsock_lib
== NULL
)
2576 serv
= pfn_getservbyname (name
, proto
);
2583 sys_shutdown (int s
, int how
)
2587 if (winsock_lib
== NULL
)
2590 return SOCKET_ERROR
;
2594 if (fd_info
[s
].flags
& FILE_SOCKET
)
2596 int rc
= pfn_shutdown (SOCK_HANDLE (s
), how
);
2597 if (rc
== SOCKET_ERROR
)
2602 return SOCKET_ERROR
;
2605 #endif /* HAVE_SOCKETS */
2608 /* Shadow main io functions: we need to handle pipes and sockets more
2609 intelligently, and implement non-blocking mode as well. */
2616 if (fd
< 0 || fd
>= MAXDESC
)
2624 child_process
* cp
= fd_info
[fd
].cp
;
2626 fd_info
[fd
].cp
= NULL
;
2628 if (CHILD_ACTIVE (cp
))
2630 /* if last descriptor to active child_process then cleanup */
2632 for (i
= 0; i
< MAXDESC
; i
++)
2636 if (fd_info
[i
].cp
== cp
)
2642 if (fd_info
[fd
].flags
& FILE_SOCKET
)
2644 #ifndef SOCK_REPLACE_HANDLE
2645 if (winsock_lib
== NULL
) abort ();
2647 pfn_shutdown (SOCK_HANDLE (fd
), 2);
2648 rc
= pfn_closesocket (SOCK_HANDLE (fd
));
2650 winsock_inuse
--; /* count open sockets */
2658 /* Note that sockets do not need special treatment here (at least on
2659 NT and Windows 95 using the standard tcp/ip stacks) - it appears that
2660 closesocket is equivalent to CloseHandle, which is to be expected
2661 because socket handles are fully fledged kernel handles. */
2665 fd_info
[fd
].flags
= 0;
2678 /* duplicate our internal info as well */
2679 fd_info
[new_fd
] = fd_info
[fd
];
2686 sys_dup2 (int src
, int dst
)
2690 if (dst
< 0 || dst
>= MAXDESC
)
2696 /* make sure we close the destination first if it's a pipe or socket */
2697 if (src
!= dst
&& fd_info
[dst
].flags
!= 0)
2700 rc
= _dup2 (src
, dst
);
2703 /* duplicate our internal info as well */
2704 fd_info
[dst
] = fd_info
[src
];
2709 /* Unix pipe() has only one arg */
2711 sys_pipe (int * phandles
)
2717 /* make pipe handles non-inheritable; when we spawn a child, we
2718 replace the relevant handle with an inheritable one. Also put
2719 pipes into binary mode; we will do text mode translation ourselves
2721 rc
= _pipe (phandles
, 0, _O_NOINHERIT
| _O_BINARY
);
2725 flags
= FILE_PIPE
| FILE_READ
| FILE_BINARY
;
2726 fd_info
[phandles
[0]].flags
= flags
;
2728 flags
= FILE_PIPE
| FILE_WRITE
| FILE_BINARY
;
2729 fd_info
[phandles
[1]].flags
= flags
;
2736 extern Lisp_Object Vw32_pipe_read_delay
;
2738 /* Function to do blocking read of one byte, needed to implement
2739 select. It is only allowed on sockets and pipes. */
2741 _sys_read_ahead (int fd
)
2746 if (fd
< 0 || fd
>= MAXDESC
)
2747 return STATUS_READ_ERROR
;
2749 cp
= fd_info
[fd
].cp
;
2751 if (cp
== NULL
|| cp
->fd
!= fd
|| cp
->status
!= STATUS_READ_READY
)
2752 return STATUS_READ_ERROR
;
2754 if ((fd_info
[fd
].flags
& (FILE_PIPE
| FILE_SOCKET
)) == 0
2755 || (fd_info
[fd
].flags
& FILE_READ
) == 0)
2757 DebPrint (("_sys_read_ahead: internal error: fd %d is not a pipe or socket!\n", fd
));
2761 cp
->status
= STATUS_READ_IN_PROGRESS
;
2763 if (fd_info
[fd
].flags
& FILE_PIPE
)
2765 rc
= _read (fd
, &cp
->chr
, sizeof (char));
2767 /* Give subprocess time to buffer some more output for us before
2768 reporting that input is available; we need this because Windows 95
2769 connects DOS programs to pipes by making the pipe appear to be
2770 the normal console stdout - as a result most DOS programs will
2771 write to stdout without buffering, ie. one character at a
2772 time. Even some W32 programs do this - "dir" in a command
2773 shell on NT is very slow if we don't do this. */
2776 int wait
= XINT (Vw32_pipe_read_delay
);
2782 /* Yield remainder of our time slice, effectively giving a
2783 temporary priority boost to the child process. */
2788 else if (fd_info
[fd
].flags
& FILE_SOCKET
)
2789 rc
= pfn_recv (SOCK_HANDLE (fd
), &cp
->chr
, sizeof (char), 0);
2792 if (rc
== sizeof (char))
2793 cp
->status
= STATUS_READ_SUCCEEDED
;
2795 cp
->status
= STATUS_READ_FAILED
;
2801 sys_read (int fd
, char * buffer
, unsigned int count
)
2806 char * orig_buffer
= buffer
;
2808 if (fd
< 0 || fd
>= MAXDESC
)
2814 if (fd_info
[fd
].flags
& (FILE_PIPE
| FILE_SOCKET
))
2816 child_process
*cp
= fd_info
[fd
].cp
;
2818 if ((fd_info
[fd
].flags
& FILE_READ
) == 0)
2826 /* re-read CR carried over from last read */
2827 if (fd_info
[fd
].flags
& FILE_LAST_CR
)
2829 if (fd_info
[fd
].flags
& FILE_BINARY
) abort ();
2833 fd_info
[fd
].flags
&= ~FILE_LAST_CR
;
2836 /* presence of a child_process structure means we are operating in
2837 non-blocking mode - otherwise we just call _read directly.
2838 Note that the child_process structure might be missing because
2839 reap_subprocess has been called; in this case the pipe is
2840 already broken, so calling _read on it is okay. */
2843 int current_status
= cp
->status
;
2845 switch (current_status
)
2847 case STATUS_READ_FAILED
:
2848 case STATUS_READ_ERROR
:
2849 /* report normal EOF if nothing in buffer */
2851 fd_info
[fd
].flags
|= FILE_AT_EOF
;
2854 case STATUS_READ_READY
:
2855 case STATUS_READ_IN_PROGRESS
:
2856 DebPrint (("sys_read called when read is in progress\n"));
2857 errno
= EWOULDBLOCK
;
2860 case STATUS_READ_SUCCEEDED
:
2861 /* consume read-ahead char */
2862 *buffer
++ = cp
->chr
;
2865 cp
->status
= STATUS_READ_ACKNOWLEDGED
;
2866 ResetEvent (cp
->char_avail
);
2868 case STATUS_READ_ACKNOWLEDGED
:
2872 DebPrint (("sys_read: bad status %d\n", current_status
));
2877 if (fd_info
[fd
].flags
& FILE_PIPE
)
2879 PeekNamedPipe ((HANDLE
) _get_osfhandle (fd
), NULL
, 0, NULL
, &waiting
, NULL
);
2880 to_read
= min (waiting
, (DWORD
) count
);
2883 nchars
+= _read (fd
, buffer
, to_read
);
2886 else /* FILE_SOCKET */
2888 if (winsock_lib
== NULL
) abort ();
2890 /* do the equivalent of a non-blocking read */
2891 pfn_ioctlsocket (SOCK_HANDLE (fd
), FIONREAD
, &waiting
);
2892 if (waiting
== 0 && nchars
== 0)
2894 h_errno
= errno
= EWOULDBLOCK
;
2900 /* always use binary mode for sockets */
2901 int res
= pfn_recv (SOCK_HANDLE (fd
), buffer
, count
, 0);
2902 if (res
== SOCKET_ERROR
)
2904 DebPrint(("sys_read.recv failed with error %d on socket %ld\n",
2905 pfn_WSAGetLastError (), SOCK_HANDLE (fd
)));
2916 int nread
= _read (fd
, buffer
, count
);
2919 else if (nchars
== 0)
2924 fd_info
[fd
].flags
|= FILE_AT_EOF
;
2925 /* Perform text mode translation if required. */
2926 else if ((fd_info
[fd
].flags
& FILE_BINARY
) == 0)
2928 nchars
= crlf_to_lf (nchars
, orig_buffer
);
2929 /* If buffer contains only CR, return that. To be absolutely
2930 sure we should attempt to read the next char, but in
2931 practice a CR to be followed by LF would not appear by
2932 itself in the buffer. */
2933 if (nchars
> 1 && orig_buffer
[nchars
- 1] == 0x0d)
2935 fd_info
[fd
].flags
|= FILE_LAST_CR
;
2941 nchars
= _read (fd
, buffer
, count
);
2946 /* For now, don't bother with a non-blocking mode */
2948 sys_write (int fd
, const void * buffer
, unsigned int count
)
2952 if (fd
< 0 || fd
>= MAXDESC
)
2958 if (fd_info
[fd
].flags
& (FILE_PIPE
| FILE_SOCKET
))
2960 if ((fd_info
[fd
].flags
& FILE_WRITE
) == 0)
2966 /* Perform text mode translation if required. */
2967 if ((fd_info
[fd
].flags
& FILE_BINARY
) == 0)
2969 char * tmpbuf
= alloca (count
* 2);
2970 unsigned char * src
= (void *)buffer
;
2971 unsigned char * dst
= tmpbuf
;
2976 unsigned char *next
;
2977 /* copy next line or remaining bytes */
2978 next
= _memccpy (dst
, src
, '\n', nbytes
);
2981 /* copied one line ending with '\n' */
2982 int copied
= next
- dst
;
2985 /* insert '\r' before '\n' */
2992 /* copied remaining partial line -> now finished */
3000 if (fd_info
[fd
].flags
& FILE_SOCKET
)
3002 if (winsock_lib
== NULL
) abort ();
3003 nchars
= pfn_send (SOCK_HANDLE (fd
), buffer
, count
, 0);
3004 if (nchars
== SOCKET_ERROR
)
3006 DebPrint(("sys_read.send failed with error %d on socket %ld\n",
3007 pfn_WSAGetLastError (), SOCK_HANDLE (fd
)));
3013 nchars
= _write (fd
, buffer
, count
);
3019 check_windows_init_file ()
3021 extern int noninteractive
, inhibit_window_system
;
3023 /* A common indication that Emacs is not installed properly is when
3024 it cannot find the Windows installation file. If this file does
3025 not exist in the expected place, tell the user. */
3027 if (!noninteractive
&& !inhibit_window_system
)
3029 extern Lisp_Object Vwindow_system
, Vload_path
, Qfile_exists_p
;
3030 Lisp_Object init_file
;
3033 init_file
= build_string ("term/w32-win");
3034 fd
= openp (Vload_path
, init_file
, ".el:.elc", NULL
, 0);
3037 Lisp_Object load_path_print
= Fprin1_to_string (Vload_path
, Qnil
);
3038 char *init_file_name
= XSTRING (init_file
)->data
;
3039 char *load_path
= XSTRING (load_path_print
)->data
;
3040 char *buffer
= alloca (1024);
3043 "The Emacs Windows initialization file \"%s.el\" "
3044 "could not be found in your Emacs installation. "
3045 "Emacs checked the following directories for this file:\n"
3047 "When Emacs cannot find this file, it usually means that it "
3048 "was not installed properly, or its distribution file was "
3049 "not unpacked properly.\nSee the README.W32 file in the "
3050 "top-level Emacs directory for more information.",
3051 init_file_name
, load_path
);
3054 "Emacs Abort Dialog",
3055 MB_OK
| MB_ICONEXCLAMATION
| MB_TASKMODAL
);
3056 /* Use the low-level Emacs abort. */
3071 /* shutdown the socket interface if necessary */
3080 /* Initialise the socket interface now if available and requested by
3081 the user by defining PRELOAD_WINSOCK; otherwise loading will be
3082 delayed until open-network-stream is called (w32-has-winsock can
3083 also be used to dynamically load or reload winsock).
3085 Conveniently, init_environment is called before us, so
3086 PRELOAD_WINSOCK can be set in the registry. */
3088 /* Always initialize this correctly. */
3091 if (getenv ("PRELOAD_WINSOCK") != NULL
)
3092 init_winsock (TRUE
);
3095 /* Initial preparation for subprocess support: replace our standard
3096 handles with non-inheritable versions. */
3099 HANDLE stdin_save
= INVALID_HANDLE_VALUE
;
3100 HANDLE stdout_save
= INVALID_HANDLE_VALUE
;
3101 HANDLE stderr_save
= INVALID_HANDLE_VALUE
;
3103 parent
= GetCurrentProcess ();
3105 /* ignore errors when duplicating and closing; typically the
3106 handles will be invalid when running as a gui program. */
3107 DuplicateHandle (parent
,
3108 GetStdHandle (STD_INPUT_HANDLE
),
3113 DUPLICATE_SAME_ACCESS
);
3115 DuplicateHandle (parent
,
3116 GetStdHandle (STD_OUTPUT_HANDLE
),
3121 DUPLICATE_SAME_ACCESS
);
3123 DuplicateHandle (parent
,
3124 GetStdHandle (STD_ERROR_HANDLE
),
3129 DUPLICATE_SAME_ACCESS
);
3135 if (stdin_save
!= INVALID_HANDLE_VALUE
)
3136 _open_osfhandle ((long) stdin_save
, O_TEXT
);
3138 _open ("nul", O_TEXT
| O_NOINHERIT
| O_RDONLY
);
3141 if (stdout_save
!= INVALID_HANDLE_VALUE
)
3142 _open_osfhandle ((long) stdout_save
, O_TEXT
);
3144 _open ("nul", O_TEXT
| O_NOINHERIT
| O_WRONLY
);
3147 if (stderr_save
!= INVALID_HANDLE_VALUE
)
3148 _open_osfhandle ((long) stderr_save
, O_TEXT
);
3150 _open ("nul", O_TEXT
| O_NOINHERIT
| O_WRONLY
);
3154 /* unfortunately, atexit depends on implementation of malloc */
3155 /* atexit (term_ntproc); */
3156 signal (SIGABRT
, term_ntproc
);
3158 /* determine which drives are fixed, for GetCachedVolumeInformation */
3160 /* GetDriveType must have trailing backslash. */
3161 char drive
[] = "A:\\";
3163 /* Loop over all possible drive letters */
3164 while (*drive
<= 'Z')
3166 /* Record if this drive letter refers to a fixed drive. */
3167 fixed_drives
[DRIVE_INDEX (*drive
)] =
3168 (GetDriveType (drive
) == DRIVE_FIXED
);
3174 /* Check to see if Emacs has been installed correctly. */
3175 check_windows_init_file ();