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
25 #define PERL_NO_GET_CONTEXT
36 #include "celib_defs.h"
39 #include "cewin32_defs.h"
40 #include "cecrt_defs.h"
42 #define GetCurrentDirectoryW XCEGetCurrentDirectoryW
45 #include "stdio-palmsize.h"
50 #define EXECF_SPAWN_NOWAIT 3
52 #if defined(PERL_IMPLICIT_SYS)
54 # define do_spawn g_do_spawn
56 # define getlogin g_getlogin
59 static void get_shell(void);
60 static long tokenize(const char *str, char **dest, char ***destv);
61 static int do_spawn2(pTHX_ char *cmd, int exectype);
62 static BOOL has_shell_metachars(char *ptr);
63 static long filetime_to_clock(PFILETIME ft);
64 static BOOL filetime_from_time(PFILETIME ft, time_t t);
65 static char * get_emd_part(SV **leading, STRLEN *const len,
67 static void remove_dead_process(long deceased);
68 static long find_pid(pTHX_ int pid);
69 static char * qualified_path(const char *cmd);
70 static char * win32_get_xlib(const char *pl, const char *xlib,
71 const char *libname, STRLEN *const len);
74 static void remove_dead_pseudo_process(long child);
75 static long find_pseudo_pid(pTHX_ int pid);
78 int _fmode = O_TEXT; /* celib do not provide _fmode, so we define it here */
81 HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
82 char w32_module_name[MAX_PATH+1];
85 static DWORD w32_platform = (DWORD)-1;
90 return (win32_os_id() == VER_PLATFORM_WIN32_WINDOWS);
96 return (win32_os_id() == VER_PLATFORM_WIN32_NT);
102 return (win32_os_id() == VER_PLATFORM_WIN32_CE);
106 set_w32_module_name(void)
109 XCEGetModuleFileNameA((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
110 ? XCEGetModuleHandleA(NULL)
111 : w32_perldll_handle),
112 w32_module_name, sizeof(w32_module_name));
114 /* normalize to forward slashes */
115 ptr = w32_module_name;
123 /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
125 get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
127 /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
130 const char *subkey = "Software\\Perl";
134 retval = XCERegOpenKeyExA(hkey, subkey, 0, KEY_READ, &handle);
135 if (retval == ERROR_SUCCESS) {
137 retval = XCERegQueryValueExA(handle, valuename, 0, &type, NULL, &datalen);
138 if (retval == ERROR_SUCCESS && type == REG_SZ) {
141 *svp = sv_2mortal(newSVpvs(""));
142 SvGROW(*svp, datalen);
143 retval = XCERegQueryValueExA(handle, valuename, 0, NULL,
144 (PBYTE)SvPVX(*svp), &datalen);
145 if (retval == ERROR_SUCCESS) {
147 SvCUR_set(*svp,datalen-1);
155 /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
157 get_regstr(const char *valuename, SV **svp)
159 char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
161 str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
165 /* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
167 get_emd_part(SV **prev_pathp, STRLEN *const len, char *trailing_path, ...)
171 char mod_name[MAX_PATH+1];
175 int oldsize, newsize;
178 va_start(ap, trailing_path);
179 strip = va_arg(ap, char *);
181 sprintf(base, "%d.%d", (int)PERL_REVISION, (int)PERL_VERSION);
182 baselen = strlen(base);
184 if (!*w32_module_name) {
185 set_w32_module_name();
187 strcpy(mod_name, w32_module_name);
188 ptr = strrchr(mod_name, '/');
189 while (ptr && strip) {
190 /* look for directories to skip back */
193 ptr = strrchr(mod_name, '/');
194 /* avoid stripping component if there is no slash,
195 * or it doesn't match ... */
196 if (!ptr || stricmp(ptr+1, strip) != 0) {
197 /* ... but not if component matches m|5\.$patchlevel.*| */
198 if (!ptr || !(*strip == '5' && *(ptr+1) == '5'
199 && strnEQ(strip, base, baselen)
200 && strnEQ(ptr+1, base, baselen)))
206 strip = va_arg(ap, char *);
214 strcpy(++ptr, trailing_path);
216 /* only add directory if it exists */
217 if (XCEGetFileAttributesA(mod_name) != (DWORD) -1) {
218 /* directory exists */
221 *prev_pathp = sv_2mortal(newSVpvs(""));
222 sv_catpvs(*prev_pathp, ";");
223 sv_catpv(*prev_pathp, mod_name);
225 *len = SvCUR(*prev_pathp);
226 return SvPVX(*prev_pathp);
233 win32_get_privlib(WIN32_NO_REGISTRY_M_(const char *pl) STRLEN *const len)
236 char *stdlib = "lib";
237 char buffer[MAX_PATH+1];
240 /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
241 sprintf(buffer, "%s-%s", stdlib, pl);
242 if (!get_regstr(buffer, &sv))
243 (void)get_regstr(stdlib, &sv);
245 /* $stdlib .= ";$EMD/../../lib" */
246 return get_emd_part(&sv, len, stdlib, ARCHNAME, "bin", NULL);
250 win32_get_xlib(const char *pl, const char *xlib, const char *libname,
255 char pathstr[MAX_PATH+1];
259 /* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */
260 sprintf(regstr, "%s-%s", xlib, pl);
261 (void)get_regstr(regstr, &sv1);
264 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */
265 sprintf(pathstr, "%s/%s/lib", libname, pl);
266 (void)get_emd_part(&sv1, NULL, pathstr, ARCHNAME, "bin", pl, NULL);
268 /* $HKCU{$xlib} || $HKLM{$xlib} . ---; */
269 (void)get_regstr(xlib, &sv2);
272 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */
273 sprintf(pathstr, "%s/lib", libname);
274 (void)get_emd_part(&sv2, NULL, pathstr, ARCHNAME, "bin", pl, NULL);
291 win32_get_sitelib(const char *pl, STRLEN *const len)
293 return win32_get_xlib(pl, "sitelib", "site", len);
296 #ifndef PERL_VENDORLIB_NAME
297 # define PERL_VENDORLIB_NAME "vendor"
301 win32_get_vendorlib(const char *pl, STRLEN *const len)
303 return win32_get_xlib(pl, "vendorlib", PERL_VENDORLIB_NAME, len);
307 has_shell_metachars(char *ptr)
313 * Scan string looking for redirection (< or >) or pipe
314 * characters (|) that are not in a quoted string.
315 * Shell variable interpolation (%VAR%) can also happen inside strings.
347 #if !defined(PERL_IMPLICIT_SYS)
348 /* since the current process environment is being updated in util.c
349 * the library functions will get the correct environment
352 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
354 printf("popen(%s)\n", cmd);
356 Perl_croak(aTHX_ PL_no_func, "popen");
361 Perl_my_pclose(pTHX_ PerlIO *fp)
363 Perl_croak(aTHX_ PL_no_func, "pclose");
368 DllExport unsigned long
371 static OSVERSIONINFOA osver;
373 if (osver.dwPlatformId != w32_platform) {
374 memset(&osver, 0, sizeof(OSVERSIONINFOA));
375 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
376 XCEGetVersionExA(&osver);
377 w32_platform = osver.dwPlatformId;
379 return (unsigned long)w32_platform;
389 return -((int)w32_pseudo_id);
395 /* Tokenize a string. Words are null-separated, and the list
396 * ends with a doubled null. Any character (except null and
397 * including backslash) may be escaped by preceding it with a
398 * backslash (the backslash will be stripped).
399 * Returns number of words in result buffer.
402 tokenize(const char *str, char **dest, char ***destv)
404 char *retstart = NULL;
405 char **retvstart = 0;
409 int slen = strlen(str);
412 Newx(ret, slen+2, char);
413 Newx(retv, (slen+3)/2, char*);
421 if (*ret == '\\' && *str)
423 else if (*ret == ' ') {
439 retvstart[items] = NULL;
449 win32_pipe(int *pfd, unsigned int size, int mode)
452 Perl_croak(aTHX_ PL_no_func, "pipe");
457 win32_times(struct tms *timebuf)
460 Perl_croak(aTHX_ PL_no_func, "times");
465 win32_signal(int sig, Sighandler_t subcode)
467 return xcesignal(sig, subcode);
474 if (!w32_perlshell_tokens) {
475 /* we don't use COMSPEC here for two reasons:
476 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
477 * uncontrolled unportability of the ensuing scripts.
478 * 2. PERL5SHELL could be set to a shell that may not be fit for
479 * interactive use (which is what most programs look in COMSPEC
482 const char* defaultshell = (IsWinNT()
483 ? "cmd.exe /x/d/c" : "command.com /c");
484 const char *usershell = PerlEnv_getenv("PERL5SHELL");
485 w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
486 &w32_perlshell_tokens,
492 Perl_do_aspawn(pTHX_ SV *really, SV **mark, SV **sp)
494 PERL_ARGS_ASSERT_DO_ASPAWN;
496 Perl_croak(aTHX_ PL_no_func, "aspawn");
500 /* returns pointer to the next unquoted space or the end of the string */
502 find_next_space(const char *s)
504 bool in_quotes = FALSE;
506 /* ignore doubled backslashes, or backslash+quote */
507 if (*s == '\\' && (s[1] == '\\' || s[1] == '"')) {
510 /* keep track of when we're within quotes */
511 else if (*s == '"') {
513 in_quotes = !in_quotes;
515 /* break it up only at spaces that aren't in quotes */
516 else if (!in_quotes && isSPACE(*s))
526 do_spawn2(pTHX_ char *cmd, int exectype)
532 BOOL needToTry = TRUE;
535 /* Save an extra exec if possible. See if there are shell
536 * metacharacters in it */
537 if (!has_shell_metachars(cmd)) {
538 Newx(argv, strlen(cmd) / 2 + 2, char*);
539 Newx(cmd2, strlen(cmd) + 1, char);
542 for (s = cmd2; *s;) {
543 while (*s && isSPACE(*s))
547 s = find_next_space(s);
555 status = win32_spawnvp(P_WAIT, argv[0],
556 (const char* const*)argv);
558 case EXECF_SPAWN_NOWAIT:
559 status = win32_spawnvp(P_NOWAIT, argv[0],
560 (const char* const*)argv);
563 status = win32_execvp(argv[0], (const char* const*)argv);
566 if (status != -1 || errno == 0)
576 Newx(argv, w32_perlshell_items + 2, char*);
577 while (++i < w32_perlshell_items)
578 argv[i] = w32_perlshell_vec[i];
583 status = win32_spawnvp(P_WAIT, argv[0],
584 (const char* const*)argv);
586 case EXECF_SPAWN_NOWAIT:
587 status = win32_spawnvp(P_NOWAIT, argv[0],
588 (const char* const*)argv);
591 status = win32_execvp(argv[0], (const char* const*)argv);
597 if (exectype == EXECF_SPAWN_NOWAIT) {
599 PL_statusvalue = -1; /* >16bits hint for pp_system() */
603 if (ckWARN(WARN_EXEC))
604 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't %s \"%s\": %s",
605 (exectype == EXECF_EXEC ? "exec" : "spawn"),
606 cmd, strerror(errno));
611 PL_statusvalue = status;
617 Perl_do_spawn(pTHX_ char *cmd)
619 PERL_ARGS_ASSERT_DO_SPAWN;
621 return do_spawn2(aTHX_ cmd, EXECF_SPAWN);
625 Perl_do_spawn_nowait(pTHX_ char *cmd)
627 PERL_ARGS_ASSERT_DO_SPAWN_NOWAIT;
629 return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT);
633 Perl_do_exec(pTHX_ const char *cmd)
635 PERL_ARGS_ASSERT_DO_EXEC;
637 do_spawn2(aTHX_ cmd, EXECF_EXEC);
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.
646 win32_opendir(const char *filename)
652 char scanname[MAX_PATH+3];
654 WIN32_FIND_DATAA aFindData;
655 WIN32_FIND_DATAW wFindData;
657 char buffer[MAX_PATH*2];
658 WCHAR wbuffer[MAX_PATH+1];
661 len = strlen(filename);
665 /* check to see if filename is a directory */
666 if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
669 /* Get us a DIR structure */
672 /* Create the search pattern */
673 strcpy(scanname, filename);
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++] = '/';
680 else if (scanname[len-1] != '/' && scanname[len-1] != '\\') {
681 scanname[len++] = '/';
683 scanname[len++] = '*';
684 scanname[len] = '\0';
686 /* do the FindFirstFile call */
687 fh = FindFirstFile(PerlDir_mapA(scanname), &aFindData);
689 if (fh == INVALID_HANDLE_VALUE) {
690 DWORD err = GetLastError();
691 /* FindFirstFile() fails on empty drives! */
693 case ERROR_FILE_NOT_FOUND:
695 case ERROR_NO_MORE_FILES:
696 case ERROR_PATH_NOT_FOUND:
699 case ERROR_NOT_ENOUGH_MEMORY:
710 /* now allocate the first part of the string table for
711 * the filenames that we find.
713 ptr = aFindData.cFileName;
719 Newx(dirp->start, dirp->size, char);
720 strcpy(dirp->start, ptr);
722 dirp->end = dirp->curr = dirp->start;
728 /* Readdir just returns the current string pointer and bumps the
729 * string pointer to the nDllExport entry.
731 DllExport struct direct *
732 win32_readdir(DIR *dirp)
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;
743 dirp->dirstr.d_ino = dirp->curr - dirp->start;
745 /* Now set up for the next call to readdir */
746 dirp->curr += len + 1;
747 if (dirp->curr >= dirp->end) {
751 WIN32_FIND_DATAW wFindData;
752 WIN32_FIND_DATAA aFindData;
753 char buffer[MAX_PATH*2];
755 /* finding the next file that matches the wildcard
756 * (which should be all of them in this directory!).
758 res = FindNextFile(dirp->handle, &aFindData);
760 ptr = aFindData.cFileName;
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 Renew(dirp->start, dirp->size * 2, char);
770 dirp->curr = dirp->start + curpos;
772 strcpy(dirp->start + endpos, ptr);
773 dirp->end = dirp->start + newsize;
779 return &(dirp->dirstr);
785 /* Telldir returns the current string pointer position */
787 win32_telldir(DIR *dirp)
789 return (dirp->curr - dirp->start);
793 /* Seekdir moves the string pointer to a previously saved position
794 * (returned by telldir).
797 win32_seekdir(DIR *dirp, long loc)
799 dirp->curr = dirp->start + loc;
802 /* Rewinddir resets the string pointer to the start */
804 win32_rewinddir(DIR *dirp)
806 dirp->curr = dirp->start;
809 /* free the memory allocated by opendir */
811 win32_closedir(DIR *dirp)
814 if (dirp->handle != INVALID_HANDLE_VALUE)
815 FindClose(dirp->handle);
816 Safefree(dirp->start);
822 /////!!!!!!!!!!! return here and do right stuff!!!!
825 win32_opendir(const char *filename)
827 return opendir(filename);
830 DllExport struct direct *
831 win32_readdir(DIR *dirp)
833 return readdir(dirp);
837 win32_telldir(DIR *dirp)
840 Perl_croak(aTHX_ PL_no_func, "telldir");
845 win32_seekdir(DIR *dirp, long loc)
848 Perl_croak(aTHX_ PL_no_func, "seekdir");
852 win32_rewinddir(DIR *dirp)
855 Perl_croak(aTHX_ PL_no_func, "rewinddir");
859 win32_closedir(DIR *dirp)
867 win32_kill(int pid, int sig)
870 Perl_croak(aTHX_ PL_no_func, "kill");
875 win32_stat(const char *path, struct stat *sbuf)
877 return xcestat(path, sbuf);
881 win32_longpath(char *path)
891 /* Can't use PerlIO to write as it allocates memory */
892 PerlLIO_write(PerlIO_fileno(Perl_error_log),
893 PL_no_mem, strlen(PL_no_mem));
899 /* The win32_ansipath() function takes a Unicode filename and converts it
900 * into the current Windows codepage. If some characters cannot be mapped,
901 * then it will convert the short name instead.
903 * The buffer to the ansi pathname must be freed with win32_free() when it
904 * it no longer needed.
906 * The argument to win32_ansipath() must exist before this function is
907 * called; otherwise there is no way to determine the short path name.
909 * Ideas for future refinement:
910 * - Only convert those segments of the path that are not in the current
911 * codepage, but leave the other segments in their long form.
912 * - If the resulting name is longer than MAX_PATH, start converting
913 * additional path segments into short names until the full name
914 * is shorter than MAX_PATH. Shorten the filename part last!
917 win32_ansipath(const WCHAR *widename)
920 size_t widelen = wcslen(widename)+1;
921 int len = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, widename, widelen,
922 NULL, 0, NULL, NULL);
923 name = win32_malloc(len);
927 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, widename, widelen,
928 name, len, NULL, NULL);
933 win32_getenv(const char *name)
935 return xcegetenv(name);
939 win32_putenv(const char *name)
941 return xceputenv(name);
945 filetime_to_clock(PFILETIME ft)
947 __int64 qw = ft->dwHighDateTime;
949 qw |= ft->dwLowDateTime;
950 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
954 /* fix utime() so it works on directories in NT */
956 filetime_from_time(PFILETIME pFileTime, time_t Time)
958 struct tm *pTM = localtime(&Time);
959 SYSTEMTIME SystemTime;
965 SystemTime.wYear = pTM->tm_year + 1900;
966 SystemTime.wMonth = pTM->tm_mon + 1;
967 SystemTime.wDay = pTM->tm_mday;
968 SystemTime.wHour = pTM->tm_hour;
969 SystemTime.wMinute = pTM->tm_min;
970 SystemTime.wSecond = pTM->tm_sec;
971 SystemTime.wMilliseconds = 0;
973 return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
974 LocalFileTimeToFileTime(&LocalTime, pFileTime);
978 win32_unlink(const char *filename)
980 return xceunlink(filename);
984 win32_utime(const char *filename, struct utimbuf *times)
986 return xceutime(filename, (struct _utimbuf *) times);
990 win32_gettimeofday(struct timeval *tp, void *not_used)
992 return xcegettimeofday(tp,not_used);
996 win32_uname(struct utsname *name)
999 STRLEN nodemax = sizeof(name->nodename)-1;
1000 OSVERSIONINFOA osver;
1002 memset(&osver, 0, sizeof(OSVERSIONINFOA));
1003 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
1004 if (XCEGetVersionExA(&osver)) {
1006 switch (osver.dwPlatformId) {
1007 case VER_PLATFORM_WIN32_CE:
1008 strcpy(name->sysname, "Windows CE");
1010 case VER_PLATFORM_WIN32_WINDOWS:
1011 strcpy(name->sysname, "Windows");
1013 case VER_PLATFORM_WIN32_NT:
1014 strcpy(name->sysname, "Windows NT");
1016 case VER_PLATFORM_WIN32s:
1017 strcpy(name->sysname, "Win32s");
1020 strcpy(name->sysname, "Win32 Unknown");
1025 sprintf(name->release, "%d.%d",
1026 osver.dwMajorVersion, osver.dwMinorVersion);
1029 sprintf(name->version, "Build %d",
1030 osver.dwPlatformId == VER_PLATFORM_WIN32_NT
1031 ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff));
1032 if (osver.szCSDVersion[0]) {
1033 char *buf = name->version + strlen(name->version);
1034 sprintf(buf, " (%s)", osver.szCSDVersion);
1038 *name->sysname = '\0';
1039 *name->version = '\0';
1040 *name->release = '\0';
1044 hep = win32_gethostbyname("localhost");
1046 STRLEN len = strlen(hep->h_name);
1047 if (len <= nodemax) {
1048 strcpy(name->nodename, hep->h_name);
1051 strncpy(name->nodename, hep->h_name, nodemax);
1052 name->nodename[nodemax] = '\0';
1057 if (!XCEGetComputerNameA(name->nodename, &sz))
1058 *name->nodename = '\0';
1061 /* machine (architecture) */
1065 GetSystemInfo(&info);
1067 switch (info.wProcessorArchitecture) {
1068 case PROCESSOR_ARCHITECTURE_INTEL:
1069 arch = "x86"; break;
1070 case PROCESSOR_ARCHITECTURE_MIPS:
1071 arch = "mips"; break;
1072 case PROCESSOR_ARCHITECTURE_ALPHA:
1073 arch = "alpha"; break;
1074 case PROCESSOR_ARCHITECTURE_PPC:
1075 arch = "ppc"; break;
1076 case PROCESSOR_ARCHITECTURE_ARM:
1077 arch = "arm"; break;
1078 case PROCESSOR_HITACHI_SH3:
1079 arch = "sh3"; break;
1080 case PROCESSOR_SHx_SH3:
1081 arch = "sh3"; break;
1084 arch = "unknown"; break;
1086 strcpy(name->machine, arch);
1091 /* Timing related stuff */
1094 do_raise(pTHX_ int sig)
1096 if (sig < SIG_SIZE) {
1097 Sighandler_t handler = w32_sighandler[sig];
1098 if (handler == SIG_IGN) {
1101 else if (handler != SIG_DFL) {
1106 /* Choose correct default behaviour */
1122 /* Tell caller to exit thread/process as appropriate */
1127 sig_terminate(pTHX_ int sig)
1129 Perl_warn(aTHX_ "Terminating on signal SIG%s(%d)\n",PL_sig_name[sig], sig);
1130 /* exit() seems to be safe, my_exit() or die() is a problem in ^C
1137 win32_async_check(pTHX)
1141 /* Passing PeekMessage -1 as HWND (2nd arg) only get PostThreadMessage() messages
1142 * and ignores window messages - should co-exist better with windows apps e.g. Tk
1144 while (PeekMessage(&msg, (HWND)-1, 0, 0, PM_REMOVE|PM_NOYIELD)) {
1146 switch(msg.message) {
1149 /* Perhaps some other messages could map to signals ? ... */
1152 /* Treat WM_QUIT like SIGHUP? */
1158 /* We use WM_USER to fake kill() with other signals */
1162 if (do_raise(aTHX_ sig)) {
1163 sig_terminate(aTHX_ sig);
1169 /* alarm() is a one-shot but SetTimer() repeats so kill it */
1171 KillTimer(NULL,w32_timerid);
1174 /* Now fake a call to signal handler */
1175 if (do_raise(aTHX_ 14)) {
1176 sig_terminate(aTHX_ 14);
1181 /* Otherwise do normal Win32 thing - in case it is useful */
1183 TranslateMessage(&msg);
1184 DispatchMessage(&msg);
1191 /* Above or other stuff may have set a signal flag */
1192 if (PL_sig_pending) {
1198 /* This function will not return until the timeout has elapsed, or until
1199 * one of the handles is ready. */
1201 win32_msgwait(pTHX_ DWORD count, LPHANDLE handles, DWORD timeout, LPDWORD resultp)
1203 /* We may need several goes at this - so compute when we stop */
1205 if (timeout != INFINITE) {
1206 ticks = GetTickCount();
1210 DWORD result = MsgWaitForMultipleObjects(count,handles,FALSE,timeout-ticks, QS_ALLEVENTS);
1213 if (result == WAIT_TIMEOUT) {
1214 /* Ran out of time - explicit return of zero to avoid -ve if we
1215 have scheduling issues
1219 if (timeout != INFINITE) {
1220 ticks = GetTickCount();
1222 if (result == WAIT_OBJECT_0 + count) {
1223 /* Message has arrived - check it */
1224 (void)win32_async_check(aTHX);
1227 /* Not timeout or message - one of handles is ready */
1231 /* compute time left to wait */
1232 ticks = timeout - ticks;
1233 /* If we are past the end say zero */
1234 return (ticks > 0) ? ticks : 0;
1237 static UINT timerid = 0;
1239 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
1242 KillTimer(NULL,timerid);
1247 DllExport unsigned int
1248 win32_sleep(unsigned int t)
1253 DllExport unsigned int
1254 win32_alarm(unsigned int sec)
1257 * the 'obvious' implentation is SetTimer() with a callback
1258 * which does whatever receiving SIGALRM would do
1259 * we cannot use SIGALRM even via raise() as it is not
1260 * one of the supported codes in <signal.h>
1262 * Snag is unless something is looking at the message queue
1263 * nothing happens :-(
1268 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
1270 Perl_croak_nocontext("Cannot set timer");
1276 KillTimer(NULL,timerid);
1283 #ifdef HAVE_DES_FCRYPT
1284 extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf);
1288 win32_crypt(const char *txt, const char *salt)
1291 #ifdef HAVE_DES_FCRYPT
1293 return des_fcrypt(txt, salt, w32_crypt_buffer);
1295 Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia.");
1302 * redirected io subsystem for all XS modules
1315 return (&(environ));
1318 /* the rest are the remapped stdio routines */
1325 char *g_getlogin() {
1326 return "no-getlogin";
1342 win32_ferror(FILE *fp)
1344 return (ferror(fp));
1349 win32_feof(FILE *fp)
1355 * Since the errors returned by the socket error function
1356 * WSAGetLastError() are not known by the library routine strerror
1357 * we have to roll our own.
1361 win32_strerror(int e)
1363 return xcestrerror(e);
1367 win32_str_os_error(void *sv, DWORD dwErr)
1371 sv_setpvs((SV*)sv, "Error");
1376 win32_fprintf(FILE *fp, const char *format, ...)
1379 va_start(marker, format); /* Initialize variable arguments. */
1381 return (vfprintf(fp, format, marker));
1385 win32_printf(const char *format, ...)
1388 va_start(marker, format); /* Initialize variable arguments. */
1390 return (vprintf(format, marker));
1394 win32_vfprintf(FILE *fp, const char *format, va_list args)
1396 return (vfprintf(fp, format, args));
1400 win32_vprintf(const char *format, va_list args)
1402 return (vprintf(format, args));
1406 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1408 return fread(buf, size, count, fp);
1412 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1414 return fwrite(buf, size, count, fp);
1418 win32_fopen(const char *filename, const char *mode)
1420 return xcefopen(filename, mode);
1424 win32_fdopen(int handle, const char *mode)
1426 return palm_fdopen(handle, mode);
1430 win32_freopen(const char *path, const char *mode, FILE *stream)
1432 return xcefreopen(path, mode, stream);
1436 win32_fclose(FILE *pf)
1438 return xcefclose(pf);
1442 win32_fputs(const char *s,FILE *pf)
1444 return fputs(s, pf);
1448 win32_fputc(int c,FILE *pf)
1454 win32_ungetc(int c,FILE *pf)
1456 return ungetc(c,pf);
1460 win32_getc(FILE *pf)
1466 win32_fileno(FILE *pf)
1468 return palm_fileno(pf);
1472 win32_clearerr(FILE *pf)
1479 win32_fflush(FILE *pf)
1485 win32_ftell(FILE *pf)
1491 win32_fseek(FILE *pf, Off_t offset,int origin)
1493 return fseek(pf, offset, origin);
1496 /* fpos_t seems to be int64 on hpc pro! Really stupid. */
1497 /* But maybe someday there will be such large disks in a hpc... */
1499 win32_fgetpos(FILE *pf, fpos_t *p)
1501 return fgetpos(pf, p);
1505 win32_fsetpos(FILE *pf, const fpos_t *p)
1507 return fsetpos(pf, p);
1511 win32_rewind(FILE *pf)
1513 fseek(pf, 0, SEEK_SET);
1521 char prefix[MAX_PATH+1];
1522 char filename[MAX_PATH+1];
1523 DWORD len = GetTempPath(MAX_PATH, prefix);
1524 if (len && len < MAX_PATH) {
1525 if (GetTempFileName(prefix, "plx", 0, filename)) {
1526 HANDLE fh = CreateFile(filename,
1527 DELETE | GENERIC_READ | GENERIC_WRITE,
1531 FILE_ATTRIBUTE_NORMAL
1532 | FILE_FLAG_DELETE_ON_CLOSE,
1534 if (fh != INVALID_HANDLE_VALUE) {
1535 int fd = win32_open_osfhandle((intptr_t)fh, 0);
1537 DEBUG_p(PerlIO_printf(Perl_debug_log,
1538 "Created tmpfile=%s\n",filename));
1550 int fd = win32_tmpfd();
1552 return win32_fdopen(fd, "w+b");
1565 win32_fstat(int fd, struct stat *sbufptr)
1567 return xcefstat(fd, sbufptr);
1571 win32_link(const char *oldname, const char *newname)
1574 Perl_croak(aTHX_ PL_no_func, "link");
1580 win32_rename(const char *oname, const char *newname)
1582 return xcerename(oname, newname);
1586 win32_setmode(int fd, int mode)
1588 /* currently 'celib' seem to have this function in src, but not
1589 * exported. When it will be, we'll uncomment following line.
1591 /* return xcesetmode(fd, mode); */
1596 win32_chsize(int fd, Off_t size)
1598 return chsize(fd, size);
1602 win32_lseek(int fd, Off_t offset, int origin)
1604 return xcelseek(fd, offset, origin);
1610 return xcelseek(fd, 0, SEEK_CUR);
1614 win32_open(const char *path, int flag, ...)
1620 pmode = va_arg(ap, int);
1623 return xceopen(path, flag, pmode);
1629 return xceclose(fd);
1636 Perl_croak(aTHX_ PL_no_func, "eof");
1643 return xcedup(fd); /* from celib/ceio.c; requires some more work on it */
1647 win32_dup2(int fd1,int fd2)
1649 return xcedup2(fd1,fd2);
1653 win32_read(int fd, void *buf, unsigned int cnt)
1655 return xceread(fd, buf, cnt);
1659 win32_write(int fd, const void *buf, unsigned int cnt)
1661 return xcewrite(fd, (void *) buf, cnt);
1665 win32_mkdir(const char *dir, int mode)
1667 return xcemkdir(dir);
1671 win32_rmdir(const char *dir)
1673 return xcermdir(dir);
1677 win32_chdir(const char *dir)
1679 return xcechdir(dir);
1683 win32_access(const char *path, int mode)
1685 return xceaccess(path, mode);
1689 win32_chmod(const char *path, int mode)
1691 return xcechmod(path, mode);
1695 create_command_line(char *cname, STRLEN clen, const char * const *args)
1702 bool bat_file = FALSE;
1703 bool cmd_shell = FALSE;
1704 bool dumb_shell = FALSE;
1705 bool extra_quotes = FALSE;
1706 bool quote_next = FALSE;
1709 cname = (char*)args[0];
1711 /* The NT cmd.exe shell has the following peculiarity that needs to be
1712 * worked around. It strips a leading and trailing dquote when any
1713 * of the following is true:
1714 * 1. the /S switch was used
1715 * 2. there are more than two dquotes
1716 * 3. there is a special character from this set: &<>()@^|
1717 * 4. no whitespace characters within the two dquotes
1718 * 5. string between two dquotes isn't an executable file
1719 * To work around this, we always add a leading and trailing dquote
1720 * to the string, if the first argument is either "cmd.exe" or "cmd",
1721 * and there were at least two or more arguments passed to cmd.exe
1722 * (not including switches).
1723 * XXX the above rules (from "cmd /?") don't seem to be applied
1724 * always, making for the convolutions below :-(
1728 clen = strlen(cname);
1731 && (stricmp(&cname[clen-4], ".bat") == 0
1732 || (IsWinNT() && stricmp(&cname[clen-4], ".cmd") == 0)))
1738 char *exe = strrchr(cname, '/');
1739 char *exe2 = strrchr(cname, '\\');
1746 if (stricmp(exe, "cmd.exe") == 0 || stricmp(exe, "cmd") == 0) {
1750 else if (stricmp(exe, "command.com") == 0
1751 || stricmp(exe, "command") == 0)
1758 DEBUG_p(PerlIO_printf(Perl_debug_log, "Args "));
1759 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1760 STRLEN curlen = strlen(arg);
1761 if (!(arg[0] == '"' && arg[curlen-1] == '"'))
1762 len += 2; /* assume quoting needed (worst case) */
1764 DEBUG_p(PerlIO_printf(Perl_debug_log, "[%s]",arg));
1766 DEBUG_p(PerlIO_printf(Perl_debug_log, "\n"));
1769 Newx(cmd, len, char);
1774 extra_quotes = TRUE;
1777 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1779 STRLEN curlen = strlen(arg);
1781 /* we want to protect empty arguments and ones with spaces with
1782 * dquotes, but only if they aren't already there */
1787 else if (quote_next) {
1788 /* see if it really is multiple arguments pretending to
1789 * be one and force a set of quotes around it */
1790 if (*find_next_space(arg))
1793 else if (!(arg[0] == '"' && curlen > 1 && arg[curlen-1] == '"')) {
1795 while (i < curlen) {
1796 if (isSPACE(arg[i])) {
1799 else if (arg[i] == '"') {
1823 && *arg == '/' /* see if arg is "/c", "/x/c", "/x/d/c" etc. */
1824 && stricmp(arg+curlen-2, "/c") == 0)
1826 /* is there a next argument? */
1827 if (args[index+1]) {
1828 /* are there two or more next arguments? */
1829 if (args[index+2]) {
1831 extra_quotes = TRUE;
1834 /* single argument, force quoting if it has spaces */
1850 qualified_path(const char *cmd)
1854 char *fullcmd, *curfullcmd;
1860 fullcmd = (char*)cmd;
1862 if (*fullcmd == '/' || *fullcmd == '\\')
1869 pathstr = PerlEnv_getenv("PATH");
1870 Newx(fullcmd, MAX_PATH+1, char);
1871 curfullcmd = fullcmd;
1876 /* start by appending the name to the current prefix */
1877 strcpy(curfullcmd, cmd);
1878 curfullcmd += cmdlen;
1880 /* if it doesn't end with '.', or has no extension, try adding
1881 * a trailing .exe first */
1882 if (cmd[cmdlen-1] != '.'
1883 && (cmdlen < 4 || cmd[cmdlen-4] != '.'))
1885 strcpy(curfullcmd, ".exe");
1886 res = GetFileAttributes(fullcmd);
1887 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1892 /* that failed, try the bare name */
1893 res = GetFileAttributes(fullcmd);
1894 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1897 /* quit if no other path exists, or if cmd already has path */
1898 if (!pathstr || !*pathstr || has_slash)
1901 /* skip leading semis */
1902 while (*pathstr == ';')
1905 /* build a new prefix from scratch */
1906 curfullcmd = fullcmd;
1907 while (*pathstr && *pathstr != ';') {
1908 if (*pathstr == '"') { /* foo;"baz;etc";bar */
1909 pathstr++; /* skip initial '"' */
1910 while (*pathstr && *pathstr != '"') {
1911 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1912 *curfullcmd++ = *pathstr;
1916 pathstr++; /* skip trailing '"' */
1919 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1920 *curfullcmd++ = *pathstr;
1925 pathstr++; /* skip trailing semi */
1926 if (curfullcmd > fullcmd /* append a dir separator */
1927 && curfullcmd[-1] != '/' && curfullcmd[-1] != '\\')
1929 *curfullcmd++ = '\\';
1937 /* The following are just place holders.
1938 * Some hosts may provide and environment that the OS is
1939 * not tracking, therefore, these host must provide that
1940 * environment and the current directory to CreateProcess
1944 win32_get_childenv(void)
1950 win32_free_childenv(void* d)
1955 win32_clearenv(void)
1957 char *envv = GetEnvironmentStrings();
1961 char *end = strchr(cur,'=');
1962 if (end && end != cur) {
1964 xcesetenv(cur, "", 0);
1966 cur = end + strlen(end+1)+2;
1968 else if ((len = strlen(cur)))
1971 FreeEnvironmentStrings(envv);
1975 win32_get_childdir(void)
1979 char szfilename[MAX_PATH+1];
1980 GetCurrentDirectoryA(MAX_PATH+1, szfilename);
1982 Newx(ptr, strlen(szfilename)+1, char);
1983 strcpy(ptr, szfilename);
1988 win32_free_childdir(char* d)
1994 /* XXX this needs to be made more compatible with the spawnvp()
1995 * provided by the various RTLs. In particular, searching for
1996 * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented.
1997 * This doesn't significantly affect perl itself, because we
1998 * always invoke things using PERL5SHELL if a direct attempt to
1999 * spawn the executable fails.
2001 * XXX splitting and rejoining the commandline between do_aspawn()
2002 * and win32_spawnvp() could also be avoided.
2006 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
2008 #ifdef USE_RTL_SPAWNVP
2009 return spawnvp(mode, cmdname, (char * const *)argv);
2016 STARTUPINFO StartupInfo;
2017 PROCESS_INFORMATION ProcessInformation;
2020 char *fullcmd = NULL;
2021 char *cname = (char *)cmdname;
2025 clen = strlen(cname);
2026 /* if command name contains dquotes, must remove them */
2027 if (strchr(cname, '"')) {
2029 Newx(cname,clen+1,char);
2042 cmd = create_command_line(cname, clen, argv);
2044 env = PerlEnv_get_childenv();
2045 dir = PerlEnv_get_childdir();
2048 case P_NOWAIT: /* asynch + remember result */
2049 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
2054 /* Create a new process group so we can use GenerateConsoleCtrlEvent()
2057 /* not supported on CE create |= CREATE_NEW_PROCESS_GROUP; */
2060 case P_WAIT: /* synchronous execution */
2062 default: /* invalid mode */
2067 memset(&StartupInfo,0,sizeof(StartupInfo));
2068 StartupInfo.cb = sizeof(StartupInfo);
2069 memset(&tbl,0,sizeof(tbl));
2070 PerlEnv_get_child_IO(&tbl);
2071 StartupInfo.dwFlags = tbl.dwFlags;
2072 StartupInfo.dwX = tbl.dwX;
2073 StartupInfo.dwY = tbl.dwY;
2074 StartupInfo.dwXSize = tbl.dwXSize;
2075 StartupInfo.dwYSize = tbl.dwYSize;
2076 StartupInfo.dwXCountChars = tbl.dwXCountChars;
2077 StartupInfo.dwYCountChars = tbl.dwYCountChars;
2078 StartupInfo.dwFillAttribute = tbl.dwFillAttribute;
2079 StartupInfo.wShowWindow = tbl.wShowWindow;
2080 StartupInfo.hStdInput = tbl.childStdIn;
2081 StartupInfo.hStdOutput = tbl.childStdOut;
2082 StartupInfo.hStdError = tbl.childStdErr;
2083 if (StartupInfo.hStdInput == INVALID_HANDLE_VALUE &&
2084 StartupInfo.hStdOutput == INVALID_HANDLE_VALUE &&
2085 StartupInfo.hStdError == INVALID_HANDLE_VALUE)
2087 create |= CREATE_NEW_CONSOLE;
2090 StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
2092 if (w32_use_showwindow) {
2093 StartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
2094 StartupInfo.wShowWindow = w32_showwindow;
2097 DEBUG_p(PerlIO_printf(Perl_debug_log, "Spawning [%s] with [%s]\n",
2100 if (!CreateProcess(cname, /* search PATH to find executable */
2101 cmd, /* executable, and its arguments */
2102 NULL, /* process attributes */
2103 NULL, /* thread attributes */
2104 TRUE, /* inherit handles */
2105 create, /* creation flags */
2106 (LPVOID)env, /* inherit environment */
2107 dir, /* inherit cwd */
2109 &ProcessInformation))
2111 /* initial NULL argument to CreateProcess() does a PATH
2112 * search, but it always first looks in the directory
2113 * where the current process was started, which behavior
2114 * is undesirable for backward compatibility. So we
2115 * jump through our own hoops by picking out the path
2116 * we really want it to use. */
2118 fullcmd = qualified_path(cname);
2120 if (cname != cmdname)
2123 DEBUG_p(PerlIO_printf(Perl_debug_log,
2124 "Retrying [%s] with same args\n",
2134 if (mode == P_NOWAIT) {
2135 /* asynchronous spawn -- store handle, return PID */
2136 ret = (int)ProcessInformation.dwProcessId;
2137 if (IsWin95() && ret < 0)
2140 w32_child_handles[w32_num_children] = ProcessInformation.hProcess;
2141 w32_child_pids[w32_num_children] = (DWORD)ret;
2146 win32_msgwait(aTHX_ 1, &ProcessInformation.hProcess, INFINITE, NULL);
2147 /* FIXME: if msgwait returned due to message perhaps forward the
2148 "signal" to the process
2150 GetExitCodeProcess(ProcessInformation.hProcess, &status);
2152 CloseHandle(ProcessInformation.hProcess);
2155 CloseHandle(ProcessInformation.hThread);
2158 PerlEnv_free_childenv(env);
2159 PerlEnv_free_childdir(dir);
2161 if (cname != cmdname)
2168 win32_execv(const char *cmdname, const char *const *argv)
2171 Perl_croak(aTHX_ PL_no_func, "execv");
2176 win32_execvp(const char *cmdname, const char *const *argv)
2179 Perl_croak(aTHX_ PL_no_func, "execvp");
2184 win32_perror(const char *str)
2190 win32_setbuf(FILE *pf, char *buf)
2193 Perl_croak(aTHX_ PL_no_func, "setbuf");
2197 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
2199 return setvbuf(pf, buf, type, size);
2203 win32_flushall(void)
2209 win32_fcloseall(void)
2215 win32_fgets(char *s, int n, FILE *pf)
2217 return fgets(s, n, pf);
2227 win32_fgetc(FILE *pf)
2233 win32_putc(int c, FILE *pf)
2239 win32_puts(const char *s)
2251 win32_putchar(int c)
2258 #ifndef USE_PERL_SBRK
2260 static char *committed = NULL;
2261 static char *base = NULL;
2262 static char *reserved = NULL;
2263 static char *brk = NULL;
2264 static DWORD pagesize = 0;
2265 static DWORD allocsize = 0;
2273 GetSystemInfo(&info);
2274 /* Pretend page size is larger so we don't perpetually
2275 * call the OS to commit just one page ...
2277 pagesize = info.dwPageSize << 3;
2278 allocsize = info.dwAllocationGranularity;
2280 /* This scheme fails eventually if request for contiguous
2281 * block is denied so reserve big blocks - this is only
2282 * address space not memory ...
2284 if (brk+need >= reserved)
2286 DWORD size = 64*1024*1024;
2288 if (committed && reserved && committed < reserved)
2290 /* Commit last of previous chunk cannot span allocations */
2291 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
2293 committed = reserved;
2295 /* Reserve some (more) space
2296 * Note this is a little sneaky, 1st call passes NULL as reserved
2297 * so lets system choose where we start, subsequent calls pass
2298 * the old end address so ask for a contiguous block
2300 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
2303 reserved = addr+size;
2318 if (brk > committed)
2320 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
2321 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
2336 win32_malloc(size_t size)
2338 return malloc(size);
2342 win32_calloc(size_t numitems, size_t size)
2344 return calloc(numitems,size);
2348 win32_realloc(void *block, size_t size)
2350 return realloc(block,size);
2354 win32_free(void *block)
2360 win32_open_osfhandle(intptr_t osfhandle, int flags)
2363 char fileflags=0; /* _osfile flags */
2365 Perl_croak_nocontext("win32_open_osfhandle() TBD on this platform");
2370 win32_get_osfhandle(int fd)
2373 char fileflags=0; /* _osfile flags */
2375 Perl_croak_nocontext("win32_get_osfhandle() TBD on this platform");
2380 win32_fdupopen(FILE *pf)
2385 int fileno = win32_dup(win32_fileno(pf));
2386 int fmode = palm_fgetmode(pfdup);
2388 fprintf(stderr,"DEBUG for win32_fdupopen()\n");
2390 /* open the file in the same mode */
2391 if(fmode & O_RDONLY) {
2395 else if(fmode & O_APPEND) {
2399 else if(fmode & O_RDWR) {
2405 /* it appears that the binmode is attached to the
2406 * file descriptor so binmode files will be handled
2409 pfdup = win32_fdopen(fileno, mode);
2411 /* move the file pointer to the same position */
2412 if (!fgetpos(pf, &pos)) {
2413 fsetpos(pfdup, &pos);
2419 win32_dynaload(const char* filename)
2424 hModule = XCELoadLibraryA(filename);
2429 /* this is needed by Cwd.pm... */
2436 SV *sv = sv_newmortal();
2438 xcegetcwd(buf, sizeof(buf));
2440 sv_setpv(sv, xcestrdup(buf));
2444 SvTAINTED_on(ST(0));
2454 Perl_croak(aTHX_ "usage: Win32::SetCwd($cwd)");
2456 if (!xcechdir(SvPV_nolen(ST(0))))
2463 XS(w32_GetTickCount)
2466 DWORD msec = GetTickCount();
2474 XS(w32_GetOSVersion)
2477 OSVERSIONINFOA osver;
2479 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
2480 if (!XCEGetVersionExA(&osver)) {
2483 mXPUSHp(osver.szCSDVersion, strlen(osver.szCSDVersion));
2484 mXPUSHi(osver.dwMajorVersion);
2485 mXPUSHi(osver.dwMinorVersion);
2486 mXPUSHi(osver.dwBuildNumber);
2488 mXPUSHi(osver.dwPlatformId);
2497 XSRETURN_IV(IsWinNT());
2505 XSRETURN_IV(IsWin95());
2513 XSRETURN_IV(IsWinCE());
2523 if(SystemParametersInfoW(SPI_GETOEMINFO, sizeof(wbuf), wbuf, FALSE))
2524 WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buf, sizeof(buf), 0, 0);
2526 sprintf(buf, "SystemParametersInfo failed: %d", GetLastError());
2537 Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)");
2548 Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)");
2551 char szSourceFile[MAX_PATH+1];
2552 strcpy(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(0))));
2553 bResult = XCECopyFileA(szSourceFile, SvPV_nolen(ST(1)),
2570 unsigned int flags = MB_OK;
2572 txt = SvPV_nolen(ST(0));
2574 if (items < 1 || items > 2)
2575 Perl_croak(aTHX_ "usage: Win32::MessageBox($txt, [$flags])");
2578 flags = SvIV(ST(1));
2580 res = XCEMessageBoxA(NULL, txt, "Perl", flags);
2586 XS(w32_GetPowerStatus)
2590 SYSTEM_POWER_STATUS_EX sps;
2592 if(GetSystemPowerStatusEx(&sps, TRUE) == FALSE)
2597 mXPUSHi(sps.ACLineStatus);
2598 mXPUSHi(sps.BatteryFlag);
2599 mXPUSHi(sps.BatteryLifePercent);
2600 mXPUSHi(sps.BatteryLifeTime);
2601 mXPUSHi(sps.BatteryFullLifeTime);
2602 mXPUSHi(sps.BackupBatteryFlag);
2603 mXPUSHi(sps.BackupBatteryLifePercent);
2604 mXPUSHi(sps.BackupBatteryLifeTime);
2605 mXPUSHi(sps.BackupBatteryFullLifeTime);
2617 SHELLEXECUTEINFO si;
2619 wchar_t wfile[MAX_PATH];
2623 Perl_croak(aTHX_ "usage: Win32::ShellEx($file, $verb)");
2625 file = SvPV_nolen(ST(0));
2626 verb = SvPV_nolen(ST(1));
2628 memset(&si, 0, sizeof(si));
2629 si.cbSize = sizeof(si);
2630 si.fMask = SEE_MASK_FLAG_NO_UI;
2632 MultiByteToWideChar(CP_ACP, 0, verb, -1,
2633 wverb, sizeof(wverb)/2);
2634 si.lpVerb = (TCHAR *)wverb;
2636 MultiByteToWideChar(CP_ACP, 0, file, -1,
2637 wfile, sizeof(wfile)/2);
2638 si.lpFile = (TCHAR *)wfile;
2640 if(ShellExecuteEx(&si) == FALSE)
2649 Perl_init_os_extras(void)
2652 char *file = __FILE__;
2655 w32_perlshell_tokens = NULL;
2656 w32_perlshell_items = -1;
2657 w32_fdpid = newAV(); /* XX needs to be in Perl_win32_init()? */
2658 Newx(w32_children, 1, child_tab);
2659 w32_num_children = 0;
2661 newXS("Win32::GetCwd", w32_GetCwd, file);
2662 newXS("Win32::SetCwd", w32_SetCwd, file);
2663 newXS("Win32::GetTickCount", w32_GetTickCount, file);
2664 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
2666 newXS("Win32::ShellEx", w32_ShellEx, file);
2668 newXS("Win32::IsWinNT", w32_IsWinNT, file);
2669 newXS("Win32::IsWin95", w32_IsWin95, file);
2670 newXS("Win32::IsWinCE", w32_IsWinCE, file);
2671 newXS("Win32::CopyFile", w32_CopyFile, file);
2672 newXS("Win32::Sleep", w32_Sleep, file);
2673 newXS("Win32::MessageBox", w32_MessageBox, file);
2674 newXS("Win32::GetPowerStatus", w32_GetPowerStatus, file);
2675 newXS("Win32::GetOemInfo", w32_GetOemInfo, file);
2684 fgets(buf, sizeof(buf), stdin);
2688 Perl_win32_init(int *argcp, char ***argvp)
2693 if((p = xcegetenv("PERLDEBUG")) && (p[0] == 'y' || p[0] == 'Y'))
2701 Perl_win32_term(void)
2712 win32_get_child_IO(child_IO_table* ptbl)
2714 ptbl->childStdIn = GetStdHandle(STD_INPUT_HANDLE);
2715 ptbl->childStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2716 ptbl->childStdErr = GetStdHandle(STD_ERROR_HANDLE);
2719 win32_flock(int fd, int oper)
2722 Perl_croak(aTHX_ PL_no_func, "flock");
2727 win32_waitpid(int pid, int *status, int flags)
2730 Perl_croak(aTHX_ PL_no_func, "waitpid");
2735 win32_wait(int *status)
2738 Perl_croak(aTHX_ PL_no_func, "wait");
2743 wce_reopen_stdout(char *fname)
2745 if(xcefreopen(fname, "w", stdout) == NULL)
2756 printf("Hit RETURN");
2758 fgets(buf, sizeof(buf), stdin);
2762 /* //////////////////////////////////////////////////////////////////// */
2767 getcwd(char *buf, size_t size)
2769 return xcegetcwd(buf, size);
2774 win32_popenlist(const char *mode, IV narg, SV **args)
2777 Perl_croak(aTHX_ "List form of pipe open not implemented");
2782 * a popen() clone that respects PERL5SHELL
2784 * changed to return PerlIO* rather than FILE * by BKS, 11-11-2000
2788 win32_popen(const char *command, const char *mode)
2790 return _popen(command, mode);
2798 win32_pclose(PerlIO *pf)
2803 #ifdef HAVE_INTERP_INTERN
2807 win32_csighandler(int sig)
2810 dTHXa(PERL_GET_SIG_CONTEXT);
2811 Perl_warn(aTHX_ "Got signal %d",sig);
2817 Perl_sys_intern_init(pTHX)
2820 w32_perlshell_tokens = NULL;
2821 w32_perlshell_vec = (char**)NULL;
2822 w32_perlshell_items = 0;
2823 w32_fdpid = newAV();
2824 Newx(w32_children, 1, child_tab);
2825 w32_num_children = 0;
2826 # ifdef USE_ITHREADS
2828 Newx(w32_pseudo_children, 1, child_tab);
2829 w32_num_pseudo_children = 0;
2831 w32_init_socktype = 0;
2837 Perl_sys_intern_clear(pTHX)
2839 Safefree(w32_perlshell_tokens);
2840 Safefree(w32_perlshell_vec);
2841 /* NOTE: w32_fdpid is freed by sv_clean_all() */
2842 Safefree(w32_children);
2844 KillTimer(NULL,w32_timerid);
2847 # ifdef USE_ITHREADS
2848 Safefree(w32_pseudo_children);
2852 # ifdef USE_ITHREADS
2855 Perl_sys_intern_dup(pTHX_ struct interp_intern *src, struct interp_intern *dst)
2857 dst->perlshell_tokens = NULL;
2858 dst->perlshell_vec = (char**)NULL;
2859 dst->perlshell_items = 0;
2860 dst->fdpid = newAV();
2861 Newxz(dst->children, 1, child_tab);
2863 Newxz(dst->pseudo_children, 1, child_tab);
2864 dst->thr_intern.Winit_socktype = 0;
2866 dst->poll_count = 0;
2867 Copy(src->sigtable,dst->sigtable,SIG_SIZE,Sighandler_t);
2869 # endif /* USE_ITHREADS */
2870 #endif /* HAVE_INTERP_INTERN */
2872 // added to remove undefied symbol error in CodeWarrior compilation
2874 Perl_Ireentrant_buffer_ptr(aTHX)