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
19 /* #include "config.h" */
21 #define PERLIO_NOT_STDIO 0
22 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
32 /* assert.h conflicts with #define of assert in perl.h */
40 /* Mingw32 defaults to globing command line
41 * So we turn it off like this:
48 #define EXECF_SPAWN_NOWAIT 3
50 static DWORD IdOS(void);
52 BOOL ProbeEnv = FALSE;
53 DWORD Win32System = (DWORD)-1;
54 char szShellPath[MAX_PATH+1];
55 char szPerlLibRoot[MAX_PATH+1];
56 HANDLE PerlDllHandle = INVALID_HANDLE_VALUE;
59 # ifdef USE_DECLSPEC_THREAD
60 __declspec(thread) char strerror_buffer[512];
61 # ifdef HAVE_DES_FCRYPT
62 __declspec(thread) char crypt_buffer[30];
65 # define strerror_buffer (thr->i.Wstrerror_buffer)
66 # define crypt_buffer (thr->i.Wcrypt_buffer)
69 char strerror_buffer[512];
70 # ifdef HAVE_DES_FCRYPT
71 char crypt_buffer[30];
75 static int do_spawn2(char *cmd, int exectype);
79 return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
84 return (IdOS() == VER_PLATFORM_WIN32_NT);
88 win32PerlLibPath(char *sfx,...)
93 GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE)
94 ? GetModuleHandle(NULL)
97 sizeof(szPerlLibRoot));
98 *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
99 if (stricmp(end-4,"\\bin") == 0)
106 sfx = va_arg(ap,char *);
109 return (szPerlLibRoot);
114 HasRedirection(char *ptr)
120 * Scan string looking for redirection (< or >) or pipe
121 * characters (|) that are not in a quoted string
151 /* since the current process environment is being updated in util.c
152 * the library functions will get the correct environment
155 my_popen(char *cmd, char *mode)
158 #define fixcmd(x) { \
159 char *pspace = strchr((x),' '); \
162 while (p < pspace) { \
173 #ifdef __BORLANDC__ /* workaround a Borland stdio bug */
174 win32_fflush(stdout);
175 win32_fflush(stderr);
177 return win32_popen(cmd, mode);
181 my_pclose(PerlIO *fp)
183 return win32_pclose(fp);
189 static OSVERSIONINFO osver;
191 if (osver.dwPlatformId != Win32System) {
192 memset(&osver, 0, sizeof(OSVERSIONINFO));
193 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
194 GetVersionEx(&osver);
195 Win32System = osver.dwPlatformId;
197 return (Win32System);
204 char* defaultshell = (IsWinNT() ? "cmd.exe" : "command.com");
205 /* we don't use COMSPEC here for two reasons:
206 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
207 * uncontrolled unportability of the ensuing scripts.
208 * 2. PERL5SHELL could be set to a shell that may not be fit for
209 * interactive use (which is what most programs look in COMSPEC
212 char *usershell = getenv("PERL5SHELL");
215 strcpy(szShellPath, usershell ? usershell : defaultshell);
221 do_aspawn(void* really, void ** mark, void ** arglast)
229 SV *sv = (SV*)really;
230 SV** pSv = (SV**)mark;
232 New(1310, argv, (arglast - mark) + 4, char*);
235 cmd = SvPV(sv, length);
238 argv[index++] = cmd = GetShell();
240 argv[index++] = "/x"; /* always enable command extensions */
241 argv[index++] = "/c";
244 while(++pSv <= (SV**)arglast) {
246 strPtr = SvPV(sv, length);
247 if(strPtr != NULL && *strPtr != '\0')
248 argv[index++] = strPtr;
252 status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv);
258 warn("Can't spawn \"%s\": %s", cmd, strerror(errno));
265 do_spawn2(char *cmd, int exectype)
271 BOOL needToTry = TRUE;
274 /* save an extra exec if possible */
277 /* see if there are shell metacharacters in it */
278 if(!HasRedirection(cmd)) {
279 New(1301,argv, strlen(cmd) / 2 + 2, char*);
280 New(1302,cmd2, strlen(cmd) + 1, char);
283 for (s = cmd2; *s;) {
284 while (*s && isspace(*s))
288 while(*s && !isspace(*s))
297 status = win32_spawnvp(P_WAIT, argv[0],
298 (const char* const*)argv);
300 case EXECF_SPAWN_NOWAIT:
301 status = win32_spawnvp(P_NOWAIT, argv[0],
302 (const char* const*)argv);
305 status = win32_execvp(argv[0], (const char* const*)argv);
308 if(status != -1 || errno == 0)
320 argv[i++] = "/c"; argv[i++] = cmd; argv[i] = Nullch;
323 status = win32_spawnvp(P_WAIT, argv[0],
324 (const char* const*)argv);
326 case EXECF_SPAWN_NOWAIT:
327 status = win32_spawnvp(P_NOWAIT, argv[0],
328 (const char* const*)argv);
331 status = win32_execvp(argv[0], (const char* const*)argv);
337 warn("Can't %s \"%s\": %s",
338 (exectype == EXECF_EXEC ? "exec" : "spawn"),
339 needToTry ? shell : argv[0],
349 return do_spawn2(cmd, EXECF_SPAWN);
355 do_spawn2(cmd, EXECF_EXEC);
362 /* The idea here is to read all the directory names into a string table
363 * (separated by nulls) and when one of the other dir functions is called
364 * return the pointer to the current file name.
367 opendir(char *filename)
372 char scannamespc[PATHLEN];
373 char *scanname = scannamespc;
375 WIN32_FIND_DATA FindData;
377 /* char root[_MAX_PATH];*/
378 /* char volname[_MAX_PATH];*/
379 /* DWORD serial, maxname, flags;*/
383 /* check to see if filename is a directory */
384 if (win32_stat(filename, &sbuf) < 0 || (sbuf.st_mode & S_IFDIR) == 0) {
388 /* get the file system characteristics */
389 /* if(GetFullPathName(filename, MAX_PATH, root, &dummy)) {
390 * if(dummy = strchr(root, '\\'))
392 * if(GetVolumeInformation(root, volname, MAX_PATH, &serial,
393 * &maxname, &flags, 0, 0)) {
394 * downcase = !(flags & FS_CASE_IS_PRESERVED);
401 /* Get us a DIR structure */
402 Newz(1303, p, 1, DIR);
406 /* Create the search pattern */
407 strcpy(scanname, filename);
409 if(index("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
410 strcat(scanname, "/*");
412 strcat(scanname, "*");
414 /* do the FindFirstFile call */
415 fh = FindFirstFile(scanname, &FindData);
416 if(fh == INVALID_HANDLE_VALUE) {
420 /* now allocate the first part of the string table for
421 * the filenames that we find.
423 idx = strlen(FindData.cFileName)+1;
424 New(1304, p->start, idx, char);
425 if(p->start == NULL) {
426 croak("opendir: malloc failed!\n");
428 strcpy(p->start, FindData.cFileName);
434 /* loop finding all the files that match the wildcard
435 * (which should be all of them in this directory!).
436 * the variable idx should point one past the null terminator
437 * of the previous string found.
439 while (FindNextFile(fh, &FindData)) {
440 len = strlen(FindData.cFileName);
441 /* bump the string table size by enough for the
442 * new name and it's null terminator
444 Renew(p->start, idx+len+1, char);
445 if(p->start == NULL) {
446 croak("opendir: malloc failed!\n");
448 strcpy(&p->start[idx], FindData.cFileName);
450 * strlwr(&p->start[idx]);
462 /* Readdir just returns the current string pointer and bumps the
463 * string pointer to the nDllExport entry.
469 static int dummy = 0;
472 /* first set up the structure to return */
473 len = strlen(dirp->curr);
474 strcpy(dirp->dirstr.d_name, dirp->curr);
475 dirp->dirstr.d_namlen = len;
478 dirp->dirstr.d_ino = dummy++;
480 /* Now set up for the nDllExport call to readdir */
481 dirp->curr += len + 1;
482 if (dirp->curr >= (dirp->start + dirp->size)) {
486 return &(dirp->dirstr);
492 /* Telldir returns the current string pointer position */
496 return (long) dirp->curr;
500 /* Seekdir moves the string pointer to a previously saved position
504 seekdir(DIR *dirp, long loc)
506 dirp->curr = (char *)loc;
509 /* Rewinddir resets the string pointer to the start */
513 dirp->curr = dirp->start;
516 /* free the memory allocated by opendir */
520 Safefree(dirp->start);
533 * Just pretend that everyone is a superuser. NT will let us know if
534 * we don\'t really have permission to do something.
537 #define ROOT_UID ((uid_t)0)
538 #define ROOT_GID ((gid_t)0)
567 return (auid == ROOT_UID ? 0 : -1);
573 return (agid == ROOT_GID ? 0 : -1);
580 kill(int pid, int sig)
582 HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
584 if (hProcess == NULL) {
585 croak("kill process failed!\n");
588 if (!TerminateProcess(hProcess, sig))
589 croak("kill process failed!\n");
590 CloseHandle(hProcess);
601 ioctl(int i, unsigned int u, char *data)
603 croak("ioctl not implemented!\n");
608 DllExport unsigned int
609 win32_sleep(unsigned int t)
616 win32_stat(const char *path, struct stat *buffer)
619 const char *p = path;
620 int l = strlen(path);
624 switch(path[l - 1]) {
627 if (path[l - 2] != ':') {
628 strncpy(t, path, l - 1);
634 res = stat(p,buffer);
637 if (S_ISDIR(buffer->st_mode))
638 buffer->st_mode |= S_IWRITE | S_IEXEC;
639 else if (S_ISREG(buffer->st_mode)) {
640 if (l >= 4 && path[l-4] == '.') {
641 const char *e = path + l - 3;
642 if (strnicmp(e,"exe",3)
643 && strnicmp(e,"bat",3)
644 && strnicmp(e,"com",3)
645 && (IsWin95() || strnicmp(e,"cmd",3)))
646 buffer->st_mode &= ~S_IEXEC;
648 buffer->st_mode |= S_IEXEC;
651 buffer->st_mode &= ~S_IEXEC;
658 #ifndef USE_WIN32_RTL_ENV
661 win32_getenv(const char *name)
663 static char *curitem = Nullch;
664 static DWORD curlen = 512;
667 New(1305,curitem,curlen,char);
668 if (!(needlen = GetEnvironmentVariable(name,curitem,curlen)))
670 while (needlen > curlen) {
671 Renew(curitem,needlen,char);
673 needlen = GetEnvironmentVariable(name,curitem,curlen);
681 FileTimeToClock(PFILETIME ft)
683 __int64 qw = ft->dwHighDateTime;
685 qw |= ft->dwLowDateTime;
686 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
691 win32_times(struct tms *timebuf)
696 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
698 timebuf->tms_utime = FileTimeToClock(&user);
699 timebuf->tms_stime = FileTimeToClock(&kernel);
700 timebuf->tms_cutime = 0;
701 timebuf->tms_cstime = 0;
704 /* That failed - e.g. Win95 fallback to clock() */
706 timebuf->tms_utime = t;
707 timebuf->tms_stime = 0;
708 timebuf->tms_cutime = 0;
709 timebuf->tms_cstime = 0;
714 static UINT timerid = 0;
717 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
719 KillTimer(NULL,timerid);
724 DllExport unsigned int
725 win32_alarm(unsigned int sec)
728 * the 'obvious' implentation is SetTimer() with a callback
729 * which does whatever receiving SIGALRM would do
730 * we cannot use SIGALRM even via raise() as it is not
731 * one of the supported codes in <signal.h>
733 * Snag is unless something is looking at the message queue
734 * nothing happens :-(
738 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
740 croak("Cannot set timer");
746 KillTimer(NULL,timerid);
753 #ifdef HAVE_DES_FCRYPT
754 extern char * des_fcrypt(char *cbuf, const char *txt, const char *salt);
757 win32_crypt(const char *txt, const char *salt)
760 return des_fcrypt(crypt_buffer, txt, salt);
764 #ifdef USE_FIXED_OSFHANDLE
766 EXTERN_C int __cdecl _alloc_osfhnd(void);
767 EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
768 EXTERN_C void __cdecl _lock_fhandle(int);
769 EXTERN_C void __cdecl _unlock_fhandle(int);
770 EXTERN_C void __cdecl _unlock(int);
772 #if (_MSC_VER >= 1000)
774 long osfhnd; /* underlying OS file HANDLE */
775 char osfile; /* attributes of file (e.g., open in text mode?) */
776 char pipech; /* one char buffer for handles opened on pipes */
777 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
779 CRITICAL_SECTION lock;
780 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
783 EXTERN_C ioinfo * __pioinfo[];
786 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
787 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
788 #define _osfile(i) (_pioinfo(i)->osfile)
790 #else /* (_MSC_VER >= 1000) */
791 extern char _osfile[];
792 #endif /* (_MSC_VER >= 1000) */
794 #define FOPEN 0x01 /* file handle open */
795 #define FAPPEND 0x20 /* file handle opened O_APPEND */
796 #define FDEV 0x40 /* file handle refers to device */
797 #define FTEXT 0x80 /* file handle is in text mode */
799 #define _STREAM_LOCKS 26 /* Table of stream locks */
800 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */
801 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */
804 *int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
807 * This function allocates a free C Runtime file handle and associates
808 * it with the Win32 HANDLE specified by the first parameter. This is a
809 * temperary fix for WIN95's brain damage GetFileType() error on socket
810 * we just bypass that call for socket
813 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
814 * int flags - flags to associate with C Runtime file handle.
817 * returns index of entry in fh, if successful
818 * return -1, if no free entry is found
822 *******************************************************************************/
825 my_open_osfhandle(long osfhandle, int flags)
828 char fileflags; /* _osfile flags */
830 /* copy relevant flags from second parameter */
834 fileflags |= FAPPEND;
839 /* attempt to allocate a C Runtime file handle */
840 if((fh = _alloc_osfhnd()) == -1) {
841 errno = EMFILE; /* too many open files */
842 _doserrno = 0L; /* not an OS error */
843 return -1; /* return error to caller */
846 /* the file is open. now, set the info in _osfhnd array */
847 _set_osfhnd(fh, osfhandle);
849 fileflags |= FOPEN; /* mark as open */
851 #if (_MSC_VER >= 1000)
852 _osfile(fh) = fileflags; /* set osfile entry */
855 _osfile[fh] = fileflags; /* set osfile entry */
856 _unlock(fh+_FH_LOCKS); /* unlock handle */
859 return fh; /* return handle */
862 #define _open_osfhandle my_open_osfhandle
863 #endif /* USE_FIXED_OSFHANDLE */
865 /* simulate flock by locking a range on the file */
867 #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
868 #define LK_LEN 0xffff0000
871 win32_flock(int fd, int oper)
878 croak("flock() unimplemented on this platform");
881 fh = (HANDLE)_get_osfhandle(fd);
882 memset(&o, 0, sizeof(o));
885 case LOCK_SH: /* shared lock */
886 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
888 case LOCK_EX: /* exclusive lock */
889 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
891 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
892 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
894 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
895 LK_ERR(LockFileEx(fh,
896 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
897 0, LK_LEN, 0, &o),i);
899 case LOCK_UN: /* unlock lock */
900 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
902 default: /* unknown */
913 * redirected io subsystem for all XS modules
926 return (&(_environ));
929 /* the rest are the remapped stdio routines */
949 win32_ferror(FILE *fp)
962 * Since the errors returned by the socket error function
963 * WSAGetLastError() are not known by the library routine strerror
964 * we have to roll our own.
968 win32_strerror(int e)
970 #ifndef __BORLANDC__ /* Borland intolerance */
975 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;
990 win32_fprintf(FILE *fp, const char *format, ...)
993 va_start(marker, format); /* Initialize variable arguments. */
995 return (vfprintf(fp, format, marker));
999 win32_printf(const char *format, ...)
1002 va_start(marker, format); /* Initialize variable arguments. */
1004 return (vprintf(format, marker));
1008 win32_vfprintf(FILE *fp, const char *format, va_list args)
1010 return (vfprintf(fp, format, args));
1014 win32_vprintf(const char *format, va_list args)
1016 return (vprintf(format, args));
1020 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1022 return fread(buf, size, count, fp);
1026 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1028 return fwrite(buf, size, count, fp);
1032 win32_fopen(const char *filename, const char *mode)
1034 if (stricmp(filename, "/dev/null")==0)
1035 return fopen("NUL", mode);
1036 return fopen(filename, mode);
1039 #ifndef USE_SOCKETS_AS_HANDLES
1041 #define fdopen my_fdopen
1045 win32_fdopen( int handle, const char *mode)
1047 return fdopen(handle, (char *) mode);
1051 win32_freopen( const char *path, const char *mode, FILE *stream)
1053 if (stricmp(path, "/dev/null")==0)
1054 return freopen("NUL", mode, stream);
1055 return freopen(path, mode, stream);
1059 win32_fclose(FILE *pf)
1061 return my_fclose(pf); /* defined in win32sck.c */
1065 win32_fputs(const char *s,FILE *pf)
1067 return fputs(s, pf);
1071 win32_fputc(int c,FILE *pf)
1077 win32_ungetc(int c,FILE *pf)
1079 return ungetc(c,pf);
1083 win32_getc(FILE *pf)
1089 win32_fileno(FILE *pf)
1095 win32_clearerr(FILE *pf)
1102 win32_fflush(FILE *pf)
1108 win32_ftell(FILE *pf)
1114 win32_fseek(FILE *pf,long offset,int origin)
1116 return fseek(pf, offset, origin);
1120 win32_fgetpos(FILE *pf,fpos_t *p)
1122 return fgetpos(pf, p);
1126 win32_fsetpos(FILE *pf,const fpos_t *p)
1128 return fsetpos(pf, p);
1132 win32_rewind(FILE *pf)
1152 win32_fstat(int fd,struct stat *sbufptr)
1154 return fstat(fd,sbufptr);
1158 win32_pipe(int *pfd, unsigned int size, int mode)
1160 return _pipe(pfd, size, mode);
1164 win32_popen(const char *command, const char *mode)
1166 return _popen(command, mode);
1170 win32_pclose(FILE *pf)
1176 win32_setmode(int fd, int mode)
1178 return setmode(fd, mode);
1182 win32_lseek(int fd, long offset, int origin)
1184 return lseek(fd, offset, origin);
1194 win32_open(const char *path, int flag, ...)
1200 pmode = va_arg(ap, int);
1203 if (stricmp(path, "/dev/null")==0)
1204 return open("NUL", flag, pmode);
1205 return open(path,flag,pmode);
1227 win32_dup2(int fd1,int fd2)
1229 return dup2(fd1,fd2);
1233 win32_read(int fd, void *buf, unsigned int cnt)
1235 return read(fd, buf, cnt);
1239 win32_write(int fd, const void *buf, unsigned int cnt)
1241 return write(fd, buf, cnt);
1245 win32_mkdir(const char *dir, int mode)
1247 return mkdir(dir); /* just ignore mode */
1251 win32_rmdir(const char *dir)
1257 win32_chdir(const char *dir)
1263 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1265 return spawnvp(mode, cmdname, (char * const *) argv);
1269 win32_execvp(const char *cmdname, const char *const *argv)
1271 return execvp(cmdname, (char *const *)argv);
1275 win32_perror(const char *str)
1281 win32_setbuf(FILE *pf, char *buf)
1287 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
1289 return setvbuf(pf, buf, type, size);
1293 win32_flushall(void)
1299 win32_fcloseall(void)
1305 win32_fgets(char *s, int n, FILE *pf)
1307 return fgets(s, n, pf);
1317 win32_fgetc(FILE *pf)
1323 win32_putc(int c, FILE *pf)
1329 win32_puts(const char *s)
1341 win32_putchar(int c)
1348 #ifndef USE_PERL_SBRK
1350 static char *committed = NULL;
1351 static char *base = NULL;
1352 static char *reserved = NULL;
1353 static char *brk = NULL;
1354 static DWORD pagesize = 0;
1355 static DWORD allocsize = 0;
1363 GetSystemInfo(&info);
1364 /* Pretend page size is larger so we don't perpetually
1365 * call the OS to commit just one page ...
1367 pagesize = info.dwPageSize << 3;
1368 allocsize = info.dwAllocationGranularity;
1370 /* This scheme fails eventually if request for contiguous
1371 * block is denied so reserve big blocks - this is only
1372 * address space not memory ...
1374 if (brk+need >= reserved)
1376 DWORD size = 64*1024*1024;
1378 if (committed && reserved && committed < reserved)
1380 /* Commit last of previous chunk cannot span allocations */
1381 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
1383 committed = reserved;
1385 /* Reserve some (more) space
1386 * Note this is a little sneaky, 1st call passes NULL as reserved
1387 * so lets system choose where we start, subsequent calls pass
1388 * the old end address so ask for a contiguous block
1390 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
1393 reserved = addr+size;
1408 if (brk > committed)
1410 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
1411 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
1426 win32_malloc(size_t size)
1428 return malloc(size);
1432 win32_calloc(size_t numitems, size_t size)
1434 return calloc(numitems,size);
1438 win32_realloc(void *block, size_t size)
1440 return realloc(block,size);
1444 win32_free(void *block)
1451 win32_open_osfhandle(long handle, int flags)
1453 return _open_osfhandle(handle, flags);
1457 win32_get_osfhandle(int fd)
1459 return _get_osfhandle(fd);
1470 SV *sv = sv_newmortal();
1471 /* Make one call with zero size - return value is required size */
1472 DWORD len = GetCurrentDirectory((DWORD)0,NULL);
1473 SvUPGRADE(sv,SVt_PV);
1475 SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
1478 * then it worked, set PV valid,
1479 * else leave it 'undef'
1493 croak("usage: Win32::SetCurrentDirectory($cwd)");
1494 if (SetCurrentDirectory(SvPV(ST(0),na)))
1501 XS(w32_GetNextAvailDrive)
1505 char root[] = "_:\\";
1508 if (GetDriveType(root) == 1) {
1517 XS(w32_GetLastError)
1520 XSRETURN_IV(GetLastError());
1528 DWORD size = sizeof(name);
1529 if (GetUserName(name,&size)) {
1530 /* size includes NULL */
1531 ST(0) = sv_2mortal(newSVpv(name,size-1));
1541 char name[MAX_COMPUTERNAME_LENGTH+1];
1542 DWORD size = sizeof(name);
1543 if (GetComputerName(name,&size)) {
1544 /* size does NOT include NULL :-( */
1545 ST(0) = sv_2mortal(newSVpv(name,size));
1557 DWORD size = sizeof(name);
1558 if (GetUserName(name,&size)) {
1560 DWORD sidlen = sizeof(sid);
1562 DWORD dnamelen = sizeof(dname);
1564 if (LookupAccountName(NULL, name, &sid, &sidlen,
1565 dname, &dnamelen, &snu)) {
1566 XSRETURN_PV(dname); /* all that for this */
1577 DWORD flags, filecomplen;
1578 if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
1579 &flags, fsname, sizeof(fsname))) {
1580 if (GIMME == G_ARRAY) {
1581 XPUSHs(sv_2mortal(newSVpv(fsname,0)));
1582 XPUSHs(sv_2mortal(newSViv(flags)));
1583 XPUSHs(sv_2mortal(newSViv(filecomplen)));
1587 XSRETURN_PV(fsname);
1593 XS(w32_GetOSVersion)
1596 OSVERSIONINFO osver;
1598 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1599 if (GetVersionEx(&osver)) {
1600 XPUSHs(newSVpv(osver.szCSDVersion, 0));
1601 XPUSHs(newSViv(osver.dwMajorVersion));
1602 XPUSHs(newSViv(osver.dwMinorVersion));
1603 XPUSHs(newSViv(osver.dwBuildNumber));
1604 XPUSHs(newSViv(osver.dwPlatformId));
1615 XSRETURN_IV(IsWinNT());
1622 XSRETURN_IV(IsWin95());
1626 XS(w32_FormatMessage)
1633 croak("usage: Win32::FormatMessage($errno)");
1635 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1636 &source, SvIV(ST(0)), 0,
1637 msgbuf, sizeof(msgbuf)-1, NULL))
1638 XSRETURN_PV(msgbuf);
1648 PROCESS_INFORMATION stProcInfo;
1649 STARTUPINFO stStartInfo;
1650 BOOL bSuccess = FALSE;
1653 croak("usage: Win32::Spawn($cmdName, $args, $PID)");
1655 cmd = SvPV(ST(0),na);
1656 args = SvPV(ST(1), na);
1658 memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */
1659 stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */
1660 stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */
1661 stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */
1664 cmd, /* Image path */
1665 args, /* Arguments for command line */
1666 NULL, /* Default process security */
1667 NULL, /* Default thread security */
1668 FALSE, /* Must be TRUE to use std handles */
1669 NORMAL_PRIORITY_CLASS, /* No special scheduling */
1670 NULL, /* Inherit our environment block */
1671 NULL, /* Inherit our currrent directory */
1672 &stStartInfo, /* -> Startup info */
1673 &stProcInfo)) /* <- Process info (if OK) */
1675 CloseHandle(stProcInfo.hThread);/* library source code does this. */
1676 sv_setiv(ST(2), stProcInfo.dwProcessId);
1679 XSRETURN_IV(bSuccess);
1683 XS(w32_GetTickCount)
1686 XSRETURN_IV(GetTickCount());
1690 XS(w32_GetShortPathName)
1697 croak("usage: Win32::GetShortPathName($longPathName)");
1699 shortpath = sv_mortalcopy(ST(0));
1700 SvUPGRADE(shortpath, SVt_PV);
1701 /* src == target is allowed */
1703 len = GetShortPathName(SvPVX(shortpath),
1706 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
1708 SvCUR_set(shortpath,len);
1717 Perl_init_os_extras()
1719 char *file = __FILE__;
1722 /* XXX should be removed after checking with Nick */
1723 newXS("Win32::GetCurrentDirectory", w32_GetCwd, file);
1725 /* these names are Activeware compatible */
1726 newXS("Win32::GetCwd", w32_GetCwd, file);
1727 newXS("Win32::SetCwd", w32_SetCwd, file);
1728 newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
1729 newXS("Win32::GetLastError", w32_GetLastError, file);
1730 newXS("Win32::LoginName", w32_LoginName, file);
1731 newXS("Win32::NodeName", w32_NodeName, file);
1732 newXS("Win32::DomainName", w32_DomainName, file);
1733 newXS("Win32::FsType", w32_FsType, file);
1734 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
1735 newXS("Win32::IsWinNT", w32_IsWinNT, file);
1736 newXS("Win32::IsWin95", w32_IsWin95, file);
1737 newXS("Win32::FormatMessage", w32_FormatMessage, file);
1738 newXS("Win32::Spawn", w32_Spawn, file);
1739 newXS("Win32::GetTickCount", w32_GetTickCount, file);
1740 newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
1742 /* XXX Bloat Alert! The following Activeware preloads really
1743 * ought to be part of Win32::Sys::*, so they're not included
1746 /* LookupAccountName
1748 * InitiateSystemShutdown
1749 * AbortSystemShutdown
1750 * ExpandEnvrironmentStrings
1755 Perl_win32_init(int *argcp, char ***argvp)
1757 /* Disable floating point errors, Perl will trap the ones we
1758 * care about. VC++ RTL defaults to switching these off
1759 * already, but the Borland RTL doesn't. Since we don't
1760 * want to be at the vendor's whim on the default, we set
1761 * it explicitly here.
1763 #if !defined(_ALPHA_) && !defined(__GNUC__)
1764 _control87(MCW_EM, MCW_EM);
1769 #ifdef USE_BINMODE_SCRIPTS
1772 win32_strip_return(SV *sv)
1774 char *s = SvPVX(sv);
1775 char *e = s+SvCUR(sv);
1779 if (*s == '\r' && s[1] == '\n')
1789 SvCUR_set(sv,d-SvPVX(sv));