1 /* WINCE.C - stuff for Windows CE
3 * Time-stamp: <26/10/01 15:25:20 keuchel@keuchelnt>
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.
9 #define WIN32_LEAN_AND_MEAN
10 #define WIN32IO_IS_STDIO
14 #define PERLIO_NOT_STDIO 0
16 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
27 #define PERL_NO_GET_CONTEXT
38 #include "celib_defs.h"
41 #include "cewin32_defs.h"
42 #include "cecrt_defs.h"
44 #define GetCurrentDirectoryW XCEGetCurrentDirectoryW
47 #include "stdio-palmsize.h"
52 #define EXECF_SPAWN_NOWAIT 3
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
62 # define do_spawn g_do_spawn
64 # define getlogin g_getlogin
67 static void get_shell(void);
68 static long tokenize(const char *str, char **dest, char ***destv);
69 static int do_spawn2(pTHX_ char *cmd, int exectype);
70 static BOOL has_shell_metachars(char *ptr);
71 static long filetime_to_clock(PFILETIME ft);
72 static BOOL filetime_from_time(PFILETIME ft, time_t t);
73 static char * get_emd_part(SV **leading, STRLEN *const len,
75 static void remove_dead_process(long deceased);
76 static long find_pid(int pid);
77 static char * qualified_path(const char *cmd);
78 static char * win32_get_xlib(const char *pl, const char *xlib,
79 const char *libname, STRLEN *const len);
82 static void remove_dead_pseudo_process(long child);
83 static long find_pseudo_pid(int pid);
86 int _fmode = O_TEXT; /* celib do not provide _fmode, so we define it here */
89 HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
90 char w32_module_name[MAX_PATH+1];
93 static DWORD w32_platform = (DWORD)-1;
98 return (win32_os_id() == VER_PLATFORM_WIN32_WINDOWS);
104 return (win32_os_id() == VER_PLATFORM_WIN32_NT);
110 return (win32_os_id() == VER_PLATFORM_WIN32_CE);
114 set_w32_module_name(void)
117 XCEGetModuleFileNameA((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
118 ? XCEGetModuleHandleA(NULL)
119 : w32_perldll_handle),
120 w32_module_name, sizeof(w32_module_name));
122 /* normalize to forward slashes */
123 ptr = w32_module_name;
131 /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
133 get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
135 /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
138 const char *subkey = "Software\\Perl";
142 retval = XCERegOpenKeyExA(hkey, subkey, 0, KEY_READ, &handle);
143 if (retval == ERROR_SUCCESS) {
145 retval = XCERegQueryValueExA(handle, valuename, 0, &type, NULL, &datalen);
146 if (retval == ERROR_SUCCESS && type == REG_SZ) {
149 *svp = sv_2mortal(newSVpvn("",0));
150 SvGROW(*svp, datalen);
151 retval = XCERegQueryValueExA(handle, valuename, 0, NULL,
152 (PBYTE)SvPVX(*svp), &datalen);
153 if (retval == ERROR_SUCCESS) {
155 SvCUR_set(*svp,datalen-1);
163 /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
165 get_regstr(const char *valuename, SV **svp)
167 char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
169 str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
173 /* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
175 get_emd_part(SV **prev_pathp, STRLEN *const len, char *trailing_path, ...)
179 char mod_name[MAX_PATH+1];
183 int oldsize, newsize;
186 va_start(ap, trailing_path);
187 strip = va_arg(ap, char *);
189 sprintf(base, "%d.%d", (int)PERL_REVISION, (int)PERL_VERSION);
190 baselen = strlen(base);
192 if (!*w32_module_name) {
193 set_w32_module_name();
195 strcpy(mod_name, w32_module_name);
196 ptr = strrchr(mod_name, '/');
197 while (ptr && strip) {
198 /* look for directories to skip back */
201 ptr = strrchr(mod_name, '/');
202 /* avoid stripping component if there is no slash,
203 * or it doesn't match ... */
204 if (!ptr || stricmp(ptr+1, strip) != 0) {
205 /* ... but not if component matches m|5\.$patchlevel.*| */
206 if (!ptr || !(*strip == '5' && *(ptr+1) == '5'
207 && strncmp(strip, base, baselen) == 0
208 && strncmp(ptr+1, base, baselen) == 0))
214 strip = va_arg(ap, char *);
222 strcpy(++ptr, trailing_path);
224 /* only add directory if it exists */
225 if (XCEGetFileAttributesA(mod_name) != (DWORD) -1) {
226 /* directory exists */
229 *prev_pathp = sv_2mortal(newSVpvn("",0));
230 sv_catpvn(*prev_pathp, ";", 1);
231 sv_catpv(*prev_pathp, mod_name);
233 *len = SvCUR(*prev_pathp);
234 return SvPVX(*prev_pathp);
241 win32_get_privlib(const char *pl, STRLEN *const len)
244 char *stdlib = "lib";
245 char buffer[MAX_PATH+1];
248 /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
249 sprintf(buffer, "%s-%s", stdlib, pl);
250 if (!get_regstr(buffer, &sv))
251 (void)get_regstr(stdlib, &sv);
253 /* $stdlib .= ";$EMD/../../lib" */
254 return get_emd_part(&sv, len, stdlib, ARCHNAME, "bin", NULL);
258 win32_get_xlib(const char *pl, const char *xlib, const char *libname,
263 char pathstr[MAX_PATH+1];
269 /* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */
270 sprintf(regstr, "%s-%s", xlib, pl);
271 (void)get_regstr(regstr, &sv1);
274 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */
275 sprintf(pathstr, "%s/%s/lib", libname, pl);
276 (void)get_emd_part(&sv1, NULL, pathstr, ARCHNAME, "bin", pl, NULL);
278 /* $HKCU{$xlib} || $HKLM{$xlib} . ---; */
279 (void)get_regstr(xlib, &sv2);
282 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */
283 sprintf(pathstr, "%s/lib", libname);
284 (void)get_emd_part(&sv2, NULL, pathstr, ARCHNAME, "bin", pl, NULL);
291 sv_catpvn(sv1, ";", 1);
301 win32_get_sitelib(const char *pl, STRLEN *const len)
303 return win32_get_xlib(pl, "sitelib", "site", len);
306 #ifndef PERL_VENDORLIB_NAME
307 # define PERL_VENDORLIB_NAME "vendor"
311 win32_get_vendorlib(const char *pl, STRLEN *const len)
313 return win32_get_xlib(pl, "vendorlib", PERL_VENDORLIB_NAME, len);
317 has_shell_metachars(char *ptr)
323 * Scan string looking for redirection (< or >) or pipe
324 * characters (|) that are not in a quoted string.
325 * Shell variable interpolation (%VAR%) can also happen inside strings.
357 #if !defined(PERL_IMPLICIT_SYS)
358 /* since the current process environment is being updated in util.c
359 * the library functions will get the correct environment
362 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
364 printf("popen(%s)\n", cmd);
366 Perl_croak(aTHX_ PL_no_func, "popen");
371 Perl_my_pclose(pTHX_ PerlIO *fp)
373 Perl_croak(aTHX_ PL_no_func, "pclose");
378 DllExport unsigned long
381 static OSVERSIONINFOA osver;
383 if (osver.dwPlatformId != w32_platform) {
384 memset(&osver, 0, sizeof(OSVERSIONINFOA));
385 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
386 XCEGetVersionExA(&osver);
387 w32_platform = osver.dwPlatformId;
389 return (unsigned long)w32_platform;
399 return -((int)w32_pseudo_id);
405 /* Tokenize a string. Words are null-separated, and the list
406 * ends with a doubled null. Any character (except null and
407 * including backslash) may be escaped by preceding it with a
408 * backslash (the backslash will be stripped).
409 * Returns number of words in result buffer.
412 tokenize(const char *str, char **dest, char ***destv)
414 char *retstart = NULL;
415 char **retvstart = 0;
419 int slen = strlen(str);
421 register char **retv;
422 Newx(ret, slen+2, char);
423 Newx(retv, (slen+3)/2, char*);
431 if (*ret == '\\' && *str)
433 else if (*ret == ' ') {
449 retvstart[items] = NULL;
459 win32_pipe(int *pfd, unsigned int size, int mode)
462 Perl_croak(aTHX_ PL_no_func, "pipe");
467 win32_times(struct tms *timebuf)
470 Perl_croak(aTHX_ PL_no_func, "times");
475 win32_signal(int sig, Sighandler_t subcode)
477 return xcesignal(sig, subcode);
484 if (!w32_perlshell_tokens) {
485 /* we don't use COMSPEC here for two reasons:
486 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
487 * uncontrolled unportability of the ensuing scripts.
488 * 2. PERL5SHELL could be set to a shell that may not be fit for
489 * interactive use (which is what most programs look in COMSPEC
492 const char* defaultshell = (IsWinNT()
493 ? "cmd.exe /x/d/c" : "command.com /c");
494 const char *usershell = PerlEnv_getenv("PERL5SHELL");
495 w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
496 &w32_perlshell_tokens,
502 Perl_do_aspawn(pTHX_ SV *really, SV **mark, SV **sp)
504 PERL_ARGS_ASSERT_DO_ASPAWN;
506 Perl_croak(aTHX_ PL_no_func, "aspawn");
510 /* returns pointer to the next unquoted space or the end of the string */
512 find_next_space(const char *s)
514 bool in_quotes = FALSE;
516 /* ignore doubled backslashes, or backslash+quote */
517 if (*s == '\\' && (s[1] == '\\' || s[1] == '"')) {
520 /* keep track of when we're within quotes */
521 else if (*s == '"') {
523 in_quotes = !in_quotes;
525 /* break it up only at spaces that aren't in quotes */
526 else if (!in_quotes && isSPACE(*s))
536 do_spawn2(pTHX_ char *cmd, int exectype)
542 BOOL needToTry = TRUE;
545 /* Save an extra exec if possible. See if there are shell
546 * metacharacters in it */
547 if (!has_shell_metachars(cmd)) {
548 Newx(argv, strlen(cmd) / 2 + 2, char*);
549 Newx(cmd2, strlen(cmd) + 1, char);
552 for (s = cmd2; *s;) {
553 while (*s && isSPACE(*s))
557 s = find_next_space(s);
565 status = win32_spawnvp(P_WAIT, argv[0],
566 (const char* const*)argv);
568 case EXECF_SPAWN_NOWAIT:
569 status = win32_spawnvp(P_NOWAIT, argv[0],
570 (const char* const*)argv);
573 status = win32_execvp(argv[0], (const char* const*)argv);
576 if (status != -1 || errno == 0)
586 Newx(argv, w32_perlshell_items + 2, char*);
587 while (++i < w32_perlshell_items)
588 argv[i] = w32_perlshell_vec[i];
593 status = win32_spawnvp(P_WAIT, argv[0],
594 (const char* const*)argv);
596 case EXECF_SPAWN_NOWAIT:
597 status = win32_spawnvp(P_NOWAIT, argv[0],
598 (const char* const*)argv);
601 status = win32_execvp(argv[0], (const char* const*)argv);
607 if (exectype == EXECF_SPAWN_NOWAIT) {
609 PL_statusvalue = -1; /* >16bits hint for pp_system() */
613 if (ckWARN(WARN_EXEC))
614 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't %s \"%s\": %s",
615 (exectype == EXECF_EXEC ? "exec" : "spawn"),
616 cmd, strerror(errno));
621 PL_statusvalue = status;
627 Perl_do_spawn(pTHX_ char *cmd)
629 PERL_ARGS_ASSERT_DO_SPAWN;
631 return do_spawn2(aTHX_ cmd, EXECF_SPAWN);
635 Perl_do_spawn_nowait(pTHX_ char *cmd)
637 PERL_ARGS_ASSERT_DO_SPAWN_NOWAIT;
639 return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT);
643 Perl_do_exec(pTHX_ const char *cmd)
645 PERL_ARGS_ASSERT_DO_EXEC;
647 do_spawn2(aTHX_ cmd, EXECF_EXEC);
651 /* The idea here is to read all the directory names into a string table
652 * (separated by nulls) and when one of the other dir functions is called
653 * return the pointer to the current file name.
656 win32_opendir(const char *filename)
662 char scanname[MAX_PATH+3];
664 WIN32_FIND_DATAA aFindData;
665 WIN32_FIND_DATAW wFindData;
667 char buffer[MAX_PATH*2];
668 WCHAR wbuffer[MAX_PATH+1];
671 len = strlen(filename);
675 /* check to see if filename is a directory */
676 if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
679 /* Get us a DIR structure */
682 /* Create the search pattern */
683 strcpy(scanname, filename);
685 /* bare drive name means look in cwd for drive */
686 if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') {
687 scanname[len++] = '.';
688 scanname[len++] = '/';
690 else if (scanname[len-1] != '/' && scanname[len-1] != '\\') {
691 scanname[len++] = '/';
693 scanname[len++] = '*';
694 scanname[len] = '\0';
696 /* do the FindFirstFile call */
697 fh = FindFirstFile(PerlDir_mapA(scanname), &aFindData);
699 if (fh == INVALID_HANDLE_VALUE) {
700 DWORD err = GetLastError();
701 /* FindFirstFile() fails on empty drives! */
703 case ERROR_FILE_NOT_FOUND:
705 case ERROR_NO_MORE_FILES:
706 case ERROR_PATH_NOT_FOUND:
709 case ERROR_NOT_ENOUGH_MEMORY:
720 /* now allocate the first part of the string table for
721 * the filenames that we find.
723 ptr = aFindData.cFileName;
729 Newx(dirp->start, dirp->size, char);
730 strcpy(dirp->start, ptr);
732 dirp->end = dirp->curr = dirp->start;
738 /* Readdir just returns the current string pointer and bumps the
739 * string pointer to the nDllExport entry.
741 DllExport struct direct *
742 win32_readdir(DIR *dirp)
747 /* first set up the structure to return */
748 len = strlen(dirp->curr);
749 strcpy(dirp->dirstr.d_name, dirp->curr);
750 dirp->dirstr.d_namlen = len;
753 dirp->dirstr.d_ino = dirp->curr - dirp->start;
755 /* Now set up for the next call to readdir */
756 dirp->curr += len + 1;
757 if (dirp->curr >= dirp->end) {
761 WIN32_FIND_DATAW wFindData;
762 WIN32_FIND_DATAA aFindData;
763 char buffer[MAX_PATH*2];
765 /* finding the next file that matches the wildcard
766 * (which should be all of them in this directory!).
768 res = FindNextFile(dirp->handle, &aFindData);
770 ptr = aFindData.cFileName;
772 long endpos = dirp->end - dirp->start;
773 long newsize = endpos + strlen(ptr) + 1;
774 /* bump the string table size by enough for the
775 * new name and its null terminator */
776 while (newsize > dirp->size) {
777 long curpos = dirp->curr - dirp->start;
779 Renew(dirp->start, dirp->size, char);
780 dirp->curr = dirp->start + curpos;
782 strcpy(dirp->start + endpos, ptr);
783 dirp->end = dirp->start + newsize;
789 return &(dirp->dirstr);
795 /* Telldir returns the current string pointer position */
797 win32_telldir(DIR *dirp)
799 return (dirp->curr - dirp->start);
803 /* Seekdir moves the string pointer to a previously saved position
804 * (returned by telldir).
807 win32_seekdir(DIR *dirp, long loc)
809 dirp->curr = dirp->start + loc;
812 /* Rewinddir resets the string pointer to the start */
814 win32_rewinddir(DIR *dirp)
816 dirp->curr = dirp->start;
819 /* free the memory allocated by opendir */
821 win32_closedir(DIR *dirp)
824 if (dirp->handle != INVALID_HANDLE_VALUE)
825 FindClose(dirp->handle);
826 Safefree(dirp->start);
832 /////!!!!!!!!!!! return here and do right stuff!!!!
835 win32_opendir(const char *filename)
837 return opendir(filename);
840 DllExport struct direct *
841 win32_readdir(DIR *dirp)
843 return readdir(dirp);
847 win32_telldir(DIR *dirp)
850 Perl_croak(aTHX_ PL_no_func, "telldir");
855 win32_seekdir(DIR *dirp, long loc)
858 Perl_croak(aTHX_ PL_no_func, "seekdir");
862 win32_rewinddir(DIR *dirp)
865 Perl_croak(aTHX_ PL_no_func, "rewinddir");
869 win32_closedir(DIR *dirp)
877 win32_kill(int pid, int sig)
880 Perl_croak(aTHX_ PL_no_func, "kill");
885 win32_stat(const char *path, struct stat *sbuf)
887 return xcestat(path, sbuf);
891 win32_longpath(char *path)
897 win32_getenv(const char *name)
899 return xcegetenv(name);
903 win32_putenv(const char *name)
905 return xceputenv(name);
909 filetime_to_clock(PFILETIME ft)
911 __int64 qw = ft->dwHighDateTime;
913 qw |= ft->dwLowDateTime;
914 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
918 /* fix utime() so it works on directories in NT */
920 filetime_from_time(PFILETIME pFileTime, time_t Time)
922 struct tm *pTM = localtime(&Time);
923 SYSTEMTIME SystemTime;
929 SystemTime.wYear = pTM->tm_year + 1900;
930 SystemTime.wMonth = pTM->tm_mon + 1;
931 SystemTime.wDay = pTM->tm_mday;
932 SystemTime.wHour = pTM->tm_hour;
933 SystemTime.wMinute = pTM->tm_min;
934 SystemTime.wSecond = pTM->tm_sec;
935 SystemTime.wMilliseconds = 0;
937 return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
938 LocalFileTimeToFileTime(&LocalTime, pFileTime);
942 win32_unlink(const char *filename)
944 return xceunlink(filename);
948 win32_utime(const char *filename, struct utimbuf *times)
950 return xceutime(filename, (struct _utimbuf *) times);
954 win32_gettimeofday(struct timeval *tp, void *not_used)
956 return xcegettimeofday(tp,not_used);
960 win32_uname(struct utsname *name)
963 STRLEN nodemax = sizeof(name->nodename)-1;
964 OSVERSIONINFOA osver;
966 memset(&osver, 0, sizeof(OSVERSIONINFOA));
967 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
968 if (XCEGetVersionExA(&osver)) {
970 switch (osver.dwPlatformId) {
971 case VER_PLATFORM_WIN32_CE:
972 strcpy(name->sysname, "Windows CE");
974 case VER_PLATFORM_WIN32_WINDOWS:
975 strcpy(name->sysname, "Windows");
977 case VER_PLATFORM_WIN32_NT:
978 strcpy(name->sysname, "Windows NT");
980 case VER_PLATFORM_WIN32s:
981 strcpy(name->sysname, "Win32s");
984 strcpy(name->sysname, "Win32 Unknown");
989 sprintf(name->release, "%d.%d",
990 osver.dwMajorVersion, osver.dwMinorVersion);
993 sprintf(name->version, "Build %d",
994 osver.dwPlatformId == VER_PLATFORM_WIN32_NT
995 ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff));
996 if (osver.szCSDVersion[0]) {
997 char *buf = name->version + strlen(name->version);
998 sprintf(buf, " (%s)", osver.szCSDVersion);
1002 *name->sysname = '\0';
1003 *name->version = '\0';
1004 *name->release = '\0';
1008 hep = win32_gethostbyname("localhost");
1010 STRLEN len = strlen(hep->h_name);
1011 if (len <= nodemax) {
1012 strcpy(name->nodename, hep->h_name);
1015 strncpy(name->nodename, hep->h_name, nodemax);
1016 name->nodename[nodemax] = '\0';
1021 if (!XCEGetComputerNameA(name->nodename, &sz))
1022 *name->nodename = '\0';
1025 /* machine (architecture) */
1029 GetSystemInfo(&info);
1031 switch (info.wProcessorArchitecture) {
1032 case PROCESSOR_ARCHITECTURE_INTEL:
1033 arch = "x86"; break;
1034 case PROCESSOR_ARCHITECTURE_MIPS:
1035 arch = "mips"; break;
1036 case PROCESSOR_ARCHITECTURE_ALPHA:
1037 arch = "alpha"; break;
1038 case PROCESSOR_ARCHITECTURE_PPC:
1039 arch = "ppc"; break;
1040 case PROCESSOR_ARCHITECTURE_ARM:
1041 arch = "arm"; break;
1042 case PROCESSOR_HITACHI_SH3:
1043 arch = "sh3"; break;
1044 case PROCESSOR_SHx_SH3:
1045 arch = "sh3"; break;
1048 arch = "unknown"; break;
1050 strcpy(name->machine, arch);
1055 /* Timing related stuff */
1058 do_raise(pTHX_ int sig)
1060 if (sig < SIG_SIZE) {
1061 Sighandler_t handler = w32_sighandler[sig];
1062 if (handler == SIG_IGN) {
1065 else if (handler != SIG_DFL) {
1070 /* Choose correct default behaviour */
1086 /* Tell caller to exit thread/process as approriate */
1091 sig_terminate(pTHX_ int sig)
1093 Perl_warn(aTHX_ "Terminating on signal SIG%s(%d)\n",PL_sig_name[sig], sig);
1094 /* exit() seems to be safe, my_exit() or die() is a problem in ^C
1101 win32_async_check(pTHX)
1105 /* Passing PeekMessage -1 as HWND (2nd arg) only get PostThreadMessage() messages
1106 * and ignores window messages - should co-exist better with windows apps e.g. Tk
1108 while (PeekMessage(&msg, (HWND)-1, 0, 0, PM_REMOVE|PM_NOYIELD)) {
1110 switch(msg.message) {
1113 /* Perhaps some other messages could map to signals ? ... */
1116 /* Treat WM_QUIT like SIGHUP? */
1122 /* We use WM_USER to fake kill() with other signals */
1126 if (do_raise(aTHX_ sig)) {
1127 sig_terminate(aTHX_ sig);
1133 /* alarm() is a one-shot but SetTimer() repeats so kill it */
1135 KillTimer(NULL,w32_timerid);
1138 /* Now fake a call to signal handler */
1139 if (do_raise(aTHX_ 14)) {
1140 sig_terminate(aTHX_ 14);
1145 /* Otherwise do normal Win32 thing - in case it is useful */
1147 TranslateMessage(&msg);
1148 DispatchMessage(&msg);
1155 /* Above or other stuff may have set a signal flag */
1156 if (PL_sig_pending) {
1162 /* This function will not return until the timeout has elapsed, or until
1163 * one of the handles is ready. */
1165 win32_msgwait(pTHX_ DWORD count, LPHANDLE handles, DWORD timeout, LPDWORD resultp)
1167 /* We may need several goes at this - so compute when we stop */
1169 if (timeout != INFINITE) {
1170 ticks = GetTickCount();
1174 DWORD result = MsgWaitForMultipleObjects(count,handles,FALSE,timeout-ticks, QS_ALLEVENTS);
1177 if (result == WAIT_TIMEOUT) {
1178 /* Ran out of time - explicit return of zero to avoid -ve if we
1179 have scheduling issues
1183 if (timeout != INFINITE) {
1184 ticks = GetTickCount();
1186 if (result == WAIT_OBJECT_0 + count) {
1187 /* Message has arrived - check it */
1188 (void)win32_async_check(aTHX);
1191 /* Not timeout or message - one of handles is ready */
1195 /* compute time left to wait */
1196 ticks = timeout - ticks;
1197 /* If we are past the end say zero */
1198 return (ticks > 0) ? ticks : 0;
1201 static UINT timerid = 0;
1203 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
1206 KillTimer(NULL,timerid);
1211 DllExport unsigned int
1212 win32_sleep(unsigned int t)
1217 DllExport unsigned int
1218 win32_alarm(unsigned int sec)
1221 * the 'obvious' implentation is SetTimer() with a callback
1222 * which does whatever receiving SIGALRM would do
1223 * we cannot use SIGALRM even via raise() as it is not
1224 * one of the supported codes in <signal.h>
1226 * Snag is unless something is looking at the message queue
1227 * nothing happens :-(
1232 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
1234 Perl_croak_nocontext("Cannot set timer");
1240 KillTimer(NULL,timerid);
1247 #ifdef HAVE_DES_FCRYPT
1248 extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf);
1252 win32_crypt(const char *txt, const char *salt)
1255 #ifdef HAVE_DES_FCRYPT
1257 return des_fcrypt(txt, salt, w32_crypt_buffer);
1259 Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia.");
1266 * redirected io subsystem for all XS modules
1279 return (&(environ));
1282 /* the rest are the remapped stdio routines */
1289 char *g_getlogin() {
1290 return "no-getlogin";
1306 win32_ferror(FILE *fp)
1308 return (ferror(fp));
1313 win32_feof(FILE *fp)
1319 * Since the errors returned by the socket error function
1320 * WSAGetLastError() are not known by the library routine strerror
1321 * we have to roll our own.
1325 win32_strerror(int e)
1327 return xcestrerror(e);
1331 win32_str_os_error(void *sv, DWORD dwErr)
1335 sv_setpvn((SV*)sv, "Error", 5);
1340 win32_fprintf(FILE *fp, const char *format, ...)
1343 va_start(marker, format); /* Initialize variable arguments. */
1345 return (vfprintf(fp, format, marker));
1349 win32_printf(const char *format, ...)
1352 va_start(marker, format); /* Initialize variable arguments. */
1354 return (vprintf(format, marker));
1358 win32_vfprintf(FILE *fp, const char *format, va_list args)
1360 return (vfprintf(fp, format, args));
1364 win32_vprintf(const char *format, va_list args)
1366 return (vprintf(format, args));
1370 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1372 return fread(buf, size, count, fp);
1376 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1378 return fwrite(buf, size, count, fp);
1382 win32_fopen(const char *filename, const char *mode)
1384 return xcefopen(filename, mode);
1388 win32_fdopen(int handle, const char *mode)
1390 return palm_fdopen(handle, mode);
1394 win32_freopen(const char *path, const char *mode, FILE *stream)
1396 return xcefreopen(path, mode, stream);
1400 win32_fclose(FILE *pf)
1402 return xcefclose(pf);
1406 win32_fputs(const char *s,FILE *pf)
1408 return fputs(s, pf);
1412 win32_fputc(int c,FILE *pf)
1418 win32_ungetc(int c,FILE *pf)
1420 return ungetc(c,pf);
1424 win32_getc(FILE *pf)
1430 win32_fileno(FILE *pf)
1432 return palm_fileno(pf);
1436 win32_clearerr(FILE *pf)
1443 win32_fflush(FILE *pf)
1449 win32_ftell(FILE *pf)
1455 win32_fseek(FILE *pf, Off_t offset,int origin)
1457 return fseek(pf, offset, origin);
1460 /* fpos_t seems to be int64 on hpc pro! Really stupid. */
1461 /* But maybe someday there will be such large disks in a hpc... */
1463 win32_fgetpos(FILE *pf, fpos_t *p)
1465 return fgetpos(pf, p);
1469 win32_fsetpos(FILE *pf, const fpos_t *p)
1471 return fsetpos(pf, p);
1475 win32_rewind(FILE *pf)
1477 fseek(pf, 0, SEEK_SET);
1485 char prefix[MAX_PATH+1];
1486 char filename[MAX_PATH+1];
1487 DWORD len = GetTempPath(MAX_PATH, prefix);
1488 if (len && len < MAX_PATH) {
1489 if (GetTempFileName(prefix, "plx", 0, filename)) {
1490 HANDLE fh = CreateFile(filename,
1491 DELETE | GENERIC_READ | GENERIC_WRITE,
1495 FILE_ATTRIBUTE_NORMAL
1496 | FILE_FLAG_DELETE_ON_CLOSE,
1498 if (fh != INVALID_HANDLE_VALUE) {
1499 int fd = win32_open_osfhandle((intptr_t)fh, 0);
1501 #if defined(__BORLANDC__)
1502 setmode(fd,O_BINARY);
1504 DEBUG_p(PerlIO_printf(Perl_debug_log,
1505 "Created tmpfile=%s\n",filename));
1517 int fd = win32_tmpfd();
1519 return win32_fdopen(fd, "w+b");
1532 win32_fstat(int fd, struct stat *sbufptr)
1534 return xcefstat(fd, sbufptr);
1538 win32_link(const char *oldname, const char *newname)
1541 Perl_croak(aTHX_ PL_no_func, "link");
1547 win32_rename(const char *oname, const char *newname)
1549 return xcerename(oname, newname);
1553 win32_setmode(int fd, int mode)
1555 /* currently 'celib' seem to have this function in src, but not
1556 * exported. When it will be, we'll uncomment following line.
1558 /* return xcesetmode(fd, mode); */
1563 win32_chsize(int fd, Off_t size)
1565 return chsize(fd, size);
1569 win32_lseek(int fd, Off_t offset, int origin)
1571 return xcelseek(fd, offset, origin);
1577 return xcelseek(fd, 0, SEEK_CUR);
1581 win32_open(const char *path, int flag, ...)
1587 pmode = va_arg(ap, int);
1590 return xceopen(path, flag, pmode);
1596 return xceclose(fd);
1603 Perl_croak(aTHX_ PL_no_func, "eof");
1610 return xcedup(fd); /* from celib/ceio.c; requires some more work on it */
1614 win32_dup2(int fd1,int fd2)
1616 return xcedup2(fd1,fd2);
1620 win32_read(int fd, void *buf, unsigned int cnt)
1622 return xceread(fd, buf, cnt);
1626 win32_write(int fd, const void *buf, unsigned int cnt)
1628 return xcewrite(fd, (void *) buf, cnt);
1632 win32_mkdir(const char *dir, int mode)
1634 return xcemkdir(dir);
1638 win32_rmdir(const char *dir)
1640 return xcermdir(dir);
1644 win32_chdir(const char *dir)
1646 return xcechdir(dir);
1650 win32_access(const char *path, int mode)
1652 return xceaccess(path, mode);
1656 win32_chmod(const char *path, int mode)
1658 return xcechmod(path, mode);
1662 create_command_line(char *cname, STRLEN clen, const char * const *args)
1669 bool bat_file = FALSE;
1670 bool cmd_shell = FALSE;
1671 bool dumb_shell = FALSE;
1672 bool extra_quotes = FALSE;
1673 bool quote_next = FALSE;
1676 cname = (char*)args[0];
1678 /* The NT cmd.exe shell has the following peculiarity that needs to be
1679 * worked around. It strips a leading and trailing dquote when any
1680 * of the following is true:
1681 * 1. the /S switch was used
1682 * 2. there are more than two dquotes
1683 * 3. there is a special character from this set: &<>()@^|
1684 * 4. no whitespace characters within the two dquotes
1685 * 5. string between two dquotes isn't an executable file
1686 * To work around this, we always add a leading and trailing dquote
1687 * to the string, if the first argument is either "cmd.exe" or "cmd",
1688 * and there were at least two or more arguments passed to cmd.exe
1689 * (not including switches).
1690 * XXX the above rules (from "cmd /?") don't seem to be applied
1691 * always, making for the convolutions below :-(
1695 clen = strlen(cname);
1698 && (stricmp(&cname[clen-4], ".bat") == 0
1699 || (IsWinNT() && stricmp(&cname[clen-4], ".cmd") == 0)))
1705 char *exe = strrchr(cname, '/');
1706 char *exe2 = strrchr(cname, '\\');
1713 if (stricmp(exe, "cmd.exe") == 0 || stricmp(exe, "cmd") == 0) {
1717 else if (stricmp(exe, "command.com") == 0
1718 || stricmp(exe, "command") == 0)
1725 DEBUG_p(PerlIO_printf(Perl_debug_log, "Args "));
1726 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1727 STRLEN curlen = strlen(arg);
1728 if (!(arg[0] == '"' && arg[curlen-1] == '"'))
1729 len += 2; /* assume quoting needed (worst case) */
1731 DEBUG_p(PerlIO_printf(Perl_debug_log, "[%s]",arg));
1733 DEBUG_p(PerlIO_printf(Perl_debug_log, "\n"));
1736 Newx(cmd, len, char);
1741 extra_quotes = TRUE;
1744 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1746 STRLEN curlen = strlen(arg);
1748 /* we want to protect empty arguments and ones with spaces with
1749 * dquotes, but only if they aren't already there */
1754 else if (quote_next) {
1755 /* see if it really is multiple arguments pretending to
1756 * be one and force a set of quotes around it */
1757 if (*find_next_space(arg))
1760 else if (!(arg[0] == '"' && curlen > 1 && arg[curlen-1] == '"')) {
1762 while (i < curlen) {
1763 if (isSPACE(arg[i])) {
1766 else if (arg[i] == '"') {
1790 && *arg == '/' /* see if arg is "/c", "/x/c", "/x/d/c" etc. */
1791 && stricmp(arg+curlen-2, "/c") == 0)
1793 /* is there a next argument? */
1794 if (args[index+1]) {
1795 /* are there two or more next arguments? */
1796 if (args[index+2]) {
1798 extra_quotes = TRUE;
1801 /* single argument, force quoting if it has spaces */
1817 qualified_path(const char *cmd)
1821 char *fullcmd, *curfullcmd;
1827 fullcmd = (char*)cmd;
1829 if (*fullcmd == '/' || *fullcmd == '\\')
1836 pathstr = PerlEnv_getenv("PATH");
1837 Newx(fullcmd, MAX_PATH+1, char);
1838 curfullcmd = fullcmd;
1843 /* start by appending the name to the current prefix */
1844 strcpy(curfullcmd, cmd);
1845 curfullcmd += cmdlen;
1847 /* if it doesn't end with '.', or has no extension, try adding
1848 * a trailing .exe first */
1849 if (cmd[cmdlen-1] != '.'
1850 && (cmdlen < 4 || cmd[cmdlen-4] != '.'))
1852 strcpy(curfullcmd, ".exe");
1853 res = GetFileAttributes(fullcmd);
1854 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1859 /* that failed, try the bare name */
1860 res = GetFileAttributes(fullcmd);
1861 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1864 /* quit if no other path exists, or if cmd already has path */
1865 if (!pathstr || !*pathstr || has_slash)
1868 /* skip leading semis */
1869 while (*pathstr == ';')
1872 /* build a new prefix from scratch */
1873 curfullcmd = fullcmd;
1874 while (*pathstr && *pathstr != ';') {
1875 if (*pathstr == '"') { /* foo;"baz;etc";bar */
1876 pathstr++; /* skip initial '"' */
1877 while (*pathstr && *pathstr != '"') {
1878 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1879 *curfullcmd++ = *pathstr;
1883 pathstr++; /* skip trailing '"' */
1886 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1887 *curfullcmd++ = *pathstr;
1892 pathstr++; /* skip trailing semi */
1893 if (curfullcmd > fullcmd /* append a dir separator */
1894 && curfullcmd[-1] != '/' && curfullcmd[-1] != '\\')
1896 *curfullcmd++ = '\\';
1904 /* The following are just place holders.
1905 * Some hosts may provide and environment that the OS is
1906 * not tracking, therefore, these host must provide that
1907 * environment and the current directory to CreateProcess
1911 win32_get_childenv(void)
1917 win32_free_childenv(void* d)
1922 win32_clearenv(void)
1924 char *envv = GetEnvironmentStrings();
1928 char *end = strchr(cur,'=');
1929 if (end && end != cur) {
1931 xcesetenv(cur, "", 0);
1933 cur = end + strlen(end+1)+2;
1935 else if ((len = strlen(cur)))
1938 FreeEnvironmentStrings(envv);
1942 win32_get_childdir(void)
1946 char szfilename[MAX_PATH+1];
1947 GetCurrentDirectoryA(MAX_PATH+1, szfilename);
1949 Newx(ptr, strlen(szfilename)+1, char);
1950 strcpy(ptr, szfilename);
1955 win32_free_childdir(char* d)
1961 /* XXX this needs to be made more compatible with the spawnvp()
1962 * provided by the various RTLs. In particular, searching for
1963 * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented.
1964 * This doesn't significantly affect perl itself, because we
1965 * always invoke things using PERL5SHELL if a direct attempt to
1966 * spawn the executable fails.
1968 * XXX splitting and rejoining the commandline between do_aspawn()
1969 * and win32_spawnvp() could also be avoided.
1973 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1975 #ifdef USE_RTL_SPAWNVP
1976 return spawnvp(mode, cmdname, (char * const *)argv);
1983 STARTUPINFO StartupInfo;
1984 PROCESS_INFORMATION ProcessInformation;
1987 char *fullcmd = NULL;
1988 char *cname = (char *)cmdname;
1992 clen = strlen(cname);
1993 /* if command name contains dquotes, must remove them */
1994 if (strchr(cname, '"')) {
1996 Newx(cname,clen+1,char);
2009 cmd = create_command_line(cname, clen, argv);
2011 env = PerlEnv_get_childenv();
2012 dir = PerlEnv_get_childdir();
2015 case P_NOWAIT: /* asynch + remember result */
2016 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
2021 /* Create a new process group so we can use GenerateConsoleCtrlEvent()
2024 /* not supported on CE create |= CREATE_NEW_PROCESS_GROUP; */
2027 case P_WAIT: /* synchronous execution */
2029 default: /* invalid mode */
2034 memset(&StartupInfo,0,sizeof(StartupInfo));
2035 StartupInfo.cb = sizeof(StartupInfo);
2036 memset(&tbl,0,sizeof(tbl));
2037 PerlEnv_get_child_IO(&tbl);
2038 StartupInfo.dwFlags = tbl.dwFlags;
2039 StartupInfo.dwX = tbl.dwX;
2040 StartupInfo.dwY = tbl.dwY;
2041 StartupInfo.dwXSize = tbl.dwXSize;
2042 StartupInfo.dwYSize = tbl.dwYSize;
2043 StartupInfo.dwXCountChars = tbl.dwXCountChars;
2044 StartupInfo.dwYCountChars = tbl.dwYCountChars;
2045 StartupInfo.dwFillAttribute = tbl.dwFillAttribute;
2046 StartupInfo.wShowWindow = tbl.wShowWindow;
2047 StartupInfo.hStdInput = tbl.childStdIn;
2048 StartupInfo.hStdOutput = tbl.childStdOut;
2049 StartupInfo.hStdError = tbl.childStdErr;
2050 if (StartupInfo.hStdInput == INVALID_HANDLE_VALUE &&
2051 StartupInfo.hStdOutput == INVALID_HANDLE_VALUE &&
2052 StartupInfo.hStdError == INVALID_HANDLE_VALUE)
2054 create |= CREATE_NEW_CONSOLE;
2057 StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
2059 if (w32_use_showwindow) {
2060 StartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
2061 StartupInfo.wShowWindow = w32_showwindow;
2064 DEBUG_p(PerlIO_printf(Perl_debug_log, "Spawning [%s] with [%s]\n",
2067 if (!CreateProcess(cname, /* search PATH to find executable */
2068 cmd, /* executable, and its arguments */
2069 NULL, /* process attributes */
2070 NULL, /* thread attributes */
2071 TRUE, /* inherit handles */
2072 create, /* creation flags */
2073 (LPVOID)env, /* inherit environment */
2074 dir, /* inherit cwd */
2076 &ProcessInformation))
2078 /* initial NULL argument to CreateProcess() does a PATH
2079 * search, but it always first looks in the directory
2080 * where the current process was started, which behavior
2081 * is undesirable for backward compatibility. So we
2082 * jump through our own hoops by picking out the path
2083 * we really want it to use. */
2085 fullcmd = qualified_path(cname);
2087 if (cname != cmdname)
2090 DEBUG_p(PerlIO_printf(Perl_debug_log,
2091 "Retrying [%s] with same args\n",
2101 if (mode == P_NOWAIT) {
2102 /* asynchronous spawn -- store handle, return PID */
2103 ret = (int)ProcessInformation.dwProcessId;
2104 if (IsWin95() && ret < 0)
2107 w32_child_handles[w32_num_children] = ProcessInformation.hProcess;
2108 w32_child_pids[w32_num_children] = (DWORD)ret;
2113 win32_msgwait(aTHX_ 1, &ProcessInformation.hProcess, INFINITE, NULL);
2114 /* FIXME: if msgwait returned due to message perhaps forward the
2115 "signal" to the process
2117 GetExitCodeProcess(ProcessInformation.hProcess, &status);
2119 CloseHandle(ProcessInformation.hProcess);
2122 CloseHandle(ProcessInformation.hThread);
2125 PerlEnv_free_childenv(env);
2126 PerlEnv_free_childdir(dir);
2128 if (cname != cmdname)
2135 win32_execv(const char *cmdname, const char *const *argv)
2138 Perl_croak(aTHX_ PL_no_func, "execv");
2143 win32_execvp(const char *cmdname, const char *const *argv)
2146 Perl_croak(aTHX_ PL_no_func, "execvp");
2151 win32_perror(const char *str)
2157 win32_setbuf(FILE *pf, char *buf)
2160 Perl_croak(aTHX_ PL_no_func, "setbuf");
2164 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
2166 return setvbuf(pf, buf, type, size);
2170 win32_flushall(void)
2176 win32_fcloseall(void)
2182 win32_fgets(char *s, int n, FILE *pf)
2184 return fgets(s, n, pf);
2194 win32_fgetc(FILE *pf)
2200 win32_putc(int c, FILE *pf)
2206 win32_puts(const char *s)
2218 win32_putchar(int c)
2225 #ifndef USE_PERL_SBRK
2227 static char *committed = NULL;
2228 static char *base = NULL;
2229 static char *reserved = NULL;
2230 static char *brk = NULL;
2231 static DWORD pagesize = 0;
2232 static DWORD allocsize = 0;
2240 GetSystemInfo(&info);
2241 /* Pretend page size is larger so we don't perpetually
2242 * call the OS to commit just one page ...
2244 pagesize = info.dwPageSize << 3;
2245 allocsize = info.dwAllocationGranularity;
2247 /* This scheme fails eventually if request for contiguous
2248 * block is denied so reserve big blocks - this is only
2249 * address space not memory ...
2251 if (brk+need >= reserved)
2253 DWORD size = 64*1024*1024;
2255 if (committed && reserved && committed < reserved)
2257 /* Commit last of previous chunk cannot span allocations */
2258 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
2260 committed = reserved;
2262 /* Reserve some (more) space
2263 * Note this is a little sneaky, 1st call passes NULL as reserved
2264 * so lets system choose where we start, subsequent calls pass
2265 * the old end address so ask for a contiguous block
2267 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
2270 reserved = addr+size;
2285 if (brk > committed)
2287 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
2288 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
2303 win32_malloc(size_t size)
2305 return malloc(size);
2309 win32_calloc(size_t numitems, size_t size)
2311 return calloc(numitems,size);
2315 win32_realloc(void *block, size_t size)
2317 return realloc(block,size);
2321 win32_free(void *block)
2327 win32_open_osfhandle(intptr_t osfhandle, int flags)
2330 char fileflags=0; /* _osfile flags */
2332 Perl_croak_nocontext("win32_open_osfhandle() TBD on this platform");
2337 win32_get_osfhandle(int fd)
2340 char fileflags=0; /* _osfile flags */
2342 Perl_croak_nocontext("win32_get_osfhandle() TBD on this platform");
2347 win32_fdupopen(FILE *pf)
2352 int fileno = win32_dup(win32_fileno(pf));
2353 int fmode = palm_fgetmode(pfdup);
2355 fprintf(stderr,"DEBUG for win32_fdupopen()\n");
2357 /* open the file in the same mode */
2358 if(fmode & O_RDONLY) {
2362 else if(fmode & O_APPEND) {
2366 else if(fmode & O_RDWR) {
2372 /* it appears that the binmode is attached to the
2373 * file descriptor so binmode files will be handled
2376 pfdup = win32_fdopen(fileno, mode);
2378 /* move the file pointer to the same position */
2379 if (!fgetpos(pf, &pos)) {
2380 fsetpos(pfdup, &pos);
2386 win32_dynaload(const char* filename)
2391 hModule = XCELoadLibraryA(filename);
2396 /* this is needed by Cwd.pm... */
2403 SV *sv = sv_newmortal();
2405 xcegetcwd(buf, sizeof(buf));
2407 sv_setpv(sv, xcestrdup(buf));
2411 #ifndef INCOMPLETE_TAINTS
2412 SvTAINTED_on(ST(0));
2423 Perl_croak(aTHX_ "usage: Win32::SetCwd($cwd)");
2425 if (!xcechdir(SvPV_nolen(ST(0))))
2432 XS(w32_GetTickCount)
2435 DWORD msec = GetTickCount();
2443 XS(w32_GetOSVersion)
2446 OSVERSIONINFOA osver;
2448 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
2449 if (!XCEGetVersionExA(&osver)) {
2452 mXPUSHp(osver.szCSDVersion, strlen(osver.szCSDVersion));
2453 mXPUSHi(osver.dwMajorVersion);
2454 mXPUSHi(osver.dwMinorVersion);
2455 mXPUSHi(osver.dwBuildNumber);
2457 mXPUSHi(osver.dwPlatformId);
2466 XSRETURN_IV(IsWinNT());
2474 XSRETURN_IV(IsWin95());
2482 XSRETURN_IV(IsWinCE());
2492 if(SystemParametersInfoW(SPI_GETOEMINFO, sizeof(wbuf), wbuf, FALSE))
2493 WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buf, sizeof(buf), 0, 0);
2495 sprintf(buf, "SystemParametersInfo failed: %d", GetLastError());
2506 Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)");
2517 Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)");
2520 char szSourceFile[MAX_PATH+1];
2521 strcpy(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(0))));
2522 bResult = XCECopyFileA(szSourceFile, SvPV_nolen(ST(1)),
2539 unsigned int flags = MB_OK;
2541 txt = SvPV_nolen(ST(0));
2543 if (items < 1 || items > 2)
2544 Perl_croak(aTHX_ "usage: Win32::MessageBox($txt, [$flags])");
2547 flags = SvIV(ST(1));
2549 res = XCEMessageBoxA(NULL, txt, "Perl", flags);
2555 XS(w32_GetPowerStatus)
2559 SYSTEM_POWER_STATUS_EX sps;
2561 if(GetSystemPowerStatusEx(&sps, TRUE) == FALSE)
2566 mXPUSHi(sps.ACLineStatus);
2567 mXPUSHi(sps.BatteryFlag);
2568 mXPUSHi(sps.BatteryLifePercent);
2569 mXPUSHi(sps.BatteryLifeTime);
2570 mXPUSHi(sps.BatteryFullLifeTime);
2571 mXPUSHi(sps.BackupBatteryFlag);
2572 mXPUSHi(sps.BackupBatteryLifePercent);
2573 mXPUSHi(sps.BackupBatteryLifeTime);
2574 mXPUSHi(sps.BackupBatteryFullLifeTime);
2586 SHELLEXECUTEINFO si;
2588 wchar_t wfile[MAX_PATH];
2592 Perl_croak(aTHX_ "usage: Win32::ShellEx($file, $verb)");
2594 file = SvPV_nolen(ST(0));
2595 verb = SvPV_nolen(ST(1));
2597 memset(&si, 0, sizeof(si));
2598 si.cbSize = sizeof(si);
2599 si.fMask = SEE_MASK_FLAG_NO_UI;
2601 MultiByteToWideChar(CP_ACP, 0, verb, -1,
2602 wverb, sizeof(wverb)/2);
2603 si.lpVerb = (TCHAR *)wverb;
2605 MultiByteToWideChar(CP_ACP, 0, file, -1,
2606 wfile, sizeof(wfile)/2);
2607 si.lpFile = (TCHAR *)wfile;
2609 if(ShellExecuteEx(&si) == FALSE)
2618 Perl_init_os_extras(void)
2621 char *file = __FILE__;
2624 w32_perlshell_tokens = NULL;
2625 w32_perlshell_items = -1;
2626 w32_fdpid = newAV(); /* XX needs to be in Perl_win32_init()? */
2627 Newx(w32_children, 1, child_tab);
2628 w32_num_children = 0;
2630 newXS("Win32::GetCwd", w32_GetCwd, file);
2631 newXS("Win32::SetCwd", w32_SetCwd, file);
2632 newXS("Win32::GetTickCount", w32_GetTickCount, file);
2633 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
2635 newXS("Win32::ShellEx", w32_ShellEx, file);
2637 newXS("Win32::IsWinNT", w32_IsWinNT, file);
2638 newXS("Win32::IsWin95", w32_IsWin95, file);
2639 newXS("Win32::IsWinCE", w32_IsWinCE, file);
2640 newXS("Win32::CopyFile", w32_CopyFile, file);
2641 newXS("Win32::Sleep", w32_Sleep, file);
2642 newXS("Win32::MessageBox", w32_MessageBox, file);
2643 newXS("Win32::GetPowerStatus", w32_GetPowerStatus, file);
2644 newXS("Win32::GetOemInfo", w32_GetOemInfo, file);
2653 fgets(buf, sizeof(buf), stdin);
2657 Perl_win32_init(int *argcp, char ***argvp)
2662 if((p = xcegetenv("PERLDEBUG")) && (p[0] == 'y' || p[0] == 'Y'))
2670 Perl_win32_term(void)
2680 win32_get_child_IO(child_IO_table* ptbl)
2682 ptbl->childStdIn = GetStdHandle(STD_INPUT_HANDLE);
2683 ptbl->childStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2684 ptbl->childStdErr = GetStdHandle(STD_ERROR_HANDLE);
2687 win32_flock(int fd, int oper)
2690 Perl_croak(aTHX_ PL_no_func, "flock");
2695 win32_waitpid(int pid, int *status, int flags)
2698 Perl_croak(aTHX_ PL_no_func, "waitpid");
2703 win32_wait(int *status)
2706 Perl_croak(aTHX_ PL_no_func, "wait");
2711 wce_reopen_stdout(char *fname)
2713 if(xcefreopen(fname, "w", stdout) == NULL)
2724 printf("Hit RETURN");
2726 fgets(buf, sizeof(buf), stdin);
2730 /* //////////////////////////////////////////////////////////////////// */
2735 getcwd(char *buf, size_t size)
2737 return xcegetcwd(buf, size);
2748 win32_popenlist(const char *mode, IV narg, SV **args)
2751 Perl_croak(aTHX_ "List form of pipe open not implemented");
2756 * a popen() clone that respects PERL5SHELL
2758 * changed to return PerlIO* rather than FILE * by BKS, 11-11-2000
2762 win32_popen(const char *command, const char *mode)
2764 return _popen(command, mode);
2772 win32_pclose(PerlIO *pf)
2777 #ifdef HAVE_INTERP_INTERN
2781 win32_csighandler(int sig)
2784 dTHXa(PERL_GET_SIG_CONTEXT);
2785 Perl_warn(aTHX_ "Got signal %d",sig);
2791 Perl_sys_intern_init(pTHX)
2794 w32_perlshell_tokens = NULL;
2795 w32_perlshell_vec = (char**)NULL;
2796 w32_perlshell_items = 0;
2797 w32_fdpid = newAV();
2798 Newx(w32_children, 1, child_tab);
2799 w32_num_children = 0;
2800 # ifdef USE_ITHREADS
2802 Newx(w32_pseudo_children, 1, child_tab);
2803 w32_num_pseudo_children = 0;
2805 w32_init_socktype = 0;
2811 Perl_sys_intern_clear(pTHX)
2813 Safefree(w32_perlshell_tokens);
2814 Safefree(w32_perlshell_vec);
2815 /* NOTE: w32_fdpid is freed by sv_clean_all() */
2816 Safefree(w32_children);
2818 KillTimer(NULL,w32_timerid);
2821 # ifdef USE_ITHREADS
2822 Safefree(w32_pseudo_children);
2826 # ifdef USE_ITHREADS
2829 Perl_sys_intern_dup(pTHX_ struct interp_intern *src, struct interp_intern *dst)
2831 dst->perlshell_tokens = NULL;
2832 dst->perlshell_vec = (char**)NULL;
2833 dst->perlshell_items = 0;
2834 dst->fdpid = newAV();
2835 Newxz(dst->children, 1, child_tab);
2837 Newxz(dst->pseudo_children, 1, child_tab);
2838 dst->thr_intern.Winit_socktype = 0;
2840 dst->poll_count = 0;
2841 Copy(src->sigtable,dst->sigtable,SIG_SIZE,Sighandler_t);
2843 # endif /* USE_ITHREADS */
2844 #endif /* HAVE_INTERP_INTERN */
2846 // added to remove undefied symbol error in CodeWarrior compilation
2848 Perl_Ireentrant_buffer_ptr(aTHX)