Remove the last regnode<==>char*
[p5sagit/p5-mst-13.2.git] / win32 / win32.c
CommitLineData
68dc0745 1/* WIN32.C
2 *
3 * (c) 1995 Microsoft Corporation. All rights reserved.
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 */
0a753a76 10
11#define WIN32_LEAN_AND_MEAN
12#define WIN32IO_IS_STDIO
13#include <tchar.h>
a835ef8a 14#ifdef __GNUC__
15#define Win32_Winsock
16#endif
0a753a76 17#include <windows.h>
18
e56670dd 19#ifndef __MINGW32__
9404a519 20#include <lmcons.h>
21#include <lmerr.h>
22/* ugliness to work around a buggy struct definition in lmwksta.h */
23#undef LPTSTR
24#define LPTSTR LPWSTR
25#include <lmwksta.h>
26#undef LPTSTR
27#define LPTSTR LPSTR
e56670dd 28#include <lmapibuf.h>
29#endif /* __MINGW32__ */
9404a519 30
68dc0745 31/* #include "config.h" */
0a753a76 32
33#define PERLIO_NOT_STDIO 0
34#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
35#define PerlIO FILE
36#endif
37
7a9ec5a3 38#include <sys/stat.h>
0a753a76 39#include "EXTERN.h"
40#include "perl.h"
c69f6586 41
42#define NO_XSLOCKS
c5be433b 43#define PERL_NO_GET_CONTEXT
ad2e33dc 44#include "XSUB.h"
c69f6586 45
46#include "Win32iop.h"
0a753a76 47#include <fcntl.h>
5b0d9cbe 48#ifndef __GNUC__
49/* assert.h conflicts with #define of assert in perl.h */
0a753a76 50#include <assert.h>
5b0d9cbe 51#endif
0a753a76 52#include <string.h>
53#include <stdarg.h>
ad2e33dc 54#include <float.h>
ad0751ec 55#include <time.h>
3730b96e 56#if defined(_MSC_VER) || defined(__MINGW32__)
ad0751ec 57#include <sys/utime.h>
58#else
59#include <utime.h>
60#endif
0a753a76 61
5b0d9cbe 62#ifdef __GNUC__
63/* Mingw32 defaults to globing command line
64 * So we turn it off like this:
65 */
66int _CRT_glob = 0;
67#endif
68
0b94c7bb 69#ifdef __BORLANDC__
70# define _stat stat
71# define _utimbuf utimbuf
72#endif
73
6890e559 74#define EXECF_EXEC 1
75#define EXECF_SPAWN 2
76#define EXECF_SPAWN_NOWAIT 3
77
32e30700 78#if defined(PERL_IMPLICIT_SYS)
79# undef win32_get_privlib
80# define win32_get_privlib g_win32_get_privlib
81# undef win32_get_sitelib
82# define win32_get_sitelib g_win32_get_sitelib
83# undef do_spawn
84# define do_spawn g_do_spawn
85# undef getlogin
86# define getlogin g_getlogin
87#endif
88
c69f6586 89#if defined(PERL_OBJECT)
32e30700 90# undef do_aspawn
91# define do_aspawn g_do_aspawn
92# undef Perl_do_exec
93# define Perl_do_exec g_do_exec
c69f6586 94#endif
95
ce1da67e 96static void get_shell(void);
97static long tokenize(char *str, char **dest, char ***destv);
c5be433b 98 int do_spawn2(char *cmd, int exectype);
e200fe59 99static BOOL has_shell_metachars(char *ptr);
2d7a9237 100static long filetime_to_clock(PFILETIME ft);
ad0751ec 101static BOOL filetime_from_time(PFILETIME ft, time_t t);
c5be433b 102static char * get_emd_part(SV **leading, char *trailing, ...);
0aaad0ff 103static void remove_dead_process(long deceased);
104static long find_pid(int pid);
105static char * qualified_path(const char *cmd);
c69f6586 106
2d7a9237 107HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
8ac9c18d 108char w32_module_name[MAX_PATH+1];
4b556e6c 109static DWORD w32_platform = (DWORD)-1;
50892819 110
26618a56 111#ifdef USE_THREADS
112# ifdef USE_DECLSPEC_THREAD
113__declspec(thread) char strerror_buffer[512];
e34ffe5a 114__declspec(thread) char getlogin_buffer[128];
4b556e6c 115__declspec(thread) char w32_perllib_root[MAX_PATH+1];
26618a56 116# ifdef HAVE_DES_FCRYPT
117__declspec(thread) char crypt_buffer[30];
118# endif
119# else
120# define strerror_buffer (thr->i.Wstrerror_buffer)
e34ffe5a 121# define getlogin_buffer (thr->i.Wgetlogin_buffer)
4b556e6c 122# define w32_perllib_root (thr->i.Ww32_perllib_root)
26618a56 123# define crypt_buffer (thr->i.Wcrypt_buffer)
124# endif
125#else
4b556e6c 126static char strerror_buffer[512];
127static char getlogin_buffer[128];
128static char w32_perllib_root[MAX_PATH+1];
26618a56 129# ifdef HAVE_DES_FCRYPT
4b556e6c 130static char crypt_buffer[30];
26618a56 131# endif
132#endif
133
3fe9a6f1 134int
ba106d47 135IsWin95(void)
136{
0cb96387 137 return (win32_os_id() == VER_PLATFORM_WIN32_WINDOWS);
3fe9a6f1 138}
139
140int
ba106d47 141IsWinNT(void)
142{
0cb96387 143 return (win32_os_id() == VER_PLATFORM_WIN32_NT);
3fe9a6f1 144}
0a753a76 145
c5be433b 146/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
51371543 147static char*
c5be433b 148get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
349ad1fe 149{
150 /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
00dc2f4f 151 HKEY handle;
152 DWORD type;
153 const char *subkey = "Software\\Perl";
349ad1fe 154 char *str = Nullch;
00dc2f4f 155 long retval;
156
157 retval = RegOpenKeyEx(hkey, subkey, 0, KEY_READ, &handle);
349ad1fe 158 if (retval == ERROR_SUCCESS) {
51371543 159 DWORD datalen;
160 retval = RegQueryValueEx(handle, valuename, 0, &type, NULL, &datalen);
ba3eb2af 161 if (retval == ERROR_SUCCESS && type == REG_SZ) {
c5be433b 162 dTHXo;
163 if (!*svp)
164 *svp = sv_2mortal(newSVpvn("",0));
165 SvGROW(*svp, datalen);
51371543 166 retval = RegQueryValueEx(handle, valuename, 0, NULL,
c5be433b 167 (PBYTE)SvPVX(*svp), &datalen);
51371543 168 if (retval == ERROR_SUCCESS) {
c5be433b 169 str = SvPVX(*svp);
170 SvCUR_set(*svp,datalen-1);
51371543 171 }
00dc2f4f 172 }
173 RegCloseKey(handle);
174 }
349ad1fe 175 return str;
00dc2f4f 176}
177
c5be433b 178/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
51371543 179static char*
c5be433b 180get_regstr(const char *valuename, SV **svp)
00dc2f4f 181{
c5be433b 182 char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
349ad1fe 183 if (!str)
c5be433b 184 str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
349ad1fe 185 return str;
00dc2f4f 186}
187
c5be433b 188/* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
e5a95ffb 189static char *
c5be433b 190get_emd_part(SV **prev_pathp, char *trailing_path, ...)
00dc2f4f 191{
dc9e4912 192 char base[10];
e5a95ffb 193 va_list ap;
e24c7c18 194 char mod_name[MAX_PATH+1];
00dc2f4f 195 char *ptr;
e5a95ffb 196 char *optr;
197 char *strip;
198 int oldsize, newsize;
199
200 va_start(ap, trailing_path);
201 strip = va_arg(ap, char *);
202
cceca5ed 203 sprintf(base, "%5.3f",
204 (double)PERL_REVISION + ((double)PERL_VERSION / (double)1000));
dc9e4912 205
8ac9c18d 206 if (!*w32_module_name) {
207 GetModuleFileName((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
208 ? GetModuleHandle(NULL)
209 : w32_perldll_handle),
210 w32_module_name, sizeof(w32_module_name));
211
212 /* try to get full path to binary (which may be mangled when perl is
213 * run from a 16-bit app) */
bf49b057 214 /*PerlIO_printf(Perl_debug_log, "Before %s\n", w32_module_name);*/
8ac9c18d 215 (void)win32_longpath(w32_module_name);
bf49b057 216 /*PerlIO_printf(Perl_debug_log, "After %s\n", w32_module_name);*/
8ac9c18d 217
218 /* normalize to forward slashes */
219 ptr = w32_module_name;
220 while (*ptr) {
221 if (*ptr == '\\')
222 *ptr = '/';
223 ++ptr;
224 }
95140b98 225 }
8ac9c18d 226 strcpy(mod_name, w32_module_name);
95140b98 227 ptr = strrchr(mod_name, '/');
e5a95ffb 228 while (ptr && strip) {
229 /* look for directories to skip back */
230 optr = ptr;
00dc2f4f 231 *ptr = '\0';
95140b98 232 ptr = strrchr(mod_name, '/');
1c39adb2 233 /* avoid stripping component if there is no slash,
234 * or it doesn't match ... */
e5a95ffb 235 if (!ptr || stricmp(ptr+1, strip) != 0) {
1c39adb2 236 /* ... but not if component matches 5.00X* */
237 if (!ptr || !(*strip == '5' && *(ptr+1) == '5'
238 && strncmp(strip, base, 5) == 0
239 && strncmp(ptr+1, base, 5) == 0))
95140b98 240 {
241 *optr = '/';
80252599 242 ptr = optr;
243 }
00dc2f4f 244 }
e5a95ffb 245 strip = va_arg(ap, char *);
00dc2f4f 246 }
e5a95ffb 247 if (!ptr) {
248 ptr = mod_name;
249 *ptr++ = '.';
95140b98 250 *ptr = '/';
00dc2f4f 251 }
e5a95ffb 252 va_end(ap);
253 strcpy(++ptr, trailing_path);
254
dc9e4912 255 /* only add directory if it exists */
349ad1fe 256 if (GetFileAttributes(mod_name) != (DWORD) -1) {
dc9e4912 257 /* directory exists */
c5be433b 258 dTHXo;
259 if (!*prev_pathp)
260 *prev_pathp = sv_2mortal(newSVpvn("",0));
261 sv_catpvn(*prev_pathp, ";", 1);
262 sv_catpv(*prev_pathp, mod_name);
263 return SvPVX(*prev_pathp);
00dc2f4f 264 }
00dc2f4f 265
cf11f4bf 266 return Nullch;
00dc2f4f 267}
268
269char *
c5be433b 270win32_get_privlib(char *pl)
00dc2f4f 271{
c5be433b 272 dTHXo;
e5a95ffb 273 char *stdlib = "lib";
274 char buffer[MAX_PATH+1];
51371543 275 SV *sv = Nullsv;
00dc2f4f 276
e5a95ffb 277 /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
278 sprintf(buffer, "%s-%s", stdlib, pl);
c5be433b 279 if (!get_regstr(buffer, &sv))
280 (void)get_regstr(stdlib, &sv);
00dc2f4f 281
e5a95ffb 282 /* $stdlib .= ";$EMD/../../lib" */
c5be433b 283 return get_emd_part(&sv, stdlib, ARCHNAME, "bin", Nullch);
00dc2f4f 284}
285
68dc0745 286char *
c5be433b 287win32_get_sitelib(char *pl)
00dc2f4f 288{
c5be433b 289 dTHXo;
e5a95ffb 290 char *sitelib = "sitelib";
291 char regstr[40];
e24c7c18 292 char pathstr[MAX_PATH+1];
e5a95ffb 293 DWORD datalen;
e5a95ffb 294 int len, newsize;
51371543 295 SV *sv1 = Nullsv;
296 SV *sv2 = Nullsv;
00dc2f4f 297
298 /* $HKCU{"sitelib-$]"} || $HKLM{"sitelib-$]"} . ---; */
e5a95ffb 299 sprintf(regstr, "%s-%s", sitelib, pl);
c5be433b 300 (void)get_regstr(regstr, &sv1);
e5a95ffb 301
302 /* $sitelib .=
303 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/$]/lib"; */
95140b98 304 sprintf(pathstr, "site/%s/lib", pl);
c5be433b 305 (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
51371543 306 if (!sv1 && strlen(pl) == 7) {
cf11f4bf 307 /* pl may have been SUBVERSION-specific; try again without
308 * SUBVERSION */
309 sprintf(pathstr, "site/%.5s/lib", pl);
c5be433b 310 (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
cf11f4bf 311 }
00dc2f4f 312
313 /* $HKCU{'sitelib'} || $HKLM{'sitelib'} . ---; */
c5be433b 314 (void)get_regstr(sitelib, &sv2);
00dc2f4f 315
e5a95ffb 316 /* $sitelib .=
317 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/lib"; */
c5be433b 318 (void)get_emd_part(&sv2, "site/lib", ARCHNAME, "bin", pl, Nullch);
e5a95ffb 319
51371543 320 if (!sv1 && !sv2)
321 return Nullch;
322 if (!sv1)
323 return SvPVX(sv2);
324 if (!sv2)
325 return SvPVX(sv1);
e5a95ffb 326
349ad1fe 327 sv_catpvn(sv1, ";", 1);
328 sv_catsv(sv1, sv2);
e5a95ffb 329
349ad1fe 330 return SvPVX(sv1);
68dc0745 331}
0a753a76 332
b4793f7f 333
2d7a9237 334static BOOL
e200fe59 335has_shell_metachars(char *ptr)
68dc0745 336{
337 int inquote = 0;
338 char quote = '\0';
339
340 /*
341 * Scan string looking for redirection (< or >) or pipe
e200fe59 342 * characters (|) that are not in a quoted string.
343 * Shell variable interpolation (%VAR%) can also happen inside strings.
68dc0745 344 */
9404a519 345 while (*ptr) {
68dc0745 346 switch(*ptr) {
e200fe59 347 case '%':
348 return TRUE;
68dc0745 349 case '\'':
350 case '\"':
9404a519 351 if (inquote) {
352 if (quote == *ptr) {
68dc0745 353 inquote = 0;
354 quote = '\0';
0a753a76 355 }
68dc0745 356 }
357 else {
358 quote = *ptr;
359 inquote++;
360 }
361 break;
362 case '>':
363 case '<':
364 case '|':
9404a519 365 if (!inquote)
68dc0745 366 return TRUE;
367 default:
368 break;
0a753a76 369 }
68dc0745 370 ++ptr;
371 }
372 return FALSE;
0a753a76 373}
374
32e30700 375#if !defined(PERL_IMPLICIT_SYS)
68dc0745 376/* since the current process environment is being updated in util.c
377 * the library functions will get the correct environment
378 */
379PerlIO *
4f63d024 380Perl_my_popen(pTHX_ char *cmd, char *mode)
0a753a76 381{
382#ifdef FIXCMD
68dc0745 383#define fixcmd(x) { \
384 char *pspace = strchr((x),' '); \
385 if (pspace) { \
386 char *p = (x); \
387 while (p < pspace) { \
388 if (*p == '/') \
389 *p = '\\'; \
390 p++; \
391 } \
392 } \
393 }
0a753a76 394#else
395#define fixcmd(x)
396#endif
68dc0745 397 fixcmd(cmd);
45bc9206 398 PERL_FLUSHALL_FOR_CHILD;
0a753a76 399 return win32_popen(cmd, mode);
0a753a76 400}
401
68dc0745 402long
4f63d024 403Perl_my_pclose(pTHX_ PerlIO *fp)
0a753a76 404{
405 return win32_pclose(fp);
406}
c69f6586 407#endif
0a753a76 408
0cb96387 409DllExport unsigned long
410win32_os_id(void)
0a753a76 411{
8b10511d 412 static OSVERSIONINFO osver;
0a753a76 413
2d7a9237 414 if (osver.dwPlatformId != w32_platform) {
8b10511d 415 memset(&osver, 0, sizeof(OSVERSIONINFO));
416 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
417 GetVersionEx(&osver);
2d7a9237 418 w32_platform = osver.dwPlatformId;
8b10511d 419 }
0cb96387 420 return (unsigned long)w32_platform;
0a753a76 421}
422
ce1da67e 423/* Tokenize a string. Words are null-separated, and the list
424 * ends with a doubled null. Any character (except null and
425 * including backslash) may be escaped by preceding it with a
426 * backslash (the backslash will be stripped).
427 * Returns number of words in result buffer.
428 */
429static long
430tokenize(char *str, char **dest, char ***destv)
431{
432 char *retstart = Nullch;
433 char **retvstart = 0;
434 int items = -1;
435 if (str) {
c5be433b 436 dTHXo;
ce1da67e 437 int slen = strlen(str);
438 register char *ret;
439 register char **retv;
440 New(1307, ret, slen+2, char);
441 New(1308, retv, (slen+3)/2, char*);
442
443 retstart = ret;
444 retvstart = retv;
445 *retv = ret;
446 items = 0;
447 while (*str) {
448 *ret = *str++;
449 if (*ret == '\\' && *str)
450 *ret = *str++;
451 else if (*ret == ' ') {
452 while (*str == ' ')
453 str++;
454 if (ret == retstart)
455 ret--;
456 else {
457 *ret = '\0';
458 ++items;
459 if (*str)
460 *++retv = ret+1;
461 }
462 }
463 else if (!*str)
464 ++items;
465 ret++;
466 }
467 retvstart[items] = Nullch;
468 *ret++ = '\0';
469 *ret = '\0';
470 }
471 *dest = retstart;
472 *destv = retvstart;
473 return items;
474}
475
476static void
2d7a9237 477get_shell(void)
0a753a76 478{
c5be433b 479 dTHXo;
ce1da67e 480 if (!w32_perlshell_tokens) {
174c211a 481 /* we don't use COMSPEC here for two reasons:
482 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
483 * uncontrolled unportability of the ensuing scripts.
484 * 2. PERL5SHELL could be set to a shell that may not be fit for
485 * interactive use (which is what most programs look in COMSPEC
486 * for).
487 */
ce1da67e 488 char* defaultshell = (IsWinNT() ? "cmd.exe /x/c" : "command.com /c");
489 char *usershell = getenv("PERL5SHELL");
490 w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
491 &w32_perlshell_tokens,
492 &w32_perlshell_vec);
68dc0745 493 }
0a753a76 494}
495
68dc0745 496int
c5be433b 497do_aspawn(void *vreally, void **vmark, void **vsp)
0a753a76 498{
c5be433b 499 dTHXo;
2d7a9237 500 SV *really = (SV*)vreally;
501 SV **mark = (SV**)vmark;
502 SV **sp = (SV**)vsp;
68dc0745 503 char **argv;
2d7a9237 504 char *str;
68dc0745 505 int status;
2d7a9237 506 int flag = P_WAIT;
68dc0745 507 int index = 0;
68dc0745 508
2d7a9237 509 if (sp <= mark)
510 return -1;
68dc0745 511
ce1da67e 512 get_shell();
513 New(1306, argv, (sp - mark) + w32_perlshell_items + 2, char*);
2d7a9237 514
515 if (SvNIOKp(*(mark+1)) && !SvPOKp(*(mark+1))) {
516 ++mark;
517 flag = SvIVx(*mark);
68dc0745 518 }
519
9404a519 520 while (++mark <= sp) {
bb897dfc 521 if (*mark && (str = SvPV_nolen(*mark)))
2d7a9237 522 argv[index++] = str;
523 else
524 argv[index++] = "";
68dc0745 525 }
526 argv[index++] = 0;
527
2d7a9237 528 status = win32_spawnvp(flag,
bb897dfc 529 (const char*)(really ? SvPV_nolen(really) : argv[0]),
2d7a9237 530 (const char* const*)argv);
531
80252599 532 if (status < 0 && (errno == ENOEXEC || errno == ENOENT)) {
2d7a9237 533 /* possible shell-builtin, invoke with shell */
ce1da67e 534 int sh_items;
535 sh_items = w32_perlshell_items;
2d7a9237 536 while (--index >= 0)
537 argv[index+sh_items] = argv[index];
ce1da67e 538 while (--sh_items >= 0)
539 argv[sh_items] = w32_perlshell_vec[sh_items];
2d7a9237 540
541 status = win32_spawnvp(flag,
bb897dfc 542 (const char*)(really ? SvPV_nolen(really) : argv[0]),
2d7a9237 543 (const char* const*)argv);
544 }
68dc0745 545
50892819 546 if (flag != P_NOWAIT) {
547 if (status < 0) {
0453d815 548 dTHR;
549 if (ckWARN(WARN_EXEC))
550 Perl_warner(aTHX_ WARN_EXEC, "Can't spawn \"%s\": %s", argv[0], strerror(errno));
50892819 551 status = 255 * 256;
552 }
553 else
554 status *= 256;
b28d0864 555 PL_statusvalue = status;
5aabfad6 556 }
ce1da67e 557 Safefree(argv);
50892819 558 return (status);
68dc0745 559}
560
c69f6586 561int
c5be433b 562do_spawn2(char *cmd, int exectype)
68dc0745 563{
c5be433b 564 dTHXo;
68dc0745 565 char **a;
566 char *s;
567 char **argv;
568 int status = -1;
569 BOOL needToTry = TRUE;
2d7a9237 570 char *cmd2;
68dc0745 571
2d7a9237 572 /* Save an extra exec if possible. See if there are shell
573 * metacharacters in it */
e200fe59 574 if (!has_shell_metachars(cmd)) {
fc36a67e 575 New(1301,argv, strlen(cmd) / 2 + 2, char*);
576 New(1302,cmd2, strlen(cmd) + 1, char);
68dc0745 577 strcpy(cmd2, cmd);
578 a = argv;
579 for (s = cmd2; *s;) {
de030af3 580 while (*s && isSPACE(*s))
68dc0745 581 s++;
582 if (*s)
583 *(a++) = s;
de030af3 584 while (*s && !isSPACE(*s))
68dc0745 585 s++;
9404a519 586 if (*s)
68dc0745 587 *s++ = '\0';
0a753a76 588 }
68dc0745 589 *a = Nullch;
ce1da67e 590 if (argv[0]) {
6890e559 591 switch (exectype) {
592 case EXECF_SPAWN:
593 status = win32_spawnvp(P_WAIT, argv[0],
594 (const char* const*)argv);
595 break;
596 case EXECF_SPAWN_NOWAIT:
597 status = win32_spawnvp(P_NOWAIT, argv[0],
598 (const char* const*)argv);
599 break;
600 case EXECF_EXEC:
601 status = win32_execvp(argv[0], (const char* const*)argv);
602 break;
603 }
2d7a9237 604 if (status != -1 || errno == 0)
68dc0745 605 needToTry = FALSE;
0a753a76 606 }
0a753a76 607 Safefree(argv);
68dc0745 608 Safefree(cmd2);
609 }
2d7a9237 610 if (needToTry) {
ce1da67e 611 char **argv;
612 int i = -1;
613 get_shell();
614 New(1306, argv, w32_perlshell_items + 2, char*);
615 while (++i < w32_perlshell_items)
616 argv[i] = w32_perlshell_vec[i];
2d7a9237 617 argv[i++] = cmd;
618 argv[i] = Nullch;
6890e559 619 switch (exectype) {
620 case EXECF_SPAWN:
621 status = win32_spawnvp(P_WAIT, argv[0],
622 (const char* const*)argv);
623 break;
624 case EXECF_SPAWN_NOWAIT:
625 status = win32_spawnvp(P_NOWAIT, argv[0],
626 (const char* const*)argv);
627 break;
628 case EXECF_EXEC:
629 status = win32_execvp(argv[0], (const char* const*)argv);
630 break;
631 }
ce1da67e 632 cmd = argv[0];
633 Safefree(argv);
68dc0745 634 }
50892819 635 if (exectype != EXECF_SPAWN_NOWAIT) {
636 if (status < 0) {
0453d815 637 dTHR;
638 if (ckWARN(WARN_EXEC))
639 Perl_warner(aTHX_ WARN_EXEC, "Can't %s \"%s\": %s",
50892819 640 (exectype == EXECF_EXEC ? "exec" : "spawn"),
641 cmd, strerror(errno));
642 status = 255 * 256;
643 }
644 else
645 status *= 256;
b28d0864 646 PL_statusvalue = status;
5aabfad6 647 }
50892819 648 return (status);
0a753a76 649}
650
6890e559 651int
c5be433b 652do_spawn(char *cmd)
6890e559 653{
c5be433b 654 return do_spawn2(cmd, EXECF_SPAWN);
6890e559 655}
656
2d7a9237 657int
c5be433b 658do_spawn_nowait(char *cmd)
2d7a9237 659{
c5be433b 660 return do_spawn2(cmd, EXECF_SPAWN_NOWAIT);
2d7a9237 661}
662
6890e559 663bool
4f63d024 664Perl_do_exec(pTHX_ char *cmd)
6890e559 665{
c5be433b 666 do_spawn2(cmd, EXECF_EXEC);
6890e559 667 return FALSE;
668}
669
68dc0745 670/* The idea here is to read all the directory names into a string table
671 * (separated by nulls) and when one of the other dir functions is called
672 * return the pointer to the current file name.
673 */
c5be433b 674DllExport DIR *
ce2e26e5 675win32_opendir(char *filename)
0a753a76 676{
c5be433b 677 dTHXo;
95136add 678 DIR *dirp;
9404a519 679 long len;
680 long idx;
681 char scanname[MAX_PATH+3];
682 struct stat sbuf;
7fac1903 683 WIN32_FIND_DATAA aFindData;
684 WIN32_FIND_DATAW wFindData;
9404a519 685 HANDLE fh;
7fac1903 686 char buffer[MAX_PATH*2];
687 WCHAR wbuffer[MAX_PATH];
95136add 688 char* ptr;
9404a519 689
690 len = strlen(filename);
691 if (len > MAX_PATH)
692 return NULL;
68dc0745 693
694 /* check to see if filename is a directory */
69d3ab13 695 if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
24caa93f 696 return NULL;
68dc0745 697
68dc0745 698 /* Get us a DIR structure */
95136add 699 Newz(1303, dirp, 1, DIR);
68dc0745 700
701 /* Create the search pattern */
702 strcpy(scanname, filename);
23db2e2d 703
704 /* bare drive name means look in cwd for drive */
705 if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') {
706 scanname[len++] = '.';
707 scanname[len++] = '/';
708 }
709 else if (scanname[len-1] != '/' && scanname[len-1] != '\\') {
9404a519 710 scanname[len++] = '/';
23db2e2d 711 }
9404a519 712 scanname[len++] = '*';
713 scanname[len] = '\0';
68dc0745 714
715 /* do the FindFirstFile call */
7fac1903 716 if (USING_WIDE()) {
0cb96387 717 A2WHELPER(scanname, wbuffer, sizeof(wbuffer));
7fac1903 718 fh = FindFirstFileW(wbuffer, &wFindData);
719 }
720 else {
721 fh = FindFirstFileA(scanname, &aFindData);
722 }
95136add 723 dirp->handle = fh;
9404a519 724 if (fh == INVALID_HANDLE_VALUE) {
95136add 725 DWORD err = GetLastError();
21e72512 726 /* FindFirstFile() fails on empty drives! */
95136add 727 switch (err) {
728 case ERROR_FILE_NOT_FOUND:
729 return dirp;
730 case ERROR_NO_MORE_FILES:
731 case ERROR_PATH_NOT_FOUND:
732 errno = ENOENT;
733 break;
734 case ERROR_NOT_ENOUGH_MEMORY:
735 errno = ENOMEM;
736 break;
737 default:
738 errno = EINVAL;
739 break;
740 }
741 Safefree(dirp);
68dc0745 742 return NULL;
743 }
744
745 /* now allocate the first part of the string table for
746 * the filenames that we find.
747 */
7fac1903 748 if (USING_WIDE()) {
0cb96387 749 W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer));
7fac1903 750 ptr = buffer;
751 }
752 else {
753 ptr = aFindData.cFileName;
754 }
755 idx = strlen(ptr)+1;
95136add 756 if (idx < 256)
757 dirp->size = 128;
758 else
759 dirp->size = idx;
760 New(1304, dirp->start, dirp->size, char);
761 strcpy(dirp->start, ptr);
762 dirp->nfiles++;
763 dirp->end = dirp->curr = dirp->start;
764 dirp->end += idx;
765 return dirp;
0a753a76 766}
767
768
68dc0745 769/* Readdir just returns the current string pointer and bumps the
770 * string pointer to the nDllExport entry.
771 */
c5be433b 772DllExport struct direct *
ce2e26e5 773win32_readdir(DIR *dirp)
0a753a76 774{
95136add 775 long len;
0a753a76 776
68dc0745 777 if (dirp->curr) {
778 /* first set up the structure to return */
779 len = strlen(dirp->curr);
0f38926b 780 strcpy(dirp->dirstr.d_name, dirp->curr);
68dc0745 781 dirp->dirstr.d_namlen = len;
0a753a76 782
68dc0745 783 /* Fake an inode */
0f38926b 784 dirp->dirstr.d_ino = dirp->curr - dirp->start;
0a753a76 785
95136add 786 /* Now set up for the next call to readdir */
68dc0745 787 dirp->curr += len + 1;
95136add 788 if (dirp->curr >= dirp->end) {
789 dTHXo;
790 char* ptr;
791 BOOL res;
792 WIN32_FIND_DATAW wFindData;
793 WIN32_FIND_DATAA aFindData;
794 char buffer[MAX_PATH*2];
795
796 /* finding the next file that matches the wildcard
797 * (which should be all of them in this directory!).
95136add 798 */
799 if (USING_WIDE()) {
800 res = FindNextFileW(dirp->handle, &wFindData);
801 if (res) {
802 W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer));
803 ptr = buffer;
804 }
805 }
806 else {
807 res = FindNextFileA(dirp->handle, &aFindData);
808 if (res)
809 ptr = aFindData.cFileName;
810 }
811 if (res) {
0f38926b 812 long endpos = dirp->end - dirp->start;
813 long newsize = endpos + strlen(ptr) + 1;
95136add 814 /* bump the string table size by enough for the
815 * new name and it's null terminator */
0f38926b 816 while (newsize > dirp->size) {
817 long curpos = dirp->curr - dirp->start;
95136add 818 dirp->size *= 2;
819 Renew(dirp->start, dirp->size, char);
0f38926b 820 dirp->curr = dirp->start + curpos;
95136add 821 }
0f38926b 822 strcpy(dirp->start + endpos, ptr);
823 dirp->end = dirp->start + newsize;
95136add 824 dirp->nfiles++;
825 }
826 else
827 dirp->curr = NULL;
68dc0745 828 }
68dc0745 829 return &(dirp->dirstr);
830 }
831 else
832 return NULL;
0a753a76 833}
834
68dc0745 835/* Telldir returns the current string pointer position */
c5be433b 836DllExport long
ce2e26e5 837win32_telldir(DIR *dirp)
0a753a76 838{
95136add 839 return (dirp->curr - dirp->start);
0a753a76 840}
841
842
68dc0745 843/* Seekdir moves the string pointer to a previously saved position
95136add 844 * (returned by telldir).
68dc0745 845 */
c5be433b 846DllExport void
ce2e26e5 847win32_seekdir(DIR *dirp, long loc)
0a753a76 848{
95136add 849 dirp->curr = dirp->start + loc;
0a753a76 850}
851
68dc0745 852/* Rewinddir resets the string pointer to the start */
c5be433b 853DllExport void
ce2e26e5 854win32_rewinddir(DIR *dirp)
0a753a76 855{
856 dirp->curr = dirp->start;
857}
858
68dc0745 859/* free the memory allocated by opendir */
c5be433b 860DllExport int
ce2e26e5 861win32_closedir(DIR *dirp)
0a753a76 862{
c5be433b 863 dTHXo;
95136add 864 if (dirp->handle != INVALID_HANDLE_VALUE)
0f38926b 865 FindClose(dirp->handle);
0a753a76 866 Safefree(dirp->start);
867 Safefree(dirp);
68dc0745 868 return 1;
0a753a76 869}
870
871
68dc0745 872/*
873 * various stubs
874 */
0a753a76 875
876
68dc0745 877/* Ownership
878 *
879 * Just pretend that everyone is a superuser. NT will let us know if
880 * we don\'t really have permission to do something.
881 */
0a753a76 882
883#define ROOT_UID ((uid_t)0)
884#define ROOT_GID ((gid_t)0)
885
68dc0745 886uid_t
887getuid(void)
0a753a76 888{
68dc0745 889 return ROOT_UID;
0a753a76 890}
891
68dc0745 892uid_t
893geteuid(void)
0a753a76 894{
68dc0745 895 return ROOT_UID;
0a753a76 896}
897
68dc0745 898gid_t
899getgid(void)
0a753a76 900{
68dc0745 901 return ROOT_GID;
0a753a76 902}
903
68dc0745 904gid_t
905getegid(void)
0a753a76 906{
68dc0745 907 return ROOT_GID;
0a753a76 908}
909
68dc0745 910int
22239a37 911setuid(uid_t auid)
0a753a76 912{
22239a37 913 return (auid == ROOT_UID ? 0 : -1);
0a753a76 914}
915
68dc0745 916int
22239a37 917setgid(gid_t agid)
0a753a76 918{
22239a37 919 return (agid == ROOT_GID ? 0 : -1);
0a753a76 920}
921
e34ffe5a 922char *
923getlogin(void)
924{
c5be433b 925 dTHXo;
e34ffe5a 926 char *buf = getlogin_buffer;
927 DWORD size = sizeof(getlogin_buffer);
928 if (GetUserName(buf,&size))
929 return buf;
930 return (char*)NULL;
931}
932
b990f8c8 933int
934chown(const char *path, uid_t owner, gid_t group)
935{
936 /* XXX noop */
1c1c7f20 937 return 0;
b990f8c8 938}
939
0aaad0ff 940static long
941find_pid(int pid)
0a753a76 942{
c5be433b 943 dTHXo;
51371543 944 long child;
f55ee38a 945 for (child = 0 ; child < w32_num_children ; ++child) {
0aaad0ff 946 if (w32_child_pids[child] == pid)
947 return child;
948 }
949 return -1;
950}
951
952static void
953remove_dead_process(long child)
954{
955 if (child >= 0) {
c5be433b 956 dTHXo;
0aaad0ff 957 CloseHandle(w32_child_handles[child]);
958 Copy(&w32_child_handles[child+1], &w32_child_handles[child],
959 (w32_num_children-child-1), HANDLE);
960 Copy(&w32_child_pids[child+1], &w32_child_pids[child],
961 (w32_num_children-child-1), DWORD);
962 w32_num_children--;
f55ee38a 963 }
f55ee38a 964}
965
966DllExport int
967win32_kill(int pid, int sig)
968{
0aaad0ff 969 HANDLE hProcess;
970 hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
971 if (hProcess && TerminateProcess(hProcess, sig))
68dc0745 972 CloseHandle(hProcess);
0aaad0ff 973 else {
974 errno = EINVAL;
975 return -1;
68dc0745 976 }
977 return 0;
0a753a76 978}
fbbbcc48 979
68dc0745 980/*
981 * File system stuff
982 */
0a753a76 983
f3986ebb 984DllExport unsigned int
985win32_sleep(unsigned int t)
0a753a76 986{
68dc0745 987 Sleep(t*1000);
988 return 0;
0a753a76 989}
990
68dc0745 991DllExport int
992win32_stat(const char *path, struct stat *buffer)
0a753a76 993{
c5be433b 994 dTHXo;
24caa93f 995 char t[MAX_PATH+1];
68dc0745 996 int l = strlen(path);
67fbe06e 997 int res;
7fac1903 998 WCHAR wbuffer[MAX_PATH];
0a753a76 999
68dc0745 1000 if (l > 1) {
1001 switch(path[l - 1]) {
e1dbac94 1002 /* FindFirstFile() and stat() are buggy with a trailing
1003 * backslash, so change it to a forward slash :-( */
68dc0745 1004 case '\\':
e1dbac94 1005 strncpy(t, path, l-1);
1006 t[l - 1] = '/';
1007 t[l] = '\0';
1008 path = t;
1009 break;
23db2e2d 1010 /* FindFirstFile() is buggy with "x:", so add a dot :-( */
e1dbac94 1011 case ':':
1012 if (l == 2 && isALPHA(path[0])) {
23db2e2d 1013 t[0] = path[0]; t[1] = ':'; t[2] = '.'; t[3] = '\0';
e1dbac94 1014 l = 3;
1015 path = t;
1016 }
1017 break;
68dc0745 1018 }
1019 }
7fac1903 1020 if (USING_WIDE()) {
0cb96387 1021 A2WHELPER(path, wbuffer, sizeof(wbuffer));
7fac1903 1022 res = _wstat(wbuffer, (struct _stat *)buffer);
1023 }
1024 else {
1025 res = stat(path, buffer);
1026 }
24caa93f 1027 if (res < 0) {
1028 /* CRT is buggy on sharenames, so make sure it really isn't.
1029 * XXX using GetFileAttributesEx() will enable us to set
1030 * buffer->st_*time (but note that's not available on the
1031 * Windows of 1995) */
7fac1903 1032 DWORD r;
1033 if (USING_WIDE()) {
1034 r = GetFileAttributesW(wbuffer);
1035 }
1036 else {
1037 r = GetFileAttributesA(path);
1038 }
24caa93f 1039 if (r != 0xffffffff && (r & FILE_ATTRIBUTE_DIRECTORY)) {
e1dbac94 1040 /* buffer may still contain old garbage since stat() failed */
1041 Zero(buffer, 1, struct stat);
1042 buffer->st_mode = S_IFDIR | S_IREAD;
24caa93f 1043 errno = 0;
1044 if (!(r & FILE_ATTRIBUTE_READONLY))
1045 buffer->st_mode |= S_IWRITE | S_IEXEC;
1046 return 0;
1047 }
1048 }
24caa93f 1049 else {
e1dbac94 1050 if (l == 3 && isALPHA(path[0]) && path[1] == ':'
1051 && (path[2] == '\\' || path[2] == '/'))
2293b0e9 1052 {
1053 /* The drive can be inaccessible, some _stat()s are buggy */
7fac1903 1054 if (USING_WIDE()
1055 ? !GetVolumeInformationW(wbuffer,NULL,0,NULL,NULL,NULL,NULL,0)
1056 : !GetVolumeInformationA(path,NULL,0,NULL,NULL,NULL,NULL,0)) {
2293b0e9 1057 errno = ENOENT;
1058 return -1;
1059 }
1060 }
1061#ifdef __BORLANDC__
67fbe06e 1062 if (S_ISDIR(buffer->st_mode))
1063 buffer->st_mode |= S_IWRITE | S_IEXEC;
1064 else if (S_ISREG(buffer->st_mode)) {
1065 if (l >= 4 && path[l-4] == '.') {
1066 const char *e = path + l - 3;
1067 if (strnicmp(e,"exe",3)
1068 && strnicmp(e,"bat",3)
1069 && strnicmp(e,"com",3)
1070 && (IsWin95() || strnicmp(e,"cmd",3)))
1071 buffer->st_mode &= ~S_IEXEC;
1072 else
1073 buffer->st_mode |= S_IEXEC;
1074 }
1075 else
1076 buffer->st_mode &= ~S_IEXEC;
1077 }
67fbe06e 1078#endif
2293b0e9 1079 }
67fbe06e 1080 return res;
0a753a76 1081}
1082
8ac9c18d 1083/* Find the longname of a given path. path is destructively modified.
1084 * It should have space for at least MAX_PATH characters. */
1085DllExport char *
1086win32_longpath(char *path)
1087{
1088 WIN32_FIND_DATA fdata;
1089 HANDLE fhand;
1090 char tmpbuf[MAX_PATH+1];
1091 char *tmpstart = tmpbuf;
1092 char *start = path;
1093 char sep;
1094 if (!path)
1095 return Nullch;
1096
1097 /* drive prefix */
1098 if (isALPHA(path[0]) && path[1] == ':' &&
1099 (path[2] == '/' || path[2] == '\\'))
1100 {
1101 start = path + 2;
1102 *tmpstart++ = path[0];
1103 *tmpstart++ = ':';
1104 }
1105 /* UNC prefix */
1106 else if ((path[0] == '/' || path[0] == '\\') &&
1107 (path[1] == '/' || path[1] == '\\'))
1108 {
1109 start = path + 2;
52fcf7ee 1110 *tmpstart++ = path[0];
1111 *tmpstart++ = path[1];
8ac9c18d 1112 /* copy machine name */
1113 while (*start && *start != '/' && *start != '\\')
1114 *tmpstart++ = *start++;
1115 if (*start) {
52fcf7ee 1116 *tmpstart++ = *start;
8ac9c18d 1117 start++;
1118 /* copy share name */
1119 while (*start && *start != '/' && *start != '\\')
1120 *tmpstart++ = *start++;
1121 }
1122 }
1123 sep = *start++;
1124 if (sep == '/' || sep == '\\')
52fcf7ee 1125 *tmpstart++ = sep;
8ac9c18d 1126 *tmpstart = '\0';
1127 while (sep) {
1128 /* walk up to slash */
1129 while (*start && *start != '/' && *start != '\\')
1130 ++start;
1131
1132 /* discard doubled slashes */
1133 while (*start && (start[1] == '/' || start[1] == '\\'))
1134 ++start;
1135 sep = *start;
1136
1137 /* stop and find full name of component */
1138 *start = '\0';
1139 fhand = FindFirstFile(path,&fdata);
1140 if (fhand != INVALID_HANDLE_VALUE) {
1141 strcpy(tmpstart, fdata.cFileName);
1142 tmpstart += strlen(fdata.cFileName);
1143 if (sep)
52fcf7ee 1144 *tmpstart++ = sep;
8ac9c18d 1145 *tmpstart = '\0';
1146 *start++ = sep;
1147 FindClose(fhand);
1148 }
1149 else {
1150 /* failed a step, just return without side effects */
bf49b057 1151 /*PerlIO_printf(Perl_debug_log, "Failed to find %s\n", path);*/
8ac9c18d 1152 *start = sep;
1153 return Nullch;
1154 }
1155 }
1156 strcpy(path,tmpbuf);
1157 return path;
1158}
1159
0551aaa8 1160#ifndef USE_WIN32_RTL_ENV
1161
1162DllExport char *
1163win32_getenv(const char *name)
1164{
c5be433b 1165 dTHXo;
7fac1903 1166 WCHAR wBuffer[MAX_PATH];
0551aaa8 1167 DWORD needlen;
51371543 1168 SV *curitem = Nullsv;
58a50f62 1169
7fac1903 1170 if (USING_WIDE()) {
0cb96387 1171 A2WHELPER(name, wBuffer, sizeof(wBuffer));
51371543 1172 needlen = GetEnvironmentVariableW(wBuffer, NULL, 0);
7fac1903 1173 }
1174 else
51371543 1175 needlen = GetEnvironmentVariableA(name,NULL,0);
58a50f62 1176 if (needlen != 0) {
51371543 1177 curitem = sv_2mortal(newSVpvn("", 0));
7fac1903 1178 if (USING_WIDE()) {
51371543 1179 SV *acuritem;
1180 do {
1181 SvGROW(curitem, (needlen+1)*sizeof(WCHAR));
1182 needlen = GetEnvironmentVariableW(wBuffer,
1183 (WCHAR*)SvPVX(curitem),
1184 needlen);
1185 } while (needlen >= SvLEN(curitem)/sizeof(WCHAR));
c5be433b 1186 SvCUR_set(curitem, (needlen*sizeof(WCHAR))+1);
51371543 1187 acuritem = sv_2mortal(newSVsv(curitem));
1188 W2AHELPER((WCHAR*)SvPVX(acuritem), SvPVX(curitem), SvCUR(curitem));
7fac1903 1189 }
1190 else {
51371543 1191 do {
1192 SvGROW(curitem, needlen+1);
1193 needlen = GetEnvironmentVariableA(name,SvPVX(curitem),
1194 needlen);
1195 } while (needlen >= SvLEN(curitem));
1196 SvCUR_set(curitem, needlen);
58a50f62 1197 }
0551aaa8 1198 }
c934e9d4 1199 else {
7a5f8e82 1200 /* allow any environment variables that begin with 'PERL'
c934e9d4 1201 to be stored in the registry */
51371543 1202 if (strncmp(name, "PERL", 4) == 0)
c5be433b 1203 (void)get_regstr(name, &curitem);
c69f6586 1204 }
51371543 1205 if (curitem && SvCUR(curitem))
1206 return SvPVX(curitem);
58a50f62 1207
51371543 1208 return Nullch;
0551aaa8 1209}
1210
ac5c734f 1211DllExport int
1212win32_putenv(const char *name)
1213{
c5be433b 1214 dTHXo;
ac5c734f 1215 char* curitem;
1216 char* val;
7fac1903 1217 WCHAR* wCuritem;
1218 WCHAR* wVal;
1219 int length, relval = -1;
51371543 1220
73c4f7a1 1221 if (name) {
7fac1903 1222 if (USING_WIDE()) {
1223 length = strlen(name)+1;
1224 New(1309,wCuritem,length,WCHAR);
c5be433b 1225 A2WHELPER(name, wCuritem, length*sizeof(WCHAR));
7fac1903 1226 wVal = wcschr(wCuritem, '=');
1227 if(wVal) {
1228 *wVal++ = '\0';
1229 if(SetEnvironmentVariableW(wCuritem, *wVal ? wVal : NULL))
1230 relval = 0;
1231 }
1232 Safefree(wCuritem);
1233 }
1234 else {
1235 New(1309,curitem,strlen(name)+1,char);
1236 strcpy(curitem, name);
1237 val = strchr(curitem, '=');
1238 if(val) {
1239 /* The sane way to deal with the environment.
1240 * Has these advantages over putenv() & co.:
1241 * * enables us to store a truly empty value in the
1242 * environment (like in UNIX).
1243 * * we don't have to deal with RTL globals, bugs and leaks.
1244 * * Much faster.
1245 * Why you may want to enable USE_WIN32_RTL_ENV:
1246 * * environ[] and RTL functions will not reflect changes,
1247 * which might be an issue if extensions want to access
1248 * the env. via RTL. This cuts both ways, since RTL will
1249 * not see changes made by extensions that call the Win32
1250 * functions directly, either.
1251 * GSAR 97-06-07
1252 */
1253 *val++ = '\0';
1254 if(SetEnvironmentVariableA(curitem, *val ? val : NULL))
1255 relval = 0;
1256 }
1257 Safefree(curitem);
ac5c734f 1258 }
ac5c734f 1259 }
1260 return relval;
1261}
1262
0551aaa8 1263#endif
1264
d55594ae 1265static long
2d7a9237 1266filetime_to_clock(PFILETIME ft)
d55594ae 1267{
1268 __int64 qw = ft->dwHighDateTime;
1269 qw <<= 32;
1270 qw |= ft->dwLowDateTime;
1271 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
1272 return (long) qw;
1273}
1274
f3986ebb 1275DllExport int
1276win32_times(struct tms *timebuf)
0a753a76 1277{
d55594ae 1278 FILETIME user;
1279 FILETIME kernel;
1280 FILETIME dummy;
1281 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
1282 &kernel,&user)) {
2d7a9237 1283 timebuf->tms_utime = filetime_to_clock(&user);
1284 timebuf->tms_stime = filetime_to_clock(&kernel);
d55594ae 1285 timebuf->tms_cutime = 0;
1286 timebuf->tms_cstime = 0;
1287
1288 } else {
1289 /* That failed - e.g. Win95 fallback to clock() */
1290 clock_t t = clock();
1291 timebuf->tms_utime = t;
1292 timebuf->tms_stime = 0;
1293 timebuf->tms_cutime = 0;
1294 timebuf->tms_cstime = 0;
1295 }
68dc0745 1296 return 0;
0a753a76 1297}
1298
9c51cf4c 1299/* fix utime() so it works on directories in NT */
ad0751ec 1300static BOOL
1301filetime_from_time(PFILETIME pFileTime, time_t Time)
1302{
9c51cf4c 1303 struct tm *pTM = localtime(&Time);
ad0751ec 1304 SYSTEMTIME SystemTime;
9c51cf4c 1305 FILETIME LocalTime;
ad0751ec 1306
1307 if (pTM == NULL)
1308 return FALSE;
1309
1310 SystemTime.wYear = pTM->tm_year + 1900;
1311 SystemTime.wMonth = pTM->tm_mon + 1;
1312 SystemTime.wDay = pTM->tm_mday;
1313 SystemTime.wHour = pTM->tm_hour;
1314 SystemTime.wMinute = pTM->tm_min;
1315 SystemTime.wSecond = pTM->tm_sec;
1316 SystemTime.wMilliseconds = 0;
1317
9c51cf4c 1318 return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
1319 LocalFileTimeToFileTime(&LocalTime, pFileTime);
ad0751ec 1320}
1321
1322DllExport int
3b405fc5 1323win32_utime(const char *filename, struct utimbuf *times)
ad0751ec 1324{
c5be433b 1325 dTHXo;
ad0751ec 1326 HANDLE handle;
1327 FILETIME ftCreate;
1328 FILETIME ftAccess;
1329 FILETIME ftWrite;
1330 struct utimbuf TimeBuffer;
7fac1903 1331 WCHAR wbuffer[MAX_PATH];
ad0751ec 1332
7fac1903 1333 int rc;
1334 if (USING_WIDE()) {
0cb96387 1335 A2WHELPER(filename, wbuffer, sizeof(wbuffer));
7fac1903 1336 rc = _wutime(wbuffer, (struct _utimbuf*)times);
1337 }
1338 else {
1339 rc = utime(filename, times);
1340 }
ad0751ec 1341 /* EACCES: path specifies directory or readonly file */
1342 if (rc == 0 || errno != EACCES /* || !IsWinNT() */)
1343 return rc;
1344
1345 if (times == NULL) {
1346 times = &TimeBuffer;
1347 time(&times->actime);
1348 times->modtime = times->actime;
1349 }
1350
1351 /* This will (and should) still fail on readonly files */
7fac1903 1352 if (USING_WIDE()) {
1353 handle = CreateFileW(wbuffer, GENERIC_READ | GENERIC_WRITE,
1354 FILE_SHARE_READ | FILE_SHARE_DELETE, NULL,
1355 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1356 }
1357 else {
1358 handle = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE,
1359 FILE_SHARE_READ | FILE_SHARE_DELETE, NULL,
1360 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1361 }
ad0751ec 1362 if (handle == INVALID_HANDLE_VALUE)
1363 return rc;
1364
1365 if (GetFileTime(handle, &ftCreate, &ftAccess, &ftWrite) &&
1366 filetime_from_time(&ftAccess, times->actime) &&
1367 filetime_from_time(&ftWrite, times->modtime) &&
1368 SetFileTime(handle, &ftCreate, &ftAccess, &ftWrite))
1369 {
1370 rc = 0;
1371 }
1372
1373 CloseHandle(handle);
1374 return rc;
1375}
1376
2d7a9237 1377DllExport int
b2af26b1 1378win32_uname(struct utsname *name)
1379{
1380 struct hostent *hep;
1381 STRLEN nodemax = sizeof(name->nodename)-1;
1382 OSVERSIONINFO osver;
1383
1384 memset(&osver, 0, sizeof(OSVERSIONINFO));
1385 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1386 if (GetVersionEx(&osver)) {
1387 /* sysname */
1388 switch (osver.dwPlatformId) {
1389 case VER_PLATFORM_WIN32_WINDOWS:
1390 strcpy(name->sysname, "Windows");
1391 break;
1392 case VER_PLATFORM_WIN32_NT:
1393 strcpy(name->sysname, "Windows NT");
1394 break;
1395 case VER_PLATFORM_WIN32s:
1396 strcpy(name->sysname, "Win32s");
1397 break;
1398 default:
1399 strcpy(name->sysname, "Win32 Unknown");
1400 break;
1401 }
1402
cf6cacac 1403 /* release */
1404 sprintf(name->release, "%d.%d",
b2af26b1 1405 osver.dwMajorVersion, osver.dwMinorVersion);
1406
cf6cacac 1407 /* version */
1408 sprintf(name->version, "Build %d",
b2af26b1 1409 osver.dwPlatformId == VER_PLATFORM_WIN32_NT
1410 ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff));
1411 if (osver.szCSDVersion[0]) {
cf6cacac 1412 char *buf = name->version + strlen(name->version);
b2af26b1 1413 sprintf(buf, " (%s)", osver.szCSDVersion);
1414 }
1415 }
1416 else {
1417 *name->sysname = '\0';
1418 *name->version = '\0';
1419 *name->release = '\0';
1420 }
1421
1422 /* nodename */
1423 hep = win32_gethostbyname("localhost");
1424 if (hep) {
1425 STRLEN len = strlen(hep->h_name);
1426 if (len <= nodemax) {
1427 strcpy(name->nodename, hep->h_name);
1428 }
1429 else {
1430 strncpy(name->nodename, hep->h_name, nodemax);
1431 name->nodename[nodemax] = '\0';
1432 }
1433 }
1434 else {
1435 DWORD sz = nodemax;
1436 if (!GetComputerName(name->nodename, &sz))
1437 *name->nodename = '\0';
1438 }
1439
1440 /* machine (architecture) */
1441 {
1442 SYSTEM_INFO info;
1443 char *arch;
1444 GetSystemInfo(&info);
a6c40364 1445
2d63fa08 1446#if defined(__BORLANDC__) || defined(__MINGW32__)
a6c40364 1447 switch (info.u.s.wProcessorArchitecture) {
1448#else
b2af26b1 1449 switch (info.wProcessorArchitecture) {
a6c40364 1450#endif
b2af26b1 1451 case PROCESSOR_ARCHITECTURE_INTEL:
1452 arch = "x86"; break;
1453 case PROCESSOR_ARCHITECTURE_MIPS:
1454 arch = "mips"; break;
1455 case PROCESSOR_ARCHITECTURE_ALPHA:
1456 arch = "alpha"; break;
1457 case PROCESSOR_ARCHITECTURE_PPC:
1458 arch = "ppc"; break;
1459 default:
1460 arch = "unknown"; break;
1461 }
1462 strcpy(name->machine, arch);
1463 }
1464 return 0;
1465}
1466
1467DllExport int
f55ee38a 1468win32_waitpid(int pid, int *status, int flags)
1469{
c5be433b 1470 dTHXo;
0aaad0ff 1471 int retval = -1;
f55ee38a 1472 if (pid == -1)
0aaad0ff 1473 return win32_wait(status);
f55ee38a 1474 else {
0aaad0ff 1475 long child = find_pid(pid);
1476 if (child >= 0) {
1477 HANDLE hProcess = w32_child_handles[child];
1478 DWORD waitcode = WaitForSingleObject(hProcess, INFINITE);
1479 if (waitcode != WAIT_FAILED) {
1480 if (GetExitCodeProcess(hProcess, &waitcode)) {
1481 *status = (int)((waitcode & 0xff) << 8);
1482 retval = (int)w32_child_pids[child];
1483 remove_dead_process(child);
1484 return retval;
1485 }
1486 }
1487 else
1488 errno = ECHILD;
1489 }
1490 else {
1491 retval = cwait(status, pid, WAIT_CHILD);
1492 /* cwait() returns "correctly" on Borland */
8f1e745d 1493#ifndef __BORLANDC__
0aaad0ff 1494 if (status)
1495 *status *= 256;
f55ee38a 1496#endif
0aaad0ff 1497 }
f55ee38a 1498 }
0aaad0ff 1499 return retval >= 0 ? pid : retval;
f55ee38a 1500}
1501
1502DllExport int
2d7a9237 1503win32_wait(int *status)
1504{
2d7a9237 1505 /* XXX this wait emulation only knows about processes
1506 * spawned via win32_spawnvp(P_NOWAIT, ...).
1507 */
c5be433b 1508 dTHXo;
2d7a9237 1509 int i, retval;
1510 DWORD exitcode, waitcode;
1511
1512 if (!w32_num_children) {
1513 errno = ECHILD;
1514 return -1;
1515 }
1516
1517 /* if a child exists, wait for it to die */
1518 waitcode = WaitForMultipleObjects(w32_num_children,
0aaad0ff 1519 w32_child_handles,
2d7a9237 1520 FALSE,
1521 INFINITE);
1522 if (waitcode != WAIT_FAILED) {
1523 if (waitcode >= WAIT_ABANDONED_0
1524 && waitcode < WAIT_ABANDONED_0 + w32_num_children)
1525 i = waitcode - WAIT_ABANDONED_0;
1526 else
1527 i = waitcode - WAIT_OBJECT_0;
0aaad0ff 1528 if (GetExitCodeProcess(w32_child_handles[i], &exitcode) ) {
2d7a9237 1529 *status = (int)((exitcode & 0xff) << 8);
1530 retval = (int)w32_child_pids[i];
0aaad0ff 1531 remove_dead_process(i);
2d7a9237 1532 return retval;
1533 }
1534 }
1535
1536FAILED:
1537 errno = GetLastError();
1538 return -1;
2d7a9237 1539}
d55594ae 1540
2d7a9237 1541static UINT timerid = 0;
d55594ae 1542
1543static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
1544{
c5be433b 1545 dTHXo;
0cb96387 1546 KillTimer(NULL,timerid);
1547 timerid=0;
1548 sighandler(14);
d55594ae 1549}
1550
f3986ebb 1551DllExport unsigned int
1552win32_alarm(unsigned int sec)
0a753a76 1553{
d55594ae 1554 /*
1555 * the 'obvious' implentation is SetTimer() with a callback
1556 * which does whatever receiving SIGALRM would do
1557 * we cannot use SIGALRM even via raise() as it is not
1558 * one of the supported codes in <signal.h>
1559 *
1560 * Snag is unless something is looking at the message queue
1561 * nothing happens :-(
1562 */
c5be433b 1563 dTHXo;
d55594ae 1564 if (sec)
1565 {
1566 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
1567 if (!timerid)
4f63d024 1568 Perl_croak_nocontext("Cannot set timer");
d55594ae 1569 }
1570 else
1571 {
1572 if (timerid)
1573 {
1574 KillTimer(NULL,timerid);
1575 timerid=0;
1576 }
1577 }
68dc0745 1578 return 0;
0a753a76 1579}
1580
ff95b63e 1581#if defined(HAVE_DES_FCRYPT) || defined(PERL_OBJECT)
26618a56 1582#ifdef HAVE_DES_FCRYPT
2d77217b 1583extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf);
ff95b63e 1584#endif
26618a56 1585
1586DllExport char *
1587win32_crypt(const char *txt, const char *salt)
1588{
0b94c7bb 1589 dTHXo;
ff95b63e 1590#ifdef HAVE_DES_FCRYPT
26618a56 1591 dTHR;
2d77217b 1592 return des_fcrypt(txt, salt, crypt_buffer);
ff95b63e 1593#else
1594 die("The crypt() function is unimplemented due to excessive paranoia.");
b8957cf1 1595 return Nullch;
ff95b63e 1596#endif
26618a56 1597}
1598#endif
1599
f3986ebb 1600#ifdef USE_FIXED_OSFHANDLE
390b85e7 1601
1602EXTERN_C int __cdecl _alloc_osfhnd(void);
1603EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
1604EXTERN_C void __cdecl _lock_fhandle(int);
1605EXTERN_C void __cdecl _unlock_fhandle(int);
1606EXTERN_C void __cdecl _unlock(int);
1607
1608#if (_MSC_VER >= 1000)
1609typedef struct {
1610 long osfhnd; /* underlying OS file HANDLE */
1611 char osfile; /* attributes of file (e.g., open in text mode?) */
1612 char pipech; /* one char buffer for handles opened on pipes */
1613#if defined (_MT) && !defined (DLL_FOR_WIN32S)
1614 int lockinitflag;
1615 CRITICAL_SECTION lock;
1616#endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
1617} ioinfo;
1618
1619EXTERN_C ioinfo * __pioinfo[];
1620
1621#define IOINFO_L2E 5
1622#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
1623#define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
1624#define _osfile(i) (_pioinfo(i)->osfile)
1625
1626#else /* (_MSC_VER >= 1000) */
1627extern char _osfile[];
1628#endif /* (_MSC_VER >= 1000) */
1629
1630#define FOPEN 0x01 /* file handle open */
1631#define FAPPEND 0x20 /* file handle opened O_APPEND */
1632#define FDEV 0x40 /* file handle refers to device */
1633#define FTEXT 0x80 /* file handle is in text mode */
1634
1635#define _STREAM_LOCKS 26 /* Table of stream locks */
1636#define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */
1637#define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */
1638
1639/***
1640*int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
1641*
1642*Purpose:
1643* This function allocates a free C Runtime file handle and associates
1644* it with the Win32 HANDLE specified by the first parameter. This is a
1645* temperary fix for WIN95's brain damage GetFileType() error on socket
1646* we just bypass that call for socket
1647*
1648*Entry:
1649* long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
1650* int flags - flags to associate with C Runtime file handle.
1651*
1652*Exit:
1653* returns index of entry in fh, if successful
1654* return -1, if no free entry is found
1655*
1656*Exceptions:
1657*
1658*******************************************************************************/
1659
1660static int
1661my_open_osfhandle(long osfhandle, int flags)
1662{
1663 int fh;
1664 char fileflags; /* _osfile flags */
1665
1666 /* copy relevant flags from second parameter */
1667 fileflags = FDEV;
1668
9404a519 1669 if (flags & O_APPEND)
390b85e7 1670 fileflags |= FAPPEND;
1671
9404a519 1672 if (flags & O_TEXT)
390b85e7 1673 fileflags |= FTEXT;
1674
1675 /* attempt to allocate a C Runtime file handle */
9404a519 1676 if ((fh = _alloc_osfhnd()) == -1) {
390b85e7 1677 errno = EMFILE; /* too many open files */
1678 _doserrno = 0L; /* not an OS error */
1679 return -1; /* return error to caller */
1680 }
1681
1682 /* the file is open. now, set the info in _osfhnd array */
1683 _set_osfhnd(fh, osfhandle);
1684
1685 fileflags |= FOPEN; /* mark as open */
1686
1687#if (_MSC_VER >= 1000)
1688 _osfile(fh) = fileflags; /* set osfile entry */
1689 _unlock_fhandle(fh);
1690#else
1691 _osfile[fh] = fileflags; /* set osfile entry */
1692 _unlock(fh+_FH_LOCKS); /* unlock handle */
1693#endif
1694
1695 return fh; /* return handle */
1696}
1697
1698#define _open_osfhandle my_open_osfhandle
f3986ebb 1699#endif /* USE_FIXED_OSFHANDLE */
390b85e7 1700
1701/* simulate flock by locking a range on the file */
1702
1703#define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
1704#define LK_LEN 0xffff0000
1705
f3986ebb 1706DllExport int
1707win32_flock(int fd, int oper)
390b85e7 1708{
1709 OVERLAPPED o;
1710 int i = -1;
1711 HANDLE fh;
1712
f3986ebb 1713 if (!IsWinNT()) {
c5be433b 1714 dTHXo;
4f63d024 1715 Perl_croak_nocontext("flock() unimplemented on this platform");
f3986ebb 1716 return -1;
1717 }
390b85e7 1718 fh = (HANDLE)_get_osfhandle(fd);
1719 memset(&o, 0, sizeof(o));
1720
1721 switch(oper) {
1722 case LOCK_SH: /* shared lock */
1723 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
1724 break;
1725 case LOCK_EX: /* exclusive lock */
1726 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
1727 break;
1728 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
1729 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
1730 break;
1731 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
1732 LK_ERR(LockFileEx(fh,
1733 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1734 0, LK_LEN, 0, &o),i);
1735 break;
1736 case LOCK_UN: /* unlock lock */
1737 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
1738 break;
1739 default: /* unknown */
1740 errno = EINVAL;
1741 break;
1742 }
1743 return i;
1744}
1745
1746#undef LK_ERR
1747#undef LK_LEN
1748
68dc0745 1749/*
1750 * redirected io subsystem for all XS modules
1751 *
1752 */
0a753a76 1753
68dc0745 1754DllExport int *
1755win32_errno(void)
0a753a76 1756{
390b85e7 1757 return (&errno);
0a753a76 1758}
1759
dcb2879a 1760DllExport char ***
1761win32_environ(void)
1762{
390b85e7 1763 return (&(_environ));
dcb2879a 1764}
1765
68dc0745 1766/* the rest are the remapped stdio routines */
1767DllExport FILE *
1768win32_stderr(void)
0a753a76 1769{
390b85e7 1770 return (stderr);
0a753a76 1771}
1772
68dc0745 1773DllExport FILE *
1774win32_stdin(void)
0a753a76 1775{
390b85e7 1776 return (stdin);
0a753a76 1777}
1778
68dc0745 1779DllExport FILE *
1780win32_stdout()
0a753a76 1781{
390b85e7 1782 return (stdout);
0a753a76 1783}
1784
68dc0745 1785DllExport int
1786win32_ferror(FILE *fp)
0a753a76 1787{
390b85e7 1788 return (ferror(fp));
0a753a76 1789}
1790
1791
68dc0745 1792DllExport int
1793win32_feof(FILE *fp)
0a753a76 1794{
390b85e7 1795 return (feof(fp));
0a753a76 1796}
1797
68dc0745 1798/*
1799 * Since the errors returned by the socket error function
1800 * WSAGetLastError() are not known by the library routine strerror
1801 * we have to roll our own.
1802 */
0a753a76 1803
68dc0745 1804DllExport char *
1805win32_strerror(int e)
0a753a76 1806{
3e3baf6d 1807#ifndef __BORLANDC__ /* Borland intolerance */
68dc0745 1808 extern int sys_nerr;
3e3baf6d 1809#endif
68dc0745 1810 DWORD source = 0;
0a753a76 1811
9404a519 1812 if (e < 0 || e > sys_nerr) {
c5be433b 1813 dTHXo;
9404a519 1814 if (e < 0)
68dc0745 1815 e = GetLastError();
0a753a76 1816
9404a519 1817 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
68dc0745 1818 strerror_buffer, sizeof(strerror_buffer), NULL) == 0)
1819 strcpy(strerror_buffer, "Unknown Error");
0a753a76 1820
68dc0745 1821 return strerror_buffer;
1822 }
390b85e7 1823 return strerror(e);
0a753a76 1824}
1825
22fae026 1826DllExport void
c5be433b 1827win32_str_os_error(void *sv, DWORD dwErr)
22fae026 1828{
1829 DWORD dwLen;
1830 char *sMsg;
1831 dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
1832 |FORMAT_MESSAGE_IGNORE_INSERTS
1833 |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
1834 dwErr, 0, (char *)&sMsg, 1, NULL);
2ce77adf 1835 /* strip trailing whitespace and period */
22fae026 1836 if (0 < dwLen) {
2ce77adf 1837 do {
1838 --dwLen; /* dwLen doesn't include trailing null */
1839 } while (0 < dwLen && isSPACE(sMsg[dwLen]));
22fae026 1840 if ('.' != sMsg[dwLen])
1841 dwLen++;
2ce77adf 1842 sMsg[dwLen] = '\0';
22fae026 1843 }
1844 if (0 == dwLen) {
c69f6586 1845 sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
db7c17d7 1846 if (sMsg)
1847 dwLen = sprintf(sMsg,
1848 "Unknown error #0x%lX (lookup 0x%lX)",
1849 dwErr, GetLastError());
1850 }
1851 if (sMsg) {
c5be433b 1852 dTHXo;
db7c17d7 1853 sv_setpvn((SV*)sv, sMsg, dwLen);
1854 LocalFree(sMsg);
22fae026 1855 }
22fae026 1856}
1857
1858
68dc0745 1859DllExport int
1860win32_fprintf(FILE *fp, const char *format, ...)
0a753a76 1861{
68dc0745 1862 va_list marker;
1863 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 1864
390b85e7 1865 return (vfprintf(fp, format, marker));
0a753a76 1866}
1867
68dc0745 1868DllExport int
1869win32_printf(const char *format, ...)
0a753a76 1870{
68dc0745 1871 va_list marker;
1872 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 1873
390b85e7 1874 return (vprintf(format, marker));
0a753a76 1875}
1876
68dc0745 1877DllExport int
1878win32_vfprintf(FILE *fp, const char *format, va_list args)
0a753a76 1879{
390b85e7 1880 return (vfprintf(fp, format, args));
0a753a76 1881}
1882
96e4d5b1 1883DllExport int
1884win32_vprintf(const char *format, va_list args)
1885{
390b85e7 1886 return (vprintf(format, args));
96e4d5b1 1887}
1888
68dc0745 1889DllExport size_t
1890win32_fread(void *buf, size_t size, size_t count, FILE *fp)
0a753a76 1891{
390b85e7 1892 return fread(buf, size, count, fp);
0a753a76 1893}
1894
68dc0745 1895DllExport size_t
1896win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
0a753a76 1897{
390b85e7 1898 return fwrite(buf, size, count, fp);
0a753a76 1899}
1900
7fac1903 1901#define MODE_SIZE 10
1902
68dc0745 1903DllExport FILE *
1904win32_fopen(const char *filename, const char *mode)
0a753a76 1905{
c5be433b 1906 dTHXo;
7fac1903 1907 WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH];
c5be433b 1908
1909 if (!*filename)
1910 return NULL;
1911
68dc0745 1912 if (stricmp(filename, "/dev/null")==0)
7fac1903 1913 filename = "NUL";
1914
1915 if (USING_WIDE()) {
0cb96387 1916 A2WHELPER(mode, wMode, sizeof(wMode));
1917 A2WHELPER(filename, wBuffer, sizeof(wBuffer));
7fac1903 1918 return _wfopen(wBuffer, wMode);
1919 }
390b85e7 1920 return fopen(filename, mode);
0a753a76 1921}
1922
f3986ebb 1923#ifndef USE_SOCKETS_AS_HANDLES
1924#undef fdopen
1925#define fdopen my_fdopen
1926#endif
1927
68dc0745 1928DllExport FILE *
7fac1903 1929win32_fdopen(int handle, const char *mode)
0a753a76 1930{
c5be433b 1931 dTHXo;
51371543 1932 WCHAR wMode[MODE_SIZE];
7fac1903 1933 if (USING_WIDE()) {
0cb96387 1934 A2WHELPER(mode, wMode, sizeof(wMode));
7fac1903 1935 return _wfdopen(handle, wMode);
1936 }
390b85e7 1937 return fdopen(handle, (char *) mode);
0a753a76 1938}
1939
68dc0745 1940DllExport FILE *
7fac1903 1941win32_freopen(const char *path, const char *mode, FILE *stream)
0a753a76 1942{
c5be433b 1943 dTHXo;
51371543 1944 WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH];
68dc0745 1945 if (stricmp(path, "/dev/null")==0)
7fac1903 1946 path = "NUL";
1947
1948 if (USING_WIDE()) {
0cb96387 1949 A2WHELPER(mode, wMode, sizeof(wMode));
1950 A2WHELPER(path, wBuffer, sizeof(wBuffer));
7fac1903 1951 return _wfreopen(wBuffer, wMode, stream);
1952 }
390b85e7 1953 return freopen(path, mode, stream);
0a753a76 1954}
1955
68dc0745 1956DllExport int
1957win32_fclose(FILE *pf)
0a753a76 1958{
f3986ebb 1959 return my_fclose(pf); /* defined in win32sck.c */
0a753a76 1960}
1961
68dc0745 1962DllExport int
1963win32_fputs(const char *s,FILE *pf)
0a753a76 1964{
390b85e7 1965 return fputs(s, pf);
0a753a76 1966}
1967
68dc0745 1968DllExport int
1969win32_fputc(int c,FILE *pf)
0a753a76 1970{
390b85e7 1971 return fputc(c,pf);
0a753a76 1972}
1973
68dc0745 1974DllExport int
1975win32_ungetc(int c,FILE *pf)
0a753a76 1976{
390b85e7 1977 return ungetc(c,pf);
0a753a76 1978}
1979
68dc0745 1980DllExport int
1981win32_getc(FILE *pf)
0a753a76 1982{
390b85e7 1983 return getc(pf);
0a753a76 1984}
1985
68dc0745 1986DllExport int
1987win32_fileno(FILE *pf)
0a753a76 1988{
390b85e7 1989 return fileno(pf);
0a753a76 1990}
1991
68dc0745 1992DllExport void
1993win32_clearerr(FILE *pf)
0a753a76 1994{
390b85e7 1995 clearerr(pf);
68dc0745 1996 return;
0a753a76 1997}
1998
68dc0745 1999DllExport int
2000win32_fflush(FILE *pf)
0a753a76 2001{
390b85e7 2002 return fflush(pf);
0a753a76 2003}
2004
68dc0745 2005DllExport long
2006win32_ftell(FILE *pf)
0a753a76 2007{
390b85e7 2008 return ftell(pf);
0a753a76 2009}
2010
68dc0745 2011DllExport int
2012win32_fseek(FILE *pf,long offset,int origin)
0a753a76 2013{
390b85e7 2014 return fseek(pf, offset, origin);
0a753a76 2015}
2016
68dc0745 2017DllExport int
2018win32_fgetpos(FILE *pf,fpos_t *p)
0a753a76 2019{
390b85e7 2020 return fgetpos(pf, p);
0a753a76 2021}
2022
68dc0745 2023DllExport int
2024win32_fsetpos(FILE *pf,const fpos_t *p)
0a753a76 2025{
390b85e7 2026 return fsetpos(pf, p);
0a753a76 2027}
2028
68dc0745 2029DllExport void
2030win32_rewind(FILE *pf)
0a753a76 2031{
390b85e7 2032 rewind(pf);
68dc0745 2033 return;
0a753a76 2034}
2035
68dc0745 2036DllExport FILE*
2037win32_tmpfile(void)
0a753a76 2038{
390b85e7 2039 return tmpfile();
0a753a76 2040}
2041
68dc0745 2042DllExport void
2043win32_abort(void)
0a753a76 2044{
390b85e7 2045 abort();
68dc0745 2046 return;
0a753a76 2047}
2048
68dc0745 2049DllExport int
22239a37 2050win32_fstat(int fd,struct stat *sbufptr)
0a753a76 2051{
22239a37 2052 return fstat(fd,sbufptr);
0a753a76 2053}
2054
68dc0745 2055DllExport int
2056win32_pipe(int *pfd, unsigned int size, int mode)
0a753a76 2057{
390b85e7 2058 return _pipe(pfd, size, mode);
0a753a76 2059}
2060
50892819 2061/*
2062 * a popen() clone that respects PERL5SHELL
2063 */
2064
68dc0745 2065DllExport FILE*
2066win32_popen(const char *command, const char *mode)
0a753a76 2067{
4b556e6c 2068#ifdef USE_RTL_POPEN
390b85e7 2069 return _popen(command, mode);
50892819 2070#else
2071 int p[2];
2072 int parent, child;
2073 int stdfd, oldfd;
2074 int ourmode;
2075 int childpid;
2076
2077 /* establish which ends read and write */
2078 if (strchr(mode,'w')) {
2079 stdfd = 0; /* stdin */
2080 parent = 1;
2081 child = 0;
2082 }
2083 else if (strchr(mode,'r')) {
2084 stdfd = 1; /* stdout */
2085 parent = 0;
2086 child = 1;
2087 }
2088 else
2089 return NULL;
2090
2091 /* set the correct mode */
2092 if (strchr(mode,'b'))
2093 ourmode = O_BINARY;
2094 else if (strchr(mode,'t'))
2095 ourmode = O_TEXT;
2096 else
2097 ourmode = _fmode & (O_TEXT | O_BINARY);
2098
2099 /* the child doesn't inherit handles */
2100 ourmode |= O_NOINHERIT;
2101
2102 if (win32_pipe( p, 512, ourmode) == -1)
2103 return NULL;
2104
2105 /* save current stdfd */
2106 if ((oldfd = win32_dup(stdfd)) == -1)
2107 goto cleanup;
2108
2109 /* make stdfd go to child end of pipe (implicitly closes stdfd) */
2110 /* stdfd will be inherited by the child */
2111 if (win32_dup2(p[child], stdfd) == -1)
2112 goto cleanup;
2113
2114 /* close the child end in parent */
2115 win32_close(p[child]);
2116
2117 /* start the child */
4f63d024 2118 {
c5be433b 2119 dTHXo;
2120 if ((childpid = do_spawn_nowait((char*)command)) == -1)
4f63d024 2121 goto cleanup;
50892819 2122
4f63d024 2123 /* revert stdfd to whatever it was before */
2124 if (win32_dup2(oldfd, stdfd) == -1)
2125 goto cleanup;
50892819 2126
4f63d024 2127 /* close saved handle */
2128 win32_close(oldfd);
50892819 2129
4f63d024 2130 sv_setiv(*av_fetch(w32_fdpid, p[parent], TRUE), childpid);
d91d68c1 2131
2132 /* set process id so that it can be returned by perl's open() */
2133 PL_forkprocess = childpid;
4f63d024 2134 }
50892819 2135
2136 /* we have an fd, return a file stream */
2137 return (win32_fdopen(p[parent], (char *)mode));
2138
2139cleanup:
2140 /* we don't need to check for errors here */
2141 win32_close(p[0]);
2142 win32_close(p[1]);
2143 if (oldfd != -1) {
2144 win32_dup2(oldfd, stdfd);
2145 win32_close(oldfd);
2146 }
2147 return (NULL);
2148
4b556e6c 2149#endif /* USE_RTL_POPEN */
0a753a76 2150}
2151
50892819 2152/*
2153 * pclose() clone
2154 */
2155
68dc0745 2156DllExport int
2157win32_pclose(FILE *pf)
0a753a76 2158{
4b556e6c 2159#ifdef USE_RTL_POPEN
390b85e7 2160 return _pclose(pf);
50892819 2161#else
c5be433b 2162 dTHXo;
e17cb2a9 2163 int childpid, status;
2164 SV *sv;
2165
4b556e6c 2166 sv = *av_fetch(w32_fdpid, win32_fileno(pf), TRUE);
e17cb2a9 2167 if (SvIOK(sv))
2168 childpid = SvIVX(sv);
2169 else
2170 childpid = 0;
50892819 2171
2172 if (!childpid) {
2173 errno = EBADF;
2174 return -1;
2175 }
2176
2177 win32_fclose(pf);
e17cb2a9 2178 SvIVX(sv) = 0;
2179
0aaad0ff 2180 if (win32_waitpid(childpid, &status, 0) == -1)
2181 return -1;
50892819 2182
0aaad0ff 2183 return status;
50892819 2184
4b556e6c 2185#endif /* USE_RTL_POPEN */
0a753a76 2186}
2187
68dc0745 2188DllExport int
8d9b2e3c 2189win32_rename(const char *oname, const char *newname)
e24c7c18 2190{
7fac1903 2191 WCHAR wOldName[MAX_PATH];
2192 WCHAR wNewName[MAX_PATH];
2193 BOOL bResult;
80252599 2194 /* XXX despite what the documentation says about MoveFileEx(),
2195 * it doesn't work under Windows95!
2196 */
2197 if (IsWinNT()) {
c5be433b 2198 dTHXo;
7fac1903 2199 if (USING_WIDE()) {
0cb96387 2200 A2WHELPER(oname, wOldName, sizeof(wOldName));
2201 A2WHELPER(newname, wNewName, sizeof(wNewName));
7fac1903 2202 bResult = MoveFileExW(wOldName,wNewName,
2203 MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING);
2204 }
2205 else {
2206 bResult = MoveFileExA(oname,newname,
2207 MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING);
2208 }
2209 if (!bResult) {
80252599 2210 DWORD err = GetLastError();
2211 switch (err) {
2212 case ERROR_BAD_NET_NAME:
2213 case ERROR_BAD_NETPATH:
2214 case ERROR_BAD_PATHNAME:
2215 case ERROR_FILE_NOT_FOUND:
2216 case ERROR_FILENAME_EXCED_RANGE:
2217 case ERROR_INVALID_DRIVE:
2218 case ERROR_NO_MORE_FILES:
2219 case ERROR_PATH_NOT_FOUND:
2220 errno = ENOENT;
2221 break;
2222 default:
2223 errno = EACCES;
2224 break;
2225 }
2226 return -1;
2227 }
2228 return 0;
e24c7c18 2229 }
80252599 2230 else {
2231 int retval = 0;
2232 char tmpname[MAX_PATH+1];
2233 char dname[MAX_PATH+1];
2234 char *endname = Nullch;
2235 STRLEN tmplen = 0;
2236 DWORD from_attr, to_attr;
2237
2238 /* if oname doesn't exist, do nothing */
2239 from_attr = GetFileAttributes(oname);
2240 if (from_attr == 0xFFFFFFFF) {
2241 errno = ENOENT;
2242 return -1;
2243 }
2244
2245 /* if newname exists, rename it to a temporary name so that we
2246 * don't delete it in case oname happens to be the same file
2247 * (but perhaps accessed via a different path)
2248 */
2249 to_attr = GetFileAttributes(newname);
2250 if (to_attr != 0xFFFFFFFF) {
2251 /* if newname is a directory, we fail
2252 * XXX could overcome this with yet more convoluted logic */
2253 if (to_attr & FILE_ATTRIBUTE_DIRECTORY) {
2254 errno = EACCES;
2255 return -1;
2256 }
2257 tmplen = strlen(newname);
2258 strcpy(tmpname,newname);
2259 endname = tmpname+tmplen;
2260 for (; endname > tmpname ; --endname) {
2261 if (*endname == '/' || *endname == '\\') {
2262 *endname = '\0';
2263 break;
2264 }
2265 }
2266 if (endname > tmpname)
2267 endname = strcpy(dname,tmpname);
e24c7c18 2268 else
80252599 2269 endname = ".";
2270
2271 /* get a temporary filename in same directory
2272 * XXX is this really the best we can do? */
2273 if (!GetTempFileName((LPCTSTR)endname, "plr", 0, tmpname)) {
2274 errno = ENOENT;
2275 return -1;
2276 }
2277 DeleteFile(tmpname);
2278
2279 retval = rename(newname, tmpname);
2280 if (retval != 0) {
2281 errno = EACCES;
2282 return retval;
e24c7c18 2283 }
2284 }
80252599 2285
2286 /* rename oname to newname */
2287 retval = rename(oname, newname);
2288
2289 /* if we created a temporary file before ... */
2290 if (endname != Nullch) {
2291 /* ...and rename succeeded, delete temporary file/directory */
2292 if (retval == 0)
2293 DeleteFile(tmpname);
2294 /* else restore it to what it was */
2295 else
2296 (void)rename(tmpname, newname);
2297 }
2298 return retval;
e24c7c18 2299 }
e24c7c18 2300}
2301
2302DllExport int
68dc0745 2303win32_setmode(int fd, int mode)
0a753a76 2304{
390b85e7 2305 return setmode(fd, mode);
0a753a76 2306}
2307
96e4d5b1 2308DllExport long
2309win32_lseek(int fd, long offset, int origin)
2310{
390b85e7 2311 return lseek(fd, offset, origin);
96e4d5b1 2312}
2313
2314DllExport long
2315win32_tell(int fd)
2316{
390b85e7 2317 return tell(fd);
96e4d5b1 2318}
2319
68dc0745 2320DllExport int
2321win32_open(const char *path, int flag, ...)
0a753a76 2322{
c5be433b 2323 dTHXo;
68dc0745 2324 va_list ap;
2325 int pmode;
b9010385 2326 WCHAR wBuffer[MAX_PATH];
0a753a76 2327
2328 va_start(ap, flag);
2329 pmode = va_arg(ap, int);
2330 va_end(ap);
2331
68dc0745 2332 if (stricmp(path, "/dev/null")==0)
7fac1903 2333 path = "NUL";
2334
2335 if (USING_WIDE()) {
0cb96387 2336 A2WHELPER(path, wBuffer, sizeof(wBuffer));
7fac1903 2337 return _wopen(wBuffer, flag, pmode);
2338 }
390b85e7 2339 return open(path,flag,pmode);
0a753a76 2340}
2341
68dc0745 2342DllExport int
2343win32_close(int fd)
0a753a76 2344{
390b85e7 2345 return close(fd);
0a753a76 2346}
2347
68dc0745 2348DllExport int
96e4d5b1 2349win32_eof(int fd)
2350{
390b85e7 2351 return eof(fd);
96e4d5b1 2352}
2353
2354DllExport int
68dc0745 2355win32_dup(int fd)
0a753a76 2356{
390b85e7 2357 return dup(fd);
0a753a76 2358}
2359
68dc0745 2360DllExport int
2361win32_dup2(int fd1,int fd2)
0a753a76 2362{
390b85e7 2363 return dup2(fd1,fd2);
0a753a76 2364}
2365
68dc0745 2366DllExport int
3e3baf6d 2367win32_read(int fd, void *buf, unsigned int cnt)
0a753a76 2368{
390b85e7 2369 return read(fd, buf, cnt);
0a753a76 2370}
2371
68dc0745 2372DllExport int
3e3baf6d 2373win32_write(int fd, const void *buf, unsigned int cnt)
0a753a76 2374{
390b85e7 2375 return write(fd, buf, cnt);
0a753a76 2376}
2377
68dc0745 2378DllExport int
5aabfad6 2379win32_mkdir(const char *dir, int mode)
2380{
390b85e7 2381 return mkdir(dir); /* just ignore mode */
5aabfad6 2382}
96e4d5b1 2383
5aabfad6 2384DllExport int
2385win32_rmdir(const char *dir)
2386{
390b85e7 2387 return rmdir(dir);
5aabfad6 2388}
96e4d5b1 2389
5aabfad6 2390DllExport int
2391win32_chdir(const char *dir)
2392{
390b85e7 2393 return chdir(dir);
5aabfad6 2394}
96e4d5b1 2395
0aaad0ff 2396static char *
2397create_command_line(const char* command, const char * const *args)
2398{
c5be433b 2399 dTHXo;
0aaad0ff 2400 int index;
2401 char *cmd, *ptr, *arg;
2402 STRLEN len = strlen(command) + 1;
2403
2404 for (index = 0; (ptr = (char*)args[index]) != NULL; ++index)
2405 len += strlen(ptr) + 1;
2406
2407 New(1310, cmd, len, char);
2408 ptr = cmd;
2409 strcpy(ptr, command);
0aaad0ff 2410
2411 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
0aaad0ff 2412 ptr += strlen(ptr);
18a945d4 2413 *ptr++ = ' ';
2414 strcpy(ptr, arg);
0aaad0ff 2415 }
2416
2417 return cmd;
2418}
2419
2420static char *
2421qualified_path(const char *cmd)
2422{
c5be433b 2423 dTHXo;
0aaad0ff 2424 char *pathstr;
2425 char *fullcmd, *curfullcmd;
2426 STRLEN cmdlen = 0;
2427 int has_slash = 0;
2428
2429 if (!cmd)
2430 return Nullch;
2431 fullcmd = (char*)cmd;
2432 while (*fullcmd) {
2433 if (*fullcmd == '/' || *fullcmd == '\\')
2434 has_slash++;
2435 fullcmd++;
2436 cmdlen++;
2437 }
2438
2439 /* look in PATH */
2440 pathstr = win32_getenv("PATH");
2441 New(0, fullcmd, MAX_PATH+1, char);
2442 curfullcmd = fullcmd;
2443
2444 while (1) {
2445 DWORD res;
2446
2447 /* start by appending the name to the current prefix */
2448 strcpy(curfullcmd, cmd);
2449 curfullcmd += cmdlen;
2450
2451 /* if it doesn't end with '.', or has no extension, try adding
2452 * a trailing .exe first */
2453 if (cmd[cmdlen-1] != '.'
2454 && (cmdlen < 4 || cmd[cmdlen-4] != '.'))
2455 {
2456 strcpy(curfullcmd, ".exe");
2457 res = GetFileAttributes(fullcmd);
2458 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
2459 return fullcmd;
2460 *curfullcmd = '\0';
2461 }
2462
2463 /* that failed, try the bare name */
2464 res = GetFileAttributes(fullcmd);
2465 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
2466 return fullcmd;
2467
2468 /* quit if no other path exists, or if cmd already has path */
2469 if (!pathstr || !*pathstr || has_slash)
2470 break;
2471
2472 /* skip leading semis */
2473 while (*pathstr == ';')
2474 pathstr++;
2475
2476 /* build a new prefix from scratch */
2477 curfullcmd = fullcmd;
2478 while (*pathstr && *pathstr != ';') {
2479 if (*pathstr == '"') { /* foo;"baz;etc";bar */
2480 pathstr++; /* skip initial '"' */
2481 while (*pathstr && *pathstr != '"') {
2482 if (curfullcmd-fullcmd < MAX_PATH-cmdlen-5)
2483 *curfullcmd++ = *pathstr;
2484 pathstr++;
2485 }
2486 if (*pathstr)
2487 pathstr++; /* skip trailing '"' */
2488 }
2489 else {
2490 if (curfullcmd-fullcmd < MAX_PATH-cmdlen-5)
2491 *curfullcmd++ = *pathstr;
2492 pathstr++;
2493 }
2494 }
2495 if (*pathstr)
2496 pathstr++; /* skip trailing semi */
2497 if (curfullcmd > fullcmd /* append a dir separator */
2498 && curfullcmd[-1] != '/' && curfullcmd[-1] != '\\')
2499 {
2500 *curfullcmd++ = '\\';
2501 }
2502 }
2503GIVE_UP:
2504 Safefree(fullcmd);
2505 return Nullch;
2506}
2507
3075ddba 2508/* The following are just place holders.
2509 * Some hosts may provide and environment that the OS is
2510 * not tracking, therefore, these host must provide that
2511 * environment and the current directory to CreateProcess
2512 */
2513
2514void*
2515get_childenv(void)
2516{
2517 return NULL;
2518}
2519
2520void
2dd19d29 2521free_childenv(void* d)
3075ddba 2522{
2523}
2524
2525char*
2526get_childdir(void)
2527{
2528 return NULL;
2529}
2530
2531void
2dd19d29 2532free_childdir(char* d)
3075ddba 2533{
2534}
2535
2536
0aaad0ff 2537/* XXX this needs to be made more compatible with the spawnvp()
2538 * provided by the various RTLs. In particular, searching for
2539 * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented.
2540 * This doesn't significantly affect perl itself, because we
2541 * always invoke things using PERL5SHELL if a direct attempt to
2542 * spawn the executable fails.
2543 *
2544 * XXX splitting and rejoining the commandline between do_aspawn()
2545 * and win32_spawnvp() could also be avoided.
2546 */
2547
5aabfad6 2548DllExport int
3e3baf6d 2549win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
0a753a76 2550{
0aaad0ff 2551#ifdef USE_RTL_SPAWNVP
2552 return spawnvp(mode, cmdname, (char * const *)argv);
2553#else
c5be433b 2554 dTHXo;
0aaad0ff 2555 DWORD ret;
3075ddba 2556 void* env;
2557 char* dir;
0aaad0ff 2558 STARTUPINFO StartupInfo;
2559 PROCESS_INFORMATION ProcessInformation;
2560 DWORD create = 0;
2561
2562 char *cmd = create_command_line(cmdname, strcmp(cmdname, argv[0]) == 0
2563 ? &argv[1] : argv);
2564 char *fullcmd = Nullch;
2565
3075ddba 2566 env = PerlEnv_get_childenv();
2567 dir = PerlEnv_get_childdir();
2568
0aaad0ff 2569 switch(mode) {
2570 case P_NOWAIT: /* asynch + remember result */
2571 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
2572 errno = EAGAIN;
2573 ret = -1;
2574 goto RETVAL;
2575 }
2576 /* FALL THROUGH */
2577 case P_WAIT: /* synchronous execution */
2578 break;
2579 default: /* invalid mode */
2580 errno = EINVAL;
2581 ret = -1;
2582 goto RETVAL;
2583 }
2584 memset(&StartupInfo,0,sizeof(StartupInfo));
2585 StartupInfo.cb = sizeof(StartupInfo);
3ffaa937 2586 StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
2587 StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
2588 StartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
2589 if (StartupInfo.hStdInput != INVALID_HANDLE_VALUE &&
2590 StartupInfo.hStdOutput != INVALID_HANDLE_VALUE &&
2591 StartupInfo.hStdError != INVALID_HANDLE_VALUE)
2592 {
2593 StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
2594 }
2595 else {
2596 create |= CREATE_NEW_CONSOLE;
2597 }
2598
0aaad0ff 2599RETRY:
2600 if (!CreateProcess(cmdname, /* search PATH to find executable */
2601 cmd, /* executable, and its arguments */
2602 NULL, /* process attributes */
2603 NULL, /* thread attributes */
2604 TRUE, /* inherit handles */
2605 create, /* creation flags */
3075ddba 2606 (LPVOID)env, /* inherit environment */
2607 dir, /* inherit cwd */
0aaad0ff 2608 &StartupInfo,
2609 &ProcessInformation))
2610 {
2611 /* initial NULL argument to CreateProcess() does a PATH
2612 * search, but it always first looks in the directory
2613 * where the current process was started, which behavior
2614 * is undesirable for backward compatibility. So we
2615 * jump through our own hoops by picking out the path
2616 * we really want it to use. */
2617 if (!fullcmd) {
2618 fullcmd = qualified_path(cmdname);
2619 if (fullcmd) {
2620 cmdname = fullcmd;
2621 goto RETRY;
2622 }
2623 }
2624 errno = ENOENT;
2625 ret = -1;
2626 goto RETVAL;
2627 }
2d7a9237 2628
0aaad0ff 2629 if (mode == P_NOWAIT) {
2630 /* asynchronous spawn -- store handle, return PID */
2631 w32_child_handles[w32_num_children] = ProcessInformation.hProcess;
2632 ret = w32_child_pids[w32_num_children] = ProcessInformation.dwProcessId;
2633 ++w32_num_children;
2634 }
2635 else {
2636 WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
2637 GetExitCodeProcess(ProcessInformation.hProcess, &ret);
2638 CloseHandle(ProcessInformation.hProcess);
2639 }
e17cb2a9 2640
0aaad0ff 2641 CloseHandle(ProcessInformation.hThread);
3075ddba 2642
0aaad0ff 2643RETVAL:
3075ddba 2644 PerlEnv_free_childenv(env);
2645 PerlEnv_free_childdir(dir);
0aaad0ff 2646 Safefree(cmd);
2647 Safefree(fullcmd);
2648 return (int)ret;
2d7a9237 2649#endif
0a753a76 2650}
2651
6890e559 2652DllExport int
eb62e965 2653win32_execv(const char *cmdname, const char *const *argv)
2654{
2655 return execv(cmdname, (char *const *)argv);
2656}
2657
2658DllExport int
6890e559 2659win32_execvp(const char *cmdname, const char *const *argv)
2660{
390b85e7 2661 return execvp(cmdname, (char *const *)argv);
6890e559 2662}
2663
84902520 2664DllExport void
2665win32_perror(const char *str)
2666{
390b85e7 2667 perror(str);
84902520 2668}
2669
2670DllExport void
2671win32_setbuf(FILE *pf, char *buf)
2672{
390b85e7 2673 setbuf(pf, buf);
84902520 2674}
2675
2676DllExport int
2677win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
2678{
390b85e7 2679 return setvbuf(pf, buf, type, size);
84902520 2680}
2681
2682DllExport int
2683win32_flushall(void)
2684{
390b85e7 2685 return flushall();
84902520 2686}
2687
2688DllExport int
2689win32_fcloseall(void)
2690{
390b85e7 2691 return fcloseall();
84902520 2692}
2693
2694DllExport char*
2695win32_fgets(char *s, int n, FILE *pf)
2696{
390b85e7 2697 return fgets(s, n, pf);
84902520 2698}
2699
2700DllExport char*
2701win32_gets(char *s)
2702{
390b85e7 2703 return gets(s);
84902520 2704}
2705
2706DllExport int
2707win32_fgetc(FILE *pf)
2708{
390b85e7 2709 return fgetc(pf);
84902520 2710}
2711
2712DllExport int
2713win32_putc(int c, FILE *pf)
2714{
390b85e7 2715 return putc(c,pf);
84902520 2716}
2717
2718DllExport int
2719win32_puts(const char *s)
2720{
390b85e7 2721 return puts(s);
84902520 2722}
2723
2724DllExport int
2725win32_getchar(void)
2726{
390b85e7 2727 return getchar();
84902520 2728}
2729
2730DllExport int
2731win32_putchar(int c)
2732{
390b85e7 2733 return putchar(c);
84902520 2734}
2735
bbc8f9de 2736#ifdef MYMALLOC
2737
2738#ifndef USE_PERL_SBRK
2739
2740static char *committed = NULL;
2741static char *base = NULL;
2742static char *reserved = NULL;
2743static char *brk = NULL;
2744static DWORD pagesize = 0;
2745static DWORD allocsize = 0;
2746
2747void *
2748sbrk(int need)
2749{
2750 void *result;
2751 if (!pagesize)
2752 {SYSTEM_INFO info;
2753 GetSystemInfo(&info);
2754 /* Pretend page size is larger so we don't perpetually
2755 * call the OS to commit just one page ...
2756 */
2757 pagesize = info.dwPageSize << 3;
2758 allocsize = info.dwAllocationGranularity;
2759 }
2760 /* This scheme fails eventually if request for contiguous
2761 * block is denied so reserve big blocks - this is only
2762 * address space not memory ...
2763 */
2764 if (brk+need >= reserved)
2765 {
2766 DWORD size = 64*1024*1024;
2767 char *addr;
2768 if (committed && reserved && committed < reserved)
2769 {
2770 /* Commit last of previous chunk cannot span allocations */
161b471a 2771 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
bbc8f9de 2772 if (addr)
2773 committed = reserved;
2774 }
2775 /* Reserve some (more) space
2776 * Note this is a little sneaky, 1st call passes NULL as reserved
2777 * so lets system choose where we start, subsequent calls pass
2778 * the old end address so ask for a contiguous block
2779 */
161b471a 2780 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
bbc8f9de 2781 if (addr)
2782 {
2783 reserved = addr+size;
2784 if (!base)
2785 base = addr;
2786 if (!committed)
2787 committed = base;
2788 if (!brk)
2789 brk = committed;
2790 }
2791 else
2792 {
2793 return (void *) -1;
2794 }
2795 }
2796 result = brk;
2797 brk += need;
2798 if (brk > committed)
2799 {
2800 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
161b471a 2801 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
bbc8f9de 2802 if (addr)
2803 {
2804 committed += size;
2805 }
2806 else
2807 return (void *) -1;
2808 }
2809 return result;
2810}
2811
2812#endif
2813#endif
2814
84902520 2815DllExport void*
2816win32_malloc(size_t size)
2817{
390b85e7 2818 return malloc(size);
84902520 2819}
2820
2821DllExport void*
2822win32_calloc(size_t numitems, size_t size)
2823{
390b85e7 2824 return calloc(numitems,size);
84902520 2825}
2826
2827DllExport void*
2828win32_realloc(void *block, size_t size)
2829{
390b85e7 2830 return realloc(block,size);
84902520 2831}
2832
2833DllExport void
2834win32_free(void *block)
2835{
390b85e7 2836 free(block);
84902520 2837}
2838
bbc8f9de 2839
68dc0745 2840int
65e48ea9 2841win32_open_osfhandle(long handle, int flags)
0a753a76 2842{
390b85e7 2843 return _open_osfhandle(handle, flags);
0a753a76 2844}
2845
68dc0745 2846long
65e48ea9 2847win32_get_osfhandle(int fd)
0a753a76 2848{
390b85e7 2849 return _get_osfhandle(fd);
0a753a76 2850}
7bac28a0 2851
0cb96387 2852DllExport void*
c5be433b 2853win32_dynaload(const char* filename)
0cb96387 2854{
c5be433b 2855 dTHXo;
51371543 2856 HMODULE hModule;
0cb96387 2857 if (USING_WIDE()) {
2858 WCHAR wfilename[MAX_PATH];
2859 A2WHELPER(filename, wfilename, sizeof(wfilename));
2860 hModule = LoadLibraryExW(wfilename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
2861 }
2862 else {
2863 hModule = LoadLibraryExA(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
2864 }
2865 return hModule;
2866}
2867
2868DllExport int
2869win32_add_host(char *nameId, void *data)
2870{
2871 /*
2872 * This must be called before the script is parsed,
2873 * therefore no locking of threads is needed
2874 */
c5be433b 2875 dTHXo;
0cb96387 2876 struct host_link *link;
2877 New(1314, link, 1, struct host_link);
2878 link->host_data = data;
2879 link->nameId = nameId;
2880 link->next = w32_host_link;
2881 w32_host_link = link;
2882 return 1;
2883}
2884
2885DllExport void *
2886win32_get_host_data(char *nameId)
2887{
c5be433b 2888 dTHXo;
0cb96387 2889 struct host_link *link = w32_host_link;
2890 while(link) {
2891 if(strEQ(link->nameId, nameId))
2892 return link->host_data;
2893 link = link->next;
2894 }
2895 return Nullch;
2896}
2897
7bac28a0 2898/*
2899 * Extras.
2900 */
2901
ad2e33dc 2902static
2903XS(w32_GetCwd)
2904{
2905 dXSARGS;
2906 SV *sv = sv_newmortal();
2907 /* Make one call with zero size - return value is required size */
2908 DWORD len = GetCurrentDirectory((DWORD)0,NULL);
2909 SvUPGRADE(sv,SVt_PV);
2910 SvGROW(sv,len);
2911 SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
2912 /*
2913 * If result != 0
2914 * then it worked, set PV valid,
2915 * else leave it 'undef'
2916 */
3467312b 2917 EXTEND(SP,1);
bb897dfc 2918 if (SvCUR(sv)) {
ad2e33dc 2919 SvPOK_on(sv);
bb897dfc 2920 ST(0) = sv;
2921 XSRETURN(1);
2922 }
3467312b 2923 XSRETURN_UNDEF;
ad2e33dc 2924}
2925
2926static
2927XS(w32_SetCwd)
2928{
2929 dXSARGS;
2930 if (items != 1)
4f63d024 2931 Perl_croak(aTHX_ "usage: Win32::SetCurrentDirectory($cwd)");
bb897dfc 2932 if (SetCurrentDirectory(SvPV_nolen(ST(0))))
ad2e33dc 2933 XSRETURN_YES;
2934
2935 XSRETURN_NO;
2936}
2937
2938static
2939XS(w32_GetNextAvailDrive)
2940{
2941 dXSARGS;
2942 char ix = 'C';
2943 char root[] = "_:\\";
3467312b 2944
2945 EXTEND(SP,1);
ad2e33dc 2946 while (ix <= 'Z') {
2947 root[0] = ix++;
2948 if (GetDriveType(root) == 1) {
2949 root[2] = '\0';
2950 XSRETURN_PV(root);
2951 }
2952 }
3467312b 2953 XSRETURN_UNDEF;
ad2e33dc 2954}
2955
2956static
2957XS(w32_GetLastError)
2958{
2959 dXSARGS;
bb897dfc 2960 EXTEND(SP,1);
ad2e33dc 2961 XSRETURN_IV(GetLastError());
2962}
2963
2964static
ca135624 2965XS(w32_SetLastError)
2966{
2967 dXSARGS;
2968 if (items != 1)
4f63d024 2969 Perl_croak(aTHX_ "usage: Win32::SetLastError($error)");
ca135624 2970 SetLastError(SvIV(ST(0)));
bb897dfc 2971 XSRETURN_EMPTY;
ca135624 2972}
2973
2974static
ad2e33dc 2975XS(w32_LoginName)
2976{
2977 dXSARGS;
e34ffe5a 2978 char *name = getlogin_buffer;
2979 DWORD size = sizeof(getlogin_buffer);
3467312b 2980 EXTEND(SP,1);
ad2e33dc 2981 if (GetUserName(name,&size)) {
2982 /* size includes NULL */
79cb57f6 2983 ST(0) = sv_2mortal(newSVpvn(name,size-1));
ad2e33dc 2984 XSRETURN(1);
2985 }
3467312b 2986 XSRETURN_UNDEF;
ad2e33dc 2987}
2988
2989static
2990XS(w32_NodeName)
2991{
2992 dXSARGS;
2993 char name[MAX_COMPUTERNAME_LENGTH+1];
2994 DWORD size = sizeof(name);
3467312b 2995 EXTEND(SP,1);
ad2e33dc 2996 if (GetComputerName(name,&size)) {
2997 /* size does NOT include NULL :-( */
79cb57f6 2998 ST(0) = sv_2mortal(newSVpvn(name,size));
ad2e33dc 2999 XSRETURN(1);
3000 }
3467312b 3001 XSRETURN_UNDEF;
ad2e33dc 3002}
3003
3004
3005static
3006XS(w32_DomainName)
3007{
3008 dXSARGS;
8c9208bc 3009#ifndef HAS_NETWKSTAGETINFO
3010 /* mingw32 (and Win95) don't have NetWksta*(), so do it the old way */
ad2e33dc 3011 char name[256];
3012 DWORD size = sizeof(name);
3467312b 3013 EXTEND(SP,1);
ad2e33dc 3014 if (GetUserName(name,&size)) {
3015 char sid[1024];
3016 DWORD sidlen = sizeof(sid);
3017 char dname[256];
3018 DWORD dnamelen = sizeof(dname);
3019 SID_NAME_USE snu;
db15561c 3020 if (LookupAccountName(NULL, name, (PSID)&sid, &sidlen,
ad2e33dc 3021 dname, &dnamelen, &snu)) {
3022 XSRETURN_PV(dname); /* all that for this */
3023 }
3024 }
e56670dd 3025#else
8c9208bc 3026 /* this way is more reliable, in case user has a local account.
3027 * XXX need dynamic binding of netapi32.dll symbols or this will fail on
3028 * Win95. Probably makes more sense to move it into libwin32. */
9404a519 3029 char dname[256];
3030 DWORD dnamelen = sizeof(dname);
0a2408cf 3031 PWKSTA_INFO_100 pwi;
3467312b 3032 EXTEND(SP,1);
0a2408cf 3033 if (NERR_Success == NetWkstaGetInfo(NULL, 100, (LPBYTE*)&pwi)) {
3034 if (pwi->wki100_langroup && *(pwi->wki100_langroup)) {
3035 WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_langroup,
3036 -1, (LPSTR)dname, dnamelen, NULL, NULL);
3037 }
3038 else {
3039 WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_computername,
3040 -1, (LPSTR)dname, dnamelen, NULL, NULL);
3041 }
3042 NetApiBufferFree(pwi);
9404a519 3043 XSRETURN_PV(dname);
3044 }
e56670dd 3045#endif
3467312b 3046 XSRETURN_UNDEF;
ad2e33dc 3047}
3048
3049static
3050XS(w32_FsType)
3051{
3052 dXSARGS;
3053 char fsname[256];
3054 DWORD flags, filecomplen;
3055 if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
3056 &flags, fsname, sizeof(fsname))) {
bb897dfc 3057 if (GIMME_V == G_ARRAY) {
79cb57f6 3058 XPUSHs(sv_2mortal(newSVpvn(fsname,strlen(fsname))));
ad2e33dc 3059 XPUSHs(sv_2mortal(newSViv(flags)));
3060 XPUSHs(sv_2mortal(newSViv(filecomplen)));
3061 PUTBACK;
3062 return;
3063 }
bb897dfc 3064 EXTEND(SP,1);
ad2e33dc 3065 XSRETURN_PV(fsname);
3066 }
bb897dfc 3067 XSRETURN_EMPTY;
ad2e33dc 3068}
3069
3070static
3071XS(w32_GetOSVersion)
3072{
3073 dXSARGS;
3074 OSVERSIONINFO osver;
3075
3076 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
3077 if (GetVersionEx(&osver)) {
79cb57f6 3078 XPUSHs(newSVpvn(osver.szCSDVersion, strlen(osver.szCSDVersion)));
ad2e33dc 3079 XPUSHs(newSViv(osver.dwMajorVersion));
3080 XPUSHs(newSViv(osver.dwMinorVersion));
3081 XPUSHs(newSViv(osver.dwBuildNumber));
3082 XPUSHs(newSViv(osver.dwPlatformId));
3083 PUTBACK;
3084 return;
3085 }
bb897dfc 3086 XSRETURN_EMPTY;
ad2e33dc 3087}
3088
3089static
3090XS(w32_IsWinNT)
3091{
3092 dXSARGS;
bb897dfc 3093 EXTEND(SP,1);
ad2e33dc 3094 XSRETURN_IV(IsWinNT());
3095}
3096
3097static
3098XS(w32_IsWin95)
3099{
3100 dXSARGS;
bb897dfc 3101 EXTEND(SP,1);
ad2e33dc 3102 XSRETURN_IV(IsWin95());
3103}
3104
3105static
3106XS(w32_FormatMessage)
3107{
3108 dXSARGS;
3109 DWORD source = 0;
3110 char msgbuf[1024];
3111
3112 if (items != 1)
4f63d024 3113 Perl_croak(aTHX_ "usage: Win32::FormatMessage($errno)");
ad2e33dc 3114
3115 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
3116 &source, SvIV(ST(0)), 0,
3117 msgbuf, sizeof(msgbuf)-1, NULL))
3118 XSRETURN_PV(msgbuf);
3119
3467312b 3120 XSRETURN_UNDEF;
ad2e33dc 3121}
3122
3123static
3124XS(w32_Spawn)
3125{
3126 dXSARGS;
3127 char *cmd, *args;
3128 PROCESS_INFORMATION stProcInfo;
3129 STARTUPINFO stStartInfo;
3130 BOOL bSuccess = FALSE;
3131
9404a519 3132 if (items != 3)
4f63d024 3133 Perl_croak(aTHX_ "usage: Win32::Spawn($cmdName, $args, $PID)");
ad2e33dc 3134
bb897dfc 3135 cmd = SvPV_nolen(ST(0));
3136 args = SvPV_nolen(ST(1));
ad2e33dc 3137
3138 memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */
3139 stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */
3140 stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */
3141 stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */
3142
9404a519 3143 if (CreateProcess(
ad2e33dc 3144 cmd, /* Image path */
3145 args, /* Arguments for command line */
3146 NULL, /* Default process security */
3147 NULL, /* Default thread security */
3148 FALSE, /* Must be TRUE to use std handles */
3149 NORMAL_PRIORITY_CLASS, /* No special scheduling */
3150 NULL, /* Inherit our environment block */
3151 NULL, /* Inherit our currrent directory */
3152 &stStartInfo, /* -> Startup info */
3153 &stProcInfo)) /* <- Process info (if OK) */
3154 {
3155 CloseHandle(stProcInfo.hThread);/* library source code does this. */
3156 sv_setiv(ST(2), stProcInfo.dwProcessId);
3157 bSuccess = TRUE;
3158 }
3159 XSRETURN_IV(bSuccess);
3160}
3161
3162static
3163XS(w32_GetTickCount)
3164{
3165 dXSARGS;
fdb068fa 3166 DWORD msec = GetTickCount();
a6c40364 3167 EXTEND(SP,1);
fdb068fa 3168 if ((IV)msec > 0)
3169 XSRETURN_IV(msec);
3170 XSRETURN_NV(msec);
ad2e33dc 3171}
3172
3173static
3174XS(w32_GetShortPathName)
3175{
3176 dXSARGS;
3177 SV *shortpath;
e8bab181 3178 DWORD len;
ad2e33dc 3179
9404a519 3180 if (items != 1)
4f63d024 3181 Perl_croak(aTHX_ "usage: Win32::GetShortPathName($longPathName)");
ad2e33dc 3182
3183 shortpath = sv_mortalcopy(ST(0));
3184 SvUPGRADE(shortpath, SVt_PV);
3185 /* src == target is allowed */
e8bab181 3186 do {
3187 len = GetShortPathName(SvPVX(shortpath),
3188 SvPVX(shortpath),
3189 SvLEN(shortpath));
3190 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
3191 if (len) {
3192 SvCUR_set(shortpath,len);
ad2e33dc 3193 ST(0) = shortpath;
bb897dfc 3194 XSRETURN(1);
e8bab181 3195 }
3467312b 3196 XSRETURN_UNDEF;
ad2e33dc 3197}
3198
ad0751ec 3199static
ca135624 3200XS(w32_GetFullPathName)
3201{
3202 dXSARGS;
3203 SV *filename;
3204 SV *fullpath;
3205 char *filepart;
3206 DWORD len;
3207
3208 if (items != 1)
4f63d024 3209 Perl_croak(aTHX_ "usage: Win32::GetFullPathName($filename)");
ca135624 3210
3211 filename = ST(0);
3212 fullpath = sv_mortalcopy(filename);
3213 SvUPGRADE(fullpath, SVt_PV);
3214 do {
3215 len = GetFullPathName(SvPVX(filename),
3216 SvLEN(fullpath),
3217 SvPVX(fullpath),
3218 &filepart);
3219 } while (len >= SvLEN(fullpath) && sv_grow(fullpath,len+1));
3220 if (len) {
3221 if (GIMME_V == G_ARRAY) {
3222 EXTEND(SP,1);
bb897dfc 3223 XST_mPV(1,filepart);
ca135624 3224 len = filepart - SvPVX(fullpath);
3225 items = 2;
3226 }
3227 SvCUR_set(fullpath,len);
3228 ST(0) = fullpath;
bb897dfc 3229 XSRETURN(items);
ca135624 3230 }
bb897dfc 3231 XSRETURN_EMPTY;
ca135624 3232}
3233
3234static
8ac9c18d 3235XS(w32_GetLongPathName)
3236{
3237 dXSARGS;
3238 SV *path;
3239 char tmpbuf[MAX_PATH+1];
3240 char *pathstr;
3241 STRLEN len;
3242
3243 if (items != 1)
4f63d024 3244 Perl_croak(aTHX_ "usage: Win32::GetLongPathName($pathname)");
8ac9c18d 3245
3246 path = ST(0);
3247 pathstr = SvPV(path,len);
3248 strcpy(tmpbuf, pathstr);
3249 pathstr = win32_longpath(tmpbuf);
3250 if (pathstr) {
3251 ST(0) = sv_2mortal(newSVpvn(pathstr, strlen(pathstr)));
3252 XSRETURN(1);
3253 }
3254 XSRETURN_EMPTY;
3255}
3256
3257static
ad0751ec 3258XS(w32_Sleep)
3259{
3260 dXSARGS;
3261 if (items != 1)
4f63d024 3262 Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)");
ad0751ec 3263 Sleep(SvIV(ST(0)));
3264 XSRETURN_YES;
3265}
3266
7509b657 3267static
3268XS(w32_CopyFile)
3269{
3270 dXSARGS;
3271 if (items != 3)
4f63d024 3272 Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)");
7509b657 3273 if (CopyFile(SvPV_nolen(ST(0)), SvPV_nolen(ST(1)), !SvTRUE(ST(2))))
3274 XSRETURN_YES;
3275 XSRETURN_NO;
3276}
3277
ad2e33dc 3278void
c5be433b 3279Perl_init_os_extras(void)
ad2e33dc 3280{
c5be433b 3281 dTHXo;
ad2e33dc 3282 char *file = __FILE__;
3283 dXSUB_SYS;
3284
4b556e6c 3285 w32_perlshell_tokens = Nullch;
3286 w32_perlshell_items = -1;
3287 w32_fdpid = newAV(); /* XXX needs to be in Perl_win32_init()? */
0aaad0ff 3288 New(1313, w32_children, 1, child_tab);
4b556e6c 3289 w32_num_children = 0;
4b556e6c 3290
ad2e33dc 3291 /* these names are Activeware compatible */
3292 newXS("Win32::GetCwd", w32_GetCwd, file);
3293 newXS("Win32::SetCwd", w32_SetCwd, file);
3294 newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
3295 newXS("Win32::GetLastError", w32_GetLastError, file);
ca135624 3296 newXS("Win32::SetLastError", w32_SetLastError, file);
ad2e33dc 3297 newXS("Win32::LoginName", w32_LoginName, file);
3298 newXS("Win32::NodeName", w32_NodeName, file);
3299 newXS("Win32::DomainName", w32_DomainName, file);
3300 newXS("Win32::FsType", w32_FsType, file);
3301 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
3302 newXS("Win32::IsWinNT", w32_IsWinNT, file);
3303 newXS("Win32::IsWin95", w32_IsWin95, file);
3304 newXS("Win32::FormatMessage", w32_FormatMessage, file);
3305 newXS("Win32::Spawn", w32_Spawn, file);
3306 newXS("Win32::GetTickCount", w32_GetTickCount, file);
3307 newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
ca135624 3308 newXS("Win32::GetFullPathName", w32_GetFullPathName, file);
8ac9c18d 3309 newXS("Win32::GetLongPathName", w32_GetLongPathName, file);
7509b657 3310 newXS("Win32::CopyFile", w32_CopyFile, file);
ad0751ec 3311 newXS("Win32::Sleep", w32_Sleep, file);
ad2e33dc 3312
3313 /* XXX Bloat Alert! The following Activeware preloads really
3314 * ought to be part of Win32::Sys::*, so they're not included
3315 * here.
3316 */
3317 /* LookupAccountName
3318 * LookupAccountSID
3319 * InitiateSystemShutdown
3320 * AbortSystemShutdown
3321 * ExpandEnvrironmentStrings
3322 */
3323}
3324
3325void
3326Perl_win32_init(int *argcp, char ***argvp)
3327{
3328 /* Disable floating point errors, Perl will trap the ones we
3329 * care about. VC++ RTL defaults to switching these off
3330 * already, but the Borland RTL doesn't. Since we don't
3331 * want to be at the vendor's whim on the default, we set
3332 * it explicitly here.
3333 */
a835ef8a 3334#if !defined(_ALPHA_) && !defined(__GNUC__)
ad2e33dc 3335 _control87(MCW_EM, MCW_EM);
3dc9191e 3336#endif
4b556e6c 3337 MALLOC_INIT;
ad2e33dc 3338}
d55594ae 3339
a868473f 3340#ifdef USE_BINMODE_SCRIPTS
3341
3342void
3343win32_strip_return(SV *sv)
3344{
3345 char *s = SvPVX(sv);
3346 char *e = s+SvCUR(sv);
3347 char *d = s;
3348 while (s < e)
3349 {
3350 if (*s == '\r' && s[1] == '\n')
3351 {
3352 *d++ = '\n';
3353 s += 2;
3354 }
3355 else
3356 {
3357 *d++ = *s++;
3358 }
3359 }
3360 SvCUR_set(sv,d-SvPVX(sv));
3361}
3362
3363#endif
0cb96387 3364