This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
remove deprecated PERL_OBJECT cruft, it has long since stopped
[perl5.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
NIS
14#ifdef __GNUC__
15#define Win32_Winsock
16#endif
0a753a76 17#include <windows.h>
f8fb7c90
GS
18#ifndef __MINGW32__ /* GCC/Mingw32-2.95.2 forgot the WINAPI on CommandLineToArgvW() */
19# include <shellapi.h>
20#else
21 LPWSTR* WINAPI CommandLineToArgvW(LPCWSTR lpCommandLine, int * pNumArgs);
22#endif
5db10396
GS
23#include <winnt.h>
24#include <io.h>
0a753a76 25
68dc0745 26/* #include "config.h" */
0a753a76 27
28#define PERLIO_NOT_STDIO 0
29#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
30#define PerlIO FILE
31#endif
32
7a9ec5a3 33#include <sys/stat.h>
0a753a76 34#include "EXTERN.h"
35#include "perl.h"
c69f6586
GS
36
37#define NO_XSLOCKS
c5be433b 38#define PERL_NO_GET_CONTEXT
ad2e33dc 39#include "XSUB.h"
c69f6586
GS
40
41#include "Win32iop.h"
0a753a76 42#include <fcntl.h>
5b0d9cbe
NIS
43#ifndef __GNUC__
44/* assert.h conflicts with #define of assert in perl.h */
0a753a76 45#include <assert.h>
5b0d9cbe 46#endif
0a753a76 47#include <string.h>
48#include <stdarg.h>
ad2e33dc 49#include <float.h>
ad0751ec 50#include <time.h>
3730b96e 51#if defined(_MSC_VER) || defined(__MINGW32__)
ad0751ec
GS
52#include <sys/utime.h>
53#else
54#include <utime.h>
55#endif
5b0d9cbe
NIS
56#ifdef __GNUC__
57/* Mingw32 defaults to globing command line
58 * So we turn it off like this:
59 */
60int _CRT_glob = 0;
61#endif
62
2b260de0 63#if defined(__MINGW32__)
f8fb7c90
GS
64/* Mingw32 is missing some prototypes */
65FILE * _wfopen(LPCWSTR wszFileName, LPCWSTR wszMode);
66FILE * _wfdopen(int nFd, LPCWSTR wszMode);
67FILE * _freopen(LPCWSTR wszFileName, LPCWSTR wszMode, FILE * pOldStream);
68int _flushall();
69int _fcloseall();
2b260de0
GS
70#endif
71
72#if defined(__BORLANDC__)
0b94c7bb
GS
73# define _stat stat
74# define _utimbuf utimbuf
75#endif
76
6890e559
GS
77#define EXECF_EXEC 1
78#define EXECF_SPAWN 2
79#define EXECF_SPAWN_NOWAIT 3
80
32e30700
GS
81#if defined(PERL_IMPLICIT_SYS)
82# undef win32_get_privlib
83# define win32_get_privlib g_win32_get_privlib
84# undef win32_get_sitelib
85# define win32_get_sitelib g_win32_get_sitelib
4ea817c6
GS
86# undef win32_get_vendorlib
87# define win32_get_vendorlib g_win32_get_vendorlib
32e30700
GS
88# undef do_spawn
89# define do_spawn g_do_spawn
90# undef getlogin
91# define getlogin g_getlogin
92#endif
93
ce1da67e 94static void get_shell(void);
dff6d3cd 95static long tokenize(const char *str, char **dest, char ***destv);
c5be433b 96 int do_spawn2(char *cmd, int exectype);
e200fe59 97static BOOL has_shell_metachars(char *ptr);
2d7a9237 98static long filetime_to_clock(PFILETIME ft);
ad0751ec 99static BOOL filetime_from_time(PFILETIME ft, time_t t);
c5be433b 100static char * get_emd_part(SV **leading, char *trailing, ...);
0aaad0ff
GS
101static void remove_dead_process(long deceased);
102static long find_pid(int pid);
103static char * qualified_path(const char *cmd);
4ea817c6
GS
104static char * win32_get_xlib(const char *pl, const char *xlib,
105 const char *libname);
106
7766f137
GS
107#ifdef USE_ITHREADS
108static void remove_dead_pseudo_process(long child);
109static long find_pseudo_pid(int pid);
110#endif
c69f6586 111
7766f137 112START_EXTERN_C
2d7a9237 113HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
8ac9c18d 114char w32_module_name[MAX_PATH+1];
7766f137
GS
115END_EXTERN_C
116
4b556e6c 117static DWORD w32_platform = (DWORD)-1;
50892819 118
7766f137
GS
119#define ONE_K_BUFSIZE 1024
120
3fe9a6f1 121int
ba106d47
GS
122IsWin95(void)
123{
0cb96387 124 return (win32_os_id() == VER_PLATFORM_WIN32_WINDOWS);
3fe9a6f1 125}
126
127int
ba106d47
GS
128IsWinNT(void)
129{
0cb96387 130 return (win32_os_id() == VER_PLATFORM_WIN32_NT);
3fe9a6f1 131}
0a753a76 132
2fa86c13
GS
133EXTERN_C void
134set_w32_module_name(void)
135{
136 char* ptr;
137 GetModuleFileName((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
138 ? GetModuleHandle(NULL)
139 : w32_perldll_handle),
140 w32_module_name, sizeof(w32_module_name));
141
142 /* try to get full path to binary (which may be mangled when perl is
143 * run from a 16-bit app) */
144 /*PerlIO_printf(Perl_debug_log, "Before %s\n", w32_module_name);*/
145 (void)win32_longpath(w32_module_name);
146 /*PerlIO_printf(Perl_debug_log, "After %s\n", w32_module_name);*/
147
148 /* normalize to forward slashes */
149 ptr = w32_module_name;
150 while (*ptr) {
151 if (*ptr == '\\')
152 *ptr = '/';
153 ++ptr;
154 }
155}
156
c5be433b 157/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
51371543 158static char*
c5be433b 159get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
349ad1fe
GS
160{
161 /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
00dc2f4f
GS
162 HKEY handle;
163 DWORD type;
164 const char *subkey = "Software\\Perl";
349ad1fe 165 char *str = Nullch;
00dc2f4f
GS
166 long retval;
167
168 retval = RegOpenKeyEx(hkey, subkey, 0, KEY_READ, &handle);
349ad1fe 169 if (retval == ERROR_SUCCESS) {
51371543
GS
170 DWORD datalen;
171 retval = RegQueryValueEx(handle, valuename, 0, &type, NULL, &datalen);
1c94caf4
GS
172 if (retval == ERROR_SUCCESS
173 && (type == REG_SZ || type == REG_EXPAND_SZ))
174 {
acfe0abc 175 dTHX;
c5be433b
GS
176 if (!*svp)
177 *svp = sv_2mortal(newSVpvn("",0));
178 SvGROW(*svp, datalen);
51371543 179 retval = RegQueryValueEx(handle, valuename, 0, NULL,
c5be433b 180 (PBYTE)SvPVX(*svp), &datalen);
51371543 181 if (retval == ERROR_SUCCESS) {
c5be433b
GS
182 str = SvPVX(*svp);
183 SvCUR_set(*svp,datalen-1);
51371543 184 }
00dc2f4f
GS
185 }
186 RegCloseKey(handle);
187 }
349ad1fe 188 return str;
00dc2f4f
GS
189}
190
c5be433b 191/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
51371543 192static char*
c5be433b 193get_regstr(const char *valuename, SV **svp)
00dc2f4f 194{
c5be433b 195 char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
349ad1fe 196 if (!str)
c5be433b 197 str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
349ad1fe 198 return str;
00dc2f4f
GS
199}
200
c5be433b 201/* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
e5a95ffb 202static char *
c5be433b 203get_emd_part(SV **prev_pathp, char *trailing_path, ...)
00dc2f4f 204{
dc9e4912 205 char base[10];
e5a95ffb 206 va_list ap;
e24c7c18 207 char mod_name[MAX_PATH+1];
00dc2f4f 208 char *ptr;
e5a95ffb
GS
209 char *optr;
210 char *strip;
211 int oldsize, newsize;
273cf8d1 212 STRLEN baselen;
e5a95ffb
GS
213
214 va_start(ap, trailing_path);
215 strip = va_arg(ap, char *);
216
273cf8d1
GS
217 sprintf(base, "%d.%d", (int)PERL_REVISION, (int)PERL_VERSION);
218 baselen = strlen(base);
dc9e4912 219
8ac9c18d 220 if (!*w32_module_name) {
2fa86c13 221 set_w32_module_name();
95140b98 222 }
8ac9c18d 223 strcpy(mod_name, w32_module_name);
95140b98 224 ptr = strrchr(mod_name, '/');
e5a95ffb
GS
225 while (ptr && strip) {
226 /* look for directories to skip back */
227 optr = ptr;
00dc2f4f 228 *ptr = '\0';
95140b98 229 ptr = strrchr(mod_name, '/');
1c39adb2
GS
230 /* avoid stripping component if there is no slash,
231 * or it doesn't match ... */
e5a95ffb 232 if (!ptr || stricmp(ptr+1, strip) != 0) {
273cf8d1 233 /* ... but not if component matches m|5\.$patchlevel.*| */
1c39adb2 234 if (!ptr || !(*strip == '5' && *(ptr+1) == '5'
273cf8d1
GS
235 && strncmp(strip, base, baselen) == 0
236 && strncmp(ptr+1, base, baselen) == 0))
95140b98
GS
237 {
238 *optr = '/';
80252599
GS
239 ptr = optr;
240 }
00dc2f4f 241 }
e5a95ffb 242 strip = va_arg(ap, char *);
00dc2f4f 243 }
e5a95ffb
GS
244 if (!ptr) {
245 ptr = mod_name;
246 *ptr++ = '.';
95140b98 247 *ptr = '/';
00dc2f4f 248 }
e5a95ffb
GS
249 va_end(ap);
250 strcpy(++ptr, trailing_path);
251
dc9e4912 252 /* only add directory if it exists */
349ad1fe 253 if (GetFileAttributes(mod_name) != (DWORD) -1) {
dc9e4912 254 /* directory exists */
acfe0abc 255 dTHX;
c5be433b
GS
256 if (!*prev_pathp)
257 *prev_pathp = sv_2mortal(newSVpvn("",0));
258 sv_catpvn(*prev_pathp, ";", 1);
259 sv_catpv(*prev_pathp, mod_name);
260 return SvPVX(*prev_pathp);
00dc2f4f 261 }
00dc2f4f 262
cf11f4bf 263 return Nullch;
00dc2f4f
GS
264}
265
266char *
4ea817c6 267win32_get_privlib(const char *pl)
00dc2f4f 268{
acfe0abc 269 dTHX;
e5a95ffb
GS
270 char *stdlib = "lib";
271 char buffer[MAX_PATH+1];
51371543 272 SV *sv = Nullsv;
00dc2f4f 273
e5a95ffb
GS
274 /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
275 sprintf(buffer, "%s-%s", stdlib, pl);
c5be433b
GS
276 if (!get_regstr(buffer, &sv))
277 (void)get_regstr(stdlib, &sv);
00dc2f4f 278
e5a95ffb 279 /* $stdlib .= ";$EMD/../../lib" */
c5be433b 280 return get_emd_part(&sv, stdlib, ARCHNAME, "bin", Nullch);
00dc2f4f
GS
281}
282
4ea817c6
GS
283static char *
284win32_get_xlib(const char *pl, const char *xlib, const char *libname)
00dc2f4f 285{
acfe0abc 286 dTHX;
e5a95ffb 287 char regstr[40];
e24c7c18 288 char pathstr[MAX_PATH+1];
e5a95ffb 289 DWORD datalen;
e5a95ffb 290 int len, newsize;
51371543
GS
291 SV *sv1 = Nullsv;
292 SV *sv2 = Nullsv;
00dc2f4f 293
4ea817c6
GS
294 /* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */
295 sprintf(regstr, "%s-%s", xlib, pl);
c5be433b 296 (void)get_regstr(regstr, &sv1);
e5a95ffb 297
4ea817c6
GS
298 /* $xlib .=
299 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */
300 sprintf(pathstr, "%s/%s/lib", libname, pl);
c5be433b 301 (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
00dc2f4f 302
4ea817c6
GS
303 /* $HKCU{$xlib} || $HKLM{$xlib} . ---; */
304 (void)get_regstr(xlib, &sv2);
00dc2f4f 305
4ea817c6
GS
306 /* $xlib .=
307 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */
308 sprintf(pathstr, "%s/lib", libname);
309 (void)get_emd_part(&sv2, pathstr, ARCHNAME, "bin", pl, Nullch);
e5a95ffb 310
51371543
GS
311 if (!sv1 && !sv2)
312 return Nullch;
313 if (!sv1)
314 return SvPVX(sv2);
315 if (!sv2)
316 return SvPVX(sv1);
e5a95ffb 317
349ad1fe
GS
318 sv_catpvn(sv1, ";", 1);
319 sv_catsv(sv1, sv2);
e5a95ffb 320
349ad1fe 321 return SvPVX(sv1);
68dc0745 322}
0a753a76 323
4ea817c6
GS
324char *
325win32_get_sitelib(const char *pl)
326{
327 return win32_get_xlib(pl, "sitelib", "site");
328}
329
330#ifndef PERL_VENDORLIB_NAME
331# define PERL_VENDORLIB_NAME "vendor"
332#endif
333
334char *
335win32_get_vendorlib(const char *pl)
336{
337 return win32_get_xlib(pl, "vendorlib", PERL_VENDORLIB_NAME);
338}
b4793f7f 339
2d7a9237 340static BOOL
e200fe59 341has_shell_metachars(char *ptr)
68dc0745 342{
343 int inquote = 0;
344 char quote = '\0';
345
346 /*
347 * Scan string looking for redirection (< or >) or pipe
e200fe59
JD
348 * characters (|) that are not in a quoted string.
349 * Shell variable interpolation (%VAR%) can also happen inside strings.
68dc0745 350 */
9404a519 351 while (*ptr) {
68dc0745 352 switch(*ptr) {
e200fe59
JD
353 case '%':
354 return TRUE;
68dc0745 355 case '\'':
356 case '\"':
9404a519
GS
357 if (inquote) {
358 if (quote == *ptr) {
68dc0745 359 inquote = 0;
360 quote = '\0';
0a753a76 361 }
68dc0745 362 }
363 else {
364 quote = *ptr;
365 inquote++;
366 }
367 break;
368 case '>':
369 case '<':
370 case '|':
9404a519 371 if (!inquote)
68dc0745 372 return TRUE;
373 default:
374 break;
0a753a76 375 }
68dc0745 376 ++ptr;
377 }
378 return FALSE;
0a753a76 379}
380
32e30700 381#if !defined(PERL_IMPLICIT_SYS)
68dc0745 382/* since the current process environment is being updated in util.c
383 * the library functions will get the correct environment
384 */
385PerlIO *
4f63d024 386Perl_my_popen(pTHX_ char *cmd, char *mode)
0a753a76 387{
388#ifdef FIXCMD
7766f137
GS
389#define fixcmd(x) { \
390 char *pspace = strchr((x),' '); \
391 if (pspace) { \
392 char *p = (x); \
393 while (p < pspace) { \
394 if (*p == '/') \
395 *p = '\\'; \
396 p++; \
397 } \
398 } \
399 }
0a753a76 400#else
401#define fixcmd(x)
402#endif
68dc0745 403 fixcmd(cmd);
45bc9206 404 PERL_FLUSHALL_FOR_CHILD;
0a753a76 405 return win32_popen(cmd, mode);
0a753a76 406}
407
68dc0745 408long
4f63d024 409Perl_my_pclose(pTHX_ PerlIO *fp)
0a753a76 410{
411 return win32_pclose(fp);
412}
c69f6586 413#endif
0a753a76 414
0cb96387
GS
415DllExport unsigned long
416win32_os_id(void)
0a753a76 417{
8b10511d 418 static OSVERSIONINFO osver;
0a753a76 419
2d7a9237 420 if (osver.dwPlatformId != w32_platform) {
8b10511d
GS
421 memset(&osver, 0, sizeof(OSVERSIONINFO));
422 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
423 GetVersionEx(&osver);
2d7a9237 424 w32_platform = osver.dwPlatformId;
8b10511d 425 }
0cb96387 426 return (unsigned long)w32_platform;
0a753a76 427}
428
7766f137
GS
429DllExport int
430win32_getpid(void)
431{
922b1888 432 int pid;
7766f137 433#ifdef USE_ITHREADS
acfe0abc 434 dTHX;
7766f137
GS
435 if (w32_pseudo_id)
436 return -((int)w32_pseudo_id);
437#endif
922b1888
GS
438 pid = _getpid();
439 /* Windows 9x appears to always reports a pid for threads and processes
440 * that has the high bit set. So we treat the lower 31 bits as the
441 * "real" PID for Perl's purposes. */
442 if (IsWin95() && pid < 0)
443 pid = -pid;
444 return pid;
7766f137
GS
445}
446
ce1da67e
GS
447/* Tokenize a string. Words are null-separated, and the list
448 * ends with a doubled null. Any character (except null and
449 * including backslash) may be escaped by preceding it with a
450 * backslash (the backslash will be stripped).
451 * Returns number of words in result buffer.
452 */
453static long
dff6d3cd 454tokenize(const char *str, char **dest, char ***destv)
ce1da67e
GS
455{
456 char *retstart = Nullch;
457 char **retvstart = 0;
458 int items = -1;
459 if (str) {
acfe0abc 460 dTHX;
ce1da67e
GS
461 int slen = strlen(str);
462 register char *ret;
463 register char **retv;
464 New(1307, ret, slen+2, char);
465 New(1308, retv, (slen+3)/2, char*);
466
467 retstart = ret;
468 retvstart = retv;
469 *retv = ret;
470 items = 0;
471 while (*str) {
472 *ret = *str++;
473 if (*ret == '\\' && *str)
474 *ret = *str++;
475 else if (*ret == ' ') {
476 while (*str == ' ')
477 str++;
478 if (ret == retstart)
479 ret--;
480 else {
481 *ret = '\0';
482 ++items;
483 if (*str)
484 *++retv = ret+1;
485 }
486 }
487 else if (!*str)
488 ++items;
489 ret++;
490 }
491 retvstart[items] = Nullch;
492 *ret++ = '\0';
493 *ret = '\0';
494 }
495 *dest = retstart;
496 *destv = retvstart;
497 return items;
498}
499
500static void
2d7a9237 501get_shell(void)
0a753a76 502{
acfe0abc 503 dTHX;
ce1da67e 504 if (!w32_perlshell_tokens) {
174c211a
GS
505 /* we don't use COMSPEC here for two reasons:
506 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
507 * uncontrolled unportability of the ensuing scripts.
508 * 2. PERL5SHELL could be set to a shell that may not be fit for
509 * interactive use (which is what most programs look in COMSPEC
510 * for).
511 */
dff6d3cd
GS
512 const char* defaultshell = (IsWinNT()
513 ? "cmd.exe /x/c" : "command.com /c");
2fb9ab56 514 const char *usershell = PerlEnv_getenv("PERL5SHELL");
ce1da67e
GS
515 w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
516 &w32_perlshell_tokens,
517 &w32_perlshell_vec);
68dc0745 518 }
0a753a76 519}
520
68dc0745 521int
c5be433b 522do_aspawn(void *vreally, void **vmark, void **vsp)
0a753a76 523{
acfe0abc 524 dTHX;
2d7a9237
GS
525 SV *really = (SV*)vreally;
526 SV **mark = (SV**)vmark;
527 SV **sp = (SV**)vsp;
68dc0745 528 char **argv;
2d7a9237 529 char *str;
68dc0745 530 int status;
2d7a9237 531 int flag = P_WAIT;
68dc0745 532 int index = 0;
68dc0745 533
2d7a9237
GS
534 if (sp <= mark)
535 return -1;
68dc0745 536
ce1da67e
GS
537 get_shell();
538 New(1306, argv, (sp - mark) + w32_perlshell_items + 2, char*);
2d7a9237
GS
539
540 if (SvNIOKp(*(mark+1)) && !SvPOKp(*(mark+1))) {
541 ++mark;
542 flag = SvIVx(*mark);
68dc0745 543 }
544
9404a519 545 while (++mark <= sp) {
bb897dfc 546 if (*mark && (str = SvPV_nolen(*mark)))
2d7a9237
GS
547 argv[index++] = str;
548 else
549 argv[index++] = "";
68dc0745 550 }
551 argv[index++] = 0;
552
2d7a9237 553 status = win32_spawnvp(flag,
bb897dfc 554 (const char*)(really ? SvPV_nolen(really) : argv[0]),
2d7a9237
GS
555 (const char* const*)argv);
556
80252599 557 if (status < 0 && (errno == ENOEXEC || errno == ENOENT)) {
2d7a9237 558 /* possible shell-builtin, invoke with shell */
ce1da67e
GS
559 int sh_items;
560 sh_items = w32_perlshell_items;
2d7a9237
GS
561 while (--index >= 0)
562 argv[index+sh_items] = argv[index];
ce1da67e
GS
563 while (--sh_items >= 0)
564 argv[sh_items] = w32_perlshell_vec[sh_items];
2d7a9237
GS
565
566 status = win32_spawnvp(flag,
bb897dfc 567 (const char*)(really ? SvPV_nolen(really) : argv[0]),
2d7a9237
GS
568 (const char* const*)argv);
569 }
68dc0745 570
922b1888
GS
571 if (flag == P_NOWAIT) {
572 if (IsWin95())
573 PL_statusvalue = -1; /* >16bits hint for pp_system() */
574 }
575 else {
50892819 576 if (status < 0) {
0453d815
PM
577 if (ckWARN(WARN_EXEC))
578 Perl_warner(aTHX_ WARN_EXEC, "Can't spawn \"%s\": %s", argv[0], strerror(errno));
50892819
GS
579 status = 255 * 256;
580 }
581 else
582 status *= 256;
b28d0864 583 PL_statusvalue = status;
5aabfad6 584 }
ce1da67e 585 Safefree(argv);
50892819 586 return (status);
68dc0745 587}
588
c69f6586 589int
c5be433b 590do_spawn2(char *cmd, int exectype)
68dc0745 591{
acfe0abc 592 dTHX;
68dc0745 593 char **a;
594 char *s;
595 char **argv;
596 int status = -1;
597 BOOL needToTry = TRUE;
2d7a9237 598 char *cmd2;
68dc0745 599
2d7a9237
GS
600 /* Save an extra exec if possible. See if there are shell
601 * metacharacters in it */
e200fe59 602 if (!has_shell_metachars(cmd)) {
fc36a67e 603 New(1301,argv, strlen(cmd) / 2 + 2, char*);
604 New(1302,cmd2, strlen(cmd) + 1, char);
68dc0745 605 strcpy(cmd2, cmd);
606 a = argv;
607 for (s = cmd2; *s;) {
de030af3 608 while (*s && isSPACE(*s))
68dc0745 609 s++;
610 if (*s)
611 *(a++) = s;
de030af3 612 while (*s && !isSPACE(*s))
68dc0745 613 s++;
9404a519 614 if (*s)
68dc0745 615 *s++ = '\0';
0a753a76 616 }
68dc0745 617 *a = Nullch;
ce1da67e 618 if (argv[0]) {
6890e559
GS
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 }
2d7a9237 632 if (status != -1 || errno == 0)
68dc0745 633 needToTry = FALSE;
0a753a76 634 }
0a753a76 635 Safefree(argv);
68dc0745 636 Safefree(cmd2);
637 }
2d7a9237 638 if (needToTry) {
ce1da67e
GS
639 char **argv;
640 int i = -1;
641 get_shell();
642 New(1306, argv, w32_perlshell_items + 2, char*);
643 while (++i < w32_perlshell_items)
644 argv[i] = w32_perlshell_vec[i];
2d7a9237
GS
645 argv[i++] = cmd;
646 argv[i] = Nullch;
6890e559
GS
647 switch (exectype) {
648 case EXECF_SPAWN:
649 status = win32_spawnvp(P_WAIT, argv[0],
650 (const char* const*)argv);
651 break;
652 case EXECF_SPAWN_NOWAIT:
653 status = win32_spawnvp(P_NOWAIT, argv[0],
654 (const char* const*)argv);
655 break;
656 case EXECF_EXEC:
657 status = win32_execvp(argv[0], (const char* const*)argv);
658 break;
659 }
ce1da67e
GS
660 cmd = argv[0];
661 Safefree(argv);
68dc0745 662 }
922b1888
GS
663 if (exectype == EXECF_SPAWN_NOWAIT) {
664 if (IsWin95())
665 PL_statusvalue = -1; /* >16bits hint for pp_system() */
666 }
667 else {
50892819 668 if (status < 0) {
0453d815
PM
669 if (ckWARN(WARN_EXEC))
670 Perl_warner(aTHX_ WARN_EXEC, "Can't %s \"%s\": %s",
50892819
GS
671 (exectype == EXECF_EXEC ? "exec" : "spawn"),
672 cmd, strerror(errno));
673 status = 255 * 256;
674 }
675 else
676 status *= 256;
b28d0864 677 PL_statusvalue = status;
5aabfad6 678 }
50892819 679 return (status);
0a753a76 680}
681
6890e559 682int
c5be433b 683do_spawn(char *cmd)
6890e559 684{
c5be433b 685 return do_spawn2(cmd, EXECF_SPAWN);
6890e559
GS
686}
687
2d7a9237 688int
c5be433b 689do_spawn_nowait(char *cmd)
2d7a9237 690{
c5be433b 691 return do_spawn2(cmd, EXECF_SPAWN_NOWAIT);
2d7a9237
GS
692}
693
6890e559 694bool
4f63d024 695Perl_do_exec(pTHX_ char *cmd)
6890e559 696{
c5be433b 697 do_spawn2(cmd, EXECF_EXEC);
6890e559
GS
698 return FALSE;
699}
700
68dc0745 701/* The idea here is to read all the directory names into a string table
702 * (separated by nulls) and when one of the other dir functions is called
703 * return the pointer to the current file name.
704 */
c5be433b 705DllExport DIR *
ce2e26e5 706win32_opendir(char *filename)
0a753a76 707{
acfe0abc 708 dTHX;
95136add 709 DIR *dirp;
9404a519
GS
710 long len;
711 long idx;
712 char scanname[MAX_PATH+3];
713 struct stat sbuf;
7fac1903
GS
714 WIN32_FIND_DATAA aFindData;
715 WIN32_FIND_DATAW wFindData;
9404a519 716 HANDLE fh;
7fac1903 717 char buffer[MAX_PATH*2];
82867ecf 718 WCHAR wbuffer[MAX_PATH+1];
95136add 719 char* ptr;
9404a519
GS
720
721 len = strlen(filename);
722 if (len > MAX_PATH)
723 return NULL;
68dc0745 724
725 /* check to see if filename is a directory */
69d3ab13 726 if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
24caa93f 727 return NULL;
68dc0745 728
68dc0745 729 /* Get us a DIR structure */
95136add 730 Newz(1303, dirp, 1, DIR);
68dc0745 731
732 /* Create the search pattern */
733 strcpy(scanname, filename);
23db2e2d
GS
734
735 /* bare drive name means look in cwd for drive */
736 if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') {
737 scanname[len++] = '.';
738 scanname[len++] = '/';
739 }
740 else if (scanname[len-1] != '/' && scanname[len-1] != '\\') {
9404a519 741 scanname[len++] = '/';
23db2e2d 742 }
9404a519
GS
743 scanname[len++] = '*';
744 scanname[len] = '\0';
68dc0745 745
746 /* do the FindFirstFile call */
7fac1903 747 if (USING_WIDE()) {
0cb96387 748 A2WHELPER(scanname, wbuffer, sizeof(wbuffer));
7766f137 749 fh = FindFirstFileW(PerlDir_mapW(wbuffer), &wFindData);
7fac1903
GS
750 }
751 else {
7766f137 752 fh = FindFirstFileA(PerlDir_mapA(scanname), &aFindData);
7fac1903 753 }
95136add 754 dirp->handle = fh;
9404a519 755 if (fh == INVALID_HANDLE_VALUE) {
95136add 756 DWORD err = GetLastError();
21e72512 757 /* FindFirstFile() fails on empty drives! */
95136add
GS
758 switch (err) {
759 case ERROR_FILE_NOT_FOUND:
760 return dirp;
761 case ERROR_NO_MORE_FILES:
762 case ERROR_PATH_NOT_FOUND:
763 errno = ENOENT;
764 break;
765 case ERROR_NOT_ENOUGH_MEMORY:
766 errno = ENOMEM;
767 break;
768 default:
769 errno = EINVAL;
770 break;
771 }
772 Safefree(dirp);
68dc0745 773 return NULL;
774 }
775
776 /* now allocate the first part of the string table for
777 * the filenames that we find.
778 */
7fac1903 779 if (USING_WIDE()) {
0cb96387 780 W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer));
7fac1903
GS
781 ptr = buffer;
782 }
783 else {
784 ptr = aFindData.cFileName;
785 }
786 idx = strlen(ptr)+1;
95136add
GS
787 if (idx < 256)
788 dirp->size = 128;
789 else
790 dirp->size = idx;
791 New(1304, dirp->start, dirp->size, char);
792 strcpy(dirp->start, ptr);
793 dirp->nfiles++;
794 dirp->end = dirp->curr = dirp->start;
795 dirp->end += idx;
796 return dirp;
0a753a76 797}
798
799
68dc0745 800/* Readdir just returns the current string pointer and bumps the
801 * string pointer to the nDllExport entry.
802 */
c5be433b 803DllExport struct direct *
ce2e26e5 804win32_readdir(DIR *dirp)
0a753a76 805{
95136add 806 long len;
0a753a76 807
68dc0745 808 if (dirp->curr) {
809 /* first set up the structure to return */
810 len = strlen(dirp->curr);
0f38926b 811 strcpy(dirp->dirstr.d_name, dirp->curr);
68dc0745 812 dirp->dirstr.d_namlen = len;
0a753a76 813
68dc0745 814 /* Fake an inode */
0f38926b 815 dirp->dirstr.d_ino = dirp->curr - dirp->start;
0a753a76 816
95136add 817 /* Now set up for the next call to readdir */
68dc0745 818 dirp->curr += len + 1;
95136add 819 if (dirp->curr >= dirp->end) {
acfe0abc 820 dTHX;
95136add
GS
821 char* ptr;
822 BOOL res;
823 WIN32_FIND_DATAW wFindData;
824 WIN32_FIND_DATAA aFindData;
825 char buffer[MAX_PATH*2];
826
827 /* finding the next file that matches the wildcard
828 * (which should be all of them in this directory!).
95136add
GS
829 */
830 if (USING_WIDE()) {
831 res = FindNextFileW(dirp->handle, &wFindData);
832 if (res) {
833 W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer));
834 ptr = buffer;
835 }
836 }
837 else {
838 res = FindNextFileA(dirp->handle, &aFindData);
839 if (res)
840 ptr = aFindData.cFileName;
841 }
842 if (res) {
0f38926b
GS
843 long endpos = dirp->end - dirp->start;
844 long newsize = endpos + strlen(ptr) + 1;
95136add 845 /* bump the string table size by enough for the
022735b4 846 * new name and its null terminator */
0f38926b
GS
847 while (newsize > dirp->size) {
848 long curpos = dirp->curr - dirp->start;
95136add
GS
849 dirp->size *= 2;
850 Renew(dirp->start, dirp->size, char);
0f38926b 851 dirp->curr = dirp->start + curpos;
95136add 852 }
0f38926b
GS
853 strcpy(dirp->start + endpos, ptr);
854 dirp->end = dirp->start + newsize;
95136add
GS
855 dirp->nfiles++;
856 }
857 else
858 dirp->curr = NULL;
68dc0745 859 }
68dc0745 860 return &(dirp->dirstr);
861 }
862 else
863 return NULL;
0a753a76 864}
865
68dc0745 866/* Telldir returns the current string pointer position */
c5be433b 867DllExport long
ce2e26e5 868win32_telldir(DIR *dirp)
0a753a76 869{
95136add 870 return (dirp->curr - dirp->start);
0a753a76 871}
872
873
68dc0745 874/* Seekdir moves the string pointer to a previously saved position
95136add 875 * (returned by telldir).
68dc0745 876 */
c5be433b 877DllExport void
ce2e26e5 878win32_seekdir(DIR *dirp, long loc)
0a753a76 879{
95136add 880 dirp->curr = dirp->start + loc;
0a753a76 881}
882
68dc0745 883/* Rewinddir resets the string pointer to the start */
c5be433b 884DllExport void
ce2e26e5 885win32_rewinddir(DIR *dirp)
0a753a76 886{
887 dirp->curr = dirp->start;
888}
889
68dc0745 890/* free the memory allocated by opendir */
c5be433b 891DllExport int
ce2e26e5 892win32_closedir(DIR *dirp)
0a753a76 893{
acfe0abc 894 dTHX;
95136add 895 if (dirp->handle != INVALID_HANDLE_VALUE)
0f38926b 896 FindClose(dirp->handle);
0a753a76 897 Safefree(dirp->start);
898 Safefree(dirp);
68dc0745 899 return 1;
0a753a76 900}
901
902
68dc0745 903/*
904 * various stubs
905 */
0a753a76 906
907
68dc0745 908/* Ownership
909 *
910 * Just pretend that everyone is a superuser. NT will let us know if
911 * we don\'t really have permission to do something.
912 */
0a753a76 913
914#define ROOT_UID ((uid_t)0)
915#define ROOT_GID ((gid_t)0)
916
68dc0745 917uid_t
918getuid(void)
0a753a76 919{
68dc0745 920 return ROOT_UID;
0a753a76 921}
922
68dc0745 923uid_t
924geteuid(void)
0a753a76 925{
68dc0745 926 return ROOT_UID;
0a753a76 927}
928
68dc0745 929gid_t
930getgid(void)
0a753a76 931{
68dc0745 932 return ROOT_GID;
0a753a76 933}
934
68dc0745 935gid_t
936getegid(void)
0a753a76 937{
68dc0745 938 return ROOT_GID;
0a753a76 939}
940
68dc0745 941int
22239a37 942setuid(uid_t auid)
0a753a76 943{
22239a37 944 return (auid == ROOT_UID ? 0 : -1);
0a753a76 945}
946
68dc0745 947int
22239a37 948setgid(gid_t agid)
0a753a76 949{
22239a37 950 return (agid == ROOT_GID ? 0 : -1);
0a753a76 951}
952
e34ffe5a
GS
953char *
954getlogin(void)
955{
acfe0abc 956 dTHX;
3352bfcb
GS
957 char *buf = w32_getlogin_buffer;
958 DWORD size = sizeof(w32_getlogin_buffer);
e34ffe5a
GS
959 if (GetUserName(buf,&size))
960 return buf;
961 return (char*)NULL;
962}
963
b990f8c8
GS
964int
965chown(const char *path, uid_t owner, gid_t group)
966{
967 /* XXX noop */
1c1c7f20 968 return 0;
b990f8c8
GS
969}
970
00b02797
JH
971/*
972 * XXX this needs strengthening (for PerlIO)
973 * -- BKS, 11-11-200
974*/
975int mkstemp(const char *path)
976{
977 dTHX;
978 char buf[MAX_PATH+1];
979 int i = 0, fd = -1;
980
981retry:
982 if (i++ > 10) { /* give up */
983 errno = ENOENT;
984 return -1;
985 }
986 if (!GetTempFileNameA((LPCSTR)path, "plr", 1, buf)) {
987 errno = ENOENT;
988 return -1;
989 }
990 fd = PerlLIO_open3(buf, O_CREAT|O_RDWR|O_EXCL, 0600);
991 if (fd == -1)
992 goto retry;
993 return fd;
994}
995
0aaad0ff
GS
996static long
997find_pid(int pid)
0a753a76 998{
acfe0abc 999 dTHX;
7766f137
GS
1000 long child = w32_num_children;
1001 while (--child >= 0) {
0aaad0ff
GS
1002 if (w32_child_pids[child] == pid)
1003 return child;
1004 }
1005 return -1;
1006}
1007
1008static void
1009remove_dead_process(long child)
1010{
1011 if (child >= 0) {
acfe0abc 1012 dTHX;
0aaad0ff 1013 CloseHandle(w32_child_handles[child]);
c00206c8 1014 Move(&w32_child_handles[child+1], &w32_child_handles[child],
0aaad0ff 1015 (w32_num_children-child-1), HANDLE);
c00206c8 1016 Move(&w32_child_pids[child+1], &w32_child_pids[child],
0aaad0ff
GS
1017 (w32_num_children-child-1), DWORD);
1018 w32_num_children--;
f55ee38a 1019 }
f55ee38a
GS
1020}
1021
7766f137
GS
1022#ifdef USE_ITHREADS
1023static long
1024find_pseudo_pid(int pid)
1025{
acfe0abc 1026 dTHX;
7766f137
GS
1027 long child = w32_num_pseudo_children;
1028 while (--child >= 0) {
1029 if (w32_pseudo_child_pids[child] == pid)
1030 return child;
1031 }
1032 return -1;
1033}
1034
1035static void
1036remove_dead_pseudo_process(long child)
1037{
1038 if (child >= 0) {
acfe0abc 1039 dTHX;
7766f137 1040 CloseHandle(w32_pseudo_child_handles[child]);
c00206c8 1041 Move(&w32_pseudo_child_handles[child+1], &w32_pseudo_child_handles[child],
7766f137 1042 (w32_num_pseudo_children-child-1), HANDLE);
c00206c8 1043 Move(&w32_pseudo_child_pids[child+1], &w32_pseudo_child_pids[child],
7766f137
GS
1044 (w32_num_pseudo_children-child-1), DWORD);
1045 w32_num_pseudo_children--;
1046 }
1047}
1048#endif
1049
f55ee38a
GS
1050DllExport int
1051win32_kill(int pid, int sig)
1052{
acfe0abc 1053 dTHX;
0aaad0ff 1054 HANDLE hProcess;
c66b022d 1055 long child;
7766f137
GS
1056#ifdef USE_ITHREADS
1057 if (pid < 0) {
1058 /* it is a pseudo-forked child */
c66b022d 1059 child = find_pseudo_pid(-pid);
7766f137 1060 if (child >= 0) {
42b8b86c
GS
1061 if (!sig)
1062 return 0;
7766f137
GS
1063 hProcess = w32_pseudo_child_handles[child];
1064 if (TerminateThread(hProcess, sig)) {
1065 remove_dead_pseudo_process(child);
1066 return 0;
1067 }
1068 }
922b1888
GS
1069 else if (IsWin95()) {
1070 pid = -pid;
1071 goto alien_process;
1072 }
68dc0745 1073 }
7766f137
GS
1074 else
1075#endif
1076 {
c66b022d 1077 child = find_pid(pid);
7766f137 1078 if (child >= 0) {
42b8b86c
GS
1079 if (!sig)
1080 return 0;
7766f137
GS
1081 hProcess = w32_child_handles[child];
1082 if (TerminateProcess(hProcess, sig)) {
1083 remove_dead_process(child);
1084 return 0;
1085 }
1086 }
1087 else {
922b1888
GS
1088alien_process:
1089 hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE,
1090 (IsWin95() ? -pid : pid));
42b8b86c
GS
1091 if (hProcess) {
1092 if (!sig)
1093 return 0;
1094 if (TerminateProcess(hProcess, sig)) {
1095 CloseHandle(hProcess);
1096 return 0;
1097 }
7766f137
GS
1098 }
1099 }
1100 }
1101 errno = EINVAL;
1102 return -1;
0a753a76 1103}
fbbbcc48 1104
68dc0745 1105/*
1106 * File system stuff
1107 */
0a753a76 1108
f3986ebb
GS
1109DllExport unsigned int
1110win32_sleep(unsigned int t)
0a753a76 1111{
68dc0745 1112 Sleep(t*1000);
1113 return 0;
0a753a76 1114}
1115
68dc0745 1116DllExport int
426c1a18 1117win32_stat(const char *path, struct stat *sbuf)
0a753a76 1118{
acfe0abc 1119 dTHX;
426c1a18 1120 char buffer[MAX_PATH+1];
68dc0745 1121 int l = strlen(path);
67fbe06e 1122 int res;
82867ecf 1123 WCHAR wbuffer[MAX_PATH+1];
e9ff6d27 1124 WCHAR* pwbuffer;
6b980173
JD
1125 HANDLE handle;
1126 int nlink = 1;
0a753a76 1127
68dc0745 1128 if (l > 1) {
1129 switch(path[l - 1]) {
e1dbac94
GS
1130 /* FindFirstFile() and stat() are buggy with a trailing
1131 * backslash, so change it to a forward slash :-( */
68dc0745 1132 case '\\':
426c1a18
GS
1133 strncpy(buffer, path, l-1);
1134 buffer[l - 1] = '/';
1135 buffer[l] = '\0';
1136 path = buffer;
e1dbac94 1137 break;
23db2e2d 1138 /* FindFirstFile() is buggy with "x:", so add a dot :-( */
e1dbac94
GS
1139 case ':':
1140 if (l == 2 && isALPHA(path[0])) {
426c1a18
GS
1141 buffer[0] = path[0];
1142 buffer[1] = ':';
1143 buffer[2] = '.';
1144 buffer[3] = '\0';
e1dbac94 1145 l = 3;
426c1a18 1146 path = buffer;
e1dbac94
GS
1147 }
1148 break;
68dc0745 1149 }
1150 }
6b980173
JD
1151
1152 /* We *must* open & close the file once; otherwise file attribute changes */
1153 /* might not yet have propagated to "other" hard links of the same file. */
1154 /* This also gives us an opportunity to determine the number of links. */
7fac1903 1155 if (USING_WIDE()) {
0cb96387 1156 A2WHELPER(path, wbuffer, sizeof(wbuffer));
e9ff6d27
GS
1157 pwbuffer = PerlDir_mapW(wbuffer);
1158 handle = CreateFileW(pwbuffer, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
7fac1903
GS
1159 }
1160 else {
e9ff6d27
GS
1161 path = PerlDir_mapA(path);
1162 l = strlen(path);
1163 handle = CreateFileA(path, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
6b980173
JD
1164 }
1165 if (handle != INVALID_HANDLE_VALUE) {
1166 BY_HANDLE_FILE_INFORMATION bhi;
1167 if (GetFileInformationByHandle(handle, &bhi))
1168 nlink = bhi.nNumberOfLinks;
1169 CloseHandle(handle);
7fac1903 1170 }
6b980173 1171
e9ff6d27 1172 /* pwbuffer or path will be mapped correctly above */
7766f137 1173 if (USING_WIDE()) {
e9ff6d27 1174 res = _wstat(pwbuffer, (struct _stat *)sbuf);
7766f137
GS
1175 }
1176 else {
e9ff6d27 1177 res = stat(path, sbuf);
7766f137 1178 }
426c1a18 1179 sbuf->st_nlink = nlink;
6b980173 1180
24caa93f
GS
1181 if (res < 0) {
1182 /* CRT is buggy on sharenames, so make sure it really isn't.
1183 * XXX using GetFileAttributesEx() will enable us to set
426c1a18 1184 * sbuf->st_*time (but note that's not available on the
24caa93f 1185 * Windows of 1995) */
7fac1903
GS
1186 DWORD r;
1187 if (USING_WIDE()) {
e9ff6d27 1188 r = GetFileAttributesW(pwbuffer);
7fac1903
GS
1189 }
1190 else {
e9ff6d27 1191 r = GetFileAttributesA(path);
7fac1903 1192 }
24caa93f 1193 if (r != 0xffffffff && (r & FILE_ATTRIBUTE_DIRECTORY)) {
426c1a18
GS
1194 /* sbuf may still contain old garbage since stat() failed */
1195 Zero(sbuf, 1, struct stat);
1196 sbuf->st_mode = S_IFDIR | S_IREAD;
24caa93f
GS
1197 errno = 0;
1198 if (!(r & FILE_ATTRIBUTE_READONLY))
426c1a18 1199 sbuf->st_mode |= S_IWRITE | S_IEXEC;
24caa93f
GS
1200 return 0;
1201 }
1202 }
24caa93f 1203 else {
e1dbac94
GS
1204 if (l == 3 && isALPHA(path[0]) && path[1] == ':'
1205 && (path[2] == '\\' || path[2] == '/'))
2293b0e9
AB
1206 {
1207 /* The drive can be inaccessible, some _stat()s are buggy */
7fac1903 1208 if (USING_WIDE()
e9ff6d27
GS
1209 ? !GetVolumeInformationW(pwbuffer,NULL,0,NULL,NULL,NULL,NULL,0)
1210 : !GetVolumeInformationA(path,NULL,0,NULL,NULL,NULL,NULL,0)) {
2293b0e9
AB
1211 errno = ENOENT;
1212 return -1;
1213 }
1214 }
1215#ifdef __BORLANDC__
426c1a18
GS
1216 if (S_ISDIR(sbuf->st_mode))
1217 sbuf->st_mode |= S_IWRITE | S_IEXEC;
1218 else if (S_ISREG(sbuf->st_mode)) {
d0650a05 1219 int perms;
67fbe06e
GS
1220 if (l >= 4 && path[l-4] == '.') {
1221 const char *e = path + l - 3;
1222 if (strnicmp(e,"exe",3)
1223 && strnicmp(e,"bat",3)
1224 && strnicmp(e,"com",3)
1225 && (IsWin95() || strnicmp(e,"cmd",3)))
426c1a18 1226 sbuf->st_mode &= ~S_IEXEC;
67fbe06e 1227 else
426c1a18 1228 sbuf->st_mode |= S_IEXEC;
67fbe06e
GS
1229 }
1230 else
426c1a18 1231 sbuf->st_mode &= ~S_IEXEC;
d0650a05
GS
1232 /* Propagate permissions to _group_ and _others_ */
1233 perms = sbuf->st_mode & (S_IREAD|S_IWRITE|S_IEXEC);
1234 sbuf->st_mode |= (perms>>3) | (perms>>6);
67fbe06e 1235 }
67fbe06e 1236#endif
2293b0e9 1237 }
67fbe06e 1238 return res;
0a753a76 1239}
1240
8ac9c18d
GS
1241/* Find the longname of a given path. path is destructively modified.
1242 * It should have space for at least MAX_PATH characters. */
1243DllExport char *
1244win32_longpath(char *path)
1245{
1246 WIN32_FIND_DATA fdata;
1247 HANDLE fhand;
1248 char tmpbuf[MAX_PATH+1];
1249 char *tmpstart = tmpbuf;
1250 char *start = path;
1251 char sep;
1252 if (!path)
1253 return Nullch;
1254
1255 /* drive prefix */
1256 if (isALPHA(path[0]) && path[1] == ':' &&
1257 (path[2] == '/' || path[2] == '\\'))
1258 {
1259 start = path + 2;
1260 *tmpstart++ = path[0];
1261 *tmpstart++ = ':';
1262 }
1263 /* UNC prefix */
1264 else if ((path[0] == '/' || path[0] == '\\') &&
1265 (path[1] == '/' || path[1] == '\\'))
1266 {
1267 start = path + 2;
52fcf7ee
GS
1268 *tmpstart++ = path[0];
1269 *tmpstart++ = path[1];
8ac9c18d
GS
1270 /* copy machine name */
1271 while (*start && *start != '/' && *start != '\\')
1272 *tmpstart++ = *start++;
1273 if (*start) {
52fcf7ee 1274 *tmpstart++ = *start;
8ac9c18d
GS
1275 start++;
1276 /* copy share name */
1277 while (*start && *start != '/' && *start != '\\')
1278 *tmpstart++ = *start++;
1279 }
1280 }
1281 sep = *start++;
1282 if (sep == '/' || sep == '\\')
52fcf7ee 1283 *tmpstart++ = sep;
8ac9c18d
GS
1284 *tmpstart = '\0';
1285 while (sep) {
1286 /* walk up to slash */
1287 while (*start && *start != '/' && *start != '\\')
1288 ++start;
1289
1290 /* discard doubled slashes */
1291 while (*start && (start[1] == '/' || start[1] == '\\'))
1292 ++start;
1293 sep = *start;
1294
1295 /* stop and find full name of component */
1296 *start = '\0';
1297 fhand = FindFirstFile(path,&fdata);
1298 if (fhand != INVALID_HANDLE_VALUE) {
1299 strcpy(tmpstart, fdata.cFileName);
1300 tmpstart += strlen(fdata.cFileName);
1301 if (sep)
52fcf7ee 1302 *tmpstart++ = sep;
8ac9c18d
GS
1303 *tmpstart = '\0';
1304 *start++ = sep;
1305 FindClose(fhand);
1306 }
1307 else {
1308 /* failed a step, just return without side effects */
bf49b057 1309 /*PerlIO_printf(Perl_debug_log, "Failed to find %s\n", path);*/
8ac9c18d
GS
1310 *start = sep;
1311 return Nullch;
1312 }
1313 }
1314 strcpy(path,tmpbuf);
1315 return path;
1316}
1317
0551aaa8
GS
1318DllExport char *
1319win32_getenv(const char *name)
1320{
acfe0abc 1321 dTHX;
82867ecf 1322 WCHAR wBuffer[MAX_PATH+1];
0551aaa8 1323 DWORD needlen;
51371543 1324 SV *curitem = Nullsv;
58a50f62 1325
7fac1903 1326 if (USING_WIDE()) {
0cb96387 1327 A2WHELPER(name, wBuffer, sizeof(wBuffer));
51371543 1328 needlen = GetEnvironmentVariableW(wBuffer, NULL, 0);
7fac1903
GS
1329 }
1330 else
51371543 1331 needlen = GetEnvironmentVariableA(name,NULL,0);
58a50f62 1332 if (needlen != 0) {
51371543 1333 curitem = sv_2mortal(newSVpvn("", 0));
7fac1903 1334 if (USING_WIDE()) {
51371543
GS
1335 SV *acuritem;
1336 do {
1337 SvGROW(curitem, (needlen+1)*sizeof(WCHAR));
1338 needlen = GetEnvironmentVariableW(wBuffer,
1339 (WCHAR*)SvPVX(curitem),
1340 needlen);
1341 } while (needlen >= SvLEN(curitem)/sizeof(WCHAR));
c5be433b 1342 SvCUR_set(curitem, (needlen*sizeof(WCHAR))+1);
51371543
GS
1343 acuritem = sv_2mortal(newSVsv(curitem));
1344 W2AHELPER((WCHAR*)SvPVX(acuritem), SvPVX(curitem), SvCUR(curitem));
7fac1903
GS
1345 }
1346 else {
51371543
GS
1347 do {
1348 SvGROW(curitem, needlen+1);
1349 needlen = GetEnvironmentVariableA(name,SvPVX(curitem),
1350 needlen);
1351 } while (needlen >= SvLEN(curitem));
1352 SvCUR_set(curitem, needlen);
58a50f62 1353 }
0551aaa8 1354 }
c934e9d4 1355 else {
7a5f8e82 1356 /* allow any environment variables that begin with 'PERL'
c934e9d4 1357 to be stored in the registry */
51371543 1358 if (strncmp(name, "PERL", 4) == 0)
c5be433b 1359 (void)get_regstr(name, &curitem);
c69f6586 1360 }
51371543
GS
1361 if (curitem && SvCUR(curitem))
1362 return SvPVX(curitem);
58a50f62 1363
51371543 1364 return Nullch;
0551aaa8
GS
1365}
1366
ac5c734f
GS
1367DllExport int
1368win32_putenv(const char *name)
1369{
acfe0abc 1370 dTHX;
ac5c734f
GS
1371 char* curitem;
1372 char* val;
7fac1903
GS
1373 WCHAR* wCuritem;
1374 WCHAR* wVal;
1375 int length, relval = -1;
51371543 1376
73c4f7a1 1377 if (name) {
7fac1903
GS
1378 if (USING_WIDE()) {
1379 length = strlen(name)+1;
1380 New(1309,wCuritem,length,WCHAR);
c5be433b 1381 A2WHELPER(name, wCuritem, length*sizeof(WCHAR));
7fac1903 1382 wVal = wcschr(wCuritem, '=');
7766f137 1383 if (wVal) {
7fac1903 1384 *wVal++ = '\0';
7766f137 1385 if (SetEnvironmentVariableW(wCuritem, *wVal ? wVal : NULL))
7fac1903
GS
1386 relval = 0;
1387 }
1388 Safefree(wCuritem);
1389 }
1390 else {
1391 New(1309,curitem,strlen(name)+1,char);
1392 strcpy(curitem, name);
1393 val = strchr(curitem, '=');
7766f137 1394 if (val) {
7fac1903
GS
1395 /* The sane way to deal with the environment.
1396 * Has these advantages over putenv() & co.:
1397 * * enables us to store a truly empty value in the
1398 * environment (like in UNIX).
1399 * * we don't have to deal with RTL globals, bugs and leaks.
1400 * * Much faster.
1401 * Why you may want to enable USE_WIN32_RTL_ENV:
1402 * * environ[] and RTL functions will not reflect changes,
1403 * which might be an issue if extensions want to access
1404 * the env. via RTL. This cuts both ways, since RTL will
1405 * not see changes made by extensions that call the Win32
1406 * functions directly, either.
1407 * GSAR 97-06-07
1408 */
1409 *val++ = '\0';
7766f137 1410 if (SetEnvironmentVariableA(curitem, *val ? val : NULL))
7fac1903
GS
1411 relval = 0;
1412 }
1413 Safefree(curitem);
ac5c734f 1414 }
ac5c734f
GS
1415 }
1416 return relval;
1417}
1418
d55594ae 1419static long
2d7a9237 1420filetime_to_clock(PFILETIME ft)
d55594ae 1421{
7766f137
GS
1422 __int64 qw = ft->dwHighDateTime;
1423 qw <<= 32;
1424 qw |= ft->dwLowDateTime;
1425 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
1426 return (long) qw;
d55594ae
GS
1427}
1428
f3986ebb
GS
1429DllExport int
1430win32_times(struct tms *timebuf)
0a753a76 1431{
d55594ae
GS
1432 FILETIME user;
1433 FILETIME kernel;
1434 FILETIME dummy;
1435 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
1436 &kernel,&user)) {
2d7a9237
GS
1437 timebuf->tms_utime = filetime_to_clock(&user);
1438 timebuf->tms_stime = filetime_to_clock(&kernel);
d55594ae
GS
1439 timebuf->tms_cutime = 0;
1440 timebuf->tms_cstime = 0;
1441
1442 } else {
1443 /* That failed - e.g. Win95 fallback to clock() */
1444 clock_t t = clock();
1445 timebuf->tms_utime = t;
1446 timebuf->tms_stime = 0;
1447 timebuf->tms_cutime = 0;
1448 timebuf->tms_cstime = 0;
1449 }
68dc0745 1450 return 0;
0a753a76 1451}
1452
9c51cf4c 1453/* fix utime() so it works on directories in NT */
ad0751ec
GS
1454static BOOL
1455filetime_from_time(PFILETIME pFileTime, time_t Time)
1456{
9c51cf4c 1457 struct tm *pTM = localtime(&Time);
ad0751ec 1458 SYSTEMTIME SystemTime;
9c51cf4c 1459 FILETIME LocalTime;
ad0751ec
GS
1460
1461 if (pTM == NULL)
1462 return FALSE;
1463
1464 SystemTime.wYear = pTM->tm_year + 1900;
1465 SystemTime.wMonth = pTM->tm_mon + 1;
1466 SystemTime.wDay = pTM->tm_mday;
1467 SystemTime.wHour = pTM->tm_hour;
1468 SystemTime.wMinute = pTM->tm_min;
1469 SystemTime.wSecond = pTM->tm_sec;
1470 SystemTime.wMilliseconds = 0;
1471
9c51cf4c
GS
1472 return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
1473 LocalFileTimeToFileTime(&LocalTime, pFileTime);
ad0751ec
GS
1474}
1475
1476DllExport int
7766f137
GS
1477win32_unlink(const char *filename)
1478{
acfe0abc 1479 dTHX;
7766f137
GS
1480 int ret;
1481 DWORD attrs;
1482
1483 if (USING_WIDE()) {
82867ecf 1484 WCHAR wBuffer[MAX_PATH+1];
e9ff6d27 1485 WCHAR* pwBuffer;
7766f137
GS
1486
1487 A2WHELPER(filename, wBuffer, sizeof(wBuffer));
e9ff6d27
GS
1488 pwBuffer = PerlDir_mapW(wBuffer);
1489 attrs = GetFileAttributesW(pwBuffer);
c00206c8
GS
1490 if (attrs == 0xFFFFFFFF)
1491 goto fail;
7766f137 1492 if (attrs & FILE_ATTRIBUTE_READONLY) {
e9ff6d27
GS
1493 (void)SetFileAttributesW(pwBuffer, attrs & ~FILE_ATTRIBUTE_READONLY);
1494 ret = _wunlink(pwBuffer);
7766f137 1495 if (ret == -1)
e9ff6d27 1496 (void)SetFileAttributesW(pwBuffer, attrs);
7766f137
GS
1497 }
1498 else
e9ff6d27 1499 ret = _wunlink(pwBuffer);
7766f137
GS
1500 }
1501 else {
e9ff6d27
GS
1502 filename = PerlDir_mapA(filename);
1503 attrs = GetFileAttributesA(filename);
c00206c8
GS
1504 if (attrs == 0xFFFFFFFF)
1505 goto fail;
7766f137 1506 if (attrs & FILE_ATTRIBUTE_READONLY) {
e9ff6d27
GS
1507 (void)SetFileAttributesA(filename, attrs & ~FILE_ATTRIBUTE_READONLY);
1508 ret = unlink(filename);
7766f137 1509 if (ret == -1)
e9ff6d27 1510 (void)SetFileAttributesA(filename, attrs);
7766f137
GS
1511 }
1512 else
e9ff6d27 1513 ret = unlink(filename);
7766f137
GS
1514 }
1515 return ret;
c00206c8
GS
1516fail:
1517 errno = ENOENT;
1518 return -1;
7766f137
GS
1519}
1520
1521DllExport int
3b405fc5 1522win32_utime(const char *filename, struct utimbuf *times)
ad0751ec 1523{
acfe0abc 1524 dTHX;
ad0751ec
GS
1525 HANDLE handle;
1526 FILETIME ftCreate;
1527 FILETIME ftAccess;
1528 FILETIME ftWrite;
1529 struct utimbuf TimeBuffer;
82867ecf 1530 WCHAR wbuffer[MAX_PATH+1];
e9ff6d27 1531 WCHAR* pwbuffer;
ad0751ec 1532
7fac1903
GS
1533 int rc;
1534 if (USING_WIDE()) {
0cb96387 1535 A2WHELPER(filename, wbuffer, sizeof(wbuffer));
e9ff6d27
GS
1536 pwbuffer = PerlDir_mapW(wbuffer);
1537 rc = _wutime(pwbuffer, (struct _utimbuf*)times);
7fac1903
GS
1538 }
1539 else {
e9ff6d27
GS
1540 filename = PerlDir_mapA(filename);
1541 rc = utime(filename, times);
7fac1903 1542 }
ad0751ec
GS
1543 /* EACCES: path specifies directory or readonly file */
1544 if (rc == 0 || errno != EACCES /* || !IsWinNT() */)
1545 return rc;
1546
1547 if (times == NULL) {
1548 times = &TimeBuffer;
1549 time(&times->actime);
1550 times->modtime = times->actime;
1551 }
1552
1553 /* This will (and should) still fail on readonly files */
7fac1903 1554 if (USING_WIDE()) {
e9ff6d27 1555 handle = CreateFileW(pwbuffer, GENERIC_READ | GENERIC_WRITE,
7fac1903
GS
1556 FILE_SHARE_READ | FILE_SHARE_DELETE, NULL,
1557 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1558 }
1559 else {
e9ff6d27 1560 handle = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE,
7fac1903
GS
1561 FILE_SHARE_READ | FILE_SHARE_DELETE, NULL,
1562 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1563 }
ad0751ec
GS
1564 if (handle == INVALID_HANDLE_VALUE)
1565 return rc;
1566
1567 if (GetFileTime(handle, &ftCreate, &ftAccess, &ftWrite) &&
1568 filetime_from_time(&ftAccess, times->actime) &&
1569 filetime_from_time(&ftWrite, times->modtime) &&
1570 SetFileTime(handle, &ftCreate, &ftAccess, &ftWrite))
1571 {
1572 rc = 0;
1573 }
1574
1575 CloseHandle(handle);
1576 return rc;
1577}
1578
2d7a9237 1579DllExport int
b2af26b1
GS
1580win32_uname(struct utsname *name)
1581{
1582 struct hostent *hep;
1583 STRLEN nodemax = sizeof(name->nodename)-1;
1584 OSVERSIONINFO osver;
1585
1586 memset(&osver, 0, sizeof(OSVERSIONINFO));
1587 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1588 if (GetVersionEx(&osver)) {
1589 /* sysname */
1590 switch (osver.dwPlatformId) {
1591 case VER_PLATFORM_WIN32_WINDOWS:
1592 strcpy(name->sysname, "Windows");
1593 break;
1594 case VER_PLATFORM_WIN32_NT:
1595 strcpy(name->sysname, "Windows NT");
1596 break;
1597 case VER_PLATFORM_WIN32s:
1598 strcpy(name->sysname, "Win32s");
1599 break;
1600 default:
1601 strcpy(name->sysname, "Win32 Unknown");
1602 break;
1603 }
1604
cf6cacac
GS
1605 /* release */
1606 sprintf(name->release, "%d.%d",
b2af26b1
GS
1607 osver.dwMajorVersion, osver.dwMinorVersion);
1608
cf6cacac
GS
1609 /* version */
1610 sprintf(name->version, "Build %d",
b2af26b1
GS
1611 osver.dwPlatformId == VER_PLATFORM_WIN32_NT
1612 ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff));
1613 if (osver.szCSDVersion[0]) {
cf6cacac 1614 char *buf = name->version + strlen(name->version);
b2af26b1
GS
1615 sprintf(buf, " (%s)", osver.szCSDVersion);
1616 }
1617 }
1618 else {
1619 *name->sysname = '\0';
1620 *name->version = '\0';
1621 *name->release = '\0';
1622 }
1623
1624 /* nodename */
1625 hep = win32_gethostbyname("localhost");
1626 if (hep) {
1627 STRLEN len = strlen(hep->h_name);
1628 if (len <= nodemax) {
1629 strcpy(name->nodename, hep->h_name);
1630 }
1631 else {
1632 strncpy(name->nodename, hep->h_name, nodemax);
1633 name->nodename[nodemax] = '\0';
1634 }
1635 }
1636 else {
1637 DWORD sz = nodemax;
1638 if (!GetComputerName(name->nodename, &sz))
1639 *name->nodename = '\0';
1640 }
1641
1642 /* machine (architecture) */
1643 {
1644 SYSTEM_INFO info;
1645 char *arch;
1646 GetSystemInfo(&info);
a6c40364 1647
6f24f39d
JK
1648#if (defined(__BORLANDC__)&&(__BORLANDC__<=0x520)) \
1649 || (defined(__MINGW32__) && !defined(_ANONYMOUS_UNION))
a6c40364
GS
1650 switch (info.u.s.wProcessorArchitecture) {
1651#else
b2af26b1 1652 switch (info.wProcessorArchitecture) {
a6c40364 1653#endif
b2af26b1
GS
1654 case PROCESSOR_ARCHITECTURE_INTEL:
1655 arch = "x86"; break;
1656 case PROCESSOR_ARCHITECTURE_MIPS:
1657 arch = "mips"; break;
1658 case PROCESSOR_ARCHITECTURE_ALPHA:
1659 arch = "alpha"; break;
1660 case PROCESSOR_ARCHITECTURE_PPC:
1661 arch = "ppc"; break;
1662 default:
1663 arch = "unknown"; break;
1664 }
1665 strcpy(name->machine, arch);
1666 }
1667 return 0;
1668}
1669
1670DllExport int
f55ee38a
GS
1671win32_waitpid(int pid, int *status, int flags)
1672{
acfe0abc 1673 dTHX;
922b1888 1674 DWORD timeout = (flags & WNOHANG) ? 0 : INFINITE;
0aaad0ff 1675 int retval = -1;
c66b022d 1676 long child;
7766f137 1677 if (pid == -1) /* XXX threadid == 1 ? */
0aaad0ff 1678 return win32_wait(status);
7766f137
GS
1679#ifdef USE_ITHREADS
1680 else if (pid < 0) {
c66b022d 1681 child = find_pseudo_pid(-pid);
7766f137
GS
1682 if (child >= 0) {
1683 HANDLE hThread = w32_pseudo_child_handles[child];
2f67576d
BC
1684 DWORD waitcode = WaitForSingleObject(hThread, timeout);
1685 if (waitcode == WAIT_TIMEOUT) {
1686 return 0;
1687 }
1688 else if (waitcode != WAIT_FAILED) {
7766f137
GS
1689 if (GetExitCodeThread(hThread, &waitcode)) {
1690 *status = (int)((waitcode & 0xff) << 8);
1691 retval = (int)w32_pseudo_child_pids[child];
1692 remove_dead_pseudo_process(child);
68a29c53 1693 return -retval;
7766f137
GS
1694 }
1695 }
1696 else
1697 errno = ECHILD;
1698 }
922b1888
GS
1699 else if (IsWin95()) {
1700 pid = -pid;
1701 goto alien_process;
1702 }
7766f137
GS
1703 }
1704#endif
f55ee38a 1705 else {
922b1888
GS
1706 HANDLE hProcess;
1707 DWORD waitcode;
c66b022d 1708 child = find_pid(pid);
0aaad0ff 1709 if (child >= 0) {
922b1888
GS
1710 hProcess = w32_child_handles[child];
1711 waitcode = WaitForSingleObject(hProcess, timeout);
a7867d0a
GS
1712 if (waitcode == WAIT_TIMEOUT) {
1713 return 0;
1714 }
1715 else if (waitcode != WAIT_FAILED) {
922b1888
GS
1716 if (GetExitCodeProcess(hProcess, &waitcode)) {
1717 *status = (int)((waitcode & 0xff) << 8);
1718 retval = (int)w32_child_pids[child];
1719 remove_dead_process(child);
1720 return retval;
1721 }
a7867d0a 1722 }
0aaad0ff
GS
1723 else
1724 errno = ECHILD;
1725 }
1726 else {
922b1888
GS
1727alien_process:
1728 hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE,
1729 (IsWin95() ? -pid : pid));
1730 if (hProcess) {
1731 waitcode = WaitForSingleObject(hProcess, timeout);
1732 if (waitcode == WAIT_TIMEOUT) {
1733 return 0;
1734 }
1735 else if (waitcode != WAIT_FAILED) {
1736 if (GetExitCodeProcess(hProcess, &waitcode)) {
1737 *status = (int)((waitcode & 0xff) << 8);
1738 CloseHandle(hProcess);
1739 return pid;
1740 }
1741 }
1742 CloseHandle(hProcess);
1743 }
1744 else
1745 errno = ECHILD;
0aaad0ff 1746 }
f55ee38a 1747 }
0aaad0ff 1748 return retval >= 0 ? pid : retval;
f55ee38a
GS
1749}
1750
1751DllExport int
2d7a9237
GS
1752win32_wait(int *status)
1753{
2d7a9237
GS
1754 /* XXX this wait emulation only knows about processes
1755 * spawned via win32_spawnvp(P_NOWAIT, ...).
1756 */
acfe0abc 1757 dTHX;
2d7a9237
GS
1758 int i, retval;
1759 DWORD exitcode, waitcode;
1760
7766f137
GS
1761#ifdef USE_ITHREADS
1762 if (w32_num_pseudo_children) {
1763 waitcode = WaitForMultipleObjects(w32_num_pseudo_children,
1764 w32_pseudo_child_handles,
1765 FALSE,
1766 INFINITE);
1767 if (waitcode != WAIT_FAILED) {
1768 if (waitcode >= WAIT_ABANDONED_0
1769 && waitcode < WAIT_ABANDONED_0 + w32_num_pseudo_children)
1770 i = waitcode - WAIT_ABANDONED_0;
1771 else
1772 i = waitcode - WAIT_OBJECT_0;
1773 if (GetExitCodeThread(w32_pseudo_child_handles[i], &exitcode)) {
1774 *status = (int)((exitcode & 0xff) << 8);
1775 retval = (int)w32_pseudo_child_pids[i];
1776 remove_dead_pseudo_process(i);
68a29c53 1777 return -retval;
7766f137
GS
1778 }
1779 }
1780 }
1781#endif
1782
2d7a9237
GS
1783 if (!w32_num_children) {
1784 errno = ECHILD;
1785 return -1;
1786 }
1787
1788 /* if a child exists, wait for it to die */
1789 waitcode = WaitForMultipleObjects(w32_num_children,
0aaad0ff 1790 w32_child_handles,
2d7a9237
GS
1791 FALSE,
1792 INFINITE);
1793 if (waitcode != WAIT_FAILED) {
1794 if (waitcode >= WAIT_ABANDONED_0
1795 && waitcode < WAIT_ABANDONED_0 + w32_num_children)
1796 i = waitcode - WAIT_ABANDONED_0;
1797 else
1798 i = waitcode - WAIT_OBJECT_0;
0aaad0ff 1799 if (GetExitCodeProcess(w32_child_handles[i], &exitcode) ) {
2d7a9237
GS
1800 *status = (int)((exitcode & 0xff) << 8);
1801 retval = (int)w32_child_pids[i];
0aaad0ff 1802 remove_dead_process(i);
2d7a9237
GS
1803 return retval;
1804 }
1805 }
1806
1807FAILED:
1808 errno = GetLastError();
1809 return -1;
2d7a9237 1810}
d55594ae 1811
2d7a9237 1812static UINT timerid = 0;
d55594ae
GS
1813
1814static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
1815{
acfe0abc 1816 dTHX;
0cb96387
GS
1817 KillTimer(NULL,timerid);
1818 timerid=0;
b319cd58 1819 CALL_FPTR(PL_sighandlerp)(14);
d55594ae
GS
1820}
1821
f3986ebb
GS
1822DllExport unsigned int
1823win32_alarm(unsigned int sec)
0a753a76 1824{
d55594ae
GS
1825 /*
1826 * the 'obvious' implentation is SetTimer() with a callback
1827 * which does whatever receiving SIGALRM would do
1828 * we cannot use SIGALRM even via raise() as it is not
1829 * one of the supported codes in <signal.h>
1830 *
1831 * Snag is unless something is looking at the message queue
1832 * nothing happens :-(
1833 */
acfe0abc 1834 dTHX;
d55594ae
GS
1835 if (sec)
1836 {
1837 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
1838 if (!timerid)
4f63d024 1839 Perl_croak_nocontext("Cannot set timer");
d55594ae
GS
1840 }
1841 else
1842 {
1843 if (timerid)
1844 {
1845 KillTimer(NULL,timerid);
1846 timerid=0;
1847 }
1848 }
68dc0745 1849 return 0;
0a753a76 1850}
1851
26618a56 1852#ifdef HAVE_DES_FCRYPT
2d77217b 1853extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf);
ff95b63e 1854#endif
26618a56
GS
1855
1856DllExport char *
1857win32_crypt(const char *txt, const char *salt)
1858{
acfe0abc 1859 dTHX;
ff95b63e 1860#ifdef HAVE_DES_FCRYPT
3352bfcb 1861 return des_fcrypt(txt, salt, w32_crypt_buffer);
ff95b63e 1862#else
25dbdbbc 1863 Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia.");
b8957cf1 1864 return Nullch;
ff95b63e 1865#endif
26618a56 1866}
26618a56 1867
9e5f57de 1868#ifdef USE_FIXED_OSFHANDLE
390b85e7
GS
1869
1870#define FOPEN 0x01 /* file handle open */
b181b6fb 1871#define FNOINHERIT 0x10 /* file handle opened O_NOINHERIT */
390b85e7
GS
1872#define FAPPEND 0x20 /* file handle opened O_APPEND */
1873#define FDEV 0x40 /* file handle refers to device */
1874#define FTEXT 0x80 /* file handle is in text mode */
1875
390b85e7
GS
1876/***
1877*int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
1878*
1879*Purpose:
1880* This function allocates a free C Runtime file handle and associates
1881* it with the Win32 HANDLE specified by the first parameter. This is a
9e5f57de
GS
1882* temperary fix for WIN95's brain damage GetFileType() error on socket
1883* we just bypass that call for socket
1884*
1885* This works with MSVC++ 4.0+ or GCC/Mingw32
390b85e7
GS
1886*
1887*Entry:
1888* long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
1889* int flags - flags to associate with C Runtime file handle.
1890*
1891*Exit:
1892* returns index of entry in fh, if successful
1893* return -1, if no free entry is found
1894*
1895*Exceptions:
1896*
1897*******************************************************************************/
1898
9e5f57de
GS
1899/*
1900 * we fake up some parts of the CRT that aren't exported by MSVCRT.dll
1901 * this lets sockets work on Win9X with GCC and should fix the problems
1902 * with perl95.exe
1903 * -- BKS, 1-23-2000
1904*/
1905
9e5f57de
GS
1906/* create an ioinfo entry, kill its handle, and steal the entry */
1907
b181b6fb
GS
1908static int
1909_alloc_osfhnd(void)
9e5f57de
GS
1910{
1911 HANDLE hF = CreateFile("NUL", 0, 0, NULL, OPEN_ALWAYS, 0, NULL);
dd8f4818 1912 int fh = _open_osfhandle((long)hF, 0);
9e5f57de
GS
1913 CloseHandle(hF);
1914 if (fh == -1)
1915 return fh;
1916 EnterCriticalSection(&(_pioinfo(fh)->lock));
1917 return fh;
1918}
1919
390b85e7
GS
1920static int
1921my_open_osfhandle(long osfhandle, int flags)
1922{
1923 int fh;
1924 char fileflags; /* _osfile flags */
1925
1926 /* copy relevant flags from second parameter */
1927 fileflags = FDEV;
1928
9404a519 1929 if (flags & O_APPEND)
390b85e7
GS
1930 fileflags |= FAPPEND;
1931
9404a519 1932 if (flags & O_TEXT)
390b85e7
GS
1933 fileflags |= FTEXT;
1934
b181b6fb
GS
1935 if (flags & O_NOINHERIT)
1936 fileflags |= FNOINHERIT;
1937
390b85e7 1938 /* attempt to allocate a C Runtime file handle */
9404a519 1939 if ((fh = _alloc_osfhnd()) == -1) {
390b85e7
GS
1940 errno = EMFILE; /* too many open files */
1941 _doserrno = 0L; /* not an OS error */
1942 return -1; /* return error to caller */
1943 }
1944
1945 /* the file is open. now, set the info in _osfhnd array */
1946 _set_osfhnd(fh, osfhandle);
1947
1948 fileflags |= FOPEN; /* mark as open */
1949
390b85e7 1950 _osfile(fh) = fileflags; /* set osfile entry */
dd8f4818 1951 LeaveCriticalSection(&_pioinfo(fh)->lock);
390b85e7
GS
1952
1953 return fh; /* return handle */
1954}
1955
f3986ebb 1956#endif /* USE_FIXED_OSFHANDLE */
390b85e7
GS
1957
1958/* simulate flock by locking a range on the file */
1959
1960#define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
1961#define LK_LEN 0xffff0000
1962
f3986ebb
GS
1963DllExport int
1964win32_flock(int fd, int oper)
390b85e7
GS
1965{
1966 OVERLAPPED o;
1967 int i = -1;
1968 HANDLE fh;
1969
f3986ebb 1970 if (!IsWinNT()) {
acfe0abc 1971 dTHX;
4f63d024 1972 Perl_croak_nocontext("flock() unimplemented on this platform");
f3986ebb
GS
1973 return -1;
1974 }
390b85e7
GS
1975 fh = (HANDLE)_get_osfhandle(fd);
1976 memset(&o, 0, sizeof(o));
1977
1978 switch(oper) {
1979 case LOCK_SH: /* shared lock */
1980 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
1981 break;
1982 case LOCK_EX: /* exclusive lock */
1983 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
1984 break;
1985 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
1986 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
1987 break;
1988 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
1989 LK_ERR(LockFileEx(fh,
1990 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1991 0, LK_LEN, 0, &o),i);
1992 break;
1993 case LOCK_UN: /* unlock lock */
1994 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
1995 break;
1996 default: /* unknown */
1997 errno = EINVAL;
1998 break;
1999 }
2000 return i;
2001}
2002
2003#undef LK_ERR
2004#undef LK_LEN
2005
68dc0745 2006/*
2007 * redirected io subsystem for all XS modules
2008 *
2009 */
0a753a76 2010
68dc0745 2011DllExport int *
2012win32_errno(void)
0a753a76 2013{
390b85e7 2014 return (&errno);
0a753a76 2015}
2016
dcb2879a
GS
2017DllExport char ***
2018win32_environ(void)
2019{
390b85e7 2020 return (&(_environ));
dcb2879a
GS
2021}
2022
68dc0745 2023/* the rest are the remapped stdio routines */
2024DllExport FILE *
2025win32_stderr(void)
0a753a76 2026{
390b85e7 2027 return (stderr);
0a753a76 2028}
2029
68dc0745 2030DllExport FILE *
2031win32_stdin(void)
0a753a76 2032{
390b85e7 2033 return (stdin);
0a753a76 2034}
2035
68dc0745 2036DllExport FILE *
2037win32_stdout()
0a753a76 2038{
390b85e7 2039 return (stdout);
0a753a76 2040}
2041
68dc0745 2042DllExport int
2043win32_ferror(FILE *fp)
0a753a76 2044{
390b85e7 2045 return (ferror(fp));
0a753a76 2046}
2047
2048
68dc0745 2049DllExport int
2050win32_feof(FILE *fp)
0a753a76 2051{
390b85e7 2052 return (feof(fp));
0a753a76 2053}
2054
68dc0745 2055/*
2056 * Since the errors returned by the socket error function
2057 * WSAGetLastError() are not known by the library routine strerror
2058 * we have to roll our own.
2059 */
0a753a76 2060
68dc0745 2061DllExport char *
2062win32_strerror(int e)
0a753a76 2063{
6f24f39d 2064#if !defined __BORLANDC__ && !defined __MINGW32__ /* compiler intolerance */
68dc0745 2065 extern int sys_nerr;
3e3baf6d 2066#endif
68dc0745 2067 DWORD source = 0;
0a753a76 2068
9404a519 2069 if (e < 0 || e > sys_nerr) {
acfe0abc 2070 dTHX;
9404a519 2071 if (e < 0)
68dc0745 2072 e = GetLastError();
0a753a76 2073
9404a519 2074 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
3352bfcb
GS
2075 w32_strerror_buffer,
2076 sizeof(w32_strerror_buffer), NULL) == 0)
2077 strcpy(w32_strerror_buffer, "Unknown Error");
0a753a76 2078
3352bfcb 2079 return w32_strerror_buffer;
68dc0745 2080 }
390b85e7 2081 return strerror(e);
0a753a76 2082}
2083
22fae026 2084DllExport void
c5be433b 2085win32_str_os_error(void *sv, DWORD dwErr)
22fae026
TM
2086{
2087 DWORD dwLen;
2088 char *sMsg;
2089 dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
2090 |FORMAT_MESSAGE_IGNORE_INSERTS
2091 |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
2092 dwErr, 0, (char *)&sMsg, 1, NULL);
2ce77adf 2093 /* strip trailing whitespace and period */
22fae026 2094 if (0 < dwLen) {
2ce77adf
GS
2095 do {
2096 --dwLen; /* dwLen doesn't include trailing null */
2097 } while (0 < dwLen && isSPACE(sMsg[dwLen]));
22fae026
TM
2098 if ('.' != sMsg[dwLen])
2099 dwLen++;
2ce77adf 2100 sMsg[dwLen] = '\0';
22fae026
TM
2101 }
2102 if (0 == dwLen) {
c69f6586 2103 sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
db7c17d7
GS
2104 if (sMsg)
2105 dwLen = sprintf(sMsg,
2106 "Unknown error #0x%lX (lookup 0x%lX)",
2107 dwErr, GetLastError());
2108 }
2109 if (sMsg) {
acfe0abc 2110 dTHX;
db7c17d7
GS
2111 sv_setpvn((SV*)sv, sMsg, dwLen);
2112 LocalFree(sMsg);
22fae026 2113 }
22fae026
TM
2114}
2115
68dc0745 2116DllExport int
2117win32_fprintf(FILE *fp, const char *format, ...)
0a753a76 2118{
68dc0745 2119 va_list marker;
2120 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 2121
390b85e7 2122 return (vfprintf(fp, format, marker));
0a753a76 2123}
2124
68dc0745 2125DllExport int
2126win32_printf(const char *format, ...)
0a753a76 2127{
68dc0745 2128 va_list marker;
2129 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 2130
390b85e7 2131 return (vprintf(format, marker));
0a753a76 2132}
2133
68dc0745 2134DllExport int
2135win32_vfprintf(FILE *fp, const char *format, va_list args)
0a753a76 2136{
390b85e7 2137 return (vfprintf(fp, format, args));
0a753a76 2138}
2139
96e4d5b1 2140DllExport int
2141win32_vprintf(const char *format, va_list args)
2142{
390b85e7 2143 return (vprintf(format, args));
96e4d5b1 2144}
2145
68dc0745 2146DllExport size_t
2147win32_fread(void *buf, size_t size, size_t count, FILE *fp)
0a753a76 2148{
390b85e7 2149 return fread(buf, size, count, fp);
0a753a76 2150}
2151
68dc0745 2152DllExport size_t
2153win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
0a753a76 2154{
390b85e7 2155 return fwrite(buf, size, count, fp);
0a753a76 2156}
2157
7fac1903
GS
2158#define MODE_SIZE 10
2159
68dc0745 2160DllExport FILE *
2161win32_fopen(const char *filename, const char *mode)
0a753a76 2162{
acfe0abc 2163 dTHX;
82867ecf 2164 WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH+1];
1c5905c2 2165 FILE *f;
c5be433b
GS
2166
2167 if (!*filename)
2168 return NULL;
2169
68dc0745 2170 if (stricmp(filename, "/dev/null")==0)
7fac1903
GS
2171 filename = "NUL";
2172
2173 if (USING_WIDE()) {
0cb96387
GS
2174 A2WHELPER(mode, wMode, sizeof(wMode));
2175 A2WHELPER(filename, wBuffer, sizeof(wBuffer));
1c5905c2 2176 f = _wfopen(PerlDir_mapW(wBuffer), wMode);
7fac1903 2177 }
1c5905c2
GS
2178 else
2179 f = fopen(PerlDir_mapA(filename), mode);
2180 /* avoid buffering headaches for child processes */
2181 if (f && *mode == 'a')
2182 win32_fseek(f, 0, SEEK_END);
2183 return f;
0a753a76 2184}
2185
f3986ebb
GS
2186#ifndef USE_SOCKETS_AS_HANDLES
2187#undef fdopen
2188#define fdopen my_fdopen
2189#endif
2190
68dc0745 2191DllExport FILE *
7fac1903 2192win32_fdopen(int handle, const char *mode)
0a753a76 2193{
acfe0abc 2194 dTHX;
51371543 2195 WCHAR wMode[MODE_SIZE];
1c5905c2 2196 FILE *f;
7fac1903 2197 if (USING_WIDE()) {
0cb96387 2198 A2WHELPER(mode, wMode, sizeof(wMode));
1c5905c2 2199 f = _wfdopen(handle, wMode);
7fac1903 2200 }
1c5905c2
GS
2201 else
2202 f = fdopen(handle, (char *) mode);
2203 /* avoid buffering headaches for child processes */
2204 if (f && *mode == 'a')
2205 win32_fseek(f, 0, SEEK_END);
2206 return f;
0a753a76 2207}
2208
68dc0745 2209DllExport FILE *
7fac1903 2210win32_freopen(const char *path, const char *mode, FILE *stream)
0a753a76 2211{
acfe0abc 2212 dTHX;
82867ecf 2213 WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH+1];
68dc0745 2214 if (stricmp(path, "/dev/null")==0)
7fac1903
GS
2215 path = "NUL";
2216
2217 if (USING_WIDE()) {
0cb96387
GS
2218 A2WHELPER(mode, wMode, sizeof(wMode));
2219 A2WHELPER(path, wBuffer, sizeof(wBuffer));
7766f137 2220 return _wfreopen(PerlDir_mapW(wBuffer), wMode, stream);
7fac1903 2221 }
7766f137 2222 return freopen(PerlDir_mapA(path), mode, stream);
0a753a76 2223}
2224
68dc0745 2225DllExport int
2226win32_fclose(FILE *pf)
0a753a76 2227{
f3986ebb 2228 return my_fclose(pf); /* defined in win32sck.c */
0a753a76 2229}
2230
68dc0745 2231DllExport int
2232win32_fputs(const char *s,FILE *pf)
0a753a76 2233{
390b85e7 2234 return fputs(s, pf);
0a753a76 2235}
2236
68dc0745 2237DllExport int
2238win32_fputc(int c,FILE *pf)
0a753a76 2239{
390b85e7 2240 return fputc(c,pf);
0a753a76 2241}
2242
68dc0745 2243DllExport int
2244win32_ungetc(int c,FILE *pf)
0a753a76 2245{
390b85e7 2246 return ungetc(c,pf);
0a753a76 2247}
2248
68dc0745 2249DllExport int
2250win32_getc(FILE *pf)
0a753a76 2251{
390b85e7 2252 return getc(pf);
0a753a76 2253}
2254
68dc0745 2255DllExport int
2256win32_fileno(FILE *pf)
0a753a76 2257{
390b85e7 2258 return fileno(pf);
0a753a76 2259}
2260
68dc0745 2261DllExport void
2262win32_clearerr(FILE *pf)
0a753a76 2263{
390b85e7 2264 clearerr(pf);
68dc0745 2265 return;
0a753a76 2266}
2267
68dc0745 2268DllExport int
2269win32_fflush(FILE *pf)
0a753a76 2270{
390b85e7 2271 return fflush(pf);
0a753a76 2272}
2273
68dc0745 2274DllExport long
2275win32_ftell(FILE *pf)
0a753a76 2276{
390b85e7 2277 return ftell(pf);
0a753a76 2278}
2279
68dc0745 2280DllExport int
2281win32_fseek(FILE *pf,long offset,int origin)
0a753a76 2282{
390b85e7 2283 return fseek(pf, offset, origin);
0a753a76 2284}
2285
68dc0745 2286DllExport int
2287win32_fgetpos(FILE *pf,fpos_t *p)
0a753a76 2288{
390b85e7 2289 return fgetpos(pf, p);
0a753a76 2290}
2291
68dc0745 2292DllExport int
2293win32_fsetpos(FILE *pf,const fpos_t *p)
0a753a76 2294{
390b85e7 2295 return fsetpos(pf, p);
0a753a76 2296}
2297
68dc0745 2298DllExport void
2299win32_rewind(FILE *pf)
0a753a76 2300{
390b85e7 2301 rewind(pf);
68dc0745 2302 return;
0a753a76 2303}
2304
68dc0745 2305DllExport FILE*
2306win32_tmpfile(void)
0a753a76 2307{
390b85e7 2308 return tmpfile();
0a753a76 2309}
2310
68dc0745 2311DllExport void
2312win32_abort(void)
0a753a76 2313{
390b85e7 2314 abort();
68dc0745 2315 return;
0a753a76 2316}
2317
68dc0745 2318DllExport int
22239a37 2319win32_fstat(int fd,struct stat *sbufptr)
0a753a76 2320{
2a07f407
VK
2321#ifdef __BORLANDC__
2322 /* A file designated by filehandle is not shown as accessible
2323 * for write operations, probably because it is opened for reading.
2324 * --Vadim Konovalov
2325 */
2326 int rc = fstat(fd,sbufptr);
2327 BY_HANDLE_FILE_INFORMATION bhfi;
2328 if (GetFileInformationByHandle((HANDLE)_get_osfhandle(fd), &bhfi)) {
2329 sbufptr->st_mode &= 0xFE00;
2330 if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
2331 sbufptr->st_mode |= (S_IREAD + (S_IREAD >> 3) + (S_IREAD >> 6));
2332 else
2333 sbufptr->st_mode |= ((S_IREAD|S_IWRITE) + ((S_IREAD|S_IWRITE) >> 3)
2334 + ((S_IREAD|S_IWRITE) >> 6));
2335 }
2336 return rc;
2337#else
ed59ec62 2338 return my_fstat(fd,sbufptr);
2a07f407 2339#endif
0a753a76 2340}
2341
68dc0745 2342DllExport int
2343win32_pipe(int *pfd, unsigned int size, int mode)
0a753a76 2344{
390b85e7 2345 return _pipe(pfd, size, mode);
0a753a76 2346}
2347
8c0134a8
NIS
2348DllExport PerlIO*
2349win32_popenlist(const char *mode, IV narg, SV **args)
2350{
2351 dTHX;
2352 Perl_croak(aTHX_ "List form of pipe open not implemented");
2353 return NULL;
2354}
2355
50892819
GS
2356/*
2357 * a popen() clone that respects PERL5SHELL
00b02797
JH
2358 *
2359 * changed to return PerlIO* rather than FILE * by BKS, 11-11-2000
50892819
GS
2360 */
2361
00b02797 2362DllExport PerlIO*
68dc0745 2363win32_popen(const char *command, const char *mode)
0a753a76 2364{
4b556e6c 2365#ifdef USE_RTL_POPEN
390b85e7 2366 return _popen(command, mode);
50892819
GS
2367#else
2368 int p[2];
2369 int parent, child;
2370 int stdfd, oldfd;
2371 int ourmode;
2372 int childpid;
2373
2374 /* establish which ends read and write */
2375 if (strchr(mode,'w')) {
2376 stdfd = 0; /* stdin */
2377 parent = 1;
2378 child = 0;
2379 }
2380 else if (strchr(mode,'r')) {
2381 stdfd = 1; /* stdout */
2382 parent = 0;
2383 child = 1;
2384 }
2385 else
2386 return NULL;
2387
2388 /* set the correct mode */
2389 if (strchr(mode,'b'))
2390 ourmode = O_BINARY;
2391 else if (strchr(mode,'t'))
2392 ourmode = O_TEXT;
2393 else
2394 ourmode = _fmode & (O_TEXT | O_BINARY);
2395
2396 /* the child doesn't inherit handles */
2397 ourmode |= O_NOINHERIT;
2398
2399 if (win32_pipe( p, 512, ourmode) == -1)
2400 return NULL;
2401
2402 /* save current stdfd */
2403 if ((oldfd = win32_dup(stdfd)) == -1)
2404 goto cleanup;
2405
2406 /* make stdfd go to child end of pipe (implicitly closes stdfd) */
2407 /* stdfd will be inherited by the child */
2408 if (win32_dup2(p[child], stdfd) == -1)
2409 goto cleanup;
2410
2411 /* close the child end in parent */
2412 win32_close(p[child]);
2413
2414 /* start the child */
4f63d024 2415 {
acfe0abc 2416 dTHX;
c5be433b 2417 if ((childpid = do_spawn_nowait((char*)command)) == -1)
4f63d024 2418 goto cleanup;
50892819 2419
4f63d024
GS
2420 /* revert stdfd to whatever it was before */
2421 if (win32_dup2(oldfd, stdfd) == -1)
2422 goto cleanup;
50892819 2423
4f63d024
GS
2424 /* close saved handle */
2425 win32_close(oldfd);
50892819 2426
4755096e 2427 LOCK_FDPID_MUTEX;
4f63d024 2428 sv_setiv(*av_fetch(w32_fdpid, p[parent], TRUE), childpid);
4755096e 2429 UNLOCK_FDPID_MUTEX;
d91d68c1
RS
2430
2431 /* set process id so that it can be returned by perl's open() */
2432 PL_forkprocess = childpid;
4f63d024 2433 }
50892819
GS
2434
2435 /* we have an fd, return a file stream */
00b02797 2436 return (PerlIO_fdopen(p[parent], (char *)mode));
50892819
GS
2437
2438cleanup:
2439 /* we don't need to check for errors here */
2440 win32_close(p[0]);
2441 win32_close(p[1]);
2442 if (oldfd != -1) {
2443 win32_dup2(oldfd, stdfd);
2444 win32_close(oldfd);
2445 }
2446 return (NULL);
2447
4b556e6c 2448#endif /* USE_RTL_POPEN */
0a753a76 2449}
2450
50892819
GS
2451/*
2452 * pclose() clone
2453 */
2454
68dc0745 2455DllExport int
00b02797 2456win32_pclose(PerlIO *pf)
0a753a76 2457{
4b556e6c 2458#ifdef USE_RTL_POPEN
390b85e7 2459 return _pclose(pf);
50892819 2460#else
acfe0abc 2461 dTHX;
e17cb2a9
JD
2462 int childpid, status;
2463 SV *sv;
2464
4755096e 2465 LOCK_FDPID_MUTEX;
00b02797 2466 sv = *av_fetch(w32_fdpid, PerlIO_fileno(pf), TRUE);
4755096e 2467
e17cb2a9
JD
2468 if (SvIOK(sv))
2469 childpid = SvIVX(sv);
2470 else
2471 childpid = 0;
50892819
GS
2472
2473 if (!childpid) {
2474 errno = EBADF;
2475 return -1;
2476 }
2477
00b02797
JH
2478#ifdef USE_PERLIO
2479 PerlIO_close(pf);
2480#else
2481 fclose(pf);
2482#endif
e17cb2a9 2483 SvIVX(sv) = 0;
4755096e 2484 UNLOCK_FDPID_MUTEX;
e17cb2a9 2485
0aaad0ff
GS
2486 if (win32_waitpid(childpid, &status, 0) == -1)
2487 return -1;
50892819 2488
0aaad0ff 2489 return status;
50892819 2490
4b556e6c 2491#endif /* USE_RTL_POPEN */
0a753a76 2492}
6b980173
JD
2493
2494static BOOL WINAPI
2495Nt4CreateHardLinkW(
2496 LPCWSTR lpFileName,
2497 LPCWSTR lpExistingFileName,
2498 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
2499{
2500 HANDLE handle;
2501 WCHAR wFullName[MAX_PATH+1];
2502 LPVOID lpContext = NULL;
2503 WIN32_STREAM_ID StreamId;
2504 DWORD dwSize = (char*)&StreamId.cStreamName - (char*)&StreamId;
2505 DWORD dwWritten;
2506 DWORD dwLen;
2507 BOOL bSuccess;
2508
2509 BOOL (__stdcall *pfnBackupWrite)(HANDLE, LPBYTE, DWORD, LPDWORD,
2510 BOOL, BOOL, LPVOID*) =
2511 (BOOL (__stdcall *)(HANDLE, LPBYTE, DWORD, LPDWORD,
2512 BOOL, BOOL, LPVOID*))
2513 GetProcAddress(GetModuleHandle("kernel32.dll"), "BackupWrite");
2514 if (pfnBackupWrite == NULL)
2515 return 0;
2516
2517 dwLen = GetFullPathNameW(lpFileName, MAX_PATH, wFullName, NULL);
2518 if (dwLen == 0)
2519 return 0;
2520 dwLen = (dwLen+1)*sizeof(WCHAR);
2521
2522 handle = CreateFileW(lpExistingFileName, FILE_WRITE_ATTRIBUTES,
2523 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2524 NULL, OPEN_EXISTING, 0, NULL);
2525 if (handle == INVALID_HANDLE_VALUE)
2526 return 0;
2527
2528 StreamId.dwStreamId = BACKUP_LINK;
2529 StreamId.dwStreamAttributes = 0;
2530 StreamId.dwStreamNameSize = 0;
6f24f39d
JK
2531#if defined(__BORLANDC__) \
2532 ||(defined(__MINGW32__) && !defined(_ANONYMOUS_UNION))
4ce4f76e
GS
2533 StreamId.Size.u.HighPart = 0;
2534 StreamId.Size.u.LowPart = dwLen;
2535#else
6b980173
JD
2536 StreamId.Size.HighPart = 0;
2537 StreamId.Size.LowPart = dwLen;
4ce4f76e 2538#endif
6b980173
JD
2539
2540 bSuccess = pfnBackupWrite(handle, (LPBYTE)&StreamId, dwSize, &dwWritten,
2541 FALSE, FALSE, &lpContext);
2542 if (bSuccess) {
2543 bSuccess = pfnBackupWrite(handle, (LPBYTE)wFullName, dwLen, &dwWritten,
2544 FALSE, FALSE, &lpContext);
2545 pfnBackupWrite(handle, NULL, 0, &dwWritten, TRUE, FALSE, &lpContext);
2546 }
2547
2548 CloseHandle(handle);
2549 return bSuccess;
2550}
2551
2552DllExport int
2553win32_link(const char *oldname, const char *newname)
2554{
acfe0abc 2555 dTHX;
6b980173 2556 BOOL (__stdcall *pfnCreateHardLinkW)(LPCWSTR,LPCWSTR,LPSECURITY_ATTRIBUTES);
82867ecf
GS
2557 WCHAR wOldName[MAX_PATH+1];
2558 WCHAR wNewName[MAX_PATH+1];
6b980173
JD
2559
2560 if (IsWin95())
1be9d9c6 2561 Perl_croak(aTHX_ PL_no_func, "link");
6b980173
JD
2562
2563 pfnCreateHardLinkW =
2564 (BOOL (__stdcall *)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES))
2565 GetProcAddress(GetModuleHandle("kernel32.dll"), "CreateHardLinkW");
2566 if (pfnCreateHardLinkW == NULL)
2567 pfnCreateHardLinkW = Nt4CreateHardLinkW;
2568
2569 if ((A2WHELPER(oldname, wOldName, sizeof(wOldName))) &&
2570 (A2WHELPER(newname, wNewName, sizeof(wNewName))) &&
7766f137
GS
2571 (wcscpy(wOldName, PerlDir_mapW(wOldName)),
2572 pfnCreateHardLinkW(PerlDir_mapW(wNewName), wOldName, NULL)))
6b980173
JD
2573 {
2574 return 0;
2575 }
2576 errno = (GetLastError() == ERROR_FILE_NOT_FOUND) ? ENOENT : EINVAL;
2577 return -1;
2578}
0a753a76 2579
68dc0745 2580DllExport int
8d9b2e3c 2581win32_rename(const char *oname, const char *newname)
e24c7c18 2582{
65cb15a1
GS
2583 WCHAR wOldName[MAX_PATH+1];
2584 WCHAR wNewName[MAX_PATH+1];
2585 char szOldName[MAX_PATH+1];
2586 char szNewName[MAX_PATH+1];
7fac1903 2587 BOOL bResult;
acfe0abc 2588 dTHX;
65cb15a1 2589
80252599
GS
2590 /* XXX despite what the documentation says about MoveFileEx(),
2591 * it doesn't work under Windows95!
2592 */
2593 if (IsWinNT()) {
65cb15a1 2594 DWORD dwFlags = MOVEFILE_COPY_ALLOWED;
7fac1903 2595 if (USING_WIDE()) {
0cb96387
GS
2596 A2WHELPER(oname, wOldName, sizeof(wOldName));
2597 A2WHELPER(newname, wNewName, sizeof(wNewName));
65cb15a1
GS
2598 if (wcsicmp(wNewName, wOldName))
2599 dwFlags |= MOVEFILE_REPLACE_EXISTING;
7766f137 2600 wcscpy(wOldName, PerlDir_mapW(wOldName));
65cb15a1 2601 bResult = MoveFileExW(wOldName,PerlDir_mapW(wNewName), dwFlags);
7fac1903
GS
2602 }
2603 else {
65cb15a1
GS
2604 if (stricmp(newname, oname))
2605 dwFlags |= MOVEFILE_REPLACE_EXISTING;
2606 strcpy(szOldName, PerlDir_mapA(oname));
2607 bResult = MoveFileExA(szOldName,PerlDir_mapA(newname), dwFlags);
7fac1903
GS
2608 }
2609 if (!bResult) {
80252599
GS
2610 DWORD err = GetLastError();
2611 switch (err) {
2612 case ERROR_BAD_NET_NAME:
2613 case ERROR_BAD_NETPATH:
2614 case ERROR_BAD_PATHNAME:
2615 case ERROR_FILE_NOT_FOUND:
2616 case ERROR_FILENAME_EXCED_RANGE:
2617 case ERROR_INVALID_DRIVE:
2618 case ERROR_NO_MORE_FILES:
2619 case ERROR_PATH_NOT_FOUND:
2620 errno = ENOENT;
2621 break;
2622 default:
2623 errno = EACCES;
2624 break;
2625 }
2626 return -1;
2627 }
2628 return 0;
e24c7c18 2629 }
80252599
GS
2630 else {
2631 int retval = 0;
65cb15a1 2632 char szTmpName[MAX_PATH+1];
80252599
GS
2633 char dname[MAX_PATH+1];
2634 char *endname = Nullch;
2635 STRLEN tmplen = 0;
2636 DWORD from_attr, to_attr;
2637
65cb15a1
GS
2638 strcpy(szOldName, PerlDir_mapA(oname));
2639 strcpy(szNewName, PerlDir_mapA(newname));
2640
80252599 2641 /* if oname doesn't exist, do nothing */
65cb15a1 2642 from_attr = GetFileAttributes(szOldName);
80252599
GS
2643 if (from_attr == 0xFFFFFFFF) {
2644 errno = ENOENT;
2645 return -1;
2646 }
2647
2648 /* if newname exists, rename it to a temporary name so that we
2649 * don't delete it in case oname happens to be the same file
2650 * (but perhaps accessed via a different path)
2651 */
65cb15a1 2652 to_attr = GetFileAttributes(szNewName);
80252599
GS
2653 if (to_attr != 0xFFFFFFFF) {
2654 /* if newname is a directory, we fail
2655 * XXX could overcome this with yet more convoluted logic */
2656 if (to_attr & FILE_ATTRIBUTE_DIRECTORY) {
2657 errno = EACCES;
2658 return -1;
2659 }
65cb15a1
GS
2660 tmplen = strlen(szNewName);
2661 strcpy(szTmpName,szNewName);
2662 endname = szTmpName+tmplen;
2663 for (; endname > szTmpName ; --endname) {
80252599
GS
2664 if (*endname == '/' || *endname == '\\') {
2665 *endname = '\0';
2666 break;
2667 }
2668 }
65cb15a1
GS
2669 if (endname > szTmpName)
2670 endname = strcpy(dname,szTmpName);
e24c7c18 2671 else
80252599
GS
2672 endname = ".";
2673
2674 /* get a temporary filename in same directory
2675 * XXX is this really the best we can do? */
65cb15a1 2676 if (!GetTempFileName((LPCTSTR)endname, "plr", 0, szTmpName)) {
80252599
GS
2677 errno = ENOENT;
2678 return -1;
2679 }
65cb15a1 2680 DeleteFile(szTmpName);
80252599 2681
65cb15a1 2682 retval = rename(szNewName, szTmpName);
80252599
GS
2683 if (retval != 0) {
2684 errno = EACCES;
2685 return retval;
e24c7c18
GS
2686 }
2687 }
80252599
GS
2688
2689 /* rename oname to newname */
65cb15a1 2690 retval = rename(szOldName, szNewName);
80252599
GS
2691
2692 /* if we created a temporary file before ... */
2693 if (endname != Nullch) {
2694 /* ...and rename succeeded, delete temporary file/directory */
2695 if (retval == 0)
65cb15a1 2696 DeleteFile(szTmpName);
80252599
GS
2697 /* else restore it to what it was */
2698 else
65cb15a1 2699 (void)rename(szTmpName, szNewName);
80252599
GS
2700 }
2701 return retval;
e24c7c18 2702 }
e24c7c18
GS
2703}
2704
2705DllExport int
68dc0745 2706win32_setmode(int fd, int mode)
0a753a76 2707{
390b85e7 2708 return setmode(fd, mode);
0a753a76 2709}
2710
96e4d5b1 2711DllExport long
2712win32_lseek(int fd, long offset, int origin)
2713{
390b85e7 2714 return lseek(fd, offset, origin);
96e4d5b1 2715}
2716
2717DllExport long
2718win32_tell(int fd)
2719{
390b85e7 2720 return tell(fd);
96e4d5b1 2721}
2722
68dc0745 2723DllExport int
2724win32_open(const char *path, int flag, ...)
0a753a76 2725{
acfe0abc 2726 dTHX;
68dc0745 2727 va_list ap;
2728 int pmode;
82867ecf 2729 WCHAR wBuffer[MAX_PATH+1];
0a753a76 2730
2731 va_start(ap, flag);
2732 pmode = va_arg(ap, int);
2733 va_end(ap);
2734
68dc0745 2735 if (stricmp(path, "/dev/null")==0)
7fac1903
GS
2736 path = "NUL";
2737
2738 if (USING_WIDE()) {
0cb96387 2739 A2WHELPER(path, wBuffer, sizeof(wBuffer));
7766f137 2740 return _wopen(PerlDir_mapW(wBuffer), flag, pmode);
7fac1903 2741 }
7766f137 2742 return open(PerlDir_mapA(path), flag, pmode);
0a753a76 2743}
2744
00b02797
JH
2745/* close() that understands socket */
2746extern int my_close(int); /* in win32sck.c */
2747
68dc0745 2748DllExport int
2749win32_close(int fd)
0a753a76 2750{
00b02797 2751 return my_close(fd);
0a753a76 2752}
2753
68dc0745 2754DllExport int
96e4d5b1 2755win32_eof(int fd)
2756{
390b85e7 2757 return eof(fd);
96e4d5b1 2758}
2759
2760DllExport int
68dc0745 2761win32_dup(int fd)
0a753a76 2762{
390b85e7 2763 return dup(fd);
0a753a76 2764}
2765
68dc0745 2766DllExport int
2767win32_dup2(int fd1,int fd2)
0a753a76 2768{
390b85e7 2769 return dup2(fd1,fd2);
0a753a76 2770}
2771
f7aeb604
GS
2772#ifdef PERL_MSVCRT_READFIX
2773
2774#define LF 10 /* line feed */
2775#define CR 13 /* carriage return */
2776#define CTRLZ 26 /* ctrl-z means eof for text */
2777#define FOPEN 0x01 /* file handle open */
2778#define FEOFLAG 0x02 /* end of file has been encountered */
2779#define FCRLF 0x04 /* CR-LF across read buffer (in text mode) */
2780#define FPIPE 0x08 /* file handle refers to a pipe */
2781#define FAPPEND 0x20 /* file handle opened O_APPEND */
2782#define FDEV 0x40 /* file handle refers to device */
2783#define FTEXT 0x80 /* file handle is in text mode */
2784#define MAX_DESCRIPTOR_COUNT (64*32) /* this is the maximun that MSVCRT can handle */
2785
b181b6fb
GS
2786int __cdecl
2787_fixed_read(int fh, void *buf, unsigned cnt)
f7aeb604
GS
2788{
2789 int bytes_read; /* number of bytes read */
2790 char *buffer; /* buffer to read to */
2791 int os_read; /* bytes read on OS call */
2792 char *p, *q; /* pointers into buffer */
2793 char peekchr; /* peek-ahead character */
2794 ULONG filepos; /* file position after seek */
2795 ULONG dosretval; /* o.s. return value */
2796
2797 /* validate handle */
2798 if (((unsigned)fh >= (unsigned)MAX_DESCRIPTOR_COUNT) ||
2799 !(_osfile(fh) & FOPEN))
2800 {
2801 /* out of range -- return error */
2802 errno = EBADF;
2803 _doserrno = 0; /* not o.s. error */
2804 return -1;
2805 }
2806
635bbe87
GS
2807 /*
2808 * If lockinitflag is FALSE, assume fd is device
2809 * lockinitflag is set to TRUE by open.
2810 */
2811 if (_pioinfo(fh)->lockinitflag)
2812 EnterCriticalSection(&(_pioinfo(fh)->lock)); /* lock file */
f7aeb604
GS
2813
2814 bytes_read = 0; /* nothing read yet */
2815 buffer = (char*)buf;
2816
2817 if (cnt == 0 || (_osfile(fh) & FEOFLAG)) {
2818 /* nothing to read or at EOF, so return 0 read */
2819 goto functionexit;
2820 }
2821
2822 if ((_osfile(fh) & (FPIPE|FDEV)) && _pipech(fh) != LF) {
2823 /* a pipe/device and pipe lookahead non-empty: read the lookahead
2824 * char */
2825 *buffer++ = _pipech(fh);
2826 ++bytes_read;
2827 --cnt;
2828 _pipech(fh) = LF; /* mark as empty */
2829 }
2830
2831 /* read the data */
2832
2833 if (!ReadFile((HANDLE)_osfhnd(fh), buffer, cnt, (LPDWORD)&os_read, NULL))
2834 {
2835 /* ReadFile has reported an error. recognize two special cases.
2836 *
2837 * 1. map ERROR_ACCESS_DENIED to EBADF
2838 *
2839 * 2. just return 0 if ERROR_BROKEN_PIPE has occurred. it
2840 * means the handle is a read-handle on a pipe for which
2841 * all write-handles have been closed and all data has been
2842 * read. */
2843
2844 if ((dosretval = GetLastError()) == ERROR_ACCESS_DENIED) {
2845 /* wrong read/write mode should return EBADF, not EACCES */
2846 errno = EBADF;
2847 _doserrno = dosretval;
2848 bytes_read = -1;
2849 goto functionexit;
2850 }
2851 else if (dosretval == ERROR_BROKEN_PIPE) {
2852 bytes_read = 0;
2853 goto functionexit;
2854 }
2855 else {
2856 bytes_read = -1;
2857 goto functionexit;
2858 }
2859 }
2860
2861 bytes_read += os_read; /* update bytes read */
2862
2863 if (_osfile(fh) & FTEXT) {
2864 /* now must translate CR-LFs to LFs in the buffer */
2865
2866 /* set CRLF flag to indicate LF at beginning of buffer */
2867 /* if ((os_read != 0) && (*(char *)buf == LF)) */
2868 /* _osfile(fh) |= FCRLF; */
2869 /* else */
2870 /* _osfile(fh) &= ~FCRLF; */
2871
2872 _osfile(fh) &= ~FCRLF;
2873
2874 /* convert chars in the buffer: p is src, q is dest */
2875 p = q = (char*)buf;
2876 while (p < (char *)buf + bytes_read) {
2877 if (*p == CTRLZ) {
2878 /* if fh is not a device, set ctrl-z flag */
2879 if (!(_osfile(fh) & FDEV))
2880 _osfile(fh) |= FEOFLAG;
2881 break; /* stop translating */
2882 }
2883 else if (*p != CR)
2884 *q++ = *p++;
2885 else {
2886 /* *p is CR, so must check next char for LF */
2887 if (p < (char *)buf + bytes_read - 1) {
2888 if (*(p+1) == LF) {
2889 p += 2;
2890 *q++ = LF; /* convert CR-LF to LF */
2891 }
2892 else
2893 *q++ = *p++; /* store char normally */
2894 }
2895 else {
2896 /* This is the hard part. We found a CR at end of
2897 buffer. We must peek ahead to see if next char
2898 is an LF. */
2899 ++p;
2900
2901 dosretval = 0;
2902 if (!ReadFile((HANDLE)_osfhnd(fh), &peekchr, 1,
2903 (LPDWORD)&os_read, NULL))
2904 dosretval = GetLastError();
2905
2906 if (dosretval != 0 || os_read == 0) {
2907 /* couldn't read ahead, store CR */
2908 *q++ = CR;
2909 }
2910 else {
2911 /* peekchr now has the extra character -- we now
2912 have several possibilities:
2913 1. disk file and char is not LF; just seek back
2914 and copy CR
2915 2. disk file and char is LF; store LF, don't seek back
2916 3. pipe/device and char is LF; store LF.
2917 4. pipe/device and char isn't LF, store CR and
2918 put char in pipe lookahead buffer. */
2919 if (_osfile(fh) & (FDEV|FPIPE)) {
2920 /* non-seekable device */
2921 if (peekchr == LF)
2922 *q++ = LF;
2923 else {
2924 *q++ = CR;
2925 _pipech(fh) = peekchr;
2926 }
2927 }
2928 else {
2929 /* disk file */
2930 if (peekchr == LF) {
2931 /* nothing read yet; must make some
2932 progress */
2933 *q++ = LF;
2934 /* turn on this flag for tell routine */
2935 _osfile(fh) |= FCRLF;
2936 }
2937 else {
2938 HANDLE osHandle; /* o.s. handle value */
2939 /* seek back */
2940 if ((osHandle = (HANDLE)_get_osfhandle(fh)) != (HANDLE)-1)
2941 {
2942 if ((filepos = SetFilePointer(osHandle, -1, NULL, FILE_CURRENT)) == -1)
2943 dosretval = GetLastError();
2944 }
2945 if (peekchr != LF)
2946 *q++ = CR;
2947 }
2948 }
2949 }
2950 }
2951 }
2952 }
2953
2954 /* we now change bytes_read to reflect the true number of chars
2955 in the buffer */
2956 bytes_read = q - (char *)buf;
2957 }
2958
2959functionexit:
635bbe87
GS
2960 if (_pioinfo(fh)->lockinitflag)
2961 LeaveCriticalSection(&(_pioinfo(fh)->lock)); /* unlock file */
f7aeb604
GS
2962
2963 return bytes_read;
2964}
2965
2966#endif /* PERL_MSVCRT_READFIX */
2967
68dc0745 2968DllExport int
3e3baf6d 2969win32_read(int fd, void *buf, unsigned int cnt)
0a753a76 2970{
f7aeb604
GS
2971#ifdef PERL_MSVCRT_READFIX
2972 return _fixed_read(fd, buf, cnt);
2973#else
390b85e7 2974 return read(fd, buf, cnt);
f7aeb604 2975#endif
0a753a76 2976}
2977
68dc0745 2978DllExport int
3e3baf6d 2979win32_write(int fd, const void *buf, unsigned int cnt)
0a753a76 2980{
390b85e7 2981 return write(fd, buf, cnt);
0a753a76 2982}
2983
68dc0745 2984DllExport int
5aabfad6 2985win32_mkdir(const char *dir, int mode)
2986{
acfe0abc 2987 dTHX;
7766f137 2988 if (USING_WIDE()) {
82867ecf 2989 WCHAR wBuffer[MAX_PATH+1];
7766f137
GS
2990 A2WHELPER(dir, wBuffer, sizeof(wBuffer));
2991 return _wmkdir(PerlDir_mapW(wBuffer));
2992 }
2993 return mkdir(PerlDir_mapA(dir)); /* just ignore mode */
5aabfad6 2994}
96e4d5b1 2995
5aabfad6 2996DllExport int
2997win32_rmdir(const char *dir)
2998{
acfe0abc 2999 dTHX;
7766f137 3000 if (USING_WIDE()) {
82867ecf 3001 WCHAR wBuffer[MAX_PATH+1];
7766f137
GS
3002 A2WHELPER(dir, wBuffer, sizeof(wBuffer));
3003 return _wrmdir(PerlDir_mapW(wBuffer));
3004 }
3005 return rmdir(PerlDir_mapA(dir));
5aabfad6 3006}
96e4d5b1 3007
5aabfad6 3008DllExport int
3009win32_chdir(const char *dir)
3010{
acfe0abc 3011 dTHX;
7766f137 3012 if (USING_WIDE()) {
82867ecf 3013 WCHAR wBuffer[MAX_PATH+1];
7766f137
GS
3014 A2WHELPER(dir, wBuffer, sizeof(wBuffer));
3015 return _wchdir(wBuffer);
3016 }
390b85e7 3017 return chdir(dir);
5aabfad6 3018}
96e4d5b1 3019
7766f137
GS
3020DllExport int
3021win32_access(const char *path, int mode)
3022{
acfe0abc 3023 dTHX;
7766f137 3024 if (USING_WIDE()) {
82867ecf 3025 WCHAR wBuffer[MAX_PATH+1];
7766f137
GS
3026 A2WHELPER(path, wBuffer, sizeof(wBuffer));
3027 return _waccess(PerlDir_mapW(wBuffer), mode);
3028 }
3029 return access(PerlDir_mapA(path), mode);
3030}
3031
3032DllExport int
3033win32_chmod(const char *path, int mode)
3034{
acfe0abc 3035 dTHX;
7766f137 3036 if (USING_WIDE()) {
82867ecf 3037 WCHAR wBuffer[MAX_PATH+1];
7766f137
GS
3038 A2WHELPER(path, wBuffer, sizeof(wBuffer));
3039 return _wchmod(PerlDir_mapW(wBuffer), mode);
3040 }
3041 return chmod(PerlDir_mapA(path), mode);
3042}
3043
3044
0aaad0ff
GS
3045static char *
3046create_command_line(const char* command, const char * const *args)
3047{
acfe0abc 3048 dTHX;
0aaad0ff
GS
3049 int index;
3050 char *cmd, *ptr, *arg;
3051 STRLEN len = strlen(command) + 1;
3052
3053 for (index = 0; (ptr = (char*)args[index]) != NULL; ++index)
3054 len += strlen(ptr) + 1;
3055
3056 New(1310, cmd, len, char);
3057 ptr = cmd;
3058 strcpy(ptr, command);
0aaad0ff
GS
3059
3060 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
0aaad0ff 3061 ptr += strlen(ptr);
18a945d4
GS
3062 *ptr++ = ' ';
3063 strcpy(ptr, arg);
0aaad0ff
GS
3064 }
3065
3066 return cmd;
3067}
3068
3069static char *
3070qualified_path(const char *cmd)
3071{
acfe0abc 3072 dTHX;
0aaad0ff
GS
3073 char *pathstr;
3074 char *fullcmd, *curfullcmd;
3075 STRLEN cmdlen = 0;
3076 int has_slash = 0;
3077
3078 if (!cmd)
3079 return Nullch;
3080 fullcmd = (char*)cmd;
3081 while (*fullcmd) {
3082 if (*fullcmd == '/' || *fullcmd == '\\')
3083 has_slash++;
3084 fullcmd++;
3085 cmdlen++;
3086 }
3087
3088 /* look in PATH */
2fb9ab56 3089 pathstr = PerlEnv_getenv("PATH");
0aaad0ff
GS
3090 New(0, fullcmd, MAX_PATH+1, char);
3091 curfullcmd = fullcmd;
3092
3093 while (1) {
3094 DWORD res;
3095
3096 /* start by appending the name to the current prefix */
3097 strcpy(curfullcmd, cmd);
3098 curfullcmd += cmdlen;
3099
3100 /* if it doesn't end with '.', or has no extension, try adding
3101 * a trailing .exe first */
3102 if (cmd[cmdlen-1] != '.'
3103 && (cmdlen < 4 || cmd[cmdlen-4] != '.'))
3104 {
3105 strcpy(curfullcmd, ".exe");
3106 res = GetFileAttributes(fullcmd);
3107 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
3108 return fullcmd;
3109 *curfullcmd = '\0';
3110 }
3111
3112 /* that failed, try the bare name */
3113 res = GetFileAttributes(fullcmd);
3114 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
3115 return fullcmd;
3116
3117 /* quit if no other path exists, or if cmd already has path */
3118 if (!pathstr || !*pathstr || has_slash)
3119 break;
3120
3121 /* skip leading semis */
3122 while (*pathstr == ';')
3123 pathstr++;
3124
3125 /* build a new prefix from scratch */
3126 curfullcmd = fullcmd;
3127 while (*pathstr && *pathstr != ';') {
3128 if (*pathstr == '"') { /* foo;"baz;etc";bar */
3129 pathstr++; /* skip initial '"' */
3130 while (*pathstr && *pathstr != '"') {
3131 if (curfullcmd-fullcmd < MAX_PATH-cmdlen-5)
3132 *curfullcmd++ = *pathstr;
3133 pathstr++;
3134 }
3135 if (*pathstr)
3136 pathstr++; /* skip trailing '"' */
3137 }
3138 else {
3139 if (curfullcmd-fullcmd < MAX_PATH-cmdlen-5)
3140 *curfullcmd++ = *pathstr;
3141 pathstr++;
3142 }
3143 }
3144 if (*pathstr)
3145 pathstr++; /* skip trailing semi */
3146 if (curfullcmd > fullcmd /* append a dir separator */
3147 && curfullcmd[-1] != '/' && curfullcmd[-1] != '\\')
3148 {
3149 *curfullcmd++ = '\\';
3150 }
3151 }
3152GIVE_UP:
3153 Safefree(fullcmd);
3154 return Nullch;
3155}
3156
3075ddba
GS
3157/* The following are just place holders.
3158 * Some hosts may provide and environment that the OS is
3159 * not tracking, therefore, these host must provide that
3160 * environment and the current directory to CreateProcess
3161 */
3162
3163void*
3164get_childenv(void)
3165{
3166 return NULL;
3167}
3168
3169void
2dd19d29 3170free_childenv(void* d)
3075ddba
GS
3171{
3172}
3173
3174char*
3175get_childdir(void)
3176{
acfe0abc 3177 dTHX;
7766f137
GS
3178 char* ptr;
3179 char szfilename[(MAX_PATH+1)*2];
3180 if (USING_WIDE()) {
3181 WCHAR wfilename[MAX_PATH+1];
3182 GetCurrentDirectoryW(MAX_PATH+1, wfilename);
3183 W2AHELPER(wfilename, szfilename, sizeof(szfilename));
3184 }
3185 else {
3186 GetCurrentDirectoryA(MAX_PATH+1, szfilename);
3187 }
3188
3189 New(0, ptr, strlen(szfilename)+1, char);
3190 strcpy(ptr, szfilename);
3191 return ptr;
3075ddba
GS
3192}
3193
3194void
2dd19d29 3195free_childdir(char* d)
3075ddba 3196{
acfe0abc 3197 dTHX;
7766f137 3198 Safefree(d);
3075ddba
GS
3199}
3200
3201
0aaad0ff
GS
3202/* XXX this needs to be made more compatible with the spawnvp()
3203 * provided by the various RTLs. In particular, searching for
3204 * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented.
3205 * This doesn't significantly affect perl itself, because we
3206 * always invoke things using PERL5SHELL if a direct attempt to
3207 * spawn the executable fails.
3208 *
3209 * XXX splitting and rejoining the commandline between do_aspawn()
3210 * and win32_spawnvp() could also be avoided.
3211 */
3212
5aabfad6 3213DllExport int
3e3baf6d 3214win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
0a753a76 3215{
0aaad0ff
GS
3216#ifdef USE_RTL_SPAWNVP
3217 return spawnvp(mode, cmdname, (char * const *)argv);
3218#else
acfe0abc 3219 dTHX;
2b260de0 3220 int ret;
3075ddba
GS
3221 void* env;
3222 char* dir;
635bbe87 3223 child_IO_table tbl;
0aaad0ff
GS
3224 STARTUPINFO StartupInfo;
3225 PROCESS_INFORMATION ProcessInformation;
3226 DWORD create = 0;
3227
3228 char *cmd = create_command_line(cmdname, strcmp(cmdname, argv[0]) == 0
3229 ? &argv[1] : argv);
3230 char *fullcmd = Nullch;
3231
3075ddba
GS
3232 env = PerlEnv_get_childenv();
3233 dir = PerlEnv_get_childdir();
3234
0aaad0ff
GS
3235 switch(mode) {
3236 case P_NOWAIT: /* asynch + remember result */
3237 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
3238 errno = EAGAIN;
3239 ret = -1;
3240 goto RETVAL;
3241 }
3242 /* FALL THROUGH */
3243 case P_WAIT: /* synchronous execution */
3244 break;
3245 default: /* invalid mode */
3246 errno = EINVAL;
3247 ret = -1;
3248 goto RETVAL;
3249 }
3250 memset(&StartupInfo,0,sizeof(StartupInfo));
3251 StartupInfo.cb = sizeof(StartupInfo);
f83751a7 3252 memset(&tbl,0,sizeof(tbl));
635bbe87 3253 PerlEnv_get_child_IO(&tbl);
f83751a7
GS
3254 StartupInfo.dwFlags = tbl.dwFlags;
3255 StartupInfo.dwX = tbl.dwX;
3256 StartupInfo.dwY = tbl.dwY;
3257 StartupInfo.dwXSize = tbl.dwXSize;
3258 StartupInfo.dwYSize = tbl.dwYSize;
3259 StartupInfo.dwXCountChars = tbl.dwXCountChars;
3260 StartupInfo.dwYCountChars = tbl.dwYCountChars;
3261 StartupInfo.dwFillAttribute = tbl.dwFillAttribute;
3262 StartupInfo.wShowWindow = tbl.wShowWindow;
3263 StartupInfo.hStdInput = tbl.childStdIn;
3264 StartupInfo.hStdOutput = tbl.childStdOut;
3265 StartupInfo.hStdError = tbl.childStdErr;
3ffaa937
GS
3266 if (StartupInfo.hStdInput != INVALID_HANDLE_VALUE &&
3267 StartupInfo.hStdOutput != INVALID_HANDLE_VALUE &&
3268 StartupInfo.hStdError != INVALID_HANDLE_VALUE)
3269 {
3270 StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
3271 }
3272 else {
3273 create |= CREATE_NEW_CONSOLE;
3274 }
3275
0aaad0ff
GS
3276RETRY:
3277 if (!CreateProcess(cmdname, /* search PATH to find executable */
3278 cmd, /* executable, and its arguments */
3279 NULL, /* process attributes */
3280 NULL, /* thread attributes */
3281 TRUE, /* inherit handles */
3282 create, /* creation flags */
3075ddba
GS
3283 (LPVOID)env, /* inherit environment */
3284 dir, /* inherit cwd */
0aaad0ff
GS
3285 &StartupInfo,
3286 &ProcessInformation))
3287 {
3288 /* initial NULL argument to CreateProcess() does a PATH
3289 * search, but it always first looks in the directory
3290 * where the current process was started, which behavior
3291 * is undesirable for backward compatibility. So we
3292 * jump through our own hoops by picking out the path
3293 * we really want it to use. */
3294 if (!fullcmd) {
3295 fullcmd = qualified_path(cmdname);
3296 if (fullcmd) {
3297 cmdname = fullcmd;
3298 goto RETRY;
3299 }
3300 }
3301 errno = ENOENT;
3302 ret = -1;
3303 goto RETVAL;
3304 }
2d7a9237 3305
0aaad0ff
GS
3306 if (mode == P_NOWAIT) {
3307 /* asynchronous spawn -- store handle, return PID */
2b260de0 3308 ret = (int)ProcessInformation.dwProcessId;
922b1888
GS
3309 if (IsWin95() && ret < 0)
3310 ret = -ret;
3311
3312 w32_child_handles[w32_num_children] = ProcessInformation.hProcess;
3313 w32_child_pids[w32_num_children] = (DWORD)ret;
0aaad0ff
GS
3314 ++w32_num_children;
3315 }
3316 else {
2b260de0 3317 DWORD status;
0aaad0ff 3318 WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
2b260de0
GS
3319 GetExitCodeProcess(ProcessInformation.hProcess, &status);
3320 ret = (int)status;
0aaad0ff
GS
3321 CloseHandle(ProcessInformation.hProcess);
3322 }
e17cb2a9 3323
0aaad0ff 3324 CloseHandle(ProcessInformation.hThread);
3075ddba 3325
0aaad0ff 3326RETVAL:
3075ddba
GS
3327 PerlEnv_free_childenv(env);
3328 PerlEnv_free_childdir(dir);
0aaad0ff
GS
3329 Safefree(cmd);
3330 Safefree(fullcmd);
2b260de0 3331 return ret;
2d7a9237 3332#endif
0a753a76 3333}
3334
6890e559 3335DllExport int
eb62e965
JD
3336win32_execv(const char *cmdname, const char *const *argv)
3337{
7766f137 3338#ifdef USE_ITHREADS
acfe0abc 3339 dTHX;
7766f137
GS
3340 /* if this is a pseudo-forked child, we just want to spawn
3341 * the new program, and return */
3342 if (w32_pseudo_id)
3343 return spawnv(P_WAIT, cmdname, (char *const *)argv);
3344#endif
eb62e965
JD
3345 return execv(cmdname, (char *const *)argv);
3346}
3347
3348DllExport int
6890e559
GS
3349win32_execvp(const char *cmdname, const char *const *argv)
3350{
7766f137 3351#ifdef USE_ITHREADS
acfe0abc 3352 dTHX;
7766f137
GS
3353 /* if this is a pseudo-forked child, we just want to spawn
3354 * the new program, and return */
190e4ad0 3355 if (w32_pseudo_id) {
ba6ce41c
GS
3356 int status = win32_spawnvp(P_WAIT, cmdname, (char *const *)argv);
3357 if (status != -1) {
3358 my_exit(status);
3359 return 0;
3360 }
3361 else
3362 return status;
190e4ad0 3363 }
7766f137 3364#endif
390b85e7 3365 return execvp(cmdname, (char *const *)argv);
6890e559
GS
3366}
3367
84902520
TB
3368DllExport void
3369win32_perror(const char *str)
3370{
390b85e7 3371 perror(str);
84902520
TB
3372}
3373
3374DllExport void
3375win32_setbuf(FILE *pf, char *buf)
3376{
390b85e7 3377 setbuf(pf, buf);
84902520
TB
3378}
3379
3380DllExport int
3381win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
3382{
390b85e7 3383 return setvbuf(pf, buf, type, size);
84902520
TB
3384}
3385
3386DllExport int
3387win32_flushall(void)
3388{
390b85e7 3389 return flushall();
84902520
TB
3390}
3391
3392DllExport int
3393win32_fcloseall(void)
3394{
390b85e7 3395 return fcloseall();
84902520
TB
3396}
3397
3398DllExport char*
3399win32_fgets(char *s, int n, FILE *pf)
3400{
390b85e7 3401 return fgets(s, n, pf);
84902520
TB
3402}
3403
3404DllExport char*
3405win32_gets(char *s)
3406{
390b85e7 3407 return gets(s);
84902520
TB
3408}
3409
3410DllExport int
3411win32_fgetc(FILE *pf)
3412{
390b85e7 3413 return fgetc(pf);
84902520
TB
3414}
3415
3416DllExport int
3417win32_putc(int c, FILE *pf)
3418{
390b85e7 3419 return putc(c,pf);
84902520
TB
3420}
3421
3422DllExport int
3423win32_puts(const char *s)
3424{
390b85e7 3425 return puts(s);
84902520
TB
3426}
3427
3428DllExport int
3429win32_getchar(void)
3430{
390b85e7 3431 return getchar();
84902520
TB
3432}
3433
3434DllExport int
3435win32_putchar(int c)
3436{
390b85e7 3437 return putchar(c);
84902520
TB
3438}
3439
bbc8f9de
NIS
3440#ifdef MYMALLOC
3441
3442#ifndef USE_PERL_SBRK
3443
3444static char *committed = NULL;
3445static char *base = NULL;
3446static char *reserved = NULL;
3447static char *brk = NULL;
3448static DWORD pagesize = 0;
3449static DWORD allocsize = 0;
3450
3451void *
3452sbrk(int need)
3453{
3454 void *result;
3455 if (!pagesize)
3456 {SYSTEM_INFO info;
3457 GetSystemInfo(&info);
3458 /* Pretend page size is larger so we don't perpetually
3459 * call the OS to commit just one page ...
3460 */
3461 pagesize = info.dwPageSize << 3;
3462 allocsize = info.dwAllocationGranularity;
3463 }
3464 /* This scheme fails eventually if request for contiguous
3465 * block is denied so reserve big blocks - this is only
3466 * address space not memory ...
3467 */
3468 if (brk+need >= reserved)
3469 {
3470 DWORD size = 64*1024*1024;
3471 char *addr;
3472 if (committed && reserved && committed < reserved)
3473 {
3474 /* Commit last of previous chunk cannot span allocations */
161b471a 3475 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
bbc8f9de
NIS
3476 if (addr)
3477 committed = reserved;
3478 }
3479 /* Reserve some (more) space
3480 * Note this is a little sneaky, 1st call passes NULL as reserved
3481 * so lets system choose where we start, subsequent calls pass
3482 * the old end address so ask for a contiguous block
3483 */
161b471a 3484 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
bbc8f9de
NIS
3485 if (addr)
3486 {
3487 reserved = addr+size;
3488 if (!base)
3489 base = addr;
3490 if (!committed)
3491 committed = base;
3492 if (!brk)
3493 brk = committed;
3494 }
3495 else
3496 {
3497 return (void *) -1;
3498 }
3499 }
3500 result = brk;
3501 brk += need;
3502 if (brk > committed)
3503 {
3504 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
161b471a 3505 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
bbc8f9de
NIS
3506 if (addr)
3507 {
3508 committed += size;
3509 }
3510 else
3511 return (void *) -1;
3512 }
3513 return result;
3514}
3515
3516#endif
3517#endif
3518
84902520
TB
3519DllExport void*
3520win32_malloc(size_t size)
3521{
390b85e7 3522 return malloc(size);
84902520
TB
3523}
3524
3525DllExport void*
3526win32_calloc(size_t numitems, size_t size)
3527{
390b85e7 3528 return calloc(numitems,size);
84902520
TB
3529}
3530
3531DllExport void*
3532win32_realloc(void *block, size_t size)
3533{
390b85e7 3534 return realloc(block,size);
84902520
TB
3535}
3536
3537DllExport void
3538win32_free(void *block)
3539{
390b85e7 3540 free(block);
84902520
TB
3541}
3542
bbc8f9de 3543
68dc0745 3544int
65e48ea9 3545win32_open_osfhandle(long handle, int flags)
0a753a76 3546{
9e5f57de
GS
3547#ifdef USE_FIXED_OSFHANDLE
3548 if (IsWin95())
3549 return my_open_osfhandle(handle, flags);
3550#endif
390b85e7 3551 return _open_osfhandle(handle, flags);
0a753a76 3552}
3553
68dc0745 3554long
65e48ea9 3555win32_get_osfhandle(int fd)
0a753a76 3556{
390b85e7 3557 return _get_osfhandle(fd);
0a753a76 3558}
7bac28a0 3559
0cb96387 3560DllExport void*
c5be433b 3561win32_dynaload(const char* filename)
0cb96387 3562{
acfe0abc 3563 dTHX;
51371543 3564 HMODULE hModule;
32f99636
GS
3565 char buf[MAX_PATH+1];
3566 char *first;
3567
3568 /* LoadLibrary() doesn't recognize forward slashes correctly,
3569 * so turn 'em back. */
3570 first = strchr(filename, '/');
3571 if (first) {
3572 STRLEN len = strlen(filename);
3573 if (len <= MAX_PATH) {
3574 strcpy(buf, filename);
3575 filename = &buf[first - filename];
3576 while (*filename) {
3577 if (*filename == '/')
3578 *(char*)filename = '\\';
3579 ++filename;
3580 }
3581 filename = buf;
3582 }
3583 }
0cb96387 3584 if (USING_WIDE()) {
82867ecf 3585 WCHAR wfilename[MAX_PATH+1];
0cb96387 3586 A2WHELPER(filename, wfilename, sizeof(wfilename));
7766f137 3587 hModule = LoadLibraryExW(PerlDir_mapW(wfilename), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
0cb96387
GS
3588 }
3589 else {
7766f137 3590 hModule = LoadLibraryExA(PerlDir_mapA(filename), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
0cb96387
GS
3591 }
3592 return hModule;
3593}
3594
7bac28a0 3595/*
3596 * Extras.
3597 */
3598
ad2e33dc
GS
3599static
3600XS(w32_GetCwd)
3601{
3602 dXSARGS;
7766f137
GS
3603 /* Make the host for current directory */
3604 char* ptr = PerlEnv_get_childdir();
ad2e33dc 3605 /*
7766f137 3606 * If ptr != Nullch
ad2e33dc 3607 * then it worked, set PV valid,
7766f137 3608 * else return 'undef'
ad2e33dc 3609 */
7766f137
GS
3610 if (ptr) {
3611 SV *sv = sv_newmortal();
3612 sv_setpv(sv, ptr);
3613 PerlEnv_free_childdir(ptr);
3614
3615 EXTEND(SP,1);
ad2e33dc 3616 SvPOK_on(sv);
bb897dfc
JD
3617 ST(0) = sv;
3618 XSRETURN(1);
3619 }
3467312b 3620 XSRETURN_UNDEF;
ad2e33dc
GS
3621}
3622
3623static
3624XS(w32_SetCwd)
3625{
3626 dXSARGS;
3627 if (items != 1)
4f63d024 3628 Perl_croak(aTHX_ "usage: Win32::SetCurrentDirectory($cwd)");
7766f137 3629 if (!PerlDir_chdir(SvPV_nolen(ST(0))))
ad2e33dc
GS
3630 XSRETURN_YES;
3631
3632 XSRETURN_NO;
3633}
3634
3635static
3636XS(w32_GetNextAvailDrive)
3637{
3638 dXSARGS;
3639 char ix = 'C';
3640 char root[] = "_:\\";
3467312b
JD
3641
3642 EXTEND(SP,1);
ad2e33dc
GS
3643 while (ix <= 'Z') {
3644 root[0] = ix++;
3645 if (GetDriveType(root) == 1) {
3646 root[2] = '\0';
3647 XSRETURN_PV(root);
3648 }
3649 }
3467312b 3650 XSRETURN_UNDEF;
ad2e33dc
GS
3651}
3652
3653static
3654XS(w32_GetLastError)
3655{
3656 dXSARGS;
bb897dfc 3657 EXTEND(SP,1);
ad2e33dc
GS
3658 XSRETURN_IV(GetLastError());
3659}
3660
3661static
ca135624
JD
3662XS(w32_SetLastError)
3663{
3664 dXSARGS;
3665 if (items != 1)
4f63d024 3666 Perl_croak(aTHX_ "usage: Win32::SetLastError($error)");
ca135624 3667 SetLastError(SvIV(ST(0)));
bb897dfc 3668 XSRETURN_EMPTY;
ca135624
JD
3669}
3670
3671static
ad2e33dc
GS
3672XS(w32_LoginName)
3673{
3674 dXSARGS;
3352bfcb
GS
3675 char *name = w32_getlogin_buffer;
3676 DWORD size = sizeof(w32_getlogin_buffer);
3467312b 3677 EXTEND(SP,1);
ad2e33dc
GS
3678 if (GetUserName(name,&size)) {
3679 /* size includes NULL */
79cb57f6 3680 ST(0) = sv_2mortal(newSVpvn(name,size-1));
ad2e33dc
GS
3681 XSRETURN(1);
3682 }
3467312b 3683 XSRETURN_UNDEF;
ad2e33dc
GS
3684}
3685
3686static
3687XS(w32_NodeName)
3688{
3689 dXSARGS;
3690 char name[MAX_COMPUTERNAME_LENGTH+1];
3691 DWORD size = sizeof(name);
3467312b 3692 EXTEND(SP,1);
ad2e33dc
GS
3693 if (GetComputerName(name,&size)) {
3694 /* size does NOT include NULL :-( */
79cb57f6 3695 ST(0) = sv_2mortal(newSVpvn(name,size));
ad2e33dc
GS
3696 XSRETURN(1);
3697 }
3467312b 3698 XSRETURN_UNDEF;
ad2e33dc
GS
3699}
3700
3701
3702static
3703XS(w32_DomainName)
3704{
3705 dXSARGS;
da147683
JD
3706 HINSTANCE hNetApi32 = LoadLibrary("netapi32.dll");
3707 DWORD (__stdcall *pfnNetApiBufferFree)(LPVOID Buffer);
3708 DWORD (__stdcall *pfnNetWkstaGetInfo)(LPWSTR servername, DWORD level,
3709 void *bufptr);
625a29bd 3710
da147683
JD
3711 if (hNetApi32) {
3712 pfnNetApiBufferFree = (DWORD (__stdcall *)(void *))
3713 GetProcAddress(hNetApi32, "NetApiBufferFree");
3714 pfnNetWkstaGetInfo = (DWORD (__stdcall *)(LPWSTR, DWORD, void *))
3715 GetProcAddress(hNetApi32, "NetWkstaGetInfo");
d12db45c 3716 }
da147683
JD
3717 EXTEND(SP,1);
3718 if (hNetApi32 && pfnNetWkstaGetInfo && pfnNetApiBufferFree) {
3719 /* this way is more reliable, in case user has a local account. */
3720 char dname[256];
3721 DWORD dnamelen = sizeof(dname);
3722 struct {
3723 DWORD wki100_platform_id;
3724 LPWSTR wki100_computername;
3725 LPWSTR wki100_langroup;
3726 DWORD wki100_ver_major;
3727 DWORD wki100_ver_minor;
3728 } *pwi;
3729 /* NERR_Success *is* 0*/
3730 if (0 == pfnNetWkstaGetInfo(NULL, 100, &pwi)) {
3731 if (pwi->wki100_langroup && *(pwi->wki100_langroup)) {
3732 WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_langroup,
3733 -1, (LPSTR)dname, dnamelen, NULL, NULL);
3734 }
3735 else {
3736 WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_computername,
3737 -1, (LPSTR)dname, dnamelen, NULL, NULL);
3738 }
3739 pfnNetApiBufferFree(pwi);
3740 FreeLibrary(hNetApi32);
3741 XSRETURN_PV(dname);
3742 }
3743 FreeLibrary(hNetApi32);
ad2e33dc 3744 }
625a29bd 3745 else {
da147683
JD
3746 /* Win95 doesn't have NetWksta*(), so do it the old way */
3747 char name[256];
3748 DWORD size = sizeof(name);
3749 if (hNetApi32)
3750 FreeLibrary(hNetApi32);
3751 if (GetUserName(name,&size)) {
3752 char sid[ONE_K_BUFSIZE];
3753 DWORD sidlen = sizeof(sid);
3754 char dname[256];
3755 DWORD dnamelen = sizeof(dname);
3756 SID_NAME_USE snu;
3757 if (LookupAccountName(NULL, name, (PSID)&sid, &sidlen,
3758 dname, &dnamelen, &snu)) {
3759 XSRETURN_PV(dname); /* all that for this */
3760 }
3761 }
9404a519 3762 }
da147683 3763 XSRETURN_UNDEF;
ad2e33dc
GS
3764}
3765
3766static
3767XS(w32_FsType)
3768{
3769 dXSARGS;
3770 char fsname[256];
3771 DWORD flags, filecomplen;
3772 if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
3773 &flags, fsname, sizeof(fsname))) {
bb897dfc 3774 if (GIMME_V == G_ARRAY) {
79cb57f6 3775 XPUSHs(sv_2mortal(newSVpvn(fsname,strlen(fsname))));
ad2e33dc
GS
3776 XPUSHs(sv_2mortal(newSViv(flags)));
3777 XPUSHs(sv_2mortal(newSViv(filecomplen)));
3778 PUTBACK;
3779 return;
3780 }
bb897dfc 3781 EXTEND(SP,1);
ad2e33dc
GS
3782 XSRETURN_PV(fsname);
3783 }
bb897dfc 3784 XSRETURN_EMPTY;
ad2e33dc
GS
3785}
3786
3787static
3788XS(w32_GetOSVersion)
3789{
3790 dXSARGS;
7766f137 3791 OSVERSIONINFOA osver;
ad2e33dc 3792
7766f137
GS
3793 if (USING_WIDE()) {
3794 OSVERSIONINFOW osverw;
3795 char szCSDVersion[sizeof(osverw.szCSDVersion)];
3796 osverw.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
3797 if (!GetVersionExW(&osverw)) {
3798 XSRETURN_EMPTY;
3799 }
3800 W2AHELPER(osverw.szCSDVersion, szCSDVersion, sizeof(szCSDVersion));
3801 XPUSHs(newSVpvn(szCSDVersion, strlen(szCSDVersion)));
3802 osver.dwMajorVersion = osverw.dwMajorVersion;
3803 osver.dwMinorVersion = osverw.dwMinorVersion;
3804 osver.dwBuildNumber = osverw.dwBuildNumber;
3805 osver.dwPlatformId = osverw.dwPlatformId;
3806 }
3807 else {
3808 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
3809 if (!GetVersionExA(&osver)) {
3810 XSRETURN_EMPTY;
3811 }
79cb57f6 3812 XPUSHs(newSVpvn(osver.szCSDVersion, strlen(osver.szCSDVersion)));
ad2e33dc 3813 }
7766f137
GS
3814 XPUSHs(newSViv(osver.dwMajorVersion));
3815 XPUSHs(newSViv(osver.dwMinorVersion));
3816 XPUSHs(newSViv(osver.dwBuildNumber));
3817 XPUSHs(newSViv(osver.dwPlatformId));
3818 PUTBACK;
ad2e33dc
GS
3819}
3820
3821static
3822XS(w32_IsWinNT)
3823{
3824 dXSARGS;
bb897dfc 3825 EXTEND(SP,1);
ad2e33dc
GS
3826 XSRETURN_IV(IsWinNT());
3827}
3828
3829static
3830XS(w32_IsWin95)
3831{
3832 dXSARGS;
bb897dfc 3833 EXTEND(SP,1);
ad2e33dc
GS
3834 XSRETURN_IV(IsWin95());
3835}
3836
3837static
3838XS(w32_FormatMessage)
3839{
3840 dXSARGS;
3841 DWORD source = 0;
7766f137 3842 char msgbuf[ONE_K_BUFSIZE];
ad2e33dc
GS
3843
3844 if (items != 1)
4f63d024 3845 Perl_croak(aTHX_ "usage: Win32::FormatMessage($errno)");
ad2e33dc 3846
7766f137
GS
3847 if (USING_WIDE()) {
3848 WCHAR wmsgbuf[ONE_K_BUFSIZE];
3849 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
3850 &source, SvIV(ST(0)), 0,
3851 wmsgbuf, ONE_K_BUFSIZE-1, NULL))
3852 {
3853 W2AHELPER(wmsgbuf, msgbuf, sizeof(msgbuf));
3854 XSRETURN_PV(msgbuf);
3855 }
3856 }
3857 else {
3858 if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
3859 &source, SvIV(ST(0)), 0,
3860 msgbuf, sizeof(msgbuf)-1, NULL))
3861 XSRETURN_PV(msgbuf);
3862 }
ad2e33dc 3863
3467312b 3864 XSRETURN_UNDEF;
ad2e33dc
GS
3865}
3866
3867static
3868XS(w32_Spawn)
3869{
3870 dXSARGS;
3871 char *cmd, *args;
33005217
JD
3872 void *env;
3873 char *dir;
ad2e33dc
GS
3874 PROCESS_INFORMATION stProcInfo;
3875 STARTUPINFO stStartInfo;
3876 BOOL bSuccess = FALSE;
3877
9404a519 3878 if (items != 3)
4f63d024 3879 Perl_croak(aTHX_ "usage: Win32::Spawn($cmdName, $args, $PID)");
ad2e33dc 3880
bb897dfc
JD
3881 cmd = SvPV_nolen(ST(0));
3882 args = SvPV_nolen(ST(1));
ad2e33dc 3883
33005217
JD
3884 env = PerlEnv_get_childenv();
3885 dir = PerlEnv_get_childdir();
3886
ad2e33dc
GS
3887 memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */
3888 stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */
3889 stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */
3890 stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */
3891
9404a519 3892 if (CreateProcess(
ad2e33dc
GS
3893 cmd, /* Image path */
3894 args, /* Arguments for command line */
3895 NULL, /* Default process security */
3896 NULL, /* Default thread security */
3897 FALSE, /* Must be TRUE to use std handles */
3898 NORMAL_PRIORITY_CLASS, /* No special scheduling */
33005217
JD
3899 env, /* Inherit our environment block */
3900 dir, /* Inherit our currrent directory */
ad2e33dc
GS
3901 &stStartInfo, /* -> Startup info */
3902 &stProcInfo)) /* <- Process info (if OK) */
3903 {
922b1888
GS
3904 int pid = (int)stProcInfo.dwProcessId;
3905 if (IsWin95() && pid < 0)
3906 pid = -pid;
3907 sv_setiv(ST(2), pid);
ad2e33dc 3908 CloseHandle(stProcInfo.hThread);/* library source code does this. */
ad2e33dc
GS
3909 bSuccess = TRUE;
3910 }
33005217
JD
3911 PerlEnv_free_childenv(env);
3912 PerlEnv_free_childdir(dir);
ad2e33dc
GS
3913 XSRETURN_IV(bSuccess);
3914}
3915
3916static
3917XS(w32_GetTickCount)
3918{
3919 dXSARGS;
fdb068fa 3920 DWORD msec = GetTickCount();
a6c40364 3921 EXTEND(SP,1);
fdb068fa
JD
3922 if ((IV)msec > 0)
3923 XSRETURN_IV(msec);
3924 XSRETURN_NV(msec);
ad2e33dc
GS
3925}
3926
3927static
3928XS(w32_GetShortPathName)
3929{
3930 dXSARGS;
3931 SV *shortpath;
e8bab181 3932 DWORD len;
ad2e33dc 3933
9404a519 3934 if (items != 1)
4f63d024 3935 Perl_croak(aTHX_ "usage: Win32::GetShortPathName($longPathName)");
ad2e33dc
GS
3936
3937 shortpath = sv_mortalcopy(ST(0));
3938 SvUPGRADE(shortpath, SVt_PV);
631c0b04
GS
3939 if (!SvPVX(shortpath) || !SvLEN(shortpath))
3940 XSRETURN_UNDEF;
3941
ad2e33dc 3942 /* src == target is allowed */
e8bab181
GS
3943 do {
3944 len = GetShortPathName(SvPVX(shortpath),
3945 SvPVX(shortpath),
3946 SvLEN(shortpath));
3947 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
3948 if (len) {
3949 SvCUR_set(shortpath,len);
ad2e33dc 3950 ST(0) = shortpath;
bb897dfc 3951 XSRETURN(1);
e8bab181 3952 }
3467312b 3953 XSRETURN_UNDEF;
ad2e33dc
GS
3954}
3955
ad0751ec 3956static
ca135624
JD
3957XS(w32_GetFullPathName)
3958{
3959 dXSARGS;
3960 SV *filename;
3961 SV *fullpath;
3962 char *filepart;
3963 DWORD len;
3964
3965 if (items != 1)
4f63d024 3966 Perl_croak(aTHX_ "usage: Win32::GetFullPathName($filename)");
ca135624
JD
3967
3968 filename = ST(0);
3969 fullpath = sv_mortalcopy(filename);
3970 SvUPGRADE(fullpath, SVt_PV);
631c0b04
GS
3971 if (!SvPVX(fullpath) || !SvLEN(fullpath))
3972 XSRETURN_UNDEF;
3973
ca135624
JD
3974 do {
3975 len = GetFullPathName(SvPVX(filename),
3976 SvLEN(fullpath),
3977 SvPVX(fullpath),
3978 &filepart);
3979 } while (len >= SvLEN(fullpath) && sv_grow(fullpath,len+1));
3980 if (len) {
3981 if (GIMME_V == G_ARRAY) {
3982 EXTEND(SP,1);
bb897dfc 3983 XST_mPV(1,filepart);
ca135624
JD
3984 len = filepart - SvPVX(fullpath);
3985 items = 2;
3986 }
3987 SvCUR_set(fullpath,len);
3988 ST(0) = fullpath;
bb897dfc 3989 XSRETURN(items);
ca135624 3990 }
bb897dfc 3991 XSRETURN_EMPTY;
ca135624
JD
3992}
3993
3994static
8ac9c18d
GS
3995XS(w32_GetLongPathName)
3996{
3997 dXSARGS;
3998 SV *path;
3999 char tmpbuf[MAX_PATH+1];
4000 char *pathstr;
4001 STRLEN len;
4002
4003 if (items != 1)
4f63d024 4004 Perl_croak(aTHX_ "usage: Win32::GetLongPathName($pathname)");
8ac9c18d
GS
4005
4006 path = ST(0);
4007 pathstr = SvPV(path,len);
4008 strcpy(tmpbuf, pathstr);
4009 pathstr = win32_longpath(tmpbuf);
4010 if (pathstr) {
4011 ST(0) = sv_2mortal(newSVpvn(pathstr, strlen(pathstr)));
4012 XSRETURN(1);
4013 }
4014 XSRETURN_EMPTY;
4015}
4016
4017static
ad0751ec
GS
4018XS(w32_Sleep)
4019{
4020 dXSARGS;
4021 if (items != 1)
4f63d024 4022 Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)");
ad0751ec
GS
4023 Sleep(SvIV(ST(0)));
4024 XSRETURN_YES;
4025}
4026
7509b657
GS
4027static
4028XS(w32_CopyFile)
4029{
4030 dXSARGS;
7766f137 4031 BOOL bResult;
7509b657 4032 if (items != 3)
4f63d024 4033 Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)");
7766f137 4034 if (USING_WIDE()) {
82867ecf
GS
4035 WCHAR wSourceFile[MAX_PATH+1];
4036 WCHAR wDestFile[MAX_PATH+1];
7766f137
GS
4037 A2WHELPER(SvPV_nolen(ST(0)), wSourceFile, sizeof(wSourceFile));
4038 wcscpy(wSourceFile, PerlDir_mapW(wSourceFile));
4039 A2WHELPER(SvPV_nolen(ST(1)), wDestFile, sizeof(wDestFile));
4040 bResult = CopyFileW(wSourceFile, PerlDir_mapW(wDestFile), !SvTRUE(ST(2)));
4041 }
4042 else {
82867ecf 4043 char szSourceFile[MAX_PATH+1];
7766f137
GS
4044 strcpy(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(0))));
4045 bResult = CopyFileA(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(1))), !SvTRUE(ST(2)));
4046 }
4047
4048 if (bResult)
7509b657
GS
4049 XSRETURN_YES;
4050 XSRETURN_NO;
4051}
4052
ad2e33dc 4053void
c5be433b 4054Perl_init_os_extras(void)
ad2e33dc 4055{
acfe0abc 4056 dTHX;
ad2e33dc
GS
4057 char *file = __FILE__;
4058 dXSUB_SYS;
4059
ad2e33dc
GS
4060 /* these names are Activeware compatible */
4061 newXS("Win32::GetCwd", w32_GetCwd, file);
4062 newXS("Win32::SetCwd", w32_SetCwd, file);
4063 newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
4064 newXS("Win32::GetLastError", w32_GetLastError, file);
ca135624 4065 newXS("Win32::SetLastError", w32_SetLastError, file);
ad2e33dc
GS
4066 newXS("Win32::LoginName", w32_LoginName, file);
4067 newXS("Win32::NodeName", w32_NodeName, file);
4068 newXS("Win32::DomainName", w32_DomainName, file);
4069 newXS("Win32::FsType", w32_FsType, file);
4070 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
4071 newXS("Win32::IsWinNT", w32_IsWinNT, file);
4072 newXS("Win32::IsWin95", w32_IsWin95, file);
4073 newXS("Win32::FormatMessage", w32_FormatMessage, file);
4074 newXS("Win32::Spawn", w32_Spawn, file);
4075 newXS("Win32::GetTickCount", w32_GetTickCount, file);
4076 newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
ca135624 4077 newXS("Win32::GetFullPathName", w32_GetFullPathName, file);
8ac9c18d 4078 newXS("Win32::GetLongPathName", w32_GetLongPathName, file);
7509b657 4079 newXS("Win32::CopyFile", w32_CopyFile, file);
ad0751ec 4080 newXS("Win32::Sleep", w32_Sleep, file);
ad2e33dc
GS
4081
4082 /* XXX Bloat Alert! The following Activeware preloads really
4083 * ought to be part of Win32::Sys::*, so they're not included
4084 * here.
4085 */
4086 /* LookupAccountName
4087 * LookupAccountSID
4088 * InitiateSystemShutdown
4089 * AbortSystemShutdown
4090 * ExpandEnvrironmentStrings
4091 */
4092}
4093
4094void
4095Perl_win32_init(int *argcp, char ***argvp)
4096{
4097 /* Disable floating point errors, Perl will trap the ones we
4098 * care about. VC++ RTL defaults to switching these off
4099 * already, but the Borland RTL doesn't. Since we don't
4100 * want to be at the vendor's whim on the default, we set
4101 * it explicitly here.
4102 */
a835ef8a 4103#if !defined(_ALPHA_) && !defined(__GNUC__)
ad2e33dc 4104 _control87(MCW_EM, MCW_EM);
3dc9191e 4105#endif
4b556e6c 4106 MALLOC_INIT;
ad2e33dc 4107}
d55594ae 4108
635bbe87
GS
4109void
4110win32_get_child_IO(child_IO_table* ptbl)
4111{
4112 ptbl->childStdIn = GetStdHandle(STD_INPUT_HANDLE);
4113 ptbl->childStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
4114 ptbl->childStdErr = GetStdHandle(STD_ERROR_HANDLE);
4115}
4116
52853b95 4117#ifdef HAVE_INTERP_INTERN
7766f137 4118
7766f137 4119void
52853b95
GS
4120Perl_sys_intern_init(pTHX)
4121{
4122 w32_perlshell_tokens = Nullch;
4123 w32_perlshell_vec = (char**)NULL;
4124 w32_perlshell_items = 0;
4125 w32_fdpid = newAV();
4126 New(1313, w32_children, 1, child_tab);
4127 w32_num_children = 0;
4128# ifdef USE_ITHREADS
4129 w32_pseudo_id = 0;
4130 New(1313, w32_pseudo_children, 1, child_tab);
4131 w32_num_pseudo_children = 0;
4132# endif
4133 w32_init_socktype = 0;
4134}
4135
3dbbd0f5
GS
4136void
4137Perl_sys_intern_clear(pTHX)
4138{
4139 Safefree(w32_perlshell_tokens);
4140 Safefree(w32_perlshell_vec);
4141 /* NOTE: w32_fdpid is freed by sv_clean_all() */
4142 Safefree(w32_children);
4143# ifdef USE_ITHREADS
4144 Safefree(w32_pseudo_children);
4145# endif
4146}
4147
52853b95
GS
4148# ifdef USE_ITHREADS
4149
4150void
7766f137
GS
4151Perl_sys_intern_dup(pTHX_ struct interp_intern *src, struct interp_intern *dst)
4152{
4153 dst->perlshell_tokens = Nullch;
4154 dst->perlshell_vec = (char**)NULL;
4155 dst->perlshell_items = 0;
4156 dst->fdpid = newAV();
4157 Newz(1313, dst->children, 1, child_tab);
7766f137 4158 dst->pseudo_id = 0;
52853b95 4159 Newz(1313, dst->pseudo_children, 1, child_tab);
862f1e8c 4160 dst->thr_intern.Winit_socktype = 0;
7766f137 4161}
52853b95
GS
4162# endif /* USE_ITHREADS */
4163#endif /* HAVE_INTERP_INTERN */
7766f137 4164
729a02f2 4165static void
acfe0abc 4166win32_free_argvw(pTHX_ void *ptr)
729a02f2
GS
4167{
4168 char** argv = (char**)ptr;
4169 while(*argv) {
4170 Safefree(*argv);
4171 *argv++ = Nullch;
4172 }
4173}
4174
4175void
c0932edc 4176win32_argv2utf8(int argc, char** argv)
729a02f2 4177{
acfe0abc 4178 dTHX;
729a02f2
GS
4179 char* psz;
4180 int length, wargc;
4181 LPWSTR* lpwStr = CommandLineToArgvW(GetCommandLineW(), &wargc);
4182 if (lpwStr && argc) {
4183 while (argc--) {
4184 length = WideCharToMultiByte(CP_UTF8, 0, lpwStr[--wargc], -1, NULL, 0, NULL, NULL);
4185 Newz(0, psz, length, char);
4186 WideCharToMultiByte(CP_UTF8, 0, lpwStr[wargc], -1, psz, length, NULL, NULL);
4187 argv[argc] = psz;
4188 }
4189 call_atexit(win32_free_argvw, argv);
4190 }
4191 GlobalFree((HGLOBAL)lpwStr);
4192}
4193