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
16 /* #include "config.h" */
18 #define PERLIO_NOT_STDIO 0
19 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
38 #define EXECF_SPAWN_NOWAIT 3
40 static DWORD IdOS(void);
42 extern WIN32_IOSUBSYSTEM win32stdio;
43 static PWIN32_IOSUBSYSTEM pIOSubSystem = &win32stdio;
45 BOOL ProbeEnv = FALSE;
46 DWORD Win32System = (DWORD)-1;
47 char szShellPath[MAX_PATH+1];
48 char szPerlLibRoot[MAX_PATH+1];
49 HANDLE PerlDllHandle = INVALID_HANDLE_VALUE;
51 static int do_spawn2(char *cmd, int exectype);
55 return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
60 return (IdOS() == VER_PLATFORM_WIN32_NT);
63 DllExport PWIN32_IOSUBSYSTEM
64 SetIOSubSystem(void *p)
66 PWIN32_IOSUBSYSTEM old = pIOSubSystem;
68 PWIN32_IOSUBSYSTEM pio = (PWIN32_IOSUBSYSTEM)p;
69 if (pio->signature_begin == 12345678L
70 && pio->signature_end == 87654321L) {
75 pIOSubSystem = &win32stdio;
80 DllExport PWIN32_IOSUBSYSTEM
87 win32PerlLibPath(void)
90 GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE)
91 ? GetModuleHandle(NULL)
94 sizeof(szPerlLibRoot));
96 *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
97 if (stricmp(end-4,"\\bin") == 0)
100 return (szPerlLibRoot);
104 win32SiteLibPath(void)
106 static char szPerlSiteLib[MAX_PATH+1];
107 strcpy(szPerlSiteLib, win32PerlLibPath());
108 strcat(szPerlSiteLib, "\\site");
109 return (szPerlSiteLib);
113 HasRedirection(char *ptr)
119 * Scan string looking for redirection (< or >) or pipe
120 * characters (|) that are not in a quoted string
150 /* since the current process environment is being updated in util.c
151 * the library functions will get the correct environment
154 my_popen(char *cmd, char *mode)
157 #define fixcmd(x) { \
158 char *pspace = strchr((x),' '); \
161 while (p < pspace) { \
173 /* was #ifndef PERLDLL, but the #else stuff doesn't work on NT
177 #ifdef __BORLANDC__ /* workaround a Borland stdio bug */
178 win32_fflush(stdout);
179 win32_fflush(stderr);
181 return win32_popen(cmd, mode);
184 * There seems to be some problems for the _popen call in a DLL
185 * this trick at the moment seems to work but it is never test
190 #define EXT_C_FUNC extern "C"
192 #define EXT_C_FUNC extern
195 EXT_C_FUNC int __cdecl _set_osfhnd(int fh, long value);
196 EXT_C_FUNC void __cdecl _lock_fhandle(int);
197 EXT_C_FUNC void __cdecl _unlock_fhandle(int);
200 PerlIO *pf; /* to store the _popen return value */
201 int tm = 0; /* flag indicating tDllExport or binary mode */
202 int fhNeeded, fhInherited, fhDup;
203 int ineeded, iinherited;
205 int phdls[2]; /* I/O handles for pipe */
206 HANDLE hPIn, hPOut, hPErr,
207 hSaveStdin, hSaveStdout, hSaveStderr,
208 hPNeeded, hPInherited, hPDuped;
210 /* first check for errors in the arguments */
211 if ( (cmd == NULL) || (mode == NULL)
212 || ((*mode != 'w') && (*mode != _T('r'))) )
215 if ( *(mode + 1) == _T('t') )
217 else if ( *(mode + 1) == _T('b') )
220 tm = (*mode == 'w' ? O_BINARY : O_TEXT);
224 if (&win32stdio != pIOSubSystem)
225 return win32_popen(cmd, mode);
228 if ( _pipe( phdls, 1024, tm ) == -1 )
230 if ( win32_pipe( phdls, 1024, tm ) == -1 )
234 /* save the current situation */
235 hSaveStdin = GetStdHandle(STD_INPUT_HANDLE);
236 hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);
237 hSaveStderr = GetStdHandle(STD_ERROR_HANDLE);
239 if (*mode == _T('w')) {
241 dwDup = STD_INPUT_HANDLE;
246 dwDup = STD_OUTPUT_HANDLE;
250 fhNeeded = phdls[ineeded];
251 fhInherited = phdls[iinherited];
253 fSuccess = DuplicateHandle(GetCurrentProcess(),
254 (HANDLE) stolen_get_osfhandle(fhNeeded),
258 FALSE, /* not inherited */
259 DUPLICATE_SAME_ACCESS);
264 fhDup = stolen_open_osfhandle((long) hPNeeded, tm);
265 win32_dup2(fhDup, fhNeeded);
269 /* Close the Out pipe, child won't need it */
270 hPDuped = (HANDLE) stolen_get_osfhandle(fhNeeded);
272 _lock_fhandle(fhNeeded);
273 _set_osfhnd(fhNeeded, (long)hPNeeded); /* put in ours duplicated one */
274 _unlock_fhandle(fhNeeded);
276 CloseHandle(hPDuped); /* close the handle first */
279 if (!SetStdHandle(dwDup, (HANDLE) stolen_get_osfhandle(fhInherited)))
283 * make sure the child see the same stderr as the calling program
285 if (!SetStdHandle(STD_ERROR_HANDLE,
286 (HANDLE)stolen_get_osfhandle(win32_fileno(win32_stderr()))))
289 pf = win32_popen(cmd, mode); /* ask _popen to do the job */
291 /* restore to where we were */
292 SetStdHandle(STD_INPUT_HANDLE, hSaveStdin);
293 SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout);
294 SetStdHandle(STD_ERROR_HANDLE, hSaveStderr);
296 /* we don't need it any more, that's for the child */
297 win32_close(fhInherited);
300 /* something wrong */
301 win32_close(fhNeeded);
306 * here we steal the file handle in pf and stuff ours in
308 win32_dup2(fhNeeded, win32_fileno(pf));
309 win32_close(fhNeeded);
314 win32_close(fhNeeded);
315 win32_close(fhInherited);
324 my_pclose(PerlIO *fp)
326 return win32_pclose(fp);
332 static OSVERSIONINFO osver;
334 if (osver.dwPlatformId != Win32System) {
335 memset(&osver, 0, sizeof(OSVERSIONINFO));
336 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
337 GetVersionEx(&osver);
338 Win32System = osver.dwPlatformId;
340 return (Win32System);
347 char* defaultshell = (IsWinNT() ? "cmd.exe" : "command.com");
348 /* we don't use COMSPEC here for two reasons:
349 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
350 * uncontrolled unportability of the ensuing scripts.
351 * 2. PERL5SHELL could be set to a shell that may not be fit for
352 * interactive use (which is what most programs look in COMSPEC
355 char *usershell = getenv("PERL5SHELL");
358 strcpy(szShellPath, usershell ? usershell : defaultshell);
364 do_aspawn(void* really, void ** mark, void ** arglast)
372 SV *sv = (SV*)really;
373 SV** pSv = (SV**)mark;
375 New(1310, argv, (arglast - mark) + 4, char*);
378 cmd = SvPV(sv, length);
381 argv[index++] = cmd = GetShell();
383 argv[index++] = "/x"; /* always enable command extensions */
384 argv[index++] = "/c";
387 while(++pSv <= (SV**)arglast) {
389 strPtr = SvPV(sv, length);
390 if(strPtr != NULL && *strPtr != '\0')
391 argv[index++] = strPtr;
395 status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv);
401 warn("Can't spawn \"%s\": %s", cmd, strerror(errno));
408 do_spawn2(char *cmd, int exectype)
414 BOOL needToTry = TRUE;
417 /* save an extra exec if possible */
420 /* see if there are shell metacharacters in it */
421 if(!HasRedirection(cmd)) {
422 New(1301,argv, strlen(cmd) / 2 + 2, char*);
423 New(1302,cmd2, strlen(cmd) + 1, char);
426 for (s = cmd2; *s;) {
427 while (*s && isspace(*s))
431 while(*s && !isspace(*s))
440 status = win32_spawnvp(P_WAIT, argv[0],
441 (const char* const*)argv);
443 case EXECF_SPAWN_NOWAIT:
444 status = win32_spawnvp(P_NOWAIT, argv[0],
445 (const char* const*)argv);
448 status = win32_execvp(argv[0], (const char* const*)argv);
451 if(status != -1 || errno == 0)
463 argv[i++] = "/c"; argv[i++] = cmd; argv[i] = Nullch;
466 status = win32_spawnvp(P_WAIT, argv[0],
467 (const char* const*)argv);
469 case EXECF_SPAWN_NOWAIT:
470 status = win32_spawnvp(P_NOWAIT, argv[0],
471 (const char* const*)argv);
474 status = win32_execvp(argv[0], (const char* const*)argv);
480 warn("Can't %s \"%s\": %s",
481 (exectype == EXECF_EXEC ? "exec" : "spawn"),
482 needToTry ? shell : argv[0],
492 return do_spawn2(cmd, EXECF_SPAWN);
498 do_spawn2(cmd, EXECF_EXEC);
505 /* The idea here is to read all the directory names into a string table
506 * (separated by nulls) and when one of the other dir functions is called
507 * return the pointer to the current file name.
510 opendir(char *filename)
515 char scannamespc[PATHLEN];
516 char *scanname = scannamespc;
518 WIN32_FIND_DATA FindData;
520 /* char root[_MAX_PATH];*/
521 /* char volname[_MAX_PATH];*/
522 /* DWORD serial, maxname, flags;*/
526 /* check to see if filename is a directory */
527 if (win32_stat(filename, &sbuf) < 0 || (sbuf.st_mode & S_IFDIR) == 0) {
531 /* get the file system characteristics */
532 /* if(GetFullPathName(filename, MAX_PATH, root, &dummy)) {
533 * if(dummy = strchr(root, '\\'))
535 * if(GetVolumeInformation(root, volname, MAX_PATH, &serial,
536 * &maxname, &flags, 0, 0)) {
537 * downcase = !(flags & FS_CASE_IS_PRESERVED);
544 /* Get us a DIR structure */
545 Newz(1303, p, 1, DIR);
549 /* Create the search pattern */
550 strcpy(scanname, filename);
552 if(index("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
553 strcat(scanname, "/*");
555 strcat(scanname, "*");
557 /* do the FindFirstFile call */
558 fh = FindFirstFile(scanname, &FindData);
559 if(fh == INVALID_HANDLE_VALUE) {
563 /* now allocate the first part of the string table for
564 * the filenames that we find.
566 idx = strlen(FindData.cFileName)+1;
567 New(1304, p->start, idx, char);
568 if(p->start == NULL) {
569 CROAK("opendir: malloc failed!\n");
571 strcpy(p->start, FindData.cFileName);
577 /* loop finding all the files that match the wildcard
578 * (which should be all of them in this directory!).
579 * the variable idx should point one past the null terminator
580 * of the previous string found.
582 while (FindNextFile(fh, &FindData)) {
583 len = strlen(FindData.cFileName);
584 /* bump the string table size by enough for the
585 * new name and it's null terminator
587 Renew(p->start, idx+len+1, char);
588 if(p->start == NULL) {
589 CROAK("opendir: malloc failed!\n");
591 strcpy(&p->start[idx], FindData.cFileName);
593 * strlwr(&p->start[idx]);
605 /* Readdir just returns the current string pointer and bumps the
606 * string pointer to the nDllExport entry.
612 static int dummy = 0;
615 /* first set up the structure to return */
616 len = strlen(dirp->curr);
617 strcpy(dirp->dirstr.d_name, dirp->curr);
618 dirp->dirstr.d_namlen = len;
621 dirp->dirstr.d_ino = dummy++;
623 /* Now set up for the nDllExport call to readdir */
624 dirp->curr += len + 1;
625 if (dirp->curr >= (dirp->start + dirp->size)) {
629 return &(dirp->dirstr);
635 /* Telldir returns the current string pointer position */
639 return (long) dirp->curr;
643 /* Seekdir moves the string pointer to a previously saved position
647 seekdir(DIR *dirp, long loc)
649 dirp->curr = (char *)loc;
652 /* Rewinddir resets the string pointer to the start */
656 dirp->curr = dirp->start;
659 /* free the memory allocated by opendir */
663 Safefree(dirp->start);
676 * Just pretend that everyone is a superuser. NT will let us know if
677 * we don\'t really have permission to do something.
680 #define ROOT_UID ((uid_t)0)
681 #define ROOT_GID ((gid_t)0)
710 return (uid == ROOT_UID ? 0 : -1);
716 return (gid == ROOT_GID ? 0 : -1);
723 kill(int pid, int sig)
725 HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
727 if (hProcess == NULL) {
728 CROAK("kill process failed!\n");
731 if (!TerminateProcess(hProcess, sig))
732 CROAK("kill process failed!\n");
733 CloseHandle(hProcess);
744 ioctl(int i, unsigned int u, char *data)
746 CROAK("ioctl not implemented!\n");
752 sleep(unsigned int t)
762 myrename(char *OldFileName, char *newname)
764 if(_access(newname, 0) != -1) { /* file exists */
767 return rename(OldFileName, newname);
772 win32_stat(const char *path, struct stat *buffer)
775 const char *p = path;
776 int l = strlen(path);
780 switch(path[l - 1]) {
783 if (path[l - 2] != ':') {
784 strncpy(t, path, l - 1);
790 res = pIOSubSystem->pfnstat(p,buffer);
793 if (S_ISDIR(buffer->st_mode))
794 buffer->st_mode |= S_IWRITE | S_IEXEC;
795 else if (S_ISREG(buffer->st_mode)) {
796 if (l >= 4 && path[l-4] == '.') {
797 const char *e = path + l - 3;
798 if (strnicmp(e,"exe",3)
799 && strnicmp(e,"bat",3)
800 && strnicmp(e,"com",3)
801 && (IsWin95() || strnicmp(e,"cmd",3)))
802 buffer->st_mode &= ~S_IEXEC;
804 buffer->st_mode |= S_IEXEC;
807 buffer->st_mode &= ~S_IEXEC;
814 #ifndef USE_WIN32_RTL_ENV
817 win32_getenv(const char *name)
819 static char *curitem = Nullch;
820 static DWORD curlen = 512;
823 New(1305,curitem,curlen,char);
824 if (!(needlen = GetEnvironmentVariable(name,curitem,curlen)))
826 while (needlen > curlen) {
827 Renew(curitem,needlen,char);
829 needlen = GetEnvironmentVariable(name,curitem,curlen);
837 FileTimeToClock(PFILETIME ft)
839 __int64 qw = ft->dwHighDateTime;
841 qw |= ft->dwLowDateTime;
842 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
848 mytimes(struct tms *timebuf)
853 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
855 timebuf->tms_utime = FileTimeToClock(&user);
856 timebuf->tms_stime = FileTimeToClock(&kernel);
857 timebuf->tms_cutime = 0;
858 timebuf->tms_cstime = 0;
861 /* That failed - e.g. Win95 fallback to clock() */
863 timebuf->tms_utime = t;
864 timebuf->tms_stime = 0;
865 timebuf->tms_cutime = 0;
866 timebuf->tms_cstime = 0;
871 static UINT timerid = 0;
874 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
876 KillTimer(NULL,timerid);
883 myalarm(unsigned int sec)
886 * the 'obvious' implentation is SetTimer() with a callback
887 * which does whatever receiving SIGALRM would do
888 * we cannot use SIGALRM even via raise() as it is not
889 * one of the supported codes in <signal.h>
891 * Snag is unless something is looking at the message queue
892 * nothing happens :-(
896 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
898 croak("Cannot set timer");
904 KillTimer(NULL,timerid);
912 * redirected io subsystem for all XS modules
919 return (pIOSubSystem->pfnerrno());
925 return (pIOSubSystem->pfnenviron());
928 /* the rest are the remapped stdio routines */
932 return (pIOSubSystem->pfnstderr());
938 return (pIOSubSystem->pfnstdin());
944 return (pIOSubSystem->pfnstdout());
948 win32_ferror(FILE *fp)
950 return (pIOSubSystem->pfnferror(fp));
957 return (pIOSubSystem->pfnfeof(fp));
961 * Since the errors returned by the socket error function
962 * WSAGetLastError() are not known by the library routine strerror
963 * we have to roll our own.
966 __declspec(thread) char strerror_buffer[512];
969 win32_strerror(int e)
971 #ifndef __BORLANDC__ /* Borland intolerance */
976 if(e < 0 || e > sys_nerr) {
980 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
981 strerror_buffer, sizeof(strerror_buffer), NULL) == 0)
982 strcpy(strerror_buffer, "Unknown Error");
984 return strerror_buffer;
986 return pIOSubSystem->pfnstrerror(e);
990 win32_fprintf(FILE *fp, const char *format, ...)
993 va_start(marker, format); /* Initialize variable arguments. */
995 return (pIOSubSystem->pfnvfprintf(fp, format, marker));
999 win32_printf(const char *format, ...)
1002 va_start(marker, format); /* Initialize variable arguments. */
1004 return (pIOSubSystem->pfnvprintf(format, marker));
1008 win32_vfprintf(FILE *fp, const char *format, va_list args)
1010 return (pIOSubSystem->pfnvfprintf(fp, format, args));
1014 win32_vprintf(const char *format, va_list args)
1016 return (pIOSubSystem->pfnvprintf(format, args));
1020 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1022 return pIOSubSystem->pfnfread(buf, size, count, fp);
1026 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1028 return pIOSubSystem->pfnfwrite(buf, size, count, fp);
1032 win32_fopen(const char *filename, const char *mode)
1034 if (stricmp(filename, "/dev/null")==0)
1035 return pIOSubSystem->pfnfopen("NUL", mode);
1036 return pIOSubSystem->pfnfopen(filename, mode);
1040 win32_fdopen( int handle, const char *mode)
1042 return pIOSubSystem->pfnfdopen(handle, (char *) mode);
1046 win32_freopen( const char *path, const char *mode, FILE *stream)
1048 if (stricmp(path, "/dev/null")==0)
1049 return pIOSubSystem->pfnfreopen("NUL", mode, stream);
1050 return pIOSubSystem->pfnfreopen(path, mode, stream);
1054 win32_fclose(FILE *pf)
1056 return pIOSubSystem->pfnfclose(pf);
1060 win32_fputs(const char *s,FILE *pf)
1062 return pIOSubSystem->pfnfputs(s, pf);
1066 win32_fputc(int c,FILE *pf)
1068 return pIOSubSystem->pfnfputc(c,pf);
1072 win32_ungetc(int c,FILE *pf)
1074 return pIOSubSystem->pfnungetc(c,pf);
1078 win32_getc(FILE *pf)
1080 return pIOSubSystem->pfngetc(pf);
1084 win32_fileno(FILE *pf)
1086 return pIOSubSystem->pfnfileno(pf);
1090 win32_clearerr(FILE *pf)
1092 pIOSubSystem->pfnclearerr(pf);
1097 win32_fflush(FILE *pf)
1099 return pIOSubSystem->pfnfflush(pf);
1103 win32_ftell(FILE *pf)
1105 return pIOSubSystem->pfnftell(pf);
1109 win32_fseek(FILE *pf,long offset,int origin)
1111 return pIOSubSystem->pfnfseek(pf, offset, origin);
1115 win32_fgetpos(FILE *pf,fpos_t *p)
1117 return pIOSubSystem->pfnfgetpos(pf, p);
1121 win32_fsetpos(FILE *pf,const fpos_t *p)
1123 return pIOSubSystem->pfnfsetpos(pf, p);
1127 win32_rewind(FILE *pf)
1129 pIOSubSystem->pfnrewind(pf);
1136 return pIOSubSystem->pfntmpfile();
1142 pIOSubSystem->pfnabort();
1147 win32_fstat(int fd,struct stat *bufptr)
1149 return pIOSubSystem->pfnfstat(fd,bufptr);
1153 win32_pipe(int *pfd, unsigned int size, int mode)
1155 return pIOSubSystem->pfnpipe(pfd, size, mode);
1159 win32_popen(const char *command, const char *mode)
1161 return pIOSubSystem->pfnpopen(command, mode);
1165 win32_pclose(FILE *pf)
1167 return pIOSubSystem->pfnpclose(pf);
1171 win32_setmode(int fd, int mode)
1173 return pIOSubSystem->pfnsetmode(fd, mode);
1177 win32_lseek(int fd, long offset, int origin)
1179 return pIOSubSystem->pfnlseek(fd, offset, origin);
1185 return pIOSubSystem->pfntell(fd);
1189 win32_open(const char *path, int flag, ...)
1195 pmode = va_arg(ap, int);
1198 if (stricmp(path, "/dev/null")==0)
1199 return pIOSubSystem->pfnopen("NUL", flag, pmode);
1200 return pIOSubSystem->pfnopen(path,flag,pmode);
1206 return pIOSubSystem->pfnclose(fd);
1212 return pIOSubSystem->pfneof(fd);
1218 return pIOSubSystem->pfndup(fd);
1222 win32_dup2(int fd1,int fd2)
1224 return pIOSubSystem->pfndup2(fd1,fd2);
1228 win32_read(int fd, void *buf, unsigned int cnt)
1230 return pIOSubSystem->pfnread(fd, buf, cnt);
1234 win32_write(int fd, const void *buf, unsigned int cnt)
1236 return pIOSubSystem->pfnwrite(fd, buf, cnt);
1240 win32_mkdir(const char *dir, int mode)
1242 return pIOSubSystem->pfnmkdir(dir); /* just ignore mode */
1246 win32_rmdir(const char *dir)
1248 return pIOSubSystem->pfnrmdir(dir);
1252 win32_chdir(const char *dir)
1254 return pIOSubSystem->pfnchdir(dir);
1258 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1260 return pIOSubSystem->pfnspawnvp(mode, cmdname, (char * const *) argv);
1264 win32_execvp(const char *cmdname, const char *const *argv)
1266 return pIOSubSystem->pfnexecvp(cmdname, (char *const *)argv);
1270 win32_perror(const char *str)
1272 pIOSubSystem->pfnperror(str);
1276 win32_setbuf(FILE *pf, char *buf)
1278 pIOSubSystem->pfnsetbuf(pf, buf);
1282 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
1284 return pIOSubSystem->pfnsetvbuf(pf, buf, type, size);
1288 win32_flushall(void)
1290 return pIOSubSystem->pfnflushall();
1294 win32_fcloseall(void)
1296 return pIOSubSystem->pfnfcloseall();
1300 win32_fgets(char *s, int n, FILE *pf)
1302 return pIOSubSystem->pfnfgets(s, n, pf);
1308 return pIOSubSystem->pfngets(s);
1312 win32_fgetc(FILE *pf)
1314 return pIOSubSystem->pfnfgetc(pf);
1318 win32_putc(int c, FILE *pf)
1320 return pIOSubSystem->pfnputc(c,pf);
1324 win32_puts(const char *s)
1326 return pIOSubSystem->pfnputs(s);
1332 return pIOSubSystem->pfngetchar();
1336 win32_putchar(int c)
1338 return pIOSubSystem->pfnputchar(c);
1342 win32_malloc(size_t size)
1344 return pIOSubSystem->pfnmalloc(size);
1348 win32_calloc(size_t numitems, size_t size)
1350 return pIOSubSystem->pfncalloc(numitems,size);
1354 win32_realloc(void *block, size_t size)
1356 return pIOSubSystem->pfnrealloc(block,size);
1360 win32_free(void *block)
1362 pIOSubSystem->pfnfree(block);
1366 stolen_open_osfhandle(long handle, int flags)
1368 return pIOSubSystem->pfn_open_osfhandle(handle, flags);
1372 stolen_get_osfhandle(int fd)
1374 return pIOSubSystem->pfn_get_osfhandle(fd);
1382 win32_flock(int fd, int oper)
1385 croak("flock() unimplemented on this platform");
1388 return pIOSubSystem->pfnflock(fd, oper);
1395 SV *sv = sv_newmortal();
1396 /* Make one call with zero size - return value is required size */
1397 DWORD len = GetCurrentDirectory((DWORD)0,NULL);
1398 SvUPGRADE(sv,SVt_PV);
1400 SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
1403 * then it worked, set PV valid,
1404 * else leave it 'undef'
1418 croak("usage: Win32::SetCurrentDirectory($cwd)");
1419 if (SetCurrentDirectory(SvPV(ST(0),na)))
1426 XS(w32_GetNextAvailDrive)
1430 char root[] = "_:\\";
1433 if (GetDriveType(root) == 1) {
1442 XS(w32_GetLastError)
1445 XSRETURN_IV(GetLastError());
1453 DWORD size = sizeof(name);
1454 if (GetUserName(name,&size)) {
1455 /* size includes NULL */
1456 ST(0) = sv_2mortal(newSVpv(name,size-1));
1466 char name[MAX_COMPUTERNAME_LENGTH+1];
1467 DWORD size = sizeof(name);
1468 if (GetComputerName(name,&size)) {
1469 /* size does NOT include NULL :-( */
1470 ST(0) = sv_2mortal(newSVpv(name,size));
1482 DWORD size = sizeof(name);
1483 if (GetUserName(name,&size)) {
1485 DWORD sidlen = sizeof(sid);
1487 DWORD dnamelen = sizeof(dname);
1489 if (LookupAccountName(NULL, name, &sid, &sidlen,
1490 dname, &dnamelen, &snu)) {
1491 XSRETURN_PV(dname); /* all that for this */
1502 DWORD flags, filecomplen;
1503 if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
1504 &flags, fsname, sizeof(fsname))) {
1505 if (GIMME == G_ARRAY) {
1506 XPUSHs(sv_2mortal(newSVpv(fsname,0)));
1507 XPUSHs(sv_2mortal(newSViv(flags)));
1508 XPUSHs(sv_2mortal(newSViv(filecomplen)));
1512 XSRETURN_PV(fsname);
1518 XS(w32_GetOSVersion)
1521 OSVERSIONINFO osver;
1523 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1524 if (GetVersionEx(&osver)) {
1525 XPUSHs(newSVpv(osver.szCSDVersion, 0));
1526 XPUSHs(newSViv(osver.dwMajorVersion));
1527 XPUSHs(newSViv(osver.dwMinorVersion));
1528 XPUSHs(newSViv(osver.dwBuildNumber));
1529 XPUSHs(newSViv(osver.dwPlatformId));
1540 XSRETURN_IV(IsWinNT());
1547 XSRETURN_IV(IsWin95());
1551 XS(w32_FormatMessage)
1558 croak("usage: Win32::FormatMessage($errno)");
1560 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1561 &source, SvIV(ST(0)), 0,
1562 msgbuf, sizeof(msgbuf)-1, NULL))
1563 XSRETURN_PV(msgbuf);
1573 PROCESS_INFORMATION stProcInfo;
1574 STARTUPINFO stStartInfo;
1575 BOOL bSuccess = FALSE;
1578 croak("usage: Win32::Spawn($cmdName, $args, $PID)");
1580 cmd = SvPV(ST(0),na);
1581 args = SvPV(ST(1), na);
1583 memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */
1584 stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */
1585 stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */
1586 stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */
1589 cmd, /* Image path */
1590 args, /* Arguments for command line */
1591 NULL, /* Default process security */
1592 NULL, /* Default thread security */
1593 FALSE, /* Must be TRUE to use std handles */
1594 NORMAL_PRIORITY_CLASS, /* No special scheduling */
1595 NULL, /* Inherit our environment block */
1596 NULL, /* Inherit our currrent directory */
1597 &stStartInfo, /* -> Startup info */
1598 &stProcInfo)) /* <- Process info (if OK) */
1600 CloseHandle(stProcInfo.hThread);/* library source code does this. */
1601 sv_setiv(ST(2), stProcInfo.dwProcessId);
1604 XSRETURN_IV(bSuccess);
1608 XS(w32_GetTickCount)
1611 XSRETURN_IV(GetTickCount());
1615 XS(w32_GetShortPathName)
1622 croak("usage: Win32::GetShortPathName($longPathName)");
1624 shortpath = sv_mortalcopy(ST(0));
1625 SvUPGRADE(shortpath, SVt_PV);
1626 /* src == target is allowed */
1628 len = GetShortPathName(SvPVX(shortpath),
1631 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
1633 SvCUR_set(shortpath,len);
1644 char *file = __FILE__;
1647 /* XXX should be removed after checking with Nick */
1648 newXS("Win32::GetCurrentDirectory", w32_GetCwd, file);
1650 /* these names are Activeware compatible */
1651 newXS("Win32::GetCwd", w32_GetCwd, file);
1652 newXS("Win32::SetCwd", w32_SetCwd, file);
1653 newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
1654 newXS("Win32::GetLastError", w32_GetLastError, file);
1655 newXS("Win32::LoginName", w32_LoginName, file);
1656 newXS("Win32::NodeName", w32_NodeName, file);
1657 newXS("Win32::DomainName", w32_DomainName, file);
1658 newXS("Win32::FsType", w32_FsType, file);
1659 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
1660 newXS("Win32::IsWinNT", w32_IsWinNT, file);
1661 newXS("Win32::IsWin95", w32_IsWin95, file);
1662 newXS("Win32::FormatMessage", w32_FormatMessage, file);
1663 newXS("Win32::Spawn", w32_Spawn, file);
1664 newXS("Win32::GetTickCount", w32_GetTickCount, file);
1665 newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
1667 /* XXX Bloat Alert! The following Activeware preloads really
1668 * ought to be part of Win32::Sys::*, so they're not included
1671 /* LookupAccountName
1673 * InitiateSystemShutdown
1674 * AbortSystemShutdown
1675 * ExpandEnvrironmentStrings
1680 Perl_win32_init(int *argcp, char ***argvp)
1682 /* Disable floating point errors, Perl will trap the ones we
1683 * care about. VC++ RTL defaults to switching these off
1684 * already, but the Borland RTL doesn't. Since we don't
1685 * want to be at the vendor's whim on the default, we set
1686 * it explicitly here.
1688 #if !defined(_ALPHA_)
1689 _control87(MCW_EM, MCW_EM);