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 __declspec(thread) char getlogin_buffer[128];
62 # ifdef HAVE_DES_FCRYPT
63 __declspec(thread) char crypt_buffer[30];
66 # define strerror_buffer (thr->i.Wstrerror_buffer)
67 # define getlogin_buffer (thr->i.Wgetlogin_buffer)
68 # define crypt_buffer (thr->i.Wcrypt_buffer)
71 char strerror_buffer[512];
72 char getlogin_buffer[128];
73 # ifdef HAVE_DES_FCRYPT
74 char crypt_buffer[30];
78 static int do_spawn2(char *cmd, int exectype);
82 return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
87 return (IdOS() == VER_PLATFORM_WIN32_NT);
91 win32PerlLibPath(char *sfx,...)
96 GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE)
97 ? GetModuleHandle(NULL)
100 sizeof(szPerlLibRoot));
101 *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
102 if (stricmp(end-4,"\\bin") == 0)
109 sfx = va_arg(ap,char *);
112 return (szPerlLibRoot);
117 HasRedirection(char *ptr)
123 * Scan string looking for redirection (< or >) or pipe
124 * characters (|) that are not in a quoted string
154 /* since the current process environment is being updated in util.c
155 * the library functions will get the correct environment
158 my_popen(char *cmd, char *mode)
161 #define fixcmd(x) { \
162 char *pspace = strchr((x),' '); \
165 while (p < pspace) { \
176 #ifdef __BORLANDC__ /* workaround a Borland stdio bug */
177 win32_fflush(stdout);
178 win32_fflush(stderr);
180 return win32_popen(cmd, mode);
184 my_pclose(PerlIO *fp)
186 return win32_pclose(fp);
192 static OSVERSIONINFO osver;
194 if (osver.dwPlatformId != Win32System) {
195 memset(&osver, 0, sizeof(OSVERSIONINFO));
196 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
197 GetVersionEx(&osver);
198 Win32System = osver.dwPlatformId;
200 return (Win32System);
207 char* defaultshell = (IsWinNT() ? "cmd.exe" : "command.com");
208 /* we don't use COMSPEC here for two reasons:
209 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
210 * uncontrolled unportability of the ensuing scripts.
211 * 2. PERL5SHELL could be set to a shell that may not be fit for
212 * interactive use (which is what most programs look in COMSPEC
215 char *usershell = getenv("PERL5SHELL");
218 strcpy(szShellPath, usershell ? usershell : defaultshell);
224 do_aspawn(void* really, void ** mark, void ** arglast)
232 SV *sv = (SV*)really;
233 SV** pSv = (SV**)mark;
235 New(1310, argv, (arglast - mark) + 4, char*);
238 cmd = SvPV(sv, length);
241 argv[index++] = cmd = GetShell();
243 argv[index++] = "/x"; /* always enable command extensions */
244 argv[index++] = "/c";
247 while(++pSv <= (SV**)arglast) {
249 strPtr = SvPV(sv, length);
250 if(strPtr != NULL && *strPtr != '\0')
251 argv[index++] = strPtr;
255 status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv);
261 warn("Can't spawn \"%s\": %s", cmd, strerror(errno));
268 do_spawn2(char *cmd, int exectype)
274 BOOL needToTry = TRUE;
277 /* save an extra exec if possible */
280 /* see if there are shell metacharacters in it */
281 if(!HasRedirection(cmd)) {
282 New(1301,argv, strlen(cmd) / 2 + 2, char*);
283 New(1302,cmd2, strlen(cmd) + 1, char);
286 for (s = cmd2; *s;) {
287 while (*s && isspace(*s))
291 while(*s && !isspace(*s))
300 status = win32_spawnvp(P_WAIT, argv[0],
301 (const char* const*)argv);
303 case EXECF_SPAWN_NOWAIT:
304 status = win32_spawnvp(P_NOWAIT, argv[0],
305 (const char* const*)argv);
308 status = win32_execvp(argv[0], (const char* const*)argv);
311 if(status != -1 || errno == 0)
323 argv[i++] = "/c"; argv[i++] = cmd; argv[i] = Nullch;
326 status = win32_spawnvp(P_WAIT, argv[0],
327 (const char* const*)argv);
329 case EXECF_SPAWN_NOWAIT:
330 status = win32_spawnvp(P_NOWAIT, argv[0],
331 (const char* const*)argv);
334 status = win32_execvp(argv[0], (const char* const*)argv);
340 warn("Can't %s \"%s\": %s",
341 (exectype == EXECF_EXEC ? "exec" : "spawn"),
342 needToTry ? shell : argv[0],
352 return do_spawn2(cmd, EXECF_SPAWN);
358 do_spawn2(cmd, EXECF_EXEC);
365 /* The idea here is to read all the directory names into a string table
366 * (separated by nulls) and when one of the other dir functions is called
367 * return the pointer to the current file name.
370 opendir(char *filename)
375 char scannamespc[PATHLEN];
376 char *scanname = scannamespc;
378 WIN32_FIND_DATA FindData;
380 /* char root[_MAX_PATH];*/
381 /* char volname[_MAX_PATH];*/
382 /* DWORD serial, maxname, flags;*/
386 /* check to see if filename is a directory */
387 if (win32_stat(filename, &sbuf) < 0 || (sbuf.st_mode & S_IFDIR) == 0) {
391 /* get the file system characteristics */
392 /* if(GetFullPathName(filename, MAX_PATH, root, &dummy)) {
393 * if(dummy = strchr(root, '\\'))
395 * if(GetVolumeInformation(root, volname, MAX_PATH, &serial,
396 * &maxname, &flags, 0, 0)) {
397 * downcase = !(flags & FS_CASE_IS_PRESERVED);
404 /* Get us a DIR structure */
405 Newz(1303, p, 1, DIR);
409 /* Create the search pattern */
410 strcpy(scanname, filename);
412 if(index("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
413 strcat(scanname, "/*");
415 strcat(scanname, "*");
417 /* do the FindFirstFile call */
418 fh = FindFirstFile(scanname, &FindData);
419 if(fh == INVALID_HANDLE_VALUE) {
423 /* now allocate the first part of the string table for
424 * the filenames that we find.
426 idx = strlen(FindData.cFileName)+1;
427 New(1304, p->start, idx, char);
428 if(p->start == NULL) {
429 croak("opendir: malloc failed!\n");
431 strcpy(p->start, FindData.cFileName);
437 /* loop finding all the files that match the wildcard
438 * (which should be all of them in this directory!).
439 * the variable idx should point one past the null terminator
440 * of the previous string found.
442 while (FindNextFile(fh, &FindData)) {
443 len = strlen(FindData.cFileName);
444 /* bump the string table size by enough for the
445 * new name and it's null terminator
447 Renew(p->start, idx+len+1, char);
448 if(p->start == NULL) {
449 croak("opendir: malloc failed!\n");
451 strcpy(&p->start[idx], FindData.cFileName);
453 * strlwr(&p->start[idx]);
465 /* Readdir just returns the current string pointer and bumps the
466 * string pointer to the nDllExport entry.
472 static int dummy = 0;
475 /* first set up the structure to return */
476 len = strlen(dirp->curr);
477 strcpy(dirp->dirstr.d_name, dirp->curr);
478 dirp->dirstr.d_namlen = len;
481 dirp->dirstr.d_ino = dummy++;
483 /* Now set up for the nDllExport call to readdir */
484 dirp->curr += len + 1;
485 if (dirp->curr >= (dirp->start + dirp->size)) {
489 return &(dirp->dirstr);
495 /* Telldir returns the current string pointer position */
499 return (long) dirp->curr;
503 /* Seekdir moves the string pointer to a previously saved position
507 seekdir(DIR *dirp, long loc)
509 dirp->curr = (char *)loc;
512 /* Rewinddir resets the string pointer to the start */
516 dirp->curr = dirp->start;
519 /* free the memory allocated by opendir */
523 Safefree(dirp->start);
536 * Just pretend that everyone is a superuser. NT will let us know if
537 * we don\'t really have permission to do something.
540 #define ROOT_UID ((uid_t)0)
541 #define ROOT_GID ((gid_t)0)
570 return (auid == ROOT_UID ? 0 : -1);
576 return (agid == ROOT_GID ? 0 : -1);
583 char *buf = getlogin_buffer;
584 DWORD size = sizeof(getlogin_buffer);
585 if (GetUserName(buf,&size))
594 kill(int pid, int sig)
596 HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
598 if (hProcess == NULL) {
599 croak("kill process failed!\n");
602 if (!TerminateProcess(hProcess, sig))
603 croak("kill process failed!\n");
604 CloseHandle(hProcess);
615 ioctl(int i, unsigned int u, char *data)
617 croak("ioctl not implemented!\n");
622 DllExport unsigned int
623 win32_sleep(unsigned int t)
630 win32_stat(const char *path, struct stat *buffer)
633 const char *p = path;
634 int l = strlen(path);
638 switch(path[l - 1]) {
641 if (path[l - 2] != ':') {
642 strncpy(t, path, l - 1);
648 res = stat(p,buffer);
651 if (S_ISDIR(buffer->st_mode))
652 buffer->st_mode |= S_IWRITE | S_IEXEC;
653 else if (S_ISREG(buffer->st_mode)) {
654 if (l >= 4 && path[l-4] == '.') {
655 const char *e = path + l - 3;
656 if (strnicmp(e,"exe",3)
657 && strnicmp(e,"bat",3)
658 && strnicmp(e,"com",3)
659 && (IsWin95() || strnicmp(e,"cmd",3)))
660 buffer->st_mode &= ~S_IEXEC;
662 buffer->st_mode |= S_IEXEC;
665 buffer->st_mode &= ~S_IEXEC;
672 #ifndef USE_WIN32_RTL_ENV
675 win32_getenv(const char *name)
677 static char *curitem = Nullch;
678 static DWORD curlen = 512;
681 New(1305,curitem,curlen,char);
682 if (!(needlen = GetEnvironmentVariable(name,curitem,curlen)))
684 while (needlen > curlen) {
685 Renew(curitem,needlen,char);
687 needlen = GetEnvironmentVariable(name,curitem,curlen);
695 FileTimeToClock(PFILETIME ft)
697 __int64 qw = ft->dwHighDateTime;
699 qw |= ft->dwLowDateTime;
700 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
705 win32_times(struct tms *timebuf)
710 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
712 timebuf->tms_utime = FileTimeToClock(&user);
713 timebuf->tms_stime = FileTimeToClock(&kernel);
714 timebuf->tms_cutime = 0;
715 timebuf->tms_cstime = 0;
718 /* That failed - e.g. Win95 fallback to clock() */
720 timebuf->tms_utime = t;
721 timebuf->tms_stime = 0;
722 timebuf->tms_cutime = 0;
723 timebuf->tms_cstime = 0;
728 static UINT timerid = 0;
731 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
733 KillTimer(NULL,timerid);
738 DllExport unsigned int
739 win32_alarm(unsigned int sec)
742 * the 'obvious' implentation is SetTimer() with a callback
743 * which does whatever receiving SIGALRM would do
744 * we cannot use SIGALRM even via raise() as it is not
745 * one of the supported codes in <signal.h>
747 * Snag is unless something is looking at the message queue
748 * nothing happens :-(
752 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
754 croak("Cannot set timer");
760 KillTimer(NULL,timerid);
767 #ifdef HAVE_DES_FCRYPT
768 extern char * des_fcrypt(char *cbuf, const char *txt, const char *salt);
771 win32_crypt(const char *txt, const char *salt)
774 return des_fcrypt(crypt_buffer, txt, salt);
778 #ifdef USE_FIXED_OSFHANDLE
780 EXTERN_C int __cdecl _alloc_osfhnd(void);
781 EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
782 EXTERN_C void __cdecl _lock_fhandle(int);
783 EXTERN_C void __cdecl _unlock_fhandle(int);
784 EXTERN_C void __cdecl _unlock(int);
786 #if (_MSC_VER >= 1000)
788 long osfhnd; /* underlying OS file HANDLE */
789 char osfile; /* attributes of file (e.g., open in text mode?) */
790 char pipech; /* one char buffer for handles opened on pipes */
791 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
793 CRITICAL_SECTION lock;
794 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
797 EXTERN_C ioinfo * __pioinfo[];
800 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
801 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
802 #define _osfile(i) (_pioinfo(i)->osfile)
804 #else /* (_MSC_VER >= 1000) */
805 extern char _osfile[];
806 #endif /* (_MSC_VER >= 1000) */
808 #define FOPEN 0x01 /* file handle open */
809 #define FAPPEND 0x20 /* file handle opened O_APPEND */
810 #define FDEV 0x40 /* file handle refers to device */
811 #define FTEXT 0x80 /* file handle is in text mode */
813 #define _STREAM_LOCKS 26 /* Table of stream locks */
814 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */
815 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */
818 *int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
821 * This function allocates a free C Runtime file handle and associates
822 * it with the Win32 HANDLE specified by the first parameter. This is a
823 * temperary fix for WIN95's brain damage GetFileType() error on socket
824 * we just bypass that call for socket
827 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
828 * int flags - flags to associate with C Runtime file handle.
831 * returns index of entry in fh, if successful
832 * return -1, if no free entry is found
836 *******************************************************************************/
839 my_open_osfhandle(long osfhandle, int flags)
842 char fileflags; /* _osfile flags */
844 /* copy relevant flags from second parameter */
848 fileflags |= FAPPEND;
853 /* attempt to allocate a C Runtime file handle */
854 if((fh = _alloc_osfhnd()) == -1) {
855 errno = EMFILE; /* too many open files */
856 _doserrno = 0L; /* not an OS error */
857 return -1; /* return error to caller */
860 /* the file is open. now, set the info in _osfhnd array */
861 _set_osfhnd(fh, osfhandle);
863 fileflags |= FOPEN; /* mark as open */
865 #if (_MSC_VER >= 1000)
866 _osfile(fh) = fileflags; /* set osfile entry */
869 _osfile[fh] = fileflags; /* set osfile entry */
870 _unlock(fh+_FH_LOCKS); /* unlock handle */
873 return fh; /* return handle */
876 #define _open_osfhandle my_open_osfhandle
877 #endif /* USE_FIXED_OSFHANDLE */
879 /* simulate flock by locking a range on the file */
881 #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
882 #define LK_LEN 0xffff0000
885 win32_flock(int fd, int oper)
892 croak("flock() unimplemented on this platform");
895 fh = (HANDLE)_get_osfhandle(fd);
896 memset(&o, 0, sizeof(o));
899 case LOCK_SH: /* shared lock */
900 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
902 case LOCK_EX: /* exclusive lock */
903 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
905 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
906 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
908 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
909 LK_ERR(LockFileEx(fh,
910 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
911 0, LK_LEN, 0, &o),i);
913 case LOCK_UN: /* unlock lock */
914 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
916 default: /* unknown */
927 * redirected io subsystem for all XS modules
940 return (&(_environ));
943 /* the rest are the remapped stdio routines */
963 win32_ferror(FILE *fp)
976 * Since the errors returned by the socket error function
977 * WSAGetLastError() are not known by the library routine strerror
978 * we have to roll our own.
982 win32_strerror(int e)
984 #ifndef __BORLANDC__ /* Borland intolerance */
989 if(e < 0 || e > sys_nerr) {
994 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
995 strerror_buffer, sizeof(strerror_buffer), NULL) == 0)
996 strcpy(strerror_buffer, "Unknown Error");
998 return strerror_buffer;
1004 win32_fprintf(FILE *fp, const char *format, ...)
1007 va_start(marker, format); /* Initialize variable arguments. */
1009 return (vfprintf(fp, format, marker));
1013 win32_printf(const char *format, ...)
1016 va_start(marker, format); /* Initialize variable arguments. */
1018 return (vprintf(format, marker));
1022 win32_vfprintf(FILE *fp, const char *format, va_list args)
1024 return (vfprintf(fp, format, args));
1028 win32_vprintf(const char *format, va_list args)
1030 return (vprintf(format, args));
1034 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1036 return fread(buf, size, count, fp);
1040 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1042 return fwrite(buf, size, count, fp);
1046 win32_fopen(const char *filename, const char *mode)
1048 if (stricmp(filename, "/dev/null")==0)
1049 return fopen("NUL", mode);
1050 return fopen(filename, mode);
1053 #ifndef USE_SOCKETS_AS_HANDLES
1055 #define fdopen my_fdopen
1059 win32_fdopen( int handle, const char *mode)
1061 return fdopen(handle, (char *) mode);
1065 win32_freopen( const char *path, const char *mode, FILE *stream)
1067 if (stricmp(path, "/dev/null")==0)
1068 return freopen("NUL", mode, stream);
1069 return freopen(path, mode, stream);
1073 win32_fclose(FILE *pf)
1075 return my_fclose(pf); /* defined in win32sck.c */
1079 win32_fputs(const char *s,FILE *pf)
1081 return fputs(s, pf);
1085 win32_fputc(int c,FILE *pf)
1091 win32_ungetc(int c,FILE *pf)
1093 return ungetc(c,pf);
1097 win32_getc(FILE *pf)
1103 win32_fileno(FILE *pf)
1109 win32_clearerr(FILE *pf)
1116 win32_fflush(FILE *pf)
1122 win32_ftell(FILE *pf)
1128 win32_fseek(FILE *pf,long offset,int origin)
1130 return fseek(pf, offset, origin);
1134 win32_fgetpos(FILE *pf,fpos_t *p)
1136 return fgetpos(pf, p);
1140 win32_fsetpos(FILE *pf,const fpos_t *p)
1142 return fsetpos(pf, p);
1146 win32_rewind(FILE *pf)
1166 win32_fstat(int fd,struct stat *sbufptr)
1168 return fstat(fd,sbufptr);
1172 win32_pipe(int *pfd, unsigned int size, int mode)
1174 return _pipe(pfd, size, mode);
1178 win32_popen(const char *command, const char *mode)
1180 return _popen(command, mode);
1184 win32_pclose(FILE *pf)
1190 win32_setmode(int fd, int mode)
1192 return setmode(fd, mode);
1196 win32_lseek(int fd, long offset, int origin)
1198 return lseek(fd, offset, origin);
1208 win32_open(const char *path, int flag, ...)
1214 pmode = va_arg(ap, int);
1217 if (stricmp(path, "/dev/null")==0)
1218 return open("NUL", flag, pmode);
1219 return open(path,flag,pmode);
1241 win32_dup2(int fd1,int fd2)
1243 return dup2(fd1,fd2);
1247 win32_read(int fd, void *buf, unsigned int cnt)
1249 return read(fd, buf, cnt);
1253 win32_write(int fd, const void *buf, unsigned int cnt)
1255 return write(fd, buf, cnt);
1259 win32_mkdir(const char *dir, int mode)
1261 return mkdir(dir); /* just ignore mode */
1265 win32_rmdir(const char *dir)
1271 win32_chdir(const char *dir)
1277 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1279 return spawnvp(mode, cmdname, (char * const *) argv);
1283 win32_execvp(const char *cmdname, const char *const *argv)
1285 return execvp(cmdname, (char *const *)argv);
1289 win32_perror(const char *str)
1295 win32_setbuf(FILE *pf, char *buf)
1301 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
1303 return setvbuf(pf, buf, type, size);
1307 win32_flushall(void)
1313 win32_fcloseall(void)
1319 win32_fgets(char *s, int n, FILE *pf)
1321 return fgets(s, n, pf);
1331 win32_fgetc(FILE *pf)
1337 win32_putc(int c, FILE *pf)
1343 win32_puts(const char *s)
1355 win32_putchar(int c)
1362 #ifndef USE_PERL_SBRK
1364 static char *committed = NULL;
1365 static char *base = NULL;
1366 static char *reserved = NULL;
1367 static char *brk = NULL;
1368 static DWORD pagesize = 0;
1369 static DWORD allocsize = 0;
1377 GetSystemInfo(&info);
1378 /* Pretend page size is larger so we don't perpetually
1379 * call the OS to commit just one page ...
1381 pagesize = info.dwPageSize << 3;
1382 allocsize = info.dwAllocationGranularity;
1384 /* This scheme fails eventually if request for contiguous
1385 * block is denied so reserve big blocks - this is only
1386 * address space not memory ...
1388 if (brk+need >= reserved)
1390 DWORD size = 64*1024*1024;
1392 if (committed && reserved && committed < reserved)
1394 /* Commit last of previous chunk cannot span allocations */
1395 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
1397 committed = reserved;
1399 /* Reserve some (more) space
1400 * Note this is a little sneaky, 1st call passes NULL as reserved
1401 * so lets system choose where we start, subsequent calls pass
1402 * the old end address so ask for a contiguous block
1404 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
1407 reserved = addr+size;
1422 if (brk > committed)
1424 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
1425 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
1440 win32_malloc(size_t size)
1442 return malloc(size);
1446 win32_calloc(size_t numitems, size_t size)
1448 return calloc(numitems,size);
1452 win32_realloc(void *block, size_t size)
1454 return realloc(block,size);
1458 win32_free(void *block)
1465 win32_open_osfhandle(long handle, int flags)
1467 return _open_osfhandle(handle, flags);
1471 win32_get_osfhandle(int fd)
1473 return _get_osfhandle(fd);
1484 SV *sv = sv_newmortal();
1485 /* Make one call with zero size - return value is required size */
1486 DWORD len = GetCurrentDirectory((DWORD)0,NULL);
1487 SvUPGRADE(sv,SVt_PV);
1489 SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
1492 * then it worked, set PV valid,
1493 * else leave it 'undef'
1507 croak("usage: Win32::SetCurrentDirectory($cwd)");
1508 if (SetCurrentDirectory(SvPV(ST(0),na)))
1515 XS(w32_GetNextAvailDrive)
1519 char root[] = "_:\\";
1522 if (GetDriveType(root) == 1) {
1531 XS(w32_GetLastError)
1534 XSRETURN_IV(GetLastError());
1541 char *name = getlogin_buffer;
1542 DWORD size = sizeof(getlogin_buffer);
1543 if (GetUserName(name,&size)) {
1544 /* size includes NULL */
1545 ST(0) = sv_2mortal(newSVpv(name,size-1));
1555 char name[MAX_COMPUTERNAME_LENGTH+1];
1556 DWORD size = sizeof(name);
1557 if (GetComputerName(name,&size)) {
1558 /* size does NOT include NULL :-( */
1559 ST(0) = sv_2mortal(newSVpv(name,size));
1571 DWORD size = sizeof(name);
1572 if (GetUserName(name,&size)) {
1574 DWORD sidlen = sizeof(sid);
1576 DWORD dnamelen = sizeof(dname);
1578 if (LookupAccountName(NULL, name, &sid, &sidlen,
1579 dname, &dnamelen, &snu)) {
1580 XSRETURN_PV(dname); /* all that for this */
1591 DWORD flags, filecomplen;
1592 if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
1593 &flags, fsname, sizeof(fsname))) {
1594 if (GIMME == G_ARRAY) {
1595 XPUSHs(sv_2mortal(newSVpv(fsname,0)));
1596 XPUSHs(sv_2mortal(newSViv(flags)));
1597 XPUSHs(sv_2mortal(newSViv(filecomplen)));
1601 XSRETURN_PV(fsname);
1607 XS(w32_GetOSVersion)
1610 OSVERSIONINFO osver;
1612 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1613 if (GetVersionEx(&osver)) {
1614 XPUSHs(newSVpv(osver.szCSDVersion, 0));
1615 XPUSHs(newSViv(osver.dwMajorVersion));
1616 XPUSHs(newSViv(osver.dwMinorVersion));
1617 XPUSHs(newSViv(osver.dwBuildNumber));
1618 XPUSHs(newSViv(osver.dwPlatformId));
1629 XSRETURN_IV(IsWinNT());
1636 XSRETURN_IV(IsWin95());
1640 XS(w32_FormatMessage)
1647 croak("usage: Win32::FormatMessage($errno)");
1649 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1650 &source, SvIV(ST(0)), 0,
1651 msgbuf, sizeof(msgbuf)-1, NULL))
1652 XSRETURN_PV(msgbuf);
1662 PROCESS_INFORMATION stProcInfo;
1663 STARTUPINFO stStartInfo;
1664 BOOL bSuccess = FALSE;
1667 croak("usage: Win32::Spawn($cmdName, $args, $PID)");
1669 cmd = SvPV(ST(0),na);
1670 args = SvPV(ST(1), na);
1672 memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */
1673 stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */
1674 stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */
1675 stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */
1678 cmd, /* Image path */
1679 args, /* Arguments for command line */
1680 NULL, /* Default process security */
1681 NULL, /* Default thread security */
1682 FALSE, /* Must be TRUE to use std handles */
1683 NORMAL_PRIORITY_CLASS, /* No special scheduling */
1684 NULL, /* Inherit our environment block */
1685 NULL, /* Inherit our currrent directory */
1686 &stStartInfo, /* -> Startup info */
1687 &stProcInfo)) /* <- Process info (if OK) */
1689 CloseHandle(stProcInfo.hThread);/* library source code does this. */
1690 sv_setiv(ST(2), stProcInfo.dwProcessId);
1693 XSRETURN_IV(bSuccess);
1697 XS(w32_GetTickCount)
1700 XSRETURN_IV(GetTickCount());
1704 XS(w32_GetShortPathName)
1711 croak("usage: Win32::GetShortPathName($longPathName)");
1713 shortpath = sv_mortalcopy(ST(0));
1714 SvUPGRADE(shortpath, SVt_PV);
1715 /* src == target is allowed */
1717 len = GetShortPathName(SvPVX(shortpath),
1720 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
1722 SvCUR_set(shortpath,len);
1731 Perl_init_os_extras()
1733 char *file = __FILE__;
1736 /* XXX should be removed after checking with Nick */
1737 newXS("Win32::GetCurrentDirectory", w32_GetCwd, file);
1739 /* these names are Activeware compatible */
1740 newXS("Win32::GetCwd", w32_GetCwd, file);
1741 newXS("Win32::SetCwd", w32_SetCwd, file);
1742 newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
1743 newXS("Win32::GetLastError", w32_GetLastError, file);
1744 newXS("Win32::LoginName", w32_LoginName, file);
1745 newXS("Win32::NodeName", w32_NodeName, file);
1746 newXS("Win32::DomainName", w32_DomainName, file);
1747 newXS("Win32::FsType", w32_FsType, file);
1748 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
1749 newXS("Win32::IsWinNT", w32_IsWinNT, file);
1750 newXS("Win32::IsWin95", w32_IsWin95, file);
1751 newXS("Win32::FormatMessage", w32_FormatMessage, file);
1752 newXS("Win32::Spawn", w32_Spawn, file);
1753 newXS("Win32::GetTickCount", w32_GetTickCount, file);
1754 newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
1756 /* XXX Bloat Alert! The following Activeware preloads really
1757 * ought to be part of Win32::Sys::*, so they're not included
1760 /* LookupAccountName
1762 * InitiateSystemShutdown
1763 * AbortSystemShutdown
1764 * ExpandEnvrironmentStrings
1769 Perl_win32_init(int *argcp, char ***argvp)
1771 /* Disable floating point errors, Perl will trap the ones we
1772 * care about. VC++ RTL defaults to switching these off
1773 * already, but the Borland RTL doesn't. Since we don't
1774 * want to be at the vendor's whim on the default, we set
1775 * it explicitly here.
1777 #if !defined(_ALPHA_) && !defined(__GNUC__)
1778 _control87(MCW_EM, MCW_EM);
1783 #ifdef USE_BINMODE_SCRIPTS
1786 win32_strip_return(SV *sv)
1788 char *s = SvPVX(sv);
1789 char *e = s+SvCUR(sv);
1793 if (*s == '\r' && s[1] == '\n')
1803 SvCUR_set(sv,d-SvPVX(sv));