This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
for pod/perlfaq2.pod against latest snapshot for Alpaca
[perl5.git] / wince / wince.c
CommitLineData
e1caacb4
JH
1/* WINCE.C - stuff for Windows CE
2 *
ca6c63e1 3 * Time-stamp: <26/10/01 15:25:20 keuchel@keuchelnt>
e1caacb4
JH
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>
12
13#define PERLIO_NOT_STDIO 0
14
15#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
16#define PerlIO FILE
17#endif
18
19#define wince_private
20#include "errno.h"
21
22#include "EXTERN.h"
23#include "perl.h"
24
25#define NO_XSLOCKS
26#define PERL_NO_GET_CONTEXT
27#include "XSUB.h"
28
29#include "win32iop.h"
30#include <string.h>
31#include <stdarg.h>
32#include <float.h>
33#include <shellapi.h>
34
35#define perl
36#include "celib_defs.h"
37#include "cewin32.h"
38#include "cecrt.h"
39#include "cewin32_defs.h"
40#include "cecrt_defs.h"
41
42#ifdef PALM_SIZE
43#include "stdio-palmsize.h"
44#endif
45
46#define EXECF_EXEC 1
47#define EXECF_SPAWN 2
48#define EXECF_SPAWN_NOWAIT 3
49
50#if defined(PERL_IMPLICIT_SYS)
51# undef win32_get_privlib
52# define win32_get_privlib g_win32_get_privlib
53# undef win32_get_sitelib
54# define win32_get_sitelib g_win32_get_sitelib
55# undef win32_get_vendorlib
56# define win32_get_vendorlib g_win32_get_vendorlib
57# undef do_spawn
58# define do_spawn g_do_spawn
59# undef getlogin
60# define getlogin g_getlogin
61#endif
62
e1caacb4
JH
63static long filetime_to_clock(PFILETIME ft);
64static BOOL filetime_from_time(PFILETIME ft, time_t t);
65static char * get_emd_part(SV **leading, char *trailing, ...);
66static char * win32_get_xlib(const char *pl, const char *xlib,
67 const char *libname);
68
69START_EXTERN_C
70HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
71char w32_module_name[MAX_PATH+1];
72END_EXTERN_C
73
74static DWORD w32_platform = (DWORD)-1;
75
76int
77IsWin95(void)
78{
79 return (win32_os_id() == VER_PLATFORM_WIN32_WINDOWS);
80}
81
82int
83IsWinNT(void)
84{
85 return (win32_os_id() == VER_PLATFORM_WIN32_NT);
86}
87
88int
89IsWinCE(void)
90{
91 return (win32_os_id() == VER_PLATFORM_WIN32_CE);
92}
93
94EXTERN_C void
95set_w32_module_name(void)
96{
97 char* ptr;
98 XCEGetModuleFileNameA((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
99 ? XCEGetModuleHandleA(NULL)
100 : w32_perldll_handle),
101 w32_module_name, sizeof(w32_module_name));
102
103 /* normalize to forward slashes */
104 ptr = w32_module_name;
105 while (*ptr) {
106 if (*ptr == '\\')
107 *ptr = '/';
108 ++ptr;
109 }
110}
111
112/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
113static char*
114get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
115{
116 /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
117 HKEY handle;
118 DWORD type;
119 const char *subkey = "Software\\Perl";
120 char *str = Nullch;
121 long retval;
122
123 retval = XCERegOpenKeyExA(hkey, subkey, 0, KEY_READ, &handle);
124 if (retval == ERROR_SUCCESS) {
125 DWORD datalen;
126 retval = XCERegQueryValueExA(handle, valuename, 0, &type, NULL, &datalen);
127 if (retval == ERROR_SUCCESS && type == REG_SZ) {
acfe0abc 128 dTHX;
e1caacb4
JH
129 if (!*svp)
130 *svp = sv_2mortal(newSVpvn("",0));
131 SvGROW(*svp, datalen);
132 retval = XCERegQueryValueExA(handle, valuename, 0, NULL,
133 (PBYTE)SvPVX(*svp), &datalen);
134 if (retval == ERROR_SUCCESS) {
135 str = SvPVX(*svp);
136 SvCUR_set(*svp,datalen-1);
137 }
138 }
139 RegCloseKey(handle);
140 }
141 return str;
142}
143
144/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
145static char*
146get_regstr(const char *valuename, SV **svp)
147{
148 char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
149 if (!str)
150 str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
151 return str;
152}
153
154/* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
155static char *
156get_emd_part(SV **prev_pathp, char *trailing_path, ...)
157{
158 char base[10];
159 va_list ap;
160 char mod_name[MAX_PATH+1];
161 char *ptr;
162 char *optr;
163 char *strip;
164 int oldsize, newsize;
165 STRLEN baselen;
166
167 va_start(ap, trailing_path);
168 strip = va_arg(ap, char *);
169
170 sprintf(base, "%d.%d", (int)PERL_REVISION, (int)PERL_VERSION);
171 baselen = strlen(base);
172
173 if (!*w32_module_name) {
174 set_w32_module_name();
175 }
176 strcpy(mod_name, w32_module_name);
177 ptr = strrchr(mod_name, '/');
178 while (ptr && strip) {
179 /* look for directories to skip back */
180 optr = ptr;
181 *ptr = '\0';
182 ptr = strrchr(mod_name, '/');
183 /* avoid stripping component if there is no slash,
184 * or it doesn't match ... */
185 if (!ptr || stricmp(ptr+1, strip) != 0) {
186 /* ... but not if component matches m|5\.$patchlevel.*| */
187 if (!ptr || !(*strip == '5' && *(ptr+1) == '5'
188 && strncmp(strip, base, baselen) == 0
189 && strncmp(ptr+1, base, baselen) == 0))
190 {
191 *optr = '/';
192 ptr = optr;
193 }
194 }
195 strip = va_arg(ap, char *);
196 }
197 if (!ptr) {
198 ptr = mod_name;
199 *ptr++ = '.';
200 *ptr = '/';
201 }
202 va_end(ap);
203 strcpy(++ptr, trailing_path);
204
205 /* only add directory if it exists */
206 if (XCEGetFileAttributesA(mod_name) != (DWORD) -1) {
207 /* directory exists */
acfe0abc 208 dTHX;
e1caacb4
JH
209 if (!*prev_pathp)
210 *prev_pathp = sv_2mortal(newSVpvn("",0));
211 sv_catpvn(*prev_pathp, ";", 1);
212 sv_catpv(*prev_pathp, mod_name);
213 return SvPVX(*prev_pathp);
214 }
215
216 return Nullch;
217}
218
219char *
220win32_get_privlib(const char *pl)
221{
acfe0abc 222 dTHX;
e1caacb4
JH
223 char *stdlib = "lib";
224 char buffer[MAX_PATH+1];
225 SV *sv = Nullsv;
226
227 /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
228 sprintf(buffer, "%s-%s", stdlib, pl);
229 if (!get_regstr(buffer, &sv))
230 (void)get_regstr(stdlib, &sv);
231
232 /* $stdlib .= ";$EMD/../../lib" */
233 return get_emd_part(&sv, stdlib, ARCHNAME, "bin", Nullch);
234}
235
236static char *
237win32_get_xlib(const char *pl, const char *xlib, const char *libname)
238{
acfe0abc 239 dTHX;
e1caacb4
JH
240 char regstr[40];
241 char pathstr[MAX_PATH+1];
242 DWORD datalen;
243 int len, newsize;
244 SV *sv1 = Nullsv;
245 SV *sv2 = Nullsv;
246
247 /* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */
248 sprintf(regstr, "%s-%s", xlib, pl);
249 (void)get_regstr(regstr, &sv1);
250
251 /* $xlib .=
252 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */
253 sprintf(pathstr, "%s/%s/lib", libname, pl);
254 (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
255
256 /* $HKCU{$xlib} || $HKLM{$xlib} . ---; */
257 (void)get_regstr(xlib, &sv2);
258
259 /* $xlib .=
260 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */
261 sprintf(pathstr, "%s/lib", libname);
262 (void)get_emd_part(&sv2, pathstr, ARCHNAME, "bin", pl, Nullch);
263
264 if (!sv1 && !sv2)
265 return Nullch;
266 if (!sv1)
267 return SvPVX(sv2);
268 if (!sv2)
269 return SvPVX(sv1);
270
271 sv_catpvn(sv1, ";", 1);
272 sv_catsv(sv1, sv2);
273
274 return SvPVX(sv1);
275}
276
277char *
278win32_get_sitelib(const char *pl)
279{
280 return win32_get_xlib(pl, "sitelib", "site");
281}
282
283#ifndef PERL_VENDORLIB_NAME
284# define PERL_VENDORLIB_NAME "vendor"
285#endif
286
287char *
288win32_get_vendorlib(const char *pl)
289{
290 return win32_get_xlib(pl, "vendorlib", PERL_VENDORLIB_NAME);
291}
292
293#if !defined(PERL_IMPLICIT_SYS)
294/* since the current process environment is being updated in util.c
295 * the library functions will get the correct environment
296 */
297PerlIO *
298Perl_my_popen(pTHX_ char *cmd, char *mode)
299{
300 printf("popen(%s)\n", cmd);
301
302 Perl_croak(aTHX_ PL_no_func, "popen");
303 return NULL;
304}
305
306long
307Perl_my_pclose(pTHX_ PerlIO *fp)
308{
309 Perl_croak(aTHX_ PL_no_func, "pclose");
310 return -1;
311}
312#endif
313
314DllExport unsigned long
315win32_os_id(void)
316{
317 static OSVERSIONINFOA osver;
318
319 if (osver.dwPlatformId != w32_platform) {
320 memset(&osver, 0, sizeof(OSVERSIONINFOA));
321 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
322 XCEGetVersionExA(&osver);
323 w32_platform = osver.dwPlatformId;
324 }
325 return (unsigned long)w32_platform;
326}
327
328DllExport int
329win32_getpid(void)
330{
331 return xcegetpid();
332}
333
334bool
335Perl_do_exec(pTHX_ char *cmd)
336{
337 Perl_croak_nocontext("exec() unimplemented on this platform");
338 return FALSE;
339}
340
341DllExport int
342win32_pipe(int *pfd, unsigned int size, int mode)
343{
344 Perl_croak(aTHX_ PL_no_func, "pipe");
345 return -1;
346}
347
348DllExport int
349win32_times(struct tms *timebuf)
350{
351 Perl_croak(aTHX_ PL_no_func, "times");
352 return -1;
353}
354
42165d27
VK
355/* TODO */
356bool
357win32_signal()
358{
359 Perl_croak_nocontext("signal() TBD on this platform");
360 return FALSE;
361}
362DllExport void
363win32_clearenv()
364{
365 return;
366}
367
368
e1caacb4
JH
369DllExport char ***
370win32_environ(void)
371{
372 return (&(environ));
373}
374
375DllExport DIR *
376win32_opendir(char *filename)
377{
378 return opendir(filename);
379}
380
381DllExport struct direct *
382win32_readdir(DIR *dirp)
383{
384 return readdir(dirp);
385}
386
387DllExport long
388win32_telldir(DIR *dirp)
389{
390 Perl_croak(aTHX_ PL_no_func, "telldir");
391 return -1;
392}
393
394DllExport void
395win32_seekdir(DIR *dirp, long loc)
396{
397 Perl_croak(aTHX_ PL_no_func, "seekdir");
398}
399
400DllExport void
401win32_rewinddir(DIR *dirp)
402{
403 Perl_croak(aTHX_ PL_no_func, "rewinddir");
404}
405
406DllExport int
407win32_closedir(DIR *dirp)
408{
409 closedir(dirp);
410 return 0;
411}
412
413DllExport int
414win32_kill(int pid, int sig)
415{
416 Perl_croak(aTHX_ PL_no_func, "kill");
417 return -1;
418}
419
420DllExport unsigned int
421win32_sleep(unsigned int t)
422{
423 return xcesleep(t);
424}
425
426DllExport int
427win32_stat(const char *path, struct stat *sbuf)
428{
429 return xcestat(path, sbuf);
430}
431
432DllExport char *
433win32_longpath(char *path)
434{
435 return path;
436}
437
438#ifndef USE_WIN32_RTL_ENV
439
440DllExport char *
441win32_getenv(const char *name)
442{
443 return xcegetenv(name);
444}
445
446DllExport int
447win32_putenv(const char *name)
448{
449 return xceputenv(name);
450}
451
452#endif
453
454static long
455filetime_to_clock(PFILETIME ft)
456{
457 __int64 qw = ft->dwHighDateTime;
458 qw <<= 32;
459 qw |= ft->dwLowDateTime;
460 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
461 return (long) qw;
462}
463
464/* fix utime() so it works on directories in NT */
465static BOOL
466filetime_from_time(PFILETIME pFileTime, time_t Time)
467{
468 struct tm *pTM = localtime(&Time);
469 SYSTEMTIME SystemTime;
470 FILETIME LocalTime;
471
472 if (pTM == NULL)
473 return FALSE;
474
475 SystemTime.wYear = pTM->tm_year + 1900;
476 SystemTime.wMonth = pTM->tm_mon + 1;
477 SystemTime.wDay = pTM->tm_mday;
478 SystemTime.wHour = pTM->tm_hour;
479 SystemTime.wMinute = pTM->tm_min;
480 SystemTime.wSecond = pTM->tm_sec;
481 SystemTime.wMilliseconds = 0;
482
483 return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
484 LocalFileTimeToFileTime(&LocalTime, pFileTime);
485}
486
487DllExport int
488win32_unlink(const char *filename)
489{
490 return xceunlink(filename);
491}
492
493DllExport int
494win32_utime(const char *filename, struct utimbuf *times)
495{
496 return xceutime(filename, (struct _utimbuf *) times);
497}
498
499DllExport int
e2a02c1e
VK
500win32_gettimeofday(struct timeval *tp, void *not_used)
501{
502 return xcegettimeofday(tp,not_used);
503}
504
505DllExport int
e1caacb4
JH
506win32_uname(struct utsname *name)
507{
508 struct hostent *hep;
509 STRLEN nodemax = sizeof(name->nodename)-1;
510 OSVERSIONINFOA osver;
511
512 memset(&osver, 0, sizeof(OSVERSIONINFOA));
513 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
514 if (XCEGetVersionExA(&osver)) {
515 /* sysname */
516 switch (osver.dwPlatformId) {
517 case VER_PLATFORM_WIN32_CE:
518 strcpy(name->sysname, "Windows CE");
519 break;
520 case VER_PLATFORM_WIN32_WINDOWS:
521 strcpy(name->sysname, "Windows");
522 break;
523 case VER_PLATFORM_WIN32_NT:
524 strcpy(name->sysname, "Windows NT");
525 break;
526 case VER_PLATFORM_WIN32s:
527 strcpy(name->sysname, "Win32s");
528 break;
529 default:
530 strcpy(name->sysname, "Win32 Unknown");
531 break;
532 }
533
534 /* release */
535 sprintf(name->release, "%d.%d",
536 osver.dwMajorVersion, osver.dwMinorVersion);
537
538 /* version */
539 sprintf(name->version, "Build %d",
540 osver.dwPlatformId == VER_PLATFORM_WIN32_NT
541 ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff));
542 if (osver.szCSDVersion[0]) {
543 char *buf = name->version + strlen(name->version);
544 sprintf(buf, " (%s)", osver.szCSDVersion);
545 }
546 }
547 else {
548 *name->sysname = '\0';
549 *name->version = '\0';
550 *name->release = '\0';
551 }
552
553 /* nodename */
554 hep = win32_gethostbyname("localhost");
555 if (hep) {
556 STRLEN len = strlen(hep->h_name);
557 if (len <= nodemax) {
558 strcpy(name->nodename, hep->h_name);
559 }
560 else {
561 strncpy(name->nodename, hep->h_name, nodemax);
562 name->nodename[nodemax] = '\0';
563 }
564 }
565 else {
566 DWORD sz = nodemax;
567 if (!XCEGetComputerNameA(name->nodename, &sz))
568 *name->nodename = '\0';
569 }
570
571 /* machine (architecture) */
572 {
573 SYSTEM_INFO info;
574 char *arch;
575 GetSystemInfo(&info);
576
e1caacb4 577 switch (info.wProcessorArchitecture) {
e1caacb4
JH
578 case PROCESSOR_ARCHITECTURE_INTEL:
579 arch = "x86"; break;
580 case PROCESSOR_ARCHITECTURE_MIPS:
581 arch = "mips"; break;
582 case PROCESSOR_ARCHITECTURE_ALPHA:
583 arch = "alpha"; break;
584 case PROCESSOR_ARCHITECTURE_PPC:
585 arch = "ppc"; break;
586 case PROCESSOR_ARCHITECTURE_ARM:
587 arch = "arm"; break;
588 case PROCESSOR_HITACHI_SH3:
589 arch = "sh3"; break;
590 case PROCESSOR_SHx_SH3:
591 arch = "sh3"; break;
592
593 default:
594 arch = "unknown"; break;
595 }
596 strcpy(name->machine, arch);
597 }
598 return 0;
599}
600
e1caacb4
JH
601static UINT timerid = 0;
602
603static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
604{
acfe0abc 605 dTHX;
e1caacb4
JH
606 KillTimer(NULL,timerid);
607 timerid=0;
608 sighandler(14);
609}
e1caacb4
JH
610
611DllExport unsigned int
612win32_alarm(unsigned int sec)
613{
e1caacb4
JH
614 /*
615 * the 'obvious' implentation is SetTimer() with a callback
616 * which does whatever receiving SIGALRM would do
617 * we cannot use SIGALRM even via raise() as it is not
618 * one of the supported codes in <signal.h>
619 *
620 * Snag is unless something is looking at the message queue
621 * nothing happens :-(
622 */
acfe0abc 623 dTHX;
e1caacb4
JH
624 if (sec)
625 {
626 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
627 if (!timerid)
628 Perl_croak_nocontext("Cannot set timer");
629 }
630 else
631 {
632 if (timerid)
633 {
634 KillTimer(NULL,timerid);
635 timerid=0;
636 }
637 }
e1caacb4
JH
638 return 0;
639}
640
641#ifdef HAVE_DES_FCRYPT
642extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf);
643#endif
644
645DllExport char *
646win32_crypt(const char *txt, const char *salt)
647{
acfe0abc 648 dTHX;
e1caacb4
JH
649#ifdef HAVE_DES_FCRYPT
650 dTHR;
651 return des_fcrypt(txt, salt, w32_crypt_buffer);
652#else
653 Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia.");
654 return Nullch;
655#endif
656}
657
658/* C doesn't like repeat struct definitions */
659
660#if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
661
662#ifndef _CRTIMP
663#define _CRTIMP __declspec(dllimport)
664#endif
665
666/*
667 * Control structure for lowio file handles
668 */
669typedef struct {
670 long osfhnd; /* underlying OS file HANDLE */
671 char osfile; /* attributes of file (e.g., open in text mode?) */
672 char pipech; /* one char buffer for handles opened on pipes */
673 int lockinitflag;
674 CRITICAL_SECTION lock;
675} ioinfo;
676
677
678/*
679 * Array of arrays of control structures for lowio files.
680 */
681EXTERN_C _CRTIMP ioinfo* __pioinfo[];
682
683/*
684 * Definition of IOINFO_L2E, the log base 2 of the number of elements in each
685 * array of ioinfo structs.
686 */
687#define IOINFO_L2E 5
688
689/*
690 * Definition of IOINFO_ARRAY_ELTS, the number of elements in ioinfo array
691 */
692#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
693
694/*
695 * Access macros for getting at an ioinfo struct and its fields from a
696 * file handle
697 */
698#define _pioinfo(i) (__pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)))
699#define _osfhnd(i) (_pioinfo(i)->osfhnd)
700#define _osfile(i) (_pioinfo(i)->osfile)
701#define _pipech(i) (_pioinfo(i)->pipech)
702
703#endif
704
705/*
706 * redirected io subsystem for all XS modules
707 *
708 */
709
710DllExport int *
711win32_errno(void)
712{
713 return (&errno);
714}
715
716/* the rest are the remapped stdio routines */
717DllExport FILE *
718win32_stderr(void)
719{
720 return (stderr);
721}
722
723DllExport FILE *
724win32_stdin(void)
725{
726 return (stdin);
727}
728
729DllExport FILE *
730win32_stdout()
731{
732 return (stdout);
733}
734
735DllExport int
736win32_ferror(FILE *fp)
737{
738 return (ferror(fp));
739}
740
741
742DllExport int
743win32_feof(FILE *fp)
744{
745 return (feof(fp));
746}
747
748/*
749 * Since the errors returned by the socket error function
750 * WSAGetLastError() are not known by the library routine strerror
751 * we have to roll our own.
752 */
753
754DllExport char *
755win32_strerror(int e)
756{
757 return xcestrerror(e);
758}
759
760DllExport void
761win32_str_os_error(void *sv, DWORD dwErr)
762{
acfe0abc 763 dTHX;
e1caacb4
JH
764
765 sv_setpvn((SV*)sv, "Error", 5);
766}
767
768
769DllExport int
770win32_fprintf(FILE *fp, const char *format, ...)
771{
772 va_list marker;
773 va_start(marker, format); /* Initialize variable arguments. */
774
775 return (vfprintf(fp, format, marker));
776}
777
778DllExport int
779win32_printf(const char *format, ...)
780{
781 va_list marker;
782 va_start(marker, format); /* Initialize variable arguments. */
783
784 return (vprintf(format, marker));
785}
786
787DllExport int
788win32_vfprintf(FILE *fp, const char *format, va_list args)
789{
790 return (vfprintf(fp, format, args));
791}
792
793DllExport int
794win32_vprintf(const char *format, va_list args)
795{
796 return (vprintf(format, args));
797}
798
799DllExport size_t
800win32_fread(void *buf, size_t size, size_t count, FILE *fp)
801{
802 return fread(buf, size, count, fp);
803}
804
805DllExport size_t
806win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
807{
808 return fwrite(buf, size, count, fp);
809}
810
811DllExport FILE *
812win32_fopen(const char *filename, const char *mode)
813{
814 return xcefopen(filename, mode);
815}
816
817DllExport FILE *
818win32_fdopen(int handle, const char *mode)
819{
820 return palm_fdopen(handle, mode);
821}
822
823DllExport FILE *
824win32_freopen(const char *path, const char *mode, FILE *stream)
825{
826 return xcefreopen(path, mode, stream);
827}
828
829DllExport int
830win32_fclose(FILE *pf)
831{
832 return xcefclose(pf);
833}
834
835DllExport int
836win32_fputs(const char *s,FILE *pf)
837{
838 return fputs(s, pf);
839}
840
841DllExport int
842win32_fputc(int c,FILE *pf)
843{
844 return fputc(c,pf);
845}
846
847DllExport int
848win32_ungetc(int c,FILE *pf)
849{
850 return ungetc(c,pf);
851}
852
853DllExport int
854win32_getc(FILE *pf)
855{
856 return getc(pf);
857}
858
859DllExport int
860win32_fileno(FILE *pf)
861{
862 return palm_fileno(pf);
863}
864
865DllExport void
866win32_clearerr(FILE *pf)
867{
868 clearerr(pf);
869 return;
870}
871
872DllExport int
873win32_fflush(FILE *pf)
874{
875 return fflush(pf);
876}
877
878DllExport long
879win32_ftell(FILE *pf)
880{
881 return ftell(pf);
882}
883
884DllExport int
885win32_fseek(FILE *pf,long offset,int origin)
886{
887 return fseek(pf, offset, origin);
888}
889
cb69f87a
MB
890/* fpos_t seems to be int64 on hpc pro! Really stupid. */
891/* But maybe someday there will be such large disks in a hpc... */
e1caacb4
JH
892DllExport int
893win32_fgetpos(FILE *pf, fpos_t *p)
894{
895 return fgetpos(pf, p);
896}
897
898DllExport int
899win32_fsetpos(FILE *pf, const fpos_t *p)
900{
901 return fsetpos(pf, p);
902}
903
904DllExport void
905win32_rewind(FILE *pf)
906{
907 fseek(pf, 0, SEEK_SET);
908 return;
909}
910
81003058
VK
911DllExport int
912win32_tmpfd(void)
913{
914 dTHX;
915 char prefix[MAX_PATH+1];
916 char filename[MAX_PATH+1];
917 DWORD len = GetTempPath(MAX_PATH, prefix);
918 if (len && len < MAX_PATH) {
919 if (GetTempFileName(prefix, "plx", 0, filename)) {
920 HANDLE fh = CreateFile(filename,
921 DELETE | GENERIC_READ | GENERIC_WRITE,
922 0,
923 NULL,
924 CREATE_ALWAYS,
925 FILE_ATTRIBUTE_NORMAL
926 | FILE_FLAG_DELETE_ON_CLOSE,
927 NULL);
928 if (fh != INVALID_HANDLE_VALUE) {
929 int fd = win32_open_osfhandle((intptr_t)fh, 0);
930 if (fd >= 0) {
931#if defined(__BORLANDC__)
932 setmode(fd,O_BINARY);
933#endif
934 DEBUG_p(PerlIO_printf(Perl_debug_log,
935 "Created tmpfile=%s\n",filename));
936 return fd;
937 }
938 }
939 }
940 }
941 return -1;
942}
943
e1caacb4
JH
944DllExport FILE*
945win32_tmpfile(void)
946{
81003058
VK
947 int fd = win32_tmpfd();
948 if (fd >= 0)
949 return win32_fdopen(fd, "w+b");
950 return NULL;
e1caacb4
JH
951}
952
953DllExport void
954win32_abort(void)
955{
956 xceabort();
957
958 return;
959}
960
961DllExport int
962win32_fstat(int fd, struct stat *sbufptr)
963{
964 return xcefstat(fd, sbufptr);
965}
966
967DllExport int
968win32_link(const char *oldname, const char *newname)
969{
970 Perl_croak(aTHX_ PL_no_func, "link");
971
972 return -1;
973}
974
975DllExport int
976win32_rename(const char *oname, const char *newname)
977{
978 return xcerename(oname, newname);
979}
980
981DllExport int
982win32_setmode(int fd, int mode)
983{
bcdf844e
VK
984 /* currently 'celib' seem to have this function in src, but not
985 * exported. When it will be, we'll uncomment following line.
986 */
987 /* return xcesetmode(fd, mode); */
988 return 0;
e1caacb4
JH
989}
990
991DllExport long
992win32_lseek(int fd, long offset, int origin)
993{
994 return xcelseek(fd, offset, origin);
995}
996
997DllExport long
998win32_tell(int fd)
999{
1000 return xcelseek(fd, 0, SEEK_CUR);
1001}
1002
1003DllExport int
1004win32_open(const char *path, int flag, ...)
1005{
1006 int pmode;
1007 va_list ap;
1008
1009 va_start(ap, flag);
1010 pmode = va_arg(ap, int);
1011 va_end(ap);
1012
1013 return xceopen(path, flag, pmode);
1014}
1015
1016DllExport int
1017win32_close(int fd)
1018{
1019 return xceclose(fd);
1020}
1021
1022DllExport int
1023win32_eof(int fd)
1024{
1025 Perl_croak(aTHX_ PL_no_func, "eof");
1026 return -1;
1027}
1028
1029DllExport int
1030win32_dup(int fd)
1031{
aebd5ec7
VK
1032 //vv Perl_croak(aTHX_ PL_no_func, "dup");
1033 return xcedup(fd); // from celib/ceio.c; requires some more work on it.
e1caacb4
JH
1034}
1035
1036DllExport int
1037win32_dup2(int fd1,int fd2)
1038{
aebd5ec7
VK
1039 //Perl_croak(aTHX_ PL_no_func, "dup2");
1040 return xcedup2(fd1,fd2);
e1caacb4
JH
1041}
1042
1043DllExport int
1044win32_read(int fd, void *buf, unsigned int cnt)
1045{
1046 return xceread(fd, buf, cnt);
1047}
1048
1049DllExport int
1050win32_write(int fd, const void *buf, unsigned int cnt)
1051{
1052 return xcewrite(fd, (void *) buf, cnt);
1053}
1054
1055DllExport int
1056win32_mkdir(const char *dir, int mode)
1057{
1058 return xcemkdir(dir);
1059}
1060
1061DllExport int
1062win32_rmdir(const char *dir)
1063{
1064 return xcermdir(dir);
1065}
1066
1067DllExport int
1068win32_chdir(const char *dir)
1069{
1070 return xcechdir(dir);
1071}
1072
1073DllExport int
1074win32_access(const char *path, int mode)
1075{
1076 return xceaccess(path, mode);
1077}
1078
1079DllExport int
1080win32_chmod(const char *path, int mode)
1081{
1082 return xcechmod(path, mode);
1083}
1084
1085DllExport void
1086win32_perror(const char *str)
1087{
1088 xceperror(str);
1089}
1090
1091DllExport void
1092win32_setbuf(FILE *pf, char *buf)
1093{
1094 Perl_croak(aTHX_ PL_no_func, "setbuf");
1095}
1096
1097DllExport int
1098win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
1099{
1100 return setvbuf(pf, buf, type, size);
1101}
1102
1103DllExport int
1104win32_flushall(void)
1105{
1106 return flushall();
1107}
1108
1109DllExport int
1110win32_fcloseall(void)
1111{
1112 return fcloseall();
1113}
1114
1115DllExport char*
1116win32_fgets(char *s, int n, FILE *pf)
1117{
1118 return fgets(s, n, pf);
1119}
1120
1121DllExport char*
1122win32_gets(char *s)
1123{
1124 return gets(s);
1125}
1126
1127DllExport int
1128win32_fgetc(FILE *pf)
1129{
1130 return fgetc(pf);
1131}
1132
1133DllExport int
1134win32_putc(int c, FILE *pf)
1135{
1136 return putc(c,pf);
1137}
1138
1139DllExport int
1140win32_puts(const char *s)
1141{
1142 return puts(s);
1143}
1144
1145DllExport int
1146win32_getchar(void)
1147{
1148 return getchar();
1149}
1150
1151DllExport int
1152win32_putchar(int c)
1153{
1154 return putchar(c);
1155}
1156
1157#ifdef MYMALLOC
1158
1159#ifndef USE_PERL_SBRK
1160
1161static char *committed = NULL;
1162static char *base = NULL;
1163static char *reserved = NULL;
1164static char *brk = NULL;
1165static DWORD pagesize = 0;
1166static DWORD allocsize = 0;
1167
1168void *
1169sbrk(int need)
1170{
1171 void *result;
1172 if (!pagesize)
1173 {SYSTEM_INFO info;
1174 GetSystemInfo(&info);
1175 /* Pretend page size is larger so we don't perpetually
1176 * call the OS to commit just one page ...
1177 */
1178 pagesize = info.dwPageSize << 3;
1179 allocsize = info.dwAllocationGranularity;
1180 }
1181 /* This scheme fails eventually if request for contiguous
1182 * block is denied so reserve big blocks - this is only
1183 * address space not memory ...
1184 */
1185 if (brk+need >= reserved)
1186 {
1187 DWORD size = 64*1024*1024;
1188 char *addr;
1189 if (committed && reserved && committed < reserved)
1190 {
1191 /* Commit last of previous chunk cannot span allocations */
1192 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
1193 if (addr)
1194 committed = reserved;
1195 }
1196 /* Reserve some (more) space
1197 * Note this is a little sneaky, 1st call passes NULL as reserved
1198 * so lets system choose where we start, subsequent calls pass
1199 * the old end address so ask for a contiguous block
1200 */
1201 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
1202 if (addr)
1203 {
1204 reserved = addr+size;
1205 if (!base)
1206 base = addr;
1207 if (!committed)
1208 committed = base;
1209 if (!brk)
1210 brk = committed;
1211 }
1212 else
1213 {
1214 return (void *) -1;
1215 }
1216 }
1217 result = brk;
1218 brk += need;
1219 if (brk > committed)
1220 {
1221 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
1222 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
1223 if (addr)
1224 {
1225 committed += size;
1226 }
1227 else
1228 return (void *) -1;
1229 }
1230 return result;
1231}
1232
1233#endif
1234#endif
1235
1236DllExport void*
1237win32_malloc(size_t size)
1238{
1239 return malloc(size);
1240}
1241
1242DllExport void*
1243win32_calloc(size_t numitems, size_t size)
1244{
1245 return calloc(numitems,size);
1246}
1247
1248DllExport void*
1249win32_realloc(void *block, size_t size)
1250{
1251 return realloc(block,size);
1252}
1253
1254DllExport void
1255win32_free(void *block)
1256{
1257 free(block);
1258}
1259
1260DllExport int
1261win32_execv(const char *cmdname, const char *const *argv)
1262{
1263 Perl_croak(aTHX_ PL_no_func, "execv");
1264 return -1;
1265}
1266
1267DllExport int
1268win32_execvp(const char *cmdname, const char *const *argv)
1269{
1270 Perl_croak(aTHX_ PL_no_func, "execvp");
1271 return -1;
1272}
1273
1274DllExport void*
1275win32_dynaload(const char* filename)
1276{
acfe0abc 1277 dTHX;
e1caacb4
JH
1278 HMODULE hModule;
1279
1280 hModule = XCELoadLibraryA(filename);
1281
1282 return hModule;
1283}
1284
cb69f87a 1285/* this is needed by Cwd.pm... */
e1caacb4
JH
1286
1287static
1288XS(w32_GetCwd)
1289{
1290 dXSARGS;
1291 char buf[MAX_PATH];
1292 SV *sv = sv_newmortal();
1293
1294 xcegetcwd(buf, sizeof(buf));
1295
1296 sv_setpv(sv, xcestrdup(buf));
1297 EXTEND(SP,1);
1298 SvPOK_on(sv);
1299 ST(0) = sv;
ebdd4fa0
JH
1300#ifndef INCOMPLETE_TAINTS
1301 SvTAINTED_on(ST(0));
1302#endif
e1caacb4
JH
1303 XSRETURN(1);
1304}
1305
1306static
1307XS(w32_SetCwd)
1308{
1309 dXSARGS;
1310
1311 if (items != 1)
1312 Perl_croak(aTHX_ "usage: Win32::SetCwd($cwd)");
1313
1314 if (!xcechdir(SvPV_nolen(ST(0))))
1315 XSRETURN_YES;
1316
1317 XSRETURN_NO;
1318}
1319
1320static
1321XS(w32_GetTickCount)
1322{
1323 dXSARGS;
1324 DWORD msec = GetTickCount();
1325 EXTEND(SP,1);
1326 if ((IV)msec > 0)
1327 XSRETURN_IV(msec);
1328 XSRETURN_NV(msec);
1329}
1330
1331static
1332XS(w32_GetOSVersion)
1333{
1334 dXSARGS;
1335 OSVERSIONINFOA osver;
1336
1337 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
1338 if (!XCEGetVersionExA(&osver)) {
1339 XSRETURN_EMPTY;
1340 }
1341 XPUSHs(newSVpvn(osver.szCSDVersion, strlen(osver.szCSDVersion)));
1342 XPUSHs(newSViv(osver.dwMajorVersion));
1343 XPUSHs(newSViv(osver.dwMinorVersion));
1344 XPUSHs(newSViv(osver.dwBuildNumber));
cb69f87a 1345 /* WINCE = 3 */
e1caacb4
JH
1346 XPUSHs(newSViv(osver.dwPlatformId));
1347 PUTBACK;
1348}
1349
1350static
1351XS(w32_IsWinNT)
1352{
1353 dXSARGS;
1354 EXTEND(SP,1);
1355 XSRETURN_IV(IsWinNT());
1356}
1357
1358static
1359XS(w32_IsWin95)
1360{
1361 dXSARGS;
1362 EXTEND(SP,1);
1363 XSRETURN_IV(IsWin95());
1364}
1365
1366static
1367XS(w32_IsWinCE)
1368{
1369 dXSARGS;
1370 EXTEND(SP,1);
1371 XSRETURN_IV(IsWinCE());
1372}
1373
1374static
1375XS(w32_GetOemInfo)
1376{
1377 dXSARGS;
1378 wchar_t wbuf[126];
1379 char buf[126];
1380
1381 if(SystemParametersInfoW(SPI_GETOEMINFO, sizeof(wbuf), wbuf, FALSE))
1382 WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buf, sizeof(buf), 0, 0);
1383 else
1384 sprintf(buf, "SystemParametersInfo failed: %d", GetLastError());
1385
1386 EXTEND(SP,1);
1387 XSRETURN_PV(buf);
1388}
1389
1390static
1391XS(w32_Sleep)
1392{
1393 dXSARGS;
1394 if (items != 1)
1395 Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)");
1396 Sleep(SvIV(ST(0)));
1397 XSRETURN_YES;
1398}
1399
1400static
1401XS(w32_CopyFile)
1402{
1403 dXSARGS;
1404 BOOL bResult;
1405 if (items != 3)
1406 Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)");
1407
1408 {
1409 char szSourceFile[MAX_PATH+1];
1410 strcpy(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(0))));
1411 bResult = XCECopyFileA(szSourceFile, SvPV_nolen(ST(1)),
1412 !SvTRUE(ST(2)));
1413 }
1414
1415 if (bResult)
1416 XSRETURN_YES;
1417
1418 XSRETURN_NO;
1419}
1420
1421static
1422XS(w32_MessageBox)
1423{
1424 dXSARGS;
1425
1426 char *txt;
1427 unsigned int res;
1428 unsigned int flags = MB_OK;
1429
1430 txt = SvPV_nolen(ST(0));
1431
1432 if (items < 1 || items > 2)
1433 Perl_croak(aTHX_ "usage: Win32::MessageBox($txt, [$flags])");
1434
1435 if(items == 2)
1436 flags = SvIV(ST(1));
1437
1438 res = XCEMessageBoxA(NULL, txt, "Perl", flags);
1439
1440 XSRETURN_IV(res);
1441}
1442
1443static
1444XS(w32_GetPowerStatus)
1445{
1446 dXSARGS;
1447
1448 SYSTEM_POWER_STATUS_EX sps;
1449
1450 if(GetSystemPowerStatusEx(&sps, TRUE) == FALSE)
1451 {
1452 XSRETURN_EMPTY;
1453 }
1454
1455 XPUSHs(newSViv(sps.ACLineStatus));
1456 XPUSHs(newSViv(sps.BatteryFlag));
1457 XPUSHs(newSViv(sps.BatteryLifePercent));
1458 XPUSHs(newSViv(sps.BatteryLifeTime));
1459 XPUSHs(newSViv(sps.BatteryFullLifeTime));
1460 XPUSHs(newSViv(sps.BackupBatteryFlag));
1461 XPUSHs(newSViv(sps.BackupBatteryLifePercent));
1462 XPUSHs(newSViv(sps.BackupBatteryLifeTime));
1463 XPUSHs(newSViv(sps.BackupBatteryFullLifeTime));
1464
1465 PUTBACK;
1466}
1467
1468#if UNDER_CE > 200
1469static
1470XS(w32_ShellEx)
1471{
1472 dXSARGS;
1473
1474 char buf[126];
1475 SHELLEXECUTEINFO si;
1476 char *file, *verb;
1477 wchar_t wfile[MAX_PATH];
1478 wchar_t wverb[20];
1479
1480 if (items != 2)
1481 Perl_croak(aTHX_ "usage: Win32::ShellEx($file, $verb)");
1482
1483 file = SvPV_nolen(ST(0));
1484 verb = SvPV_nolen(ST(1));
1485
1486 memset(&si, 0, sizeof(si));
1487 si.cbSize = sizeof(si);
1488 si.fMask = SEE_MASK_FLAG_NO_UI;
1489
1490 MultiByteToWideChar(CP_ACP, 0, verb, -1,
1491 wverb, sizeof(wverb)/2);
1492 si.lpVerb = (TCHAR *)wverb;
1493
1494 MultiByteToWideChar(CP_ACP, 0, file, -1,
1495 wfile, sizeof(wfile)/2);
1496 si.lpFile = (TCHAR *)wfile;
1497
1498 if(ShellExecuteEx(&si) == FALSE)
1499 {
1500 XSRETURN_NO;
1501 }
1502 XSRETURN_YES;
1503}
1504#endif
1505
1506void
1507Perl_init_os_extras(void)
1508{
acfe0abc 1509 dTHX;
e1caacb4
JH
1510 char *file = __FILE__;
1511 dXSUB_SYS;
1512
1513 w32_perlshell_tokens = Nullch;
1514 w32_perlshell_items = -1;
1515 w32_fdpid = newAV(); /* XX needs to be in Perl_win32_init()? */
1516 New(1313, w32_children, 1, child_tab);
1517 w32_num_children = 0;
1518
1519 newXS("Win32::GetCwd", w32_GetCwd, file);
1520 newXS("Win32::SetCwd", w32_SetCwd, file);
1521 newXS("Win32::GetTickCount", w32_GetTickCount, file);
1522 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
1523#if UNDER_CE > 200
1524 newXS("Win32::ShellEx", w32_ShellEx, file);
1525#endif
1526 newXS("Win32::IsWinNT", w32_IsWinNT, file);
1527 newXS("Win32::IsWin95", w32_IsWin95, file);
1528 newXS("Win32::IsWinCE", w32_IsWinCE, file);
1529 newXS("Win32::CopyFile", w32_CopyFile, file);
1530 newXS("Win32::Sleep", w32_Sleep, file);
1531 newXS("Win32::MessageBox", w32_MessageBox, file);
1532 newXS("Win32::GetPowerStatus", w32_GetPowerStatus, file);
1533 newXS("Win32::GetOemInfo", w32_GetOemInfo, file);
1534}
1535
1536void
1537myexit(void)
1538{
1539 char buf[126];
1540
1541 puts("Hit return");
1542 fgets(buf, sizeof(buf), stdin);
1543}
1544
1545void
1546Perl_win32_init(int *argcp, char ***argvp)
1547{
1548#ifdef UNDER_CE
1549 char *p;
1550
1551 if((p = xcegetenv("PERLDEBUG")) && (p[0] == 'y' || p[0] == 'Y'))
1552 atexit(myexit);
1553#endif
1554
1555 MALLOC_INIT;
1556}
1557
81003058
VK
1558void
1559Perl_win32_term(void)
1560{
1561 OP_REFCNT_TERM;
1562 MALLOC_TERM;
1563}
1564
e1caacb4
JH
1565DllExport int
1566win32_flock(int fd, int oper)
1567{
1568 Perl_croak(aTHX_ PL_no_func, "flock");
1569 return -1;
1570}
1571
1572DllExport int
1573win32_waitpid(int pid, int *status, int flags)
1574{
1575 Perl_croak(aTHX_ PL_no_func, "waitpid");
1576 return -1;
1577}
1578
1579DllExport int
1580win32_wait(int *status)
1581{
1582 Perl_croak(aTHX_ PL_no_func, "wait");
1583 return -1;
1584}
1585
1586int
1587do_spawn(char *cmd)
1588{
1589 return xcesystem(cmd);
1590}
1591
1592int
1593do_aspawn(void *vreally, void **vmark, void **vsp)
1594{
1595 Perl_croak(aTHX_ PL_no_func, "aspawn");
1596 return -1;
1597}
1598
1599int
1600wce_reopen_stdout(char *fname)
1601{
1602 if(xcefreopen(fname, "w", stdout) == NULL)
1603 return -1;
1604
1605 return 0;
1606}
1607
1608void
1609wce_hitreturn()
1610{
1611 char buf[126];
1612
1613 printf("Hit RETURN");
1614 fflush(stdout);
1615 fgets(buf, sizeof(buf), stdin);
1616 return;
1617}
1618
cb69f87a 1619/* //////////////////////////////////////////////////////////////////// */
e1caacb4 1620
e1caacb4
JH
1621void
1622win32_argv2utf8(int argc, char** argv)
1623{
cb69f87a 1624 /* do nothing... */
e1caacb4
JH
1625}
1626
1627void
1628Perl_sys_intern_init(pTHX)
1629{
1630 w32_perlshell_tokens = Nullch;
1631 w32_perlshell_vec = (char**)NULL;
1632 w32_perlshell_items = 0;
1633 w32_fdpid = newAV();
1634 New(1313, w32_children, 1, child_tab);
1635 w32_num_children = 0;
1636# ifdef USE_ITHREADS
1637 w32_pseudo_id = 0;
1638 New(1313, w32_pseudo_children, 1, child_tab);
1639 w32_num_pseudo_children = 0;
1640# endif
1641
1642#ifndef UNDER_CE
1643 w32_init_socktype = 0;
1644#endif
1645}
1646
1647void
1648Perl_sys_intern_clear(pTHX)
1649{
1650 Safefree(w32_perlshell_tokens);
1651 Safefree(w32_perlshell_vec);
1652 /* NOTE: w32_fdpid is freed by sv_clean_all() */
1653 Safefree(w32_children);
1654# ifdef USE_ITHREADS
1655 Safefree(w32_pseudo_children);
1656# endif
1657}
1658
cb69f87a 1659/* //////////////////////////////////////////////////////////////////// */
ca6c63e1
JH
1660
1661#undef getcwd
1662
1663char *
1664getcwd(char *buf, size_t size)
1665{
1666 return xcegetcwd(buf, size);
1667}
1668
1669int
1670isnan(double d)
1671{
1672 return _isnan(d);
1673}
1674
42165d27
VK
1675int
1676win32_open_osfhandle(intptr_t osfhandle, int flags)
1677{
1678 int fh;
1679 char fileflags=0; /* _osfile flags */
1680
18f68570 1681 XCEMessageBoxA(NULL, "NEED TO IMPLEMENT in wince/wince.c(win32_open_osfhandle)", "error", 0);
42165d27
VK
1682 Perl_croak_nocontext("win32_open_osfhandle() TBD on this platform");
1683 return 0;
1684}
18f68570
VK
1685
1686int
1687win32_get_osfhandle(intptr_t osfhandle, int flags)
1688{
1689 int fh;
1690 char fileflags=0; /* _osfile flags */
1691
1692 XCEMessageBoxA(NULL, "NEED TO IMPLEMENT in wince/wince.c(win32_get_osfhandle)", "error", 0);
1693 Perl_croak_nocontext("win32_get_osfhandle() TBD on this platform");
1694 return 0;
1695}
1696
1697/*
1698 * a popen() clone that respects PERL5SHELL
1699 *
1700 * changed to return PerlIO* rather than FILE * by BKS, 11-11-2000
1701 */
1702
1703DllExport PerlIO*
1704win32_popen(const char *command, const char *mode)
1705{
1706 XCEMessageBoxA(NULL, "NEED TO IMPLEMENT in wince/wince.c(win32_popen)", "error", 0);
1707 Perl_croak_nocontext("win32_popen() TBD on this platform");
1708}
1709
1710/*
1711 * pclose() clone
1712 */
1713
1714DllExport int
1715win32_pclose(PerlIO *pf)
1716{
1717#ifdef USE_RTL_POPEN
1718 return _pclose(pf);
1719#else
1720 dTHX;
1721 int childpid, status;
1722 SV *sv;
1723
1724 LOCK_FDPID_MUTEX;
1725 sv = *av_fetch(w32_fdpid, PerlIO_fileno(pf), TRUE);
1726
1727 if (SvIOK(sv))
1728 childpid = SvIVX(sv);
1729 else
1730 childpid = 0;
1731
1732 if (!childpid) {
1733 errno = EBADF;
1734 return -1;
1735 }
1736
1737#ifdef USE_PERLIO
1738 PerlIO_close(pf);
1739#else
1740 fclose(pf);
1741#endif
1742 SvIVX(sv) = 0;
1743 UNLOCK_FDPID_MUTEX;
1744
1745 if (win32_waitpid(childpid, &status, 0) == -1)
1746 return -1;
1747
1748 return status;
1749
1750#endif /* USE_RTL_POPEN */
1751}
1752
1753FILE *
1754win32_fdupopen(FILE *pf)
1755{
1756 FILE* pfdup;
1757 fpos_t pos;
1758 char mode[3];
1759 int fileno = win32_dup(win32_fileno(pf));
1760
1761 XCEMessageBoxA(NULL, "NEED TO IMPLEMENT a place in .../wince/wince.c(win32_fdupopen)", "Perl(developer)", 0);
1762 Perl_croak_nocontext("win32_fdupopen() TBD on this platform");
1763
1764#if 0
1765 /* open the file in the same mode */
1766 if((pf)->_flag & _IOREAD) {
1767 mode[0] = 'r';
1768 mode[1] = 0;
1769 }
1770 else if((pf)->_flag & _IOWRT) {
1771 mode[0] = 'a';
1772 mode[1] = 0;
1773 }
1774 else if((pf)->_flag & _IORW) {
1775 mode[0] = 'r';
1776 mode[1] = '+';
1777 mode[2] = 0;
1778 }
1779
1780 /* it appears that the binmode is attached to the
1781 * file descriptor so binmode files will be handled
1782 * correctly
1783 */
1784 pfdup = win32_fdopen(fileno, mode);
1785
1786 /* move the file pointer to the same position */
1787 if (!fgetpos(pf, &pos)) {
1788 fsetpos(pfdup, &pos);
1789 }
1790#endif
1791 return pfdup;
1792}