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)
35 #define EXECF_SPAWN_NOWAIT 3
37 static DWORD IdOS(void);
39 BOOL ProbeEnv = FALSE;
40 DWORD Win32System = (DWORD)-1;
41 char szShellPath[MAX_PATH+1];
42 char szPerlLibRoot[MAX_PATH+1];
43 HANDLE PerlDllHandle = INVALID_HANDLE_VALUE;
45 static int do_spawn2(char *cmd, int exectype);
49 return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
54 return (IdOS() == VER_PLATFORM_WIN32_NT);
58 win32PerlLibPath(char *sfx,...)
63 GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE)
64 ? GetModuleHandle(NULL)
67 sizeof(szPerlLibRoot));
68 *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
69 if (stricmp(end-4,"\\bin") == 0)
76 sfx = va_arg(ap,char *);
79 return (szPerlLibRoot);
84 HasRedirection(char *ptr)
90 * Scan string looking for redirection (< or >) or pipe
91 * characters (|) that are not in a quoted string
121 /* since the current process environment is being updated in util.c
122 * the library functions will get the correct environment
125 my_popen(char *cmd, char *mode)
128 #define fixcmd(x) { \
129 char *pspace = strchr((x),' '); \
132 while (p < pspace) { \
143 #ifdef __BORLANDC__ /* workaround a Borland stdio bug */
144 win32_fflush(stdout);
145 win32_fflush(stderr);
147 return win32_popen(cmd, mode);
151 my_pclose(PerlIO *fp)
153 return win32_pclose(fp);
159 static OSVERSIONINFO osver;
161 if (osver.dwPlatformId != Win32System) {
162 memset(&osver, 0, sizeof(OSVERSIONINFO));
163 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
164 GetVersionEx(&osver);
165 Win32System = osver.dwPlatformId;
167 return (Win32System);
174 char* defaultshell = (IsWinNT() ? "cmd.exe" : "command.com");
175 /* we don't use COMSPEC here for two reasons:
176 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
177 * uncontrolled unportability of the ensuing scripts.
178 * 2. PERL5SHELL could be set to a shell that may not be fit for
179 * interactive use (which is what most programs look in COMSPEC
182 char *usershell = getenv("PERL5SHELL");
185 strcpy(szShellPath, usershell ? usershell : defaultshell);
191 do_aspawn(void* really, void ** mark, void ** arglast)
199 SV *sv = (SV*)really;
200 SV** pSv = (SV**)mark;
202 New(1310, argv, (arglast - mark) + 4, char*);
205 cmd = SvPV(sv, length);
208 argv[index++] = cmd = GetShell();
210 argv[index++] = "/x"; /* always enable command extensions */
211 argv[index++] = "/c";
214 while(++pSv <= (SV**)arglast) {
216 strPtr = SvPV(sv, length);
217 if(strPtr != NULL && *strPtr != '\0')
218 argv[index++] = strPtr;
222 status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv);
228 warn("Can't spawn \"%s\": %s", cmd, strerror(errno));
235 do_spawn2(char *cmd, int exectype)
241 BOOL needToTry = TRUE;
244 /* save an extra exec if possible */
247 /* see if there are shell metacharacters in it */
248 if(!HasRedirection(cmd)) {
249 New(1301,argv, strlen(cmd) / 2 + 2, char*);
250 New(1302,cmd2, strlen(cmd) + 1, char);
253 for (s = cmd2; *s;) {
254 while (*s && isspace(*s))
258 while(*s && !isspace(*s))
267 status = win32_spawnvp(P_WAIT, argv[0],
268 (const char* const*)argv);
270 case EXECF_SPAWN_NOWAIT:
271 status = win32_spawnvp(P_NOWAIT, argv[0],
272 (const char* const*)argv);
275 status = win32_execvp(argv[0], (const char* const*)argv);
278 if(status != -1 || errno == 0)
290 argv[i++] = "/c"; argv[i++] = cmd; argv[i] = Nullch;
293 status = win32_spawnvp(P_WAIT, argv[0],
294 (const char* const*)argv);
296 case EXECF_SPAWN_NOWAIT:
297 status = win32_spawnvp(P_NOWAIT, argv[0],
298 (const char* const*)argv);
301 status = win32_execvp(argv[0], (const char* const*)argv);
307 warn("Can't %s \"%s\": %s",
308 (exectype == EXECF_EXEC ? "exec" : "spawn"),
309 needToTry ? shell : argv[0],
319 return do_spawn2(cmd, EXECF_SPAWN);
325 do_spawn2(cmd, EXECF_EXEC);
332 /* The idea here is to read all the directory names into a string table
333 * (separated by nulls) and when one of the other dir functions is called
334 * return the pointer to the current file name.
337 opendir(char *filename)
342 char scannamespc[PATHLEN];
343 char *scanname = scannamespc;
345 WIN32_FIND_DATA FindData;
347 /* char root[_MAX_PATH];*/
348 /* char volname[_MAX_PATH];*/
349 /* DWORD serial, maxname, flags;*/
353 /* check to see if filename is a directory */
354 if (win32_stat(filename, &sbuf) < 0 || (sbuf.st_mode & S_IFDIR) == 0) {
358 /* get the file system characteristics */
359 /* if(GetFullPathName(filename, MAX_PATH, root, &dummy)) {
360 * if(dummy = strchr(root, '\\'))
362 * if(GetVolumeInformation(root, volname, MAX_PATH, &serial,
363 * &maxname, &flags, 0, 0)) {
364 * downcase = !(flags & FS_CASE_IS_PRESERVED);
371 /* Get us a DIR structure */
372 Newz(1303, p, 1, DIR);
376 /* Create the search pattern */
377 strcpy(scanname, filename);
379 if(index("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
380 strcat(scanname, "/*");
382 strcat(scanname, "*");
384 /* do the FindFirstFile call */
385 fh = FindFirstFile(scanname, &FindData);
386 if(fh == INVALID_HANDLE_VALUE) {
390 /* now allocate the first part of the string table for
391 * the filenames that we find.
393 idx = strlen(FindData.cFileName)+1;
394 New(1304, p->start, idx, char);
395 if(p->start == NULL) {
396 croak("opendir: malloc failed!\n");
398 strcpy(p->start, FindData.cFileName);
404 /* loop finding all the files that match the wildcard
405 * (which should be all of them in this directory!).
406 * the variable idx should point one past the null terminator
407 * of the previous string found.
409 while (FindNextFile(fh, &FindData)) {
410 len = strlen(FindData.cFileName);
411 /* bump the string table size by enough for the
412 * new name and it's null terminator
414 Renew(p->start, idx+len+1, char);
415 if(p->start == NULL) {
416 croak("opendir: malloc failed!\n");
418 strcpy(&p->start[idx], FindData.cFileName);
420 * strlwr(&p->start[idx]);
432 /* Readdir just returns the current string pointer and bumps the
433 * string pointer to the nDllExport entry.
439 static int dummy = 0;
442 /* first set up the structure to return */
443 len = strlen(dirp->curr);
444 strcpy(dirp->dirstr.d_name, dirp->curr);
445 dirp->dirstr.d_namlen = len;
448 dirp->dirstr.d_ino = dummy++;
450 /* Now set up for the nDllExport call to readdir */
451 dirp->curr += len + 1;
452 if (dirp->curr >= (dirp->start + dirp->size)) {
456 return &(dirp->dirstr);
462 /* Telldir returns the current string pointer position */
466 return (long) dirp->curr;
470 /* Seekdir moves the string pointer to a previously saved position
474 seekdir(DIR *dirp, long loc)
476 dirp->curr = (char *)loc;
479 /* Rewinddir resets the string pointer to the start */
483 dirp->curr = dirp->start;
486 /* free the memory allocated by opendir */
490 Safefree(dirp->start);
503 * Just pretend that everyone is a superuser. NT will let us know if
504 * we don\'t really have permission to do something.
507 #define ROOT_UID ((uid_t)0)
508 #define ROOT_GID ((gid_t)0)
537 return (uid == ROOT_UID ? 0 : -1);
543 return (gid == ROOT_GID ? 0 : -1);
550 kill(int pid, int sig)
552 HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
554 if (hProcess == NULL) {
555 croak("kill process failed!\n");
558 if (!TerminateProcess(hProcess, sig))
559 croak("kill process failed!\n");
560 CloseHandle(hProcess);
571 ioctl(int i, unsigned int u, char *data)
573 croak("ioctl not implemented!\n");
578 DllExport unsigned int
579 win32_sleep(unsigned int t)
586 win32_stat(const char *path, struct stat *buffer)
589 const char *p = path;
590 int l = strlen(path);
594 switch(path[l - 1]) {
597 if (path[l - 2] != ':') {
598 strncpy(t, path, l - 1);
604 res = stat(p,buffer);
607 if (S_ISDIR(buffer->st_mode))
608 buffer->st_mode |= S_IWRITE | S_IEXEC;
609 else if (S_ISREG(buffer->st_mode)) {
610 if (l >= 4 && path[l-4] == '.') {
611 const char *e = path + l - 3;
612 if (strnicmp(e,"exe",3)
613 && strnicmp(e,"bat",3)
614 && strnicmp(e,"com",3)
615 && (IsWin95() || strnicmp(e,"cmd",3)))
616 buffer->st_mode &= ~S_IEXEC;
618 buffer->st_mode |= S_IEXEC;
621 buffer->st_mode &= ~S_IEXEC;
628 #ifndef USE_WIN32_RTL_ENV
631 win32_getenv(const char *name)
633 static char *curitem = Nullch;
634 static DWORD curlen = 512;
637 New(1305,curitem,curlen,char);
638 if (!(needlen = GetEnvironmentVariable(name,curitem,curlen)))
640 while (needlen > curlen) {
641 Renew(curitem,needlen,char);
643 needlen = GetEnvironmentVariable(name,curitem,curlen);
651 FileTimeToClock(PFILETIME ft)
653 __int64 qw = ft->dwHighDateTime;
655 qw |= ft->dwLowDateTime;
656 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
661 win32_times(struct tms *timebuf)
666 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
668 timebuf->tms_utime = FileTimeToClock(&user);
669 timebuf->tms_stime = FileTimeToClock(&kernel);
670 timebuf->tms_cutime = 0;
671 timebuf->tms_cstime = 0;
674 /* That failed - e.g. Win95 fallback to clock() */
676 timebuf->tms_utime = t;
677 timebuf->tms_stime = 0;
678 timebuf->tms_cutime = 0;
679 timebuf->tms_cstime = 0;
684 static UINT timerid = 0;
687 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
689 KillTimer(NULL,timerid);
694 DllExport unsigned int
695 win32_alarm(unsigned int sec)
698 * the 'obvious' implentation is SetTimer() with a callback
699 * which does whatever receiving SIGALRM would do
700 * we cannot use SIGALRM even via raise() as it is not
701 * one of the supported codes in <signal.h>
703 * Snag is unless something is looking at the message queue
704 * nothing happens :-(
708 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
710 croak("Cannot set timer");
716 KillTimer(NULL,timerid);
723 #ifdef USE_FIXED_OSFHANDLE
725 EXTERN_C int __cdecl _alloc_osfhnd(void);
726 EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
727 EXTERN_C void __cdecl _lock_fhandle(int);
728 EXTERN_C void __cdecl _unlock_fhandle(int);
729 EXTERN_C void __cdecl _unlock(int);
731 #if (_MSC_VER >= 1000)
733 long osfhnd; /* underlying OS file HANDLE */
734 char osfile; /* attributes of file (e.g., open in text mode?) */
735 char pipech; /* one char buffer for handles opened on pipes */
736 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
738 CRITICAL_SECTION lock;
739 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
742 EXTERN_C ioinfo * __pioinfo[];
745 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
746 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
747 #define _osfile(i) (_pioinfo(i)->osfile)
749 #else /* (_MSC_VER >= 1000) */
750 extern char _osfile[];
751 #endif /* (_MSC_VER >= 1000) */
753 #define FOPEN 0x01 /* file handle open */
754 #define FAPPEND 0x20 /* file handle opened O_APPEND */
755 #define FDEV 0x40 /* file handle refers to device */
756 #define FTEXT 0x80 /* file handle is in text mode */
758 #define _STREAM_LOCKS 26 /* Table of stream locks */
759 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */
760 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */
763 *int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
766 * This function allocates a free C Runtime file handle and associates
767 * it with the Win32 HANDLE specified by the first parameter. This is a
768 * temperary fix for WIN95's brain damage GetFileType() error on socket
769 * we just bypass that call for socket
772 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
773 * int flags - flags to associate with C Runtime file handle.
776 * returns index of entry in fh, if successful
777 * return -1, if no free entry is found
781 *******************************************************************************/
784 my_open_osfhandle(long osfhandle, int flags)
787 char fileflags; /* _osfile flags */
789 /* copy relevant flags from second parameter */
793 fileflags |= FAPPEND;
798 /* attempt to allocate a C Runtime file handle */
799 if((fh = _alloc_osfhnd()) == -1) {
800 errno = EMFILE; /* too many open files */
801 _doserrno = 0L; /* not an OS error */
802 return -1; /* return error to caller */
805 /* the file is open. now, set the info in _osfhnd array */
806 _set_osfhnd(fh, osfhandle);
808 fileflags |= FOPEN; /* mark as open */
810 #if (_MSC_VER >= 1000)
811 _osfile(fh) = fileflags; /* set osfile entry */
814 _osfile[fh] = fileflags; /* set osfile entry */
815 _unlock(fh+_FH_LOCKS); /* unlock handle */
818 return fh; /* return handle */
821 #define _open_osfhandle my_open_osfhandle
822 #endif /* USE_FIXED_OSFHANDLE */
824 /* simulate flock by locking a range on the file */
826 #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
827 #define LK_LEN 0xffff0000
830 win32_flock(int fd, int oper)
837 croak("flock() unimplemented on this platform");
840 fh = (HANDLE)_get_osfhandle(fd);
841 memset(&o, 0, sizeof(o));
844 case LOCK_SH: /* shared lock */
845 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
847 case LOCK_EX: /* exclusive lock */
848 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
850 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
851 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
853 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
854 LK_ERR(LockFileEx(fh,
855 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
856 0, LK_LEN, 0, &o),i);
858 case LOCK_UN: /* unlock lock */
859 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
861 default: /* unknown */
872 * redirected io subsystem for all XS modules
885 return (&(_environ));
888 /* the rest are the remapped stdio routines */
908 win32_ferror(FILE *fp)
921 * Since the errors returned by the socket error function
922 * WSAGetLastError() are not known by the library routine strerror
923 * we have to roll our own.
926 __declspec(thread) char strerror_buffer[512];
929 win32_strerror(int e)
931 #ifndef __BORLANDC__ /* Borland intolerance */
936 if(e < 0 || e > sys_nerr) {
940 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
941 strerror_buffer, sizeof(strerror_buffer), NULL) == 0)
942 strcpy(strerror_buffer, "Unknown Error");
944 return strerror_buffer;
950 win32_fprintf(FILE *fp, const char *format, ...)
953 va_start(marker, format); /* Initialize variable arguments. */
955 return (vfprintf(fp, format, marker));
959 win32_printf(const char *format, ...)
962 va_start(marker, format); /* Initialize variable arguments. */
964 return (vprintf(format, marker));
968 win32_vfprintf(FILE *fp, const char *format, va_list args)
970 return (vfprintf(fp, format, args));
974 win32_vprintf(const char *format, va_list args)
976 return (vprintf(format, args));
980 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
982 return fread(buf, size, count, fp);
986 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
988 return fwrite(buf, size, count, fp);
992 win32_fopen(const char *filename, const char *mode)
994 if (stricmp(filename, "/dev/null")==0)
995 return fopen("NUL", mode);
996 return fopen(filename, mode);
999 #ifndef USE_SOCKETS_AS_HANDLES
1001 #define fdopen my_fdopen
1005 win32_fdopen( int handle, const char *mode)
1007 return fdopen(handle, (char *) mode);
1011 win32_freopen( const char *path, const char *mode, FILE *stream)
1013 if (stricmp(path, "/dev/null")==0)
1014 return freopen("NUL", mode, stream);
1015 return freopen(path, mode, stream);
1019 win32_fclose(FILE *pf)
1021 return my_fclose(pf); /* defined in win32sck.c */
1025 win32_fputs(const char *s,FILE *pf)
1027 return fputs(s, pf);
1031 win32_fputc(int c,FILE *pf)
1037 win32_ungetc(int c,FILE *pf)
1039 return ungetc(c,pf);
1043 win32_getc(FILE *pf)
1049 win32_fileno(FILE *pf)
1055 win32_clearerr(FILE *pf)
1062 win32_fflush(FILE *pf)
1068 win32_ftell(FILE *pf)
1074 win32_fseek(FILE *pf,long offset,int origin)
1076 return fseek(pf, offset, origin);
1080 win32_fgetpos(FILE *pf,fpos_t *p)
1082 return fgetpos(pf, p);
1086 win32_fsetpos(FILE *pf,const fpos_t *p)
1088 return fsetpos(pf, p);
1092 win32_rewind(FILE *pf)
1112 win32_fstat(int fd,struct stat *bufptr)
1114 return fstat(fd,bufptr);
1118 win32_pipe(int *pfd, unsigned int size, int mode)
1120 return _pipe(pfd, size, mode);
1124 win32_popen(const char *command, const char *mode)
1126 return _popen(command, mode);
1130 win32_pclose(FILE *pf)
1136 win32_setmode(int fd, int mode)
1138 return setmode(fd, mode);
1142 win32_lseek(int fd, long offset, int origin)
1144 return lseek(fd, offset, origin);
1154 win32_open(const char *path, int flag, ...)
1160 pmode = va_arg(ap, int);
1163 if (stricmp(path, "/dev/null")==0)
1164 return open("NUL", flag, pmode);
1165 return open(path,flag,pmode);
1187 win32_dup2(int fd1,int fd2)
1189 return dup2(fd1,fd2);
1193 win32_read(int fd, void *buf, unsigned int cnt)
1195 return read(fd, buf, cnt);
1199 win32_write(int fd, const void *buf, unsigned int cnt)
1201 return write(fd, buf, cnt);
1205 win32_mkdir(const char *dir, int mode)
1207 return mkdir(dir); /* just ignore mode */
1211 win32_rmdir(const char *dir)
1217 win32_chdir(const char *dir)
1223 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1225 return spawnvp(mode, cmdname, (char * const *) argv);
1229 win32_execvp(const char *cmdname, const char *const *argv)
1231 return execvp(cmdname, (char *const *)argv);
1235 win32_perror(const char *str)
1241 win32_setbuf(FILE *pf, char *buf)
1247 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
1249 return setvbuf(pf, buf, type, size);
1253 win32_flushall(void)
1259 win32_fcloseall(void)
1265 win32_fgets(char *s, int n, FILE *pf)
1267 return fgets(s, n, pf);
1277 win32_fgetc(FILE *pf)
1283 win32_putc(int c, FILE *pf)
1289 win32_puts(const char *s)
1301 win32_putchar(int c)
1308 #ifndef USE_PERL_SBRK
1310 static char *committed = NULL;
1311 static char *base = NULL;
1312 static char *reserved = NULL;
1313 static char *brk = NULL;
1314 static DWORD pagesize = 0;
1315 static DWORD allocsize = 0;
1323 GetSystemInfo(&info);
1324 /* Pretend page size is larger so we don't perpetually
1325 * call the OS to commit just one page ...
1327 pagesize = info.dwPageSize << 3;
1328 allocsize = info.dwAllocationGranularity;
1330 /* This scheme fails eventually if request for contiguous
1331 * block is denied so reserve big blocks - this is only
1332 * address space not memory ...
1334 if (brk+need >= reserved)
1336 DWORD size = 64*1024*1024;
1338 if (committed && reserved && committed < reserved)
1340 /* Commit last of previous chunk cannot span allocations */
1341 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
1343 committed = reserved;
1345 /* Reserve some (more) space
1346 * Note this is a little sneaky, 1st call passes NULL as reserved
1347 * so lets system choose where we start, subsequent calls pass
1348 * the old end address so ask for a contiguous block
1350 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
1353 reserved = addr+size;
1368 if (brk > committed)
1370 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
1371 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
1386 win32_malloc(size_t size)
1388 return malloc(size);
1392 win32_calloc(size_t numitems, size_t size)
1394 return calloc(numitems,size);
1398 win32_realloc(void *block, size_t size)
1400 return realloc(block,size);
1404 win32_free(void *block)
1411 win32_open_osfhandle(long handle, int flags)
1413 return _open_osfhandle(handle, flags);
1417 win32_get_osfhandle(int fd)
1419 return _get_osfhandle(fd);
1430 SV *sv = sv_newmortal();
1431 /* Make one call with zero size - return value is required size */
1432 DWORD len = GetCurrentDirectory((DWORD)0,NULL);
1433 SvUPGRADE(sv,SVt_PV);
1435 SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
1438 * then it worked, set PV valid,
1439 * else leave it 'undef'
1453 croak("usage: Win32::SetCurrentDirectory($cwd)");
1454 if (SetCurrentDirectory(SvPV(ST(0),na)))
1461 XS(w32_GetNextAvailDrive)
1465 char root[] = "_:\\";
1468 if (GetDriveType(root) == 1) {
1477 XS(w32_GetLastError)
1480 XSRETURN_IV(GetLastError());
1488 DWORD size = sizeof(name);
1489 if (GetUserName(name,&size)) {
1490 /* size includes NULL */
1491 ST(0) = sv_2mortal(newSVpv(name,size-1));
1501 char name[MAX_COMPUTERNAME_LENGTH+1];
1502 DWORD size = sizeof(name);
1503 if (GetComputerName(name,&size)) {
1504 /* size does NOT include NULL :-( */
1505 ST(0) = sv_2mortal(newSVpv(name,size));
1517 DWORD size = sizeof(name);
1518 if (GetUserName(name,&size)) {
1520 DWORD sidlen = sizeof(sid);
1522 DWORD dnamelen = sizeof(dname);
1524 if (LookupAccountName(NULL, name, &sid, &sidlen,
1525 dname, &dnamelen, &snu)) {
1526 XSRETURN_PV(dname); /* all that for this */
1537 DWORD flags, filecomplen;
1538 if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
1539 &flags, fsname, sizeof(fsname))) {
1540 if (GIMME == G_ARRAY) {
1541 XPUSHs(sv_2mortal(newSVpv(fsname,0)));
1542 XPUSHs(sv_2mortal(newSViv(flags)));
1543 XPUSHs(sv_2mortal(newSViv(filecomplen)));
1547 XSRETURN_PV(fsname);
1553 XS(w32_GetOSVersion)
1556 OSVERSIONINFO osver;
1558 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1559 if (GetVersionEx(&osver)) {
1560 XPUSHs(newSVpv(osver.szCSDVersion, 0));
1561 XPUSHs(newSViv(osver.dwMajorVersion));
1562 XPUSHs(newSViv(osver.dwMinorVersion));
1563 XPUSHs(newSViv(osver.dwBuildNumber));
1564 XPUSHs(newSViv(osver.dwPlatformId));
1575 XSRETURN_IV(IsWinNT());
1582 XSRETURN_IV(IsWin95());
1586 XS(w32_FormatMessage)
1593 croak("usage: Win32::FormatMessage($errno)");
1595 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1596 &source, SvIV(ST(0)), 0,
1597 msgbuf, sizeof(msgbuf)-1, NULL))
1598 XSRETURN_PV(msgbuf);
1608 PROCESS_INFORMATION stProcInfo;
1609 STARTUPINFO stStartInfo;
1610 BOOL bSuccess = FALSE;
1613 croak("usage: Win32::Spawn($cmdName, $args, $PID)");
1615 cmd = SvPV(ST(0),na);
1616 args = SvPV(ST(1), na);
1618 memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */
1619 stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */
1620 stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */
1621 stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */
1624 cmd, /* Image path */
1625 args, /* Arguments for command line */
1626 NULL, /* Default process security */
1627 NULL, /* Default thread security */
1628 FALSE, /* Must be TRUE to use std handles */
1629 NORMAL_PRIORITY_CLASS, /* No special scheduling */
1630 NULL, /* Inherit our environment block */
1631 NULL, /* Inherit our currrent directory */
1632 &stStartInfo, /* -> Startup info */
1633 &stProcInfo)) /* <- Process info (if OK) */
1635 CloseHandle(stProcInfo.hThread);/* library source code does this. */
1636 sv_setiv(ST(2), stProcInfo.dwProcessId);
1639 XSRETURN_IV(bSuccess);
1643 XS(w32_GetTickCount)
1646 XSRETURN_IV(GetTickCount());
1650 XS(w32_GetShortPathName)
1657 croak("usage: Win32::GetShortPathName($longPathName)");
1659 shortpath = sv_mortalcopy(ST(0));
1660 SvUPGRADE(shortpath, SVt_PV);
1661 /* src == target is allowed */
1663 len = GetShortPathName(SvPVX(shortpath),
1666 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
1668 SvCUR_set(shortpath,len);
1677 Perl_init_os_extras()
1679 char *file = __FILE__;
1682 /* XXX should be removed after checking with Nick */
1683 newXS("Win32::GetCurrentDirectory", w32_GetCwd, file);
1685 /* these names are Activeware compatible */
1686 newXS("Win32::GetCwd", w32_GetCwd, file);
1687 newXS("Win32::SetCwd", w32_SetCwd, file);
1688 newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
1689 newXS("Win32::GetLastError", w32_GetLastError, file);
1690 newXS("Win32::LoginName", w32_LoginName, file);
1691 newXS("Win32::NodeName", w32_NodeName, file);
1692 newXS("Win32::DomainName", w32_DomainName, file);
1693 newXS("Win32::FsType", w32_FsType, file);
1694 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
1695 newXS("Win32::IsWinNT", w32_IsWinNT, file);
1696 newXS("Win32::IsWin95", w32_IsWin95, file);
1697 newXS("Win32::FormatMessage", w32_FormatMessage, file);
1698 newXS("Win32::Spawn", w32_Spawn, file);
1699 newXS("Win32::GetTickCount", w32_GetTickCount, file);
1700 newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
1702 /* XXX Bloat Alert! The following Activeware preloads really
1703 * ought to be part of Win32::Sys::*, so they're not included
1706 /* LookupAccountName
1708 * InitiateSystemShutdown
1709 * AbortSystemShutdown
1710 * ExpandEnvrironmentStrings
1715 Perl_win32_init(int *argcp, char ***argvp)
1717 /* Disable floating point errors, Perl will trap the ones we
1718 * care about. VC++ RTL defaults to switching these off
1719 * already, but the Borland RTL doesn't. Since we don't
1720 * want to be at the vendor's whim on the default, we set
1721 * it explicitly here.
1723 #if !defined(_ALPHA_)
1724 _control87(MCW_EM, MCW_EM);
1728 #ifdef USE_BINMODE_SCRIPTS
1731 win32_strip_return(SV *sv)
1733 char *s = SvPVX(sv);
1734 char *e = s+SvCUR(sv);
1738 if (*s == '\r' && s[1] == '\n')
1748 SvCUR_set(sv,d-SvPVX(sv));