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, char *trailing, ...);
74 static void remove_dead_process(long deceased);
75 static long find_pid(int pid);
76 static char * qualified_path(const char *cmd);
77 static char * win32_get_xlib(const char *pl, const char *xlib,
81 static void remove_dead_pseudo_process(long child);
82 static long find_pseudo_pid(int pid);
85 int _fmode = O_TEXT; /* celib do not provide _fmode, so we define it here */
88 HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
89 char w32_module_name[MAX_PATH+1];
92 static DWORD w32_platform = (DWORD)-1;
97 return (win32_os_id() == VER_PLATFORM_WIN32_WINDOWS);
103 return (win32_os_id() == VER_PLATFORM_WIN32_NT);
109 return (win32_os_id() == VER_PLATFORM_WIN32_CE);
113 set_w32_module_name(void)
116 XCEGetModuleFileNameA((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
117 ? XCEGetModuleHandleA(NULL)
118 : w32_perldll_handle),
119 w32_module_name, sizeof(w32_module_name));
121 /* normalize to forward slashes */
122 ptr = w32_module_name;
130 /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
132 get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
134 /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
137 const char *subkey = "Software\\Perl";
141 retval = XCERegOpenKeyExA(hkey, subkey, 0, KEY_READ, &handle);
142 if (retval == ERROR_SUCCESS) {
144 retval = XCERegQueryValueExA(handle, valuename, 0, &type, NULL, &datalen);
145 if (retval == ERROR_SUCCESS && type == REG_SZ) {
148 *svp = sv_2mortal(newSVpvn("",0));
149 SvGROW(*svp, datalen);
150 retval = XCERegQueryValueExA(handle, valuename, 0, NULL,
151 (PBYTE)SvPVX(*svp), &datalen);
152 if (retval == ERROR_SUCCESS) {
154 SvCUR_set(*svp,datalen-1);
162 /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
164 get_regstr(const char *valuename, SV **svp)
166 char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
168 str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
172 /* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
174 get_emd_part(SV **prev_pathp, char *trailing_path, ...)
178 char mod_name[MAX_PATH+1];
182 int oldsize, newsize;
185 va_start(ap, trailing_path);
186 strip = va_arg(ap, char *);
188 sprintf(base, "%d.%d", (int)PERL_REVISION, (int)PERL_VERSION);
189 baselen = strlen(base);
191 if (!*w32_module_name) {
192 set_w32_module_name();
194 strcpy(mod_name, w32_module_name);
195 ptr = strrchr(mod_name, '/');
196 while (ptr && strip) {
197 /* look for directories to skip back */
200 ptr = strrchr(mod_name, '/');
201 /* avoid stripping component if there is no slash,
202 * or it doesn't match ... */
203 if (!ptr || stricmp(ptr+1, strip) != 0) {
204 /* ... but not if component matches m|5\.$patchlevel.*| */
205 if (!ptr || !(*strip == '5' && *(ptr+1) == '5'
206 && strncmp(strip, base, baselen) == 0
207 && strncmp(ptr+1, base, baselen) == 0))
213 strip = va_arg(ap, char *);
221 strcpy(++ptr, trailing_path);
223 /* only add directory if it exists */
224 if (XCEGetFileAttributesA(mod_name) != (DWORD) -1) {
225 /* directory exists */
228 *prev_pathp = sv_2mortal(newSVpvn("",0));
229 sv_catpvn(*prev_pathp, ";", 1);
230 sv_catpv(*prev_pathp, mod_name);
231 return SvPVX(*prev_pathp);
238 win32_get_privlib(const char *pl)
241 char *stdlib = "lib";
242 char buffer[MAX_PATH+1];
245 /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
246 sprintf(buffer, "%s-%s", stdlib, pl);
247 if (!get_regstr(buffer, &sv))
248 (void)get_regstr(stdlib, &sv);
250 /* $stdlib .= ";$EMD/../../lib" */
251 return get_emd_part(&sv, stdlib, ARCHNAME, "bin", Nullch);
255 win32_get_xlib(const char *pl, const char *xlib, const char *libname)
259 char pathstr[MAX_PATH+1];
265 /* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */
266 sprintf(regstr, "%s-%s", xlib, pl);
267 (void)get_regstr(regstr, &sv1);
270 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */
271 sprintf(pathstr, "%s/%s/lib", libname, pl);
272 (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
274 /* $HKCU{$xlib} || $HKLM{$xlib} . ---; */
275 (void)get_regstr(xlib, &sv2);
278 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */
279 sprintf(pathstr, "%s/lib", libname);
280 (void)get_emd_part(&sv2, pathstr, ARCHNAME, "bin", pl, Nullch);
289 sv_catpvn(sv1, ";", 1);
296 win32_get_sitelib(const char *pl)
298 return win32_get_xlib(pl, "sitelib", "site");
301 #ifndef PERL_VENDORLIB_NAME
302 # define PERL_VENDORLIB_NAME "vendor"
306 win32_get_vendorlib(const char *pl)
308 return win32_get_xlib(pl, "vendorlib", PERL_VENDORLIB_NAME);
312 has_shell_metachars(char *ptr)
318 * Scan string looking for redirection (< or >) or pipe
319 * characters (|) that are not in a quoted string.
320 * Shell variable interpolation (%VAR%) can also happen inside strings.
352 #if !defined(PERL_IMPLICIT_SYS)
353 /* since the current process environment is being updated in util.c
354 * the library functions will get the correct environment
357 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
359 printf("popen(%s)\n", cmd);
361 Perl_croak(aTHX_ PL_no_func, "popen");
366 Perl_my_pclose(pTHX_ PerlIO *fp)
368 Perl_croak(aTHX_ PL_no_func, "pclose");
373 DllExport unsigned long
376 static OSVERSIONINFOA osver;
378 if (osver.dwPlatformId != w32_platform) {
379 memset(&osver, 0, sizeof(OSVERSIONINFOA));
380 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
381 XCEGetVersionExA(&osver);
382 w32_platform = osver.dwPlatformId;
384 return (unsigned long)w32_platform;
394 return -((int)w32_pseudo_id);
400 /* Tokenize a string. Words are null-separated, and the list
401 * ends with a doubled null. Any character (except null and
402 * including backslash) may be escaped by preceding it with a
403 * backslash (the backslash will be stripped).
404 * Returns number of words in result buffer.
407 tokenize(const char *str, char **dest, char ***destv)
409 char *retstart = Nullch;
410 char **retvstart = 0;
414 int slen = strlen(str);
416 register char **retv;
417 Newx(ret, slen+2, char);
418 Newx(retv, (slen+3)/2, char*);
426 if (*ret == '\\' && *str)
428 else if (*ret == ' ') {
444 retvstart[items] = Nullch;
454 win32_pipe(int *pfd, unsigned int size, int mode)
457 Perl_croak(aTHX_ PL_no_func, "pipe");
462 win32_times(struct tms *timebuf)
465 Perl_croak(aTHX_ PL_no_func, "times");
470 win32_signal(int sig, Sighandler_t subcode)
472 return xcesignal(sig, subcode);
479 if (!w32_perlshell_tokens) {
480 /* we don't use COMSPEC here for two reasons:
481 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
482 * uncontrolled unportability of the ensuing scripts.
483 * 2. PERL5SHELL could be set to a shell that may not be fit for
484 * interactive use (which is what most programs look in COMSPEC
487 const char* defaultshell = (IsWinNT()
488 ? "cmd.exe /x/d/c" : "command.com /c");
489 const char *usershell = PerlEnv_getenv("PERL5SHELL");
490 w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
491 &w32_perlshell_tokens,
497 Perl_do_aspawn(pTHX_ SV *really, SV **mark, SV **sp)
499 Perl_croak(aTHX_ PL_no_func, "aspawn");
503 /* returns pointer to the next unquoted space or the end of the string */
505 find_next_space(const char *s)
507 bool in_quotes = FALSE;
509 /* ignore doubled backslashes, or backslash+quote */
510 if (*s == '\\' && (s[1] == '\\' || s[1] == '"')) {
513 /* keep track of when we're within quotes */
514 else if (*s == '"') {
516 in_quotes = !in_quotes;
518 /* break it up only at spaces that aren't in quotes */
519 else if (!in_quotes && isSPACE(*s))
529 do_spawn2(pTHX_ char *cmd, int exectype)
535 BOOL needToTry = TRUE;
538 /* Save an extra exec if possible. See if there are shell
539 * metacharacters in it */
540 if (!has_shell_metachars(cmd)) {
541 Newx(argv, strlen(cmd) / 2 + 2, char*);
542 Newx(cmd2, strlen(cmd) + 1, char);
545 for (s = cmd2; *s;) {
546 while (*s && isSPACE(*s))
550 s = find_next_space(s);
558 status = win32_spawnvp(P_WAIT, argv[0],
559 (const char* const*)argv);
561 case EXECF_SPAWN_NOWAIT:
562 status = win32_spawnvp(P_NOWAIT, argv[0],
563 (const char* const*)argv);
566 status = win32_execvp(argv[0], (const char* const*)argv);
569 if (status != -1 || errno == 0)
579 Newx(argv, w32_perlshell_items + 2, char*);
580 while (++i < w32_perlshell_items)
581 argv[i] = w32_perlshell_vec[i];
586 status = win32_spawnvp(P_WAIT, argv[0],
587 (const char* const*)argv);
589 case EXECF_SPAWN_NOWAIT:
590 status = win32_spawnvp(P_NOWAIT, argv[0],
591 (const char* const*)argv);
594 status = win32_execvp(argv[0], (const char* const*)argv);
600 if (exectype == EXECF_SPAWN_NOWAIT) {
602 PL_statusvalue = -1; /* >16bits hint for pp_system() */
606 if (ckWARN(WARN_EXEC))
607 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't %s \"%s\": %s",
608 (exectype == EXECF_EXEC ? "exec" : "spawn"),
609 cmd, strerror(errno));
614 PL_statusvalue = status;
620 Perl_do_spawn(pTHX_ char *cmd)
622 return do_spawn2(aTHX_ cmd, EXECF_SPAWN);
626 Perl_do_spawn_nowait(pTHX_ char *cmd)
628 return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT);
632 Perl_do_exec(pTHX_ const char *cmd)
634 do_spawn2(aTHX_ cmd, EXECF_EXEC);
638 /* The idea here is to read all the directory names into a string table
639 * (separated by nulls) and when one of the other dir functions is called
640 * return the pointer to the current file name.
643 win32_opendir(const char *filename)
649 char scanname[MAX_PATH+3];
651 WIN32_FIND_DATAA aFindData;
652 WIN32_FIND_DATAW wFindData;
654 char buffer[MAX_PATH*2];
655 WCHAR wbuffer[MAX_PATH+1];
658 len = strlen(filename);
662 /* check to see if filename is a directory */
663 if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
666 /* Get us a DIR structure */
669 /* Create the search pattern */
670 strcpy(scanname, filename);
672 /* bare drive name means look in cwd for drive */
673 if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') {
674 scanname[len++] = '.';
675 scanname[len++] = '/';
677 else if (scanname[len-1] != '/' && scanname[len-1] != '\\') {
678 scanname[len++] = '/';
680 scanname[len++] = '*';
681 scanname[len] = '\0';
683 /* do the FindFirstFile call */
684 fh = FindFirstFile(PerlDir_mapA(scanname), &aFindData);
686 if (fh == INVALID_HANDLE_VALUE) {
687 DWORD err = GetLastError();
688 /* FindFirstFile() fails on empty drives! */
690 case ERROR_FILE_NOT_FOUND:
692 case ERROR_NO_MORE_FILES:
693 case ERROR_PATH_NOT_FOUND:
696 case ERROR_NOT_ENOUGH_MEMORY:
707 /* now allocate the first part of the string table for
708 * the filenames that we find.
710 ptr = aFindData.cFileName;
716 Newx(dirp->start, dirp->size, char);
717 strcpy(dirp->start, ptr);
719 dirp->end = dirp->curr = dirp->start;
725 /* Readdir just returns the current string pointer and bumps the
726 * string pointer to the nDllExport entry.
728 DllExport struct direct *
729 win32_readdir(DIR *dirp)
734 /* first set up the structure to return */
735 len = strlen(dirp->curr);
736 strcpy(dirp->dirstr.d_name, dirp->curr);
737 dirp->dirstr.d_namlen = len;
740 dirp->dirstr.d_ino = dirp->curr - dirp->start;
742 /* Now set up for the next call to readdir */
743 dirp->curr += len + 1;
744 if (dirp->curr >= dirp->end) {
748 WIN32_FIND_DATAW wFindData;
749 WIN32_FIND_DATAA aFindData;
750 char buffer[MAX_PATH*2];
752 /* finding the next file that matches the wildcard
753 * (which should be all of them in this directory!).
755 res = FindNextFile(dirp->handle, &aFindData);
757 ptr = aFindData.cFileName;
759 long endpos = dirp->end - dirp->start;
760 long newsize = endpos + strlen(ptr) + 1;
761 /* bump the string table size by enough for the
762 * new name and its null terminator */
763 while (newsize > dirp->size) {
764 long curpos = dirp->curr - dirp->start;
766 Renew(dirp->start, dirp->size, char);
767 dirp->curr = dirp->start + curpos;
769 strcpy(dirp->start + endpos, ptr);
770 dirp->end = dirp->start + newsize;
776 return &(dirp->dirstr);
782 /* Telldir returns the current string pointer position */
784 win32_telldir(DIR *dirp)
786 return (dirp->curr - dirp->start);
790 /* Seekdir moves the string pointer to a previously saved position
791 * (returned by telldir).
794 win32_seekdir(DIR *dirp, long loc)
796 dirp->curr = dirp->start + loc;
799 /* Rewinddir resets the string pointer to the start */
801 win32_rewinddir(DIR *dirp)
803 dirp->curr = dirp->start;
806 /* free the memory allocated by opendir */
808 win32_closedir(DIR *dirp)
811 if (dirp->handle != INVALID_HANDLE_VALUE)
812 FindClose(dirp->handle);
813 Safefree(dirp->start);
819 /////!!!!!!!!!!! return here and do right stuff!!!!
822 win32_opendir(const char *filename)
824 return opendir(filename);
827 DllExport struct direct *
828 win32_readdir(DIR *dirp)
830 return readdir(dirp);
834 win32_telldir(DIR *dirp)
837 Perl_croak(aTHX_ PL_no_func, "telldir");
842 win32_seekdir(DIR *dirp, long loc)
845 Perl_croak(aTHX_ PL_no_func, "seekdir");
849 win32_rewinddir(DIR *dirp)
852 Perl_croak(aTHX_ PL_no_func, "rewinddir");
856 win32_closedir(DIR *dirp)
864 win32_kill(int pid, int sig)
867 Perl_croak(aTHX_ PL_no_func, "kill");
872 win32_stat(const char *path, struct stat *sbuf)
874 return xcestat(path, sbuf);
878 win32_longpath(char *path)
883 #ifndef USE_WIN32_RTL_ENV
886 win32_getenv(const char *name)
888 return xcegetenv(name);
892 win32_putenv(const char *name)
894 return xceputenv(name);
900 filetime_to_clock(PFILETIME ft)
902 __int64 qw = ft->dwHighDateTime;
904 qw |= ft->dwLowDateTime;
905 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
909 /* fix utime() so it works on directories in NT */
911 filetime_from_time(PFILETIME pFileTime, time_t Time)
913 struct tm *pTM = localtime(&Time);
914 SYSTEMTIME SystemTime;
920 SystemTime.wYear = pTM->tm_year + 1900;
921 SystemTime.wMonth = pTM->tm_mon + 1;
922 SystemTime.wDay = pTM->tm_mday;
923 SystemTime.wHour = pTM->tm_hour;
924 SystemTime.wMinute = pTM->tm_min;
925 SystemTime.wSecond = pTM->tm_sec;
926 SystemTime.wMilliseconds = 0;
928 return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
929 LocalFileTimeToFileTime(&LocalTime, pFileTime);
933 win32_unlink(const char *filename)
935 return xceunlink(filename);
939 win32_utime(const char *filename, struct utimbuf *times)
941 return xceutime(filename, (struct _utimbuf *) times);
945 win32_gettimeofday(struct timeval *tp, void *not_used)
947 return xcegettimeofday(tp,not_used);
951 win32_uname(struct utsname *name)
954 STRLEN nodemax = sizeof(name->nodename)-1;
955 OSVERSIONINFOA osver;
957 memset(&osver, 0, sizeof(OSVERSIONINFOA));
958 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
959 if (XCEGetVersionExA(&osver)) {
961 switch (osver.dwPlatformId) {
962 case VER_PLATFORM_WIN32_CE:
963 strcpy(name->sysname, "Windows CE");
965 case VER_PLATFORM_WIN32_WINDOWS:
966 strcpy(name->sysname, "Windows");
968 case VER_PLATFORM_WIN32_NT:
969 strcpy(name->sysname, "Windows NT");
971 case VER_PLATFORM_WIN32s:
972 strcpy(name->sysname, "Win32s");
975 strcpy(name->sysname, "Win32 Unknown");
980 sprintf(name->release, "%d.%d",
981 osver.dwMajorVersion, osver.dwMinorVersion);
984 sprintf(name->version, "Build %d",
985 osver.dwPlatformId == VER_PLATFORM_WIN32_NT
986 ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff));
987 if (osver.szCSDVersion[0]) {
988 char *buf = name->version + strlen(name->version);
989 sprintf(buf, " (%s)", osver.szCSDVersion);
993 *name->sysname = '\0';
994 *name->version = '\0';
995 *name->release = '\0';
999 hep = win32_gethostbyname("localhost");
1001 STRLEN len = strlen(hep->h_name);
1002 if (len <= nodemax) {
1003 strcpy(name->nodename, hep->h_name);
1006 strncpy(name->nodename, hep->h_name, nodemax);
1007 name->nodename[nodemax] = '\0';
1012 if (!XCEGetComputerNameA(name->nodename, &sz))
1013 *name->nodename = '\0';
1016 /* machine (architecture) */
1020 GetSystemInfo(&info);
1022 switch (info.wProcessorArchitecture) {
1023 case PROCESSOR_ARCHITECTURE_INTEL:
1024 arch = "x86"; break;
1025 case PROCESSOR_ARCHITECTURE_MIPS:
1026 arch = "mips"; break;
1027 case PROCESSOR_ARCHITECTURE_ALPHA:
1028 arch = "alpha"; break;
1029 case PROCESSOR_ARCHITECTURE_PPC:
1030 arch = "ppc"; break;
1031 case PROCESSOR_ARCHITECTURE_ARM:
1032 arch = "arm"; break;
1033 case PROCESSOR_HITACHI_SH3:
1034 arch = "sh3"; break;
1035 case PROCESSOR_SHx_SH3:
1036 arch = "sh3"; break;
1039 arch = "unknown"; break;
1041 strcpy(name->machine, arch);
1046 /* Timing related stuff */
1049 do_raise(pTHX_ int sig)
1051 if (sig < SIG_SIZE) {
1052 Sighandler_t handler = w32_sighandler[sig];
1053 if (handler == SIG_IGN) {
1056 else if (handler != SIG_DFL) {
1061 /* Choose correct default behaviour */
1077 /* Tell caller to exit thread/process as approriate */
1082 sig_terminate(pTHX_ int sig)
1084 Perl_warn(aTHX_ "Terminating on signal SIG%s(%d)\n",PL_sig_name[sig], sig);
1085 /* exit() seems to be safe, my_exit() or die() is a problem in ^C
1092 win32_async_check(pTHX)
1096 /* Passing PeekMessage -1 as HWND (2nd arg) only get PostThreadMessage() messages
1097 * and ignores window messages - should co-exist better with windows apps e.g. Tk
1099 while (PeekMessage(&msg, (HWND)-1, 0, 0, PM_REMOVE|PM_NOYIELD)) {
1101 switch(msg.message) {
1104 /* Perhaps some other messages could map to signals ? ... */
1107 /* Treat WM_QUIT like SIGHUP? */
1113 /* We use WM_USER to fake kill() with other signals */
1117 if (do_raise(aTHX_ sig)) {
1118 sig_terminate(aTHX_ sig);
1124 /* alarm() is a one-shot but SetTimer() repeats so kill it */
1126 KillTimer(NULL,w32_timerid);
1129 /* Now fake a call to signal handler */
1130 if (do_raise(aTHX_ 14)) {
1131 sig_terminate(aTHX_ 14);
1136 /* Otherwise do normal Win32 thing - in case it is useful */
1138 TranslateMessage(&msg);
1139 DispatchMessage(&msg);
1146 /* Above or other stuff may have set a signal flag */
1147 if (PL_sig_pending) {
1153 /* This function will not return until the timeout has elapsed, or until
1154 * one of the handles is ready. */
1156 win32_msgwait(pTHX_ DWORD count, LPHANDLE handles, DWORD timeout, LPDWORD resultp)
1158 /* We may need several goes at this - so compute when we stop */
1160 if (timeout != INFINITE) {
1161 ticks = GetTickCount();
1165 DWORD result = MsgWaitForMultipleObjects(count,handles,FALSE,timeout-ticks, QS_ALLEVENTS);
1168 if (result == WAIT_TIMEOUT) {
1169 /* Ran out of time - explicit return of zero to avoid -ve if we
1170 have scheduling issues
1174 if (timeout != INFINITE) {
1175 ticks = GetTickCount();
1177 if (result == WAIT_OBJECT_0 + count) {
1178 /* Message has arrived - check it */
1179 (void)win32_async_check(aTHX);
1182 /* Not timeout or message - one of handles is ready */
1186 /* compute time left to wait */
1187 ticks = timeout - ticks;
1188 /* If we are past the end say zero */
1189 return (ticks > 0) ? ticks : 0;
1192 static UINT timerid = 0;
1194 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
1197 KillTimer(NULL,timerid);
1202 DllExport unsigned int
1203 win32_sleep(unsigned int t)
1208 DllExport unsigned int
1209 win32_alarm(unsigned int sec)
1212 * the 'obvious' implentation is SetTimer() with a callback
1213 * which does whatever receiving SIGALRM would do
1214 * we cannot use SIGALRM even via raise() as it is not
1215 * one of the supported codes in <signal.h>
1217 * Snag is unless something is looking at the message queue
1218 * nothing happens :-(
1223 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
1225 Perl_croak_nocontext("Cannot set timer");
1231 KillTimer(NULL,timerid);
1238 #ifdef HAVE_DES_FCRYPT
1239 extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf);
1243 win32_crypt(const char *txt, const char *salt)
1246 #ifdef HAVE_DES_FCRYPT
1248 return des_fcrypt(txt, salt, w32_crypt_buffer);
1250 Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia.");
1257 * redirected io subsystem for all XS modules
1270 return (&(environ));
1273 /* the rest are the remapped stdio routines */
1280 char *g_getlogin() {
1281 return "no-getlogin";
1297 win32_ferror(FILE *fp)
1299 return (ferror(fp));
1304 win32_feof(FILE *fp)
1310 * Since the errors returned by the socket error function
1311 * WSAGetLastError() are not known by the library routine strerror
1312 * we have to roll our own.
1316 win32_strerror(int e)
1318 return xcestrerror(e);
1322 win32_str_os_error(void *sv, DWORD dwErr)
1326 sv_setpvn((SV*)sv, "Error", 5);
1331 win32_fprintf(FILE *fp, const char *format, ...)
1334 va_start(marker, format); /* Initialize variable arguments. */
1336 return (vfprintf(fp, format, marker));
1340 win32_printf(const char *format, ...)
1343 va_start(marker, format); /* Initialize variable arguments. */
1345 return (vprintf(format, marker));
1349 win32_vfprintf(FILE *fp, const char *format, va_list args)
1351 return (vfprintf(fp, format, args));
1355 win32_vprintf(const char *format, va_list args)
1357 return (vprintf(format, args));
1361 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1363 return fread(buf, size, count, fp);
1367 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1369 return fwrite(buf, size, count, fp);
1373 win32_fopen(const char *filename, const char *mode)
1375 return xcefopen(filename, mode);
1379 win32_fdopen(int handle, const char *mode)
1381 return palm_fdopen(handle, mode);
1385 win32_freopen(const char *path, const char *mode, FILE *stream)
1387 return xcefreopen(path, mode, stream);
1391 win32_fclose(FILE *pf)
1393 return xcefclose(pf);
1397 win32_fputs(const char *s,FILE *pf)
1399 return fputs(s, pf);
1403 win32_fputc(int c,FILE *pf)
1409 win32_ungetc(int c,FILE *pf)
1411 return ungetc(c,pf);
1415 win32_getc(FILE *pf)
1421 win32_fileno(FILE *pf)
1423 return palm_fileno(pf);
1427 win32_clearerr(FILE *pf)
1434 win32_fflush(FILE *pf)
1440 win32_ftell(FILE *pf)
1446 win32_fseek(FILE *pf, Off_t offset,int origin)
1448 return fseek(pf, offset, origin);
1451 /* fpos_t seems to be int64 on hpc pro! Really stupid. */
1452 /* But maybe someday there will be such large disks in a hpc... */
1454 win32_fgetpos(FILE *pf, fpos_t *p)
1456 return fgetpos(pf, p);
1460 win32_fsetpos(FILE *pf, const fpos_t *p)
1462 return fsetpos(pf, p);
1466 win32_rewind(FILE *pf)
1468 fseek(pf, 0, SEEK_SET);
1476 char prefix[MAX_PATH+1];
1477 char filename[MAX_PATH+1];
1478 DWORD len = GetTempPath(MAX_PATH, prefix);
1479 if (len && len < MAX_PATH) {
1480 if (GetTempFileName(prefix, "plx", 0, filename)) {
1481 HANDLE fh = CreateFile(filename,
1482 DELETE | GENERIC_READ | GENERIC_WRITE,
1486 FILE_ATTRIBUTE_NORMAL
1487 | FILE_FLAG_DELETE_ON_CLOSE,
1489 if (fh != INVALID_HANDLE_VALUE) {
1490 int fd = win32_open_osfhandle((intptr_t)fh, 0);
1492 #if defined(__BORLANDC__)
1493 setmode(fd,O_BINARY);
1495 DEBUG_p(PerlIO_printf(Perl_debug_log,
1496 "Created tmpfile=%s\n",filename));
1508 int fd = win32_tmpfd();
1510 return win32_fdopen(fd, "w+b");
1523 win32_fstat(int fd, struct stat *sbufptr)
1525 return xcefstat(fd, sbufptr);
1529 win32_link(const char *oldname, const char *newname)
1532 Perl_croak(aTHX_ PL_no_func, "link");
1538 win32_rename(const char *oname, const char *newname)
1540 return xcerename(oname, newname);
1544 win32_setmode(int fd, int mode)
1546 /* currently 'celib' seem to have this function in src, but not
1547 * exported. When it will be, we'll uncomment following line.
1549 /* return xcesetmode(fd, mode); */
1554 win32_chsize(int fd, Off_t size)
1556 return chsize(fd, size);
1560 win32_lseek(int fd, Off_t offset, int origin)
1562 return xcelseek(fd, offset, origin);
1568 return xcelseek(fd, 0, SEEK_CUR);
1572 win32_open(const char *path, int flag, ...)
1578 pmode = va_arg(ap, int);
1581 return xceopen(path, flag, pmode);
1587 return xceclose(fd);
1594 Perl_croak(aTHX_ PL_no_func, "eof");
1601 return xcedup(fd); /* from celib/ceio.c; requires some more work on it */
1605 win32_dup2(int fd1,int fd2)
1607 return xcedup2(fd1,fd2);
1611 win32_read(int fd, void *buf, unsigned int cnt)
1613 return xceread(fd, buf, cnt);
1617 win32_write(int fd, const void *buf, unsigned int cnt)
1619 return xcewrite(fd, (void *) buf, cnt);
1623 win32_mkdir(const char *dir, int mode)
1625 return xcemkdir(dir);
1629 win32_rmdir(const char *dir)
1631 return xcermdir(dir);
1635 win32_chdir(const char *dir)
1637 return xcechdir(dir);
1641 win32_access(const char *path, int mode)
1643 return xceaccess(path, mode);
1647 win32_chmod(const char *path, int mode)
1649 return xcechmod(path, mode);
1653 create_command_line(char *cname, STRLEN clen, const char * const *args)
1660 bool bat_file = FALSE;
1661 bool cmd_shell = FALSE;
1662 bool dumb_shell = FALSE;
1663 bool extra_quotes = FALSE;
1664 bool quote_next = FALSE;
1667 cname = (char*)args[0];
1669 /* The NT cmd.exe shell has the following peculiarity that needs to be
1670 * worked around. It strips a leading and trailing dquote when any
1671 * of the following is true:
1672 * 1. the /S switch was used
1673 * 2. there are more than two dquotes
1674 * 3. there is a special character from this set: &<>()@^|
1675 * 4. no whitespace characters within the two dquotes
1676 * 5. string between two dquotes isn't an executable file
1677 * To work around this, we always add a leading and trailing dquote
1678 * to the string, if the first argument is either "cmd.exe" or "cmd",
1679 * and there were at least two or more arguments passed to cmd.exe
1680 * (not including switches).
1681 * XXX the above rules (from "cmd /?") don't seem to be applied
1682 * always, making for the convolutions below :-(
1686 clen = strlen(cname);
1689 && (stricmp(&cname[clen-4], ".bat") == 0
1690 || (IsWinNT() && stricmp(&cname[clen-4], ".cmd") == 0)))
1696 char *exe = strrchr(cname, '/');
1697 char *exe2 = strrchr(cname, '\\');
1704 if (stricmp(exe, "cmd.exe") == 0 || stricmp(exe, "cmd") == 0) {
1708 else if (stricmp(exe, "command.com") == 0
1709 || stricmp(exe, "command") == 0)
1716 DEBUG_p(PerlIO_printf(Perl_debug_log, "Args "));
1717 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1718 STRLEN curlen = strlen(arg);
1719 if (!(arg[0] == '"' && arg[curlen-1] == '"'))
1720 len += 2; /* assume quoting needed (worst case) */
1722 DEBUG_p(PerlIO_printf(Perl_debug_log, "[%s]",arg));
1724 DEBUG_p(PerlIO_printf(Perl_debug_log, "\n"));
1727 Newx(cmd, len, char);
1732 extra_quotes = TRUE;
1735 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1737 STRLEN curlen = strlen(arg);
1739 /* we want to protect empty arguments and ones with spaces with
1740 * dquotes, but only if they aren't already there */
1745 else if (quote_next) {
1746 /* see if it really is multiple arguments pretending to
1747 * be one and force a set of quotes around it */
1748 if (*find_next_space(arg))
1751 else if (!(arg[0] == '"' && curlen > 1 && arg[curlen-1] == '"')) {
1753 while (i < curlen) {
1754 if (isSPACE(arg[i])) {
1757 else if (arg[i] == '"') {
1781 && *arg == '/' /* see if arg is "/c", "/x/c", "/x/d/c" etc. */
1782 && stricmp(arg+curlen-2, "/c") == 0)
1784 /* is there a next argument? */
1785 if (args[index+1]) {
1786 /* are there two or more next arguments? */
1787 if (args[index+2]) {
1789 extra_quotes = TRUE;
1792 /* single argument, force quoting if it has spaces */
1808 qualified_path(const char *cmd)
1812 char *fullcmd, *curfullcmd;
1818 fullcmd = (char*)cmd;
1820 if (*fullcmd == '/' || *fullcmd == '\\')
1827 pathstr = PerlEnv_getenv("PATH");
1828 Newx(fullcmd, MAX_PATH+1, char);
1829 curfullcmd = fullcmd;
1834 /* start by appending the name to the current prefix */
1835 strcpy(curfullcmd, cmd);
1836 curfullcmd += cmdlen;
1838 /* if it doesn't end with '.', or has no extension, try adding
1839 * a trailing .exe first */
1840 if (cmd[cmdlen-1] != '.'
1841 && (cmdlen < 4 || cmd[cmdlen-4] != '.'))
1843 strcpy(curfullcmd, ".exe");
1844 res = GetFileAttributes(fullcmd);
1845 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1850 /* that failed, try the bare name */
1851 res = GetFileAttributes(fullcmd);
1852 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1855 /* quit if no other path exists, or if cmd already has path */
1856 if (!pathstr || !*pathstr || has_slash)
1859 /* skip leading semis */
1860 while (*pathstr == ';')
1863 /* build a new prefix from scratch */
1864 curfullcmd = fullcmd;
1865 while (*pathstr && *pathstr != ';') {
1866 if (*pathstr == '"') { /* foo;"baz;etc";bar */
1867 pathstr++; /* skip initial '"' */
1868 while (*pathstr && *pathstr != '"') {
1869 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1870 *curfullcmd++ = *pathstr;
1874 pathstr++; /* skip trailing '"' */
1877 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1878 *curfullcmd++ = *pathstr;
1883 pathstr++; /* skip trailing semi */
1884 if (curfullcmd > fullcmd /* append a dir separator */
1885 && curfullcmd[-1] != '/' && curfullcmd[-1] != '\\')
1887 *curfullcmd++ = '\\';
1895 /* The following are just place holders.
1896 * Some hosts may provide and environment that the OS is
1897 * not tracking, therefore, these host must provide that
1898 * environment and the current directory to CreateProcess
1902 win32_get_childenv(void)
1908 win32_free_childenv(void* d)
1913 win32_clearenv(void)
1915 char *envv = GetEnvironmentStrings();
1919 char *end = strchr(cur,'=');
1920 if (end && end != cur) {
1922 xcesetenv(cur, "", 0);
1924 cur = end + strlen(end+1)+2;
1926 else if ((len = strlen(cur)))
1929 FreeEnvironmentStrings(envv);
1933 win32_get_childdir(void)
1937 char szfilename[MAX_PATH+1];
1938 GetCurrentDirectoryA(MAX_PATH+1, szfilename);
1940 Newx(ptr, strlen(szfilename)+1, char);
1941 strcpy(ptr, szfilename);
1946 win32_free_childdir(char* d)
1952 /* XXX this needs to be made more compatible with the spawnvp()
1953 * provided by the various RTLs. In particular, searching for
1954 * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented.
1955 * This doesn't significantly affect perl itself, because we
1956 * always invoke things using PERL5SHELL if a direct attempt to
1957 * spawn the executable fails.
1959 * XXX splitting and rejoining the commandline between do_aspawn()
1960 * and win32_spawnvp() could also be avoided.
1964 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1966 #ifdef USE_RTL_SPAWNVP
1967 return spawnvp(mode, cmdname, (char * const *)argv);
1974 STARTUPINFO StartupInfo;
1975 PROCESS_INFORMATION ProcessInformation;
1978 char *fullcmd = Nullch;
1979 char *cname = (char *)cmdname;
1983 clen = strlen(cname);
1984 /* if command name contains dquotes, must remove them */
1985 if (strchr(cname, '"')) {
1987 Newx(cname,clen+1,char);
2000 cmd = create_command_line(cname, clen, argv);
2002 env = PerlEnv_get_childenv();
2003 dir = PerlEnv_get_childdir();
2006 case P_NOWAIT: /* asynch + remember result */
2007 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
2012 /* Create a new process group so we can use GenerateConsoleCtrlEvent()
2015 /* not supported on CE create |= CREATE_NEW_PROCESS_GROUP; */
2018 case P_WAIT: /* synchronous execution */
2020 default: /* invalid mode */
2025 memset(&StartupInfo,0,sizeof(StartupInfo));
2026 StartupInfo.cb = sizeof(StartupInfo);
2027 memset(&tbl,0,sizeof(tbl));
2028 PerlEnv_get_child_IO(&tbl);
2029 StartupInfo.dwFlags = tbl.dwFlags;
2030 StartupInfo.dwX = tbl.dwX;
2031 StartupInfo.dwY = tbl.dwY;
2032 StartupInfo.dwXSize = tbl.dwXSize;
2033 StartupInfo.dwYSize = tbl.dwYSize;
2034 StartupInfo.dwXCountChars = tbl.dwXCountChars;
2035 StartupInfo.dwYCountChars = tbl.dwYCountChars;
2036 StartupInfo.dwFillAttribute = tbl.dwFillAttribute;
2037 StartupInfo.wShowWindow = tbl.wShowWindow;
2038 StartupInfo.hStdInput = tbl.childStdIn;
2039 StartupInfo.hStdOutput = tbl.childStdOut;
2040 StartupInfo.hStdError = tbl.childStdErr;
2041 if (StartupInfo.hStdInput == INVALID_HANDLE_VALUE &&
2042 StartupInfo.hStdOutput == INVALID_HANDLE_VALUE &&
2043 StartupInfo.hStdError == INVALID_HANDLE_VALUE)
2045 create |= CREATE_NEW_CONSOLE;
2048 StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
2050 if (w32_use_showwindow) {
2051 StartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
2052 StartupInfo.wShowWindow = w32_showwindow;
2055 DEBUG_p(PerlIO_printf(Perl_debug_log, "Spawning [%s] with [%s]\n",
2058 if (!CreateProcess(cname, /* search PATH to find executable */
2059 cmd, /* executable, and its arguments */
2060 NULL, /* process attributes */
2061 NULL, /* thread attributes */
2062 TRUE, /* inherit handles */
2063 create, /* creation flags */
2064 (LPVOID)env, /* inherit environment */
2065 dir, /* inherit cwd */
2067 &ProcessInformation))
2069 /* initial NULL argument to CreateProcess() does a PATH
2070 * search, but it always first looks in the directory
2071 * where the current process was started, which behavior
2072 * is undesirable for backward compatibility. So we
2073 * jump through our own hoops by picking out the path
2074 * we really want it to use. */
2076 fullcmd = qualified_path(cname);
2078 if (cname != cmdname)
2081 DEBUG_p(PerlIO_printf(Perl_debug_log,
2082 "Retrying [%s] with same args\n",
2092 if (mode == P_NOWAIT) {
2093 /* asynchronous spawn -- store handle, return PID */
2094 ret = (int)ProcessInformation.dwProcessId;
2095 if (IsWin95() && ret < 0)
2098 w32_child_handles[w32_num_children] = ProcessInformation.hProcess;
2099 w32_child_pids[w32_num_children] = (DWORD)ret;
2104 win32_msgwait(aTHX_ 1, &ProcessInformation.hProcess, INFINITE, NULL);
2105 /* FIXME: if msgwait returned due to message perhaps forward the
2106 "signal" to the process
2108 GetExitCodeProcess(ProcessInformation.hProcess, &status);
2110 CloseHandle(ProcessInformation.hProcess);
2113 CloseHandle(ProcessInformation.hThread);
2116 PerlEnv_free_childenv(env);
2117 PerlEnv_free_childdir(dir);
2119 if (cname != cmdname)
2126 win32_execv(const char *cmdname, const char *const *argv)
2129 Perl_croak(aTHX_ PL_no_func, "execv");
2134 win32_execvp(const char *cmdname, const char *const *argv)
2137 Perl_croak(aTHX_ PL_no_func, "execvp");
2142 win32_perror(const char *str)
2148 win32_setbuf(FILE *pf, char *buf)
2151 Perl_croak(aTHX_ PL_no_func, "setbuf");
2155 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
2157 return setvbuf(pf, buf, type, size);
2161 win32_flushall(void)
2167 win32_fcloseall(void)
2173 win32_fgets(char *s, int n, FILE *pf)
2175 return fgets(s, n, pf);
2185 win32_fgetc(FILE *pf)
2191 win32_putc(int c, FILE *pf)
2197 win32_puts(const char *s)
2209 win32_putchar(int c)
2216 #ifndef USE_PERL_SBRK
2218 static char *committed = NULL;
2219 static char *base = NULL;
2220 static char *reserved = NULL;
2221 static char *brk = NULL;
2222 static DWORD pagesize = 0;
2223 static DWORD allocsize = 0;
2231 GetSystemInfo(&info);
2232 /* Pretend page size is larger so we don't perpetually
2233 * call the OS to commit just one page ...
2235 pagesize = info.dwPageSize << 3;
2236 allocsize = info.dwAllocationGranularity;
2238 /* This scheme fails eventually if request for contiguous
2239 * block is denied so reserve big blocks - this is only
2240 * address space not memory ...
2242 if (brk+need >= reserved)
2244 DWORD size = 64*1024*1024;
2246 if (committed && reserved && committed < reserved)
2248 /* Commit last of previous chunk cannot span allocations */
2249 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
2251 committed = reserved;
2253 /* Reserve some (more) space
2254 * Note this is a little sneaky, 1st call passes NULL as reserved
2255 * so lets system choose where we start, subsequent calls pass
2256 * the old end address so ask for a contiguous block
2258 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
2261 reserved = addr+size;
2276 if (brk > committed)
2278 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
2279 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
2294 win32_malloc(size_t size)
2296 return malloc(size);
2300 win32_calloc(size_t numitems, size_t size)
2302 return calloc(numitems,size);
2306 win32_realloc(void *block, size_t size)
2308 return realloc(block,size);
2312 win32_free(void *block)
2318 win32_open_osfhandle(intptr_t osfhandle, int flags)
2321 char fileflags=0; /* _osfile flags */
2323 Perl_croak_nocontext("win32_open_osfhandle() TBD on this platform");
2328 win32_get_osfhandle(int fd)
2331 char fileflags=0; /* _osfile flags */
2333 Perl_croak_nocontext("win32_get_osfhandle() TBD on this platform");
2338 win32_fdupopen(FILE *pf)
2343 int fileno = win32_dup(win32_fileno(pf));
2344 int fmode = palm_fgetmode(pfdup);
2346 fprintf(stderr,"DEBUG for win32_fdupopen()\n");
2348 /* open the file in the same mode */
2349 if(fmode & O_RDONLY) {
2353 else if(fmode & O_APPEND) {
2357 else if(fmode & O_RDWR) {
2363 /* it appears that the binmode is attached to the
2364 * file descriptor so binmode files will be handled
2367 pfdup = win32_fdopen(fileno, mode);
2369 /* move the file pointer to the same position */
2370 if (!fgetpos(pf, &pos)) {
2371 fsetpos(pfdup, &pos);
2377 win32_dynaload(const char* filename)
2382 hModule = XCELoadLibraryA(filename);
2387 /* this is needed by Cwd.pm... */
2394 SV *sv = sv_newmortal();
2396 xcegetcwd(buf, sizeof(buf));
2398 sv_setpv(sv, xcestrdup(buf));
2402 #ifndef INCOMPLETE_TAINTS
2403 SvTAINTED_on(ST(0));
2414 Perl_croak(aTHX_ "usage: Win32::SetCwd($cwd)");
2416 if (!xcechdir(SvPV_nolen(ST(0))))
2423 XS(w32_GetTickCount)
2426 DWORD msec = GetTickCount();
2434 XS(w32_GetOSVersion)
2437 OSVERSIONINFOA osver;
2439 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
2440 if (!XCEGetVersionExA(&osver)) {
2443 XPUSHs(newSVpvn(osver.szCSDVersion, strlen(osver.szCSDVersion)));
2444 XPUSHs(newSViv(osver.dwMajorVersion));
2445 XPUSHs(newSViv(osver.dwMinorVersion));
2446 XPUSHs(newSViv(osver.dwBuildNumber));
2448 XPUSHs(newSViv(osver.dwPlatformId));
2457 XSRETURN_IV(IsWinNT());
2465 XSRETURN_IV(IsWin95());
2473 XSRETURN_IV(IsWinCE());
2483 if(SystemParametersInfoW(SPI_GETOEMINFO, sizeof(wbuf), wbuf, FALSE))
2484 WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buf, sizeof(buf), 0, 0);
2486 sprintf(buf, "SystemParametersInfo failed: %d", GetLastError());
2497 Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)");
2508 Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)");
2511 char szSourceFile[MAX_PATH+1];
2512 strcpy(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(0))));
2513 bResult = XCECopyFileA(szSourceFile, SvPV_nolen(ST(1)),
2530 unsigned int flags = MB_OK;
2532 txt = SvPV_nolen(ST(0));
2534 if (items < 1 || items > 2)
2535 Perl_croak(aTHX_ "usage: Win32::MessageBox($txt, [$flags])");
2538 flags = SvIV(ST(1));
2540 res = XCEMessageBoxA(NULL, txt, "Perl", flags);
2546 XS(w32_GetPowerStatus)
2550 SYSTEM_POWER_STATUS_EX sps;
2552 if(GetSystemPowerStatusEx(&sps, TRUE) == FALSE)
2557 XPUSHs(newSViv(sps.ACLineStatus));
2558 XPUSHs(newSViv(sps.BatteryFlag));
2559 XPUSHs(newSViv(sps.BatteryLifePercent));
2560 XPUSHs(newSViv(sps.BatteryLifeTime));
2561 XPUSHs(newSViv(sps.BatteryFullLifeTime));
2562 XPUSHs(newSViv(sps.BackupBatteryFlag));
2563 XPUSHs(newSViv(sps.BackupBatteryLifePercent));
2564 XPUSHs(newSViv(sps.BackupBatteryLifeTime));
2565 XPUSHs(newSViv(sps.BackupBatteryFullLifeTime));
2577 SHELLEXECUTEINFO si;
2579 wchar_t wfile[MAX_PATH];
2583 Perl_croak(aTHX_ "usage: Win32::ShellEx($file, $verb)");
2585 file = SvPV_nolen(ST(0));
2586 verb = SvPV_nolen(ST(1));
2588 memset(&si, 0, sizeof(si));
2589 si.cbSize = sizeof(si);
2590 si.fMask = SEE_MASK_FLAG_NO_UI;
2592 MultiByteToWideChar(CP_ACP, 0, verb, -1,
2593 wverb, sizeof(wverb)/2);
2594 si.lpVerb = (TCHAR *)wverb;
2596 MultiByteToWideChar(CP_ACP, 0, file, -1,
2597 wfile, sizeof(wfile)/2);
2598 si.lpFile = (TCHAR *)wfile;
2600 if(ShellExecuteEx(&si) == FALSE)
2609 Perl_init_os_extras(void)
2612 char *file = __FILE__;
2615 w32_perlshell_tokens = Nullch;
2616 w32_perlshell_items = -1;
2617 w32_fdpid = newAV(); /* XX needs to be in Perl_win32_init()? */
2618 Newx(w32_children, 1, child_tab);
2619 w32_num_children = 0;
2621 newXS("Win32::GetCwd", w32_GetCwd, file);
2622 newXS("Win32::SetCwd", w32_SetCwd, file);
2623 newXS("Win32::GetTickCount", w32_GetTickCount, file);
2624 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
2626 newXS("Win32::ShellEx", w32_ShellEx, file);
2628 newXS("Win32::IsWinNT", w32_IsWinNT, file);
2629 newXS("Win32::IsWin95", w32_IsWin95, file);
2630 newXS("Win32::IsWinCE", w32_IsWinCE, file);
2631 newXS("Win32::CopyFile", w32_CopyFile, file);
2632 newXS("Win32::Sleep", w32_Sleep, file);
2633 newXS("Win32::MessageBox", w32_MessageBox, file);
2634 newXS("Win32::GetPowerStatus", w32_GetPowerStatus, file);
2635 newXS("Win32::GetOemInfo", w32_GetOemInfo, file);
2644 fgets(buf, sizeof(buf), stdin);
2648 Perl_win32_init(int *argcp, char ***argvp)
2653 if((p = xcegetenv("PERLDEBUG")) && (p[0] == 'y' || p[0] == 'Y'))
2661 Perl_win32_term(void)
2671 win32_get_child_IO(child_IO_table* ptbl)
2673 ptbl->childStdIn = GetStdHandle(STD_INPUT_HANDLE);
2674 ptbl->childStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2675 ptbl->childStdErr = GetStdHandle(STD_ERROR_HANDLE);
2678 win32_flock(int fd, int oper)
2681 Perl_croak(aTHX_ PL_no_func, "flock");
2686 win32_waitpid(int pid, int *status, int flags)
2689 Perl_croak(aTHX_ PL_no_func, "waitpid");
2694 win32_wait(int *status)
2697 Perl_croak(aTHX_ PL_no_func, "wait");
2702 wce_reopen_stdout(char *fname)
2704 if(xcefreopen(fname, "w", stdout) == NULL)
2715 printf("Hit RETURN");
2717 fgets(buf, sizeof(buf), stdin);
2721 /* //////////////////////////////////////////////////////////////////// */
2726 getcwd(char *buf, size_t size)
2728 return xcegetcwd(buf, size);
2739 win32_popenlist(const char *mode, IV narg, SV **args)
2742 Perl_croak(aTHX_ "List form of pipe open not implemented");
2747 * a popen() clone that respects PERL5SHELL
2749 * changed to return PerlIO* rather than FILE * by BKS, 11-11-2000
2753 win32_popen(const char *command, const char *mode)
2755 return _popen(command, mode);
2763 win32_pclose(PerlIO *pf)
2768 #ifdef HAVE_INTERP_INTERN
2772 win32_csighandler(int sig)
2775 dTHXa(PERL_GET_SIG_CONTEXT);
2776 Perl_warn(aTHX_ "Got signal %d",sig);
2782 Perl_sys_intern_init(pTHX)
2785 w32_perlshell_tokens = Nullch;
2786 w32_perlshell_vec = (char**)NULL;
2787 w32_perlshell_items = 0;
2788 w32_fdpid = newAV();
2789 Newx(w32_children, 1, child_tab);
2790 w32_num_children = 0;
2791 # ifdef USE_ITHREADS
2793 Newx(w32_pseudo_children, 1, child_tab);
2794 w32_num_pseudo_children = 0;
2796 w32_init_socktype = 0;
2802 Perl_sys_intern_clear(pTHX)
2804 Safefree(w32_perlshell_tokens);
2805 Safefree(w32_perlshell_vec);
2806 /* NOTE: w32_fdpid is freed by sv_clean_all() */
2807 Safefree(w32_children);
2809 KillTimer(NULL,w32_timerid);
2812 # ifdef USE_ITHREADS
2813 Safefree(w32_pseudo_children);
2817 # ifdef USE_ITHREADS
2820 Perl_sys_intern_dup(pTHX_ struct interp_intern *src, struct interp_intern *dst)
2822 dst->perlshell_tokens = Nullch;
2823 dst->perlshell_vec = (char**)NULL;
2824 dst->perlshell_items = 0;
2825 dst->fdpid = newAV();
2826 Newxz(dst->children, 1, child_tab);
2828 Newxz(dst->pseudo_children, 1, child_tab);
2829 dst->thr_intern.Winit_socktype = 0;
2831 dst->poll_count = 0;
2832 Copy(src->sigtable,dst->sigtable,SIG_SIZE,Sighandler_t);
2834 # endif /* USE_ITHREADS */
2835 #endif /* HAVE_INTERP_INTERN */
2837 // added to remove undefied symbol error in CodeWarrior compilation
2839 Perl_Ireentrant_buffer_ptr(aTHX)