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;
58 static int do_spawn2(char *cmd, int exectype);
62 return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
67 return (IdOS() == VER_PLATFORM_WIN32_NT);
71 win32PerlLibPath(char *sfx,...)
76 GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE)
77 ? GetModuleHandle(NULL)
80 sizeof(szPerlLibRoot));
81 *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
82 if (stricmp(end-4,"\\bin") == 0)
89 sfx = va_arg(ap,char *);
92 return (szPerlLibRoot);
97 HasRedirection(char *ptr)
103 * Scan string looking for redirection (< or >) or pipe
104 * characters (|) that are not in a quoted string
134 /* since the current process environment is being updated in util.c
135 * the library functions will get the correct environment
138 my_popen(char *cmd, char *mode)
141 #define fixcmd(x) { \
142 char *pspace = strchr((x),' '); \
145 while (p < pspace) { \
156 #ifdef __BORLANDC__ /* workaround a Borland stdio bug */
157 win32_fflush(stdout);
158 win32_fflush(stderr);
160 return win32_popen(cmd, mode);
164 my_pclose(PerlIO *fp)
166 return win32_pclose(fp);
172 static OSVERSIONINFO osver;
174 if (osver.dwPlatformId != Win32System) {
175 memset(&osver, 0, sizeof(OSVERSIONINFO));
176 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
177 GetVersionEx(&osver);
178 Win32System = osver.dwPlatformId;
180 return (Win32System);
187 char* defaultshell = (IsWinNT() ? "cmd.exe" : "command.com");
188 /* we don't use COMSPEC here for two reasons:
189 * 1. the same reason perl on UNIX doesn't use SHELL--rampant and
190 * uncontrolled unportability of the ensuing scripts.
191 * 2. PERL5SHELL could be set to a shell that may not be fit for
192 * interactive use (which is what most programs look in COMSPEC
195 char *usershell = getenv("PERL5SHELL");
198 strcpy(szShellPath, usershell ? usershell : defaultshell);
204 do_aspawn(void* really, void ** mark, void ** arglast)
212 SV *sv = (SV*)really;
213 SV** pSv = (SV**)mark;
215 New(1310, argv, (arglast - mark) + 4, char*);
218 cmd = SvPV(sv, length);
221 argv[index++] = cmd = GetShell();
223 argv[index++] = "/x"; /* always enable command extensions */
224 argv[index++] = "/c";
227 while(++pSv <= (SV**)arglast) {
229 strPtr = SvPV(sv, length);
230 if(strPtr != NULL && *strPtr != '\0')
231 argv[index++] = strPtr;
235 status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv);
241 warn("Can't spawn \"%s\": %s", cmd, strerror(errno));
248 do_spawn2(char *cmd, int exectype)
254 BOOL needToTry = TRUE;
257 /* save an extra exec if possible */
260 /* see if there are shell metacharacters in it */
261 if(!HasRedirection(cmd)) {
262 New(1301,argv, strlen(cmd) / 2 + 2, char*);
263 New(1302,cmd2, strlen(cmd) + 1, char);
266 for (s = cmd2; *s;) {
267 while (*s && isspace(*s))
271 while(*s && !isspace(*s))
280 status = win32_spawnvp(P_WAIT, argv[0],
281 (const char* const*)argv);
283 case EXECF_SPAWN_NOWAIT:
284 status = win32_spawnvp(P_NOWAIT, argv[0],
285 (const char* const*)argv);
288 status = win32_execvp(argv[0], (const char* const*)argv);
291 if(status != -1 || errno == 0)
303 argv[i++] = "/c"; argv[i++] = cmd; argv[i] = Nullch;
306 status = win32_spawnvp(P_WAIT, argv[0],
307 (const char* const*)argv);
309 case EXECF_SPAWN_NOWAIT:
310 status = win32_spawnvp(P_NOWAIT, argv[0],
311 (const char* const*)argv);
314 status = win32_execvp(argv[0], (const char* const*)argv);
320 warn("Can't %s \"%s\": %s",
321 (exectype == EXECF_EXEC ? "exec" : "spawn"),
322 needToTry ? shell : argv[0],
332 return do_spawn2(cmd, EXECF_SPAWN);
338 do_spawn2(cmd, EXECF_EXEC);
345 /* The idea here is to read all the directory names into a string table
346 * (separated by nulls) and when one of the other dir functions is called
347 * return the pointer to the current file name.
350 opendir(char *filename)
355 char scannamespc[PATHLEN];
356 char *scanname = scannamespc;
358 WIN32_FIND_DATA FindData;
360 /* char root[_MAX_PATH];*/
361 /* char volname[_MAX_PATH];*/
362 /* DWORD serial, maxname, flags;*/
366 /* check to see if filename is a directory */
367 if (win32_stat(filename, &sbuf) < 0 || (sbuf.st_mode & S_IFDIR) == 0) {
371 /* get the file system characteristics */
372 /* if(GetFullPathName(filename, MAX_PATH, root, &dummy)) {
373 * if(dummy = strchr(root, '\\'))
375 * if(GetVolumeInformation(root, volname, MAX_PATH, &serial,
376 * &maxname, &flags, 0, 0)) {
377 * downcase = !(flags & FS_CASE_IS_PRESERVED);
384 /* Get us a DIR structure */
385 Newz(1303, p, 1, DIR);
389 /* Create the search pattern */
390 strcpy(scanname, filename);
392 if(index("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
393 strcat(scanname, "/*");
395 strcat(scanname, "*");
397 /* do the FindFirstFile call */
398 fh = FindFirstFile(scanname, &FindData);
399 if(fh == INVALID_HANDLE_VALUE) {
403 /* now allocate the first part of the string table for
404 * the filenames that we find.
406 idx = strlen(FindData.cFileName)+1;
407 New(1304, p->start, idx, char);
408 if(p->start == NULL) {
409 croak("opendir: malloc failed!\n");
411 strcpy(p->start, FindData.cFileName);
417 /* loop finding all the files that match the wildcard
418 * (which should be all of them in this directory!).
419 * the variable idx should point one past the null terminator
420 * of the previous string found.
422 while (FindNextFile(fh, &FindData)) {
423 len = strlen(FindData.cFileName);
424 /* bump the string table size by enough for the
425 * new name and it's null terminator
427 Renew(p->start, idx+len+1, char);
428 if(p->start == NULL) {
429 croak("opendir: malloc failed!\n");
431 strcpy(&p->start[idx], FindData.cFileName);
433 * strlwr(&p->start[idx]);
445 /* Readdir just returns the current string pointer and bumps the
446 * string pointer to the nDllExport entry.
452 static int dummy = 0;
455 /* first set up the structure to return */
456 len = strlen(dirp->curr);
457 strcpy(dirp->dirstr.d_name, dirp->curr);
458 dirp->dirstr.d_namlen = len;
461 dirp->dirstr.d_ino = dummy++;
463 /* Now set up for the nDllExport call to readdir */
464 dirp->curr += len + 1;
465 if (dirp->curr >= (dirp->start + dirp->size)) {
469 return &(dirp->dirstr);
475 /* Telldir returns the current string pointer position */
479 return (long) dirp->curr;
483 /* Seekdir moves the string pointer to a previously saved position
487 seekdir(DIR *dirp, long loc)
489 dirp->curr = (char *)loc;
492 /* Rewinddir resets the string pointer to the start */
496 dirp->curr = dirp->start;
499 /* free the memory allocated by opendir */
503 Safefree(dirp->start);
516 * Just pretend that everyone is a superuser. NT will let us know if
517 * we don\'t really have permission to do something.
520 #define ROOT_UID ((uid_t)0)
521 #define ROOT_GID ((gid_t)0)
550 return (auid == ROOT_UID ? 0 : -1);
556 return (agid == ROOT_GID ? 0 : -1);
563 kill(int pid, int sig)
565 HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
567 if (hProcess == NULL) {
568 croak("kill process failed!\n");
571 if (!TerminateProcess(hProcess, sig))
572 croak("kill process failed!\n");
573 CloseHandle(hProcess);
584 ioctl(int i, unsigned int u, char *data)
586 croak("ioctl not implemented!\n");
591 DllExport unsigned int
592 win32_sleep(unsigned int t)
599 win32_stat(const char *path, struct stat *buffer)
602 const char *p = path;
603 int l = strlen(path);
607 switch(path[l - 1]) {
610 if (path[l - 2] != ':') {
611 strncpy(t, path, l - 1);
617 res = stat(p,buffer);
620 if (S_ISDIR(buffer->st_mode))
621 buffer->st_mode |= S_IWRITE | S_IEXEC;
622 else if (S_ISREG(buffer->st_mode)) {
623 if (l >= 4 && path[l-4] == '.') {
624 const char *e = path + l - 3;
625 if (strnicmp(e,"exe",3)
626 && strnicmp(e,"bat",3)
627 && strnicmp(e,"com",3)
628 && (IsWin95() || strnicmp(e,"cmd",3)))
629 buffer->st_mode &= ~S_IEXEC;
631 buffer->st_mode |= S_IEXEC;
634 buffer->st_mode &= ~S_IEXEC;
641 #ifndef USE_WIN32_RTL_ENV
644 win32_getenv(const char *name)
646 static char *curitem = Nullch;
647 static DWORD curlen = 512;
650 New(1305,curitem,curlen,char);
651 if (!(needlen = GetEnvironmentVariable(name,curitem,curlen)))
653 while (needlen > curlen) {
654 Renew(curitem,needlen,char);
656 needlen = GetEnvironmentVariable(name,curitem,curlen);
664 FileTimeToClock(PFILETIME ft)
666 __int64 qw = ft->dwHighDateTime;
668 qw |= ft->dwLowDateTime;
669 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
674 win32_times(struct tms *timebuf)
679 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
681 timebuf->tms_utime = FileTimeToClock(&user);
682 timebuf->tms_stime = FileTimeToClock(&kernel);
683 timebuf->tms_cutime = 0;
684 timebuf->tms_cstime = 0;
687 /* That failed - e.g. Win95 fallback to clock() */
689 timebuf->tms_utime = t;
690 timebuf->tms_stime = 0;
691 timebuf->tms_cutime = 0;
692 timebuf->tms_cstime = 0;
697 static UINT timerid = 0;
700 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
702 KillTimer(NULL,timerid);
707 DllExport unsigned int
708 win32_alarm(unsigned int sec)
711 * the 'obvious' implentation is SetTimer() with a callback
712 * which does whatever receiving SIGALRM would do
713 * we cannot use SIGALRM even via raise() as it is not
714 * one of the supported codes in <signal.h>
716 * Snag is unless something is looking at the message queue
717 * nothing happens :-(
721 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
723 croak("Cannot set timer");
729 KillTimer(NULL,timerid);
736 #ifdef USE_FIXED_OSFHANDLE
738 EXTERN_C int __cdecl _alloc_osfhnd(void);
739 EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
740 EXTERN_C void __cdecl _lock_fhandle(int);
741 EXTERN_C void __cdecl _unlock_fhandle(int);
742 EXTERN_C void __cdecl _unlock(int);
744 #if (_MSC_VER >= 1000)
746 long osfhnd; /* underlying OS file HANDLE */
747 char osfile; /* attributes of file (e.g., open in text mode?) */
748 char pipech; /* one char buffer for handles opened on pipes */
749 #if defined (_MT) && !defined (DLL_FOR_WIN32S)
751 CRITICAL_SECTION lock;
752 #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
755 EXTERN_C ioinfo * __pioinfo[];
758 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
759 #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1)))
760 #define _osfile(i) (_pioinfo(i)->osfile)
762 #else /* (_MSC_VER >= 1000) */
763 extern char _osfile[];
764 #endif /* (_MSC_VER >= 1000) */
766 #define FOPEN 0x01 /* file handle open */
767 #define FAPPEND 0x20 /* file handle opened O_APPEND */
768 #define FDEV 0x40 /* file handle refers to device */
769 #define FTEXT 0x80 /* file handle is in text mode */
771 #define _STREAM_LOCKS 26 /* Table of stream locks */
772 #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */
773 #define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */
776 *int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
779 * This function allocates a free C Runtime file handle and associates
780 * it with the Win32 HANDLE specified by the first parameter. This is a
781 * temperary fix for WIN95's brain damage GetFileType() error on socket
782 * we just bypass that call for socket
785 * long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
786 * int flags - flags to associate with C Runtime file handle.
789 * returns index of entry in fh, if successful
790 * return -1, if no free entry is found
794 *******************************************************************************/
797 my_open_osfhandle(long osfhandle, int flags)
800 char fileflags; /* _osfile flags */
802 /* copy relevant flags from second parameter */
806 fileflags |= FAPPEND;
811 /* attempt to allocate a C Runtime file handle */
812 if((fh = _alloc_osfhnd()) == -1) {
813 errno = EMFILE; /* too many open files */
814 _doserrno = 0L; /* not an OS error */
815 return -1; /* return error to caller */
818 /* the file is open. now, set the info in _osfhnd array */
819 _set_osfhnd(fh, osfhandle);
821 fileflags |= FOPEN; /* mark as open */
823 #if (_MSC_VER >= 1000)
824 _osfile(fh) = fileflags; /* set osfile entry */
827 _osfile[fh] = fileflags; /* set osfile entry */
828 _unlock(fh+_FH_LOCKS); /* unlock handle */
831 return fh; /* return handle */
834 #define _open_osfhandle my_open_osfhandle
835 #endif /* USE_FIXED_OSFHANDLE */
837 /* simulate flock by locking a range on the file */
839 #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
840 #define LK_LEN 0xffff0000
843 win32_flock(int fd, int oper)
850 croak("flock() unimplemented on this platform");
853 fh = (HANDLE)_get_osfhandle(fd);
854 memset(&o, 0, sizeof(o));
857 case LOCK_SH: /* shared lock */
858 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
860 case LOCK_EX: /* exclusive lock */
861 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
863 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
864 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
866 case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
867 LK_ERR(LockFileEx(fh,
868 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
869 0, LK_LEN, 0, &o),i);
871 case LOCK_UN: /* unlock lock */
872 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
874 default: /* unknown */
885 * redirected io subsystem for all XS modules
898 return (&(_environ));
901 /* the rest are the remapped stdio routines */
921 win32_ferror(FILE *fp)
934 * Since the errors returned by the socket error function
935 * WSAGetLastError() are not known by the library routine strerror
936 * we have to roll our own.
940 #ifdef USE_DECLSPEC_THREAD
941 __declspec(thread) char strerror_buffer[512];
943 #define strerror_buffer (thr->i.Wstrerror_buffer)
946 char strerror_buffer[512];
950 win32_strerror(int e)
952 #ifndef __BORLANDC__ /* Borland intolerance */
957 if(e < 0 || e > sys_nerr) {
962 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
963 strerror_buffer, sizeof(strerror_buffer), NULL) == 0)
964 strcpy(strerror_buffer, "Unknown Error");
966 return strerror_buffer;
972 win32_fprintf(FILE *fp, const char *format, ...)
975 va_start(marker, format); /* Initialize variable arguments. */
977 return (vfprintf(fp, format, marker));
981 win32_printf(const char *format, ...)
984 va_start(marker, format); /* Initialize variable arguments. */
986 return (vprintf(format, marker));
990 win32_vfprintf(FILE *fp, const char *format, va_list args)
992 return (vfprintf(fp, format, args));
996 win32_vprintf(const char *format, va_list args)
998 return (vprintf(format, args));
1002 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
1004 return fread(buf, size, count, fp);
1008 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
1010 return fwrite(buf, size, count, fp);
1014 win32_fopen(const char *filename, const char *mode)
1016 if (stricmp(filename, "/dev/null")==0)
1017 return fopen("NUL", mode);
1018 return fopen(filename, mode);
1021 #ifndef USE_SOCKETS_AS_HANDLES
1023 #define fdopen my_fdopen
1027 win32_fdopen( int handle, const char *mode)
1029 return fdopen(handle, (char *) mode);
1033 win32_freopen( const char *path, const char *mode, FILE *stream)
1035 if (stricmp(path, "/dev/null")==0)
1036 return freopen("NUL", mode, stream);
1037 return freopen(path, mode, stream);
1041 win32_fclose(FILE *pf)
1043 return my_fclose(pf); /* defined in win32sck.c */
1047 win32_fputs(const char *s,FILE *pf)
1049 return fputs(s, pf);
1053 win32_fputc(int c,FILE *pf)
1059 win32_ungetc(int c,FILE *pf)
1061 return ungetc(c,pf);
1065 win32_getc(FILE *pf)
1071 win32_fileno(FILE *pf)
1077 win32_clearerr(FILE *pf)
1084 win32_fflush(FILE *pf)
1090 win32_ftell(FILE *pf)
1096 win32_fseek(FILE *pf,long offset,int origin)
1098 return fseek(pf, offset, origin);
1102 win32_fgetpos(FILE *pf,fpos_t *p)
1104 return fgetpos(pf, p);
1108 win32_fsetpos(FILE *pf,const fpos_t *p)
1110 return fsetpos(pf, p);
1114 win32_rewind(FILE *pf)
1134 win32_fstat(int fd,struct stat *sbufptr)
1136 return fstat(fd,sbufptr);
1140 win32_pipe(int *pfd, unsigned int size, int mode)
1142 return _pipe(pfd, size, mode);
1146 win32_popen(const char *command, const char *mode)
1148 return _popen(command, mode);
1152 win32_pclose(FILE *pf)
1158 win32_setmode(int fd, int mode)
1160 return setmode(fd, mode);
1164 win32_lseek(int fd, long offset, int origin)
1166 return lseek(fd, offset, origin);
1176 win32_open(const char *path, int flag, ...)
1182 pmode = va_arg(ap, int);
1185 if (stricmp(path, "/dev/null")==0)
1186 return open("NUL", flag, pmode);
1187 return open(path,flag,pmode);
1209 win32_dup2(int fd1,int fd2)
1211 return dup2(fd1,fd2);
1215 win32_read(int fd, void *buf, unsigned int cnt)
1217 return read(fd, buf, cnt);
1221 win32_write(int fd, const void *buf, unsigned int cnt)
1223 return write(fd, buf, cnt);
1227 win32_mkdir(const char *dir, int mode)
1229 return mkdir(dir); /* just ignore mode */
1233 win32_rmdir(const char *dir)
1239 win32_chdir(const char *dir)
1245 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1247 return spawnvp(mode, cmdname, (char * const *) argv);
1251 win32_execvp(const char *cmdname, const char *const *argv)
1253 return execvp(cmdname, (char *const *)argv);
1257 win32_perror(const char *str)
1263 win32_setbuf(FILE *pf, char *buf)
1269 win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
1271 return setvbuf(pf, buf, type, size);
1275 win32_flushall(void)
1281 win32_fcloseall(void)
1287 win32_fgets(char *s, int n, FILE *pf)
1289 return fgets(s, n, pf);
1299 win32_fgetc(FILE *pf)
1305 win32_putc(int c, FILE *pf)
1311 win32_puts(const char *s)
1323 win32_putchar(int c)
1330 #ifndef USE_PERL_SBRK
1332 static char *committed = NULL;
1333 static char *base = NULL;
1334 static char *reserved = NULL;
1335 static char *brk = NULL;
1336 static DWORD pagesize = 0;
1337 static DWORD allocsize = 0;
1345 GetSystemInfo(&info);
1346 /* Pretend page size is larger so we don't perpetually
1347 * call the OS to commit just one page ...
1349 pagesize = info.dwPageSize << 3;
1350 allocsize = info.dwAllocationGranularity;
1352 /* This scheme fails eventually if request for contiguous
1353 * block is denied so reserve big blocks - this is only
1354 * address space not memory ...
1356 if (brk+need >= reserved)
1358 DWORD size = 64*1024*1024;
1360 if (committed && reserved && committed < reserved)
1362 /* Commit last of previous chunk cannot span allocations */
1363 addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
1365 committed = reserved;
1367 /* Reserve some (more) space
1368 * Note this is a little sneaky, 1st call passes NULL as reserved
1369 * so lets system choose where we start, subsequent calls pass
1370 * the old end address so ask for a contiguous block
1372 addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
1375 reserved = addr+size;
1390 if (brk > committed)
1392 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
1393 char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
1408 win32_malloc(size_t size)
1410 return malloc(size);
1414 win32_calloc(size_t numitems, size_t size)
1416 return calloc(numitems,size);
1420 win32_realloc(void *block, size_t size)
1422 return realloc(block,size);
1426 win32_free(void *block)
1433 win32_open_osfhandle(long handle, int flags)
1435 return _open_osfhandle(handle, flags);
1439 win32_get_osfhandle(int fd)
1441 return _get_osfhandle(fd);
1452 SV *sv = sv_newmortal();
1453 /* Make one call with zero size - return value is required size */
1454 DWORD len = GetCurrentDirectory((DWORD)0,NULL);
1455 SvUPGRADE(sv,SVt_PV);
1457 SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
1460 * then it worked, set PV valid,
1461 * else leave it 'undef'
1475 croak("usage: Win32::SetCurrentDirectory($cwd)");
1476 if (SetCurrentDirectory(SvPV(ST(0),na)))
1483 XS(w32_GetNextAvailDrive)
1487 char root[] = "_:\\";
1490 if (GetDriveType(root) == 1) {
1499 XS(w32_GetLastError)
1502 XSRETURN_IV(GetLastError());
1510 DWORD size = sizeof(name);
1511 if (GetUserName(name,&size)) {
1512 /* size includes NULL */
1513 ST(0) = sv_2mortal(newSVpv(name,size-1));
1523 char name[MAX_COMPUTERNAME_LENGTH+1];
1524 DWORD size = sizeof(name);
1525 if (GetComputerName(name,&size)) {
1526 /* size does NOT include NULL :-( */
1527 ST(0) = sv_2mortal(newSVpv(name,size));
1539 DWORD size = sizeof(name);
1540 if (GetUserName(name,&size)) {
1542 DWORD sidlen = sizeof(sid);
1544 DWORD dnamelen = sizeof(dname);
1546 if (LookupAccountName(NULL, name, &sid, &sidlen,
1547 dname, &dnamelen, &snu)) {
1548 XSRETURN_PV(dname); /* all that for this */
1559 DWORD flags, filecomplen;
1560 if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
1561 &flags, fsname, sizeof(fsname))) {
1562 if (GIMME == G_ARRAY) {
1563 XPUSHs(sv_2mortal(newSVpv(fsname,0)));
1564 XPUSHs(sv_2mortal(newSViv(flags)));
1565 XPUSHs(sv_2mortal(newSViv(filecomplen)));
1569 XSRETURN_PV(fsname);
1575 XS(w32_GetOSVersion)
1578 OSVERSIONINFO osver;
1580 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1581 if (GetVersionEx(&osver)) {
1582 XPUSHs(newSVpv(osver.szCSDVersion, 0));
1583 XPUSHs(newSViv(osver.dwMajorVersion));
1584 XPUSHs(newSViv(osver.dwMinorVersion));
1585 XPUSHs(newSViv(osver.dwBuildNumber));
1586 XPUSHs(newSViv(osver.dwPlatformId));
1597 XSRETURN_IV(IsWinNT());
1604 XSRETURN_IV(IsWin95());
1608 XS(w32_FormatMessage)
1615 croak("usage: Win32::FormatMessage($errno)");
1617 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1618 &source, SvIV(ST(0)), 0,
1619 msgbuf, sizeof(msgbuf)-1, NULL))
1620 XSRETURN_PV(msgbuf);
1630 PROCESS_INFORMATION stProcInfo;
1631 STARTUPINFO stStartInfo;
1632 BOOL bSuccess = FALSE;
1635 croak("usage: Win32::Spawn($cmdName, $args, $PID)");
1637 cmd = SvPV(ST(0),na);
1638 args = SvPV(ST(1), na);
1640 memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */
1641 stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */
1642 stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */
1643 stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */
1646 cmd, /* Image path */
1647 args, /* Arguments for command line */
1648 NULL, /* Default process security */
1649 NULL, /* Default thread security */
1650 FALSE, /* Must be TRUE to use std handles */
1651 NORMAL_PRIORITY_CLASS, /* No special scheduling */
1652 NULL, /* Inherit our environment block */
1653 NULL, /* Inherit our currrent directory */
1654 &stStartInfo, /* -> Startup info */
1655 &stProcInfo)) /* <- Process info (if OK) */
1657 CloseHandle(stProcInfo.hThread);/* library source code does this. */
1658 sv_setiv(ST(2), stProcInfo.dwProcessId);
1661 XSRETURN_IV(bSuccess);
1665 XS(w32_GetTickCount)
1668 XSRETURN_IV(GetTickCount());
1672 XS(w32_GetShortPathName)
1679 croak("usage: Win32::GetShortPathName($longPathName)");
1681 shortpath = sv_mortalcopy(ST(0));
1682 SvUPGRADE(shortpath, SVt_PV);
1683 /* src == target is allowed */
1685 len = GetShortPathName(SvPVX(shortpath),
1688 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
1690 SvCUR_set(shortpath,len);
1699 Perl_init_os_extras()
1701 char *file = __FILE__;
1704 /* XXX should be removed after checking with Nick */
1705 newXS("Win32::GetCurrentDirectory", w32_GetCwd, file);
1707 /* these names are Activeware compatible */
1708 newXS("Win32::GetCwd", w32_GetCwd, file);
1709 newXS("Win32::SetCwd", w32_SetCwd, file);
1710 newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
1711 newXS("Win32::GetLastError", w32_GetLastError, file);
1712 newXS("Win32::LoginName", w32_LoginName, file);
1713 newXS("Win32::NodeName", w32_NodeName, file);
1714 newXS("Win32::DomainName", w32_DomainName, file);
1715 newXS("Win32::FsType", w32_FsType, file);
1716 newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
1717 newXS("Win32::IsWinNT", w32_IsWinNT, file);
1718 newXS("Win32::IsWin95", w32_IsWin95, file);
1719 newXS("Win32::FormatMessage", w32_FormatMessage, file);
1720 newXS("Win32::Spawn", w32_Spawn, file);
1721 newXS("Win32::GetTickCount", w32_GetTickCount, file);
1722 newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
1724 /* XXX Bloat Alert! The following Activeware preloads really
1725 * ought to be part of Win32::Sys::*, so they're not included
1728 /* LookupAccountName
1730 * InitiateSystemShutdown
1731 * AbortSystemShutdown
1732 * ExpandEnvrironmentStrings
1737 Perl_win32_init(int *argcp, char ***argvp)
1739 /* Disable floating point errors, Perl will trap the ones we
1740 * care about. VC++ RTL defaults to switching these off
1741 * already, but the Borland RTL doesn't. Since we don't
1742 * want to be at the vendor's whim on the default, we set
1743 * it explicitly here.
1745 #if !defined(_ALPHA_) && !defined(__GNUC__)
1746 _control87(MCW_EM, MCW_EM);
1751 #ifdef USE_BINMODE_SCRIPTS
1754 win32_strip_return(SV *sv)
1756 char *s = SvPVX(sv);
1757 char *e = s+SvCUR(sv);
1761 if (*s == '\r' && s[1] == '\n')
1771 SvCUR_set(sv,d-SvPVX(sv));