This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Text::Wrap maintained via the CPAN release
[perl5.git] / win32 / win32.c
CommitLineData
68dc0745
PP
1/* WIN32.C
2 *
3fadfdf1 3 * (c) 1995 Microsoft Corporation. All rights reserved.
68dc0745
PP
4 * Developed by hip communications inc., http://info.hip.com/info/
5 * Portions (c) 1993 Intergraph Corporation. All rights reserved.
6 *
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
9 */
3fadfdf1 10#define PERLIO_NOT_STDIO 0
0a753a76
PP
11#define WIN32_LEAN_AND_MEAN
12#define WIN32IO_IS_STDIO
13#include <tchar.h>
a835ef8a
NIS
14#ifdef __GNUC__
15#define Win32_Winsock
16#endif
0a753a76 17#include <windows.h>
aeecf691
JD
18#ifndef HWND_MESSAGE
19# define HWND_MESSAGE ((HWND)-3)
20#endif
35cf1ab6 21#ifndef WC_NO_BEST_FIT_CHARS
3839a0fa 22# define WC_NO_BEST_FIT_CHARS 0x00000400 /* requires Windows 2000 or later */
35cf1ab6 23#endif
5db10396 24#include <winnt.h>
542cb85f 25#include <tlhelp32.h>
5db10396 26#include <io.h>
c843839f 27#include <signal.h>
0a753a76 28
542cb85f
JD
29#define SystemProcessesAndThreadsInformation 5
30
31/* Inline some definitions from the DDK */
32typedef struct {
33 USHORT Length;
34 USHORT MaximumLength;
35 PWSTR Buffer;
36} UNICODE_STRING;
37
38typedef struct {
39 ULONG NextEntryDelta;
40 ULONG ThreadCount;
41 ULONG Reserved1[6];
42 LARGE_INTEGER CreateTime;
43 LARGE_INTEGER UserTime;
44 LARGE_INTEGER KernelTime;
45 UNICODE_STRING ProcessName;
46 LONG BasePriority;
47 ULONG ProcessId;
48 ULONG InheritedFromProcessId;
49 /* Remainder of the structure depends on the Windows version,
50 * but we don't need those additional fields anyways... */
51} SYSTEM_PROCESSES;
52
68dc0745 53/* #include "config.h" */
0a753a76 54
0a753a76
PP
55#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
56#define PerlIO FILE
57#endif
58
7a9ec5a3 59#include <sys/stat.h>
0a753a76
PP
60#include "EXTERN.h"
61#include "perl.h"
c69f6586
GS
62
63#define NO_XSLOCKS
c5be433b 64#define PERL_NO_GET_CONTEXT
ad2e33dc 65#include "XSUB.h"
c69f6586
GS
66
67#include "Win32iop.h"
0a753a76 68#include <fcntl.h>
5b0d9cbe
NIS
69#ifndef __GNUC__
70/* assert.h conflicts with #define of assert in perl.h */
0a753a76 71#include <assert.h>
5b0d9cbe 72#endif
0a753a76
PP
73#include <string.h>
74#include <stdarg.h>
ad2e33dc 75#include <float.h>
ad0751ec 76#include <time.h>
3730b96e 77#if defined(_MSC_VER) || defined(__MINGW32__)
ad0751ec
GS
78#include <sys/utime.h>
79#else
80#include <utime.h>
81#endif
5b0d9cbe 82#ifdef __GNUC__
3fadfdf1 83/* Mingw32 defaults to globing command line
5b0d9cbe
NIS
84 * So we turn it off like this:
85 */
86int _CRT_glob = 0;
87#endif
88
7c5b6093
AB
89#if defined(__MINGW32__) && (__MINGW32_MAJOR_VERSION==1)
90/* Mingw32-1.1 is missing some prototypes */
d06fc7d4 91START_EXTERN_C
f8fb7c90
GS
92FILE * _wfopen(LPCWSTR wszFileName, LPCWSTR wszMode);
93FILE * _wfdopen(int nFd, LPCWSTR wszMode);
94FILE * _freopen(LPCWSTR wszFileName, LPCWSTR wszMode, FILE * pOldStream);
95int _flushall();
96int _fcloseall();
d06fc7d4 97END_EXTERN_C
2b260de0
GS
98#endif
99
100#if defined(__BORLANDC__)
0b94c7bb
GS
101# define _stat stat
102# define _utimbuf utimbuf
103#endif
104
6890e559
GS
105#define EXECF_EXEC 1
106#define EXECF_SPAWN 2
107#define EXECF_SPAWN_NOWAIT 3
108
32e30700
GS
109#if defined(PERL_IMPLICIT_SYS)
110# undef win32_get_privlib
111# define win32_get_privlib g_win32_get_privlib
112# undef win32_get_sitelib
113# define win32_get_sitelib g_win32_get_sitelib
4ea817c6
GS
114# undef win32_get_vendorlib
115# define win32_get_vendorlib g_win32_get_vendorlib
32e30700
GS
116# undef getlogin
117# define getlogin g_getlogin
118#endif
119
ce1da67e 120static void get_shell(void);
dff6d3cd 121static long tokenize(const char *str, char **dest, char ***destv);
08039b81
SH
122static int do_spawn2(pTHX_ const char *cmd, int exectype);
123static BOOL has_shell_metachars(const char *ptr);
2d7a9237 124static long filetime_to_clock(PFILETIME ft);
ad0751ec 125static BOOL filetime_from_time(PFILETIME ft, time_t t);
c5be433b 126static char * get_emd_part(SV **leading, char *trailing, ...);
0aaad0ff
GS
127static void remove_dead_process(long deceased);
128static long find_pid(int pid);
129static char * qualified_path(const char *cmd);
4ea817c6
GS
130static char * win32_get_xlib(const char *pl, const char *xlib,
131 const char *libname);
099b16d3
RM
132static LRESULT win32_process_message(HWND hwnd, UINT msg,
133 WPARAM wParam, LPARAM lParam);
4ea817c6 134
7766f137
GS
135#ifdef USE_ITHREADS
136static void remove_dead_pseudo_process(long child);
137static long find_pseudo_pid(int pid);
138#endif
c69f6586 139
7766f137 140START_EXTERN_C
2d7a9237 141HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
8ac9c18d 142char w32_module_name[MAX_PATH+1];
7766f137
GS
143END_EXTERN_C
144
aeecf691 145static OSVERSIONINFO g_osver = {0, 0, 0, 0, 0, ""};
50892819 146
542cb85f
JD
147static HANDLE (WINAPI *pfnCreateToolhelp32Snapshot)(DWORD, DWORD) = NULL;
148static BOOL (WINAPI *pfnProcess32First)(HANDLE, PROCESSENTRY32*) = NULL;
149static BOOL (WINAPI *pfnProcess32Next)(HANDLE, PROCESSENTRY32*) = NULL;
150static LONG (WINAPI *pfnZwQuerySystemInformation)(UINT, PVOID, ULONG, PULONG);
151
3a00b83e
SH
152#ifdef __BORLANDC__
153/* Silence STDERR grumblings from Borland's math library. */
154DllExport int
155_matherr(struct _exception *a)
156{
157 PERL_UNUSED_VAR(a);
158 return 1;
159}
160#endif
161
58d049f0
JD
162/* VS2005 (MSC version 14) provides a mechanism to set an invalid
163 * parameter handler. This functionality is not available in the
164 * 64-bit compiler from the Platform SDK, which unfortunately also
165 * believes itself to be MSC version 14.
166 *
167 * There is no #define related to _set_invalid_parameter_handler(),
168 * but we can check for one of the constants defined for
169 * _set_abort_behavior(), which was introduced into stdlib.h at
170 * the same time.
171 */
172
173#if _MSC_VER >= 1400 && defined(_WRITE_ABORT_MSG)
174# define SET_INVALID_PARAMETER_HANDLER
175#endif
176
177#ifdef SET_INVALID_PARAMETER_HANDLER
0448a0bd
SH
178void my_invalid_parameter_handler(const wchar_t* expression,
179 const wchar_t* function,
180 const wchar_t* file,
181 unsigned int line,
182 uintptr_t pReserved)
183{
184# ifdef _DEBUG
185 wprintf(L"Invalid parameter detected in function %s."
186 L" File: %s Line: %d\n", function, file, line);
187 wprintf(L"Expression: %s\n", expression);
188# endif
189}
190#endif
191
3fadfdf1 192int
ba106d47
GS
193IsWin95(void)
194{
aeecf691 195 return (g_osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
3fe9a6f1
PP
196}
197
198int
ba106d47
GS
199IsWinNT(void)
200{
aeecf691 201 return (g_osver.dwPlatformId == VER_PLATFORM_WIN32_NT);
3fe9a6f1 202}
0a753a76 203
3839a0fa
JD
204int
205IsWin2000(void)
206{
207 return (g_osver.dwMajorVersion > 4);
208}
209
2fa86c13
GS
210EXTERN_C void
211set_w32_module_name(void)
212{
aa2b96ec 213 /* this function may be called at DLL_PROCESS_ATTACH time */
2fa86c13 214 char* ptr;
aa2b96ec
JD
215 HMODULE module = (HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
216 ? GetModuleHandle(NULL)
217 : w32_perldll_handle);
2fa86c13 218
aa2b96ec
JD
219 OSVERSIONINFO osver; /* g_osver may not yet be initialized */
220 osver.dwOSVersionInfoSize = sizeof(osver);
221 GetVersionEx(&osver);
be9da6a9 222
3839a0fa 223 if (osver.dwMajorVersion > 4) {
aa2b96ec
JD
224 WCHAR modulename[MAX_PATH];
225 WCHAR fullname[MAX_PATH];
226 char *ansi;
227
228 GetModuleFileNameW(module, modulename, sizeof(modulename)/sizeof(WCHAR));
229
230 /* Make sure we get an absolute pathname in case the module was loaded
231 * explicitly by LoadLibrary() with a relative path. */
232 GetFullPathNameW(modulename, sizeof(fullname)/sizeof(WCHAR), fullname, NULL);
233
234 /* remove \\?\ prefix */
235 if (memcmp(fullname, L"\\\\?\\", 4*sizeof(WCHAR)) == 0)
236 memmove(fullname, fullname+4, (wcslen(fullname+4)+1)*sizeof(WCHAR));
237
238 ansi = win32_ansipath(fullname);
239 my_strlcpy(w32_module_name, ansi, sizeof(w32_module_name));
240 win32_free(ansi);
241 }
242 else {
243 GetModuleFileName(module, w32_module_name, sizeof(w32_module_name));
244
245 /* remove \\?\ prefix */
246 if (memcmp(w32_module_name, "\\\\?\\", 4) == 0)
247 memmove(w32_module_name, w32_module_name+4, strlen(w32_module_name+4)+1);
248
249 /* try to get full path to binary (which may be mangled when perl is
250 * run from a 16-bit app) */
251 /*PerlIO_printf(Perl_debug_log, "Before %s\n", w32_module_name);*/
252 win32_longpath(w32_module_name);
253 /*PerlIO_printf(Perl_debug_log, "After %s\n", w32_module_name);*/
254 }
2fa86c13
GS
255
256 /* normalize to forward slashes */
257 ptr = w32_module_name;
258 while (*ptr) {
259 if (*ptr == '\\')
260 *ptr = '/';
261 ++ptr;
262 }
263}
264
c5be433b 265/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
51371543 266static char*
c5be433b 267get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
349ad1fe
GS
268{
269 /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
00dc2f4f
GS
270 HKEY handle;
271 DWORD type;
272 const char *subkey = "Software\\Perl";
4e205ed6 273 char *str = NULL;
00dc2f4f
GS
274 long retval;
275
276 retval = RegOpenKeyEx(hkey, subkey, 0, KEY_READ, &handle);
349ad1fe 277 if (retval == ERROR_SUCCESS) {
51371543
GS
278 DWORD datalen;
279 retval = RegQueryValueEx(handle, valuename, 0, &type, NULL, &datalen);
1c94caf4
GS
280 if (retval == ERROR_SUCCESS
281 && (type == REG_SZ || type == REG_EXPAND_SZ))
282 {
acfe0abc 283 dTHX;
c5be433b
GS
284 if (!*svp)
285 *svp = sv_2mortal(newSVpvn("",0));
286 SvGROW(*svp, datalen);
51371543 287 retval = RegQueryValueEx(handle, valuename, 0, NULL,
c5be433b 288 (PBYTE)SvPVX(*svp), &datalen);
51371543 289 if (retval == ERROR_SUCCESS) {
c5be433b
GS
290 str = SvPVX(*svp);
291 SvCUR_set(*svp,datalen-1);
51371543 292 }
00dc2f4f
GS
293 }
294 RegCloseKey(handle);
295 }
349ad1fe 296 return str;
00dc2f4f
GS
297}
298
c5be433b 299/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
51371543 300static char*
c5be433b 301get_regstr(const char *valuename, SV **svp)
00dc2f4f 302{
c5be433b 303 char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
349ad1fe 304 if (!str)
c5be433b 305 str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
349ad1fe 306 return str;
00dc2f4f
GS
307}
308
c5be433b 309/* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
e5a95ffb 310static char *
c5be433b 311get_emd_part(SV **prev_pathp, char *trailing_path, ...)
00dc2f4f 312{
dc9e4912 313 char base[10];
e5a95ffb 314 va_list ap;
e24c7c18 315 char mod_name[MAX_PATH+1];
00dc2f4f 316 char *ptr;
e5a95ffb
GS
317 char *optr;
318 char *strip;
273cf8d1 319 STRLEN baselen;
e5a95ffb
GS
320
321 va_start(ap, trailing_path);
322 strip = va_arg(ap, char *);
323
273cf8d1
GS
324 sprintf(base, "%d.%d", (int)PERL_REVISION, (int)PERL_VERSION);
325 baselen = strlen(base);
dc9e4912 326
8ac9c18d 327 if (!*w32_module_name) {
2fa86c13 328 set_w32_module_name();
95140b98 329 }
8ac9c18d 330 strcpy(mod_name, w32_module_name);
95140b98 331 ptr = strrchr(mod_name, '/');
e5a95ffb
GS
332 while (ptr && strip) {
333 /* look for directories to skip back */
334 optr = ptr;
00dc2f4f 335 *ptr = '\0';
95140b98 336 ptr = strrchr(mod_name, '/');
1c39adb2
GS
337 /* avoid stripping component if there is no slash,
338 * or it doesn't match ... */
e5a95ffb 339 if (!ptr || stricmp(ptr+1, strip) != 0) {
273cf8d1 340 /* ... but not if component matches m|5\.$patchlevel.*| */
1c39adb2 341 if (!ptr || !(*strip == '5' && *(ptr+1) == '5'
273cf8d1
GS
342 && strncmp(strip, base, baselen) == 0
343 && strncmp(ptr+1, base, baselen) == 0))
95140b98
GS
344 {
345 *optr = '/';
80252599
GS
346 ptr = optr;
347 }
00dc2f4f 348 }
e5a95ffb 349 strip = va_arg(ap, char *);
00dc2f4f 350 }
e5a95ffb
GS
351 if (!ptr) {
352 ptr = mod_name;
353 *ptr++ = '.';
95140b98 354 *ptr = '/';
00dc2f4f 355 }
e5a95ffb
GS
356 va_end(ap);
357 strcpy(++ptr, trailing_path);
358
dc9e4912 359 /* only add directory if it exists */
349ad1fe 360 if (GetFileAttributes(mod_name) != (DWORD) -1) {
dc9e4912 361 /* directory exists */
acfe0abc 362 dTHX;
c5be433b
GS
363 if (!*prev_pathp)
364 *prev_pathp = sv_2mortal(newSVpvn("",0));
f0c8bec2
SH
365 else if (SvPVX(*prev_pathp))
366 sv_catpvn(*prev_pathp, ";", 1);
c5be433b
GS
367 sv_catpv(*prev_pathp, mod_name);
368 return SvPVX(*prev_pathp);
00dc2f4f 369 }
00dc2f4f 370
4e205ed6 371 return NULL;
00dc2f4f
GS
372}
373
374char *
4ea817c6 375win32_get_privlib(const char *pl)
00dc2f4f 376{
acfe0abc 377 dTHX;
e5a95ffb
GS
378 char *stdlib = "lib";
379 char buffer[MAX_PATH+1];
4e205ed6 380 SV *sv = NULL;
00dc2f4f 381
e5a95ffb
GS
382 /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
383 sprintf(buffer, "%s-%s", stdlib, pl);
c5be433b
GS
384 if (!get_regstr(buffer, &sv))
385 (void)get_regstr(stdlib, &sv);
00dc2f4f 386
e5a95ffb 387 /* $stdlib .= ";$EMD/../../lib" */
4e205ed6 388 return get_emd_part(&sv, stdlib, ARCHNAME, "bin", NULL);
00dc2f4f
GS
389}
390
4ea817c6
GS
391static char *
392win32_get_xlib(const char *pl, const char *xlib, const char *libname)
00dc2f4f 393{
acfe0abc 394 dTHX;
e5a95ffb 395 char regstr[40];
e24c7c18 396 char pathstr[MAX_PATH+1];
4e205ed6
SP
397 SV *sv1 = NULL;
398 SV *sv2 = NULL;
00dc2f4f 399
4ea817c6
GS
400 /* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */
401 sprintf(regstr, "%s-%s", xlib, pl);
c5be433b 402 (void)get_regstr(regstr, &sv1);
e5a95ffb 403
4ea817c6
GS
404 /* $xlib .=
405 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */
406 sprintf(pathstr, "%s/%s/lib", libname, pl);
4e205ed6 407 (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, NULL);
00dc2f4f 408
4ea817c6
GS
409 /* $HKCU{$xlib} || $HKLM{$xlib} . ---; */
410 (void)get_regstr(xlib, &sv2);
00dc2f4f 411
4ea817c6
GS
412 /* $xlib .=
413 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */
414 sprintf(pathstr, "%s/lib", libname);
4e205ed6 415 (void)get_emd_part(&sv2, pathstr, ARCHNAME, "bin", pl, NULL);
e5a95ffb 416
51371543 417 if (!sv1 && !sv2)
4e205ed6 418 return NULL;
51371543
GS
419 if (!sv1)
420 return SvPVX(sv2);
421 if (!sv2)
422 return SvPVX(sv1);
e5a95ffb 423
349ad1fe
GS
424 sv_catpvn(sv1, ";", 1);
425 sv_catsv(sv1, sv2);
e5a95ffb 426
349ad1fe 427 return SvPVX(sv1);
68dc0745 428}
0a753a76 429
4ea817c6
GS
430char *
431win32_get_sitelib(const char *pl)
432{
433 return win32_get_xlib(pl, "sitelib", "site");
434}
435
436#ifndef PERL_VENDORLIB_NAME
437# define PERL_VENDORLIB_NAME "vendor"
438#endif
439
440char *
441win32_get_vendorlib(const char *pl)
442{
443 return win32_get_xlib(pl, "vendorlib", PERL_VENDORLIB_NAME);
444}
b4793f7f 445
2d7a9237 446static BOOL
08039b81 447has_shell_metachars(const char *ptr)
68dc0745
PP
448{
449 int inquote = 0;
450 char quote = '\0';
451
452 /*
453 * Scan string looking for redirection (< or >) or pipe
e200fe59
JD
454 * characters (|) that are not in a quoted string.
455 * Shell variable interpolation (%VAR%) can also happen inside strings.
68dc0745 456 */
9404a519 457 while (*ptr) {
68dc0745 458 switch(*ptr) {
e200fe59
JD
459 case '%':
460 return TRUE;
68dc0745
PP
461 case '\'':
462 case '\"':
9404a519
GS
463 if (inquote) {
464 if (quote == *ptr) {
68dc0745
PP
465 inquote = 0;
466 quote = '\0';
0a753a76 467 }
68dc0745
PP
468 }
469 else {
470 quote = *ptr;
471 inquote++;
472 }
473 break;
474 case '>':
475 case '<':
476 case '|':
9404a519 477 if (!inquote)
68dc0745
PP
478 return TRUE;
479 default:
480 break;
0a753a76 481 }
68dc0745
PP
482 ++ptr;
483 }
484 return FALSE;
0a753a76
PP
485}
486
32e30700 487#if !defined(PERL_IMPLICIT_SYS)
68dc0745
PP
488/* since the current process environment is being updated in util.c
489 * the library functions will get the correct environment
490 */
491PerlIO *
79d39d80 492Perl_my_popen(pTHX_ const char *cmd, const char *mode)
0a753a76
PP
493{
494#ifdef FIXCMD
7766f137
GS
495#define fixcmd(x) { \
496 char *pspace = strchr((x),' '); \
497 if (pspace) { \
498 char *p = (x); \
499 while (p < pspace) { \
500 if (*p == '/') \
501 *p = '\\'; \
502 p++; \
503 } \
504 } \
505 }
0a753a76
PP
506#else
507#define fixcmd(x)
508#endif
68dc0745 509 fixcmd(cmd);
45bc9206 510 PERL_FLUSHALL_FOR_CHILD;
0a753a76 511 return win32_popen(cmd, mode);
0a753a76
PP
512}
513
68dc0745 514long
4f63d024 515Perl_my_pclose(pTHX_ PerlIO *fp)
0a753a76
PP
516{
517 return win32_pclose(fp);
518}
c69f6586 519#endif
0a753a76 520
0cb96387
GS
521DllExport unsigned long
522win32_os_id(void)
0a753a76 523{
aeecf691 524 return (unsigned long)g_osver.dwPlatformId;
0a753a76
PP
525}
526
7766f137
GS
527DllExport int
528win32_getpid(void)
529{
922b1888 530 int pid;
7766f137 531#ifdef USE_ITHREADS
acfe0abc 532 dTHX;
7766f137
GS
533 if (w32_pseudo_id)
534 return -((int)w32_pseudo_id);
535#endif
922b1888
GS
536 pid = _getpid();
537 /* Windows 9x appears to always reports a pid for threads and processes
538 * that has the high bit set. So we treat the lower 31 bits as the
539 * "real" PID for Perl's purposes. */
540 if (IsWin95() && pid < 0)
541 pid = -pid;
542 return pid;
7766f137
GS
543}
544
ce1da67e
GS
545/* Tokenize a string. Words are null-separated, and the list
546 * ends with a doubled null. Any character (except null and
547 * including backslash) may be escaped by preceding it with a
548 * backslash (the backslash will be stripped).
549 * Returns number of words in result buffer.
550 */
551static long
dff6d3cd 552tokenize(const char *str, char **dest, char ***destv)
ce1da67e 553{
4e205ed6 554 char *retstart = NULL;
ce1da67e
GS
555 char **retvstart = 0;
556 int items = -1;
557 if (str) {
acfe0abc 558 dTHX;
ce1da67e
GS
559 int slen = strlen(str);
560 register char *ret;
561 register char **retv;
a02a5408
JC
562 Newx(ret, slen+2, char);
563 Newx(retv, (slen+3)/2, char*);
ce1da67e
GS
564
565 retstart = ret;
566 retvstart = retv;
567 *retv = ret;
568 items = 0;
569 while (*str) {
570 *ret = *str++;
571 if (*ret == '\\' && *str)
572 *ret = *str++;
573 else if (*ret == ' ') {
574 while (*str == ' ')
575 str++;
576 if (ret == retstart)
577 ret--;
578 else {
579 *ret = '\0';
580 ++items;
581 if (*str)
582 *++retv = ret+1;
583 }
584 }
585 else if (!*str)
586 ++items;
587 ret++;
588 }
4e205ed6 589 retvstart[items] = NULL;
ce1da67e
GS
590 *ret++ = '\0';
591 *ret = '\0';
592 }
593 *dest = retstart;
594 *destv = retvstart;
595 return items;
596}
597
598static void
2d7a9237 599get_shell(void)
0a753a76 600{
acfe0abc 601 dTHX;
ce1da67e 602 if (!w32_perlshell_tokens) {
174c211a
GS
603 /* we don't use COMSPEC here for two reasons:
604 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
605 * uncontrolled unportability of the ensuing scripts.
606 * 2. PERL5SHELL could be set to a shell that may not be fit for
607 * interactive use (which is what most programs look in COMSPEC
608 * for).
609 */
dff6d3cd 610 const char* defaultshell = (IsWinNT()
11998fdb 611 ? "cmd.exe /x/d/c" : "command.com /c");
2fb9ab56 612 const char *usershell = PerlEnv_getenv("PERL5SHELL");
ce1da67e
GS
613 w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
614 &w32_perlshell_tokens,
615 &w32_perlshell_vec);
68dc0745 616 }
0a753a76
PP
617}
618
68dc0745 619int
54725af6 620Perl_do_aspawn(pTHX_ SV *really, SV **mark, SV **sp)
0a753a76 621{
68dc0745 622 char **argv;
2d7a9237 623 char *str;
68dc0745 624 int status;
2d7a9237 625 int flag = P_WAIT;
68dc0745 626 int index = 0;
68dc0745 627
7918f24d
NC
628 PERL_ARGS_ASSERT_DO_ASPAWN;
629
2d7a9237
GS
630 if (sp <= mark)
631 return -1;
68dc0745 632
ce1da67e 633 get_shell();
a02a5408 634 Newx(argv, (sp - mark) + w32_perlshell_items + 2, char*);
2d7a9237
GS
635
636 if (SvNIOKp(*(mark+1)) && !SvPOKp(*(mark+1))) {
637 ++mark;
638 flag = SvIVx(*mark);
68dc0745
PP
639 }
640
9404a519 641 while (++mark <= sp) {
bb897dfc 642 if (*mark && (str = SvPV_nolen(*mark)))
2d7a9237
GS
643 argv[index++] = str;
644 else
645 argv[index++] = "";
68dc0745
PP
646 }
647 argv[index++] = 0;
3fadfdf1 648
2d7a9237 649 status = win32_spawnvp(flag,
bb897dfc 650 (const char*)(really ? SvPV_nolen(really) : argv[0]),
2d7a9237
GS
651 (const char* const*)argv);
652
80252599 653 if (status < 0 && (errno == ENOEXEC || errno == ENOENT)) {
2d7a9237 654 /* possible shell-builtin, invoke with shell */
ce1da67e
GS
655 int sh_items;
656 sh_items = w32_perlshell_items;
2d7a9237
GS
657 while (--index >= 0)
658 argv[index+sh_items] = argv[index];
ce1da67e
GS
659 while (--sh_items >= 0)
660 argv[sh_items] = w32_perlshell_vec[sh_items];
3fadfdf1 661
2d7a9237 662 status = win32_spawnvp(flag,
bb897dfc 663 (const char*)(really ? SvPV_nolen(really) : argv[0]),
2d7a9237
GS
664 (const char* const*)argv);
665 }
68dc0745 666
922b1888 667 if (flag == P_NOWAIT) {
40c7cc6d 668 PL_statusvalue = -1; /* >16bits hint for pp_system() */
922b1888
GS
669 }
670 else {
50892819 671 if (status < 0) {
0453d815 672 if (ckWARN(WARN_EXEC))
f98bc0c6 673 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't spawn \"%s\": %s", argv[0], strerror(errno));
50892819
GS
674 status = 255 * 256;
675 }
676 else
677 status *= 256;
b28d0864 678 PL_statusvalue = status;
5aabfad6 679 }
ce1da67e 680 Safefree(argv);
50892819 681 return (status);
68dc0745
PP
682}
683
dd7038b3
JH
684/* returns pointer to the next unquoted space or the end of the string */
685static char*
686find_next_space(const char *s)
687{
688 bool in_quotes = FALSE;
689 while (*s) {
690 /* ignore doubled backslashes, or backslash+quote */
691 if (*s == '\\' && (s[1] == '\\' || s[1] == '"')) {
692 s += 2;
693 }
694 /* keep track of when we're within quotes */
695 else if (*s == '"') {
696 s++;
697 in_quotes = !in_quotes;
698 }
699 /* break it up only at spaces that aren't in quotes */
700 else if (!in_quotes && isSPACE(*s))
701 return (char*)s;
702 else
703 s++;
704 }
705 return (char*)s;
706}
707
54725af6 708static int
08039b81 709do_spawn2(pTHX_ const char *cmd, int exectype)
68dc0745
PP
710{
711 char **a;
712 char *s;
713 char **argv;
714 int status = -1;
715 BOOL needToTry = TRUE;
2d7a9237 716 char *cmd2;
68dc0745 717
2d7a9237
GS
718 /* Save an extra exec if possible. See if there are shell
719 * metacharacters in it */
e200fe59 720 if (!has_shell_metachars(cmd)) {
a02a5408
JC
721 Newx(argv, strlen(cmd) / 2 + 2, char*);
722 Newx(cmd2, strlen(cmd) + 1, char);
68dc0745
PP
723 strcpy(cmd2, cmd);
724 a = argv;
725 for (s = cmd2; *s;) {
de030af3 726 while (*s && isSPACE(*s))
68dc0745
PP
727 s++;
728 if (*s)
729 *(a++) = s;
dd7038b3 730 s = find_next_space(s);
9404a519 731 if (*s)
68dc0745 732 *s++ = '\0';
0a753a76 733 }
4e205ed6 734 *a = NULL;
ce1da67e 735 if (argv[0]) {
6890e559
GS
736 switch (exectype) {
737 case EXECF_SPAWN:
738 status = win32_spawnvp(P_WAIT, argv[0],
739 (const char* const*)argv);
740 break;
741 case EXECF_SPAWN_NOWAIT:
742 status = win32_spawnvp(P_NOWAIT, argv[0],
743 (const char* const*)argv);
744 break;
745 case EXECF_EXEC:
746 status = win32_execvp(argv[0], (const char* const*)argv);
747 break;
748 }
2d7a9237 749 if (status != -1 || errno == 0)
68dc0745 750 needToTry = FALSE;
0a753a76 751 }
0a753a76 752 Safefree(argv);
68dc0745
PP
753 Safefree(cmd2);
754 }
2d7a9237 755 if (needToTry) {
ce1da67e
GS
756 char **argv;
757 int i = -1;
758 get_shell();
a02a5408 759 Newx(argv, w32_perlshell_items + 2, char*);
ce1da67e
GS
760 while (++i < w32_perlshell_items)
761 argv[i] = w32_perlshell_vec[i];
08039b81 762 argv[i++] = (char *)cmd;
4e205ed6 763 argv[i] = NULL;
6890e559
GS
764 switch (exectype) {
765 case EXECF_SPAWN:
766 status = win32_spawnvp(P_WAIT, argv[0],
767 (const char* const*)argv);
768 break;
769 case EXECF_SPAWN_NOWAIT:
770 status = win32_spawnvp(P_NOWAIT, argv[0],
771 (const char* const*)argv);
772 break;
773 case EXECF_EXEC:
774 status = win32_execvp(argv[0], (const char* const*)argv);
775 break;
776 }
ce1da67e
GS
777 cmd = argv[0];
778 Safefree(argv);
68dc0745 779 }
922b1888 780 if (exectype == EXECF_SPAWN_NOWAIT) {
40c7cc6d 781 PL_statusvalue = -1; /* >16bits hint for pp_system() */
922b1888
GS
782 }
783 else {
50892819 784 if (status < 0) {
0453d815 785 if (ckWARN(WARN_EXEC))
f98bc0c6 786 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't %s \"%s\": %s",
50892819
GS
787 (exectype == EXECF_EXEC ? "exec" : "spawn"),
788 cmd, strerror(errno));
789 status = 255 * 256;
790 }
791 else
792 status *= 256;
b28d0864 793 PL_statusvalue = status;
5aabfad6 794 }
50892819 795 return (status);
0a753a76
PP
796}
797
6890e559 798int
54725af6 799Perl_do_spawn(pTHX_ char *cmd)
6890e559 800{
7918f24d
NC
801 PERL_ARGS_ASSERT_DO_SPAWN;
802
54725af6 803 return do_spawn2(aTHX_ cmd, EXECF_SPAWN);
6890e559
GS
804}
805
2d7a9237 806int
54725af6 807Perl_do_spawn_nowait(pTHX_ char *cmd)
2d7a9237 808{
7918f24d
NC
809 PERL_ARGS_ASSERT_DO_SPAWN_NOWAIT;
810
54725af6 811 return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT);
2d7a9237
GS
812}
813
6890e559 814bool
79d39d80 815Perl_do_exec(pTHX_ const char *cmd)
6890e559 816{
7918f24d
NC
817 PERL_ARGS_ASSERT_DO_EXEC;
818
08039b81 819 do_spawn2(aTHX_ cmd, EXECF_EXEC);
6890e559
GS
820 return FALSE;
821}
822
68dc0745
PP
823/* The idea here is to read all the directory names into a string table
824 * (separated by nulls) and when one of the other dir functions is called
825 * return the pointer to the current file name.
826 */
c5be433b 827DllExport DIR *
0e06f75d 828win32_opendir(const char *filename)
0a753a76 829{
acfe0abc 830 dTHX;
95136add 831 DIR *dirp;
9404a519
GS
832 long len;
833 long idx;
834 char scanname[MAX_PATH+3];
c623ac67 835 Stat_t sbuf;
7fac1903 836 WIN32_FIND_DATAA aFindData;
35cf1ab6
JD
837 WIN32_FIND_DATAW wFindData;
838 bool using_wide;
839 char buffer[MAX_PATH*2];
840 char *ptr;
9404a519
GS
841
842 len = strlen(filename);
843 if (len > MAX_PATH)
844 return NULL;
68dc0745
PP
845
846 /* check to see if filename is a directory */
69d3ab13 847 if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
24caa93f 848 return NULL;
68dc0745 849
68dc0745 850 /* Get us a DIR structure */
a02a5408 851 Newxz(dirp, 1, DIR);
68dc0745
PP
852
853 /* Create the search pattern */
854 strcpy(scanname, filename);
23db2e2d
GS
855
856 /* bare drive name means look in cwd for drive */
857 if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') {
858 scanname[len++] = '.';
859 scanname[len++] = '/';
860 }
861 else if (scanname[len-1] != '/' && scanname[len-1] != '\\') {
9404a519 862 scanname[len++] = '/';
23db2e2d 863 }
9404a519
GS
864 scanname[len++] = '*';
865 scanname[len] = '\0';
68dc0745
PP
866
867 /* do the FindFirstFile call */
3839a0fa 868 if (IsWin2000()) {
35cf1ab6
JD
869 WCHAR wscanname[sizeof(scanname)];
870 MultiByteToWideChar(CP_ACP, 0, scanname, -1, wscanname, sizeof(wscanname)/sizeof(WCHAR));
871 dirp->handle = FindFirstFileW(PerlDir_mapW(wscanname), &wFindData);
872 using_wide = TRUE;
873 }
874 else {
875 dirp->handle = FindFirstFileA(PerlDir_mapA(scanname), &aFindData);
876 }
8c56068e 877 if (dirp->handle == INVALID_HANDLE_VALUE) {
95136add 878 DWORD err = GetLastError();
21e72512 879 /* FindFirstFile() fails on empty drives! */
95136add
GS
880 switch (err) {
881 case ERROR_FILE_NOT_FOUND:
882 return dirp;
883 case ERROR_NO_MORE_FILES:
884 case ERROR_PATH_NOT_FOUND:
885 errno = ENOENT;
886 break;
887 case ERROR_NOT_ENOUGH_MEMORY:
888 errno = ENOMEM;
889 break;
890 default:
891 errno = EINVAL;
892 break;
893 }
894 Safefree(dirp);
68dc0745
PP
895 return NULL;
896 }
897
35cf1ab6
JD
898 if (using_wide) {
899 BOOL use_default = FALSE;
900 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
901 wFindData.cFileName, -1,
902 buffer, sizeof(buffer), NULL, &use_default);
903 if (use_default && *wFindData.cAlternateFileName) {
904 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
905 wFindData.cAlternateFileName, -1,
906 buffer, sizeof(buffer), NULL, NULL);
907 }
908 ptr = buffer;
909 }
910 else {
911 ptr = aFindData.cFileName;
912 }
68dc0745
PP
913 /* now allocate the first part of the string table for
914 * the filenames that we find.
915 */
35cf1ab6 916 idx = strlen(ptr)+1;
95136add 917 if (idx < 256)
35cf1ab6 918 dirp->size = 256;
95136add
GS
919 else
920 dirp->size = idx;
a02a5408 921 Newx(dirp->start, dirp->size, char);
35cf1ab6 922 strcpy(dirp->start, ptr);
95136add
GS
923 dirp->nfiles++;
924 dirp->end = dirp->curr = dirp->start;
925 dirp->end += idx;
926 return dirp;
0a753a76
PP
927}
928
929
68dc0745
PP
930/* Readdir just returns the current string pointer and bumps the
931 * string pointer to the nDllExport entry.
932 */
c5be433b 933DllExport struct direct *
ce2e26e5 934win32_readdir(DIR *dirp)
0a753a76 935{
95136add 936 long len;
0a753a76 937
68dc0745
PP
938 if (dirp->curr) {
939 /* first set up the structure to return */
940 len = strlen(dirp->curr);
0f38926b 941 strcpy(dirp->dirstr.d_name, dirp->curr);
68dc0745 942 dirp->dirstr.d_namlen = len;
0a753a76 943
68dc0745 944 /* Fake an inode */
0f38926b 945 dirp->dirstr.d_ino = dirp->curr - dirp->start;
0a753a76 946
95136add 947 /* Now set up for the next call to readdir */
68dc0745 948 dirp->curr += len + 1;
95136add 949 if (dirp->curr >= dirp->end) {
acfe0abc 950 dTHX;
35cf1ab6
JD
951 BOOL res;
952 WIN32_FIND_DATAA aFindData;
953 char buffer[MAX_PATH*2];
954 char *ptr;
95136add
GS
955
956 /* finding the next file that matches the wildcard
957 * (which should be all of them in this directory!).
95136add 958 */
3839a0fa 959 if (IsWin2000()) {
35cf1ab6
JD
960 WIN32_FIND_DATAW wFindData;
961 res = FindNextFileW(dirp->handle, &wFindData);
962 if (res) {
963 BOOL use_default = FALSE;
964 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
965 wFindData.cFileName, -1,
966 buffer, sizeof(buffer), NULL, &use_default);
967 if (use_default && *wFindData.cAlternateFileName) {
968 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
969 wFindData.cAlternateFileName, -1,
970 buffer, sizeof(buffer), NULL, NULL);
971 }
972 ptr = buffer;
973 }
974 }
975 else {
976 res = FindNextFileA(dirp->handle, &aFindData);
977 ptr = aFindData.cFileName;
978 }
95136add 979 if (res) {
0f38926b 980 long endpos = dirp->end - dirp->start;
35cf1ab6 981 long newsize = endpos + strlen(ptr) + 1;
95136add 982 /* bump the string table size by enough for the
022735b4 983 * new name and its null terminator */
0f38926b
GS
984 while (newsize > dirp->size) {
985 long curpos = dirp->curr - dirp->start;
95136add
GS
986 dirp->size *= 2;
987 Renew(dirp->start, dirp->size, char);
0f38926b 988 dirp->curr = dirp->start + curpos;
95136add 989 }
35cf1ab6 990 strcpy(dirp->start + endpos, ptr);
0f38926b 991 dirp->end = dirp->start + newsize;
95136add
GS
992 dirp->nfiles++;
993 }
994 else
995 dirp->curr = NULL;
68dc0745 996 }
68dc0745 997 return &(dirp->dirstr);
3fadfdf1 998 }
68dc0745
PP
999 else
1000 return NULL;
0a753a76
PP
1001}
1002
68dc0745 1003/* Telldir returns the current string pointer position */
c5be433b 1004DllExport long
ce2e26e5 1005win32_telldir(DIR *dirp)
0a753a76 1006{
95136add 1007 return (dirp->curr - dirp->start);
0a753a76
PP
1008}
1009
1010
68dc0745 1011/* Seekdir moves the string pointer to a previously saved position
95136add 1012 * (returned by telldir).
68dc0745 1013 */
c5be433b 1014DllExport void
ce2e26e5 1015win32_seekdir(DIR *dirp, long loc)
0a753a76 1016{
95136add 1017 dirp->curr = dirp->start + loc;
0a753a76
PP
1018}
1019
68dc0745 1020/* Rewinddir resets the string pointer to the start */
c5be433b 1021DllExport void
ce2e26e5 1022win32_rewinddir(DIR *dirp)
0a753a76
PP
1023{
1024 dirp->curr = dirp->start;
1025}
1026
68dc0745 1027/* free the memory allocated by opendir */
c5be433b 1028DllExport int
ce2e26e5 1029win32_closedir(DIR *dirp)
0a753a76 1030{
acfe0abc 1031 dTHX;
95136add 1032 if (dirp->handle != INVALID_HANDLE_VALUE)
0f38926b 1033 FindClose(dirp->handle);
0a753a76
PP
1034 Safefree(dirp->start);
1035 Safefree(dirp);
68dc0745 1036 return 1;
0a753a76
PP
1037}
1038
1039
68dc0745
PP
1040/*
1041 * various stubs
1042 */
0a753a76
PP
1043
1044
68dc0745
PP
1045/* Ownership
1046 *
1047 * Just pretend that everyone is a superuser. NT will let us know if
1048 * we don\'t really have permission to do something.
1049 */
0a753a76
PP
1050
1051#define ROOT_UID ((uid_t)0)
1052#define ROOT_GID ((gid_t)0)
1053
68dc0745
PP
1054uid_t
1055getuid(void)
0a753a76 1056{
68dc0745 1057 return ROOT_UID;
0a753a76
PP
1058}
1059
68dc0745
PP
1060uid_t
1061geteuid(void)
0a753a76 1062{
68dc0745 1063 return ROOT_UID;
0a753a76
PP
1064}
1065
68dc0745
PP
1066gid_t
1067getgid(void)
0a753a76 1068{
68dc0745 1069 return ROOT_GID;
0a753a76
PP
1070}
1071
68dc0745
PP
1072gid_t
1073getegid(void)
0a753a76 1074{
68dc0745 1075 return ROOT_GID;
0a753a76
PP
1076}
1077
68dc0745 1078int
22239a37 1079setuid(uid_t auid)
3fadfdf1 1080{
22239a37 1081 return (auid == ROOT_UID ? 0 : -1);
0a753a76
PP
1082}
1083
68dc0745 1084int
22239a37 1085setgid(gid_t agid)
0a753a76 1086{
22239a37 1087 return (agid == ROOT_GID ? 0 : -1);
0a753a76
PP
1088}
1089
e34ffe5a
GS
1090char *
1091getlogin(void)
1092{
acfe0abc 1093 dTHX;
3352bfcb
GS
1094 char *buf = w32_getlogin_buffer;
1095 DWORD size = sizeof(w32_getlogin_buffer);
e34ffe5a
GS
1096 if (GetUserName(buf,&size))
1097 return buf;
1098 return (char*)NULL;
1099}
1100
b990f8c8
GS
1101int
1102chown(const char *path, uid_t owner, gid_t group)
1103{
1104 /* XXX noop */
1c1c7f20 1105 return 0;
b990f8c8
GS
1106}
1107
00b02797
JH
1108/*
1109 * XXX this needs strengthening (for PerlIO)
1110 * -- BKS, 11-11-200
1111*/
1112int mkstemp(const char *path)
1113{
1114 dTHX;
1115 char buf[MAX_PATH+1];
1116 int i = 0, fd = -1;
1117
1118retry:
1119 if (i++ > 10) { /* give up */
1120 errno = ENOENT;
1121 return -1;
1122 }
1123 if (!GetTempFileNameA((LPCSTR)path, "plr", 1, buf)) {
1124 errno = ENOENT;
1125 return -1;
1126 }
1127 fd = PerlLIO_open3(buf, O_CREAT|O_RDWR|O_EXCL, 0600);
1128 if (fd == -1)
1129 goto retry;
1130 return fd;
1131}
1132
0aaad0ff
GS
1133static long
1134find_pid(int pid)
0a753a76 1135{
acfe0abc 1136 dTHX;
7766f137
GS
1137 long child = w32_num_children;
1138 while (--child >= 0) {
eb160463 1139 if ((int)w32_child_pids[child] == pid)
0aaad0ff
GS
1140 return child;
1141 }
1142 return -1;
1143}
1144
1145static void
1146remove_dead_process(long child)
1147{
1148 if (child >= 0) {
acfe0abc 1149 dTHX;
0aaad0ff 1150 CloseHandle(w32_child_handles[child]);
c00206c8 1151 Move(&w32_child_handles[child+1], &w32_child_handles[child],
0aaad0ff 1152 (w32_num_children-child-1), HANDLE);
c00206c8 1153 Move(&w32_child_pids[child+1], &w32_child_pids[child],
0aaad0ff
GS
1154 (w32_num_children-child-1), DWORD);
1155 w32_num_children--;
f55ee38a 1156 }
f55ee38a
GS
1157}
1158
7766f137
GS
1159#ifdef USE_ITHREADS
1160static long
1161find_pseudo_pid(int pid)
1162{
acfe0abc 1163 dTHX;
7766f137
GS
1164 long child = w32_num_pseudo_children;
1165 while (--child >= 0) {
eb160463 1166 if ((int)w32_pseudo_child_pids[child] == pid)
7766f137
GS
1167 return child;
1168 }
1169 return -1;
1170}
1171
1172static void
1173remove_dead_pseudo_process(long child)
1174{
1175 if (child >= 0) {
acfe0abc 1176 dTHX;
7766f137 1177 CloseHandle(w32_pseudo_child_handles[child]);
c00206c8 1178 Move(&w32_pseudo_child_handles[child+1], &w32_pseudo_child_handles[child],
7766f137 1179 (w32_num_pseudo_children-child-1), HANDLE);
c00206c8 1180 Move(&w32_pseudo_child_pids[child+1], &w32_pseudo_child_pids[child],
7766f137 1181 (w32_num_pseudo_children-child-1), DWORD);
aeecf691
JD
1182 Move(&w32_pseudo_child_message_hwnds[child+1], &w32_pseudo_child_message_hwnds[child],
1183 (w32_num_pseudo_children-child-1), HWND);
7766f137
GS
1184 w32_num_pseudo_children--;
1185 }
1186}
1187#endif
1188
542cb85f
JD
1189static int
1190terminate_process(DWORD pid, HANDLE process_handle, int sig)
1191{
1192 switch(sig) {
1193 case 0:
1194 /* "Does process exist?" use of kill */
1195 return 1;
1196 case 2:
1197 if (GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid))
1198 return 1;
1199 break;
1200 case SIGBREAK:
1201 case SIGTERM:
1202 if (GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid))
1203 return 1;
1204 break;
1205 default: /* For now be backwards compatible with perl 5.6 */
1206 case 9:
1207 /* Note that we will only be able to kill processes owned by the
1208 * current process owner, even when we are running as an administrator.
1209 * To kill processes of other owners we would need to set the
1210 * 'SeDebugPrivilege' privilege before obtaining the process handle.
1211 */
1212 if (TerminateProcess(process_handle, sig))
1213 return 1;
1214 break;
1215 }
1216 return 0;
1217}
1218
1219/* Traverse process tree using ToolHelp functions */
1220static int
1221kill_process_tree_toolhelp(DWORD pid, int sig)
1222{
1223 HANDLE process_handle;
1224 HANDLE snapshot_handle;
1225 int killed = 0;
1226
1227 process_handle = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
6a4d57af 1228 if (process_handle == NULL)
542cb85f
JD
1229 return 0;
1230
1231 killed += terminate_process(pid, process_handle, sig);
1232
1233 snapshot_handle = pfnCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
1234 if (snapshot_handle != INVALID_HANDLE_VALUE) {
1235 PROCESSENTRY32 entry;
1236
1237 entry.dwSize = sizeof(entry);
1238 if (pfnProcess32First(snapshot_handle, &entry)) {
1239 do {
1240 if (entry.th32ParentProcessID == pid)
1241 killed += kill_process_tree_toolhelp(entry.th32ProcessID, sig);
1242 entry.dwSize = sizeof(entry);
1243 }
1244 while (pfnProcess32Next(snapshot_handle, &entry));
1245 }
1246 CloseHandle(snapshot_handle);
1247 }
1248 CloseHandle(process_handle);
1249 return killed;
1250}
1251
1252/* Traverse process tree using undocumented system information structures.
1253 * This is only necessary on Windows NT, which lacks the ToolHelp functions.
1254 */
1255static int
1256kill_process_tree_sysinfo(SYSTEM_PROCESSES *process_info, DWORD pid, int sig)
1257{
1258 HANDLE process_handle;
1259 SYSTEM_PROCESSES *p = process_info;
1260 int killed = 0;
1261
1262 process_handle = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
6a4d57af 1263 if (process_handle == NULL)
542cb85f
JD
1264 return 0;
1265
1266 killed += terminate_process(pid, process_handle, sig);
1267
1268 while (1) {
1269 if (p->InheritedFromProcessId == (DWORD)pid)
1270 killed += kill_process_tree_sysinfo(process_info, p->ProcessId, sig);
1271
1272 if (p->NextEntryDelta == 0)
1273 break;
1274
1275 p = (SYSTEM_PROCESSES*)((char*)p + p->NextEntryDelta);
1276 }
1277
1278 CloseHandle(process_handle);
1279 return killed;
1280}
1281
1282int
1283killpg(int pid, int sig)
1284{
1285 /* Use "documented" method whenever available */
1286 if (pfnCreateToolhelp32Snapshot && pfnProcess32First && pfnProcess32Next) {
1287 return kill_process_tree_toolhelp((DWORD)pid, sig);
1288 }
1289
1290 /* Fall back to undocumented Windows internals on Windows NT */
1291 if (pfnZwQuerySystemInformation) {
1292 dTHX;
1293 char *buffer;
1294 DWORD size = 0;
1295
1296 pfnZwQuerySystemInformation(SystemProcessesAndThreadsInformation, NULL, 0, &size);
1297 Newx(buffer, size, char);
1298
1299 if (pfnZwQuerySystemInformation(SystemProcessesAndThreadsInformation, buffer, size, NULL) >= 0) {
1300 int killed = kill_process_tree_sysinfo((SYSTEM_PROCESSES*)buffer, (DWORD)pid, sig);
1301 Safefree(buffer);
1302 return killed;
1303 }
1304 }
1305 return 0;
1306}
1307
1308static int
1309my_kill(int pid, int sig)
1310{
1311 int retval = 0;
1312 HANDLE process_handle;
1313
1314 if (sig < 0)
1315 return killpg(pid, -sig);
1316
1317 process_handle = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
6a4d57af
JD
1318 /* OpenProcess() returns NULL on error, *not* INVALID_HANDLE_VALUE */
1319 if (process_handle != NULL) {
542cb85f
JD
1320 retval = terminate_process(pid, process_handle, sig);
1321 CloseHandle(process_handle);
1322 }
1323 return retval;
1324}
1325
f55ee38a
GS
1326DllExport int
1327win32_kill(int pid, int sig)
1328{
acfe0abc 1329 dTHX;
c66b022d 1330 long child;
7766f137
GS
1331#ifdef USE_ITHREADS
1332 if (pid < 0) {
1333 /* it is a pseudo-forked child */
c66b022d 1334 child = find_pseudo_pid(-pid);
7766f137 1335 if (child >= 0) {
aeecf691 1336 HWND hwnd = w32_pseudo_child_message_hwnds[child];
85c508c3 1337 HANDLE hProcess = w32_pseudo_child_handles[child];
7e5f34c0
NIS
1338 switch (sig) {
1339 case 0:
c843839f 1340 /* "Does process exist?" use of kill */
7766f137 1341 return 0;
aeecf691 1342
7e5f34c0
NIS
1343 case 9:
1344 /* kill -9 style un-graceful exit */
1345 if (TerminateThread(hProcess, sig)) {
1346 remove_dead_pseudo_process(child);
1347 return 0;
1348 }
1349 break;
aeecf691
JD
1350
1351 default: {
1352 int count = 0;
1353 /* pseudo-process has not yet properly initialized if hwnd isn't set */
1354 while (hwnd == INVALID_HANDLE_VALUE && count < 5) {
1355 /* Yield and wait for the other thread to send us its message_hwnd */
1356 Sleep(0);
1357 win32_async_check(aTHX);
5e162c31 1358 hwnd = w32_pseudo_child_message_hwnds[child];
aeecf691
JD
1359 ++count;
1360 }
1361 if (hwnd != INVALID_HANDLE_VALUE) {
1362 /* We fake signals to pseudo-processes using Win32
1363 * message queue. In Win9X the pids are negative already. */
1364 if ((hwnd != NULL && PostMessage(hwnd, WM_USER_KILL, sig, 0)) ||
1365 PostThreadMessage(IsWin95() ? pid : -pid, WM_USER_KILL, sig, 0))
1366 {
1367 /* It might be us ... */
1368 PERL_ASYNC_CHECK();
1369 return 0;
1370 }
1371 }
7e5f34c0
NIS
1372 break;
1373 }
aeecf691 1374 } /* switch */
7766f137 1375 }
922b1888
GS
1376 else if (IsWin95()) {
1377 pid = -pid;
1378 goto alien_process;
1379 }
68dc0745 1380 }
7766f137
GS
1381 else
1382#endif
1383 {
c66b022d 1384 child = find_pid(pid);
7766f137 1385 if (child >= 0) {
542cb85f
JD
1386 if (my_kill(pid, sig)) {
1387 DWORD exitcode = 0;
1388 if (GetExitCodeProcess(w32_child_handles[child], &exitcode) &&
1389 exitcode != STILL_ACTIVE)
1390 {
1391 remove_dead_process(child);
1392 }
1393 return 0;
7e5f34c0 1394 }
7766f137
GS
1395 }
1396 else {
922b1888 1397alien_process:
542cb85f 1398 if (my_kill((IsWin95() ? -pid : pid), sig))
48db714f 1399 return 0;
7766f137
GS
1400 }
1401 }
1402 errno = EINVAL;
1403 return -1;
0a753a76 1404}
fbbbcc48 1405
68dc0745 1406DllExport int
c623ac67 1407win32_stat(const char *path, Stat_t *sbuf)
0a753a76 1408{
acfe0abc 1409 dTHX;
3fadfdf1 1410 char buffer[MAX_PATH+1];
68dc0745 1411 int l = strlen(path);
67fbe06e 1412 int res;
6b980173 1413 int nlink = 1;
44221b20 1414 BOOL expect_dir = FALSE;
0a753a76 1415
cba61fe1
JD
1416 GV *gv_sloppy = gv_fetchpvs("\027IN32_SLOPPY_STAT",
1417 GV_NOTQUAL, SVt_PV);
1418 BOOL sloppy = gv_sloppy && SvTRUE(GvSV(gv_sloppy));
1419
68dc0745
PP
1420 if (l > 1) {
1421 switch(path[l - 1]) {
e1dbac94 1422 /* FindFirstFile() and stat() are buggy with a trailing
44221b20 1423 * slashes, except for the root directory of a drive */
68dc0745 1424 case '\\':
44221b20
JD
1425 case '/':
1426 if (l > sizeof(buffer)) {
0b96339f
JD
1427 errno = ENAMETOOLONG;
1428 return -1;
1429 }
44221b20
JD
1430 --l;
1431 strncpy(buffer, path, l);
1432 /* remove additional trailing slashes */
1433 while (l > 1 && (buffer[l-1] == '/' || buffer[l-1] == '\\'))
1434 --l;
1435 /* add back slash if we otherwise end up with just a drive letter */
1436 if (l == 2 && isALPHA(buffer[0]) && buffer[1] == ':')
1437 buffer[l++] = '\\';
1438 buffer[l] = '\0';
1439 path = buffer;
1440 expect_dir = TRUE;
e1dbac94 1441 break;
44221b20 1442
23db2e2d 1443 /* FindFirstFile() is buggy with "x:", so add a dot :-( */
e1dbac94
GS
1444 case ':':
1445 if (l == 2 && isALPHA(path[0])) {
426c1a18
GS
1446 buffer[0] = path[0];
1447 buffer[1] = ':';
1448 buffer[2] = '.';
1449 buffer[3] = '\0';
e1dbac94 1450 l = 3;
426c1a18 1451 path = buffer;
e1dbac94
GS
1452 }
1453 break;
68dc0745
PP
1454 }
1455 }
6b980173 1456
8c56068e
JD
1457 path = PerlDir_mapA(path);
1458 l = strlen(path);
cba61fe1
JD
1459
1460 if (!sloppy) {
1461 /* We must open & close the file once; otherwise file attribute changes */
1462 /* might not yet have propagated to "other" hard links of the same file. */
1463 /* This also gives us an opportunity to determine the number of links. */
1464 HANDLE handle = CreateFileA(path, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
1465 if (handle != INVALID_HANDLE_VALUE) {
1466 BY_HANDLE_FILE_INFORMATION bhi;
1467 if (GetFileInformationByHandle(handle, &bhi))
1468 nlink = bhi.nNumberOfLinks;
1469 CloseHandle(handle);
1470 }
7fac1903 1471 }
6b980173 1472
8c56068e 1473 /* path will be mapped correctly above */
c623ac67 1474#if defined(WIN64) || defined(USE_LARGE_FILES)
8c56068e 1475 res = _stati64(path, sbuf);
c623ac67 1476#else
8c56068e 1477 res = stat(path, sbuf);
c623ac67 1478#endif
426c1a18 1479 sbuf->st_nlink = nlink;
6b980173 1480
24caa93f
GS
1481 if (res < 0) {
1482 /* CRT is buggy on sharenames, so make sure it really isn't.
1483 * XXX using GetFileAttributesEx() will enable us to set
426c1a18 1484 * sbuf->st_*time (but note that's not available on the
24caa93f 1485 * Windows of 1995) */
8c56068e 1486 DWORD r = GetFileAttributesA(path);
24caa93f 1487 if (r != 0xffffffff && (r & FILE_ATTRIBUTE_DIRECTORY)) {
426c1a18 1488 /* sbuf may still contain old garbage since stat() failed */
c623ac67 1489 Zero(sbuf, 1, Stat_t);
426c1a18 1490 sbuf->st_mode = S_IFDIR | S_IREAD;
24caa93f
GS
1491 errno = 0;
1492 if (!(r & FILE_ATTRIBUTE_READONLY))
426c1a18 1493 sbuf->st_mode |= S_IWRITE | S_IEXEC;
24caa93f
GS
1494 return 0;
1495 }
1496 }
24caa93f 1497 else {
e1dbac94
GS
1498 if (l == 3 && isALPHA(path[0]) && path[1] == ':'
1499 && (path[2] == '\\' || path[2] == '/'))
2293b0e9
AB
1500 {
1501 /* The drive can be inaccessible, some _stat()s are buggy */
8c56068e 1502 if (!GetVolumeInformationA(path,NULL,0,NULL,NULL,NULL,NULL,0)) {
2293b0e9
AB
1503 errno = ENOENT;
1504 return -1;
1505 }
1506 }
44221b20
JD
1507 if (expect_dir && !S_ISDIR(sbuf->st_mode)) {
1508 errno = ENOTDIR;
1509 return -1;
1510 }
038ae9a4
SH
1511 if (S_ISDIR(sbuf->st_mode)) {
1512 /* Ensure the "write" bit is switched off in the mode for
1513 * directories with the read-only attribute set. Borland (at least)
1514 * switches it on for directories, which is technically correct
1515 * (directories are indeed always writable unless denied by DACLs),
1516 * but we want stat() and -w to reflect the state of the read-only
1517 * attribute for symmetry with chmod(). */
1518 DWORD r = GetFileAttributesA(path);
1519 if (r != 0xffffffff && (r & FILE_ATTRIBUTE_READONLY)) {
1520 sbuf->st_mode &= ~S_IWRITE;
1521 }
1522 }
2293b0e9 1523#ifdef __BORLANDC__
038ae9a4
SH
1524 if (S_ISDIR(sbuf->st_mode)) {
1525 sbuf->st_mode |= S_IEXEC;
1526 }
426c1a18 1527 else if (S_ISREG(sbuf->st_mode)) {
d0650a05 1528 int perms;
67fbe06e
GS
1529 if (l >= 4 && path[l-4] == '.') {
1530 const char *e = path + l - 3;
1531 if (strnicmp(e,"exe",3)
1532 && strnicmp(e,"bat",3)
1533 && strnicmp(e,"com",3)
1534 && (IsWin95() || strnicmp(e,"cmd",3)))
426c1a18 1535 sbuf->st_mode &= ~S_IEXEC;
67fbe06e 1536 else
426c1a18 1537 sbuf->st_mode |= S_IEXEC;
67fbe06e
GS
1538 }
1539 else
426c1a18 1540 sbuf->st_mode &= ~S_IEXEC;
d0650a05
GS
1541 /* Propagate permissions to _group_ and _others_ */
1542 perms = sbuf->st_mode & (S_IREAD|S_IWRITE|S_IEXEC);
1543 sbuf->st_mode |= (perms>>3) | (perms>>6);
67fbe06e 1544 }
67fbe06e 1545#endif
2293b0e9 1546 }
67fbe06e 1547 return res;
0a753a76
PP
1548}
1549
bb27e7b6
JH
1550#define isSLASH(c) ((c) == '/' || (c) == '\\')
1551#define SKIP_SLASHES(s) \
1552 STMT_START { \
1553 while (*(s) && isSLASH(*(s))) \
1554 ++(s); \
1555 } STMT_END
1556#define COPY_NONSLASHES(d,s) \
1557 STMT_START { \
1558 while (*(s) && !isSLASH(*(s))) \
1559 *(d)++ = *(s)++; \
1560 } STMT_END
1561
8ac9c18d
GS
1562/* Find the longname of a given path. path is destructively modified.
1563 * It should have space for at least MAX_PATH characters. */
1564DllExport char *
1565win32_longpath(char *path)
1566{
1567 WIN32_FIND_DATA fdata;
1568 HANDLE fhand;
1569 char tmpbuf[MAX_PATH+1];
1570 char *tmpstart = tmpbuf;
1571 char *start = path;
1572 char sep;
1573 if (!path)
4e205ed6 1574 return NULL;
8ac9c18d
GS
1575
1576 /* drive prefix */
bb27e7b6 1577 if (isALPHA(path[0]) && path[1] == ':') {
8ac9c18d
GS
1578 start = path + 2;
1579 *tmpstart++ = path[0];
1580 *tmpstart++ = ':';
1581 }
1582 /* UNC prefix */
bb27e7b6 1583 else if (isSLASH(path[0]) && isSLASH(path[1])) {
8ac9c18d 1584 start = path + 2;
52fcf7ee
GS
1585 *tmpstart++ = path[0];
1586 *tmpstart++ = path[1];
bb27e7b6
JH
1587 SKIP_SLASHES(start);
1588 COPY_NONSLASHES(tmpstart,start); /* copy machine name */
8ac9c18d 1589 if (*start) {
bb27e7b6
JH
1590 *tmpstart++ = *start++;
1591 SKIP_SLASHES(start);
1592 COPY_NONSLASHES(tmpstart,start); /* copy share name */
8ac9c18d
GS
1593 }
1594 }
8ac9c18d 1595 *tmpstart = '\0';
bb27e7b6
JH
1596 while (*start) {
1597 /* copy initial slash, if any */
1598 if (isSLASH(*start)) {
1599 *tmpstart++ = *start++;
1600 *tmpstart = '\0';
1601 SKIP_SLASHES(start);
1602 }
1603
1604 /* FindFirstFile() expands "." and "..", so we need to pass
1605 * those through unmolested */
1606 if (*start == '.'
1607 && (!start[1] || isSLASH(start[1])
1608 || (start[1] == '.' && (!start[2] || isSLASH(start[2])))))
1609 {
1610 COPY_NONSLASHES(tmpstart,start); /* copy "." or ".." */
1611 *tmpstart = '\0';
1612 continue;
1613 }
1614
1615 /* if this is the end, bust outta here */
1616 if (!*start)
1617 break;
8ac9c18d 1618
bb27e7b6
JH
1619 /* now we're at a non-slash; walk up to next slash */
1620 while (*start && !isSLASH(*start))
8ac9c18d 1621 ++start;
8ac9c18d
GS
1622
1623 /* stop and find full name of component */
bb27e7b6 1624 sep = *start;
8ac9c18d
GS
1625 *start = '\0';
1626 fhand = FindFirstFile(path,&fdata);
bb27e7b6 1627 *start = sep;
8ac9c18d 1628 if (fhand != INVALID_HANDLE_VALUE) {
bb27e7b6
JH
1629 STRLEN len = strlen(fdata.cFileName);
1630 if ((STRLEN)(tmpbuf + sizeof(tmpbuf) - tmpstart) > len) {
1631 strcpy(tmpstart, fdata.cFileName);
1632 tmpstart += len;
1633 FindClose(fhand);
1634 }
1635 else {
1636 FindClose(fhand);
1637 errno = ERANGE;
4e205ed6 1638 return NULL;
bb27e7b6 1639 }
8ac9c18d
GS
1640 }
1641 else {
1642 /* failed a step, just return without side effects */
bf49b057 1643 /*PerlIO_printf(Perl_debug_log, "Failed to find %s\n", path);*/
bb27e7b6 1644 errno = EINVAL;
4e205ed6 1645 return NULL;
8ac9c18d
GS
1646 }
1647 }
1648 strcpy(path,tmpbuf);
1649 return path;
1650}
1651
aa2b96ec 1652static void
0934c9d9 1653out_of_memory(void)
aa2b96ec 1654{
ae6198af
JD
1655 if (PL_curinterp) {
1656 dTHX;
1657 /* Can't use PerlIO to write as it allocates memory */
1658 PerlLIO_write(PerlIO_fileno(Perl_error_log),
1659 PL_no_mem, strlen(PL_no_mem));
1660 my_exit(1);
1661 }
1662 exit(1);
aa2b96ec
JD
1663}
1664
1665/* The win32_ansipath() function takes a Unicode filename and converts it
1666 * into the current Windows codepage. If some characters cannot be mapped,
1667 * then it will convert the short name instead.
1668 *
1669 * The buffer to the ansi pathname must be freed with win32_free() when it
1670 * it no longer needed.
1671 *
1672 * The argument to win32_ansipath() must exist before this function is
1673 * called; otherwise there is no way to determine the short path name.
1674 *
1675 * Ideas for future refinement:
1676 * - Only convert those segments of the path that are not in the current
1677 * codepage, but leave the other segments in their long form.
1678 * - If the resulting name is longer than MAX_PATH, start converting
1679 * additional path segments into short names until the full name
1680 * is shorter than MAX_PATH. Shorten the filename part last!
1681 */
1682DllExport char *
1683win32_ansipath(const WCHAR *widename)
1684{
1685 char *name;
1686 BOOL use_default = FALSE;
1687 size_t widelen = wcslen(widename)+1;
1688 int len = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, widename, widelen,
1689 NULL, 0, NULL, NULL);
1690 name = win32_malloc(len);
1691 if (!name)
1692 out_of_memory();
1693
1694 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, widename, widelen,
1695 name, len, NULL, &use_default);
1696 if (use_default) {
aa2b96ec 1697 DWORD shortlen = GetShortPathNameW(widename, NULL, 0);
ae6198af
JD
1698 if (shortlen) {
1699 WCHAR *shortname = win32_malloc(shortlen*sizeof(WCHAR));
1700 if (!shortname)
1701 out_of_memory();
1702 shortlen = GetShortPathNameW(widename, shortname, shortlen)+1;
1703
1704 len = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, shortname, shortlen,
1705 NULL, 0, NULL, NULL);
1706 name = win32_realloc(name, len);
1707 if (!name)
1708 out_of_memory();
1709 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, shortname, shortlen,
1710 name, len, NULL, NULL);
1711 win32_free(shortname);
1712 }
aa2b96ec
JD
1713 }
1714 return name;
1715}
1716
0551aaa8
GS
1717DllExport char *
1718win32_getenv(const char *name)
1719{
acfe0abc 1720 dTHX;
0551aaa8 1721 DWORD needlen;
4e205ed6 1722 SV *curitem = NULL;
58a50f62 1723
8c56068e 1724 needlen = GetEnvironmentVariableA(name,NULL,0);
58a50f62 1725 if (needlen != 0) {
51371543 1726 curitem = sv_2mortal(newSVpvn("", 0));
8c56068e
JD
1727 do {
1728 SvGROW(curitem, needlen+1);
1729 needlen = GetEnvironmentVariableA(name,SvPVX(curitem),
1730 needlen);
1731 } while (needlen >= SvLEN(curitem));
1732 SvCUR_set(curitem, needlen);
0551aaa8 1733 }
c934e9d4 1734 else {
7a5f8e82 1735 /* allow any environment variables that begin with 'PERL'
c934e9d4 1736 to be stored in the registry */
51371543 1737 if (strncmp(name, "PERL", 4) == 0)
c5be433b 1738 (void)get_regstr(name, &curitem);
c69f6586 1739 }
51371543
GS
1740 if (curitem && SvCUR(curitem))
1741 return SvPVX(curitem);
58a50f62 1742
4e205ed6 1743 return NULL;
0551aaa8
GS
1744}
1745
ac5c734f
GS
1746DllExport int
1747win32_putenv(const char *name)
1748{
acfe0abc 1749 dTHX;
ac5c734f
GS
1750 char* curitem;
1751 char* val;
b813a9c7 1752 int relval = -1;
51371543 1753
73c4f7a1 1754 if (name) {
8c56068e
JD
1755 Newx(curitem,strlen(name)+1,char);
1756 strcpy(curitem, name);
1757 val = strchr(curitem, '=');
1758 if (val) {
1759 /* The sane way to deal with the environment.
1760 * Has these advantages over putenv() & co.:
1761 * * enables us to store a truly empty value in the
1762 * environment (like in UNIX).
1763 * * we don't have to deal with RTL globals, bugs and leaks.
1764 * * Much faster.
1765 * Why you may want to enable USE_WIN32_RTL_ENV:
1766 * * environ[] and RTL functions will not reflect changes,
1767 * which might be an issue if extensions want to access
1768 * the env. via RTL. This cuts both ways, since RTL will
1769 * not see changes made by extensions that call the Win32
1770 * functions directly, either.
1771 * GSAR 97-06-07
1772 */
1773 *val++ = '\0';
1774 if (SetEnvironmentVariableA(curitem, *val ? val : NULL))
1775 relval = 0;
1776 }
1777 Safefree(curitem);
ac5c734f
GS
1778 }
1779 return relval;
1780}
1781
d55594ae 1782static long
2d7a9237 1783filetime_to_clock(PFILETIME ft)
d55594ae 1784{
7766f137
GS
1785 __int64 qw = ft->dwHighDateTime;
1786 qw <<= 32;
1787 qw |= ft->dwLowDateTime;
1788 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
1789 return (long) qw;
d55594ae
GS
1790}
1791
f3986ebb
GS
1792DllExport int
1793win32_times(struct tms *timebuf)
0a753a76 1794{
d55594ae
GS
1795 FILETIME user;
1796 FILETIME kernel;
1797 FILETIME dummy;
50ee8e5e 1798 clock_t process_time_so_far = clock();
3fadfdf1 1799 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
d55594ae 1800 &kernel,&user)) {
2d7a9237
GS
1801 timebuf->tms_utime = filetime_to_clock(&user);
1802 timebuf->tms_stime = filetime_to_clock(&kernel);
d55594ae
GS
1803 timebuf->tms_cutime = 0;
1804 timebuf->tms_cstime = 0;
3fadfdf1 1805 } else {
d55594ae 1806 /* That failed - e.g. Win95 fallback to clock() */
50ee8e5e 1807 timebuf->tms_utime = process_time_so_far;
d55594ae
GS
1808 timebuf->tms_stime = 0;
1809 timebuf->tms_cutime = 0;
1810 timebuf->tms_cstime = 0;
1811 }
50ee8e5e 1812 return process_time_so_far;
0a753a76
PP
1813}
1814
9c51cf4c 1815/* fix utime() so it works on directories in NT */
ad0751ec
GS
1816static BOOL
1817filetime_from_time(PFILETIME pFileTime, time_t Time)
1818{
9c51cf4c 1819 struct tm *pTM = localtime(&Time);
ad0751ec 1820 SYSTEMTIME SystemTime;
9c51cf4c 1821 FILETIME LocalTime;
ad0751ec
GS
1822
1823 if (pTM == NULL)
1824 return FALSE;
1825
1826 SystemTime.wYear = pTM->tm_year + 1900;
1827 SystemTime.wMonth = pTM->tm_mon + 1;
1828 SystemTime.wDay = pTM->tm_mday;
1829 SystemTime.wHour = pTM->tm_hour;
1830 SystemTime.wMinute = pTM->tm_min;
1831 SystemTime.wSecond = pTM->tm_sec;
1832 SystemTime.wMilliseconds = 0;
1833
9c51cf4c
GS
1834 return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
1835 LocalFileTimeToFileTime(&LocalTime, pFileTime);
ad0751ec
GS
1836}
1837
1838DllExport int
7766f137
GS
1839win32_unlink(const char *filename)
1840{
acfe0abc 1841 dTHX;
7766f137
GS
1842 int ret;
1843 DWORD attrs;
1844
8c56068e
JD
1845 filename = PerlDir_mapA(filename);
1846 attrs = GetFileAttributesA(filename);
1847 if (attrs == 0xFFFFFFFF) {
1848 errno = ENOENT;
1849 return -1;
7766f137 1850 }
8c56068e
JD
1851 if (attrs & FILE_ATTRIBUTE_READONLY) {
1852 (void)SetFileAttributesA(filename, attrs & ~FILE_ATTRIBUTE_READONLY);
1853 ret = unlink(filename);
1854 if (ret == -1)
1855 (void)SetFileAttributesA(filename, attrs);
7766f137 1856 }
8c56068e
JD
1857 else
1858 ret = unlink(filename);
7766f137
GS
1859 return ret;
1860}
1861
1862DllExport int
3b405fc5 1863win32_utime(const char *filename, struct utimbuf *times)
ad0751ec 1864{
acfe0abc 1865 dTHX;
ad0751ec
GS
1866 HANDLE handle;
1867 FILETIME ftCreate;
1868 FILETIME ftAccess;
1869 FILETIME ftWrite;
1870 struct utimbuf TimeBuffer;
7fac1903 1871 int rc;
8c56068e
JD
1872
1873 filename = PerlDir_mapA(filename);
1874 rc = utime(filename, times);
1875
ad0751ec
GS
1876 /* EACCES: path specifies directory or readonly file */
1877 if (rc == 0 || errno != EACCES /* || !IsWinNT() */)
1878 return rc;
1879
1880 if (times == NULL) {
1881 times = &TimeBuffer;
1882 time(&times->actime);
1883 times->modtime = times->actime;
1884 }
1885
1886 /* This will (and should) still fail on readonly files */
8c56068e
JD
1887 handle = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE,
1888 FILE_SHARE_READ | FILE_SHARE_DELETE, NULL,
1889 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
ad0751ec
GS
1890 if (handle == INVALID_HANDLE_VALUE)
1891 return rc;
1892
1893 if (GetFileTime(handle, &ftCreate, &ftAccess, &ftWrite) &&
1894 filetime_from_time(&ftAccess, times->actime) &&
1895 filetime_from_time(&ftWrite, times->modtime) &&
1896 SetFileTime(handle, &ftCreate, &ftAccess, &ftWrite))
1897 {
1898 rc = 0;
1899 }
1900
1901 CloseHandle(handle);
1902 return rc;
1903}
1904
6e3b076d
JH
1905typedef union {
1906 unsigned __int64 ft_i64;
1907 FILETIME ft_val;
1908} FT_t;
1909
1910#ifdef __GNUC__
1911#define Const64(x) x##LL
1912#else
1913#define Const64(x) x##i64
1914#endif
1915/* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
1916#define EPOCH_BIAS Const64(116444736000000000)
1917
57ab3dfe
GS
1918/* NOTE: This does not compute the timezone info (doing so can be expensive,
1919 * and appears to be unsupported even by glibc) */
1920DllExport int
1921win32_gettimeofday(struct timeval *tp, void *not_used)
1922{
6e3b076d
JH
1923 FT_t ft;
1924
1925 /* this returns time in 100-nanosecond units (i.e. tens of usecs) */
1926 GetSystemTimeAsFileTime(&ft.ft_val);
1927
1928 /* seconds since epoch */
1929 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
1930
1931 /* microseconds remaining */
1932 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
1933
1934 return 0;
57ab3dfe
GS
1935}
1936
2d7a9237 1937DllExport int
b2af26b1
GS
1938win32_uname(struct utsname *name)
1939{
1940 struct hostent *hep;
1941 STRLEN nodemax = sizeof(name->nodename)-1;
b2af26b1 1942
aeecf691
JD
1943 /* sysname */
1944 switch (g_osver.dwPlatformId) {
1945 case VER_PLATFORM_WIN32_WINDOWS:
1946 strcpy(name->sysname, "Windows");
1947 break;
1948 case VER_PLATFORM_WIN32_NT:
1949 strcpy(name->sysname, "Windows NT");
1950 break;
1951 case VER_PLATFORM_WIN32s:
1952 strcpy(name->sysname, "Win32s");
1953 break;
1954 default:
1955 strcpy(name->sysname, "Win32 Unknown");
1956 break;
b2af26b1 1957 }
aeecf691
JD
1958
1959 /* release */
1960 sprintf(name->release, "%d.%d",
1961 g_osver.dwMajorVersion, g_osver.dwMinorVersion);
1962
1963 /* version */
1964 sprintf(name->version, "Build %d",
1965 g_osver.dwPlatformId == VER_PLATFORM_WIN32_NT
1966 ? g_osver.dwBuildNumber : (g_osver.dwBuildNumber & 0xffff));
1967 if (g_osver.szCSDVersion[0]) {
1968 char *buf = name->version + strlen(name->version);
1969 sprintf(buf, " (%s)", g_osver.szCSDVersion);
b2af26b1
GS
1970 }
1971
1972 /* nodename */
1973 hep = win32_gethostbyname("localhost");
1974 if (hep) {
1975 STRLEN len = strlen(hep->h_name);
1976 if (len <= nodemax) {
1977 strcpy(name->nodename, hep->h_name);
1978 }
1979 else {
1980 strncpy(name->nodename, hep->h_name, nodemax);
1981 name->nodename[nodemax] = '\0';
1982 }
1983 }
1984 else {
1985 DWORD sz = nodemax;
1986 if (!GetComputerName(name->nodename, &sz))
1987 *name->nodename = '\0';
1988 }
1989
1990 /* machine (architecture) */
1991 {
1992 SYSTEM_INFO info;
fe537c65 1993 DWORD procarch;
b2af26b1
GS
1994 char *arch;
1995 GetSystemInfo(&info);
a6c40364 1996
6f24f39d
JK
1997#if (defined(__BORLANDC__)&&(__BORLANDC__<=0x520)) \
1998 || (defined(__MINGW32__) && !defined(_ANONYMOUS_UNION))
fe537c65 1999 procarch = info.u.s.wProcessorArchitecture;
a6c40364 2000#else
fe537c65 2001 procarch = info.wProcessorArchitecture;
a6c40364 2002#endif
fe537c65 2003 switch (procarch) {
b2af26b1
GS
2004 case PROCESSOR_ARCHITECTURE_INTEL:
2005 arch = "x86"; break;
2006 case PROCESSOR_ARCHITECTURE_MIPS:
2007 arch = "mips"; break;
2008 case PROCESSOR_ARCHITECTURE_ALPHA:
2009 arch = "alpha"; break;
2010 case PROCESSOR_ARCHITECTURE_PPC:
2011 arch = "ppc"; break;
fe537c65
GS
2012#ifdef PROCESSOR_ARCHITECTURE_SHX
2013 case PROCESSOR_ARCHITECTURE_SHX:
2014 arch = "shx"; break;
2015#endif
2016#ifdef PROCESSOR_ARCHITECTURE_ARM
2017 case PROCESSOR_ARCHITECTURE_ARM:
2018 arch = "arm"; break;
2019#endif
2020#ifdef PROCESSOR_ARCHITECTURE_IA64
2021 case PROCESSOR_ARCHITECTURE_IA64:
2022 arch = "ia64"; break;
2023#endif
2024#ifdef PROCESSOR_ARCHITECTURE_ALPHA64
2025 case PROCESSOR_ARCHITECTURE_ALPHA64:
2026 arch = "alpha64"; break;
2027#endif
2028#ifdef PROCESSOR_ARCHITECTURE_MSIL
2029 case PROCESSOR_ARCHITECTURE_MSIL:
2030 arch = "msil"; break;
2031#endif
2032#ifdef PROCESSOR_ARCHITECTURE_AMD64
2033 case PROCESSOR_ARCHITECTURE_AMD64:
2034 arch = "amd64"; break;
2035#endif
2036#ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
2037 case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
2038 arch = "ia32-64"; break;
2039#endif
2040#ifdef PROCESSOR_ARCHITECTURE_UNKNOWN
2041 case PROCESSOR_ARCHITECTURE_UNKNOWN:
b2af26b1 2042 arch = "unknown"; break;
fe537c65
GS
2043#endif
2044 default:
2045 sprintf(name->machine, "unknown(0x%x)", procarch);
2046 arch = name->machine;
2047 break;
b2af26b1 2048 }
fe537c65
GS
2049 if (name->machine != arch)
2050 strcpy(name->machine, arch);
b2af26b1
GS
2051 }
2052 return 0;
2053}
2054
8fb3fcfb
NIS
2055/* Timing related stuff */
2056
3fadfdf1
NIS
2057int
2058do_raise(pTHX_ int sig)
2059{
2060 if (sig < SIG_SIZE) {
2061 Sighandler_t handler = w32_sighandler[sig];
2062 if (handler == SIG_IGN) {
2063 return 0;
2064 }
2065 else if (handler != SIG_DFL) {
2066 (*handler)(sig);
2067 return 0;
2068 }
2069 else {
2070 /* Choose correct default behaviour */
2071 switch (sig) {
2072#ifdef SIGCLD
2073 case SIGCLD:
2074#endif
2075#ifdef SIGCHLD
2076 case SIGCHLD:
2077#endif
2078 case 0:
2079 return 0;
2080 case SIGTERM:
2081 default:
2082 break;
2083 }
2084 }
2085 }
2086 /* Tell caller to exit thread/process as approriate */
2087 return 1;
2088}
2089
2090void
2091sig_terminate(pTHX_ int sig)
2092{
2093 Perl_warn(aTHX_ "Terminating on signal SIG%s(%d)\n",PL_sig_name[sig], sig);
2094 /* exit() seems to be safe, my_exit() or die() is a problem in ^C
2095 thread
2096 */
2097 exit(sig);
2098}
2099
8fb3fcfb
NIS
2100DllExport int
2101win32_async_check(pTHX)
2102{
2103 MSG msg;
aeecf691
JD
2104 HWND hwnd = w32_message_hwnd;
2105
099b16d3
RM
2106 /* Reset w32_poll_count before doing anything else, incase we dispatch
2107 * messages that end up calling back into perl */
aeecf691
JD
2108 w32_poll_count = 0;
2109
099b16d3
RM
2110 if (hwnd != INVALID_HANDLE_VALUE) {
2111 /* Passing PeekMessage -1 as HWND (2nd arg) only gets PostThreadMessage() messages
2112 * and ignores window messages - should co-exist better with windows apps e.g. Tk
2113 */
2114 if (hwnd == NULL)
2115 hwnd = (HWND)-1;
2116
2117 while (PeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE|PM_NOYIELD) ||
2118 PeekMessage(&msg, hwnd, WM_USER_MIN, WM_USER_MAX, PM_REMOVE|PM_NOYIELD))
2119 {
2120 /* re-post a WM_QUIT message (we'll mark it as read later) */
2121 if(msg.message == WM_QUIT) {
2122 PostQuitMessage((int)msg.wParam);
2123 break;
2124 }
8fb3fcfb 2125
099b16d3
RM
2126 if(!CallMsgFilter(&msg, MSGF_USER))
2127 {
2128 TranslateMessage(&msg);
2129 DispatchMessage(&msg);
aeecf691 2130 }
099b16d3 2131 }
8fb3fcfb
NIS
2132 }
2133
099b16d3
RM
2134 /* Call PeekMessage() to mark all pending messages in the queue as "old".
2135 * This is necessary when we are being called by win32_msgwait() to
2136 * make sure MsgWaitForMultipleObjects() stops reporting the same waiting
2137 * message over and over. An example how this can happen is when
2138 * Perl is calling win32_waitpid() inside a GUI application and the GUI
2139 * is generating messages before the process terminated.
2140 */
4386d69d 2141 PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE|PM_NOYIELD);
099b16d3 2142
7e5f34c0 2143 /* Above or other stuff may have set a signal flag */
099b16d3
RM
2144 if (PL_sig_pending)
2145 despatch_signals();
2146
aeecf691 2147 return 1;
8fb3fcfb
NIS
2148}
2149
089197fa
GS
2150/* This function will not return until the timeout has elapsed, or until
2151 * one of the handles is ready. */
8fb3fcfb
NIS
2152DllExport DWORD
2153win32_msgwait(pTHX_ DWORD count, LPHANDLE handles, DWORD timeout, LPDWORD resultp)
2154{
2155 /* We may need several goes at this - so compute when we stop */
2156 DWORD ticks = 0;
2157 if (timeout != INFINITE) {
2158 ticks = GetTickCount();
2159 timeout += ticks;
2160 }
2161 while (1) {
039698bb 2162 DWORD result = MsgWaitForMultipleObjects(count,handles,FALSE,timeout-ticks, QS_POSTMESSAGE|QS_TIMER|QS_SENDMESSAGE);
8fb3fcfb
NIS
2163 if (resultp)
2164 *resultp = result;
2165 if (result == WAIT_TIMEOUT) {
3fadfdf1
NIS
2166 /* Ran out of time - explicit return of zero to avoid -ve if we
2167 have scheduling issues
2168 */
8fb3fcfb
NIS
2169 return 0;
2170 }
2171 if (timeout != INFINITE) {
2172 ticks = GetTickCount();
2173 }
2174 if (result == WAIT_OBJECT_0 + count) {
2175 /* Message has arrived - check it */
089197fa 2176 (void)win32_async_check(aTHX);
8fb3fcfb
NIS
2177 }
2178 else {
2179 /* Not timeout or message - one of handles is ready */
2180 break;
2181 }
2182 }
2183 /* compute time left to wait */
2184 ticks = timeout - ticks;
2185 /* If we are past the end say zero */
2186 return (ticks > 0) ? ticks : 0;
2187}
2188
932b7487
RC
2189int
2190win32_internal_wait(int *status, DWORD timeout)
2191{
2192 /* XXX this wait emulation only knows about processes
2193 * spawned via win32_spawnvp(P_NOWAIT, ...).
2194 */
2195 dTHX;
2196 int i, retval;
2197 DWORD exitcode, waitcode;
2198
2199#ifdef USE_ITHREADS
2200 if (w32_num_pseudo_children) {
8fb3fcfb
NIS
2201 win32_msgwait(aTHX_ w32_num_pseudo_children, w32_pseudo_child_handles,
2202 timeout, &waitcode);
932b7487
RC
2203 /* Time out here if there are no other children to wait for. */
2204 if (waitcode == WAIT_TIMEOUT) {
2205 if (!w32_num_children) {
2206 return 0;
2207 }
2208 }
2209 else if (waitcode != WAIT_FAILED) {
2210 if (waitcode >= WAIT_ABANDONED_0
2211 && waitcode < WAIT_ABANDONED_0 + w32_num_pseudo_children)
2212 i = waitcode - WAIT_ABANDONED_0;
2213 else
2214 i = waitcode - WAIT_OBJECT_0;
2215 if (GetExitCodeThread(w32_pseudo_child_handles[i], &exitcode)) {
2216 *status = (int)((exitcode & 0xff) << 8);
2217 retval = (int)w32_pseudo_child_pids[i];
2218 remove_dead_pseudo_process(i);
2219 return -retval;
2220 }
2221 }
2222 }
2223#endif
2224
2225 if (!w32_num_children) {
2226 errno = ECHILD;
2227 return -1;
2228 }
2229
2230 /* if a child exists, wait for it to die */
8fb3fcfb 2231 win32_msgwait(aTHX_ w32_num_children, w32_child_handles, timeout, &waitcode);
932b7487
RC
2232 if (waitcode == WAIT_TIMEOUT) {
2233 return 0;
2234 }
2235 if (waitcode != WAIT_FAILED) {
2236 if (waitcode >= WAIT_ABANDONED_0
2237 && waitcode < WAIT_ABANDONED_0 + w32_num_children)
2238 i = waitcode - WAIT_ABANDONED_0;
2239 else
2240 i = waitcode - WAIT_OBJECT_0;
2241 if (GetExitCodeProcess(w32_child_handles[i], &exitcode) ) {
2242 *status = (int)((exitcode & 0xff) << 8);
2243 retval = (int)w32_child_pids[i];
2244 remove_dead_process(i);
2245 return retval;
2246 }
2247 }
2248
932b7487
RC
2249 errno = GetLastError();
2250 return -1;
2251}
2252
b2af26b1 2253DllExport int
f55ee38a
GS
2254win32_waitpid(int pid, int *status, int flags)
2255{
acfe0abc 2256 dTHX;
922b1888 2257 DWORD timeout = (flags & WNOHANG) ? 0 : INFINITE;
0aaad0ff 2258 int retval = -1;
c66b022d 2259 long child;
7766f137 2260 if (pid == -1) /* XXX threadid == 1 ? */
932b7487 2261 return win32_internal_wait(status, timeout);
7766f137
GS
2262#ifdef USE_ITHREADS
2263 else if (pid < 0) {
c66b022d 2264 child = find_pseudo_pid(-pid);
7766f137
GS
2265 if (child >= 0) {
2266 HANDLE hThread = w32_pseudo_child_handles[child];
8fb3fcfb
NIS
2267 DWORD waitcode;
2268 win32_msgwait(aTHX_ 1, &hThread, timeout, &waitcode);
2f67576d
BC
2269 if (waitcode == WAIT_TIMEOUT) {
2270 return 0;
2271 }
8fb3fcfb 2272 else if (waitcode == WAIT_OBJECT_0) {
7766f137
GS
2273 if (GetExitCodeThread(hThread, &waitcode)) {
2274 *status = (int)((waitcode & 0xff) << 8);
2275 retval = (int)w32_pseudo_child_pids[child];
2276 remove_dead_pseudo_process(child);
68a29c53 2277 return -retval;
7766f137
GS
2278 }
2279 }
2280 else
2281 errno = ECHILD;
2282 }
922b1888
GS
2283 else if (IsWin95()) {
2284 pid = -pid;
2285 goto alien_process;
2286 }
7766f137
GS
2287 }
2288#endif
f55ee38a 2289 else {
922b1888
GS
2290 HANDLE hProcess;
2291 DWORD waitcode;
c66b022d 2292 child = find_pid(pid);
0aaad0ff 2293 if (child >= 0) {
922b1888 2294 hProcess = w32_child_handles[child];
8fb3fcfb 2295 win32_msgwait(aTHX_ 1, &hProcess, timeout, &waitcode);
a7867d0a
GS
2296 if (waitcode == WAIT_TIMEOUT) {
2297 return 0;
2298 }
8fb3fcfb 2299 else if (waitcode == WAIT_OBJECT_0) {
922b1888
GS
2300 if (GetExitCodeProcess(hProcess, &waitcode)) {
2301 *status = (int)((waitcode & 0xff) << 8);
2302 retval = (int)w32_child_pids[child];
2303 remove_dead_process(child);
2304 return retval;
2305 }
a7867d0a 2306 }
0aaad0ff
GS
2307 else
2308 errno = ECHILD;
2309 }
2310 else {
922b1888
GS
2311alien_process:
2312 hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE,
2313 (IsWin95() ? -pid : pid));
2314 if (hProcess) {
8fb3fcfb 2315 win32_msgwait(aTHX_ 1, &hProcess, timeout, &waitcode);
922b1888 2316 if (waitcode == WAIT_TIMEOUT) {
48db714f 2317 CloseHandle(hProcess);
922b1888
GS
2318 return 0;
2319 }
8fb3fcfb 2320 else if (waitcode == WAIT_OBJECT_0) {
922b1888
GS
2321 if (GetExitCodeProcess(hProcess, &waitcode)) {
2322 *status = (int)((waitcode & 0xff) << 8);
2323 CloseHandle(hProcess);
2324 return pid;
2325 }
2326 }
2327 CloseHandle(hProcess);
2328 }
2329 else
2330 errno = ECHILD;
0aaad0ff 2331 }
f55ee38a 2332 }
3fadfdf1 2333 return retval >= 0 ? pid : retval;
f55ee38a
GS
2334}
2335
2336DllExport int
2d7a9237
GS
2337win32_wait(int *status)
2338{
932b7487 2339 return win32_internal_wait(status, INFINITE);
2d7a9237 2340}
d55594ae 2341
8fb3fcfb
NIS
2342DllExport unsigned int
2343win32_sleep(unsigned int t)
d55594ae 2344{
acfe0abc 2345 dTHX;
8fb3fcfb
NIS
2346 /* Win32 times are in ms so *1000 in and /1000 out */
2347 return win32_msgwait(aTHX_ 0, NULL, t*1000, NULL)/1000;
d55594ae
GS
2348}
2349
f3986ebb
GS
2350DllExport unsigned int
2351win32_alarm(unsigned int sec)
0a753a76 2352{
3fadfdf1 2353 /*
d55594ae 2354 * the 'obvious' implentation is SetTimer() with a callback
3fadfdf1
NIS
2355 * which does whatever receiving SIGALRM would do
2356 * we cannot use SIGALRM even via raise() as it is not
d55594ae 2357 * one of the supported codes in <signal.h>
3fadfdf1 2358 */
acfe0abc 2359 dTHX;
aeecf691
JD
2360
2361 if (w32_message_hwnd == INVALID_HANDLE_VALUE)
2362 w32_message_hwnd = win32_create_message_window();
2363
8fb3fcfb 2364 if (sec) {
aeecf691
JD
2365 if (w32_message_hwnd == NULL)
2366 w32_timerid = SetTimer(NULL, w32_timerid, sec*1000, NULL);
2367 else {
2368 w32_timerid = 1;
2369 SetTimer(w32_message_hwnd, w32_timerid, sec*1000, NULL);
2370 }
8fb3fcfb
NIS
2371 }
2372 else {
2373 if (w32_timerid) {
aeecf691
JD
2374 KillTimer(w32_message_hwnd, w32_timerid);
2375 w32_timerid = 0;
8fb3fcfb 2376 }
3fadfdf1 2377 }
afe91769 2378 return 0;
0a753a76
PP
2379}
2380
26618a56 2381#ifdef HAVE_DES_FCRYPT
2d77217b 2382extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf);
ff95b63e 2383#endif
26618a56
GS
2384
2385DllExport char *
2386win32_crypt(const char *txt, const char *salt)
2387{
acfe0abc 2388 dTHX;
ff95b63e 2389#ifdef HAVE_DES_FCRYPT
3352bfcb 2390 return des_fcrypt(txt, salt, w32_crypt_buffer);
ff95b63e 2391#else
25dbdbbc 2392 Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia.");
4e205ed6 2393 return NULL;
ff95b63e 2394#endif
26618a56 2395}
26618a56 2396
9e5f57de 2397#ifdef USE_FIXED_OSFHANDLE
390b85e7
GS
2398
2399#define FOPEN 0x01 /* file handle open */
b181b6fb 2400#define FNOINHERIT 0x10 /* file handle opened O_NOINHERIT */
390b85e7
GS
2401#define FAPPEND 0x20 /* file handle opened O_APPEND */
2402#define FDEV 0x40 /* file handle refers to device */
2403#define FTEXT 0x80 /* file handle is in text mode */
2404
390b85e7 2405/***
c623ac67 2406*int my_open_osfhandle(intptr_t osfhandle, int flags) - open C Runtime file handle
390b85e7
GS
2407*
2408*Purpose:
2409* This function allocates a free C Runtime file handle and associates
2410* it with the Win32 HANDLE specified by the first parameter. This is a
9e5f57de
GS
2411* temperary fix for WIN95's brain damage GetFileType() error on socket
2412* we just bypass that call for socket
2413*
2414* This works with MSVC++ 4.0+ or GCC/Mingw32
390b85e7
GS
2415*
2416*Entry:
c623ac67 2417* intptr_t osfhandle - Win32 HANDLE to associate with C Runtime file handle.
390b85e7
GS
2418* int flags - flags to associate with C Runtime file handle.
2419*
2420*Exit:
2421* returns index of entry in fh, if successful
2422* return -1, if no free entry is found
2423*
2424*Exceptions:
2425*
2426*******************************************************************************/
2427
9e5f57de
GS
2428/*
2429 * we fake up some parts of the CRT that aren't exported by MSVCRT.dll
2430 * this lets sockets work on Win9X with GCC and should fix the problems
2431 * with perl95.exe
2432 * -- BKS, 1-23-2000
2433*/
2434
9e5f57de
GS
2435/* create an ioinfo entry, kill its handle, and steal the entry */
2436
b181b6fb
GS
2437static int
2438_alloc_osfhnd(void)
9e5f57de
GS
2439{
2440 HANDLE hF = CreateFile("NUL", 0, 0, NULL, OPEN_ALWAYS, 0, NULL);
c623ac67 2441 int fh = _open_osfhandle((intptr_t)hF, 0);
9e5f57de
GS
2442 CloseHandle(hF);
2443 if (fh == -1)
2444 return fh;
2445 EnterCriticalSection(&(_pioinfo(fh)->lock));
2446 return fh;
2447}
2448
390b85e7 2449static int
c623ac67 2450my_open_osfhandle(intptr_t osfhandle, int flags)
390b85e7
GS
2451{
2452 int fh;
2453 char fileflags; /* _osfile flags */
2454
2455 /* copy relevant flags from second parameter */
2456 fileflags = FDEV;
2457
9404a519 2458 if (flags & O_APPEND)
390b85e7
GS
2459 fileflags |= FAPPEND;
2460
9404a519 2461 if (flags & O_TEXT)
390b85e7
GS
2462 fileflags |= FTEXT;
2463
b181b6fb
GS
2464 if (flags & O_NOINHERIT)
2465 fileflags |= FNOINHERIT;
2466
390b85e7 2467 /* attempt to allocate a C Runtime file handle */
9404a519 2468 if ((fh = _alloc_osfhnd()) == -1) {
390b85e7
GS
2469 errno = EMFILE; /* too many open files */
2470 _doserrno = 0L; /* not an OS error */
2471 return -1; /* return error to caller */
2472 }
2473
2474 /* the file is open. now, set the info in _osfhnd array */
2475 _set_osfhnd(fh, osfhandle);
2476
2477 fileflags |= FOPEN; /* mark as open */
2478
390b85e7 2479 _osfile(fh) = fileflags; /* set osfile entry */
dd8f4818 2480 LeaveCriticalSection(&_pioinfo(fh)->lock);
390b85e7
GS
2481
2482 return fh; /* return handle */
2483}
2484
f3986ebb 2485#endif /* USE_FIXED_OSFHANDLE */
390b85e7
GS
2486
2487/* simulate flock by locking a range on the file */
2488
2489#define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
2490#define LK_LEN 0xffff0000
2491
f3986ebb
GS
2492DllExport int
2493win32_flock(int fd, int oper)
390b85e7
GS
2494{
2495 OVERLAPPED o;
2496 int i = -1;
2497 HANDLE fh;
2498
f3986ebb 2499 if (!IsWinNT()) {
acfe0abc 2500 dTHX;
4f63d024 2501 Perl_croak_nocontext("flock() unimplemented on this platform");
f3986ebb
GS
2502 return -1;
2503 }
390b85e7
GS
2504 fh = (HANDLE)_get_osfhandle(fd);
2505 memset(&o, 0, sizeof(o));
2506
2507 switch(oper) {
2508 case LOCK_SH: /* shared lock */
2509 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
2510 break;
2511 case LOCK_EX: /* exclusive lock */
2512 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
2513 break;
2514 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
2515 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
2516 break;
2517 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
2518 LK_ERR(LockFileEx(fh,
2519 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
2520 0, LK_LEN, 0, &o),i);
2521 break;
2522 case LOCK_UN: /* unlock lock */
2523 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
2524 break;
2525 default: /* unknown */
2526 errno = EINVAL;
2527 break;
2528 }
2529 return i;
2530}
2531
2532#undef LK_ERR
2533#undef LK_LEN
2534
68dc0745
PP
2535/*
2536 * redirected io subsystem for all XS modules
2537 *
2538 */
0a753a76 2539
68dc0745
PP
2540DllExport int *
2541win32_errno(void)
0a753a76 2542{
390b85e7 2543 return (&errno);
0a753a76
PP
2544}
2545
dcb2879a
GS
2546DllExport char ***
2547win32_environ(void)
2548{
390b85e7 2549 return (&(_environ));
dcb2879a
GS
2550}
2551
68dc0745
PP
2552/* the rest are the remapped stdio routines */
2553DllExport FILE *
2554win32_stderr(void)
0a753a76 2555{
390b85e7 2556 return (stderr);
0a753a76
PP
2557}
2558
68dc0745
PP
2559DllExport FILE *
2560win32_stdin(void)
0a753a76 2561{
390b85e7 2562 return (stdin);
0a753a76
PP
2563}
2564
68dc0745 2565DllExport FILE *
0934c9d9 2566win32_stdout(void)
0a753a76 2567{
390b85e7 2568 return (stdout);
0a753a76
PP
2569}
2570
68dc0745
PP
2571DllExport int
2572win32_ferror(FILE *fp)
0a753a76 2573{
390b85e7 2574 return (ferror(fp));
0a753a76
PP
2575}
2576
2577
68dc0745
PP
2578DllExport int
2579win32_feof(FILE *fp)
0a753a76 2580{
390b85e7 2581 return (feof(fp));
0a753a76
PP
2582}
2583
68dc0745 2584/*
3fadfdf1 2585 * Since the errors returned by the socket error function
68dc0745
PP
2586 * WSAGetLastError() are not known by the library routine strerror
2587 * we have to roll our own.
2588 */
0a753a76 2589
68dc0745 2590DllExport char *
3fadfdf1 2591win32_strerror(int e)
0a753a76 2592{
6f24f39d 2593#if !defined __BORLANDC__ && !defined __MINGW32__ /* compiler intolerance */
68dc0745 2594 extern int sys_nerr;
3e3baf6d 2595#endif
68dc0745 2596 DWORD source = 0;
0a753a76 2597
9404a519 2598 if (e < 0 || e > sys_nerr) {
acfe0abc 2599 dTHX;
9404a519 2600 if (e < 0)
68dc0745 2601 e = GetLastError();
0a753a76 2602
9404a519 2603 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
3352bfcb 2604 w32_strerror_buffer,
3fadfdf1 2605 sizeof(w32_strerror_buffer), NULL) == 0)
3352bfcb 2606 strcpy(w32_strerror_buffer, "Unknown Error");
0a753a76 2607
3352bfcb 2608 return w32_strerror_buffer;
68dc0745 2609 }
390b85e7 2610 return strerror(e);
0a753a76
PP
2611}
2612
22fae026 2613DllExport void
c5be433b 2614win32_str_os_error(void *sv, DWORD dwErr)
22fae026
TM
2615{
2616 DWORD dwLen;
2617 char *sMsg;
2618 dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
2619 |FORMAT_MESSAGE_IGNORE_INSERTS
2620 |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
2621 dwErr, 0, (char *)&sMsg, 1, NULL);
2ce77adf 2622 /* strip trailing whitespace and period */
22fae026 2623 if (0 < dwLen) {
2ce77adf
GS
2624 do {
2625 --dwLen; /* dwLen doesn't include trailing null */
2626 } while (0 < dwLen && isSPACE(sMsg[dwLen]));
22fae026
TM
2627 if ('.' != sMsg[dwLen])
2628 dwLen++;
2ce77adf 2629 sMsg[dwLen] = '\0';
22fae026
TM
2630 }
2631 if (0 == dwLen) {
c69f6586 2632 sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
db7c17d7
GS
2633 if (sMsg)
2634 dwLen = sprintf(sMsg,
2635 "Unknown error #0x%lX (lookup 0x%lX)",
2636 dwErr, GetLastError());
2637 }
2638 if (sMsg) {
acfe0abc 2639 dTHX;
db7c17d7
GS
2640 sv_setpvn((SV*)sv, sMsg, dwLen);
2641 LocalFree(sMsg);
22fae026 2642 }
22fae026
TM
2643}
2644
68dc0745
PP
2645DllExport int
2646win32_fprintf(FILE *fp, const char *format, ...)
0a753a76 2647{
68dc0745
PP
2648 va_list marker;
2649 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 2650
390b85e7 2651 return (vfprintf(fp, format, marker));
0a753a76
PP
2652}
2653
68dc0745
PP
2654DllExport int
2655win32_printf(const char *format, ...)
0a753a76 2656{
68dc0745
PP
2657 va_list marker;
2658 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 2659
390b85e7 2660 return (vprintf(format, marker));
0a753a76
PP
2661}
2662
68dc0745
PP
2663DllExport int
2664win32_vfprintf(FILE *fp, const char *format, va_list args)
0a753a76 2665{
390b85e7 2666 return (vfprintf(fp, format, args));
0a753a76
PP
2667}
2668
96e4d5b1
PP
2669DllExport int
2670win32_vprintf(const char *format, va_list args)
2671{
390b85e7 2672 return (vprintf(format, args));
96e4d5b1
PP
2673}
2674
68dc0745
PP
2675DllExport size_t
2676win32_fread(void *buf, size_t size, size_t count, FILE *fp)
0a753a76 2677{
390b85e7 2678 return fread(buf, size, count, fp);
0a753a76
PP
2679}
2680
68dc0745
PP
2681DllExport size_t
2682win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
0a753a76 2683{
390b85e7 2684 return fwrite(buf, size, count, fp);
0a753a76
PP
2685}
2686
7fac1903
GS
2687#define MODE_SIZE 10
2688
68dc0745
PP
2689DllExport FILE *
2690win32_fopen(const char *filename, const char *mode)
0a753a76 2691{
acfe0abc 2692 dTHX;
1c5905c2 2693 FILE *f;
3fadfdf1 2694
c5be433b
GS
2695 if (!*filename)
2696 return NULL;
2697
68dc0745 2698 if (stricmp(filename, "/dev/null")==0)
7fac1903
GS
2699 filename = "NUL";
2700
8c56068e 2701 f = fopen(PerlDir_mapA(filename), mode);
1c5905c2
GS
2702 /* avoid buffering headaches for child processes */
2703 if (f && *mode == 'a')
2704 win32_fseek(f, 0, SEEK_END);
2705 return f;
0a753a76
PP
2706}
2707
f3986ebb
GS
2708#ifndef USE_SOCKETS_AS_HANDLES
2709#undef fdopen
2710#define fdopen my_fdopen
2711#endif
2712
68dc0745 2713DllExport FILE *
7fac1903 2714win32_fdopen(int handle, const char *mode)
0a753a76 2715{
acfe0abc 2716 dTHX;
1c5905c2 2717 FILE *f;
8c56068e 2718 f = fdopen(handle, (char *) mode);
1c5905c2
GS
2719 /* avoid buffering headaches for child processes */
2720 if (f && *mode == 'a')
2721 win32_fseek(f, 0, SEEK_END);
2722 return f;
0a753a76
PP
2723}
2724
68dc0745 2725DllExport FILE *
7fac1903 2726win32_freopen(const char *path, const char *mode, FILE *stream)
0a753a76 2727{
acfe0abc 2728 dTHX;
68dc0745 2729 if (stricmp(path, "/dev/null")==0)
7fac1903
GS
2730 path = "NUL";
2731
7766f137 2732 return freopen(PerlDir_mapA(path), mode, stream);
0a753a76
PP
2733}
2734
68dc0745
PP
2735DllExport int
2736win32_fclose(FILE *pf)
0a753a76 2737{
f3986ebb 2738 return my_fclose(pf); /* defined in win32sck.c */
0a753a76
PP
2739}
2740
68dc0745
PP
2741DllExport int
2742win32_fputs(const char *s,FILE *pf)
0a753a76 2743{
390b85e7 2744 return fputs(s, pf);
0a753a76
PP
2745}
2746
68dc0745
PP
2747DllExport int
2748win32_fputc(int c,FILE *pf)
0a753a76 2749{
390b85e7 2750 return fputc(c,pf);
0a753a76
PP
2751}
2752
68dc0745
PP
2753DllExport int
2754win32_ungetc(int c,FILE *pf)
0a753a76 2755{
390b85e7 2756 return ungetc(c,pf);
0a753a76
PP
2757}
2758
68dc0745
PP
2759DllExport int
2760win32_getc(FILE *pf)
0a753a76 2761{
390b85e7 2762 return getc(pf);
0a753a76
PP
2763}
2764
68dc0745
PP
2765DllExport int
2766win32_fileno(FILE *pf)
0a753a76 2767{
390b85e7 2768 return fileno(pf);
0a753a76
PP
2769}
2770
68dc0745
PP
2771DllExport void
2772win32_clearerr(FILE *pf)
0a753a76 2773{
390b85e7 2774 clearerr(pf);
68dc0745 2775 return;
0a753a76
PP
2776}
2777
68dc0745
PP
2778DllExport int
2779win32_fflush(FILE *pf)
0a753a76 2780{
390b85e7 2781 return fflush(pf);
0a753a76
PP
2782}
2783
c623ac67 2784DllExport Off_t
68dc0745 2785win32_ftell(FILE *pf)
0a753a76 2786{
c623ac67 2787#if defined(WIN64) || defined(USE_LARGE_FILES)
56460430 2788#if defined(__BORLANDC__) /* buk */
a810272a
NS
2789 return win32_tell( fileno( pf ) );
2790#else
c623ac67
GS
2791 fpos_t pos;
2792 if (fgetpos(pf, &pos))
2793 return -1;
2794 return (Off_t)pos;
a810272a 2795#endif
c623ac67 2796#else
390b85e7 2797 return ftell(pf);
c623ac67 2798#endif
0a753a76
PP
2799}
2800
68dc0745 2801DllExport int
c623ac67 2802win32_fseek(FILE *pf, Off_t offset,int origin)
0a753a76 2803{
c623ac67 2804#if defined(WIN64) || defined(USE_LARGE_FILES)
a810272a
NS
2805#if defined(__BORLANDC__) /* buk */
2806 return win32_lseek(
2807 fileno(pf),
2808 offset,
2809 origin
2810 );
2811#else
c623ac67
GS
2812 fpos_t pos;
2813 switch (origin) {
2814 case SEEK_CUR:
2815 if (fgetpos(pf, &pos))
2816 return -1;
2817 offset += pos;
2818 break;
2819 case SEEK_END:
2820 fseek(pf, 0, SEEK_END);
2821 pos = _telli64(fileno(pf));
2822 offset += pos;
2823 break;
2824 case SEEK_SET:
2825 break;
2826 default:
2827 errno = EINVAL;
2828 return -1;
2829 }
2830 return fsetpos(pf, &offset);
a810272a 2831#endif
c623ac67 2832#else
8859a7a0 2833 return fseek(pf, (long)offset, origin);
c623ac67 2834#endif
0a753a76
PP
2835}
2836
68dc0745
PP
2837DllExport int
2838win32_fgetpos(FILE *pf,fpos_t *p)
0a753a76 2839{
a810272a
NS
2840#if defined(__BORLANDC__) && defined(USE_LARGE_FILES) /* buk */
2841 if( win32_tell(fileno(pf)) == -1L ) {
2842 errno = EBADF;
2843 return -1;
2844 }
2845 return 0;
2846#else
390b85e7 2847 return fgetpos(pf, p);
a810272a 2848#endif
0a753a76
PP
2849}
2850
68dc0745
PP
2851DllExport int
2852win32_fsetpos(FILE *pf,const fpos_t *p)
0a753a76 2853{
a810272a
NS
2854#if defined(__BORLANDC__) && defined(USE_LARGE_FILES) /* buk */
2855 return win32_lseek(fileno(pf), *p, SEEK_CUR);
2856#else
390b85e7 2857 return fsetpos(pf, p);
a810272a 2858#endif
0a753a76
PP
2859}
2860
68dc0745
PP
2861DllExport void
2862win32_rewind(FILE *pf)
0a753a76 2863{
390b85e7 2864 rewind(pf);
68dc0745 2865 return;
0a753a76
PP
2866}
2867
2941a2e1
JH
2868DllExport int
2869win32_tmpfd(void)
0a753a76 2870{
b3122bc4
JH
2871 dTHX;
2872 char prefix[MAX_PATH+1];
2873 char filename[MAX_PATH+1];
2874 DWORD len = GetTempPath(MAX_PATH, prefix);
2875 if (len && len < MAX_PATH) {
2876 if (GetTempFileName(prefix, "plx", 0, filename)) {
2877 HANDLE fh = CreateFile(filename,
2878 DELETE | GENERIC_READ | GENERIC_WRITE,
2879 0,
2880 NULL,
2881 CREATE_ALWAYS,
2882 FILE_ATTRIBUTE_NORMAL
2883 | FILE_FLAG_DELETE_ON_CLOSE,
2884 NULL);
2885 if (fh != INVALID_HANDLE_VALUE) {
c623ac67 2886 int fd = win32_open_osfhandle((intptr_t)fh, 0);
b3122bc4 2887 if (fd >= 0) {
a051bdb4
VK
2888#if defined(__BORLANDC__)
2889 setmode(fd,O_BINARY);
2890#endif
b3122bc4
JH
2891 DEBUG_p(PerlIO_printf(Perl_debug_log,
2892 "Created tmpfile=%s\n",filename));
2941a2e1 2893 return fd;
b3122bc4
JH
2894 }
2895 }
2896 }
2897 }
2941a2e1
JH
2898 return -1;
2899}
2900
2901DllExport FILE*
2902win32_tmpfile(void)
2903{
2904 int fd = win32_tmpfd();
2905 if (fd >= 0)
2906 return win32_fdopen(fd, "w+b");
b3122bc4 2907 return NULL;
0a753a76
PP
2908}
2909
68dc0745
PP
2910DllExport void
2911win32_abort(void)
0a753a76 2912{
390b85e7 2913 abort();
68dc0745 2914 return;
0a753a76
PP
2915}
2916
68dc0745 2917DllExport int
c623ac67 2918win32_fstat(int fd, Stat_t *sbufptr)
0a753a76 2919{
2a07f407
VK
2920#ifdef __BORLANDC__
2921 /* A file designated by filehandle is not shown as accessible
2922 * for write operations, probably because it is opened for reading.
2923 * --Vadim Konovalov
3fadfdf1 2924 */
2a07f407 2925 BY_HANDLE_FILE_INFORMATION bhfi;
bda6ed21
PM
2926#if defined(WIN64) || defined(USE_LARGE_FILES)
2927 /* Borland 5.5.1 has a 64-bit stat, but only a 32-bit fstat */
2928 struct stat tmp;
2929 int rc = fstat(fd,&tmp);
2930
2931 sbufptr->st_dev = tmp.st_dev;
2932 sbufptr->st_ino = tmp.st_ino;
2933 sbufptr->st_mode = tmp.st_mode;
2934 sbufptr->st_nlink = tmp.st_nlink;
2935 sbufptr->st_uid = tmp.st_uid;
2936 sbufptr->st_gid = tmp.st_gid;
2937 sbufptr->st_rdev = tmp.st_rdev;
2938 sbufptr->st_size = tmp.st_size;
2939 sbufptr->st_atime = tmp.st_atime;
2940 sbufptr->st_mtime = tmp.st_mtime;
2941 sbufptr->st_ctime = tmp.st_ctime;
2942#else
2943 int rc = fstat(fd,sbufptr);
2944#endif
2945
2a07f407 2946 if (GetFileInformationByHandle((HANDLE)_get_osfhandle(fd), &bhfi)) {
bda6ed21 2947#if defined(WIN64) || defined(USE_LARGE_FILES)
0934c9d9 2948 sbufptr->st_size = ((__int64)bhfi.nFileSizeHigh << 32) | bhfi.nFileSizeLow ;
bda6ed21 2949#endif
2a07f407
VK
2950 sbufptr->st_mode &= 0xFE00;
2951 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
2952 sbufptr->st_mode |= (S_IREAD + (S_IREAD >> 3) + (S_IREAD >> 6));
2953 else
2954 sbufptr->st_mode |= ((S_IREAD|S_IWRITE) + ((S_IREAD|S_IWRITE) >> 3)
2955 + ((S_IREAD|S_IWRITE) >> 6));
2956 }
2957 return rc;
2958#else
ed59ec62 2959 return my_fstat(fd,sbufptr);
2a07f407 2960#endif
0a753a76
PP
2961}
2962
68dc0745
PP
2963DllExport int
2964win32_pipe(int *pfd, unsigned int size, int mode)
0a753a76 2965{
390b85e7 2966 return _pipe(pfd, size, mode);
0a753a76
PP
2967}
2968
8c0134a8
NIS
2969DllExport PerlIO*
2970win32_popenlist(const char *mode, IV narg, SV **args)
2971{
2972 dTHX;
2973 Perl_croak(aTHX_ "List form of pipe open not implemented");
2974 return NULL;
2975}
2976
50892819
GS
2977/*
2978 * a popen() clone that respects PERL5SHELL
00b02797
JH
2979 *
2980 * changed to return PerlIO* rather than FILE * by BKS, 11-11-2000
50892819
GS
2981 */
2982
00b02797 2983DllExport PerlIO*
68dc0745 2984win32_popen(const char *command, const char *mode)
0a753a76 2985{
4b556e6c 2986#ifdef USE_RTL_POPEN
390b85e7 2987 return _popen(command, mode);
50892819 2988#else
2cbbe5a1 2989 dTHX;
50892819
GS
2990 int p[2];
2991 int parent, child;
2992 int stdfd, oldfd;
2993 int ourmode;
2994 int childpid;
1095be37
GS
2995 DWORD nhandle;
2996 HANDLE old_h;
2997 int lock_held = 0;
50892819
GS
2998
2999 /* establish which ends read and write */
3000 if (strchr(mode,'w')) {
3001 stdfd = 0; /* stdin */
3002 parent = 1;
3003 child = 0;
1095be37 3004 nhandle = STD_INPUT_HANDLE;
50892819
GS
3005 }
3006 else if (strchr(mode,'r')) {
3007 stdfd = 1; /* stdout */
3008 parent = 0;
3009 child = 1;
1095be37 3010 nhandle = STD_OUTPUT_HANDLE;
50892819
GS
3011 }
3012 else
3013 return NULL;
3014
3015 /* set the correct mode */
3016 if (strchr(mode,'b'))
3017 ourmode = O_BINARY;
3018 else if (strchr(mode,'t'))
3019 ourmode = O_TEXT;
3020 else
3021 ourmode = _fmode & (O_TEXT | O_BINARY);
3022
3023 /* the child doesn't inherit handles */
3024 ourmode |= O_NOINHERIT;
3025
1095be37 3026 if (win32_pipe(p, 512, ourmode) == -1)
50892819
GS
3027 return NULL;
3028
498d7dc4
GS
3029 /* save the old std handle (this needs to happen before the
3030 * dup2(), since that might call SetStdHandle() too) */
3031 OP_REFCNT_LOCK;
3032 lock_held = 1;
3033 old_h = GetStdHandle(nhandle);
3034
564914cd
AS
3035 /* save current stdfd */
3036 if ((oldfd = win32_dup(stdfd)) == -1)
3037 goto cleanup;
3038
50892819
GS
3039 /* make stdfd go to child end of pipe (implicitly closes stdfd) */
3040 /* stdfd will be inherited by the child */
3041 if (win32_dup2(p[child], stdfd) == -1)
3042 goto cleanup;
3043
3044 /* close the child end in parent */
3045 win32_close(p[child]);
3046
498d7dc4 3047 /* set the new std handle (in case dup2() above didn't) */
1095be37
GS
3048 SetStdHandle(nhandle, (HANDLE)_get_osfhandle(stdfd));
3049
50892819 3050 /* start the child */
4f63d024 3051 {
acfe0abc 3052 dTHX;
c5be433b 3053 if ((childpid = do_spawn_nowait((char*)command)) == -1)
4f63d024 3054 goto cleanup;
50892819 3055
498d7dc4
GS
3056 /* revert stdfd to whatever it was before */
3057 if (win32_dup2(oldfd, stdfd) == -1)
3058 goto cleanup;
3059
564914cd
AS
3060 /* close saved handle */
3061 win32_close(oldfd);
3062
498d7dc4
GS
3063 /* restore the old std handle (this needs to happen after the
3064 * dup2(), since that might call SetStdHandle() too */
1095be37
GS
3065 if (lock_held) {
3066 SetStdHandle(nhandle, old_h);
3067 OP_REFCNT_UNLOCK;
3068 lock_held = 0;
3069 }
3070
4755096e 3071 LOCK_FDPID_MUTEX;
4f63d024 3072 sv_setiv(*av_fetch(w32_fdpid, p[parent], TRUE), childpid);
4755096e 3073 UNLOCK_FDPID_MUTEX;
d91d68c1
R
3074
3075 /* set process id so that it can be returned by perl's open() */
3076 PL_forkprocess = childpid;
4f63d024 3077 }
50892819
GS
3078
3079 /* we have an fd, return a file stream */
00b02797 3080 return (PerlIO_fdopen(p[parent], (char *)mode));
50892819
GS
3081
3082cleanup:
3083 /* we don't need to check for errors here */
3084 win32_close(p[0]);
3085 win32_close(p[1]);
564914cd
AS
3086 if (oldfd != -1) {
3087 win32_dup2(oldfd, stdfd);
3088 win32_close(oldfd);
3089 }
1095be37
GS
3090 if (lock_held) {
3091 SetStdHandle(nhandle, old_h);
3092 OP_REFCNT_UNLOCK;
3093 lock_held = 0;
3094 }
50892819
GS
3095 return (NULL);
3096
4b556e6c 3097#endif /* USE_RTL_POPEN */
0a753a76
PP
3098}
3099
50892819
GS
3100/*
3101 * pclose() clone
3102 */
3103
68dc0745 3104DllExport int
00b02797 3105win32_pclose(PerlIO *pf)
0a753a76 3106{
4b556e6c 3107#ifdef USE_RTL_POPEN
390b85e7 3108 return _pclose(pf);
50892819 3109#else
acfe0abc 3110 dTHX;
e17cb2a9
JD
3111 int childpid, status;
3112 SV *sv;
3113
4755096e 3114 LOCK_FDPID_MUTEX;
00b02797 3115 sv = *av_fetch(w32_fdpid, PerlIO_fileno(pf), TRUE);
4755096e 3116
e17cb2a9
JD
3117 if (SvIOK(sv))
3118 childpid = SvIVX(sv);
3119 else
3120 childpid = 0;
50892819
GS
3121
3122 if (!childpid) {
db2fab48 3123 UNLOCK_FDPID_MUTEX;
50892819
GS
3124 errno = EBADF;
3125 return -1;
3126 }
3127
00b02797
JH
3128#ifdef USE_PERLIO
3129 PerlIO_close(pf);
3130#else
3131 fclose(pf);
3132#endif
e17cb2a9 3133 SvIVX(sv) = 0;
4755096e 3134 UNLOCK_FDPID_MUTEX;
e17cb2a9 3135
0aaad0ff
GS
3136 if (win32_waitpid(childpid, &status, 0) == -1)
3137 return -1;
50892819 3138
0aaad0ff 3139 return status;
50892819 3140
4b556e6c 3141#endif /* USE_RTL_POPEN */
0a753a76 3142}
6b980173
JD
3143
3144static BOOL WINAPI
3145Nt4CreateHardLinkW(
3146 LPCWSTR lpFileName,
3147 LPCWSTR lpExistingFileName,
3148 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
3149{
3150 HANDLE handle;
3151 WCHAR wFullName[MAX_PATH+1];
3152 LPVOID lpContext = NULL;
3153 WIN32_STREAM_ID StreamId;
3154 DWORD dwSize = (char*)&StreamId.cStreamName - (char*)&StreamId;
3155 DWORD dwWritten;
3156 DWORD dwLen;
3157 BOOL bSuccess;
3158
3159 BOOL (__stdcall *pfnBackupWrite)(HANDLE, LPBYTE, DWORD, LPDWORD,
3160 BOOL, BOOL, LPVOID*) =
3161 (BOOL (__stdcall *)(HANDLE, LPBYTE, DWORD, LPDWORD,
3162 BOOL, BOOL, LPVOID*))
3163 GetProcAddress(GetModuleHandle("kernel32.dll"), "BackupWrite");
3164 if (pfnBackupWrite == NULL)
3165 return 0;
3166
3167 dwLen = GetFullPathNameW(lpFileName, MAX_PATH, wFullName, NULL);
3168 if (dwLen == 0)
3169 return 0;
3170 dwLen = (dwLen+1)*sizeof(WCHAR);
3171
3172 handle = CreateFileW(lpExistingFileName, FILE_WRITE_ATTRIBUTES,
3173 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3174 NULL, OPEN_EXISTING, 0, NULL);
3175 if (handle == INVALID_HANDLE_VALUE)
3176 return 0;
3177
3178 StreamId.dwStreamId = BACKUP_LINK;
3179 StreamId.dwStreamAttributes = 0;
3180 StreamId.dwStreamNameSize = 0;
6f24f39d
JK
3181#if defined(__BORLANDC__) \
3182 ||(defined(__MINGW32__) && !defined(_ANONYMOUS_UNION))
4ce4f76e
GS
3183 StreamId.Size.u.HighPart = 0;
3184 StreamId.Size.u.LowPart = dwLen;
3185#else
6b980173
JD
3186 StreamId.Size.HighPart = 0;
3187 StreamId.Size.LowPart = dwLen;
4ce4f76e 3188#endif
6b980173
JD
3189
3190 bSuccess = pfnBackupWrite(handle, (LPBYTE)&StreamId, dwSize, &dwWritten,
3191 FALSE, FALSE, &lpContext);
3192 if (bSuccess) {
3193 bSuccess = pfnBackupWrite(handle, (LPBYTE)wFullName, dwLen, &dwWritten,
3194 FALSE, FALSE, &lpContext);
3195 pfnBackupWrite(handle, NULL, 0, &dwWritten, TRUE, FALSE, &lpContext);
3196 }
3197
3198 CloseHandle(handle);
3199 return bSuccess;
3200}
3201
3202DllExport int
3203win32_link(const char *oldname, const char *newname)
3204{
acfe0abc 3205 dTHX;
6b980173 3206 BOOL (__stdcall *pfnCreateHardLinkW)(LPCWSTR,LPCWSTR,LPSECURITY_ATTRIBUTES);
82867ecf
GS
3207 WCHAR wOldName[MAX_PATH+1];
3208 WCHAR wNewName[MAX_PATH+1];
6b980173
JD
3209
3210 if (IsWin95())
1be9d9c6 3211 Perl_croak(aTHX_ PL_no_func, "link");
6b980173
JD
3212
3213 pfnCreateHardLinkW =
3214 (BOOL (__stdcall *)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES))
3215 GetProcAddress(GetModuleHandle("kernel32.dll"), "CreateHardLinkW");
3216 if (pfnCreateHardLinkW == NULL)
3217 pfnCreateHardLinkW = Nt4CreateHardLinkW;
3218
8c56068e
JD
3219 if (MultiByteToWideChar(CP_ACP, 0, oldname, -1, wOldName, MAX_PATH+1) &&
3220 MultiByteToWideChar(CP_ACP, 0, newname, -1, wNewName, MAX_PATH+1) &&
7766f137 3221 (wcscpy(wOldName, PerlDir_mapW(wOldName)),
8c56068e 3222 pfnCreateHardLinkW(PerlDir_mapW(wNewName), wOldName, NULL)))
6b980173
JD
3223 {
3224 return 0;
3225 }
3226 errno = (GetLastError() == ERROR_FILE_NOT_FOUND) ? ENOENT : EINVAL;
3227 return -1;
3228}
0a753a76 3229
68dc0745 3230DllExport int
8d9b2e3c 3231win32_rename(const char *oname, const char *newname)
e24c7c18 3232{
65cb15a1
GS
3233 char szOldName[MAX_PATH+1];
3234 char szNewName[MAX_PATH+1];
7fac1903 3235 BOOL bResult;
acfe0abc 3236 dTHX;
65cb15a1 3237
80252599
GS
3238 /* XXX despite what the documentation says about MoveFileEx(),
3239 * it doesn't work under Windows95!
3240 */
3241 if (IsWinNT()) {
65cb15a1 3242 DWORD dwFlags = MOVEFILE_COPY_ALLOWED;
8c56068e
JD
3243 if (stricmp(newname, oname))
3244 dwFlags |= MOVEFILE_REPLACE_EXISTING;
3245 strcpy(szOldName, PerlDir_mapA(oname));
3246 bResult = MoveFileExA(szOldName,PerlDir_mapA(newname), dwFlags);
7fac1903 3247 if (!bResult) {
80252599
GS
3248 DWORD err = GetLastError();
3249 switch (err) {
3250 case ERROR_BAD_NET_NAME:
3251 case ERROR_BAD_NETPATH:
3252 case ERROR_BAD_PATHNAME:
3253 case ERROR_FILE_NOT_FOUND:
3254 case ERROR_FILENAME_EXCED_RANGE:
3255 case ERROR_INVALID_DRIVE:
3256 case ERROR_NO_MORE_FILES:
3257 case ERROR_PATH_NOT_FOUND:
3258 errno = ENOENT;
3259 break;
3260 default:
3261 errno = EACCES;
3262 break;
3263 }
3264 return -1;
3265 }
3266 return 0;
e24c7c18 3267 }
80252599
GS
3268 else {
3269 int retval = 0;
65cb15a1 3270 char szTmpName[MAX_PATH+1];
80252599 3271 char dname[MAX_PATH+1];
4e205ed6 3272 char *endname = NULL;
80252599
GS
3273 STRLEN tmplen = 0;
3274 DWORD from_attr, to_attr;
3275
65cb15a1
GS
3276 strcpy(szOldName, PerlDir_mapA(oname));
3277 strcpy(szNewName, PerlDir_mapA(newname));
3278
80252599 3279 /* if oname doesn't exist, do nothing */
65cb15a1 3280 from_attr = GetFileAttributes(szOldName);
80252599
GS
3281 if (from_attr == 0xFFFFFFFF) {
3282 errno = ENOENT;
3283 return -1;
3284 }
3285
3286 /* if newname exists, rename it to a temporary name so that we
3287 * don't delete it in case oname happens to be the same file
3288 * (but perhaps accessed via a different path)
3289 */
65cb15a1 3290 to_attr = GetFileAttributes(szNewName);
80252599
GS
3291 if (to_attr != 0xFFFFFFFF) {
3292 /* if newname is a directory, we fail
3293 * XXX could overcome this with yet more convoluted logic */
3294 if (to_attr & FILE_ATTRIBUTE_DIRECTORY) {
3295 errno = EACCES;
3296 return -1;
3297 }
65cb15a1
GS
3298 tmplen = strlen(szNewName);
3299 strcpy(szTmpName,szNewName);
3300 endname = szTmpName+tmplen;
3301 for (; endname > szTmpName ; --endname) {
80252599
GS
3302 if (*endname == '/' || *endname == '\\') {
3303 *endname = '\0';
3304 break;
3305 }
3306 }
65cb15a1
GS
3307 if (endname > szTmpName)
3308 endname = strcpy(dname,szTmpName);
e24c7c18 3309 else
80252599
GS
3310 endname = ".";
3311
3312 /* get a temporary filename in same directory
3313 * XXX is this really the best we can do? */
65cb15a1 3314 if (!GetTempFileName((LPCTSTR)endname, "plr", 0, szTmpName)) {
80252599
GS
3315 errno = ENOENT;
3316 return -1;
3317 }
65cb15a1 3318 DeleteFile(szTmpName);
80252599 3319
65cb15a1 3320 retval = rename(szNewName, szTmpName);
80252599
GS
3321 if (retval != 0) {
3322 errno = EACCES;
3323 return retval;
e24c7c18
GS
3324 }
3325 }
80252599
GS
3326
3327 /* rename oname to newname */
65cb15a1 3328 retval = rename(szOldName, szNewName);
80252599
GS
3329
3330 /* if we created a temporary file before ... */
4e205ed6 3331 if (endname != NULL) {
80252599
GS
3332 /* ...and rename succeeded, delete temporary file/directory */
3333 if (retval == 0)
65cb15a1 3334 DeleteFile(szTmpName);
80252599
GS
3335 /* else restore it to what it was */
3336 else
65cb15a1 3337 (void)rename(szTmpName, szNewName);
80252599
GS
3338 }
3339 return retval;
e24c7c18 3340 }
e24c7c18
GS
3341}
3342
3343DllExport int
68dc0745 3344win32_setmode(int fd, int mode)
0a753a76 3345{
390b85e7 3346 return setmode(fd, mode);
0a753a76
PP
3347}
3348
4a9d6100
GS
3349DllExport int
3350win32_chsize(int fd, Off_t size)
3351{
3352#if defined(WIN64) || defined(USE_LARGE_FILES)
3353 int retval = 0;
3354 Off_t cur, end, extend;
3355
3356 cur = win32_tell(fd);
3357 if (cur < 0)
3358 return -1;
3359 end = win32_lseek(fd, 0, SEEK_END);
3360 if (end < 0)
3361 return -1;
3362 extend = size - end;
3363 if (extend == 0) {
3364 /* do nothing */
3365 }
3366 else if (extend > 0) {
3367 /* must grow the file, padding with nulls */
3368 char b[4096];
3369 int oldmode = win32_setmode(fd, O_BINARY);
3370 size_t count;
3371 memset(b, '\0', sizeof(b));
3372 do {
3373 count = extend >= sizeof(b) ? sizeof(b) : (size_t)extend;
3374 count = win32_write(fd, b, count);
21424390 3375 if ((int)count < 0) {
4a9d6100
GS
3376 retval = -1;
3377 break;
3378 }
3379 } while ((extend -= count) > 0);
3380 win32_setmode(fd, oldmode);
3381 }
3382 else {
3383 /* shrink the file */
3384 win32_lseek(fd, size, SEEK_SET);
3385 if (!SetEndOfFile((HANDLE)_get_osfhandle(fd))) {
3386 errno = EACCES;
3387 retval = -1;
3388 }
3389 }
3390finish:
3391 win32_lseek(fd, cur, SEEK_SET);
3392 return retval;
3393#else
8859a7a0 3394 return chsize(fd, (long)size);
4a9d6100
GS
3395#endif
3396}
3397
c623ac67
GS
3398DllExport Off_t
3399win32_lseek(int fd, Off_t offset, int origin)
96e4d5b1 3400{
c623ac67 3401#if defined(WIN64) || defined(USE_LARGE_FILES)
a810272a
NS
3402#if defined(__BORLANDC__) /* buk */
3403 LARGE_INTEGER pos;
3404 pos.QuadPart = offset;
3405 pos.LowPart = SetFilePointer(
3406 (HANDLE)_get_osfhandle(fd),
3407 pos.LowPart,
3408 &pos.HighPart,
3409 origin
3410 );
3411 if (pos.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
3412 pos.QuadPart = -1;
3413 }
3414
3415 return pos.QuadPart;
3416#else
c623ac67 3417 return _lseeki64(fd, offset, origin);
a810272a 3418#endif
c623ac67 3419#else
8859a7a0 3420 return lseek(fd, (long)offset, origin);
c623ac67 3421#endif
96e4d5b1
PP
3422}
3423
c623ac67 3424DllExport Off_t
96e4d5b1
PP
3425win32_tell(int fd)
3426{
c623ac67 3427#if defined(WIN64) || defined(USE_LARGE_FILES)
05e23382 3428#if defined(__BORLANDC__) /* buk */
a810272a
NS
3429 LARGE_INTEGER pos;
3430 pos.QuadPart = 0;
3431 pos.LowPart = SetFilePointer(
3432 (HANDLE)_get_osfhandle(fd),
3433 pos.LowPart,
3434 &pos.HighPart,
3435 FILE_CURRENT
3436 );
3437 if (pos.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
3438 pos.QuadPart = -1;
3439 }
3440
3441 return pos.QuadPart;
3442 /* return tell(fd); */
3443#else
c623ac67 3444 return _telli64(fd);
a810272a 3445#endif
c623ac67 3446#else
390b85e7 3447 return tell(fd);
c623ac67 3448#endif
96e4d5b1
PP
3449}
3450
68dc0745
PP
3451DllExport int
3452win32_open(const char *path, int flag, ...)
0a753a76 3453{
acfe0abc 3454 dTHX;
68dc0745
PP
3455 va_list ap;
3456 int pmode;
0a753a76
PP
3457
3458 va_start(ap, flag);
3459 pmode = va_arg(ap, int);
3460 va_end(ap);
3461
68dc0745 3462 if (stricmp(path, "/dev/null")==0)
7fac1903
GS
3463 path = "NUL";
3464
7766f137 3465 return open(PerlDir_mapA(path), flag, pmode);
0a753a76
PP
3466}
3467
00b02797
JH
3468/* close() that understands socket */
3469extern int my_close(int); /* in win32sck.c */
3470
68dc0745
PP
3471DllExport int
3472win32_close(int fd)
0a753a76 3473{
00b02797 3474 return my_close(fd);
0a753a76
PP
3475}
3476
68dc0745 3477DllExport int
96e4d5b1
PP
3478win32_eof(int fd)
3479{
390b85e7 3480 return eof(fd);
96e4d5b1
PP
3481}
3482
3483DllExport int
68dc0745 3484win32_dup(int fd)
0a753a76 3485{
390b85e7 3486 return dup(fd);
0a753a76
PP
3487}
3488
68dc0745
PP
3489DllExport int
3490win32_dup2(int fd1,int fd2)
0a753a76 3491{
390b85e7 3492 return dup2(fd1,fd2);
0a753a76
PP
3493}
3494
f7aeb604
GS
3495#ifdef PERL_MSVCRT_READFIX
3496
3497#define LF 10 /* line feed */
3498#define CR 13 /* carriage return */
3499#define CTRLZ 26 /* ctrl-z means eof for text */
3500#define FOPEN 0x01 /* file handle open */
3501#define FEOFLAG 0x02 /* end of file has been encountered */
3502#define FCRLF 0x04 /* CR-LF across read buffer (in text mode) */
3503#define FPIPE 0x08 /* file handle refers to a pipe */
3504#define FAPPEND 0x20 /* file handle opened O_APPEND */
3505#define FDEV 0x40 /* file handle refers to device */
3506#define FTEXT 0x80 /* file handle is in text mode */
3507#define MAX_DESCRIPTOR_COUNT (64*32) /* this is the maximun that MSVCRT can handle */
3508
b181b6fb
GS
3509int __cdecl
3510_fixed_read(int fh, void *buf, unsigned cnt)
f7aeb604
GS
3511{
3512 int bytes_read; /* number of bytes read */
3513 char *buffer; /* buffer to read to */
3514 int os_read; /* bytes read on OS call */
3515 char *p, *q; /* pointers into buffer */
3516 char peekchr; /* peek-ahead character */
3517 ULONG filepos; /* file position after seek */
3518 ULONG dosretval; /* o.s. return value */
3519
3520 /* validate handle */
3521 if (((unsigned)fh >= (unsigned)MAX_DESCRIPTOR_COUNT) ||
3522 !(_osfile(fh) & FOPEN))
3523 {
3524 /* out of range -- return error */
3525 errno = EBADF;
3526 _doserrno = 0; /* not o.s. error */
3527 return -1;
3528 }
3529
635bbe87
GS
3530 /*
3531 * If lockinitflag is FALSE, assume fd is device
3532 * lockinitflag is set to TRUE by open.
3533 */
3534 if (_pioinfo(fh)->lockinitflag)
3535 EnterCriticalSection(&(_pioinfo(fh)->lock)); /* lock file */
f7aeb604
GS
3536
3537 bytes_read = 0; /* nothing read yet */
3538 buffer = (char*)buf;
3539
3540 if (cnt == 0 || (_osfile(fh) & FEOFLAG)) {
3541 /* nothing to read or at EOF, so return 0 read */
3542 goto functionexit;
3543 }
3544
3545 if ((_osfile(fh) & (FPIPE|FDEV)) && _pipech(fh) != LF) {
3546 /* a pipe/device and pipe lookahead non-empty: read the lookahead
3547 * char */
3548 *buffer++ = _pipech(fh);
3549 ++bytes_read;
3550 --cnt;
3551 _pipech(fh) = LF; /* mark as empty */
3552 }
3553
3554 /* read the data */
3555
3556 if (!ReadFile((HANDLE)_osfhnd(fh), buffer, cnt, (LPDWORD)&os_read, NULL))
3557 {
3558 /* ReadFile has reported an error. recognize two special cases.
3559 *
3560 * 1. map ERROR_ACCESS_DENIED to EBADF
3561 *
3562 * 2. just return 0 if ERROR_BROKEN_PIPE has occurred. it
3563 * means the handle is a read-handle on a pipe for which
3564 * all write-handles have been closed and all data has been
3565 * read. */
3566
3567 if ((dosretval = GetLastError()) == ERROR_ACCESS_DENIED) {
3568 /* wrong read/write mode should return EBADF, not EACCES */
3569 errno = EBADF;
3570 _doserrno = dosretval;
3571 bytes_read = -1;
3572 goto functionexit;
3573 }
3574 else if (dosretval == ERROR_BROKEN_PIPE) {
3575 bytes_read = 0;
3576 goto functionexit;
3577 }
3578 else {
3579 bytes_read = -1;
3580 goto functionexit;
3581 }
3582 }
3583
3584 bytes_read += os_read; /* update bytes read */
3585
3586 if (_osfile(fh) & FTEXT) {
3587 /* now must translate CR-LFs to LFs in the buffer */
3588
3589 /* set CRLF flag to indicate LF at beginning of buffer */
3590 /* if ((os_read != 0) && (*(char *)buf == LF)) */
3591 /* _osfile(fh) |= FCRLF; */
3592 /* else */
3593 /* _osfile(fh) &= ~FCRLF; */
3594
3595 _osfile(fh) &= ~FCRLF;
3596
3597 /* convert chars in the buffer: p is src, q is dest */
3598 p = q = (char*)buf;
3599 while (p < (char *)buf + bytes_read) {
3600 if (*p == CTRLZ) {
3601 /* if fh is not a device, set ctrl-z flag */
3602 if (!(_osfile(fh) & FDEV))
3603 _osfile(fh) |= FEOFLAG;
3604 break; /* stop translating */
3605 }
3606 else if (*p != CR)
3607 *q++ = *p++;
3608 else {
3609 /* *p is CR, so must check next char for LF */
3610 if (p < (char *)buf + bytes_read - 1) {
3611 if (*(p+1) == LF) {
3612 p += 2;
3613 *q++ = LF; /* convert CR-LF to LF */
3614 }
3615 else
3616 *q++ = *p++; /* store char normally */
3617 }
3618 else {
3619 /* This is the hard part. We found a CR at end of
3620 buffer. We must peek ahead to see if next char
3621 is an LF. */
3622 ++p;
3623
3624 dosretval = 0;
3625 if (!ReadFile((HANDLE)_osfhnd(fh), &peekchr, 1,
3626 (LPDWORD)&os_read, NULL))
3627 dosretval = GetLastError();
3628
3629 if (dosretval != 0 || os_read == 0) {
3630 /* couldn't read ahead, store CR */
3631 *q++ = CR;
3632 }
3633 else {
3634 /* peekchr now has the extra character -- we now
3635 have several possibilities:
3636 1. disk file and char is not LF; just seek back
3637 and copy CR
3638 2. disk file and char is LF; store LF, don't seek back
3639 3. pipe/device and char is LF; store LF.
3640 4. pipe/device and char isn't LF, store CR and
3641 put char in pipe lookahead buffer. */
3642 if (_osfile(fh) & (FDEV|FPIPE)) {
3643 /* non