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)
38 #define EXECF_SPAWN_NOWAIT 3
40 static DWORD IdOS(void);
42 BOOL ProbeEnv = FALSE;
43 DWORD Win32System = (DWORD)-1;
44 char szShellPath[MAX_PATH+1];
45 char szPerlLibRoot[MAX_PATH+1];
46 HANDLE PerlDllHandle = INVALID_HANDLE_VALUE;
48 static int do_spawn2(char *cmd, int exectype);
52 return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
57 return (IdOS() == VER_PLATFORM_WIN32_NT);
61 win32PerlLibPath(char *sfx,...)
66 GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE)
67 ? GetModuleHandle(NULL)
70 sizeof(szPerlLibRoot));
71 *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
72 if (stricmp(end-4,"\\bin") == 0)
79 sfx = va_arg(ap,char *);
82 return (szPerlLibRoot);
87 HasRedirection(char *ptr)
93 * Scan string looking for redirection (< or >) or pipe
94 * characters (|) that are not in a quoted string
124 /* since the current process environment is being updated in util.c
125 * the library functions will get the correct environment
128 my_popen(char *cmd, char *mode)
131 #define fixcmd(x) { \
132 char *pspace = strchr((x),' '); \
135 while (p < pspace) { \
146 #ifdef __BORLANDC__ /* workaround a Borland stdio bug */
147 win32_fflush(stdout);
148 win32_fflush(stderr);
150 return win32_popen(cmd, mode);
154 my_pclose(PerlIO *fp)
156 return win32_pclose(fp);
162 static OSVERSIONINFO osver;
164 if (osver.dwPlatformId != Win32System) {
165 memset(&osver, 0, sizeof(OSVERSIONINFO));
166 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
167 GetVersionEx(&osver);
168 Win32System = osver.dwPlatformId;
170 return (Win32System);
177 char* defaultshell = (IsWinNT() ? "cmd.exe" : "command.com");
178 /* we don't use COMSPEC here for two reasons:
179 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
180 * uncontrolled unportability of the ensuing scripts.
181 * 2. PERL5SHELL could be set to a shell that may not be fit for
182 * interactive use (which is what most programs look in COMSPEC
185 char *usershell = getenv("PERL5SHELL");
188 strcpy(szShellPath, usershell ? usershell : defaultshell);
194 do_aspawn(void* really, void ** mark, void ** arglast)
202 SV *sv = (SV*)really;
203 SV** pSv = (SV**)mark;
205 New(1310, argv, (arglast - mark) + 4, char*);
208 cmd = SvPV(sv, length);
211 argv[index++] = cmd = GetShell();
213 argv[index++] = "/x"; /* always enable command extensions */
214 argv[index++] = "/c";
217 while(++pSv <= (SV**)arglast) {
219 strPtr = SvPV(sv, length);
220 if(strPtr != NULL && *strPtr != '\0')
221 argv[index++] = strPtr;
225 status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv);
231 warn("Can't spawn \"%s\": %s", cmd, strerror(errno));
238 do_spawn2(char *cmd, int exectype)
244 BOOL needToTry = TRUE;
247 /* save an extra exec if possible */
250 /* see if there are shell metacharacters in it */
251 if(!HasRedirection(cmd)) {
252 New(1301,argv, strlen(cmd) / 2 + 2, char*);
253 New(1302,cmd2, strlen(cmd) + 1, char);
256 for (s = cmd2; *s;) {
257 while (*s && isspace(*s))
261 while(*s && !isspace(*s))
270 status = win32_spawnvp(P_WAIT, argv[0],
271 (const char* const*)argv);
273 case EXECF_SPAWN_NOWAIT:
274 status = win32_spawnvp(P_NOWAIT, argv[0],
275 (const char* const*)argv);
278 status = win32_execvp(argv[0], (const char* const*)argv);
281 if(status != -1 || errno == 0)
293 argv[i++] = "/c"; argv[i++] = cmd; argv[i] = Nullch;
296 status = win32_spawnvp(P_WAIT, argv[0],
297 (const char* const*)argv);
299 case EXECF_SPAWN_NOWAIT:
300 status = win32_spawnvp(P_NOWAIT, argv[0],
301 (const char* const*)argv);
304 status = win32_execvp(argv[0], (const char* const*)argv);
310 warn("Can't %s \"%s\": %s",
311 (exectype == EXECF_EXEC ? "exec" : "spawn"),
312 needToTry ? shell : argv[0],
322 return do_spawn2(cmd, EXECF_SPAWN);
328 do_spawn2(cmd, EXECF_EXEC);
335 /* The idea here is to read all the directory names into a string table
336 * (separated by nulls) and when one of the other dir functions is called
337 * return the pointer to the current file name.
340 opendir(char *filename)
345 char scannamespc[PATHLEN];
346 char *scanname = scannamespc;
348 WIN32_FIND_DATA FindData;
350 /* char root[_MAX_PATH];*/
351 /* char volname[_MAX_PATH];*/
352 /* DWORD serial, maxname, flags;*/
356 /* check to see if filename is a directory */
357 if (win32_stat(filename, &sbuf) < 0 || (sbuf.st_mode & S_IFDIR) == 0) {
361 /* get the file system characteristics */
362 /* if(GetFullPathName(filename, MAX_PATH, root, &dummy)) {
363 * if(dummy = strchr(root, '\\'))
365 * if(GetVolumeInformation(root, volname, MAX_PATH, &serial,
366 * &maxname, &flags, 0, 0)) {
367 * downcase = !(flags & FS_CASE_IS_PRESERVED);
374 /* Get us a DIR structure */
375 Newz(1303, p, 1, DIR);
379 /* Create the search pattern */
380 strcpy(scanname, filename);
382 if(index("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
383 strcat(scanname, "/*");
385 strcat(scanname, "*");
387 /* do the FindFirstFile call */
388 fh = FindFirstFile(scanname, &FindData);
389 if(fh == INVALID_HANDLE_VALUE) {
393 /* now allocate the first part of the string table for
394 * the filenames that we find.
396 idx = strlen(FindData.cFileName)+1;
397 New(1304, p->start, idx, char);
398 if(p->start == NULL) {
399 croak("opendir: malloc failed!\n");
401 strcpy(p->start, FindData.cFileName);
407 /* loop finding all the files that match the wildcard
408 * (which should be all of them in this directory!).
409 * the variable idx should point one past the null terminator
410 * of the previous string found.
412 while (FindNextFile(fh, &FindData)) {
413 len = strlen(FindData.cFileName);
414 /* bump the string table size by enough for the
415 * new name and it's null terminator
417 Renew(p->start, idx+len+1, char);
418 if(p->start == NULL) {
419 croak("opendir: malloc failed!\n");
421 strcpy(&p->start[idx], FindData.cFileName);
423 * strlwr(&p->start[idx]);
435 /* Readdir just returns the current string pointer and bumps the
436 * string pointer to the nDllExport entry.
442 static int dummy = 0;
445 /* first set up the structure to return */
446 len = strlen(dirp->curr);
447 strcpy(dirp->dirstr.d_name, dirp->curr);
448 dirp->dirstr.d_namlen = len;
451 dirp->dirstr.d_ino = dummy++;
453 /* Now set up for the nDllExport call to readdir */
454 dirp->curr += len + 1;
455 if (dirp->curr >= (dirp->start + dirp->size)) {
459 return &(dirp->dirstr);
465 /* Telldir returns the current string pointer position */
469 return (long) dirp->curr;
473 /* Seekdir moves the string pointer to a previously saved position
477 seekdir(DIR *dirp, long loc)
479 dirp->curr = (char *)loc;
482 /* Rewinddir resets the string pointer to the start */
486 dirp->curr = dirp->start;
489 /* free the memory allocated by opendir */
493 Safefree(dirp->start);
506 * Just pretend that everyone is a superuser. NT will let us know if
507 * we don\'t really have permission to do something.
510 #define ROOT_UID ((uid_t)0)
511 #define ROOT_GID ((gid_t)0)
540 return (uid == ROOT_UID ? 0 : -1);
546 return (gid == ROOT_GID ? 0 : -1);
553 kill(int pid, int sig)
555 HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
557 if (hProcess == NULL) {
558 croak("kill process failed!\n");
561 if (!TerminateProcess(hProcess, sig))
562 croak("kill process failed!\n");
563 CloseHandle(hProcess);
574 ioctl(int i, unsigned int u, char *data)
576 croak("ioctl not implemented!\n");
581 DllExport unsigned int
582 win32_sleep(unsigned int t)
589 win32_stat(const char *path, struct stat *buffer)
592 const char *p = path;
593 int l = strlen(path);
597 switch(path[l - 1]) {
600 if (path[l - 2] != ':') {
601 strncpy(t, path, l - 1);
607 res = stat(p,buffer);
610 if (S_ISDIR(buffer->st_mode))
611 buffer->st_mode |= S_IWRITE | S_IEXEC;
612 else if (S_ISREG(buffer->st_mode)) {
613 if (l >= 4 && path[l-4] == '.') {
614 const char *e = path + l - 3;
615 if (strnicmp(e,"exe",3)
616 && strnicmp(e,"bat",3)
617 && strnicmp(e,"com",3)
618 && (IsWin95() || strnicmp(e,"cmd",3)))
619 buffer->st_mode &= ~S_IEXEC;
621 buffer->st_mode |= S_IEXEC;
624 buffer->st_mode &= ~S_IEXEC;
631 #ifndef USE_WIN32_RTL_ENV
634 win32_getenv(const char *name)
636 static char *curitem = Nullch;
637 static DWORD curlen = 512;
640 New(1305,curitem,curlen,char);
641 if (!(needlen = GetEnvironmentVariable(name,curitem,curlen)))
643 while (needlen > curlen) {
644 Renew(curitem,needlen,char);
646 needlen = GetEnvironmentVariable(name,curitem,curlen);
654 FileTimeToClock(PFILETIME ft)
656 __int64 qw = ft->dwHighDateTime;
658 qw |= ft->dwLowDateTime;
659 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
664 win32_times(struct tms *timebuf)
669 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
671 timebuf->tms_utime = FileTimeToClock(&user);
672 timebuf->tms_stime = FileTimeToClock(&kernel);
673 timebuf->tms_cutime = 0;
674 timebuf->tms_cstime = 0;
677 /* That failed - e.g. Win95 fallback to clock() */
679 timebuf->tms_utime = t;
680 timebuf->tms_stime = 0;
681 timebuf->tms_cutime = 0;
682 timebuf->tms_cstime = 0;
687 static UINT timerid = 0;
690 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
692 KillTimer(NULL,timerid);
697 DllExport unsigned int
698 win32_alarm(unsigned int sec)
701 * the 'obvious' implentation is SetTimer() with a callback
702 * which does whatever receiving SIGALRM would do
703 * we cannot use SIGALRM even via raise() as it is not
704 * one of the supported codes in <signal.h>
706 * Snag is unless something is looking at the message queue
707 * nothing happens :-(
711 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
713 croak("Cannot set timer");
719 KillTimer(NULL,timerid);
726 #ifdef USE_FIXED_OSFHANDLE
728 EXTERN_C int __cdecl _alloc_osfhnd(void);
729 EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
730 EXTERN_C void __cdecl _lock_fhandle(int);
731 EXTERN_C void __cdecl _unlock_fhandle(int);
732 EXTERN_C void __cdecl _unlock(int);
734 #if (_MSC_VER >= 1000)
736 long osfhnd; /* underlying OS file HANDLE */
737 char osfile; /* attributes of file (e.g., open in text mode?) */
738 char pipech; /* one char buffer for handles opened on pipes */
739 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
741 CRITICAL_SECTION lock;
742 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
745 EXTERN_C ioinfo * __pioinfo[];
748 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
749 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
750 #define _osfile(i) (_pioinfo(i)->osfile)
752 #else /* (_MSC_VER >= 1000) */
753 extern char _osfile[];
754 #endif /* (_MSC_VER >= 1000) */
756 #define FOPEN 0x01 /* file handle open */
757 #define FAPPEND 0x20 /* file handle opened O_APPEND */
758 #define FDEV 0x40 /* file handle refers to device */
759 #define FTEXT 0x80 /* file handle is in text mode */
761 #define _STREAM_LOCKS 26 /* Table of stream locks */
762 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */
763 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */
766 *int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
769 * This function allocates a free C Runtime file handle and associates
770 * it with the Win32 HANDLE specified by the first parameter. This is a
771 * temperary fix for WIN95's brain damage GetFileType() error on socket
772 * we just bypass that call for socket
775 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
776 * int flags - flags to associate with C Runtime file handle.
779 * returns index of entry in fh, if successful
780 * return -1, if no free entry is found
784 *******************************************************************************/
787 my_open_osfhandle(long osfhandle, int flags)
790 char fileflags; /* _osfile flags */
792 /* copy relevant flags from second parameter */
796 fileflags |= FAPPEND;
801 /* attempt to allocate a C Runtime file handle */
802 if((fh = _alloc_osfhnd()) == -1) {
803 errno = EMFILE; /* too many open files */
804 _doserrno = 0L; /* not an OS error */
805 return -1; /* return error to caller */
808 /* the file is open. now, set the info in _osfhnd array */
809 _set_osfhnd(fh, osfhandle);
811 fileflags |= FOPEN; /* mark as open */
813 #if (_MSC_VER >= 1000)
814 _osfile(fh) = fileflags; /* set osfile entry */
817 _osfile[fh] = fileflags; /* set osfile entry */
818 _unlock(fh+_FH_LOCKS); /* unlock handle */
821 return fh; /* return handle */
824 #define _open_osfhandle my_open_osfhandle
825 #endif /* USE_FIXED_OSFHANDLE */
827 /* simulate flock by locking a range on the file */
829 #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
830 #define LK_LEN 0xffff0000
833 win32_flock(int fd, int oper)
840 croak("flock() unimplemented on this platform");
843 fh = (HANDLE)_get_osfhandle(fd);
844 memset(&o, 0, sizeof(o));
847 case LOCK_SH: /* shared lock */
848 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
850 case LOCK_EX: /* exclusive lock */
851 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
853 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
854 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
856 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
857 LK_ERR(LockFileEx(fh,
858 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
859 0, LK_LEN, 0, &o),i);
861 case LOCK_UN: /* unlock lock */
862 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
864 default: /* unknown */
875 * redirected io subsystem for all XS modules
888 return (&(_environ));
891 /* the rest are the remapped stdio routines */
911 win32_ferror(FILE *fp)
924 * Since the errors returned by the socket error function
925 * WSAGetLastError() are not known by the library routine strerror
926 * we have to roll our own.
929 __declspec(thread) char strerror_buffer[512];
932 win32_strerror(int e)
934 #ifndef __BORLANDC__ /* Borland intolerance */
939 if(e < 0 || e > sys_nerr) {
943 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
944 strerror_buffer, sizeof(strerror_buffer), NULL) == 0)
945 strcpy(strerror_buffer, "Unknown Error");
947 return strerror_buffer;
953 win32_fprintf(FILE *fp, const char *format, ...)
956 va_start(marker, format); /* Initialize variable arguments. */
958 return (vfprintf(fp, format, marker));
962 win32_printf(const char *format, ...)
965 va_start(marker, format); /* Initialize variable arguments. */
967 return (vprintf(format, marker));
971 win32_vfprintf(FILE *fp, const char *format, va_list args)
973 return (vfprintf(fp, format, args));
977 win32_vprintf(const char *format, va_list args)
979 return (vprintf(format, args));
983 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
985 return fread(buf, size, count, fp);
989 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
991 return fwrite(buf, size, count, fp);
995 win32_fopen(const char *filename, const char *mode)
997 if (stricmp(filename, "/dev/null")==0)
998 return fopen("NUL", mode);
999 return fopen(filename, mode);
1002 #ifndef USE_SOCKETS_AS_HANDLES
1004 #define fdopen my_fdopen
1008 win32_fdopen( int handle, const char *mode)
1010 return fdopen(handle, (char *) mode);
1014 win32_freopen( const char *path, const char *mode, FILE *stream)
1016 if (stricmp(path, "/dev/null")==0)
1017 return freopen("NUL", mode, stream);
1018 return freopen(path, mode, stream);
1022 win32_fclose(FILE *pf)
1024 return my_fclose(pf); /* defined in win32sck.c */
1028 win32_fputs(const char *s,FILE *pf)
1030 return fputs(s, pf);
1034 win32_fputc(int c,FILE *pf)
1040 win32_ungetc(int c,FILE *pf)
1042 return ungetc(c,pf);
1046 win32_getc(FILE *pf)
1052 win32_fileno(FILE *pf)
1058 win32_clearerr(FILE *pf)
1065 win32_fflush(FILE *pf)
1071 win32_ftell(FILE *pf)
1077 win32_fseek(FILE *pf,long offset,int origin)
1079 return fseek(pf, offset, origin);
1083 win32_fgetpos(FILE *pf,fpos_t *p)
1085 return fgetpos(pf, p);
1089 win32_fsetpos(FILE *pf,const fpos_t *p)
1091 return fsetpos(pf, p);
1095 win32_rewind(FILE *pf)
1115 win32_fstat(int fd,struct stat *bufptr)
1117 return fstat(fd,bufptr);
1121 win32_pipe(int *pfd, unsigned int size, int mode)
1123 return _pipe(pfd, size, mode);
1127 win32_popen(const char *command, const char *mode)
1132 return _popen(command, mode);
1137 win32_pclose(FILE *pf)
1147 win32_setmode(int fd, int mode)
1149 return setmode(fd, mode);
1153 win32_lseek(int fd, long offset, int origin)
1155 return lseek(fd, offset, origin);
1165 win32_open(const char *path, int flag, ...)
1171 pmode = va_arg(ap, int);
1174 if (stricmp(path, "/dev/null")==0)
1175 return open("NUL", flag, pmode);
1176 return open(path,flag,pmode);
1198 win32_dup2(int fd1,int fd2)
1200 return dup2(fd1,fd2);
1204 win32_read(int fd, void *buf, unsigned int cnt)
1206 return read(fd, buf, cnt);
1210 win32_write(int fd, const void *buf, unsigned int cnt)
1212 return write(fd, buf, cnt);
1216 win32_mkdir(const char *dir, int mode)
1218 return mkdir(dir); /* just ignore mode */
1222 win32_rmdir(const char *dir)
1228 win32_chdir(const char *dir)
1234 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1236 return spawnvp(mode, cmdname, (char * const *) argv);
1240 win32_execvp(const char *cmdname, const char *const *argv)
1242 return execvp(cmdname, (char *const *)argv);
1246 win32_perror(const char *str)
1252 win32_setbuf(FILE *pf, char *buf)
1258 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
1260 return setvbuf(pf, buf, type, size);
1264 win32_flushall(void)
1272 win32_fcloseall(void)
1280 win32_fgets(char *s, int n, FILE *pf)
1282 return fgets(s, n, pf);
1292 win32_fgetc(FILE *pf)
1298 win32_putc(int c, FILE *pf)
1304 win32_puts(const char *s)
1316 win32_putchar(int c)
1323 #ifndef USE_PERL_SBRK
1325 static char *committed = NULL;
1326 static char *base = NULL;
1327 static char *reserved = NULL;
1328 static char *brk = NULL;
1329 static DWORD pagesize = 0;
1330 static DWORD allocsize = 0;
1338 GetSystemInfo(&info);
1339 /* Pretend page size is larger so we don't perpetually
1340 * call the OS to commit just one page ...
1342 pagesize = info.dwPageSize << 3;
1343 allocsize = info.dwAllocationGranularity;
1345 /* This scheme fails eventually if request for contiguous
1346 * block is denied so reserve big blocks - this is only
1347 * address space not memory ...
1349 if (brk+need >= reserved)
1351 DWORD size = 64*1024*1024;
1353 if (committed && reserved && committed < reserved)
1355 /* Commit last of previous chunk cannot span allocations */
1356 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
1358 committed = reserved;
1360 /* Reserve some (more) space
1361 * Note this is a little sneaky, 1st call passes NULL as reserved
1362 * so lets system choose where we start, subsequent calls pass
1363 * the old end address so ask for a contiguous block
1365 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
1368 reserved = addr+size;
1383 if (brk > committed)
1385 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
1386 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
1401 win32_malloc(size_t size)
1403 return malloc(size);
1407 win32_calloc(size_t numitems, size_t size)
1409 return calloc(numitems,size);
1413 win32_realloc(void *block, size_t size)
1415 return realloc(block,size);
1419 win32_free(void *block)
1426 win32_open_osfhandle(long handle, int flags)
1428 return _open_osfhandle(handle, flags);
1432 win32_get_osfhandle(int fd)
1434 return _get_osfhandle(fd);
1445 SV *sv = sv_newmortal();
1446 /* Make one call with zero size - return value is required size */
1447 DWORD len = GetCurrentDirectory((DWORD)0,NULL);
1448 SvUPGRADE(sv,SVt_PV);
1450 SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
1453 * then it worked, set PV valid,
1454 * else leave it 'undef'
1468 croak("usage: Win32::SetCurrentDirectory($cwd)");
1469 if (SetCurrentDirectory(SvPV(ST(0),na)))
1476 XS(w32_GetNextAvailDrive)
1480 char root[] = "_:\\";
1483 if (GetDriveType(root) == 1) {
1492 XS(w32_GetLastError)
1495 XSRETURN_IV(GetLastError());
1503 DWORD size = sizeof(name);
1504 if (GetUserName(name,&size)) {
1505 /* size includes NULL */
1506 ST(0) = sv_2mortal(newSVpv(name,size-1));
1516 char name[MAX_COMPUTERNAME_LENGTH+1];
1517 DWORD size = sizeof(name);
1518 if (GetComputerName(name,&size)) {
1519 /* size does NOT include NULL :-( */
1520 ST(0) = sv_2mortal(newSVpv(name,size));
1532 DWORD size = sizeof(name);
1533 if (GetUserName(name,&size)) {
1535 DWORD sidlen = sizeof(sid);
1537 DWORD dnamelen = sizeof(dname);
1539 if (LookupAccountName(NULL, name, &sid, &sidlen,
1540 dname, &dnamelen, &snu)) {
1541 XSRETURN_PV(dname); /* all that for this */
1552 DWORD flags, filecomplen;
1553 if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
1554 &flags, fsname, sizeof(fsname))) {
1555 if (GIMME == G_ARRAY) {
1556 XPUSHs(sv_2mortal(newSVpv(fsname,0)));
1557 XPUSHs(sv_2mortal(newSViv(flags)));
1558 XPUSHs(sv_2mortal(newSViv(filecomplen)));
1562 XSRETURN_PV(fsname);
1568 XS(w32_GetOSVersion)
1571 OSVERSIONINFO osver;
1573 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1574 if (GetVersionEx(&osver)) {
1575 XPUSHs(newSVpv(osver.szCSDVersion, 0));
1576 XPUSHs(newSViv(osver.dwMajorVersion));
1577 XPUSHs(newSViv(osver.dwMinorVersion));
1578 XPUSHs(newSViv(osver.dwBuildNumber));
1579 XPUSHs(newSViv(osver.dwPlatformId));
1590 XSRETURN_IV(IsWinNT());
1597 XSRETURN_IV(IsWin95());
1601 XS(w32_FormatMessage)
1608 croak("usage: Win32::FormatMessage($errno)");
1610 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1611 &source, SvIV(ST(0)), 0,
1612 msgbuf, sizeof(msgbuf)-1, NULL))
1613 XSRETURN_PV(msgbuf);
1623 PROCESS_INFORMATION stProcInfo;
1624 STARTUPINFO stStartInfo;
1625 BOOL bSuccess = FALSE;
1628 croak("usage: Win32::Spawn($cmdName, $args, $PID)");
1630 cmd = SvPV(ST(0),na);
1631 args = SvPV(ST(1), na);
1633 memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */
1634 stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */
1635 stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */
1636 stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */
1639 cmd, /* Image path */
1640 args, /* Arguments for command line */
1641 NULL, /* Default process security */
1642 NULL, /* Default thread security */
1643 FALSE, /* Must be TRUE to use std handles */
1644 NORMAL_PRIORITY_CLASS, /* No special scheduling */
1645 NULL, /* Inherit our environment block */
1646 NULL, /* Inherit our currrent directory */
1647 &stStartInfo, /* -> Startup info */
1648 &stProcInfo)) /* <- Process info (if OK) */
1650 CloseHandle(stProcInfo.hThread);/* library source code does this. */
1651 sv_setiv(ST(2), stProcInfo.dwProcessId);
1654 XSRETURN_IV(bSuccess);
1658 XS(w32_GetTickCount)
1661 XSRETURN_IV(GetTickCount());
1665 XS(w32_GetShortPathName)
1672 croak("usage: Win32::GetShortPathName($longPathName)");
1674 shortpath = sv_mortalcopy(ST(0));
1675 SvUPGRADE(shortpath, SVt_PV);
1676 /* src == target is allowed */
1678 len = GetShortPathName(SvPVX(shortpath),
1681 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
1683 SvCUR_set(shortpath,len);
1692 Perl_init_os_extras()
1694 char *file = __FILE__;
1697 /* XXX should be removed after checking with Nick */
1698 newXS("Win32::GetCurrentDirectory", w32_GetCwd, file);
1700 /* these names are Activeware compatible */
1701 newXS("Win32::GetCwd", w32_GetCwd, file);
1702 newXS("Win32::SetCwd", w32_SetCwd, file);
1703 newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
1704 newXS("Win32::GetLastError", w32_GetLastError, file);
1705 newXS("Win32::LoginName", w32_LoginName, file);
1706 newXS("Win32::NodeName", w32_NodeName, file);
1707 newXS("Win32::DomainName", w32_DomainName, file);
1708 newXS("Win32::FsType", w32_FsType, file);
1709 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
1710 newXS("Win32::IsWinNT", w32_IsWinNT, file);
1711 newXS("Win32::IsWin95", w32_IsWin95, file);
1712 newXS("Win32::FormatMessage", w32_FormatMessage, file);
1713 newXS("Win32::Spawn", w32_Spawn, file);
1714 newXS("Win32::GetTickCount", w32_GetTickCount, file);
1715 newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
1717 /* XXX Bloat Alert! The following Activeware preloads really
1718 * ought to be part of Win32::Sys::*, so they're not included
1721 /* LookupAccountName
1723 * InitiateSystemShutdown
1724 * AbortSystemShutdown
1725 * ExpandEnvrironmentStrings
1730 Perl_win32_init(int *argcp, char ***argvp)
1732 /* Disable floating point errors, Perl will trap the ones we
1733 * care about. VC++ RTL defaults to switching these off
1734 * already, but the Borland RTL doesn't. Since we don't
1735 * want to be at the vendor's whim on the default, we set
1736 * it explicitly here.
1738 #if !defined(_ALPHA_) && !defined(__GNUC__)
1739 _control87(MCW_EM, MCW_EM);
1743 #ifdef USE_BINMODE_SCRIPTS
1746 win32_strip_return(SV *sv)
1748 char *s = SvPVX(sv);
1749 char *e = s+SvCUR(sv);
1753 if (*s == '\r' && s[1] == '\n')
1763 SvCUR_set(sv,d-SvPVX(sv));