3 * (c) 1995 Microsoft Corporation. All rights reserved.
4 * Developed by hip communications inc., http://info.hip.com/info/
5 * Portions (c) 1993 Intergraph Corporation. All rights reserved.
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
11 #define WIN32_LEAN_AND_MEAN
12 #define WIN32IO_IS_STDIO
22 /* ugliness to work around a buggy struct definition in lmwksta.h */
29 #endif /* __MINGW32__ */
31 /* #include "config.h" */
33 #define PERLIO_NOT_STDIO 0
34 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
43 extern CPerlObj* pPerl;
51 /* assert.h conflicts with #define of assert in perl.h */
58 #if defined(_MSC_VER) || defined(__MINGW32__)
59 #include <sys/utime.h>
65 /* Mingw32 defaults to globing command line
66 * So we turn it off like this:
73 #define EXECF_SPAWN_NOWAIT 3
75 #if defined(PERL_OBJECT)
76 #undef win32_get_privlib
77 #define win32_get_privlib g_win32_get_privlib
78 #undef win32_get_sitelib
79 #define win32_get_sitelib g_win32_get_sitelib
81 #define do_aspawn g_do_aspawn
83 #define do_spawn g_do_spawn
85 #define do_exec g_do_exec
87 #define getlogin g_getlogin
90 static DWORD os_id(void);
91 static void get_shell(void);
92 static long tokenize(char *str, char **dest, char ***destv);
93 int do_spawn2(char *cmd, int exectype);
94 static BOOL has_shell_metachars(char *ptr);
95 static long filetime_to_clock(PFILETIME ft);
96 static BOOL filetime_from_time(PFILETIME ft, time_t t);
97 static char * get_emd_part(char *leading, char *trailing, ...);
98 static void remove_dead_process(HANDLE deceased);
100 HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
101 static DWORD w32_platform = (DWORD)-1;
104 # ifdef USE_DECLSPEC_THREAD
105 __declspec(thread) char strerror_buffer[512];
106 __declspec(thread) char getlogin_buffer[128];
107 __declspec(thread) char w32_perllib_root[MAX_PATH+1];
108 # ifdef HAVE_DES_FCRYPT
109 __declspec(thread) char crypt_buffer[30];
112 # define strerror_buffer (thr->i.Wstrerror_buffer)
113 # define getlogin_buffer (thr->i.Wgetlogin_buffer)
114 # define w32_perllib_root (thr->i.Ww32_perllib_root)
115 # define crypt_buffer (thr->i.Wcrypt_buffer)
118 static char strerror_buffer[512];
119 static char getlogin_buffer[128];
120 static char w32_perllib_root[MAX_PATH+1];
121 # ifdef HAVE_DES_FCRYPT
122 static char crypt_buffer[30];
128 return (os_id() == VER_PLATFORM_WIN32_WINDOWS);
133 return (os_id() == VER_PLATFORM_WIN32_NT);
137 GetRegStrFromKey(HKEY hkey, const char *lpszValueName, char** ptr, DWORD* lpDataLen)
138 { /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
141 const char *subkey = "Software\\Perl";
144 retval = RegOpenKeyEx(hkey, subkey, 0, KEY_READ, &handle);
145 if (retval == ERROR_SUCCESS){
146 retval = RegQueryValueEx(handle, lpszValueName, 0, &type, NULL, lpDataLen);
147 if (retval == ERROR_SUCCESS && type == REG_SZ) {
149 Renew(*ptr, *lpDataLen, char);
152 New(1312, *ptr, *lpDataLen, char);
154 retval = RegQueryValueEx(handle, lpszValueName, 0, NULL, (PBYTE)*ptr, lpDataLen);
155 if (retval != ERROR_SUCCESS) {
166 GetRegStr(const char *lpszValueName, char** ptr, DWORD* lpDataLen)
168 *ptr = GetRegStrFromKey(HKEY_CURRENT_USER, lpszValueName, ptr, lpDataLen);
171 *ptr = GetRegStrFromKey(HKEY_LOCAL_MACHINE, lpszValueName, ptr, lpDataLen);
177 get_emd_part(char *prev_path, char *trailing_path, ...)
181 char mod_name[MAX_PATH+1];
185 int oldsize, newsize;
187 va_start(ap, trailing_path);
188 strip = va_arg(ap, char *);
190 sprintf(base, "%5.3f",
191 (double)PERL_REVISION + ((double)PERL_VERSION / (double)1000));
193 GetModuleFileName((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
194 ? GetModuleHandle(NULL) : w32_perldll_handle),
195 mod_name, sizeof(mod_name));
196 ptr = strrchr(mod_name, '\\');
197 while (ptr && strip) {
198 /* look for directories to skip back */
201 ptr = strrchr(mod_name, '\\');
202 if (!ptr || stricmp(ptr+1, strip) != 0) {
203 if(!(*strip == '5' && *(ptr+1) == '5' && strncmp(strip, base, 5) == 0
204 && strncmp(ptr+1, base, 5) == 0)) {
209 strip = va_arg(ap, char *);
217 strcpy(++ptr, trailing_path);
219 /* only add directory if it exists */
220 if(GetFileAttributes(mod_name) != (DWORD) -1) {
221 /* directory exists */
222 newsize = strlen(mod_name) + 1;
224 oldsize = strlen(prev_path) + 1;
225 newsize += oldsize; /* includes plus 1 for ';' */
226 Renew(prev_path, newsize, char);
227 prev_path[oldsize-1] = ';';
228 strcpy(&prev_path[oldsize], mod_name);
231 New(1311, prev_path, newsize, char);
232 strcpy(prev_path, mod_name);
240 win32_get_privlib(char *pl)
242 char *stdlib = "lib";
243 char buffer[MAX_PATH+1];
247 /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
248 sprintf(buffer, "%s-%s", stdlib, pl);
249 path = GetRegStr(buffer, &path, &datalen);
251 path = GetRegStr(stdlib, &path, &datalen);
253 /* $stdlib .= ";$EMD/../../lib" */
254 return get_emd_part(path, stdlib, ARCHNAME, "bin", Nullch);
258 win32_get_sitelib(char *pl)
260 char *sitelib = "sitelib";
262 char pathstr[MAX_PATH+1];
264 char *path1 = Nullch;
265 char *path2 = Nullch;
268 /* $HKCU{"sitelib-$]"} || $HKLM{"sitelib-$]"} . ---; */
269 sprintf(regstr, "%s-%s", sitelib, pl);
270 path1 = GetRegStr(regstr, &path1, &datalen);
273 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/$]/lib"; */
274 sprintf(pathstr, "site\\%s\\lib", pl);
275 path1 = get_emd_part(path1, pathstr, ARCHNAME, "bin", pl, Nullch);
277 /* $HKCU{'sitelib'} || $HKLM{'sitelib'} . ---; */
278 path2 = GetRegStr(sitelib, &path2, &datalen);
281 * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/lib"; */
282 path2 = get_emd_part(path2, "site\\lib", ARCHNAME, "bin", pl, Nullch);
291 newsize = len + strlen(path2) + 2; /* plus one for ';' */
293 Renew(path1, newsize, char);
295 strcpy(&path1[len], path2);
303 has_shell_metachars(char *ptr)
309 * Scan string looking for redirection (< or >) or pipe
310 * characters (|) that are not in a quoted string.
311 * Shell variable interpolation (%VAR%) can also happen inside strings.
343 #if !defined(PERL_OBJECT)
344 /* since the current process environment is being updated in util.c
345 * the library functions will get the correct environment
348 my_popen(char *cmd, char *mode)
351 #define fixcmd(x) { \
352 char *pspace = strchr((x),' '); \
355 while (p < pspace) { \
366 win32_fflush(stdout);
367 win32_fflush(stderr);
368 return win32_popen(cmd, mode);
372 my_pclose(PerlIO *fp)
374 return win32_pclose(fp);
381 static OSVERSIONINFO osver;
383 if (osver.dwPlatformId != w32_platform) {
384 memset(&osver, 0, sizeof(OSVERSIONINFO));
385 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
386 GetVersionEx(&osver);
387 w32_platform = osver.dwPlatformId;
389 return (w32_platform);
392 /* Tokenize a string. Words are null-separated, and the list
393 * ends with a doubled null. Any character (except null and
394 * including backslash) may be escaped by preceding it with a
395 * backslash (the backslash will be stripped).
396 * Returns number of words in result buffer.
399 tokenize(char *str, char **dest, char ***destv)
401 char *retstart = Nullch;
402 char **retvstart = 0;
405 int slen = strlen(str);
407 register char **retv;
408 New(1307, ret, slen+2, char);
409 New(1308, retv, (slen+3)/2, char*);
417 if (*ret == '\\' && *str)
419 else if (*ret == ' ') {
435 retvstart[items] = Nullch;
447 if (!w32_perlshell_tokens) {
448 /* we don't use COMSPEC here for two reasons:
449 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
450 * uncontrolled unportability of the ensuing scripts.
451 * 2. PERL5SHELL could be set to a shell that may not be fit for
452 * interactive use (which is what most programs look in COMSPEC
455 char* defaultshell = (IsWinNT() ? "cmd.exe /x/c" : "command.com /c");
456 char *usershell = getenv("PERL5SHELL");
457 w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
458 &w32_perlshell_tokens,
464 do_aspawn(void *vreally, void **vmark, void **vsp)
466 SV *really = (SV*)vreally;
467 SV **mark = (SV**)vmark;
479 New(1306, argv, (sp - mark) + w32_perlshell_items + 2, char*);
481 if (SvNIOKp(*(mark+1)) && !SvPOKp(*(mark+1))) {
486 while (++mark <= sp) {
487 if (*mark && (str = SvPV_nolen(*mark)))
494 status = win32_spawnvp(flag,
495 (const char*)(really ? SvPV_nolen(really) : argv[0]),
496 (const char* const*)argv);
498 if (status < 0 && (errno == ENOEXEC || errno == ENOENT)) {
499 /* possible shell-builtin, invoke with shell */
501 sh_items = w32_perlshell_items;
503 argv[index+sh_items] = argv[index];
504 while (--sh_items >= 0)
505 argv[sh_items] = w32_perlshell_vec[sh_items];
507 status = win32_spawnvp(flag,
508 (const char*)(really ? SvPV_nolen(really) : argv[0]),
509 (const char* const*)argv);
512 if (flag != P_NOWAIT) {
515 warn("Can't spawn \"%s\": %s", argv[0], strerror(errno));
520 PL_statusvalue = status;
527 do_spawn2(char *cmd, int exectype)
533 BOOL needToTry = TRUE;
536 /* Save an extra exec if possible. See if there are shell
537 * metacharacters in it */
538 if (!has_shell_metachars(cmd)) {
539 New(1301,argv, strlen(cmd) / 2 + 2, char*);
540 New(1302,cmd2, strlen(cmd) + 1, char);
543 for (s = cmd2; *s;) {
544 while (*s && isspace(*s))
548 while (*s && !isspace(*s))
557 status = win32_spawnvp(P_WAIT, argv[0],
558 (const char* const*)argv);
560 case EXECF_SPAWN_NOWAIT:
561 status = win32_spawnvp(P_NOWAIT, argv[0],
562 (const char* const*)argv);
565 status = win32_execvp(argv[0], (const char* const*)argv);
568 if (status != -1 || errno == 0)
578 New(1306, argv, w32_perlshell_items + 2, char*);
579 while (++i < w32_perlshell_items)
580 argv[i] = w32_perlshell_vec[i];
585 status = win32_spawnvp(P_WAIT, argv[0],
586 (const char* const*)argv);
588 case EXECF_SPAWN_NOWAIT:
589 status = win32_spawnvp(P_NOWAIT, argv[0],
590 (const char* const*)argv);
593 status = win32_execvp(argv[0], (const char* const*)argv);
599 if (exectype != EXECF_SPAWN_NOWAIT) {
602 warn("Can't %s \"%s\": %s",
603 (exectype == EXECF_EXEC ? "exec" : "spawn"),
604 cmd, strerror(errno));
609 PL_statusvalue = status;
617 return do_spawn2(cmd, EXECF_SPAWN);
621 do_spawn_nowait(char *cmd)
623 return do_spawn2(cmd, EXECF_SPAWN_NOWAIT);
629 do_spawn2(cmd, EXECF_EXEC);
633 /* The idea here is to read all the directory names into a string table
634 * (separated by nulls) and when one of the other dir functions is called
635 * return the pointer to the current file name.
638 win32_opendir(char *filename)
643 char scanname[MAX_PATH+3];
645 WIN32_FIND_DATA FindData;
648 len = strlen(filename);
652 /* check to see if filename is a directory */
653 if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
656 /* Get us a DIR structure */
657 Newz(1303, p, 1, DIR);
661 /* Create the search pattern */
662 strcpy(scanname, filename);
663 if (scanname[len-1] != '/' && scanname[len-1] != '\\')
664 scanname[len++] = '/';
665 scanname[len++] = '*';
666 scanname[len] = '\0';
668 /* do the FindFirstFile call */
669 fh = FindFirstFile(scanname, &FindData);
670 if (fh == INVALID_HANDLE_VALUE) {
671 /* FindFirstFile() fails on empty drives! */
672 if (GetLastError() == ERROR_FILE_NOT_FOUND)
678 /* now allocate the first part of the string table for
679 * the filenames that we find.
681 idx = strlen(FindData.cFileName)+1;
682 New(1304, p->start, idx, char);
683 if (p->start == NULL)
684 croak("opendir: malloc failed!\n");
685 strcpy(p->start, FindData.cFileName);
688 /* loop finding all the files that match the wildcard
689 * (which should be all of them in this directory!).
690 * the variable idx should point one past the null terminator
691 * of the previous string found.
693 while (FindNextFile(fh, &FindData)) {
694 len = strlen(FindData.cFileName);
695 /* bump the string table size by enough for the
696 * new name and it's null terminator
698 Renew(p->start, idx+len+1, char);
699 if (p->start == NULL)
700 croak("opendir: malloc failed!\n");
701 strcpy(&p->start[idx], FindData.cFileName);
712 /* Readdir just returns the current string pointer and bumps the
713 * string pointer to the nDllExport entry.
716 win32_readdir(DIR *dirp)
719 static int dummy = 0;
722 /* first set up the structure to return */
723 len = strlen(dirp->curr);
724 strcpy(dirp->dirstr.d_name, dirp->curr);
725 dirp->dirstr.d_namlen = len;
728 dirp->dirstr.d_ino = dummy++;
730 /* Now set up for the nDllExport call to readdir */
731 dirp->curr += len + 1;
732 if (dirp->curr >= (dirp->start + dirp->size)) {
736 return &(dirp->dirstr);
742 /* Telldir returns the current string pointer position */
744 win32_telldir(DIR *dirp)
746 return (long) dirp->curr;
750 /* Seekdir moves the string pointer to a previously saved position
754 win32_seekdir(DIR *dirp, long loc)
756 dirp->curr = (char *)loc;
759 /* Rewinddir resets the string pointer to the start */
761 win32_rewinddir(DIR *dirp)
763 dirp->curr = dirp->start;
766 /* free the memory allocated by opendir */
768 win32_closedir(DIR *dirp)
770 Safefree(dirp->start);
783 * Just pretend that everyone is a superuser. NT will let us know if
784 * we don\'t really have permission to do something.
787 #define ROOT_UID ((uid_t)0)
788 #define ROOT_GID ((gid_t)0)
817 return (auid == ROOT_UID ? 0 : -1);
823 return (agid == ROOT_GID ? 0 : -1);
830 char *buf = getlogin_buffer;
831 DWORD size = sizeof(getlogin_buffer);
832 if (GetUserName(buf,&size))
838 chown(const char *path, uid_t owner, gid_t group)
845 remove_dead_process(HANDLE deceased)
849 for (child = 0 ; child < w32_num_children ; ++child) {
850 if (w32_child_pids[child] == deceased) {
851 Copy(&w32_child_pids[child+1], &w32_child_pids[child],
852 (w32_num_children-child-1), HANDLE);
861 win32_kill(int pid, int sig)
864 HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
866 HANDLE hProcess = (HANDLE) pid;
869 if (hProcess == NULL) {
870 croak("kill process failed!\n");
873 if (!TerminateProcess(hProcess, sig))
874 croak("kill process failed!\n");
875 CloseHandle(hProcess);
877 /* WaitForMultipleObjects() on a pid that was killed returns error
878 * so if we know the pid is gone we remove it from process list */
879 remove_dead_process(hProcess);
888 DllExport unsigned int
889 win32_sleep(unsigned int t)
896 win32_stat(const char *path, struct stat *buffer)
899 const char *p = path;
900 int l = strlen(path);
904 switch(path[l - 1]) {
907 if (path[l - 2] != ':') {
908 strncpy(t, path, l - 1);
914 res = stat(p,buffer);
916 /* CRT is buggy on sharenames, so make sure it really isn't.
917 * XXX using GetFileAttributesEx() will enable us to set
918 * buffer->st_*time (but note that's not available on the
919 * Windows of 1995) */
920 DWORD r = GetFileAttributes(p);
921 if (r != 0xffffffff && (r & FILE_ATTRIBUTE_DIRECTORY)) {
922 buffer->st_mode |= S_IFDIR | S_IREAD;
924 if (!(r & FILE_ATTRIBUTE_READONLY))
925 buffer->st_mode |= S_IWRITE | S_IEXEC;
930 if (l == 3 && path[l-2] == ':'
931 && (path[l-1] == '\\' || path[l-1] == '/'))
933 /* The drive can be inaccessible, some _stat()s are buggy */
934 if (!GetVolumeInformation(path,NULL,0,NULL,NULL,NULL,NULL,0)) {
940 if (S_ISDIR(buffer->st_mode))
941 buffer->st_mode |= S_IWRITE | S_IEXEC;
942 else if (S_ISREG(buffer->st_mode)) {
943 if (l >= 4 && path[l-4] == '.') {
944 const char *e = path + l - 3;
945 if (strnicmp(e,"exe",3)
946 && strnicmp(e,"bat",3)
947 && strnicmp(e,"com",3)
948 && (IsWin95() || strnicmp(e,"cmd",3)))
949 buffer->st_mode &= ~S_IEXEC;
951 buffer->st_mode |= S_IEXEC;
954 buffer->st_mode &= ~S_IEXEC;
961 #ifndef USE_WIN32_RTL_ENV
964 win32_getenv(const char *name)
966 static char *curitem = Nullch; /* XXX threadead */
967 static DWORD curlen = 0; /* XXX threadead */
971 New(1305,curitem,curlen,char);
974 needlen = GetEnvironmentVariable(name,curitem,curlen);
976 while (needlen > curlen) {
977 Renew(curitem,needlen,char);
979 needlen = GetEnvironmentVariable(name,curitem,curlen);
983 /* allow any environment variables that begin with 'PERL'
984 to be stored in the registry */
988 if (strncmp(name, "PERL", 4) == 0) {
994 curitem = GetRegStr(name, &curitem, &curlen);
997 if (curitem && *curitem == '\0')
1004 win32_putenv(const char *name)
1010 New(1309,curitem,strlen(name)+1,char);
1011 strcpy(curitem, name);
1012 val = strchr(curitem, '=');
1014 /* The sane way to deal with the environment.
1015 * Has these advantages over putenv() & co.:
1016 * * enables us to store a truly empty value in the
1017 * environment (like in UNIX).
1018 * * we don't have to deal with RTL globals, bugs and leaks.
1020 * Why you may want to enable USE_WIN32_RTL_ENV:
1021 * * environ[] and RTL functions will not reflect changes,
1022 * which might be an issue if extensions want to access
1023 * the env. via RTL. This cuts both ways, since RTL will
1024 * not see changes made by extensions that call the Win32
1025 * functions directly, either.
1029 if(SetEnvironmentVariable(curitem, *val ? val : NULL))
1040 filetime_to_clock(PFILETIME ft)
1042 __int64 qw = ft->dwHighDateTime;
1044 qw |= ft->dwLowDateTime;
1045 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
1050 win32_times(struct tms *timebuf)
1055 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
1057 timebuf->tms_utime = filetime_to_clock(&user);
1058 timebuf->tms_stime = filetime_to_clock(&kernel);
1059 timebuf->tms_cutime = 0;
1060 timebuf->tms_cstime = 0;
1063 /* That failed - e.g. Win95 fallback to clock() */
1064 clock_t t = clock();
1065 timebuf->tms_utime = t;
1066 timebuf->tms_stime = 0;
1067 timebuf->tms_cutime = 0;
1068 timebuf->tms_cstime = 0;
1073 /* fix utime() so it works on directories in NT
1074 * thanks to Jan Dubois <jan.dubois@ibm.net>
1077 filetime_from_time(PFILETIME pFileTime, time_t Time)
1079 struct tm *pTM = gmtime(&Time);
1080 SYSTEMTIME SystemTime;
1085 SystemTime.wYear = pTM->tm_year + 1900;
1086 SystemTime.wMonth = pTM->tm_mon + 1;
1087 SystemTime.wDay = pTM->tm_mday;
1088 SystemTime.wHour = pTM->tm_hour;
1089 SystemTime.wMinute = pTM->tm_min;
1090 SystemTime.wSecond = pTM->tm_sec;
1091 SystemTime.wMilliseconds = 0;
1093 return SystemTimeToFileTime(&SystemTime, pFileTime);
1097 win32_utime(const char *filename, struct utimbuf *times)
1103 struct utimbuf TimeBuffer;
1105 int rc = utime(filename,times);
1106 /* EACCES: path specifies directory or readonly file */
1107 if (rc == 0 || errno != EACCES /* || !IsWinNT() */)
1110 if (times == NULL) {
1111 times = &TimeBuffer;
1112 time(×->actime);
1113 times->modtime = times->actime;
1116 /* This will (and should) still fail on readonly files */
1117 handle = CreateFile(filename, GENERIC_READ | GENERIC_WRITE,
1118 FILE_SHARE_READ | FILE_SHARE_DELETE, NULL,
1119 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1120 if (handle == INVALID_HANDLE_VALUE)
1123 if (GetFileTime(handle, &ftCreate, &ftAccess, &ftWrite) &&
1124 filetime_from_time(&ftAccess, times->actime) &&
1125 filetime_from_time(&ftWrite, times->modtime) &&
1126 SetFileTime(handle, &ftCreate, &ftAccess, &ftWrite))
1131 CloseHandle(handle);
1136 win32_waitpid(int pid, int *status, int flags)
1140 return win32_wait(status);
1142 rc = cwait(status, pid, WAIT_CHILD);
1143 /* cwait() returns "correctly" on Borland */
1144 #ifndef __BORLANDC__
1148 remove_dead_process((HANDLE)pid);
1150 return rc >= 0 ? pid : rc;
1154 win32_wait(int *status)
1157 return wait(status);
1159 /* XXX this wait emulation only knows about processes
1160 * spawned via win32_spawnvp(P_NOWAIT, ...).
1163 DWORD exitcode, waitcode;
1165 if (!w32_num_children) {
1170 /* if a child exists, wait for it to die */
1171 waitcode = WaitForMultipleObjects(w32_num_children,
1175 if (waitcode != WAIT_FAILED) {
1176 if (waitcode >= WAIT_ABANDONED_0
1177 && waitcode < WAIT_ABANDONED_0 + w32_num_children)
1178 i = waitcode - WAIT_ABANDONED_0;
1180 i = waitcode - WAIT_OBJECT_0;
1181 if (GetExitCodeProcess(w32_child_pids[i], &exitcode) ) {
1182 CloseHandle(w32_child_pids[i]);
1183 *status = (int)((exitcode & 0xff) << 8);
1184 retval = (int)w32_child_pids[i];
1185 Copy(&w32_child_pids[i+1], &w32_child_pids[i],
1186 (w32_num_children-i-1), HANDLE);
1193 errno = GetLastError();
1199 static UINT timerid = 0;
1201 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
1203 KillTimer(NULL,timerid);
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 :-(
1222 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
1224 croak("Cannot set timer");
1230 KillTimer(NULL,timerid);
1237 #if defined(HAVE_DES_FCRYPT) || defined(PERL_OBJECT)
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)
1245 #ifdef HAVE_DES_FCRYPT
1247 return des_fcrypt(txt, salt, crypt_buffer);
1249 die("The crypt() function is unimplemented due to excessive paranoia.");
1255 #ifdef USE_FIXED_OSFHANDLE
1257 EXTERN_C int __cdecl _alloc_osfhnd(void);
1258 EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
1259 EXTERN_C void __cdecl _lock_fhandle(int);
1260 EXTERN_C void __cdecl _unlock_fhandle(int);
1261 EXTERN_C void __cdecl _unlock(int);
1263 #if (_MSC_VER >= 1000)
1265 long osfhnd; /* underlying OS file HANDLE */
1266 char osfile; /* attributes of file (e.g., open in text mode?) */
1267 char pipech; /* one char buffer for handles opened on pipes */
1268 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
1270 CRITICAL_SECTION lock;
1271 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
1274 EXTERN_C ioinfo * __pioinfo[];
1276 #define IOINFO_L2E 5
1277 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
1278 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
1279 #define _osfile(i) (_pioinfo(i)->osfile)
1281 #else /* (_MSC_VER >= 1000) */
1282 extern char _osfile[];
1283 #endif /* (_MSC_VER >= 1000) */
1285 #define FOPEN 0x01 /* file handle open */
1286 #define FAPPEND 0x20 /* file handle opened O_APPEND */
1287 #define FDEV 0x40 /* file handle refers to device */
1288 #define FTEXT 0x80 /* file handle is in text mode */
1290 #define _STREAM_LOCKS 26 /* Table of stream locks */
1291 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */
1292 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */
1295 *int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
1298 * This function allocates a free C Runtime file handle and associates
1299 * it with the Win32 HANDLE specified by the first parameter. This is a
1300 * temperary fix for WIN95's brain damage GetFileType() error on socket
1301 * we just bypass that call for socket
1304 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
1305 * int flags - flags to associate with C Runtime file handle.
1308 * returns index of entry in fh, if successful
1309 * return -1, if no free entry is found
1313 *******************************************************************************/
1316 my_open_osfhandle(long osfhandle, int flags)
1319 char fileflags; /* _osfile flags */
1321 /* copy relevant flags from second parameter */
1324 if (flags & O_APPEND)
1325 fileflags |= FAPPEND;
1330 /* attempt to allocate a C Runtime file handle */
1331 if ((fh = _alloc_osfhnd()) == -1) {
1332 errno = EMFILE; /* too many open files */
1333 _doserrno = 0L; /* not an OS error */
1334 return -1; /* return error to caller */
1337 /* the file is open. now, set the info in _osfhnd array */
1338 _set_osfhnd(fh, osfhandle);
1340 fileflags |= FOPEN; /* mark as open */
1342 #if (_MSC_VER >= 1000)
1343 _osfile(fh) = fileflags; /* set osfile entry */
1344 _unlock_fhandle(fh);
1346 _osfile[fh] = fileflags; /* set osfile entry */
1347 _unlock(fh+_FH_LOCKS); /* unlock handle */
1350 return fh; /* return handle */
1353 #define _open_osfhandle my_open_osfhandle
1354 #endif /* USE_FIXED_OSFHANDLE */
1356 /* simulate flock by locking a range on the file */
1358 #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
1359 #define LK_LEN 0xffff0000
1362 win32_flock(int fd, int oper)
1369 croak("flock() unimplemented on this platform");
1372 fh = (HANDLE)_get_osfhandle(fd);
1373 memset(&o, 0, sizeof(o));
1376 case LOCK_SH: /* shared lock */
1377 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
1379 case LOCK_EX: /* exclusive lock */
1380 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
1382 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
1383 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
1385 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
1386 LK_ERR(LockFileEx(fh,
1387 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1388 0, LK_LEN, 0, &o),i);
1390 case LOCK_UN: /* unlock lock */
1391 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
1393 default: /* unknown */
1404 * redirected io subsystem for all XS modules
1417 return (&(_environ));
1420 /* the rest are the remapped stdio routines */
1440 win32_ferror(FILE *fp)
1442 return (ferror(fp));
1447 win32_feof(FILE *fp)
1453 * Since the errors returned by the socket error function
1454 * WSAGetLastError() are not known by the library routine strerror
1455 * we have to roll our own.
1459 win32_strerror(int e)
1461 #ifndef __BORLANDC__ /* Borland intolerance */
1462 extern int sys_nerr;
1466 if (e < 0 || e > sys_nerr) {
1471 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
1472 strerror_buffer, sizeof(strerror_buffer), NULL) == 0)
1473 strcpy(strerror_buffer, "Unknown Error");
1475 return strerror_buffer;
1481 win32_str_os_error(void *sv, DWORD dwErr)
1485 dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
1486 |FORMAT_MESSAGE_IGNORE_INSERTS
1487 |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
1488 dwErr, 0, (char *)&sMsg, 1, NULL);
1490 while (0 < dwLen && isspace(sMsg[--dwLen]))
1492 if ('.' != sMsg[dwLen])
1497 sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
1498 dwLen = sprintf(sMsg,
1499 "Unknown error #0x%lX (lookup 0x%lX)",
1500 dwErr, GetLastError());
1502 sv_setpvn((SV*)sv, sMsg, dwLen);
1508 win32_fprintf(FILE *fp, const char *format, ...)
1511 va_start(marker, format); /* Initialize variable arguments. */
1513 return (vfprintf(fp, format, marker));
1517 win32_printf(const char *format, ...)
1520 va_start(marker, format); /* Initialize variable arguments. */
1522 return (vprintf(format, marker));
1526 win32_vfprintf(FILE *fp, const char *format, va_list args)
1528 return (vfprintf(fp, format, args));
1532 win32_vprintf(const char *format, va_list args)
1534 return (vprintf(format, args));
1538 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1540 return fread(buf, size, count, fp);
1544 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1546 return fwrite(buf, size, count, fp);
1550 win32_fopen(const char *filename, const char *mode)
1552 if (stricmp(filename, "/dev/null")==0)
1553 return fopen("NUL", mode);
1554 return fopen(filename, mode);
1557 #ifndef USE_SOCKETS_AS_HANDLES
1559 #define fdopen my_fdopen
1563 win32_fdopen( int handle, const char *mode)
1565 return fdopen(handle, (char *) mode);
1569 win32_freopen( const char *path, const char *mode, FILE *stream)
1571 if (stricmp(path, "/dev/null")==0)
1572 return freopen("NUL", mode, stream);
1573 return freopen(path, mode, stream);
1577 win32_fclose(FILE *pf)
1579 return my_fclose(pf); /* defined in win32sck.c */
1583 win32_fputs(const char *s,FILE *pf)
1585 return fputs(s, pf);
1589 win32_fputc(int c,FILE *pf)
1595 win32_ungetc(int c,FILE *pf)
1597 return ungetc(c,pf);
1601 win32_getc(FILE *pf)
1607 win32_fileno(FILE *pf)
1613 win32_clearerr(FILE *pf)
1620 win32_fflush(FILE *pf)
1626 win32_ftell(FILE *pf)
1632 win32_fseek(FILE *pf,long offset,int origin)
1634 return fseek(pf, offset, origin);
1638 win32_fgetpos(FILE *pf,fpos_t *p)
1640 return fgetpos(pf, p);
1644 win32_fsetpos(FILE *pf,const fpos_t *p)
1646 return fsetpos(pf, p);
1650 win32_rewind(FILE *pf)
1670 win32_fstat(int fd,struct stat *sbufptr)
1672 return fstat(fd,sbufptr);
1676 win32_pipe(int *pfd, unsigned int size, int mode)
1678 return _pipe(pfd, size, mode);
1682 * a popen() clone that respects PERL5SHELL
1686 win32_popen(const char *command, const char *mode)
1688 #ifdef USE_RTL_POPEN
1689 return _popen(command, mode);
1697 /* establish which ends read and write */
1698 if (strchr(mode,'w')) {
1699 stdfd = 0; /* stdin */
1703 else if (strchr(mode,'r')) {
1704 stdfd = 1; /* stdout */
1711 /* set the correct mode */
1712 if (strchr(mode,'b'))
1714 else if (strchr(mode,'t'))
1717 ourmode = _fmode & (O_TEXT | O_BINARY);
1719 /* the child doesn't inherit handles */
1720 ourmode |= O_NOINHERIT;
1722 if (win32_pipe( p, 512, ourmode) == -1)
1725 /* save current stdfd */
1726 if ((oldfd = win32_dup(stdfd)) == -1)
1729 /* make stdfd go to child end of pipe (implicitly closes stdfd) */
1730 /* stdfd will be inherited by the child */
1731 if (win32_dup2(p[child], stdfd) == -1)
1734 /* close the child end in parent */
1735 win32_close(p[child]);
1737 /* start the child */
1738 if ((childpid = do_spawn_nowait((char*)command)) == -1)
1741 /* revert stdfd to whatever it was before */
1742 if (win32_dup2(oldfd, stdfd) == -1)
1745 /* close saved handle */
1748 sv_setiv(*av_fetch(w32_fdpid, p[parent], TRUE), childpid);
1750 /* we have an fd, return a file stream */
1751 return (win32_fdopen(p[parent], (char *)mode));
1754 /* we don't need to check for errors here */
1758 win32_dup2(oldfd, stdfd);
1763 #endif /* USE_RTL_POPEN */
1771 win32_pclose(FILE *pf)
1773 #ifdef USE_RTL_POPEN
1777 int childpid, status;
1780 sv = *av_fetch(w32_fdpid, win32_fileno(pf), TRUE);
1782 childpid = SvIVX(sv);
1794 remove_dead_process((HANDLE)childpid);
1796 /* wait for the child */
1797 if (cwait(&status, childpid, WAIT_CHILD) == -1)
1799 /* cwait() returns "correctly" on Borland */
1800 #ifndef __BORLANDC__
1805 #endif /* USE_RTL_POPEN */
1809 win32_rename(const char *oname, const char *newname)
1811 /* XXX despite what the documentation says about MoveFileEx(),
1812 * it doesn't work under Windows95!
1815 if (!MoveFileEx(oname,newname,
1816 MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING)) {
1817 DWORD err = GetLastError();
1819 case ERROR_BAD_NET_NAME:
1820 case ERROR_BAD_NETPATH:
1821 case ERROR_BAD_PATHNAME:
1822 case ERROR_FILE_NOT_FOUND:
1823 case ERROR_FILENAME_EXCED_RANGE:
1824 case ERROR_INVALID_DRIVE:
1825 case ERROR_NO_MORE_FILES:
1826 case ERROR_PATH_NOT_FOUND:
1839 char tmpname[MAX_PATH+1];
1840 char dname[MAX_PATH+1];
1841 char *endname = Nullch;
1843 DWORD from_attr, to_attr;
1845 /* if oname doesn't exist, do nothing */
1846 from_attr = GetFileAttributes(oname);
1847 if (from_attr == 0xFFFFFFFF) {
1852 /* if newname exists, rename it to a temporary name so that we
1853 * don't delete it in case oname happens to be the same file
1854 * (but perhaps accessed via a different path)
1856 to_attr = GetFileAttributes(newname);
1857 if (to_attr != 0xFFFFFFFF) {
1858 /* if newname is a directory, we fail
1859 * XXX could overcome this with yet more convoluted logic */
1860 if (to_attr & FILE_ATTRIBUTE_DIRECTORY) {
1864 tmplen = strlen(newname);
1865 strcpy(tmpname,newname);
1866 endname = tmpname+tmplen;
1867 for (; endname > tmpname ; --endname) {
1868 if (*endname == '/' || *endname == '\\') {
1873 if (endname > tmpname)
1874 endname = strcpy(dname,tmpname);
1878 /* get a temporary filename in same directory
1879 * XXX is this really the best we can do? */
1880 if (!GetTempFileName((LPCTSTR)endname, "plr", 0, tmpname)) {
1884 DeleteFile(tmpname);
1886 retval = rename(newname, tmpname);
1893 /* rename oname to newname */
1894 retval = rename(oname, newname);
1896 /* if we created a temporary file before ... */
1897 if (endname != Nullch) {
1898 /* ...and rename succeeded, delete temporary file/directory */
1900 DeleteFile(tmpname);
1901 /* else restore it to what it was */
1903 (void)rename(tmpname, newname);
1910 win32_setmode(int fd, int mode)
1912 return setmode(fd, mode);
1916 win32_lseek(int fd, long offset, int origin)
1918 return lseek(fd, offset, origin);
1928 win32_open(const char *path, int flag, ...)
1934 pmode = va_arg(ap, int);
1937 if (stricmp(path, "/dev/null")==0)
1938 return open("NUL", flag, pmode);
1939 return open(path,flag,pmode);
1961 win32_dup2(int fd1,int fd2)
1963 return dup2(fd1,fd2);
1967 win32_read(int fd, void *buf, unsigned int cnt)
1969 return read(fd, buf, cnt);
1973 win32_write(int fd, const void *buf, unsigned int cnt)
1975 return write(fd, buf, cnt);
1979 win32_mkdir(const char *dir, int mode)
1981 return mkdir(dir); /* just ignore mode */
1985 win32_rmdir(const char *dir)
1991 win32_chdir(const char *dir)
1997 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
2001 #ifndef USE_RTL_WAIT
2002 if (mode == P_NOWAIT && w32_num_children >= MAXIMUM_WAIT_OBJECTS)
2006 status = spawnvp(mode, cmdname, (char * const *) argv);
2007 #ifndef USE_RTL_WAIT
2008 /* XXX For the P_NOWAIT case, Borland RTL returns pinfo.dwProcessId
2009 * while VC RTL returns pinfo.hProcess. For purposes of the custom
2010 * implementation of win32_wait(), we assume the latter.
2012 if (mode == P_NOWAIT && status >= 0)
2013 w32_child_pids[w32_num_children++] = (HANDLE)status;
2019 win32_execv(const char *cmdname, const char *const *argv)
2021 return execv(cmdname, (char *const *)argv);
2025 win32_execvp(const char *cmdname, const char *const *argv)
2027 return execvp(cmdname, (char *const *)argv);
2031 win32_perror(const char *str)
2037 win32_setbuf(FILE *pf, char *buf)
2043 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
2045 return setvbuf(pf, buf, type, size);
2049 win32_flushall(void)
2055 win32_fcloseall(void)
2061 win32_fgets(char *s, int n, FILE *pf)
2063 return fgets(s, n, pf);
2073 win32_fgetc(FILE *pf)
2079 win32_putc(int c, FILE *pf)
2085 win32_puts(const char *s)
2097 win32_putchar(int c)
2104 #ifndef USE_PERL_SBRK
2106 static char *committed = NULL;
2107 static char *base = NULL;
2108 static char *reserved = NULL;
2109 static char *brk = NULL;
2110 static DWORD pagesize = 0;
2111 static DWORD allocsize = 0;
2119 GetSystemInfo(&info);
2120 /* Pretend page size is larger so we don't perpetually
2121 * call the OS to commit just one page ...
2123 pagesize = info.dwPageSize << 3;
2124 allocsize = info.dwAllocationGranularity;
2126 /* This scheme fails eventually if request for contiguous
2127 * block is denied so reserve big blocks - this is only
2128 * address space not memory ...
2130 if (brk+need >= reserved)
2132 DWORD size = 64*1024*1024;
2134 if (committed && reserved && committed < reserved)
2136 /* Commit last of previous chunk cannot span allocations */
2137 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
2139 committed = reserved;
2141 /* Reserve some (more) space
2142 * Note this is a little sneaky, 1st call passes NULL as reserved
2143 * so lets system choose where we start, subsequent calls pass
2144 * the old end address so ask for a contiguous block
2146 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
2149 reserved = addr+size;
2164 if (brk > committed)
2166 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
2167 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
2182 win32_malloc(size_t size)
2184 return malloc(size);
2188 win32_calloc(size_t numitems, size_t size)
2190 return calloc(numitems,size);
2194 win32_realloc(void *block, size_t size)
2196 return realloc(block,size);
2200 win32_free(void *block)
2207 win32_open_osfhandle(long handle, int flags)
2209 return _open_osfhandle(handle, flags);
2213 win32_get_osfhandle(int fd)
2215 return _get_osfhandle(fd);
2226 SV *sv = sv_newmortal();
2227 /* Make one call with zero size - return value is required size */
2228 DWORD len = GetCurrentDirectory((DWORD)0,NULL);
2229 SvUPGRADE(sv,SVt_PV);
2231 SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
2234 * then it worked, set PV valid,
2235 * else leave it 'undef'
2251 croak("usage: Win32::SetCurrentDirectory($cwd)");
2252 if (SetCurrentDirectory(SvPV_nolen(ST(0))))
2259 XS(w32_GetNextAvailDrive)
2263 char root[] = "_:\\";
2266 if (GetDriveType(root) == 1) {
2276 XS(w32_GetLastError)
2280 XSRETURN_IV(GetLastError());
2284 XS(w32_SetLastError)
2288 croak("usage: Win32::SetLastError($error)");
2289 SetLastError(SvIV(ST(0)));
2297 char *name = getlogin_buffer;
2298 DWORD size = sizeof(getlogin_buffer);
2299 if (GetUserName(name,&size)) {
2301 /* size includes NULL */
2302 ST(0) = sv_2mortal(newSVpv(name,size-1));
2312 char name[MAX_COMPUTERNAME_LENGTH+1];
2313 DWORD size = sizeof(name);
2314 if (GetComputerName(name,&size)) {
2316 /* size does NOT include NULL :-( */
2317 ST(0) = sv_2mortal(newSVpv(name,size));
2328 #ifndef HAS_NETWKSTAGETINFO
2329 /* mingw32 (and Win95) don't have NetWksta*(), so do it the old way */
2331 DWORD size = sizeof(name);
2332 if (GetUserName(name,&size)) {
2334 DWORD sidlen = sizeof(sid);
2336 DWORD dnamelen = sizeof(dname);
2338 if (LookupAccountName(NULL, name, (PSID)&sid, &sidlen,
2339 dname, &dnamelen, &snu)) {
2341 XSRETURN_PV(dname); /* all that for this */
2345 /* this way is more reliable, in case user has a local account.
2346 * XXX need dynamic binding of netapi32.dll symbols or this will fail on
2347 * Win95. Probably makes more sense to move it into libwin32. */
2349 DWORD dnamelen = sizeof(dname);
2350 PWKSTA_INFO_100 pwi;
2351 if (NERR_Success == NetWkstaGetInfo(NULL, 100, (LPBYTE*)&pwi)) {
2352 if (pwi->wki100_langroup && *(pwi->wki100_langroup)) {
2353 WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_langroup,
2354 -1, (LPSTR)dname, dnamelen, NULL, NULL);
2357 WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_computername,
2358 -1, (LPSTR)dname, dnamelen, NULL, NULL);
2360 NetApiBufferFree(pwi);
2373 DWORD flags, filecomplen;
2374 if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
2375 &flags, fsname, sizeof(fsname))) {
2376 if (GIMME_V == G_ARRAY) {
2377 XPUSHs(sv_2mortal(newSVpv(fsname,0)));
2378 XPUSHs(sv_2mortal(newSViv(flags)));
2379 XPUSHs(sv_2mortal(newSViv(filecomplen)));
2384 XSRETURN_PV(fsname);
2390 XS(w32_GetOSVersion)
2393 OSVERSIONINFO osver;
2395 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
2396 if (GetVersionEx(&osver)) {
2397 XPUSHs(newSVpv(osver.szCSDVersion, 0));
2398 XPUSHs(newSViv(osver.dwMajorVersion));
2399 XPUSHs(newSViv(osver.dwMinorVersion));
2400 XPUSHs(newSViv(osver.dwBuildNumber));
2401 XPUSHs(newSViv(osver.dwPlatformId));
2413 XSRETURN_IV(IsWinNT());
2421 XSRETURN_IV(IsWin95());
2425 XS(w32_FormatMessage)
2432 croak("usage: Win32::FormatMessage($errno)");
2434 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
2435 &source, SvIV(ST(0)), 0,
2436 msgbuf, sizeof(msgbuf)-1, NULL))
2437 XSRETURN_PV(msgbuf);
2447 PROCESS_INFORMATION stProcInfo;
2448 STARTUPINFO stStartInfo;
2449 BOOL bSuccess = FALSE;
2452 croak("usage: Win32::Spawn($cmdName, $args, $PID)");
2454 cmd = SvPV_nolen(ST(0));
2455 args = SvPV_nolen(ST(1));
2457 memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */
2458 stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */
2459 stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */
2460 stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */
2463 cmd, /* Image path */
2464 args, /* Arguments for command line */
2465 NULL, /* Default process security */
2466 NULL, /* Default thread security */
2467 FALSE, /* Must be TRUE to use std handles */
2468 NORMAL_PRIORITY_CLASS, /* No special scheduling */
2469 NULL, /* Inherit our environment block */
2470 NULL, /* Inherit our currrent directory */
2471 &stStartInfo, /* -> Startup info */
2472 &stProcInfo)) /* <- Process info (if OK) */
2474 CloseHandle(stProcInfo.hThread);/* library source code does this. */
2475 sv_setiv(ST(2), stProcInfo.dwProcessId);
2478 XSRETURN_IV(bSuccess);
2482 XS(w32_GetTickCount)
2486 XSRETURN_IV(GetTickCount());
2490 XS(w32_GetShortPathName)
2497 croak("usage: Win32::GetShortPathName($longPathName)");
2499 shortpath = sv_mortalcopy(ST(0));
2500 SvUPGRADE(shortpath, SVt_PV);
2501 /* src == target is allowed */
2503 len = GetShortPathName(SvPVX(shortpath),
2506 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
2508 SvCUR_set(shortpath,len);
2516 XS(w32_GetFullPathName)
2525 croak("usage: Win32::GetFullPathName($filename)");
2528 fullpath = sv_mortalcopy(filename);
2529 SvUPGRADE(fullpath, SVt_PV);
2531 len = GetFullPathName(SvPVX(filename),
2535 } while (len >= SvLEN(fullpath) && sv_grow(fullpath,len+1));
2537 if (GIMME_V == G_ARRAY) {
2539 XST_mPV(1,filepart);
2540 len = filepart - SvPVX(fullpath);
2543 SvCUR_set(fullpath,len);
2555 croak("usage: Win32::Sleep($milliseconds)");
2561 Perl_init_os_extras()
2563 char *file = __FILE__;
2566 w32_perlshell_tokens = Nullch;
2567 w32_perlshell_items = -1;
2568 w32_fdpid = newAV(); /* XXX needs to be in Perl_win32_init()? */
2569 #ifndef USE_RTL_WAIT
2570 w32_num_children = 0;
2573 /* these names are Activeware compatible */
2574 newXS("Win32::GetCwd", w32_GetCwd, file);
2575 newXS("Win32::SetCwd", w32_SetCwd, file);
2576 newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
2577 newXS("Win32::GetLastError", w32_GetLastError, file);
2578 newXS("Win32::SetLastError", w32_SetLastError, file);
2579 newXS("Win32::LoginName", w32_LoginName, file);
2580 newXS("Win32::NodeName", w32_NodeName, file);
2581 newXS("Win32::DomainName", w32_DomainName, file);
2582 newXS("Win32::FsType", w32_FsType, file);
2583 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
2584 newXS("Win32::IsWinNT", w32_IsWinNT, file);
2585 newXS("Win32::IsWin95", w32_IsWin95, file);
2586 newXS("Win32::FormatMessage", w32_FormatMessage, file);
2587 newXS("Win32::Spawn", w32_Spawn, file);
2588 newXS("Win32::GetTickCount", w32_GetTickCount, file);
2589 newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
2590 newXS("Win32::GetFullPathName", w32_GetFullPathName, file);
2591 newXS("Win32::Sleep", w32_Sleep, file);
2593 /* XXX Bloat Alert! The following Activeware preloads really
2594 * ought to be part of Win32::Sys::*, so they're not included
2597 /* LookupAccountName
2599 * InitiateSystemShutdown
2600 * AbortSystemShutdown
2601 * ExpandEnvrironmentStrings
2606 Perl_win32_init(int *argcp, char ***argvp)
2608 /* Disable floating point errors, Perl will trap the ones we
2609 * care about. VC++ RTL defaults to switching these off
2610 * already, but the Borland RTL doesn't. Since we don't
2611 * want to be at the vendor's whim on the default, we set
2612 * it explicitly here.
2614 #if !defined(_ALPHA_) && !defined(__GNUC__)
2615 _control87(MCW_EM, MCW_EM);
2620 #ifdef USE_BINMODE_SCRIPTS
2623 win32_strip_return(SV *sv)
2625 char *s = SvPVX(sv);
2626 char *e = s+SvCUR(sv);
2630 if (*s == '\r' && s[1] == '\n')
2640 SvCUR_set(sv,d-SvPVX(sv));