Re: [perl #37688] Unexpected regex failure.
[p5sagit/p5-mst-13.2.git] / wince / wince.c
CommitLineData
e1caacb4 1/* WINCE.C - stuff for Windows CE
2 *
ca6c63e1 3 * Time-stamp: <26/10/01 15:25:20 keuchel@keuchelnt>
e1caacb4 4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 */
8
9#define WIN32_LEAN_AND_MEAN
10#define WIN32IO_IS_STDIO
11#include <windows.h>
216db7ee 12#include <signal.h>
e1caacb4 13
14#define PERLIO_NOT_STDIO 0
15
16#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
17#define PerlIO FILE
18#endif
19
20#define wince_private
21#include "errno.h"
22
23#include "EXTERN.h"
24#include "perl.h"
25
26#define NO_XSLOCKS
27#define PERL_NO_GET_CONTEXT
28#include "XSUB.h"
29
30#include "win32iop.h"
31#include <string.h>
32#include <stdarg.h>
33#include <float.h>
34#include <shellapi.h>
814ffeea 35#include <process.h>
e1caacb4 36
37#define perl
38#include "celib_defs.h"
39#include "cewin32.h"
40#include "cecrt.h"
41#include "cewin32_defs.h"
42#include "cecrt_defs.h"
43
94081db1 44#define GetCurrentDirectoryW XCEGetCurrentDirectoryW
45
e1caacb4 46#ifdef PALM_SIZE
47#include "stdio-palmsize.h"
48#endif
49
50#define EXECF_EXEC 1
51#define EXECF_SPAWN 2
52#define EXECF_SPAWN_NOWAIT 3
53
54#if defined(PERL_IMPLICIT_SYS)
55# undef win32_get_privlib
56# define win32_get_privlib g_win32_get_privlib
57# undef win32_get_sitelib
58# define win32_get_sitelib g_win32_get_sitelib
59# undef win32_get_vendorlib
60# define win32_get_vendorlib g_win32_get_vendorlib
61# undef do_spawn
62# define do_spawn g_do_spawn
63# undef getlogin
64# define getlogin g_getlogin
65#endif
66
814ffeea 67static void get_shell(void);
68static long tokenize(const char *str, char **dest, char ***destv);
69static int do_spawn2(pTHX_ char *cmd, int exectype);
70static BOOL has_shell_metachars(char *ptr);
e1caacb4 71static long filetime_to_clock(PFILETIME ft);
72static BOOL filetime_from_time(PFILETIME ft, time_t t);
73static char * get_emd_part(SV **leading, char *trailing, ...);
814ffeea 74static void remove_dead_process(long deceased);
75static long find_pid(int pid);
76static char * qualified_path(const char *cmd);
e1caacb4 77static char * win32_get_xlib(const char *pl, const char *xlib,
78 const char *libname);
79
814ffeea 80#ifdef USE_ITHREADS
81static void remove_dead_pseudo_process(long child);
82static long find_pseudo_pid(int pid);
83#endif
84
85int _fmode = O_TEXT; /* celib do not provide _fmode, so we define it here */
86
e1caacb4 87START_EXTERN_C
88HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
89char w32_module_name[MAX_PATH+1];
90END_EXTERN_C
91
92static DWORD w32_platform = (DWORD)-1;
93
94int
95IsWin95(void)
96{
97 return (win32_os_id() == VER_PLATFORM_WIN32_WINDOWS);
98}
99
100int
101IsWinNT(void)
102{
103 return (win32_os_id() == VER_PLATFORM_WIN32_NT);
104}
105
106int
107IsWinCE(void)
108{
109 return (win32_os_id() == VER_PLATFORM_WIN32_CE);
110}
111
112EXTERN_C void
113set_w32_module_name(void)
114{
115 char* ptr;
116 XCEGetModuleFileNameA((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
117 ? XCEGetModuleHandleA(NULL)
118 : w32_perldll_handle),
119 w32_module_name, sizeof(w32_module_name));
120
121 /* normalize to forward slashes */
122 ptr = w32_module_name;
123 while (*ptr) {
124 if (*ptr == '\\')
125 *ptr = '/';
126 ++ptr;
127 }
128}
129
130/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
131static char*
132get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
133{
134 /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
135 HKEY handle;
136 DWORD type;
137 const char *subkey = "Software\\Perl";
138 char *str = Nullch;
139 long retval;
140
141 retval = XCERegOpenKeyExA(hkey, subkey, 0, KEY_READ, &handle);
142 if (retval == ERROR_SUCCESS) {
143 DWORD datalen;
144 retval = XCERegQueryValueExA(handle, valuename, 0, &type, NULL, &datalen);
145 if (retval == ERROR_SUCCESS && type == REG_SZ) {
acfe0abc 146 dTHX;
e1caacb4 147 if (!*svp)
148 *svp = sv_2mortal(newSVpvn("",0));
149 SvGROW(*svp, datalen);
150 retval = XCERegQueryValueExA(handle, valuename, 0, NULL,
151 (PBYTE)SvPVX(*svp), &datalen);
152 if (retval == ERROR_SUCCESS) {
153 str = SvPVX(*svp);
154 SvCUR_set(*svp,datalen-1);
155 }
156 }
157 RegCloseKey(handle);
158 }
159 return str;
160}
161
162/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
163static char*
164get_regstr(const char *valuename, SV **svp)
165{
166 char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
167 if (!str)
168 str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
169 return str;
170}
171
172/* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
173static char *
174get_emd_part(SV **prev_pathp, char *trailing_path, ...)
175{
176 char base[10];
177 va_list ap;
178 char mod_name[MAX_PATH+1];
179 char *ptr;
180 char *optr;
181 char *strip;
182 int oldsize, newsize;
183 STRLEN baselen;
184
185 va_start(ap, trailing_path);
186 strip = va_arg(ap, char *);
187
188 sprintf(base, "%d.%d", (int)PERL_REVISION, (int)PERL_VERSION);
189 baselen = strlen(base);
190
191 if (!*w32_module_name) {
192 set_w32_module_name();
193 }
194 strcpy(mod_name, w32_module_name);
195 ptr = strrchr(mod_name, '/');
196 while (ptr && strip) {
197 /* look for directories to skip back */
198 optr = ptr;
199 *ptr = '\0';
200 ptr = strrchr(mod_name, '/');
201 /* avoid stripping component if there is no slash,
202 * or it doesn't match ... */
203 if (!ptr || stricmp(ptr+1, strip) != 0) {
204 /* ... but not if component matches m|5\.$patchlevel.*| */
205 if (!ptr || !(*strip == '5' && *(ptr+1) == '5'
206 && strncmp(strip, base, baselen) == 0
207 && strncmp(ptr+1, base, baselen) == 0))
208 {
209 *optr = '/';
210 ptr = optr;
211 }
212 }
213 strip = va_arg(ap, char *);
214 }
215 if (!ptr) {
216 ptr = mod_name;
217 *ptr++ = '.';
218 *ptr = '/';
219 }
220 va_end(ap);
221 strcpy(++ptr, trailing_path);
222
223 /* only add directory if it exists */
224 if (XCEGetFileAttributesA(mod_name) != (DWORD) -1) {
225 /* directory exists */
acfe0abc 226 dTHX;
e1caacb4 227 if (!*prev_pathp)
228 *prev_pathp = sv_2mortal(newSVpvn("",0));
229 sv_catpvn(*prev_pathp, ";", 1);
230 sv_catpv(*prev_pathp, mod_name);
231 return SvPVX(*prev_pathp);
232 }
233
234 return Nullch;
235}
236
237char *
238win32_get_privlib(const char *pl)
239{
acfe0abc 240 dTHX;
e1caacb4 241 char *stdlib = "lib";
242 char buffer[MAX_PATH+1];
243 SV *sv = Nullsv;
244
245 /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
246 sprintf(buffer, "%s-%s", stdlib, pl);
247 if (!get_regstr(buffer, &sv))
248 (void)get_regstr(stdlib, &sv);
249
250 /* $stdlib .= ";$EMD/../../lib" */
251 return get_emd_part(&sv, stdlib, ARCHNAME, "bin", Nullch);
252}
253
254static char *
255win32_get_xlib(const char *pl, const char *xlib, const char *libname)
256{
acfe0abc 257 dTHX;
e1caacb4 258 char regstr[40];
259 char pathstr[MAX_PATH+1];
260 DWORD datalen;
261 int len, newsize;
262 SV *sv1 = Nullsv;
263 SV *sv2 = Nullsv;
264
265 /* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */
266 sprintf(regstr, "%s-%s", xlib, pl);
267 (void)get_regstr(regstr, &sv1);
268
269 /* $xlib .=
270 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */
271 sprintf(pathstr, "%s/%s/lib", libname, pl);
272 (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
273
274 /* $HKCU{$xlib} || $HKLM{$xlib} . ---; */
275 (void)get_regstr(xlib, &sv2);
276
277 /* $xlib .=
278 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */
279 sprintf(pathstr, "%s/lib", libname);
280 (void)get_emd_part(&sv2, pathstr, ARCHNAME, "bin", pl, Nullch);
281
282 if (!sv1 && !sv2)
283 return Nullch;
284 if (!sv1)
285 return SvPVX(sv2);
286 if (!sv2)
287 return SvPVX(sv1);
288
289 sv_catpvn(sv1, ";", 1);
290 sv_catsv(sv1, sv2);
291
292 return SvPVX(sv1);
293}
294
295char *
296win32_get_sitelib(const char *pl)
297{
298 return win32_get_xlib(pl, "sitelib", "site");
299}
300
301#ifndef PERL_VENDORLIB_NAME
302# define PERL_VENDORLIB_NAME "vendor"
303#endif
304
305char *
306win32_get_vendorlib(const char *pl)
307{
308 return win32_get_xlib(pl, "vendorlib", PERL_VENDORLIB_NAME);
309}
310
814ffeea 311static BOOL
312has_shell_metachars(char *ptr)
313{
314 int inquote = 0;
315 char quote = '\0';
316
317 /*
318 * Scan string looking for redirection (< or >) or pipe
319 * characters (|) that are not in a quoted string.
320 * Shell variable interpolation (%VAR%) can also happen inside strings.
321 */
322 while (*ptr) {
323 switch(*ptr) {
324 case '%':
325 return TRUE;
326 case '\'':
327 case '\"':
328 if (inquote) {
329 if (quote == *ptr) {
330 inquote = 0;
331 quote = '\0';
332 }
333 }
334 else {
335 quote = *ptr;
336 inquote++;
337 }
338 break;
339 case '>':
340 case '<':
341 case '|':
342 if (!inquote)
343 return TRUE;
344 default:
345 break;
346 }
347 ++ptr;
348 }
349 return FALSE;
350}
351
e1caacb4 352#if !defined(PERL_IMPLICIT_SYS)
353/* since the current process environment is being updated in util.c
354 * the library functions will get the correct environment
355 */
356PerlIO *
357Perl_my_popen(pTHX_ char *cmd, char *mode)
358{
359 printf("popen(%s)\n", cmd);
360
361 Perl_croak(aTHX_ PL_no_func, "popen");
362 return NULL;
363}
364
365long
366Perl_my_pclose(pTHX_ PerlIO *fp)
367{
368 Perl_croak(aTHX_ PL_no_func, "pclose");
369 return -1;
370}
371#endif
372
373DllExport unsigned long
374win32_os_id(void)
375{
376 static OSVERSIONINFOA osver;
377
378 if (osver.dwPlatformId != w32_platform) {
379 memset(&osver, 0, sizeof(OSVERSIONINFOA));
380 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
381 XCEGetVersionExA(&osver);
382 w32_platform = osver.dwPlatformId;
383 }
384 return (unsigned long)w32_platform;
385}
386
387DllExport int
388win32_getpid(void)
389{
814ffeea 390 int pid;
391#ifdef USE_ITHREADS
392 dTHX;
393 if (w32_pseudo_id)
394 return -((int)w32_pseudo_id);
395#endif
396 pid = xcegetpid();
397 return pid;
e1caacb4 398}
399
814ffeea 400/* Tokenize a string. Words are null-separated, and the list
401 * ends with a doubled null. Any character (except null and
402 * including backslash) may be escaped by preceding it with a
403 * backslash (the backslash will be stripped).
404 * Returns number of words in result buffer.
405 */
406static long
407tokenize(const char *str, char **dest, char ***destv)
e1caacb4 408{
814ffeea 409 char *retstart = Nullch;
410 char **retvstart = 0;
411 int items = -1;
412 if (str) {
413 dTHX;
414 int slen = strlen(str);
415 register char *ret;
416 register char **retv;
a02a5408 417 Newx(ret, slen+2, char);
418 Newx(retv, (slen+3)/2, char*);
814ffeea 419
420 retstart = ret;
421 retvstart = retv;
422 *retv = ret;
423 items = 0;
424 while (*str) {
425 *ret = *str++;
426 if (*ret == '\\' && *str)
427 *ret = *str++;
428 else if (*ret == ' ') {
429 while (*str == ' ')
430 str++;
431 if (ret == retstart)
432 ret--;
433 else {
434 *ret = '\0';
435 ++items;
436 if (*str)
437 *++retv = ret+1;
438 }
439 }
440 else if (!*str)
441 ++items;
442 ret++;
443 }
444 retvstart[items] = Nullch;
445 *ret++ = '\0';
446 *ret = '\0';
447 }
448 *dest = retstart;
449 *destv = retvstart;
450 return items;
e1caacb4 451}
452
453DllExport int
454win32_pipe(int *pfd, unsigned int size, int mode)
455{
216db7ee 456 dTHX;
e1caacb4 457 Perl_croak(aTHX_ PL_no_func, "pipe");
458 return -1;
459}
460
461DllExport int
462win32_times(struct tms *timebuf)
463{
216db7ee 464 dTHX;
e1caacb4 465 Perl_croak(aTHX_ PL_no_func, "times");
466 return -1;
467}
468
42165d27 469/* TODO */
216db7ee 470Sighandler_t
471win32_signal(int sig, Sighandler_t subcode)
42165d27 472{
216db7ee 473 dTHX;
42165d27 474 Perl_croak_nocontext("signal() TBD on this platform");
475 return FALSE;
476}
216db7ee 477
814ffeea 478static void
479get_shell(void)
480{
481 dTHX;
482 if (!w32_perlshell_tokens) {
483 /* we don't use COMSPEC here for two reasons:
484 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
485 * uncontrolled unportability of the ensuing scripts.
486 * 2. PERL5SHELL could be set to a shell that may not be fit for
487 * interactive use (which is what most programs look in COMSPEC
488 * for).
489 */
490 const char* defaultshell = (IsWinNT()
491 ? "cmd.exe /x/d/c" : "command.com /c");
492 const char *usershell = PerlEnv_getenv("PERL5SHELL");
493 w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
494 &w32_perlshell_tokens,
495 &w32_perlshell_vec);
496 }
497}
498
499int
500Perl_do_aspawn(pTHX_ SV *really, SV **mark, SV **sp)
501{
502 Perl_croak(aTHX_ PL_no_func, "aspawn");
503 return -1;
504}
42165d27 505
814ffeea 506/* returns pointer to the next unquoted space or the end of the string */
507static char*
508find_next_space(const char *s)
e1caacb4 509{
814ffeea 510 bool in_quotes = FALSE;
511 while (*s) {
512 /* ignore doubled backslashes, or backslash+quote */
513 if (*s == '\\' && (s[1] == '\\' || s[1] == '"')) {
514 s += 2;
515 }
516 /* keep track of when we're within quotes */
517 else if (*s == '"') {
518 s++;
519 in_quotes = !in_quotes;
520 }
521 /* break it up only at spaces that aren't in quotes */
522 else if (!in_quotes && isSPACE(*s))
523 return (char*)s;
524 else
525 s++;
526 }
527 return (char*)s;
528}
529
530#if 1
531static int
532do_spawn2(pTHX_ char *cmd, int exectype)
533{
534 char **a;
535 char *s;
536 char **argv;
537 int status = -1;
538 BOOL needToTry = TRUE;
539 char *cmd2;
540
541 /* Save an extra exec if possible. See if there are shell
542 * metacharacters in it */
543 if (!has_shell_metachars(cmd)) {
a02a5408 544 Newx(argv, strlen(cmd) / 2 + 2, char*);
545 Newx(cmd2, strlen(cmd) + 1, char);
814ffeea 546 strcpy(cmd2, cmd);
547 a = argv;
548 for (s = cmd2; *s;) {
549 while (*s && isSPACE(*s))
550 s++;
551 if (*s)
552 *(a++) = s;
553 s = find_next_space(s);
554 if (*s)
555 *s++ = '\0';
556 }
557 *a = Nullch;
558 if (argv[0]) {
559 switch (exectype) {
560 case EXECF_SPAWN:
561 status = win32_spawnvp(P_WAIT, argv[0],
562 (const char* const*)argv);
563 break;
564 case EXECF_SPAWN_NOWAIT:
565 status = win32_spawnvp(P_NOWAIT, argv[0],
566 (const char* const*)argv);
567 break;
568 case EXECF_EXEC:
569 status = win32_execvp(argv[0], (const char* const*)argv);
570 break;
571 }
572 if (status != -1 || errno == 0)
573 needToTry = FALSE;
574 }
575 Safefree(argv);
576 Safefree(cmd2);
577 }
578 if (needToTry) {
579 char **argv;
580 int i = -1;
581 get_shell();
a02a5408 582 Newx(argv, w32_perlshell_items + 2, char*);
814ffeea 583 while (++i < w32_perlshell_items)
584 argv[i] = w32_perlshell_vec[i];
585 argv[i++] = cmd;
586 argv[i] = Nullch;
587 switch (exectype) {
588 case EXECF_SPAWN:
589 status = win32_spawnvp(P_WAIT, argv[0],
590 (const char* const*)argv);
591 break;
592 case EXECF_SPAWN_NOWAIT:
593 status = win32_spawnvp(P_NOWAIT, argv[0],
594 (const char* const*)argv);
595 break;
596 case EXECF_EXEC:
597 status = win32_execvp(argv[0], (const char* const*)argv);
598 break;
599 }
600 cmd = argv[0];
601 Safefree(argv);
602 }
603 if (exectype == EXECF_SPAWN_NOWAIT) {
604 if (IsWin95())
605 PL_statusvalue = -1; /* >16bits hint for pp_system() */
606 }
607 else {
608 if (status < 0) {
609 if (ckWARN(WARN_EXEC))
610 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't %s \"%s\": %s",
611 (exectype == EXECF_EXEC ? "exec" : "spawn"),
612 cmd, strerror(errno));
613 status = 255 * 256;
614 }
615 else
616 status *= 256;
617 PL_statusvalue = status;
618 }
619 return (status);
620}
621
622int
623Perl_do_spawn(pTHX_ char *cmd)
624{
625 return do_spawn2(aTHX_ cmd, EXECF_SPAWN);
626}
627
628int
629Perl_do_spawn_nowait(pTHX_ char *cmd)
630{
631 return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT);
632}
633
634bool
635Perl_do_exec(pTHX_ char *cmd)
636{
637 do_spawn2(aTHX_ cmd, EXECF_EXEC);
638 return FALSE;
639}
640
641/* The idea here is to read all the directory names into a string table
642 * (separated by nulls) and when one of the other dir functions is called
643 * return the pointer to the current file name.
644 */
645DllExport DIR *
0e06f75d 646win32_opendir(const char *filename)
814ffeea 647{
648 dTHX;
649 DIR *dirp;
650 long len;
651 long idx;
652 char scanname[MAX_PATH+3];
653 Stat_t sbuf;
654 WIN32_FIND_DATAA aFindData;
655 WIN32_FIND_DATAW wFindData;
656 HANDLE fh;
657 char buffer[MAX_PATH*2];
658 WCHAR wbuffer[MAX_PATH+1];
659 char* ptr;
660
661 len = strlen(filename);
662 if (len > MAX_PATH)
663 return NULL;
664
665 /* check to see if filename is a directory */
666 if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
667 return NULL;
668
669 /* Get us a DIR structure */
a02a5408 670 Newxz(dirp, 1, DIR);
814ffeea 671
672 /* Create the search pattern */
673 strcpy(scanname, filename);
674
675 /* bare drive name means look in cwd for drive */
676 if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') {
677 scanname[len++] = '.';
678 scanname[len++] = '/';
679 }
680 else if (scanname[len-1] != '/' && scanname[len-1] != '\\') {
681 scanname[len++] = '/';
682 }
683 scanname[len++] = '*';
684 scanname[len] = '\0';
685
686 /* do the FindFirstFile call */
687 fh = FindFirstFile(PerlDir_mapA(scanname), &aFindData);
688 dirp->handle = fh;
689 if (fh == INVALID_HANDLE_VALUE) {
690 DWORD err = GetLastError();
691 /* FindFirstFile() fails on empty drives! */
692 switch (err) {
693 case ERROR_FILE_NOT_FOUND:
694 return dirp;
695 case ERROR_NO_MORE_FILES:
696 case ERROR_PATH_NOT_FOUND:
697 errno = ENOENT;
698 break;
699 case ERROR_NOT_ENOUGH_MEMORY:
700 errno = ENOMEM;
701 break;
702 default:
703 errno = EINVAL;
704 break;
705 }
706 Safefree(dirp);
707 return NULL;
708 }
709
710 /* now allocate the first part of the string table for
711 * the filenames that we find.
712 */
713 ptr = aFindData.cFileName;
714 idx = strlen(ptr)+1;
715 if (idx < 256)
716 dirp->size = 128;
717 else
718 dirp->size = idx;
a02a5408 719 Newx(dirp->start, dirp->size, char);
814ffeea 720 strcpy(dirp->start, ptr);
721 dirp->nfiles++;
722 dirp->end = dirp->curr = dirp->start;
723 dirp->end += idx;
724 return dirp;
725}
726
727
728/* Readdir just returns the current string pointer and bumps the
729 * string pointer to the nDllExport entry.
730 */
731DllExport struct direct *
732win32_readdir(DIR *dirp)
733{
734 long len;
735
736 if (dirp->curr) {
737 /* first set up the structure to return */
738 len = strlen(dirp->curr);
739 strcpy(dirp->dirstr.d_name, dirp->curr);
740 dirp->dirstr.d_namlen = len;
741
742 /* Fake an inode */
743 dirp->dirstr.d_ino = dirp->curr - dirp->start;
744
745 /* Now set up for the next call to readdir */
746 dirp->curr += len + 1;
747 if (dirp->curr >= dirp->end) {
748 dTHX;
749 char* ptr;
750 BOOL res;
751 WIN32_FIND_DATAW wFindData;
752 WIN32_FIND_DATAA aFindData;
753 char buffer[MAX_PATH*2];
754
755 /* finding the next file that matches the wildcard
756 * (which should be all of them in this directory!).
757 */
758 res = FindNextFile(dirp->handle, &aFindData);
759 if (res)
760 ptr = aFindData.cFileName;
761 if (res) {
762 long endpos = dirp->end - dirp->start;
763 long newsize = endpos + strlen(ptr) + 1;
764 /* bump the string table size by enough for the
765 * new name and its null terminator */
766 while (newsize > dirp->size) {
767 long curpos = dirp->curr - dirp->start;
768 dirp->size *= 2;
769 Renew(dirp->start, dirp->size, char);
770 dirp->curr = dirp->start + curpos;
771 }
772 strcpy(dirp->start + endpos, ptr);
773 dirp->end = dirp->start + newsize;
774 dirp->nfiles++;
775 }
776 else
777 dirp->curr = NULL;
778 }
779 return &(dirp->dirstr);
780 }
781 else
782 return NULL;
783}
784
785/* Telldir returns the current string pointer position */
786DllExport long
787win32_telldir(DIR *dirp)
788{
789 return (dirp->curr - dirp->start);
790}
791
792
793/* Seekdir moves the string pointer to a previously saved position
794 * (returned by telldir).
795 */
796DllExport void
797win32_seekdir(DIR *dirp, long loc)
798{
799 dirp->curr = dirp->start + loc;
800}
801
802/* Rewinddir resets the string pointer to the start */
803DllExport void
804win32_rewinddir(DIR *dirp)
805{
806 dirp->curr = dirp->start;
807}
808
809/* free the memory allocated by opendir */
810DllExport int
811win32_closedir(DIR *dirp)
812{
813 dTHX;
814 if (dirp->handle != INVALID_HANDLE_VALUE)
815 FindClose(dirp->handle);
816 Safefree(dirp->start);
817 Safefree(dirp);
818 return 1;
e1caacb4 819}
820
814ffeea 821#else
822/////!!!!!!!!!!! return here and do right stuff!!!!
823
e1caacb4 824DllExport DIR *
0e06f75d 825win32_opendir(const char *filename)
e1caacb4 826{
827 return opendir(filename);
828}
829
830DllExport struct direct *
831win32_readdir(DIR *dirp)
832{
833 return readdir(dirp);
834}
835
836DllExport long
837win32_telldir(DIR *dirp)
838{
216db7ee 839 dTHX;
e1caacb4 840 Perl_croak(aTHX_ PL_no_func, "telldir");
841 return -1;
842}
843
844DllExport void
845win32_seekdir(DIR *dirp, long loc)
846{
216db7ee 847 dTHX;
e1caacb4 848 Perl_croak(aTHX_ PL_no_func, "seekdir");
849}
850
851DllExport void
852win32_rewinddir(DIR *dirp)
853{
216db7ee 854 dTHX;
e1caacb4 855 Perl_croak(aTHX_ PL_no_func, "rewinddir");
856}
857
858DllExport int
859win32_closedir(DIR *dirp)
860{
861 closedir(dirp);
862 return 0;
863}
814ffeea 864#endif // 1
e1caacb4 865
866DllExport int
867win32_kill(int pid, int sig)
868{
216db7ee 869 dTHX;
e1caacb4 870 Perl_croak(aTHX_ PL_no_func, "kill");
871 return -1;
872}
873
e1caacb4 874DllExport int
875win32_stat(const char *path, struct stat *sbuf)
876{
877 return xcestat(path, sbuf);
878}
879
880DllExport char *
881win32_longpath(char *path)
882{
883 return path;
884}
885
886#ifndef USE_WIN32_RTL_ENV
887
888DllExport char *
889win32_getenv(const char *name)
890{
891 return xcegetenv(name);
892}
893
894DllExport int
895win32_putenv(const char *name)
896{
897 return xceputenv(name);
898}
899
900#endif
901
902static long
903filetime_to_clock(PFILETIME ft)
904{
905 __int64 qw = ft->dwHighDateTime;
906 qw <<= 32;
907 qw |= ft->dwLowDateTime;
908 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
909 return (long) qw;
910}
911
912/* fix utime() so it works on directories in NT */
913static BOOL
914filetime_from_time(PFILETIME pFileTime, time_t Time)
915{
916 struct tm *pTM = localtime(&Time);
917 SYSTEMTIME SystemTime;
918 FILETIME LocalTime;
919
920 if (pTM == NULL)
921 return FALSE;
922
923 SystemTime.wYear = pTM->tm_year + 1900;
924 SystemTime.wMonth = pTM->tm_mon + 1;
925 SystemTime.wDay = pTM->tm_mday;
926 SystemTime.wHour = pTM->tm_hour;
927 SystemTime.wMinute = pTM->tm_min;
928 SystemTime.wSecond = pTM->tm_sec;
929 SystemTime.wMilliseconds = 0;
930
931 return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
932 LocalFileTimeToFileTime(&LocalTime, pFileTime);
933}
934
935DllExport int
936win32_unlink(const char *filename)
937{
938 return xceunlink(filename);
939}
940
941DllExport int
942win32_utime(const char *filename, struct utimbuf *times)
943{
944 return xceutime(filename, (struct _utimbuf *) times);
945}
946
947DllExport int
e2a02c1e 948win32_gettimeofday(struct timeval *tp, void *not_used)
949{
950 return xcegettimeofday(tp,not_used);
951}
952
953DllExport int
e1caacb4 954win32_uname(struct utsname *name)
955{
956 struct hostent *hep;
957 STRLEN nodemax = sizeof(name->nodename)-1;
958 OSVERSIONINFOA osver;
959
960 memset(&osver, 0, sizeof(OSVERSIONINFOA));
961 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
962 if (XCEGetVersionExA(&osver)) {
963 /* sysname */
964 switch (osver.dwPlatformId) {
965 case VER_PLATFORM_WIN32_CE:
966 strcpy(name->sysname, "Windows CE");
967 break;
968 case VER_PLATFORM_WIN32_WINDOWS:
969 strcpy(name->sysname, "Windows");
970 break;
971 case VER_PLATFORM_WIN32_NT:
972 strcpy(name->sysname, "Windows NT");
973 break;
974 case VER_PLATFORM_WIN32s:
975 strcpy(name->sysname, "Win32s");
976 break;
977 default:
978 strcpy(name->sysname, "Win32 Unknown");
979 break;
980 }
981
982 /* release */
983 sprintf(name->release, "%d.%d",
984 osver.dwMajorVersion, osver.dwMinorVersion);
985
986 /* version */
987 sprintf(name->version, "Build %d",
988 osver.dwPlatformId == VER_PLATFORM_WIN32_NT
989 ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff));
990 if (osver.szCSDVersion[0]) {
991 char *buf = name->version + strlen(name->version);
992 sprintf(buf, " (%s)", osver.szCSDVersion);
993 }
994 }
995 else {
996 *name->sysname = '\0';
997 *name->version = '\0';
998 *name->release = '\0';
999 }
1000
1001 /* nodename */
1002 hep = win32_gethostbyname("localhost");
1003 if (hep) {
1004 STRLEN len = strlen(hep->h_name);
1005 if (len <= nodemax) {
1006 strcpy(name->nodename, hep->h_name);
1007 }
1008 else {
1009 strncpy(name->nodename, hep->h_name, nodemax);
1010 name->nodename[nodemax] = '\0';
1011 }
1012 }
1013 else {
1014 DWORD sz = nodemax;
1015 if (!XCEGetComputerNameA(name->nodename, &sz))
1016 *name->nodename = '\0';
1017 }
1018
1019 /* machine (architecture) */
1020 {
1021 SYSTEM_INFO info;
1022 char *arch;
1023 GetSystemInfo(&info);
1024
e1caacb4 1025 switch (info.wProcessorArchitecture) {
e1caacb4 1026 case PROCESSOR_ARCHITECTURE_INTEL:
1027 arch = "x86"; break;
1028 case PROCESSOR_ARCHITECTURE_MIPS:
1029 arch = "mips"; break;
1030 case PROCESSOR_ARCHITECTURE_ALPHA:
1031 arch = "alpha"; break;
1032 case PROCESSOR_ARCHITECTURE_PPC:
1033 arch = "ppc"; break;
1034 case PROCESSOR_ARCHITECTURE_ARM:
1035 arch = "arm"; break;
1036 case PROCESSOR_HITACHI_SH3:
1037 arch = "sh3"; break;
1038 case PROCESSOR_SHx_SH3:
1039 arch = "sh3"; break;
1040
1041 default:
1042 arch = "unknown"; break;
1043 }
1044 strcpy(name->machine, arch);
1045 }
1046 return 0;
1047}
1048
814ffeea 1049/* Timing related stuff */
1050
1051int
1052do_raise(pTHX_ int sig)
1053{
1054 if (sig < SIG_SIZE) {
1055 Sighandler_t handler = w32_sighandler[sig];
1056 if (handler == SIG_IGN) {
1057 return 0;
1058 }
1059 else if (handler != SIG_DFL) {
1060 (*handler)(sig);
1061 return 0;
1062 }
1063 else {
1064 /* Choose correct default behaviour */
1065 switch (sig) {
1066#ifdef SIGCLD
1067 case SIGCLD:
1068#endif
1069#ifdef SIGCHLD
1070 case SIGCHLD:
1071#endif
1072 case 0:
1073 return 0;
1074 case SIGTERM:
1075 default:
1076 break;
1077 }
1078 }
1079 }
1080 /* Tell caller to exit thread/process as approriate */
1081 return 1;
1082}
1083
216db7ee 1084void
1085sig_terminate(pTHX_ int sig)
1086{
2e332e71 1087 Perl_warn(aTHX_ "Terminating on signal SIG%s(%d)\n",PL_sig_name[sig], sig);
216db7ee 1088 /* exit() seems to be safe, my_exit() or die() is a problem in ^C
1089 thread
1090 */
1091 exit(sig);
1092}
1093
1094DllExport int
1095win32_async_check(pTHX)
1096{
1097 MSG msg;
1098 int ours = 1;
1099 /* Passing PeekMessage -1 as HWND (2nd arg) only get PostThreadMessage() messages
1100 * and ignores window messages - should co-exist better with windows apps e.g. Tk
1101 */
1102 while (PeekMessage(&msg, (HWND)-1, 0, 0, PM_REMOVE|PM_NOYIELD)) {
1103 int sig;
1104 switch(msg.message) {
1105
1106#if 0
1107 /* Perhaps some other messages could map to signals ? ... */
1108 case WM_CLOSE:
1109 case WM_QUIT:
1110 /* Treat WM_QUIT like SIGHUP? */
1111 sig = SIGHUP;
1112 goto Raise;
1113 break;
1114#endif
2e332e71 1115
216db7ee 1116 /* We use WM_USER to fake kill() with other signals */
1117 case WM_USER: {
1118 sig = msg.wParam;
1119 Raise:
1120 if (do_raise(aTHX_ sig)) {
1121 sig_terminate(aTHX_ sig);
1122 }
1123 break;
1124 }
1125
1126 case WM_TIMER: {
1127 /* alarm() is a one-shot but SetTimer() repeats so kill it */
1128 if (w32_timerid) {
1129 KillTimer(NULL,w32_timerid);
1130 w32_timerid=0;
1131 }
1132 /* Now fake a call to signal handler */
1133 if (do_raise(aTHX_ 14)) {
1134 sig_terminate(aTHX_ 14);
1135 }
1136 break;
1137 }
1138
1139 /* Otherwise do normal Win32 thing - in case it is useful */
1140 default:
1141 TranslateMessage(&msg);
1142 DispatchMessage(&msg);
1143 ours = 0;
1144 break;
1145 }
1146 }
1147 w32_poll_count = 0;
1148
1149 /* Above or other stuff may have set a signal flag */
1150 if (PL_sig_pending) {
1151 despatch_signals();
1152 }
1153 return ours;
1154}
1155
1156/* This function will not return until the timeout has elapsed, or until
1157 * one of the handles is ready. */
1158DllExport DWORD
1159win32_msgwait(pTHX_ DWORD count, LPHANDLE handles, DWORD timeout, LPDWORD resultp)
1160{
1161 /* We may need several goes at this - so compute when we stop */
1162 DWORD ticks = 0;
1163 if (timeout != INFINITE) {
1164 ticks = GetTickCount();
1165 timeout += ticks;
1166 }
1167 while (1) {
1168 DWORD result = MsgWaitForMultipleObjects(count,handles,FALSE,timeout-ticks, QS_ALLEVENTS);
1169 if (resultp)
1170 *resultp = result;
1171 if (result == WAIT_TIMEOUT) {
1172 /* Ran out of time - explicit return of zero to avoid -ve if we
1173 have scheduling issues
1174 */
1175 return 0;
1176 }
1177 if (timeout != INFINITE) {
1178 ticks = GetTickCount();
1179 }
1180 if (result == WAIT_OBJECT_0 + count) {
1181 /* Message has arrived - check it */
1182 (void)win32_async_check(aTHX);
1183 }
1184 else {
1185 /* Not timeout or message - one of handles is ready */
1186 break;
1187 }
1188 }
1189 /* compute time left to wait */
1190 ticks = timeout - ticks;
1191 /* If we are past the end say zero */
1192 return (ticks > 0) ? ticks : 0;
1193}
1194
e1caacb4 1195static UINT timerid = 0;
1196
1197static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
1198{
acfe0abc 1199 dTHX;
e1caacb4 1200 KillTimer(NULL,timerid);
1201 timerid=0;
1202 sighandler(14);
1203}
e1caacb4 1204
1205DllExport unsigned int
814ffeea 1206win32_sleep(unsigned int t)
1207{
1208 return xcesleep(t);
1209}
1210
1211DllExport unsigned int
e1caacb4 1212win32_alarm(unsigned int sec)
1213{
e1caacb4 1214 /*
1215 * the 'obvious' implentation is SetTimer() with a callback
1216 * which does whatever receiving SIGALRM would do
1217 * we cannot use SIGALRM even via raise() as it is not
1218 * one of the supported codes in <signal.h>
1219 *
1220 * Snag is unless something is looking at the message queue
1221 * nothing happens :-(
1222 */
acfe0abc 1223 dTHX;
e1caacb4 1224 if (sec)
1225 {
1226 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
1227 if (!timerid)
1228 Perl_croak_nocontext("Cannot set timer");
1229 }
1230 else
1231 {
1232 if (timerid)
1233 {
1234 KillTimer(NULL,timerid);
1235 timerid=0;
1236 }
1237 }
e1caacb4 1238 return 0;
1239}
1240
1241#ifdef HAVE_DES_FCRYPT
1242extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf);
1243#endif
1244
1245DllExport char *
1246win32_crypt(const char *txt, const char *salt)
1247{
acfe0abc 1248 dTHX;
e1caacb4 1249#ifdef HAVE_DES_FCRYPT
1250 dTHR;
1251 return des_fcrypt(txt, salt, w32_crypt_buffer);
1252#else
1253 Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia.");
1254 return Nullch;
1255#endif
1256}
1257
e1caacb4 1258
1259/*
1260 * redirected io subsystem for all XS modules
1261 *
1262 */
1263
1264DllExport int *
1265win32_errno(void)
1266{
1267 return (&errno);
1268}
1269
814ffeea 1270DllExport char ***
1271win32_environ(void)
1272{
1273 return (&(environ));
1274}
1275
e1caacb4 1276/* the rest are the remapped stdio routines */
1277DllExport FILE *
1278win32_stderr(void)
1279{
1280 return (stderr);
1281}
1282
216db7ee 1283char *g_getlogin() {
1284 return "no-getlogin";
1285}
1286
e1caacb4 1287DllExport FILE *
1288win32_stdin(void)
1289{
1290 return (stdin);
1291}
1292
1293DllExport FILE *
1294win32_stdout()
1295{
1296 return (stdout);
1297}
1298
1299DllExport int
1300win32_ferror(FILE *fp)
1301{
1302 return (ferror(fp));
1303}
1304
1305
1306DllExport int
1307win32_feof(FILE *fp)
1308{
1309 return (feof(fp));
1310}
1311
1312/*
1313 * Since the errors returned by the socket error function
1314 * WSAGetLastError() are not known by the library routine strerror
1315 * we have to roll our own.
1316 */
1317
1318DllExport char *
814ffeea 1319win32_strerror(int e)
e1caacb4 1320{
1321 return xcestrerror(e);
1322}
1323
1324DllExport void
1325win32_str_os_error(void *sv, DWORD dwErr)
1326{
acfe0abc 1327 dTHX;
e1caacb4 1328
1329 sv_setpvn((SV*)sv, "Error", 5);
1330}
1331
1332
1333DllExport int
1334win32_fprintf(FILE *fp, const char *format, ...)
1335{
1336 va_list marker;
1337 va_start(marker, format); /* Initialize variable arguments. */
1338
1339 return (vfprintf(fp, format, marker));
1340}
1341
1342DllExport int
1343win32_printf(const char *format, ...)
1344{
1345 va_list marker;
1346 va_start(marker, format); /* Initialize variable arguments. */
1347
1348 return (vprintf(format, marker));
1349}
1350
1351DllExport int
1352win32_vfprintf(FILE *fp, const char *format, va_list args)
1353{
1354 return (vfprintf(fp, format, args));
1355}
1356
1357DllExport int
1358win32_vprintf(const char *format, va_list args)
1359{
1360 return (vprintf(format, args));
1361}
1362
1363DllExport size_t
1364win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1365{
1366 return fread(buf, size, count, fp);
1367}
1368
1369DllExport size_t
1370win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1371{
1372 return fwrite(buf, size, count, fp);
1373}
1374
1375DllExport FILE *
1376win32_fopen(const char *filename, const char *mode)
1377{
1378 return xcefopen(filename, mode);
1379}
1380
1381DllExport FILE *
1382win32_fdopen(int handle, const char *mode)
1383{
1384 return palm_fdopen(handle, mode);
1385}
1386
1387DllExport FILE *
1388win32_freopen(const char *path, const char *mode, FILE *stream)
1389{
1390 return xcefreopen(path, mode, stream);
1391}
1392
1393DllExport int
1394win32_fclose(FILE *pf)
1395{
1396 return xcefclose(pf);
1397}
1398
1399DllExport int
1400win32_fputs(const char *s,FILE *pf)
1401{
1402 return fputs(s, pf);
1403}
1404
1405DllExport int
1406win32_fputc(int c,FILE *pf)
1407{
1408 return fputc(c,pf);
1409}
1410
1411DllExport int
1412win32_ungetc(int c,FILE *pf)
1413{
1414 return ungetc(c,pf);
1415}
1416
1417DllExport int
1418win32_getc(FILE *pf)
1419{
1420 return getc(pf);
1421}
1422
1423DllExport int
1424win32_fileno(FILE *pf)
1425{
1426 return palm_fileno(pf);
1427}
1428
1429DllExport void
1430win32_clearerr(FILE *pf)
1431{
1432 clearerr(pf);
1433 return;
1434}
1435
1436DllExport int
1437win32_fflush(FILE *pf)
1438{
1439 return fflush(pf);
1440}
1441
1442DllExport long
1443win32_ftell(FILE *pf)
1444{
1445 return ftell(pf);
1446}
1447
1448DllExport int
814ffeea 1449win32_fseek(FILE *pf, Off_t offset,int origin)
e1caacb4 1450{
1451 return fseek(pf, offset, origin);
1452}
1453
cb69f87a 1454/* fpos_t seems to be int64 on hpc pro! Really stupid. */
1455/* But maybe someday there will be such large disks in a hpc... */
e1caacb4 1456DllExport int
1457win32_fgetpos(FILE *pf, fpos_t *p)
1458{
1459 return fgetpos(pf, p);
1460}
1461
1462DllExport int
1463win32_fsetpos(FILE *pf, const fpos_t *p)
1464{
1465 return fsetpos(pf, p);
1466}
1467
1468DllExport void
1469win32_rewind(FILE *pf)
1470{
1471 fseek(pf, 0, SEEK_SET);
1472 return;
1473}
1474
81003058 1475DllExport int
1476win32_tmpfd(void)
1477{
1478 dTHX;
1479 char prefix[MAX_PATH+1];
1480 char filename[MAX_PATH+1];
1481 DWORD len = GetTempPath(MAX_PATH, prefix);
1482 if (len && len < MAX_PATH) {
1483 if (GetTempFileName(prefix, "plx", 0, filename)) {
1484 HANDLE fh = CreateFile(filename,
1485 DELETE | GENERIC_READ | GENERIC_WRITE,
1486 0,
1487 NULL,
1488 CREATE_ALWAYS,
1489 FILE_ATTRIBUTE_NORMAL
1490 | FILE_FLAG_DELETE_ON_CLOSE,
1491 NULL);
1492 if (fh != INVALID_HANDLE_VALUE) {
1493 int fd = win32_open_osfhandle((intptr_t)fh, 0);
1494 if (fd >= 0) {
1495#if defined(__BORLANDC__)
1496 setmode(fd,O_BINARY);
1497#endif
1498 DEBUG_p(PerlIO_printf(Perl_debug_log,
1499 "Created tmpfile=%s\n",filename));
1500 return fd;
1501 }
1502 }
1503 }
1504 }
1505 return -1;
1506}
1507
e1caacb4 1508DllExport FILE*
1509win32_tmpfile(void)
1510{
81003058 1511 int fd = win32_tmpfd();
1512 if (fd >= 0)
1513 return win32_fdopen(fd, "w+b");
1514 return NULL;
e1caacb4 1515}
1516
1517DllExport void
1518win32_abort(void)
1519{
1520 xceabort();
1521
1522 return;
1523}
1524
1525DllExport int
1526win32_fstat(int fd, struct stat *sbufptr)
1527{
1528 return xcefstat(fd, sbufptr);
1529}
1530
1531DllExport int
1532win32_link(const char *oldname, const char *newname)
1533{
216db7ee 1534 dTHX;
e1caacb4 1535 Perl_croak(aTHX_ PL_no_func, "link");
1536
1537 return -1;
1538}
1539
1540DllExport int
1541win32_rename(const char *oname, const char *newname)
1542{
1543 return xcerename(oname, newname);
1544}
1545
1546DllExport int
1547win32_setmode(int fd, int mode)
1548{
bcdf844e 1549 /* currently 'celib' seem to have this function in src, but not
1550 * exported. When it will be, we'll uncomment following line.
1551 */
1552 /* return xcesetmode(fd, mode); */
1553 return 0;
e1caacb4 1554}
1555
216db7ee 1556DllExport int
1557win32_chsize(int fd, Off_t size)
1558{
1559 return chsize(fd, size);
1560}
1561
e1caacb4 1562DllExport long
814ffeea 1563win32_lseek(int fd, Off_t offset, int origin)
e1caacb4 1564{
1565 return xcelseek(fd, offset, origin);
1566}
1567
1568DllExport long
1569win32_tell(int fd)
1570{
1571 return xcelseek(fd, 0, SEEK_CUR);
1572}
1573
1574DllExport int
1575win32_open(const char *path, int flag, ...)
1576{
1577 int pmode;
1578 va_list ap;
1579
1580 va_start(ap, flag);
1581 pmode = va_arg(ap, int);
1582 va_end(ap);
1583
1584 return xceopen(path, flag, pmode);
1585}
1586
1587DllExport int
1588win32_close(int fd)
1589{
1590 return xceclose(fd);
1591}
1592
1593DllExport int
1594win32_eof(int fd)
1595{
216db7ee 1596 dTHX;
e1caacb4 1597 Perl_croak(aTHX_ PL_no_func, "eof");
1598 return -1;
1599}
1600
1601DllExport int
1602win32_dup(int fd)
1603{
216db7ee 1604 return xcedup(fd); /* from celib/ceio.c; requires some more work on it */
e1caacb4 1605}
1606
1607DllExport int
1608win32_dup2(int fd1,int fd2)
1609{
aebd5ec7 1610 return xcedup2(fd1,fd2);
e1caacb4 1611}
1612
1613DllExport int
1614win32_read(int fd, void *buf, unsigned int cnt)
1615{
1616 return xceread(fd, buf, cnt);
1617}
1618
1619DllExport int
1620win32_write(int fd, const void *buf, unsigned int cnt)
1621{
1622 return xcewrite(fd, (void *) buf, cnt);
1623}
1624
1625DllExport int
1626win32_mkdir(const char *dir, int mode)
1627{
1628 return xcemkdir(dir);
1629}
1630
1631DllExport int
1632win32_rmdir(const char *dir)
1633{
1634 return xcermdir(dir);
1635}
1636
1637DllExport int
1638win32_chdir(const char *dir)
1639{
1640 return xcechdir(dir);
1641}
1642
1643DllExport int
1644win32_access(const char *path, int mode)
1645{
1646 return xceaccess(path, mode);
1647}
1648
1649DllExport int
1650win32_chmod(const char *path, int mode)
1651{
1652 return xcechmod(path, mode);
1653}
1654
814ffeea 1655static char *
1656create_command_line(char *cname, STRLEN clen, const char * const *args)
216db7ee 1657{
1658 dTHX;
1659 int index, argc;
1660 char *cmd, *ptr;
1661 const char *arg;
1662 STRLEN len = 0;
1663 bool bat_file = FALSE;
1664 bool cmd_shell = FALSE;
1665 bool dumb_shell = FALSE;
1666 bool extra_quotes = FALSE;
1667 bool quote_next = FALSE;
1668
1669 if (!cname)
1670 cname = (char*)args[0];
1671
1672 /* The NT cmd.exe shell has the following peculiarity that needs to be
1673 * worked around. It strips a leading and trailing dquote when any
1674 * of the following is true:
1675 * 1. the /S switch was used
1676 * 2. there are more than two dquotes
1677 * 3. there is a special character from this set: &<>()@^|
1678 * 4. no whitespace characters within the two dquotes
1679 * 5. string between two dquotes isn't an executable file
1680 * To work around this, we always add a leading and trailing dquote
1681 * to the string, if the first argument is either "cmd.exe" or "cmd",
1682 * and there were at least two or more arguments passed to cmd.exe
1683 * (not including switches).
1684 * XXX the above rules (from "cmd /?") don't seem to be applied
1685 * always, making for the convolutions below :-(
1686 */
1687 if (cname) {
1688 if (!clen)
1689 clen = strlen(cname);
1690
1691 if (clen > 4
1692 && (stricmp(&cname[clen-4], ".bat") == 0
1693 || (IsWinNT() && stricmp(&cname[clen-4], ".cmd") == 0)))
1694 {
1695 bat_file = TRUE;
1696 len += 3;
1697 }
1698 else {
1699 char *exe = strrchr(cname, '/');
1700 char *exe2 = strrchr(cname, '\\');
1701 if (exe2 > exe)
1702 exe = exe2;
1703 if (exe)
1704 ++exe;
1705 else
1706 exe = cname;
1707 if (stricmp(exe, "cmd.exe") == 0 || stricmp(exe, "cmd") == 0) {
1708 cmd_shell = TRUE;
1709 len += 3;
1710 }
1711 else if (stricmp(exe, "command.com") == 0
1712 || stricmp(exe, "command") == 0)
1713 {
1714 dumb_shell = TRUE;
1715 }
1716 }
1717 }
1718
1719 DEBUG_p(PerlIO_printf(Perl_debug_log, "Args "));
1720 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1721 STRLEN curlen = strlen(arg);
1722 if (!(arg[0] == '"' && arg[curlen-1] == '"'))
1723 len += 2; /* assume quoting needed (worst case) */
1724 len += curlen + 1;
1725 DEBUG_p(PerlIO_printf(Perl_debug_log, "[%s]",arg));
1726 }
1727 DEBUG_p(PerlIO_printf(Perl_debug_log, "\n"));
1728
1729 argc = index;
a02a5408 1730 Newx(cmd, len, char);
216db7ee 1731 ptr = cmd;
1732
1733 if (bat_file) {
1734 *ptr++ = '"';
1735 extra_quotes = TRUE;
1736 }
1737
1738 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1739 bool do_quote = 0;
1740 STRLEN curlen = strlen(arg);
1741
1742 /* we want to protect empty arguments and ones with spaces with
1743 * dquotes, but only if they aren't already there */
1744 if (!dumb_shell) {
1745 if (!curlen) {
1746 do_quote = 1;
1747 }
1748 else if (quote_next) {
1749 /* see if it really is multiple arguments pretending to
1750 * be one and force a set of quotes around it */
1751 if (*find_next_space(arg))
1752 do_quote = 1;
1753 }
1754 else if (!(arg[0] == '"' && curlen > 1 && arg[curlen-1] == '"')) {
1755 STRLEN i = 0;
1756 while (i < curlen) {
1757 if (isSPACE(arg[i])) {
1758 do_quote = 1;
1759 }
1760 else if (arg[i] == '"') {
1761 do_quote = 0;
1762 break;
1763 }
1764 i++;
1765 }
1766 }
1767 }
1768
1769 if (do_quote)
1770 *ptr++ = '"';
1771
1772 strcpy(ptr, arg);
1773 ptr += curlen;
1774
1775 if (do_quote)
1776 *ptr++ = '"';
1777
1778 if (args[index+1])
1779 *ptr++ = ' ';
1780
1781 if (!extra_quotes
1782 && cmd_shell
1783 && curlen >= 2
1784 && *arg == '/' /* see if arg is "/c", "/x/c", "/x/d/c" etc. */
1785 && stricmp(arg+curlen-2, "/c") == 0)
1786 {
1787 /* is there a next argument? */
1788 if (args[index+1]) {
1789 /* are there two or more next arguments? */
1790 if (args[index+2]) {
1791 *ptr++ = '"';
1792 extra_quotes = TRUE;
1793 }
1794 else {
1795 /* single argument, force quoting if it has spaces */
1796 quote_next = TRUE;
1797 }
1798 }
1799 }
1800 }
1801
1802 if (extra_quotes)
1803 *ptr++ = '"';
1804
1805 *ptr = '\0';
1806
1807 return cmd;
1808}
1809
1810static char *
1811qualified_path(const char *cmd)
1812{
1813 dTHX;
1814 char *pathstr;
1815 char *fullcmd, *curfullcmd;
1816 STRLEN cmdlen = 0;
1817 int has_slash = 0;
1818
1819 if (!cmd)
1820 return Nullch;
1821 fullcmd = (char*)cmd;
1822 while (*fullcmd) {
1823 if (*fullcmd == '/' || *fullcmd == '\\')
1824 has_slash++;
1825 fullcmd++;
1826 cmdlen++;
1827 }
1828
1829 /* look in PATH */
1830 pathstr = PerlEnv_getenv("PATH");
a02a5408 1831 Newx(fullcmd, MAX_PATH+1, char);
216db7ee 1832 curfullcmd = fullcmd;
1833
1834 while (1) {
1835 DWORD res;
1836
1837 /* start by appending the name to the current prefix */
1838 strcpy(curfullcmd, cmd);
1839 curfullcmd += cmdlen;
1840
1841 /* if it doesn't end with '.', or has no extension, try adding
1842 * a trailing .exe first */
1843 if (cmd[cmdlen-1] != '.'
1844 && (cmdlen < 4 || cmd[cmdlen-4] != '.'))
1845 {
1846 strcpy(curfullcmd, ".exe");
1847 res = GetFileAttributes(fullcmd);
1848 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1849 return fullcmd;
1850 *curfullcmd = '\0';
1851 }
1852
1853 /* that failed, try the bare name */
1854 res = GetFileAttributes(fullcmd);
1855 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1856 return fullcmd;
1857
1858 /* quit if no other path exists, or if cmd already has path */
1859 if (!pathstr || !*pathstr || has_slash)
1860 break;
1861
1862 /* skip leading semis */
1863 while (*pathstr == ';')
1864 pathstr++;
1865
1866 /* build a new prefix from scratch */
1867 curfullcmd = fullcmd;
1868 while (*pathstr && *pathstr != ';') {
1869 if (*pathstr == '"') { /* foo;"baz;etc";bar */
1870 pathstr++; /* skip initial '"' */
1871 while (*pathstr && *pathstr != '"') {
1872 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1873 *curfullcmd++ = *pathstr;
1874 pathstr++;
1875 }
1876 if (*pathstr)
1877 pathstr++; /* skip trailing '"' */
1878 }
1879 else {
1880 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1881 *curfullcmd++ = *pathstr;
1882 pathstr++;
1883 }
1884 }
1885 if (*pathstr)
1886 pathstr++; /* skip trailing semi */
1887 if (curfullcmd > fullcmd /* append a dir separator */
1888 && curfullcmd[-1] != '/' && curfullcmd[-1] != '\\')
1889 {
1890 *curfullcmd++ = '\\';
1891 }
1892 }
1893
1894 Safefree(fullcmd);
1895 return Nullch;
1896}
1897
8f33b42a 1898/* The following are just place holders.
1899 * Some hosts may provide and environment that the OS is
1900 * not tracking, therefore, these host must provide that
1901 * environment and the current directory to CreateProcess
1902 */
1903
1904DllExport void*
1905win32_get_childenv(void)
1906{
1907 return NULL;
1908}
1909
1910DllExport void
1911win32_free_childenv(void* d)
1912{
1913}
1914
1915DllExport void
1916win32_clearenv(void)
1917{
1918 char *envv = GetEnvironmentStrings();
1919 char *cur = envv;
1920 STRLEN len;
1921 while (*cur) {
1922 char *end = strchr(cur,'=');
1923 if (end && end != cur) {
1924 *end = '\0';
1925 xcesetenv(cur, "", 0);
1926 *end = '=';
1927 cur = end + strlen(end+1)+2;
1928 }
1929 else if ((len = strlen(cur)))
1930 cur += len+1;
1931 }
1932 FreeEnvironmentStrings(envv);
1933}
1934
1935DllExport char*
1936win32_get_childdir(void)
1937{
1938 dTHX;
1939 char* ptr;
8c56068e 1940 char szfilename[MAX_PATH+1];
1941 GetCurrentDirectoryA(MAX_PATH+1, szfilename);
8f33b42a 1942
a02a5408 1943 Newx(ptr, strlen(szfilename)+1, char);
8f33b42a 1944 strcpy(ptr, szfilename);
1945 return ptr;
1946}
1947
1948DllExport void
1949win32_free_childdir(char* d)
1950{
1951 dTHX;
1952 Safefree(d);
1953}
1954
216db7ee 1955/* XXX this needs to be made more compatible with the spawnvp()
1956 * provided by the various RTLs. In particular, searching for
1957 * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented.
1958 * This doesn't significantly affect perl itself, because we
1959 * always invoke things using PERL5SHELL if a direct attempt to
1960 * spawn the executable fails.
1961 *
1962 * XXX splitting and rejoining the commandline between do_aspawn()
1963 * and win32_spawnvp() could also be avoided.
1964 */
1965
216db7ee 1966DllExport int
1967win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1968{
1969#ifdef USE_RTL_SPAWNVP
1970 return spawnvp(mode, cmdname, (char * const *)argv);
1971#else
1972 dTHX;
1973 int ret;
1974 void* env;
1975 char* dir;
1976 child_IO_table tbl;
1977 STARTUPINFO StartupInfo;
1978 PROCESS_INFORMATION ProcessInformation;
1979 DWORD create = 0;
1980 char *cmd;
1981 char *fullcmd = Nullch;
1982 char *cname = (char *)cmdname;
1983 STRLEN clen = 0;
1984
1985 if (cname) {
1986 clen = strlen(cname);
1987 /* if command name contains dquotes, must remove them */
1988 if (strchr(cname, '"')) {
1989 cmd = cname;
a02a5408 1990 Newx(cname,clen+1,char);
216db7ee 1991 clen = 0;
1992 while (*cmd) {
1993 if (*cmd != '"') {
1994 cname[clen] = *cmd;
1995 ++clen;
1996 }
1997 ++cmd;
1998 }
1999 cname[clen] = '\0';
2000 }
2001 }
2002
2003 cmd = create_command_line(cname, clen, argv);
2004
2005 env = PerlEnv_get_childenv();
2006 dir = PerlEnv_get_childdir();
2007
2008 switch(mode) {
2009 case P_NOWAIT: /* asynch + remember result */
2010 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
2011 errno = EAGAIN;
2012 ret = -1;
2013 goto RETVAL;
2014 }
2015 /* Create a new process group so we can use GenerateConsoleCtrlEvent()
2016 * in win32_kill()
2017 */
2018 /* not supported on CE create |= CREATE_NEW_PROCESS_GROUP; */
2019 /* FALL THROUGH */
2020
2021 case P_WAIT: /* synchronous execution */
2022 break;
2023 default: /* invalid mode */
2024 errno = EINVAL;
2025 ret = -1;
2026 goto RETVAL;
2027 }
2028 memset(&StartupInfo,0,sizeof(StartupInfo));
2029 StartupInfo.cb = sizeof(StartupInfo);
2030 memset(&tbl,0,sizeof(tbl));
2031 PerlEnv_get_child_IO(&tbl);
2032 StartupInfo.dwFlags = tbl.dwFlags;
2033 StartupInfo.dwX = tbl.dwX;
2034 StartupInfo.dwY = tbl.dwY;
2035 StartupInfo.dwXSize = tbl.dwXSize;
2036 StartupInfo.dwYSize = tbl.dwYSize;
2037 StartupInfo.dwXCountChars = tbl.dwXCountChars;
2038 StartupInfo.dwYCountChars = tbl.dwYCountChars;
2039 StartupInfo.dwFillAttribute = tbl.dwFillAttribute;
2040 StartupInfo.wShowWindow = tbl.wShowWindow;
2041 StartupInfo.hStdInput = tbl.childStdIn;
2042 StartupInfo.hStdOutput = tbl.childStdOut;
2043 StartupInfo.hStdError = tbl.childStdErr;
2044 if (StartupInfo.hStdInput == INVALID_HANDLE_VALUE &&
2045 StartupInfo.hStdOutput == INVALID_HANDLE_VALUE &&
2046 StartupInfo.hStdError == INVALID_HANDLE_VALUE)
2047 {
2048 create |= CREATE_NEW_CONSOLE;
2049 }
2050 else {
2051 StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
2052 }
2053 if (w32_use_showwindow) {
2054 StartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
2055 StartupInfo.wShowWindow = w32_showwindow;
2056 }
2057
2058 DEBUG_p(PerlIO_printf(Perl_debug_log, "Spawning [%s] with [%s]\n",
2059 cname,cmd));
2060RETRY:
2061 if (!CreateProcess(cname, /* search PATH to find executable */
2062 cmd, /* executable, and its arguments */
2063 NULL, /* process attributes */
2064 NULL, /* thread attributes */
2065 TRUE, /* inherit handles */
2066 create, /* creation flags */
2067 (LPVOID)env, /* inherit environment */
2068 dir, /* inherit cwd */
2069 &StartupInfo,
2070 &ProcessInformation))
2071 {
2072 /* initial NULL argument to CreateProcess() does a PATH
2073 * search, but it always first looks in the directory
2074 * where the current process was started, which behavior
2075 * is undesirable for backward compatibility. So we
2076 * jump through our own hoops by picking out the path
2077 * we really want it to use. */
2078 if (!fullcmd) {
2079 fullcmd = qualified_path(cname);
2080 if (fullcmd) {
2081 if (cname != cmdname)
2082 Safefree(cname);
2083 cname = fullcmd;
2084 DEBUG_p(PerlIO_printf(Perl_debug_log,
2085 "Retrying [%s] with same args\n",
2086 cname));
2087 goto RETRY;
2088 }
2089 }
2090 errno = ENOENT;
2091 ret = -1;
2092 goto RETVAL;
2093 }
2094
2095 if (mode == P_NOWAIT) {
2096 /* asynchronous spawn -- store handle, return PID */
2097 ret = (int)ProcessInformation.dwProcessId;
2098 if (IsWin95() && ret < 0)
2099 ret = -ret;
2100
2101 w32_child_handles[w32_num_children] = ProcessInformation.hProcess;
2102 w32_child_pids[w32_num_children] = (DWORD)ret;
2103 ++w32_num_children;
2104 }
2105 else {
2106 DWORD status;
2107 win32_msgwait(aTHX_ 1, &ProcessInformation.hProcess, INFINITE, NULL);
2108 /* FIXME: if msgwait returned due to message perhaps forward the
2109 "signal" to the process
2110 */
2111 GetExitCodeProcess(ProcessInformation.hProcess, &status);
2112 ret = (int)status;
2113 CloseHandle(ProcessInformation.hProcess);
2114 }
2115
2116 CloseHandle(ProcessInformation.hThread);
2117
2118RETVAL:
2119 PerlEnv_free_childenv(env);
2120 PerlEnv_free_childdir(dir);
2121 Safefree(cmd);
2122 if (cname != cmdname)
2123 Safefree(cname);
2124 return ret;
2125#endif
2126}
2127
e1caacb4 2128DllExport int
2129win32_execv(const char *cmdname, const char *const *argv)
2130{
216db7ee 2131 dTHX;
e1caacb4 2132 Perl_croak(aTHX_ PL_no_func, "execv");
2133 return -1;
2134}
2135
2136DllExport int
2137win32_execvp(const char *cmdname, const char *const *argv)
2138{
216db7ee 2139 dTHX;
e1caacb4 2140 Perl_croak(aTHX_ PL_no_func, "execvp");
2141 return -1;
2142}
2143
814ffeea 2144DllExport void
2145win32_perror(const char *str)
e1caacb4 2146{
814ffeea 2147 xceperror(str);
2148}
e1caacb4 2149
814ffeea 2150DllExport void
2151win32_setbuf(FILE *pf, char *buf)
2152{
2153 dTHX;
2154 Perl_croak(aTHX_ PL_no_func, "setbuf");
2155}
e1caacb4 2156
814ffeea 2157DllExport int
2158win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
2159{
2160 return setvbuf(pf, buf, type, size);
e1caacb4 2161}
2162
814ffeea 2163DllExport int
2164win32_flushall(void)
2165{
2166 return flushall();
2167}
e1caacb4 2168
814ffeea 2169DllExport int
2170win32_fcloseall(void)
e1caacb4 2171{
814ffeea 2172 return fcloseall();
2173}
e1caacb4 2174
814ffeea 2175DllExport char*
2176win32_fgets(char *s, int n, FILE *pf)
2177{
2178 return fgets(s, n, pf);
2179}
e1caacb4 2180
814ffeea 2181DllExport char*
2182win32_gets(char *s)
2183{
2184 return gets(s);
e1caacb4 2185}
2186
814ffeea 2187DllExport int
2188win32_fgetc(FILE *pf)
e1caacb4 2189{
814ffeea 2190 return fgetc(pf);
2191}
e1caacb4 2192
814ffeea 2193DllExport int
2194win32_putc(int c, FILE *pf)
2195{
2196 return putc(c,pf);
2197}
2198
2199DllExport int
2200win32_puts(const char *s)
2201{
2202 return puts(s);
2203}
2204
2205DllExport int
2206win32_getchar(void)
2207{
2208 return getchar();
2209}
2210
2211DllExport int
2212win32_putchar(int c)
2213{
2214 return putchar(c);
2215}
2216
2217#ifdef MYMALLOC
2218
2219#ifndef USE_PERL_SBRK
2220
2221static char *committed = NULL;
2222static char *base = NULL;
2223static char *reserved = NULL;
2224static char *brk = NULL;
2225static DWORD pagesize = 0;
2226static DWORD allocsize = 0;
2227
2228void *
2229sbrk(int need)
2230{
2231 void *result;
2232 if (!pagesize)
2233 {SYSTEM_INFO info;
2234 GetSystemInfo(&info);
2235 /* Pretend page size is larger so we don't perpetually
2236 * call the OS to commit just one page ...
2237 */
2238 pagesize = info.dwPageSize << 3;
2239 allocsize = info.dwAllocationGranularity;
2240 }
2241 /* This scheme fails eventually if request for contiguous
2242 * block is denied so reserve big blocks - this is only
2243 * address space not memory ...
2244 */
2245 if (brk+need >= reserved)
2246 {
2247 DWORD size = 64*1024*1024;
2248 char *addr;
2249 if (committed && reserved && committed < reserved)
2250 {
2251 /* Commit last of previous chunk cannot span allocations */
2252 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
2253 if (addr)
2254 committed = reserved;
2255 }
2256 /* Reserve some (more) space
2257 * Note this is a little sneaky, 1st call passes NULL as reserved
2258 * so lets system choose where we start, subsequent calls pass
2259 * the old end address so ask for a contiguous block
2260 */
2261 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
2262 if (addr)
2263 {
2264 reserved = addr+size;
2265 if (!base)
2266 base = addr;
2267 if (!committed)
2268 committed = base;
2269 if (!brk)
2270 brk = committed;
2271 }
2272 else
2273 {
2274 return (void *) -1;
2275 }
2276 }
2277 result = brk;
2278 brk += need;
2279 if (brk > committed)
2280 {
2281 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
2282 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
2283 if (addr)
2284 {
2285 committed += size;
2286 }
2287 else
2288 return (void *) -1;
2289 }
2290 return result;
2291}
2292
2293#endif
2294#endif
2295
2296DllExport void*
2297win32_malloc(size_t size)
2298{
2299 return malloc(size);
2300}
2301
2302DllExport void*
2303win32_calloc(size_t numitems, size_t size)
2304{
2305 return calloc(numitems,size);
2306}
2307
2308DllExport void*
2309win32_realloc(void *block, size_t size)
2310{
2311 return realloc(block,size);
2312}
2313
2314DllExport void
2315win32_free(void *block)
2316{
2317 free(block);
2318}
2319
2320int
2321win32_open_osfhandle(intptr_t osfhandle, int flags)
2322{
2323 int fh;
2324 char fileflags=0; /* _osfile flags */
2325
2326 Perl_croak_nocontext("win32_open_osfhandle() TBD on this platform");
2327 return 0;
2328}
2329
2330int
2331win32_get_osfhandle(int fd)
2332{
2333 int fh;
2334 char fileflags=0; /* _osfile flags */
2335
2336 Perl_croak_nocontext("win32_get_osfhandle() TBD on this platform");
2337 return 0;
2338}
2339
2340FILE *
2341win32_fdupopen(FILE *pf)
2342{
2343 FILE* pfdup;
2344 fpos_t pos;
2345 char mode[3];
2346 int fileno = win32_dup(win32_fileno(pf));
2347 int fmode = palm_fgetmode(pfdup);
2348
2349 fprintf(stderr,"DEBUG for win32_fdupopen()\n");
2350
2351 /* open the file in the same mode */
2352 if(fmode & O_RDONLY) {
2353 mode[0] = 'r';
2354 mode[1] = 0;
2355 }
2356 else if(fmode & O_APPEND) {
2357 mode[0] = 'a';
2358 mode[1] = 0;
2359 }
2360 else if(fmode & O_RDWR) {
2361 mode[0] = 'r';
2362 mode[1] = '+';
2363 mode[2] = 0;
2364 }
2365
2366 /* it appears that the binmode is attached to the
2367 * file descriptor so binmode files will be handled
2368 * correctly
2369 */
2370 pfdup = win32_fdopen(fileno, mode);
2371
2372 /* move the file pointer to the same position */
2373 if (!fgetpos(pf, &pos)) {
2374 fsetpos(pfdup, &pos);
2375 }
2376 return pfdup;
2377}
2378
2379DllExport void*
2380win32_dynaload(const char* filename)
2381{
2382 dTHX;
2383 HMODULE hModule;
2384
2385 hModule = XCELoadLibraryA(filename);
2386
2387 return hModule;
2388}
2389
2390/* this is needed by Cwd.pm... */
2391
2392static
2393XS(w32_GetCwd)
2394{
2395 dXSARGS;
2396 char buf[MAX_PATH];
2397 SV *sv = sv_newmortal();
2398
2399 xcegetcwd(buf, sizeof(buf));
2400
2401 sv_setpv(sv, xcestrdup(buf));
2402 EXTEND(SP,1);
2403 SvPOK_on(sv);
2404 ST(0) = sv;
2405#ifndef INCOMPLETE_TAINTS
2406 SvTAINTED_on(ST(0));
2407#endif
2408 XSRETURN(1);
2409}
2410
2411static
2412XS(w32_SetCwd)
2413{
2414 dXSARGS;
2415
2416 if (items != 1)
e1caacb4 2417 Perl_croak(aTHX_ "usage: Win32::SetCwd($cwd)");
2418
2419 if (!xcechdir(SvPV_nolen(ST(0))))
2420 XSRETURN_YES;
2421
2422 XSRETURN_NO;
2423}
2424
2425static
2426XS(w32_GetTickCount)
2427{
2428 dXSARGS;
2429 DWORD msec = GetTickCount();
2430 EXTEND(SP,1);
2431 if ((IV)msec > 0)
2432 XSRETURN_IV(msec);
2433 XSRETURN_NV(msec);
2434}
2435
2436static
2437XS(w32_GetOSVersion)
2438{
2439 dXSARGS;
2440 OSVERSIONINFOA osver;
2441
2442 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
2443 if (!XCEGetVersionExA(&osver)) {
2444 XSRETURN_EMPTY;
2445 }
2446 XPUSHs(newSVpvn(osver.szCSDVersion, strlen(osver.szCSDVersion)));
2447 XPUSHs(newSViv(osver.dwMajorVersion));
2448 XPUSHs(newSViv(osver.dwMinorVersion));
2449 XPUSHs(newSViv(osver.dwBuildNumber));
cb69f87a 2450 /* WINCE = 3 */
e1caacb4 2451 XPUSHs(newSViv(osver.dwPlatformId));
2452 PUTBACK;
2453}
2454
2455static
2456XS(w32_IsWinNT)
2457{
2458 dXSARGS;
2459 EXTEND(SP,1);
2460 XSRETURN_IV(IsWinNT());
2461}
2462
2463static
2464XS(w32_IsWin95)
2465{
2466 dXSARGS;
2467 EXTEND(SP,1);
2468 XSRETURN_IV(IsWin95());
2469}
2470
2471static
2472XS(w32_IsWinCE)
2473{
2474 dXSARGS;
2475 EXTEND(SP,1);
2476 XSRETURN_IV(IsWinCE());
2477}
2478
2479static
2480XS(w32_GetOemInfo)
2481{
2482 dXSARGS;
2483 wchar_t wbuf[126];
2484 char buf[126];
2485
2486 if(SystemParametersInfoW(SPI_GETOEMINFO, sizeof(wbuf), wbuf, FALSE))
2487 WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buf, sizeof(buf), 0, 0);
2488 else
2489 sprintf(buf, "SystemParametersInfo failed: %d", GetLastError());
2490
2491 EXTEND(SP,1);
2492 XSRETURN_PV(buf);
2493}
2494
2495static
2496XS(w32_Sleep)
2497{
2498 dXSARGS;
2499 if (items != 1)
2500 Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)");
2501 Sleep(SvIV(ST(0)));
2502 XSRETURN_YES;
2503}
2504
2505static
2506XS(w32_CopyFile)
2507{
2508 dXSARGS;
2509 BOOL bResult;
2510 if (items != 3)
2511 Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)");
2512
2513 {
2514 char szSourceFile[MAX_PATH+1];
2515 strcpy(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(0))));
2516 bResult = XCECopyFileA(szSourceFile, SvPV_nolen(ST(1)),
2517 !SvTRUE(ST(2)));
2518 }
2519
2520 if (bResult)
2521 XSRETURN_YES;
2522
2523 XSRETURN_NO;
2524}
2525
2526static
2527XS(w32_MessageBox)
2528{
2529 dXSARGS;
2530
2531 char *txt;
2532 unsigned int res;
2533 unsigned int flags = MB_OK;
2534
2535 txt = SvPV_nolen(ST(0));
2536
2537 if (items < 1 || items > 2)
2538 Perl_croak(aTHX_ "usage: Win32::MessageBox($txt, [$flags])");
2539
2540 if(items == 2)
2541 flags = SvIV(ST(1));
2542
2543 res = XCEMessageBoxA(NULL, txt, "Perl", flags);
2544
2545 XSRETURN_IV(res);
2546}
2547
2548static
2549XS(w32_GetPowerStatus)
2550{
2551 dXSARGS;
2552
2553 SYSTEM_POWER_STATUS_EX sps;
2554
2555 if(GetSystemPowerStatusEx(&sps, TRUE) == FALSE)
2556 {
2557 XSRETURN_EMPTY;
2558 }
2559
2560 XPUSHs(newSViv(sps.ACLineStatus));
2561 XPUSHs(newSViv(sps.BatteryFlag));
2562 XPUSHs(newSViv(sps.BatteryLifePercent));
2563 XPUSHs(newSViv(sps.BatteryLifeTime));
2564 XPUSHs(newSViv(sps.BatteryFullLifeTime));
2565 XPUSHs(newSViv(sps.BackupBatteryFlag));
2566 XPUSHs(newSViv(sps.BackupBatteryLifePercent));
2567 XPUSHs(newSViv(sps.BackupBatteryLifeTime));
2568 XPUSHs(newSViv(sps.BackupBatteryFullLifeTime));
2569
2570 PUTBACK;
2571}
2572
2573#if UNDER_CE > 200
2574static
2575XS(w32_ShellEx)
2576{
2577 dXSARGS;
2578
2579 char buf[126];
2580 SHELLEXECUTEINFO si;
2581 char *file, *verb;
2582 wchar_t wfile[MAX_PATH];
2583 wchar_t wverb[20];
2584
2585 if (items != 2)
2586 Perl_croak(aTHX_ "usage: Win32::ShellEx($file, $verb)");
2587
2588 file = SvPV_nolen(ST(0));
2589 verb = SvPV_nolen(ST(1));
2590
2591 memset(&si, 0, sizeof(si));
2592 si.cbSize = sizeof(si);
2593 si.fMask = SEE_MASK_FLAG_NO_UI;
2594
2595 MultiByteToWideChar(CP_ACP, 0, verb, -1,
2596 wverb, sizeof(wverb)/2);
2597 si.lpVerb = (TCHAR *)wverb;
2598
2599 MultiByteToWideChar(CP_ACP, 0, file, -1,
2600 wfile, sizeof(wfile)/2);
2601 si.lpFile = (TCHAR *)wfile;
2602
2603 if(ShellExecuteEx(&si) == FALSE)
2604 {
2605 XSRETURN_NO;
2606 }
2607 XSRETURN_YES;
2608}
2609#endif
2610
2611void
2612Perl_init_os_extras(void)
2613{
acfe0abc 2614 dTHX;
e1caacb4 2615 char *file = __FILE__;
2616 dXSUB_SYS;
2617
2618 w32_perlshell_tokens = Nullch;
2619 w32_perlshell_items = -1;
2620 w32_fdpid = newAV(); /* XX needs to be in Perl_win32_init()? */
a02a5408 2621 Newx(w32_children, 1, child_tab);
e1caacb4 2622 w32_num_children = 0;
2623
2624 newXS("Win32::GetCwd", w32_GetCwd, file);
2625 newXS("Win32::SetCwd", w32_SetCwd, file);
2626 newXS("Win32::GetTickCount", w32_GetTickCount, file);
2627 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
2628#if UNDER_CE > 200
2629 newXS("Win32::ShellEx", w32_ShellEx, file);
2630#endif
2631 newXS("Win32::IsWinNT", w32_IsWinNT, file);
2632 newXS("Win32::IsWin95", w32_IsWin95, file);
2633 newXS("Win32::IsWinCE", w32_IsWinCE, file);
2634 newXS("Win32::CopyFile", w32_CopyFile, file);
2635 newXS("Win32::Sleep", w32_Sleep, file);
2636 newXS("Win32::MessageBox", w32_MessageBox, file);
2637 newXS("Win32::GetPowerStatus", w32_GetPowerStatus, file);
2638 newXS("Win32::GetOemInfo", w32_GetOemInfo, file);
2639}
2640
2641void
2642myexit(void)
2643{
2644 char buf[126];
2645
2646 puts("Hit return");
2647 fgets(buf, sizeof(buf), stdin);
2648}
2649
2650void
2651Perl_win32_init(int *argcp, char ***argvp)
2652{
2653#ifdef UNDER_CE
2654 char *p;
2655
2656 if((p = xcegetenv("PERLDEBUG")) && (p[0] == 'y' || p[0] == 'Y'))
2657 atexit(myexit);
2658#endif
2659
2660 MALLOC_INIT;
2661}
2662
216db7ee 2663DllExport void
2664Perl_win32_term(void)
2665{
2666 OP_REFCNT_TERM;
2667 MALLOC_TERM;
2668}
2669
2670void
2671win32_get_child_IO(child_IO_table* ptbl)
2672{
2673 ptbl->childStdIn = GetStdHandle(STD_INPUT_HANDLE);
2674 ptbl->childStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2675 ptbl->childStdErr = GetStdHandle(STD_ERROR_HANDLE);
2676}
2677
e1caacb4 2678win32_flock(int fd, int oper)
2679{
216db7ee 2680 dTHX;
e1caacb4 2681 Perl_croak(aTHX_ PL_no_func, "flock");
2682 return -1;
2683}
2684
2685DllExport int
2686win32_waitpid(int pid, int *status, int flags)
2687{
216db7ee 2688 dTHX;
e1caacb4 2689 Perl_croak(aTHX_ PL_no_func, "waitpid");
2690 return -1;
2691}
2692
2693DllExport int
2694win32_wait(int *status)
2695{
216db7ee 2696 dTHX;
e1caacb4 2697 Perl_croak(aTHX_ PL_no_func, "wait");
2698 return -1;
2699}
2700
2701int
e1caacb4 2702wce_reopen_stdout(char *fname)
2703{
2704 if(xcefreopen(fname, "w", stdout) == NULL)
2705 return -1;
2706
2707 return 0;
2708}
2709
2710void
2711wce_hitreturn()
2712{
2713 char buf[126];
2714
2715 printf("Hit RETURN");
2716 fflush(stdout);
2717 fgets(buf, sizeof(buf), stdin);
2718 return;
2719}
2720
cb69f87a 2721/* //////////////////////////////////////////////////////////////////// */
e1caacb4 2722
ca6c63e1 2723#undef getcwd
2724
2725char *
2726getcwd(char *buf, size_t size)
2727{
2728 return xcegetcwd(buf, size);
2729}
2730
2731int
2732isnan(double d)
2733{
2734 return _isnan(d);
2735}
2736
18f68570 2737
814ffeea 2738DllExport PerlIO*
2739win32_popenlist(const char *mode, IV narg, SV **args)
18f68570 2740{
814ffeea 2741 dTHX;
2742 Perl_croak(aTHX_ "List form of pipe open not implemented");
2743 return NULL;
18f68570 2744}
2745
2746/*
2747 * a popen() clone that respects PERL5SHELL
2748 *
2749 * changed to return PerlIO* rather than FILE * by BKS, 11-11-2000
2750 */
2751
2752DllExport PerlIO*
2753win32_popen(const char *command, const char *mode)
2754{
814ffeea 2755#ifdef USE_RTL_POPEN
2756 return _popen(command, mode);
2757#else
2758 dTHX;
2759 int p[2];
2760 int parent, child;
2761 int stdfd, oldfd;
2762 int ourmode;
2763 int childpid;
2764 DWORD nhandle;
2765 HANDLE old_h;
2766 int lock_held = 0;
2767
2768 /* establish which ends read and write */
2769 if (strchr(mode,'w')) {
2770 stdfd = 0; /* stdin */
2771 parent = 1;
2772 child = 0;
2773 nhandle = STD_INPUT_HANDLE;
2774 }
2775 else if (strchr(mode,'r')) {
2776 stdfd = 1; /* stdout */
2777 parent = 0;
2778 child = 1;
2779 nhandle = STD_OUTPUT_HANDLE;
2780 }
2781 else
2782 return NULL;
18f68570 2783
814ffeea 2784 /* set the correct mode */
2785 if (strchr(mode,'b'))
2786 ourmode = O_BINARY;
2787 else if (strchr(mode,'t'))
2788 ourmode = O_TEXT;
2789 else
2790 ourmode = _fmode & (O_TEXT | O_BINARY);
2791
2792 /* the child doesn't inherit handles */
2793 ourmode |= O_NOINHERIT;
2794
2795 if (win32_pipe(p, 512, ourmode) == -1)
2796 return NULL;
2797
2798 /* save current stdfd */
2799 if ((oldfd = win32_dup(stdfd)) == -1)
2800 goto cleanup;
2801
2802 /* save the old std handle (this needs to happen before the
2803 * dup2(), since that might call SetStdHandle() too) */
2804 OP_REFCNT_LOCK;
2805 lock_held = 1;
2806 old_h = GetStdHandle(nhandle);
2807
2808 /* make stdfd go to child end of pipe (implicitly closes stdfd) */
2809 /* stdfd will be inherited by the child */
2810 if (win32_dup2(p[child], stdfd) == -1)
2811 goto cleanup;
2812
2813 /* close the child end in parent */
2814 win32_close(p[child]);
2815
2816 /* set the new std handle (in case dup2() above didn't) */
2817 SetStdHandle(nhandle, (HANDLE)_get_osfhandle(stdfd));
2818
2819 /* start the child */
2820 {
2821 dTHX;
2822 if ((childpid = do_spawn_nowait((char*)command)) == -1)
2823 goto cleanup;
2824
2825 /* revert stdfd to whatever it was before */
2826 if (win32_dup2(oldfd, stdfd) == -1)
2827 goto cleanup;
2828
2829 /* restore the old std handle (this needs to happen after the
2830 * dup2(), since that might call SetStdHandle() too */
2831 if (lock_held) {
2832 SetStdHandle(nhandle, old_h);
2833 OP_REFCNT_UNLOCK;
2834 lock_held = 0;
2835 }
2836
2837 /* close saved handle */
2838 win32_close(oldfd);
2839
2840 LOCK_FDPID_MUTEX;
2841 sv_setiv(*av_fetch(w32_fdpid, p[parent], TRUE), childpid);
2842 UNLOCK_FDPID_MUTEX;
2843
2844 /* set process id so that it can be returned by perl's open() */
2845 PL_forkprocess = childpid;
2846 }
2847
2848 /* we have an fd, return a file stream */
2849 return (PerlIO_fdopen(p[parent], (char *)mode));
2850
2851cleanup:
2852 /* we don't need to check for errors here */
2853 win32_close(p[0]);
2854 win32_close(p[1]);
2855 if (lock_held) {
2856 SetStdHandle(nhandle, old_h);
2857 OP_REFCNT_UNLOCK;
2858 lock_held = 0;
2859 }
2860 if (oldfd != -1) {
2861 win32_dup2(oldfd, stdfd);
2862 win32_close(oldfd);
2863 }
2864 return (NULL);
2865
2866#endif /* USE_RTL_POPEN */
216db7ee 2867}
2868
18f68570 2869/*
2870 * pclose() clone
2871 */
2872
2873DllExport int
2874win32_pclose(PerlIO *pf)
2875{
2876#ifdef USE_RTL_POPEN
2877 return _pclose(pf);
2878#else
2879 dTHX;
2880 int childpid, status;
2881 SV *sv;
2882
2883 LOCK_FDPID_MUTEX;
2884 sv = *av_fetch(w32_fdpid, PerlIO_fileno(pf), TRUE);
2885
2886 if (SvIOK(sv))
2887 childpid = SvIVX(sv);
2888 else
2889 childpid = 0;
2890
2891 if (!childpid) {
2892 errno = EBADF;
2893 return -1;
2894 }
2895
2896#ifdef USE_PERLIO
2897 PerlIO_close(pf);
2898#else
2899 fclose(pf);
2900#endif
2901 SvIVX(sv) = 0;
2902 UNLOCK_FDPID_MUTEX;
2903
2904 if (win32_waitpid(childpid, &status, 0) == -1)
2905 return -1;
2906
2907 return status;
2908
2909#endif /* USE_RTL_POPEN */
2910}
2911
216db7ee 2912#ifdef HAVE_INTERP_INTERN
2913
2914
2915static void
2916win32_csighandler(int sig)
2917{
2918#if 0
2919 dTHXa(PERL_GET_SIG_CONTEXT);
2920 Perl_warn(aTHX_ "Got signal %d",sig);
2921#endif
2922 /* Does nothing */
2923}
2924
2925void
2926Perl_sys_intern_init(pTHX)
2927{
2928 int i;
2929 w32_perlshell_tokens = Nullch;
2930 w32_perlshell_vec = (char**)NULL;
2931 w32_perlshell_items = 0;
2932 w32_fdpid = newAV();
a02a5408 2933 Newx(w32_children, 1, child_tab);
216db7ee 2934 w32_num_children = 0;
2935# ifdef USE_ITHREADS
2936 w32_pseudo_id = 0;
a02a5408 2937 Newx(w32_pseudo_children, 1, child_tab);
216db7ee 2938 w32_num_pseudo_children = 0;
2939# endif
2940 w32_init_socktype = 0;
2941 w32_timerid = 0;
2942 w32_poll_count = 0;
2943}
2944
2945void
2946Perl_sys_intern_clear(pTHX)
2947{
2948 Safefree(w32_perlshell_tokens);
2949 Safefree(w32_perlshell_vec);
2950 /* NOTE: w32_fdpid is freed by sv_clean_all() */
2951 Safefree(w32_children);
2952 if (w32_timerid) {
2953 KillTimer(NULL,w32_timerid);
2954 w32_timerid=0;
2955 }
2956# ifdef USE_ITHREADS
2957 Safefree(w32_pseudo_children);
2958# endif
2959}
2960
2961# ifdef USE_ITHREADS
2962
2963void
2964Perl_sys_intern_dup(pTHX_ struct interp_intern *src, struct interp_intern *dst)
2965{
2966 dst->perlshell_tokens = Nullch;
2967 dst->perlshell_vec = (char**)NULL;
2968 dst->perlshell_items = 0;
2969 dst->fdpid = newAV();
a02a5408 2970 Newxz(dst->children, 1, child_tab);
216db7ee 2971 dst->pseudo_id = 0;
a02a5408 2972 Newxz(dst->pseudo_children, 1, child_tab);
216db7ee 2973 dst->thr_intern.Winit_socktype = 0;
2974 dst->timerid = 0;
2975 dst->poll_count = 0;
2976 Copy(src->sigtable,dst->sigtable,SIG_SIZE,Sighandler_t);
2977}
2978# endif /* USE_ITHREADS */
2979#endif /* HAVE_INTERP_INTERN */
2980
2981static void
2982win32_free_argvw(pTHX_ void *ptr)
2983{
2984 char** argv = (char**)ptr;
2985 while(*argv) {
2986 Safefree(*argv);
2987 *argv++ = Nullch;
2988 }
2989}
2990
2991void
2992win32_argv2utf8(int argc, char** argv)
2993{
2994 /* do nothing, since we're not aware of command line arguments
2995 * currently ...
2996 */
2997}
2998
2999#if 0
3000void
3001Perl_sys_intern_clear(pTHX)
3002{
3003 Safefree(w32_perlshell_tokens);
3004 Safefree(w32_perlshell_vec);
3005 /* NOTE: w32_fdpid is freed by sv_clean_all() */
3006 Safefree(w32_children);
3007# ifdef USE_ITHREADS
3008 Safefree(w32_pseudo_children);
3009# endif
3010}
3011
3012#endif
3013// added to remove undefied symbol error in CodeWarrior compilation
3014int
3015Perl_Ireentrant_buffer_ptr(aTHX)
3016{
3017 return 0;
3018}