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)
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(pTHX_ 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(pTHX_ 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];
267 /* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */
268 sprintf(regstr, "%s-%s", xlib, pl);
269 (void)get_regstr(regstr, &sv1);
272 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */
273 sprintf(pathstr, "%s/%s/lib", libname, pl);
274 (void)get_emd_part(&sv1, NULL, pathstr, ARCHNAME, "bin", pl, NULL);
276 /* $HKCU{$xlib} || $HKLM{$xlib} . ---; */
277 (void)get_regstr(xlib, &sv2);
280 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */
281 sprintf(pathstr, "%s/lib", libname);
282 (void)get_emd_part(&sv2, NULL, pathstr, ARCHNAME, "bin", pl, NULL);
289 sv_catpvn(sv1, ";", 1);
299 win32_get_sitelib(const char *pl, STRLEN *const len)
301 return win32_get_xlib(pl, "sitelib", "site", len);
304 #ifndef PERL_VENDORLIB_NAME
305 # define PERL_VENDORLIB_NAME "vendor"
309 win32_get_vendorlib(const char *pl, STRLEN *const len)
311 return win32_get_xlib(pl, "vendorlib", PERL_VENDORLIB_NAME, len);
315 has_shell_metachars(char *ptr)
321 * Scan string looking for redirection (< or >) or pipe
322 * characters (|) that are not in a quoted string.
323 * Shell variable interpolation (%VAR%) can also happen inside strings.
355 #if !defined(PERL_IMPLICIT_SYS)
356 /* since the current process environment is being updated in util.c
357 * the library functions will get the correct environment
360 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
362 printf("popen(%s)\n", cmd);
364 Perl_croak(aTHX_ PL_no_func, "popen");
369 Perl_my_pclose(pTHX_ PerlIO *fp)
371 Perl_croak(aTHX_ PL_no_func, "pclose");
376 DllExport unsigned long
379 static OSVERSIONINFOA osver;
381 if (osver.dwPlatformId != w32_platform) {
382 memset(&osver, 0, sizeof(OSVERSIONINFOA));
383 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
384 XCEGetVersionExA(&osver);
385 w32_platform = osver.dwPlatformId;
387 return (unsigned long)w32_platform;
397 return -((int)w32_pseudo_id);
403 /* Tokenize a string. Words are null-separated, and the list
404 * ends with a doubled null. Any character (except null and
405 * including backslash) may be escaped by preceding it with a
406 * backslash (the backslash will be stripped).
407 * Returns number of words in result buffer.
410 tokenize(const char *str, char **dest, char ***destv)
412 char *retstart = NULL;
413 char **retvstart = 0;
417 int slen = strlen(str);
420 Newx(ret, slen+2, char);
421 Newx(retv, (slen+3)/2, char*);
429 if (*ret == '\\' && *str)
431 else if (*ret == ' ') {
447 retvstart[items] = NULL;
457 win32_pipe(int *pfd, unsigned int size, int mode)
460 Perl_croak(aTHX_ PL_no_func, "pipe");
465 win32_times(struct tms *timebuf)
468 Perl_croak(aTHX_ PL_no_func, "times");
473 win32_signal(int sig, Sighandler_t subcode)
475 return xcesignal(sig, subcode);
482 if (!w32_perlshell_tokens) {
483 /* we don't use COMSPEC here for two reasons:
484 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
485 * uncontrolled unportability of the ensuing scripts.
486 * 2. PERL5SHELL could be set to a shell that may not be fit for
487 * interactive use (which is what most programs look in COMSPEC
490 const char* defaultshell = (IsWinNT()
491 ? "cmd.exe /x/d/c" : "command.com /c");
492 const char *usershell = PerlEnv_getenv("PERL5SHELL");
493 w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
494 &w32_perlshell_tokens,
500 Perl_do_aspawn(pTHX_ SV *really, SV **mark, SV **sp)
502 PERL_ARGS_ASSERT_DO_ASPAWN;
504 Perl_croak(aTHX_ PL_no_func, "aspawn");
508 /* returns pointer to the next unquoted space or the end of the string */
510 find_next_space(const char *s)
512 bool in_quotes = FALSE;
514 /* ignore doubled backslashes, or backslash+quote */
515 if (*s == '\\' && (s[1] == '\\' || s[1] == '"')) {
518 /* keep track of when we're within quotes */
519 else if (*s == '"') {
521 in_quotes = !in_quotes;
523 /* break it up only at spaces that aren't in quotes */
524 else if (!in_quotes && isSPACE(*s))
534 do_spawn2(pTHX_ char *cmd, int exectype)
540 BOOL needToTry = TRUE;
543 /* Save an extra exec if possible. See if there are shell
544 * metacharacters in it */
545 if (!has_shell_metachars(cmd)) {
546 Newx(argv, strlen(cmd) / 2 + 2, char*);
547 Newx(cmd2, strlen(cmd) + 1, char);
550 for (s = cmd2; *s;) {
551 while (*s && isSPACE(*s))
555 s = find_next_space(s);
563 status = win32_spawnvp(P_WAIT, argv[0],
564 (const char* const*)argv);
566 case EXECF_SPAWN_NOWAIT:
567 status = win32_spawnvp(P_NOWAIT, argv[0],
568 (const char* const*)argv);
571 status = win32_execvp(argv[0], (const char* const*)argv);
574 if (status != -1 || errno == 0)
584 Newx(argv, w32_perlshell_items + 2, char*);
585 while (++i < w32_perlshell_items)
586 argv[i] = w32_perlshell_vec[i];
591 status = win32_spawnvp(P_WAIT, argv[0],
592 (const char* const*)argv);
594 case EXECF_SPAWN_NOWAIT:
595 status = win32_spawnvp(P_NOWAIT, argv[0],
596 (const char* const*)argv);
599 status = win32_execvp(argv[0], (const char* const*)argv);
605 if (exectype == EXECF_SPAWN_NOWAIT) {
607 PL_statusvalue = -1; /* >16bits hint for pp_system() */
611 if (ckWARN(WARN_EXEC))
612 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't %s \"%s\": %s",
613 (exectype == EXECF_EXEC ? "exec" : "spawn"),
614 cmd, strerror(errno));
619 PL_statusvalue = status;
625 Perl_do_spawn(pTHX_ char *cmd)
627 PERL_ARGS_ASSERT_DO_SPAWN;
629 return do_spawn2(aTHX_ cmd, EXECF_SPAWN);
633 Perl_do_spawn_nowait(pTHX_ char *cmd)
635 PERL_ARGS_ASSERT_DO_SPAWN_NOWAIT;
637 return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT);
641 Perl_do_exec(pTHX_ const char *cmd)
643 PERL_ARGS_ASSERT_DO_EXEC;
645 do_spawn2(aTHX_ cmd, EXECF_EXEC);
649 /* The idea here is to read all the directory names into a string table
650 * (separated by nulls) and when one of the other dir functions is called
651 * return the pointer to the current file name.
654 win32_opendir(const char *filename)
660 char scanname[MAX_PATH+3];
662 WIN32_FIND_DATAA aFindData;
663 WIN32_FIND_DATAW wFindData;
665 char buffer[MAX_PATH*2];
666 WCHAR wbuffer[MAX_PATH+1];
669 len = strlen(filename);
673 /* check to see if filename is a directory */
674 if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
677 /* Get us a DIR structure */
680 /* Create the search pattern */
681 strcpy(scanname, filename);
683 /* bare drive name means look in cwd for drive */
684 if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') {
685 scanname[len++] = '.';
686 scanname[len++] = '/';
688 else if (scanname[len-1] != '/' && scanname[len-1] != '\\') {
689 scanname[len++] = '/';
691 scanname[len++] = '*';
692 scanname[len] = '\0';
694 /* do the FindFirstFile call */
695 fh = FindFirstFile(PerlDir_mapA(scanname), &aFindData);
697 if (fh == INVALID_HANDLE_VALUE) {
698 DWORD err = GetLastError();
699 /* FindFirstFile() fails on empty drives! */
701 case ERROR_FILE_NOT_FOUND:
703 case ERROR_NO_MORE_FILES:
704 case ERROR_PATH_NOT_FOUND:
707 case ERROR_NOT_ENOUGH_MEMORY:
718 /* now allocate the first part of the string table for
719 * the filenames that we find.
721 ptr = aFindData.cFileName;
727 Newx(dirp->start, dirp->size, char);
728 strcpy(dirp->start, ptr);
730 dirp->end = dirp->curr = dirp->start;
736 /* Readdir just returns the current string pointer and bumps the
737 * string pointer to the nDllExport entry.
739 DllExport struct direct *
740 win32_readdir(DIR *dirp)
745 /* first set up the structure to return */
746 len = strlen(dirp->curr);
747 strcpy(dirp->dirstr.d_name, dirp->curr);
748 dirp->dirstr.d_namlen = len;
751 dirp->dirstr.d_ino = dirp->curr - dirp->start;
753 /* Now set up for the next call to readdir */
754 dirp->curr += len + 1;
755 if (dirp->curr >= dirp->end) {
759 WIN32_FIND_DATAW wFindData;
760 WIN32_FIND_DATAA aFindData;
761 char buffer[MAX_PATH*2];
763 /* finding the next file that matches the wildcard
764 * (which should be all of them in this directory!).
766 res = FindNextFile(dirp->handle, &aFindData);
768 ptr = aFindData.cFileName;
770 long endpos = dirp->end - dirp->start;
771 long newsize = endpos + strlen(ptr) + 1;
772 /* bump the string table size by enough for the
773 * new name and its null terminator */
774 while (newsize > dirp->size) {
775 long curpos = dirp->curr - dirp->start;
777 Renew(dirp->start, dirp->size, char);
778 dirp->curr = dirp->start + curpos;
780 strcpy(dirp->start + endpos, ptr);
781 dirp->end = dirp->start + newsize;
787 return &(dirp->dirstr);
793 /* Telldir returns the current string pointer position */
795 win32_telldir(DIR *dirp)
797 return (dirp->curr - dirp->start);
801 /* Seekdir moves the string pointer to a previously saved position
802 * (returned by telldir).
805 win32_seekdir(DIR *dirp, long loc)
807 dirp->curr = dirp->start + loc;
810 /* Rewinddir resets the string pointer to the start */
812 win32_rewinddir(DIR *dirp)
814 dirp->curr = dirp->start;
817 /* free the memory allocated by opendir */
819 win32_closedir(DIR *dirp)
822 if (dirp->handle != INVALID_HANDLE_VALUE)
823 FindClose(dirp->handle);
824 Safefree(dirp->start);
830 /////!!!!!!!!!!! return here and do right stuff!!!!
833 win32_opendir(const char *filename)
835 return opendir(filename);
838 DllExport struct direct *
839 win32_readdir(DIR *dirp)
841 return readdir(dirp);
845 win32_telldir(DIR *dirp)
848 Perl_croak(aTHX_ PL_no_func, "telldir");
853 win32_seekdir(DIR *dirp, long loc)
856 Perl_croak(aTHX_ PL_no_func, "seekdir");
860 win32_rewinddir(DIR *dirp)
863 Perl_croak(aTHX_ PL_no_func, "rewinddir");
867 win32_closedir(DIR *dirp)
875 win32_kill(int pid, int sig)
878 Perl_croak(aTHX_ PL_no_func, "kill");
883 win32_stat(const char *path, struct stat *sbuf)
885 return xcestat(path, sbuf);
889 win32_longpath(char *path)
899 /* Can't use PerlIO to write as it allocates memory */
900 PerlLIO_write(PerlIO_fileno(Perl_error_log),
901 PL_no_mem, strlen(PL_no_mem));
907 /* The win32_ansipath() function takes a Unicode filename and converts it
908 * into the current Windows codepage. If some characters cannot be mapped,
909 * then it will convert the short name instead.
911 * The buffer to the ansi pathname must be freed with win32_free() when it
912 * it no longer needed.
914 * The argument to win32_ansipath() must exist before this function is
915 * called; otherwise there is no way to determine the short path name.
917 * Ideas for future refinement:
918 * - Only convert those segments of the path that are not in the current
919 * codepage, but leave the other segments in their long form.
920 * - If the resulting name is longer than MAX_PATH, start converting
921 * additional path segments into short names until the full name
922 * is shorter than MAX_PATH. Shorten the filename part last!
925 win32_ansipath(const WCHAR *widename)
928 size_t widelen = wcslen(widename)+1;
929 int len = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, widename, widelen,
930 NULL, 0, NULL, NULL);
931 name = win32_malloc(len);
935 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, widename, widelen,
936 name, len, NULL, NULL);
941 win32_getenv(const char *name)
943 return xcegetenv(name);
947 win32_putenv(const char *name)
949 return xceputenv(name);
953 filetime_to_clock(PFILETIME ft)
955 __int64 qw = ft->dwHighDateTime;
957 qw |= ft->dwLowDateTime;
958 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
962 /* fix utime() so it works on directories in NT */
964 filetime_from_time(PFILETIME pFileTime, time_t Time)
966 struct tm *pTM = localtime(&Time);
967 SYSTEMTIME SystemTime;
973 SystemTime.wYear = pTM->tm_year + 1900;
974 SystemTime.wMonth = pTM->tm_mon + 1;
975 SystemTime.wDay = pTM->tm_mday;
976 SystemTime.wHour = pTM->tm_hour;
977 SystemTime.wMinute = pTM->tm_min;
978 SystemTime.wSecond = pTM->tm_sec;
979 SystemTime.wMilliseconds = 0;
981 return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
982 LocalFileTimeToFileTime(&LocalTime, pFileTime);
986 win32_unlink(const char *filename)
988 return xceunlink(filename);
992 win32_utime(const char *filename, struct utimbuf *times)
994 return xceutime(filename, (struct _utimbuf *) times);
998 win32_gettimeofday(struct timeval *tp, void *not_used)
1000 return xcegettimeofday(tp,not_used);
1004 win32_uname(struct utsname *name)
1006 struct hostent *hep;
1007 STRLEN nodemax = sizeof(name->nodename)-1;
1008 OSVERSIONINFOA osver;
1010 memset(&osver, 0, sizeof(OSVERSIONINFOA));
1011 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
1012 if (XCEGetVersionExA(&osver)) {
1014 switch (osver.dwPlatformId) {
1015 case VER_PLATFORM_WIN32_CE:
1016 strcpy(name->sysname, "Windows CE");
1018 case VER_PLATFORM_WIN32_WINDOWS:
1019 strcpy(name->sysname, "Windows");
1021 case VER_PLATFORM_WIN32_NT:
1022 strcpy(name->sysname, "Windows NT");
1024 case VER_PLATFORM_WIN32s:
1025 strcpy(name->sysname, "Win32s");
1028 strcpy(name->sysname, "Win32 Unknown");
1033 sprintf(name->release, "%d.%d",
1034 osver.dwMajorVersion, osver.dwMinorVersion);
1037 sprintf(name->version, "Build %d",
1038 osver.dwPlatformId == VER_PLATFORM_WIN32_NT
1039 ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff));
1040 if (osver.szCSDVersion[0]) {
1041 char *buf = name->version + strlen(name->version);
1042 sprintf(buf, " (%s)", osver.szCSDVersion);
1046 *name->sysname = '\0';
1047 *name->version = '\0';
1048 *name->release = '\0';
1052 hep = win32_gethostbyname("localhost");
1054 STRLEN len = strlen(hep->h_name);
1055 if (len <= nodemax) {
1056 strcpy(name->nodename, hep->h_name);
1059 strncpy(name->nodename, hep->h_name, nodemax);
1060 name->nodename[nodemax] = '\0';
1065 if (!XCEGetComputerNameA(name->nodename, &sz))
1066 *name->nodename = '\0';
1069 /* machine (architecture) */
1073 GetSystemInfo(&info);
1075 switch (info.wProcessorArchitecture) {
1076 case PROCESSOR_ARCHITECTURE_INTEL:
1077 arch = "x86"; break;
1078 case PROCESSOR_ARCHITECTURE_MIPS:
1079 arch = "mips"; break;
1080 case PROCESSOR_ARCHITECTURE_ALPHA:
1081 arch = "alpha"; break;
1082 case PROCESSOR_ARCHITECTURE_PPC:
1083 arch = "ppc"; break;
1084 case PROCESSOR_ARCHITECTURE_ARM:
1085 arch = "arm"; break;
1086 case PROCESSOR_HITACHI_SH3:
1087 arch = "sh3"; break;
1088 case PROCESSOR_SHx_SH3:
1089 arch = "sh3"; break;
1092 arch = "unknown"; break;
1094 strcpy(name->machine, arch);
1099 /* Timing related stuff */
1102 do_raise(pTHX_ int sig)
1104 if (sig < SIG_SIZE) {
1105 Sighandler_t handler = w32_sighandler[sig];
1106 if (handler == SIG_IGN) {
1109 else if (handler != SIG_DFL) {
1114 /* Choose correct default behaviour */
1130 /* Tell caller to exit thread/process as approriate */
1135 sig_terminate(pTHX_ int sig)
1137 Perl_warn(aTHX_ "Terminating on signal SIG%s(%d)\n",PL_sig_name[sig], sig);
1138 /* exit() seems to be safe, my_exit() or die() is a problem in ^C
1145 win32_async_check(pTHX)
1149 /* Passing PeekMessage -1 as HWND (2nd arg) only get PostThreadMessage() messages
1150 * and ignores window messages - should co-exist better with windows apps e.g. Tk
1152 while (PeekMessage(&msg, (HWND)-1, 0, 0, PM_REMOVE|PM_NOYIELD)) {
1154 switch(msg.message) {
1157 /* Perhaps some other messages could map to signals ? ... */
1160 /* Treat WM_QUIT like SIGHUP? */
1166 /* We use WM_USER to fake kill() with other signals */
1170 if (do_raise(aTHX_ sig)) {
1171 sig_terminate(aTHX_ sig);
1177 /* alarm() is a one-shot but SetTimer() repeats so kill it */
1179 KillTimer(NULL,w32_timerid);
1182 /* Now fake a call to signal handler */
1183 if (do_raise(aTHX_ 14)) {
1184 sig_terminate(aTHX_ 14);
1189 /* Otherwise do normal Win32 thing - in case it is useful */
1191 TranslateMessage(&msg);
1192 DispatchMessage(&msg);
1199 /* Above or other stuff may have set a signal flag */
1200 if (PL_sig_pending) {
1206 /* This function will not return until the timeout has elapsed, or until
1207 * one of the handles is ready. */
1209 win32_msgwait(pTHX_ DWORD count, LPHANDLE handles, DWORD timeout, LPDWORD resultp)
1211 /* We may need several goes at this - so compute when we stop */
1213 if (timeout != INFINITE) {
1214 ticks = GetTickCount();
1218 DWORD result = MsgWaitForMultipleObjects(count,handles,FALSE,timeout-ticks, QS_ALLEVENTS);
1221 if (result == WAIT_TIMEOUT) {
1222 /* Ran out of time - explicit return of zero to avoid -ve if we
1223 have scheduling issues
1227 if (timeout != INFINITE) {
1228 ticks = GetTickCount();
1230 if (result == WAIT_OBJECT_0 + count) {
1231 /* Message has arrived - check it */
1232 (void)win32_async_check(aTHX);
1235 /* Not timeout or message - one of handles is ready */
1239 /* compute time left to wait */
1240 ticks = timeout - ticks;
1241 /* If we are past the end say zero */
1242 return (ticks > 0) ? ticks : 0;
1245 static UINT timerid = 0;
1247 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
1250 KillTimer(NULL,timerid);
1255 DllExport unsigned int
1256 win32_sleep(unsigned int t)
1261 DllExport unsigned int
1262 win32_alarm(unsigned int sec)
1265 * the 'obvious' implentation is SetTimer() with a callback
1266 * which does whatever receiving SIGALRM would do
1267 * we cannot use SIGALRM even via raise() as it is not
1268 * one of the supported codes in <signal.h>
1270 * Snag is unless something is looking at the message queue
1271 * nothing happens :-(
1276 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
1278 Perl_croak_nocontext("Cannot set timer");
1284 KillTimer(NULL,timerid);
1291 #ifdef HAVE_DES_FCRYPT
1292 extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf);
1296 win32_crypt(const char *txt, const char *salt)
1299 #ifdef HAVE_DES_FCRYPT
1301 return des_fcrypt(txt, salt, w32_crypt_buffer);
1303 Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia.");
1310 * redirected io subsystem for all XS modules
1323 return (&(environ));
1326 /* the rest are the remapped stdio routines */
1333 char *g_getlogin() {
1334 return "no-getlogin";
1350 win32_ferror(FILE *fp)
1352 return (ferror(fp));
1357 win32_feof(FILE *fp)
1363 * Since the errors returned by the socket error function
1364 * WSAGetLastError() are not known by the library routine strerror
1365 * we have to roll our own.
1369 win32_strerror(int e)
1371 return xcestrerror(e);
1375 win32_str_os_error(void *sv, DWORD dwErr)
1379 sv_setpvn((SV*)sv, "Error", 5);
1384 win32_fprintf(FILE *fp, const char *format, ...)
1387 va_start(marker, format); /* Initialize variable arguments. */
1389 return (vfprintf(fp, format, marker));
1393 win32_printf(const char *format, ...)
1396 va_start(marker, format); /* Initialize variable arguments. */
1398 return (vprintf(format, marker));
1402 win32_vfprintf(FILE *fp, const char *format, va_list args)
1404 return (vfprintf(fp, format, args));
1408 win32_vprintf(const char *format, va_list args)
1410 return (vprintf(format, args));
1414 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1416 return fread(buf, size, count, fp);
1420 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1422 return fwrite(buf, size, count, fp);
1426 win32_fopen(const char *filename, const char *mode)
1428 return xcefopen(filename, mode);
1432 win32_fdopen(int handle, const char *mode)
1434 return palm_fdopen(handle, mode);
1438 win32_freopen(const char *path, const char *mode, FILE *stream)
1440 return xcefreopen(path, mode, stream);
1444 win32_fclose(FILE *pf)
1446 return xcefclose(pf);
1450 win32_fputs(const char *s,FILE *pf)
1452 return fputs(s, pf);
1456 win32_fputc(int c,FILE *pf)
1462 win32_ungetc(int c,FILE *pf)
1464 return ungetc(c,pf);
1468 win32_getc(FILE *pf)
1474 win32_fileno(FILE *pf)
1476 return palm_fileno(pf);
1480 win32_clearerr(FILE *pf)
1487 win32_fflush(FILE *pf)
1493 win32_ftell(FILE *pf)
1499 win32_fseek(FILE *pf, Off_t offset,int origin)
1501 return fseek(pf, offset, origin);
1504 /* fpos_t seems to be int64 on hpc pro! Really stupid. */
1505 /* But maybe someday there will be such large disks in a hpc... */
1507 win32_fgetpos(FILE *pf, fpos_t *p)
1509 return fgetpos(pf, p);
1513 win32_fsetpos(FILE *pf, const fpos_t *p)
1515 return fsetpos(pf, p);
1519 win32_rewind(FILE *pf)
1521 fseek(pf, 0, SEEK_SET);
1529 char prefix[MAX_PATH+1];
1530 char filename[MAX_PATH+1];
1531 DWORD len = GetTempPath(MAX_PATH, prefix);
1532 if (len && len < MAX_PATH) {
1533 if (GetTempFileName(prefix, "plx", 0, filename)) {
1534 HANDLE fh = CreateFile(filename,
1535 DELETE | GENERIC_READ | GENERIC_WRITE,
1539 FILE_ATTRIBUTE_NORMAL
1540 | FILE_FLAG_DELETE_ON_CLOSE,
1542 if (fh != INVALID_HANDLE_VALUE) {
1543 int fd = win32_open_osfhandle((intptr_t)fh, 0);
1545 DEBUG_p(PerlIO_printf(Perl_debug_log,
1546 "Created tmpfile=%s\n",filename));
1558 int fd = win32_tmpfd();
1560 return win32_fdopen(fd, "w+b");
1573 win32_fstat(int fd, struct stat *sbufptr)
1575 return xcefstat(fd, sbufptr);
1579 win32_link(const char *oldname, const char *newname)
1582 Perl_croak(aTHX_ PL_no_func, "link");
1588 win32_rename(const char *oname, const char *newname)
1590 return xcerename(oname, newname);
1594 win32_setmode(int fd, int mode)
1596 /* currently 'celib' seem to have this function in src, but not
1597 * exported. When it will be, we'll uncomment following line.
1599 /* return xcesetmode(fd, mode); */
1604 win32_chsize(int fd, Off_t size)
1606 return chsize(fd, size);
1610 win32_lseek(int fd, Off_t offset, int origin)
1612 return xcelseek(fd, offset, origin);
1618 return xcelseek(fd, 0, SEEK_CUR);
1622 win32_open(const char *path, int flag, ...)
1628 pmode = va_arg(ap, int);
1631 return xceopen(path, flag, pmode);
1637 return xceclose(fd);
1644 Perl_croak(aTHX_ PL_no_func, "eof");
1651 return xcedup(fd); /* from celib/ceio.c; requires some more work on it */
1655 win32_dup2(int fd1,int fd2)
1657 return xcedup2(fd1,fd2);
1661 win32_read(int fd, void *buf, unsigned int cnt)
1663 return xceread(fd, buf, cnt);
1667 win32_write(int fd, const void *buf, unsigned int cnt)
1669 return xcewrite(fd, (void *) buf, cnt);
1673 win32_mkdir(const char *dir, int mode)
1675 return xcemkdir(dir);
1679 win32_rmdir(const char *dir)
1681 return xcermdir(dir);
1685 win32_chdir(const char *dir)
1687 return xcechdir(dir);
1691 win32_access(const char *path, int mode)
1693 return xceaccess(path, mode);
1697 win32_chmod(const char *path, int mode)
1699 return xcechmod(path, mode);
1703 create_command_line(char *cname, STRLEN clen, const char * const *args)
1710 bool bat_file = FALSE;
1711 bool cmd_shell = FALSE;
1712 bool dumb_shell = FALSE;
1713 bool extra_quotes = FALSE;
1714 bool quote_next = FALSE;
1717 cname = (char*)args[0];
1719 /* The NT cmd.exe shell has the following peculiarity that needs to be
1720 * worked around. It strips a leading and trailing dquote when any
1721 * of the following is true:
1722 * 1. the /S switch was used
1723 * 2. there are more than two dquotes
1724 * 3. there is a special character from this set: &<>()@^|
1725 * 4. no whitespace characters within the two dquotes
1726 * 5. string between two dquotes isn't an executable file
1727 * To work around this, we always add a leading and trailing dquote
1728 * to the string, if the first argument is either "cmd.exe" or "cmd",
1729 * and there were at least two or more arguments passed to cmd.exe
1730 * (not including switches).
1731 * XXX the above rules (from "cmd /?") don't seem to be applied
1732 * always, making for the convolutions below :-(
1736 clen = strlen(cname);
1739 && (stricmp(&cname[clen-4], ".bat") == 0
1740 || (IsWinNT() && stricmp(&cname[clen-4], ".cmd") == 0)))
1746 char *exe = strrchr(cname, '/');
1747 char *exe2 = strrchr(cname, '\\');
1754 if (stricmp(exe, "cmd.exe") == 0 || stricmp(exe, "cmd") == 0) {
1758 else if (stricmp(exe, "command.com") == 0
1759 || stricmp(exe, "command") == 0)
1766 DEBUG_p(PerlIO_printf(Perl_debug_log, "Args "));
1767 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1768 STRLEN curlen = strlen(arg);
1769 if (!(arg[0] == '"' && arg[curlen-1] == '"'))
1770 len += 2; /* assume quoting needed (worst case) */
1772 DEBUG_p(PerlIO_printf(Perl_debug_log, "[%s]",arg));
1774 DEBUG_p(PerlIO_printf(Perl_debug_log, "\n"));
1777 Newx(cmd, len, char);
1782 extra_quotes = TRUE;
1785 for (index = 0; (arg = (char*)args[index]) != NULL; ++index) {
1787 STRLEN curlen = strlen(arg);
1789 /* we want to protect empty arguments and ones with spaces with
1790 * dquotes, but only if they aren't already there */
1795 else if (quote_next) {
1796 /* see if it really is multiple arguments pretending to
1797 * be one and force a set of quotes around it */
1798 if (*find_next_space(arg))
1801 else if (!(arg[0] == '"' && curlen > 1 && arg[curlen-1] == '"')) {
1803 while (i < curlen) {
1804 if (isSPACE(arg[i])) {
1807 else if (arg[i] == '"') {
1831 && *arg == '/' /* see if arg is "/c", "/x/c", "/x/d/c" etc. */
1832 && stricmp(arg+curlen-2, "/c") == 0)
1834 /* is there a next argument? */
1835 if (args[index+1]) {
1836 /* are there two or more next arguments? */
1837 if (args[index+2]) {
1839 extra_quotes = TRUE;
1842 /* single argument, force quoting if it has spaces */
1858 qualified_path(const char *cmd)
1862 char *fullcmd, *curfullcmd;
1868 fullcmd = (char*)cmd;
1870 if (*fullcmd == '/' || *fullcmd == '\\')
1877 pathstr = PerlEnv_getenv("PATH");
1878 Newx(fullcmd, MAX_PATH+1, char);
1879 curfullcmd = fullcmd;
1884 /* start by appending the name to the current prefix */
1885 strcpy(curfullcmd, cmd);
1886 curfullcmd += cmdlen;
1888 /* if it doesn't end with '.', or has no extension, try adding
1889 * a trailing .exe first */
1890 if (cmd[cmdlen-1] != '.'
1891 && (cmdlen < 4 || cmd[cmdlen-4] != '.'))
1893 strcpy(curfullcmd, ".exe");
1894 res = GetFileAttributes(fullcmd);
1895 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1900 /* that failed, try the bare name */
1901 res = GetFileAttributes(fullcmd);
1902 if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY))
1905 /* quit if no other path exists, or if cmd already has path */
1906 if (!pathstr || !*pathstr || has_slash)
1909 /* skip leading semis */
1910 while (*pathstr == ';')
1913 /* build a new prefix from scratch */
1914 curfullcmd = fullcmd;
1915 while (*pathstr && *pathstr != ';') {
1916 if (*pathstr == '"') { /* foo;"baz;etc";bar */
1917 pathstr++; /* skip initial '"' */
1918 while (*pathstr && *pathstr != '"') {
1919 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1920 *curfullcmd++ = *pathstr;
1924 pathstr++; /* skip trailing '"' */
1927 if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5)
1928 *curfullcmd++ = *pathstr;
1933 pathstr++; /* skip trailing semi */
1934 if (curfullcmd > fullcmd /* append a dir separator */
1935 && curfullcmd[-1] != '/' && curfullcmd[-1] != '\\')
1937 *curfullcmd++ = '\\';
1945 /* The following are just place holders.
1946 * Some hosts may provide and environment that the OS is
1947 * not tracking, therefore, these host must provide that
1948 * environment and the current directory to CreateProcess
1952 win32_get_childenv(void)
1958 win32_free_childenv(void* d)
1963 win32_clearenv(void)
1965 char *envv = GetEnvironmentStrings();
1969 char *end = strchr(cur,'=');
1970 if (end && end != cur) {
1972 xcesetenv(cur, "", 0);
1974 cur = end + strlen(end+1)+2;
1976 else if ((len = strlen(cur)))
1979 FreeEnvironmentStrings(envv);
1983 win32_get_childdir(void)
1987 char szfilename[MAX_PATH+1];
1988 GetCurrentDirectoryA(MAX_PATH+1, szfilename);
1990 Newx(ptr, strlen(szfilename)+1, char);
1991 strcpy(ptr, szfilename);
1996 win32_free_childdir(char* d)
2002 /* XXX this needs to be made more compatible with the spawnvp()
2003 * provided by the various RTLs. In particular, searching for
2004 * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented.
2005 * This doesn't significantly affect perl itself, because we
2006 * always invoke things using PERL5SHELL if a direct attempt to
2007 * spawn the executable fails.
2009 * XXX splitting and rejoining the commandline between do_aspawn()
2010 * and win32_spawnvp() could also be avoided.
2014 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
2016 #ifdef USE_RTL_SPAWNVP
2017 return spawnvp(mode, cmdname, (char * const *)argv);
2024 STARTUPINFO StartupInfo;
2025 PROCESS_INFORMATION ProcessInformation;
2028 char *fullcmd = NULL;
2029 char *cname = (char *)cmdname;
2033 clen = strlen(cname);
2034 /* if command name contains dquotes, must remove them */
2035 if (strchr(cname, '"')) {
2037 Newx(cname,clen+1,char);
2050 cmd = create_command_line(cname, clen, argv);
2052 env = PerlEnv_get_childenv();
2053 dir = PerlEnv_get_childdir();
2056 case P_NOWAIT: /* asynch + remember result */
2057 if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
2062 /* Create a new process group so we can use GenerateConsoleCtrlEvent()
2065 /* not supported on CE create |= CREATE_NEW_PROCESS_GROUP; */
2068 case P_WAIT: /* synchronous execution */
2070 default: /* invalid mode */
2075 memset(&StartupInfo,0,sizeof(StartupInfo));
2076 StartupInfo.cb = sizeof(StartupInfo);
2077 memset(&tbl,0,sizeof(tbl));
2078 PerlEnv_get_child_IO(&tbl);
2079 StartupInfo.dwFlags = tbl.dwFlags;
2080 StartupInfo.dwX = tbl.dwX;
2081 StartupInfo.dwY = tbl.dwY;
2082 StartupInfo.dwXSize = tbl.dwXSize;
2083 StartupInfo.dwYSize = tbl.dwYSize;
2084 StartupInfo.dwXCountChars = tbl.dwXCountChars;
2085 StartupInfo.dwYCountChars = tbl.dwYCountChars;
2086 StartupInfo.dwFillAttribute = tbl.dwFillAttribute;
2087 StartupInfo.wShowWindow = tbl.wShowWindow;
2088 StartupInfo.hStdInput = tbl.childStdIn;
2089 StartupInfo.hStdOutput = tbl.childStdOut;
2090 StartupInfo.hStdError = tbl.childStdErr;
2091 if (StartupInfo.hStdInput == INVALID_HANDLE_VALUE &&
2092 StartupInfo.hStdOutput == INVALID_HANDLE_VALUE &&
2093 StartupInfo.hStdError == INVALID_HANDLE_VALUE)
2095 create |= CREATE_NEW_CONSOLE;
2098 StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
2100 if (w32_use_showwindow) {
2101 StartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
2102 StartupInfo.wShowWindow = w32_showwindow;
2105 DEBUG_p(PerlIO_printf(Perl_debug_log, "Spawning [%s] with [%s]\n",
2108 if (!CreateProcess(cname, /* search PATH to find executable */
2109 cmd, /* executable, and its arguments */
2110 NULL, /* process attributes */
2111 NULL, /* thread attributes */
2112 TRUE, /* inherit handles */
2113 create, /* creation flags */
2114 (LPVOID)env, /* inherit environment */
2115 dir, /* inherit cwd */
2117 &ProcessInformation))
2119 /* initial NULL argument to CreateProcess() does a PATH
2120 * search, but it always first looks in the directory
2121 * where the current process was started, which behavior
2122 * is undesirable for backward compatibility. So we
2123 * jump through our own hoops by picking out the path
2124 * we really want it to use. */
2126 fullcmd = qualified_path(cname);
2128 if (cname != cmdname)
2131 DEBUG_p(PerlIO_printf(Perl_debug_log,
2132 "Retrying [%s] with same args\n",
2142 if (mode == P_NOWAIT) {
2143 /* asynchronous spawn -- store handle, return PID */
2144 ret = (int)ProcessInformation.dwProcessId;
2145 if (IsWin95() && ret < 0)
2148 w32_child_handles[w32_num_children] = ProcessInformation.hProcess;
2149 w32_child_pids[w32_num_children] = (DWORD)ret;
2154 win32_msgwait(aTHX_ 1, &ProcessInformation.hProcess, INFINITE, NULL);
2155 /* FIXME: if msgwait returned due to message perhaps forward the
2156 "signal" to the process
2158 GetExitCodeProcess(ProcessInformation.hProcess, &status);
2160 CloseHandle(ProcessInformation.hProcess);
2163 CloseHandle(ProcessInformation.hThread);
2166 PerlEnv_free_childenv(env);
2167 PerlEnv_free_childdir(dir);
2169 if (cname != cmdname)
2176 win32_execv(const char *cmdname, const char *const *argv)
2179 Perl_croak(aTHX_ PL_no_func, "execv");
2184 win32_execvp(const char *cmdname, const char *const *argv)
2187 Perl_croak(aTHX_ PL_no_func, "execvp");
2192 win32_perror(const char *str)
2198 win32_setbuf(FILE *pf, char *buf)
2201 Perl_croak(aTHX_ PL_no_func, "setbuf");
2205 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
2207 return setvbuf(pf, buf, type, size);
2211 win32_flushall(void)
2217 win32_fcloseall(void)
2223 win32_fgets(char *s, int n, FILE *pf)
2225 return fgets(s, n, pf);
2235 win32_fgetc(FILE *pf)
2241 win32_putc(int c, FILE *pf)
2247 win32_puts(const char *s)
2259 win32_putchar(int c)
2266 #ifndef USE_PERL_SBRK
2268 static char *committed = NULL;
2269 static char *base = NULL;
2270 static char *reserved = NULL;
2271 static char *brk = NULL;
2272 static DWORD pagesize = 0;
2273 static DWORD allocsize = 0;
2281 GetSystemInfo(&info);
2282 /* Pretend page size is larger so we don't perpetually
2283 * call the OS to commit just one page ...
2285 pagesize = info.dwPageSize << 3;
2286 allocsize = info.dwAllocationGranularity;
2288 /* This scheme fails eventually if request for contiguous
2289 * block is denied so reserve big blocks - this is only
2290 * address space not memory ...
2292 if (brk+need >= reserved)
2294 DWORD size = 64*1024*1024;
2296 if (committed && reserved && committed < reserved)
2298 /* Commit last of previous chunk cannot span allocations */
2299 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
2301 committed = reserved;
2303 /* Reserve some (more) space
2304 * Note this is a little sneaky, 1st call passes NULL as reserved
2305 * so lets system choose where we start, subsequent calls pass
2306 * the old end address so ask for a contiguous block
2308 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
2311 reserved = addr+size;
2326 if (brk > committed)
2328 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
2329 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
2344 win32_malloc(size_t size)
2346 return malloc(size);
2350 win32_calloc(size_t numitems, size_t size)
2352 return calloc(numitems,size);
2356 win32_realloc(void *block, size_t size)
2358 return realloc(block,size);
2362 win32_free(void *block)
2368 win32_open_osfhandle(intptr_t osfhandle, int flags)
2371 char fileflags=0; /* _osfile flags */
2373 Perl_croak_nocontext("win32_open_osfhandle() TBD on this platform");
2378 win32_get_osfhandle(int fd)
2381 char fileflags=0; /* _osfile flags */
2383 Perl_croak_nocontext("win32_get_osfhandle() TBD on this platform");
2388 win32_fdupopen(FILE *pf)
2393 int fileno = win32_dup(win32_fileno(pf));
2394 int fmode = palm_fgetmode(pfdup);
2396 fprintf(stderr,"DEBUG for win32_fdupopen()\n");
2398 /* open the file in the same mode */
2399 if(fmode & O_RDONLY) {
2403 else if(fmode & O_APPEND) {
2407 else if(fmode & O_RDWR) {
2413 /* it appears that the binmode is attached to the
2414 * file descriptor so binmode files will be handled
2417 pfdup = win32_fdopen(fileno, mode);
2419 /* move the file pointer to the same position */
2420 if (!fgetpos(pf, &pos)) {
2421 fsetpos(pfdup, &pos);
2427 win32_dynaload(const char* filename)
2432 hModule = XCELoadLibraryA(filename);
2437 /* this is needed by Cwd.pm... */
2444 SV *sv = sv_newmortal();
2446 xcegetcwd(buf, sizeof(buf));
2448 sv_setpv(sv, xcestrdup(buf));
2452 SvTAINTED_on(ST(0));
2462 Perl_croak(aTHX_ "usage: Win32::SetCwd($cwd)");
2464 if (!xcechdir(SvPV_nolen(ST(0))))
2471 XS(w32_GetTickCount)
2474 DWORD msec = GetTickCount();
2482 XS(w32_GetOSVersion)
2485 OSVERSIONINFOA osver;
2487 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
2488 if (!XCEGetVersionExA(&osver)) {
2491 mXPUSHp(osver.szCSDVersion, strlen(osver.szCSDVersion));
2492 mXPUSHi(osver.dwMajorVersion);
2493 mXPUSHi(osver.dwMinorVersion);
2494 mXPUSHi(osver.dwBuildNumber);
2496 mXPUSHi(osver.dwPlatformId);
2505 XSRETURN_IV(IsWinNT());
2513 XSRETURN_IV(IsWin95());
2521 XSRETURN_IV(IsWinCE());
2531 if(SystemParametersInfoW(SPI_GETOEMINFO, sizeof(wbuf), wbuf, FALSE))
2532 WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buf, sizeof(buf), 0, 0);
2534 sprintf(buf, "SystemParametersInfo failed: %d", GetLastError());
2545 Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)");
2556 Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)");
2559 char szSourceFile[MAX_PATH+1];
2560 strcpy(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(0))));
2561 bResult = XCECopyFileA(szSourceFile, SvPV_nolen(ST(1)),
2578 unsigned int flags = MB_OK;
2580 txt = SvPV_nolen(ST(0));
2582 if (items < 1 || items > 2)
2583 Perl_croak(aTHX_ "usage: Win32::MessageBox($txt, [$flags])");
2586 flags = SvIV(ST(1));
2588 res = XCEMessageBoxA(NULL, txt, "Perl", flags);
2594 XS(w32_GetPowerStatus)
2598 SYSTEM_POWER_STATUS_EX sps;
2600 if(GetSystemPowerStatusEx(&sps, TRUE) == FALSE)
2605 mXPUSHi(sps.ACLineStatus);
2606 mXPUSHi(sps.BatteryFlag);
2607 mXPUSHi(sps.BatteryLifePercent);
2608 mXPUSHi(sps.BatteryLifeTime);
2609 mXPUSHi(sps.BatteryFullLifeTime);
2610 mXPUSHi(sps.BackupBatteryFlag);
2611 mXPUSHi(sps.BackupBatteryLifePercent);
2612 mXPUSHi(sps.BackupBatteryLifeTime);
2613 mXPUSHi(sps.BackupBatteryFullLifeTime);
2625 SHELLEXECUTEINFO si;
2627 wchar_t wfile[MAX_PATH];
2631 Perl_croak(aTHX_ "usage: Win32::ShellEx($file, $verb)");
2633 file = SvPV_nolen(ST(0));
2634 verb = SvPV_nolen(ST(1));
2636 memset(&si, 0, sizeof(si));
2637 si.cbSize = sizeof(si);
2638 si.fMask = SEE_MASK_FLAG_NO_UI;
2640 MultiByteToWideChar(CP_ACP, 0, verb, -1,
2641 wverb, sizeof(wverb)/2);
2642 si.lpVerb = (TCHAR *)wverb;
2644 MultiByteToWideChar(CP_ACP, 0, file, -1,
2645 wfile, sizeof(wfile)/2);
2646 si.lpFile = (TCHAR *)wfile;
2648 if(ShellExecuteEx(&si) == FALSE)
2657 Perl_init_os_extras(void)
2660 char *file = __FILE__;
2663 w32_perlshell_tokens = NULL;
2664 w32_perlshell_items = -1;
2665 w32_fdpid = newAV(); /* XX needs to be in Perl_win32_init()? */
2666 Newx(w32_children, 1, child_tab);
2667 w32_num_children = 0;
2669 newXS("Win32::GetCwd", w32_GetCwd, file);
2670 newXS("Win32::SetCwd", w32_SetCwd, file);
2671 newXS("Win32::GetTickCount", w32_GetTickCount, file);
2672 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
2674 newXS("Win32::ShellEx", w32_ShellEx, file);
2676 newXS("Win32::IsWinNT", w32_IsWinNT, file);
2677 newXS("Win32::IsWin95", w32_IsWin95, file);
2678 newXS("Win32::IsWinCE", w32_IsWinCE, file);
2679 newXS("Win32::CopyFile", w32_CopyFile, file);
2680 newXS("Win32::Sleep", w32_Sleep, file);
2681 newXS("Win32::MessageBox", w32_MessageBox, file);
2682 newXS("Win32::GetPowerStatus", w32_GetPowerStatus, file);
2683 newXS("Win32::GetOemInfo", w32_GetOemInfo, file);
2692 fgets(buf, sizeof(buf), stdin);
2696 Perl_win32_init(int *argcp, char ***argvp)
2701 if((p = xcegetenv("PERLDEBUG")) && (p[0] == 'y' || p[0] == 'Y'))
2709 Perl_win32_term(void)
2719 win32_get_child_IO(child_IO_table* ptbl)
2721 ptbl->childStdIn = GetStdHandle(STD_INPUT_HANDLE);
2722 ptbl->childStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2723 ptbl->childStdErr = GetStdHandle(STD_ERROR_HANDLE);
2726 win32_flock(int fd, int oper)
2729 Perl_croak(aTHX_ PL_no_func, "flock");
2734 win32_waitpid(int pid, int *status, int flags)
2737 Perl_croak(aTHX_ PL_no_func, "waitpid");
2742 win32_wait(int *status)
2745 Perl_croak(aTHX_ PL_no_func, "wait");
2750 wce_reopen_stdout(char *fname)
2752 if(xcefreopen(fname, "w", stdout) == NULL)
2763 printf("Hit RETURN");
2765 fgets(buf, sizeof(buf), stdin);
2769 /* //////////////////////////////////////////////////////////////////// */
2774 getcwd(char *buf, size_t size)
2776 return xcegetcwd(buf, size);
2781 win32_popenlist(const char *mode, IV narg, SV **args)
2784 Perl_croak(aTHX_ "List form of pipe open not implemented");
2789 * a popen() clone that respects PERL5SHELL
2791 * changed to return PerlIO* rather than FILE * by BKS, 11-11-2000
2795 win32_popen(const char *command, const char *mode)
2797 return _popen(command, mode);
2805 win32_pclose(PerlIO *pf)
2810 #ifdef HAVE_INTERP_INTERN
2814 win32_csighandler(int sig)
2817 dTHXa(PERL_GET_SIG_CONTEXT);
2818 Perl_warn(aTHX_ "Got signal %d",sig);
2824 Perl_sys_intern_init(pTHX)
2827 w32_perlshell_tokens = NULL;
2828 w32_perlshell_vec = (char**)NULL;
2829 w32_perlshell_items = 0;
2830 w32_fdpid = newAV();
2831 Newx(w32_children, 1, child_tab);
2832 w32_num_children = 0;
2833 # ifdef USE_ITHREADS
2835 Newx(w32_pseudo_children, 1, child_tab);
2836 w32_num_pseudo_children = 0;
2838 w32_init_socktype = 0;
2844 Perl_sys_intern_clear(pTHX)
2846 Safefree(w32_perlshell_tokens);
2847 Safefree(w32_perlshell_vec);
2848 /* NOTE: w32_fdpid is freed by sv_clean_all() */
2849 Safefree(w32_children);
2851 KillTimer(NULL,w32_timerid);
2854 # ifdef USE_ITHREADS
2855 Safefree(w32_pseudo_children);
2859 # ifdef USE_ITHREADS
2862 Perl_sys_intern_dup(pTHX_ struct interp_intern *src, struct interp_intern *dst)
2864 dst->perlshell_tokens = NULL;
2865 dst->perlshell_vec = (char**)NULL;
2866 dst->perlshell_items = 0;
2867 dst->fdpid = newAV();
2868 Newxz(dst->children, 1, child_tab);
2870 Newxz(dst->pseudo_children, 1, child_tab);
2871 dst->thr_intern.Winit_socktype = 0;
2873 dst->poll_count = 0;
2874 Copy(src->sigtable,dst->sigtable,SIG_SIZE,Sighandler_t);
2876 # endif /* USE_ITHREADS */
2877 #endif /* HAVE_INTERP_INTERN */
2879 // added to remove undefied symbol error in CodeWarrior compilation
2881 Perl_Ireentrant_buffer_ptr(aTHX)