Generic file changes for MYMALLOC
[p5sagit/p5-mst-13.2.git] / win32 / win32.c
CommitLineData
68dc0745 1/* WIN32.C
2 *
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.
6 *
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.
9 */
0a753a76 10
11#define WIN32_LEAN_AND_MEAN
12#define WIN32IO_IS_STDIO
13#include <tchar.h>
14#include <windows.h>
15
68dc0745 16/* #include "config.h" */
0a753a76 17
18#define PERLIO_NOT_STDIO 0
19#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
20#define PerlIO FILE
21#endif
22
23#include "EXTERN.h"
24#include "perl.h"
ad2e33dc 25#include "XSUB.h"
0a753a76 26#include <fcntl.h>
27#include <sys/stat.h>
28#include <assert.h>
29#include <string.h>
30#include <stdarg.h>
ad2e33dc 31#include <float.h>
0a753a76 32
6890e559 33#define EXECF_EXEC 1
34#define EXECF_SPAWN 2
35#define EXECF_SPAWN_NOWAIT 3
36
8b10511d 37static DWORD IdOS(void);
38
0a753a76 39BOOL ProbeEnv = FALSE;
8b10511d 40DWORD Win32System = (DWORD)-1;
0a753a76 41char szShellPath[MAX_PATH+1];
42char szPerlLibRoot[MAX_PATH+1];
43HANDLE PerlDllHandle = INVALID_HANDLE_VALUE;
44
6890e559 45static int do_spawn2(char *cmd, int exectype);
46
3fe9a6f1 47int
48IsWin95(void) {
8b10511d 49 return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
3fe9a6f1 50}
51
52int
53IsWinNT(void) {
8b10511d 54 return (IdOS() == VER_PLATFORM_WIN32_NT);
3fe9a6f1 55}
0a753a76 56
68dc0745 57char *
58win32PerlLibPath(void)
59{
60 char *end;
61 GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE)
62 ? GetModuleHandle(NULL)
63 : PerlDllHandle,
64 szPerlLibRoot,
65 sizeof(szPerlLibRoot));
66
67 *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
68 if (stricmp(end-4,"\\bin") == 0)
69 end -= 4;
70 strcpy(end,"\\lib");
71 return (szPerlLibRoot);
72}
0a753a76 73
b4793f7f 74char *
75win32SiteLibPath(void)
76{
2bc71ef8 77 static char szPerlSiteLib[MAX_PATH+1];
b4793f7f 78 strcpy(szPerlSiteLib, win32PerlLibPath());
79 strcat(szPerlSiteLib, "\\site");
80 return (szPerlSiteLib);
81}
82
68dc0745 83BOOL
84HasRedirection(char *ptr)
85{
86 int inquote = 0;
87 char quote = '\0';
88
89 /*
90 * Scan string looking for redirection (< or >) or pipe
91 * characters (|) that are not in a quoted string
92 */
93 while(*ptr) {
94 switch(*ptr) {
95 case '\'':
96 case '\"':
97 if(inquote) {
98 if(quote == *ptr) {
99 inquote = 0;
100 quote = '\0';
0a753a76 101 }
68dc0745 102 }
103 else {
104 quote = *ptr;
105 inquote++;
106 }
107 break;
108 case '>':
109 case '<':
110 case '|':
111 if(!inquote)
112 return TRUE;
113 default:
114 break;
0a753a76 115 }
68dc0745 116 ++ptr;
117 }
118 return FALSE;
0a753a76 119}
120
68dc0745 121/* since the current process environment is being updated in util.c
122 * the library functions will get the correct environment
123 */
124PerlIO *
125my_popen(char *cmd, char *mode)
0a753a76 126{
127#ifdef FIXCMD
68dc0745 128#define fixcmd(x) { \
129 char *pspace = strchr((x),' '); \
130 if (pspace) { \
131 char *p = (x); \
132 while (p < pspace) { \
133 if (*p == '/') \
134 *p = '\\'; \
135 p++; \
136 } \
137 } \
138 }
0a753a76 139#else
140#define fixcmd(x)
141#endif
68dc0745 142 fixcmd(cmd);
3e3baf6d 143#ifdef __BORLANDC__ /* workaround a Borland stdio bug */
144 win32_fflush(stdout);
145 win32_fflush(stderr);
146#endif
0a753a76 147 return win32_popen(cmd, mode);
0a753a76 148}
149
68dc0745 150long
151my_pclose(PerlIO *fp)
0a753a76 152{
153 return win32_pclose(fp);
154}
155
8b10511d 156static DWORD
68dc0745 157IdOS(void)
0a753a76 158{
8b10511d 159 static OSVERSIONINFO osver;
0a753a76 160
8b10511d 161 if (osver.dwPlatformId != Win32System) {
162 memset(&osver, 0, sizeof(OSVERSIONINFO));
163 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
164 GetVersionEx(&osver);
165 Win32System = osver.dwPlatformId;
166 }
167 return (Win32System);
0a753a76 168}
169
68dc0745 170static char *
171GetShell(void)
0a753a76 172{
68dc0745 173 if (!ProbeEnv) {
174c211a 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
180 * for).
181 */
182 char *usershell = getenv("PERL5SHELL");
183
8b10511d 184 ProbeEnv = TRUE;
174c211a 185 strcpy(szShellPath, usershell ? usershell : defaultshell);
68dc0745 186 }
187 return szShellPath;
0a753a76 188}
189
68dc0745 190int
d55594ae 191do_aspawn(void* really, void ** mark, void ** arglast)
0a753a76 192{
68dc0745 193 char **argv;
194 char *strPtr;
195 char *cmd;
196 int status;
197 unsigned int length;
198 int index = 0;
0a753a76 199 SV *sv = (SV*)really;
68dc0745 200 SV** pSv = (SV**)mark;
201
fc36a67e 202 New(1310, argv, (arglast - mark) + 4, char*);
68dc0745 203
204 if(sv != Nullsv) {
205 cmd = SvPV(sv, length);
206 }
207 else {
3fe9a6f1 208 argv[index++] = cmd = GetShell();
0a9a032c 209 if (IsWinNT())
210 argv[index++] = "/x"; /* always enable command extensions */
68dc0745 211 argv[index++] = "/c";
212 }
213
5aabfad6 214 while(++pSv <= (SV**)arglast) {
215 sv = *pSv;
68dc0745 216 strPtr = SvPV(sv, length);
217 if(strPtr != NULL && *strPtr != '\0')
218 argv[index++] = strPtr;
219 }
220 argv[index++] = 0;
221
3e3baf6d 222 status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv);
68dc0745 223
224 Safefree(argv);
225
5aabfad6 226 if (status < 0) {
227 if (dowarn)
228 warn("Can't spawn \"%s\": %s", cmd, strerror(errno));
229 status = 255 << 8;
230 }
231 return (status);
68dc0745 232}
233
234int
6890e559 235do_spawn2(char *cmd, int exectype)
68dc0745 236{
237 char **a;
238 char *s;
239 char **argv;
240 int status = -1;
241 BOOL needToTry = TRUE;
242 char *shell, *cmd2;
243
244 /* save an extra exec if possible */
245 shell = GetShell();
246
247 /* see if there are shell metacharacters in it */
248 if(!HasRedirection(cmd)) {
fc36a67e 249 New(1301,argv, strlen(cmd) / 2 + 2, char*);
250 New(1302,cmd2, strlen(cmd) + 1, char);
68dc0745 251 strcpy(cmd2, cmd);
252 a = argv;
253 for (s = cmd2; *s;) {
254 while (*s && isspace(*s))
255 s++;
256 if (*s)
257 *(a++) = s;
258 while(*s && !isspace(*s))
259 s++;
260 if(*s)
261 *s++ = '\0';
0a753a76 262 }
68dc0745 263 *a = Nullch;
264 if(argv[0]) {
6890e559 265 switch (exectype) {
266 case EXECF_SPAWN:
267 status = win32_spawnvp(P_WAIT, argv[0],
268 (const char* const*)argv);
269 break;
270 case EXECF_SPAWN_NOWAIT:
271 status = win32_spawnvp(P_NOWAIT, argv[0],
272 (const char* const*)argv);
273 break;
274 case EXECF_EXEC:
275 status = win32_execvp(argv[0], (const char* const*)argv);
276 break;
277 }
68dc0745 278 if(status != -1 || errno == 0)
279 needToTry = FALSE;
0a753a76 280 }
0a753a76 281 Safefree(argv);
68dc0745 282 Safefree(cmd2);
283 }
284 if(needToTry) {
3e3baf6d 285 char *argv[5];
0a9a032c 286 int i = 0;
287 argv[i++] = shell;
288 if (IsWinNT())
289 argv[i++] = "/x";
290 argv[i++] = "/c"; argv[i++] = cmd; argv[i] = Nullch;
6890e559 291 switch (exectype) {
292 case EXECF_SPAWN:
293 status = win32_spawnvp(P_WAIT, argv[0],
294 (const char* const*)argv);
295 break;
296 case EXECF_SPAWN_NOWAIT:
297 status = win32_spawnvp(P_NOWAIT, argv[0],
298 (const char* const*)argv);
299 break;
300 case EXECF_EXEC:
301 status = win32_execvp(argv[0], (const char* const*)argv);
302 break;
303 }
68dc0745 304 }
5aabfad6 305 if (status < 0) {
306 if (dowarn)
6890e559 307 warn("Can't %s \"%s\": %s",
308 (exectype == EXECF_EXEC ? "exec" : "spawn"),
309 needToTry ? shell : argv[0],
5aabfad6 310 strerror(errno));
311 status = 255 << 8;
312 }
313 return (status);
0a753a76 314}
315
6890e559 316int
317do_spawn(char *cmd)
318{
319 return do_spawn2(cmd, EXECF_SPAWN);
320}
321
322bool
323do_exec(char *cmd)
324{
325 do_spawn2(cmd, EXECF_EXEC);
326 return FALSE;
327}
328
0a753a76 329
330#define PATHLEN 1024
331
68dc0745 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.
335 */
336DIR *
337opendir(char *filename)
0a753a76 338{
339 DIR *p;
68dc0745 340 long len;
341 long idx;
342 char scannamespc[PATHLEN];
343 char *scanname = scannamespc;
344 struct stat sbuf;
345 WIN32_FIND_DATA FindData;
346 HANDLE fh;
347/* char root[_MAX_PATH];*/
348/* char volname[_MAX_PATH];*/
349/* DWORD serial, maxname, flags;*/
350/* BOOL downcase;*/
351/* char *dummy;*/
352
353 /* check to see if filename is a directory */
d55594ae 354 if (win32_stat(filename, &sbuf) < 0 || (sbuf.st_mode & S_IFDIR) == 0) {
68dc0745 355 return NULL;
356 }
357
358 /* get the file system characteristics */
359/* if(GetFullPathName(filename, MAX_PATH, root, &dummy)) {
360 * if(dummy = strchr(root, '\\'))
361 * *++dummy = '\0';
362 * if(GetVolumeInformation(root, volname, MAX_PATH, &serial,
363 * &maxname, &flags, 0, 0)) {
364 * downcase = !(flags & FS_CASE_IS_PRESERVED);
365 * }
366 * }
367 * else {
368 * downcase = TRUE;
369 * }
370 */
371 /* Get us a DIR structure */
fc36a67e 372 Newz(1303, p, 1, DIR);
68dc0745 373 if(p == NULL)
374 return NULL;
375
376 /* Create the search pattern */
377 strcpy(scanname, filename);
378
379 if(index("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
380 strcat(scanname, "/*");
381 else
382 strcat(scanname, "*");
383
384 /* do the FindFirstFile call */
385 fh = FindFirstFile(scanname, &FindData);
386 if(fh == INVALID_HANDLE_VALUE) {
387 return NULL;
388 }
389
390 /* now allocate the first part of the string table for
391 * the filenames that we find.
392 */
393 idx = strlen(FindData.cFileName)+1;
fc36a67e 394 New(1304, p->start, idx, char);
68dc0745 395 if(p->start == NULL) {
65e48ea9 396 croak("opendir: malloc failed!\n");
68dc0745 397 }
398 strcpy(p->start, FindData.cFileName);
399/* if(downcase)
400 * strlwr(p->start);
401 */
402 p->nfiles++;
403
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.
408 */
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
413 */
414 Renew(p->start, idx+len+1, char);
415 if(p->start == NULL) {
65e48ea9 416 croak("opendir: malloc failed!\n");
0a753a76 417 }
68dc0745 418 strcpy(&p->start[idx], FindData.cFileName);
419/* if (downcase)
420 * strlwr(&p->start[idx]);
421 */
0a753a76 422 p->nfiles++;
423 idx += len+1;
424 }
425 FindClose(fh);
426 p->size = idx;
427 p->curr = p->start;
428 return p;
429}
430
431
68dc0745 432/* Readdir just returns the current string pointer and bumps the
433 * string pointer to the nDllExport entry.
434 */
435struct direct *
436readdir(DIR *dirp)
0a753a76 437{
68dc0745 438 int len;
439 static int dummy = 0;
0a753a76 440
68dc0745 441 if (dirp->curr) {
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;
0a753a76 446
68dc0745 447 /* Fake an inode */
448 dirp->dirstr.d_ino = dummy++;
0a753a76 449
68dc0745 450 /* Now set up for the nDllExport call to readdir */
451 dirp->curr += len + 1;
452 if (dirp->curr >= (dirp->start + dirp->size)) {
453 dirp->curr = NULL;
454 }
0a753a76 455
68dc0745 456 return &(dirp->dirstr);
457 }
458 else
459 return NULL;
0a753a76 460}
461
68dc0745 462/* Telldir returns the current string pointer position */
463long
464telldir(DIR *dirp)
0a753a76 465{
466 return (long) dirp->curr;
467}
468
469
68dc0745 470/* Seekdir moves the string pointer to a previously saved position
471 *(Saved by telldir).
472 */
473void
474seekdir(DIR *dirp, long loc)
0a753a76 475{
476 dirp->curr = (char *)loc;
477}
478
68dc0745 479/* Rewinddir resets the string pointer to the start */
480void
481rewinddir(DIR *dirp)
0a753a76 482{
483 dirp->curr = dirp->start;
484}
485
68dc0745 486/* free the memory allocated by opendir */
487int
488closedir(DIR *dirp)
0a753a76 489{
490 Safefree(dirp->start);
491 Safefree(dirp);
68dc0745 492 return 1;
0a753a76 493}
494
495
68dc0745 496/*
497 * various stubs
498 */
0a753a76 499
500
68dc0745 501/* Ownership
502 *
503 * Just pretend that everyone is a superuser. NT will let us know if
504 * we don\'t really have permission to do something.
505 */
0a753a76 506
507#define ROOT_UID ((uid_t)0)
508#define ROOT_GID ((gid_t)0)
509
68dc0745 510uid_t
511getuid(void)
0a753a76 512{
68dc0745 513 return ROOT_UID;
0a753a76 514}
515
68dc0745 516uid_t
517geteuid(void)
0a753a76 518{
68dc0745 519 return ROOT_UID;
0a753a76 520}
521
68dc0745 522gid_t
523getgid(void)
0a753a76 524{
68dc0745 525 return ROOT_GID;
0a753a76 526}
527
68dc0745 528gid_t
529getegid(void)
0a753a76 530{
68dc0745 531 return ROOT_GID;
0a753a76 532}
533
68dc0745 534int
535setuid(uid_t uid)
0a753a76 536{
68dc0745 537 return (uid == ROOT_UID ? 0 : -1);
0a753a76 538}
539
68dc0745 540int
541setgid(gid_t gid)
0a753a76 542{
68dc0745 543 return (gid == ROOT_GID ? 0 : -1);
0a753a76 544}
545
68dc0745 546/*
547 * pretended kill
548 */
549int
550kill(int pid, int sig)
0a753a76 551{
68dc0745 552 HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
0a753a76 553
554 if (hProcess == NULL) {
65e48ea9 555 croak("kill process failed!\n");
68dc0745 556 }
557 else {
558 if (!TerminateProcess(hProcess, sig))
65e48ea9 559 croak("kill process failed!\n");
68dc0745 560 CloseHandle(hProcess);
561 }
562 return 0;
0a753a76 563}
564
68dc0745 565/*
566 * File system stuff
567 */
0a753a76 568
3e3baf6d 569#if 0
68dc0745 570int
571ioctl(int i, unsigned int u, char *data)
0a753a76 572{
65e48ea9 573 croak("ioctl not implemented!\n");
68dc0745 574 return -1;
0a753a76 575}
3e3baf6d 576#endif
0a753a76 577
f3986ebb 578DllExport unsigned int
579win32_sleep(unsigned int t)
0a753a76 580{
68dc0745 581 Sleep(t*1000);
582 return 0;
0a753a76 583}
584
68dc0745 585DllExport int
586win32_stat(const char *path, struct stat *buffer)
0a753a76 587{
68dc0745 588 char t[MAX_PATH];
589 const char *p = path;
590 int l = strlen(path);
67fbe06e 591 int res;
0a753a76 592
68dc0745 593 if (l > 1) {
594 switch(path[l - 1]) {
595 case '\\':
596 case '/':
597 if (path[l - 2] != ':') {
598 strncpy(t, path, l - 1);
599 t[l - 1] = 0;
600 p = t;
601 };
602 }
603 }
390b85e7 604 res = stat(p,buffer);
67fbe06e 605#ifdef __BORLANDC__
606 if (res == 0) {
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;
617 else
618 buffer->st_mode |= S_IEXEC;
619 }
620 else
621 buffer->st_mode &= ~S_IEXEC;
622 }
623 }
624#endif
625 return res;
0a753a76 626}
627
0551aaa8 628#ifndef USE_WIN32_RTL_ENV
629
630DllExport char *
631win32_getenv(const char *name)
632{
633 static char *curitem = Nullch;
634 static DWORD curlen = 512;
635 DWORD needlen;
636 if (!curitem)
637 New(1305,curitem,curlen,char);
638 if (!(needlen = GetEnvironmentVariable(name,curitem,curlen)))
639 return Nullch;
640 while (needlen > curlen) {
641 Renew(curitem,needlen,char);
642 curlen = needlen;
643 needlen = GetEnvironmentVariable(name,curitem,curlen);
644 }
645 return curitem;
646}
647
648#endif
649
d55594ae 650static long
651FileTimeToClock(PFILETIME ft)
652{
653 __int64 qw = ft->dwHighDateTime;
654 qw <<= 32;
655 qw |= ft->dwLowDateTime;
656 qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */
657 return (long) qw;
658}
659
f3986ebb 660DllExport int
661win32_times(struct tms *timebuf)
0a753a76 662{
d55594ae 663 FILETIME user;
664 FILETIME kernel;
665 FILETIME dummy;
666 if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
667 &kernel,&user)) {
668 timebuf->tms_utime = FileTimeToClock(&user);
669 timebuf->tms_stime = FileTimeToClock(&kernel);
670 timebuf->tms_cutime = 0;
671 timebuf->tms_cstime = 0;
672
673 } else {
674 /* That failed - e.g. Win95 fallback to clock() */
675 clock_t t = clock();
676 timebuf->tms_utime = t;
677 timebuf->tms_stime = 0;
678 timebuf->tms_cutime = 0;
679 timebuf->tms_cstime = 0;
680 }
68dc0745 681 return 0;
0a753a76 682}
683
d55594ae 684static UINT timerid = 0;
685
686
687static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
688{
689 KillTimer(NULL,timerid);
690 timerid=0;
691 sighandler(14);
692}
693
f3986ebb 694DllExport unsigned int
695win32_alarm(unsigned int sec)
0a753a76 696{
d55594ae 697 /*
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>
702 *
703 * Snag is unless something is looking at the message queue
704 * nothing happens :-(
705 */
706 if (sec)
707 {
708 timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
709 if (!timerid)
710 croak("Cannot set timer");
711 }
712 else
713 {
714 if (timerid)
715 {
716 KillTimer(NULL,timerid);
717 timerid=0;
718 }
719 }
68dc0745 720 return 0;
0a753a76 721}
722
f3986ebb 723#ifdef USE_FIXED_OSFHANDLE
390b85e7 724
725EXTERN_C int __cdecl _alloc_osfhnd(void);
726EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
727EXTERN_C void __cdecl _lock_fhandle(int);
728EXTERN_C void __cdecl _unlock_fhandle(int);
729EXTERN_C void __cdecl _unlock(int);
730
731#if (_MSC_VER >= 1000)
732typedef struct {
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)
737 int lockinitflag;
738 CRITICAL_SECTION lock;
739#endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */
740} ioinfo;
741
742EXTERN_C ioinfo * __pioinfo[];
743
744#define IOINFO_L2E 5
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)
748
749#else /* (_MSC_VER >= 1000) */
750extern char _osfile[];
751#endif /* (_MSC_VER >= 1000) */
752
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 */
757
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 */
761
762/***
763*int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle
764*
765*Purpose:
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
770*
771*Entry:
772* long osfhandle - Win32 HANDLE to associate with C Runtime file handle.
773* int flags - flags to associate with C Runtime file handle.
774*
775*Exit:
776* returns index of entry in fh, if successful
777* return -1, if no free entry is found
778*
779*Exceptions:
780*
781*******************************************************************************/
782
783static int
784my_open_osfhandle(long osfhandle, int flags)
785{
786 int fh;
787 char fileflags; /* _osfile flags */
788
789 /* copy relevant flags from second parameter */
790 fileflags = FDEV;
791
792 if(flags & O_APPEND)
793 fileflags |= FAPPEND;
794
795 if(flags & O_TEXT)
796 fileflags |= FTEXT;
797
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 */
803 }
804
805 /* the file is open. now, set the info in _osfhnd array */
806 _set_osfhnd(fh, osfhandle);
807
808 fileflags |= FOPEN; /* mark as open */
809
810#if (_MSC_VER >= 1000)
811 _osfile(fh) = fileflags; /* set osfile entry */
812 _unlock_fhandle(fh);
813#else
814 _osfile[fh] = fileflags; /* set osfile entry */
815 _unlock(fh+_FH_LOCKS); /* unlock handle */
816#endif
817
818 return fh; /* return handle */
819}
820
821#define _open_osfhandle my_open_osfhandle
f3986ebb 822#endif /* USE_FIXED_OSFHANDLE */
390b85e7 823
824/* simulate flock by locking a range on the file */
825
826#define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
827#define LK_LEN 0xffff0000
828
f3986ebb 829DllExport int
830win32_flock(int fd, int oper)
390b85e7 831{
832 OVERLAPPED o;
833 int i = -1;
834 HANDLE fh;
835
f3986ebb 836 if (!IsWinNT()) {
837 croak("flock() unimplemented on this platform");
838 return -1;
839 }
390b85e7 840 fh = (HANDLE)_get_osfhandle(fd);
841 memset(&o, 0, sizeof(o));
842
843 switch(oper) {
844 case LOCK_SH: /* shared lock */
845 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
846 break;
847 case LOCK_EX: /* exclusive lock */
848 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
849 break;
850 case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
851 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
852 break;
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);
857 break;
858 case LOCK_UN: /* unlock lock */
859 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
860 break;
861 default: /* unknown */
862 errno = EINVAL;
863 break;
864 }
865 return i;
866}
867
868#undef LK_ERR
869#undef LK_LEN
870
68dc0745 871/*
872 * redirected io subsystem for all XS modules
873 *
874 */
0a753a76 875
68dc0745 876DllExport int *
877win32_errno(void)
0a753a76 878{
390b85e7 879 return (&errno);
0a753a76 880}
881
dcb2879a 882DllExport char ***
883win32_environ(void)
884{
390b85e7 885 return (&(_environ));
dcb2879a 886}
887
68dc0745 888/* the rest are the remapped stdio routines */
889DllExport FILE *
890win32_stderr(void)
0a753a76 891{
390b85e7 892 return (stderr);
0a753a76 893}
894
68dc0745 895DllExport FILE *
896win32_stdin(void)
0a753a76 897{
390b85e7 898 return (stdin);
0a753a76 899}
900
68dc0745 901DllExport FILE *
902win32_stdout()
0a753a76 903{
390b85e7 904 return (stdout);
0a753a76 905}
906
68dc0745 907DllExport int
908win32_ferror(FILE *fp)
0a753a76 909{
390b85e7 910 return (ferror(fp));
0a753a76 911}
912
913
68dc0745 914DllExport int
915win32_feof(FILE *fp)
0a753a76 916{
390b85e7 917 return (feof(fp));
0a753a76 918}
919
68dc0745 920/*
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.
924 */
0a753a76 925
926__declspec(thread) char strerror_buffer[512];
927
68dc0745 928DllExport char *
929win32_strerror(int e)
0a753a76 930{
3e3baf6d 931#ifndef __BORLANDC__ /* Borland intolerance */
68dc0745 932 extern int sys_nerr;
3e3baf6d 933#endif
68dc0745 934 DWORD source = 0;
0a753a76 935
68dc0745 936 if(e < 0 || e > sys_nerr) {
937 if(e < 0)
938 e = GetLastError();
0a753a76 939
68dc0745 940 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
941 strerror_buffer, sizeof(strerror_buffer), NULL) == 0)
942 strcpy(strerror_buffer, "Unknown Error");
0a753a76 943
68dc0745 944 return strerror_buffer;
945 }
390b85e7 946 return strerror(e);
0a753a76 947}
948
68dc0745 949DllExport int
950win32_fprintf(FILE *fp, const char *format, ...)
0a753a76 951{
68dc0745 952 va_list marker;
953 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 954
390b85e7 955 return (vfprintf(fp, format, marker));
0a753a76 956}
957
68dc0745 958DllExport int
959win32_printf(const char *format, ...)
0a753a76 960{
68dc0745 961 va_list marker;
962 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 963
390b85e7 964 return (vprintf(format, marker));
0a753a76 965}
966
68dc0745 967DllExport int
968win32_vfprintf(FILE *fp, const char *format, va_list args)
0a753a76 969{
390b85e7 970 return (vfprintf(fp, format, args));
0a753a76 971}
972
96e4d5b1 973DllExport int
974win32_vprintf(const char *format, va_list args)
975{
390b85e7 976 return (vprintf(format, args));
96e4d5b1 977}
978
68dc0745 979DllExport size_t
980win32_fread(void *buf, size_t size, size_t count, FILE *fp)
0a753a76 981{
390b85e7 982 return fread(buf, size, count, fp);
0a753a76 983}
984
68dc0745 985DllExport size_t
986win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
0a753a76 987{
390b85e7 988 return fwrite(buf, size, count, fp);
0a753a76 989}
990
68dc0745 991DllExport FILE *
992win32_fopen(const char *filename, const char *mode)
0a753a76 993{
68dc0745 994 if (stricmp(filename, "/dev/null")==0)
390b85e7 995 return fopen("NUL", mode);
996 return fopen(filename, mode);
0a753a76 997}
998
f3986ebb 999#ifndef USE_SOCKETS_AS_HANDLES
1000#undef fdopen
1001#define fdopen my_fdopen
1002#endif
1003
68dc0745 1004DllExport FILE *
1005win32_fdopen( int handle, const char *mode)
0a753a76 1006{
390b85e7 1007 return fdopen(handle, (char *) mode);
0a753a76 1008}
1009
68dc0745 1010DllExport FILE *
1011win32_freopen( const char *path, const char *mode, FILE *stream)
0a753a76 1012{
68dc0745 1013 if (stricmp(path, "/dev/null")==0)
390b85e7 1014 return freopen("NUL", mode, stream);
1015 return freopen(path, mode, stream);
0a753a76 1016}
1017
68dc0745 1018DllExport int
1019win32_fclose(FILE *pf)
0a753a76 1020{
f3986ebb 1021 return my_fclose(pf); /* defined in win32sck.c */
0a753a76 1022}
1023
68dc0745 1024DllExport int
1025win32_fputs(const char *s,FILE *pf)
0a753a76 1026{
390b85e7 1027 return fputs(s, pf);
0a753a76 1028}
1029
68dc0745 1030DllExport int
1031win32_fputc(int c,FILE *pf)
0a753a76 1032{
390b85e7 1033 return fputc(c,pf);
0a753a76 1034}
1035
68dc0745 1036DllExport int
1037win32_ungetc(int c,FILE *pf)
0a753a76 1038{
390b85e7 1039 return ungetc(c,pf);
0a753a76 1040}
1041
68dc0745 1042DllExport int
1043win32_getc(FILE *pf)
0a753a76 1044{
390b85e7 1045 return getc(pf);
0a753a76 1046}
1047
68dc0745 1048DllExport int
1049win32_fileno(FILE *pf)
0a753a76 1050{
390b85e7 1051 return fileno(pf);
0a753a76 1052}
1053
68dc0745 1054DllExport void
1055win32_clearerr(FILE *pf)
0a753a76 1056{
390b85e7 1057 clearerr(pf);
68dc0745 1058 return;
0a753a76 1059}
1060
68dc0745 1061DllExport int
1062win32_fflush(FILE *pf)
0a753a76 1063{
390b85e7 1064 return fflush(pf);
0a753a76 1065}
1066
68dc0745 1067DllExport long
1068win32_ftell(FILE *pf)
0a753a76 1069{
390b85e7 1070 return ftell(pf);
0a753a76 1071}
1072
68dc0745 1073DllExport int
1074win32_fseek(FILE *pf,long offset,int origin)
0a753a76 1075{
390b85e7 1076 return fseek(pf, offset, origin);
0a753a76 1077}
1078
68dc0745 1079DllExport int
1080win32_fgetpos(FILE *pf,fpos_t *p)
0a753a76 1081{
390b85e7 1082 return fgetpos(pf, p);
0a753a76 1083}
1084
68dc0745 1085DllExport int
1086win32_fsetpos(FILE *pf,const fpos_t *p)
0a753a76 1087{
390b85e7 1088 return fsetpos(pf, p);
0a753a76 1089}
1090
68dc0745 1091DllExport void
1092win32_rewind(FILE *pf)
0a753a76 1093{
390b85e7 1094 rewind(pf);
68dc0745 1095 return;
0a753a76 1096}
1097
68dc0745 1098DllExport FILE*
1099win32_tmpfile(void)
0a753a76 1100{
390b85e7 1101 return tmpfile();
0a753a76 1102}
1103
68dc0745 1104DllExport void
1105win32_abort(void)
0a753a76 1106{
390b85e7 1107 abort();
68dc0745 1108 return;
0a753a76 1109}
1110
68dc0745 1111DllExport int
1112win32_fstat(int fd,struct stat *bufptr)
0a753a76 1113{
390b85e7 1114 return fstat(fd,bufptr);
0a753a76 1115}
1116
68dc0745 1117DllExport int
1118win32_pipe(int *pfd, unsigned int size, int mode)
0a753a76 1119{
390b85e7 1120 return _pipe(pfd, size, mode);
0a753a76 1121}
1122
68dc0745 1123DllExport FILE*
1124win32_popen(const char *command, const char *mode)
0a753a76 1125{
390b85e7 1126 return _popen(command, mode);
0a753a76 1127}
1128
68dc0745 1129DllExport int
1130win32_pclose(FILE *pf)
0a753a76 1131{
390b85e7 1132 return _pclose(pf);
0a753a76 1133}
1134
68dc0745 1135DllExport int
1136win32_setmode(int fd, int mode)
0a753a76 1137{
390b85e7 1138 return setmode(fd, mode);
0a753a76 1139}
1140
96e4d5b1 1141DllExport long
1142win32_lseek(int fd, long offset, int origin)
1143{
390b85e7 1144 return lseek(fd, offset, origin);
96e4d5b1 1145}
1146
1147DllExport long
1148win32_tell(int fd)
1149{
390b85e7 1150 return tell(fd);
96e4d5b1 1151}
1152
68dc0745 1153DllExport int
1154win32_open(const char *path, int flag, ...)
0a753a76 1155{
68dc0745 1156 va_list ap;
1157 int pmode;
0a753a76 1158
1159 va_start(ap, flag);
1160 pmode = va_arg(ap, int);
1161 va_end(ap);
1162
68dc0745 1163 if (stricmp(path, "/dev/null")==0)
390b85e7 1164 return open("NUL", flag, pmode);
1165 return open(path,flag,pmode);
0a753a76 1166}
1167
68dc0745 1168DllExport int
1169win32_close(int fd)
0a753a76 1170{
390b85e7 1171 return close(fd);
0a753a76 1172}
1173
68dc0745 1174DllExport int
96e4d5b1 1175win32_eof(int fd)
1176{
390b85e7 1177 return eof(fd);
96e4d5b1 1178}
1179
1180DllExport int
68dc0745 1181win32_dup(int fd)
0a753a76 1182{
390b85e7 1183 return dup(fd);
0a753a76 1184}
1185
68dc0745 1186DllExport int
1187win32_dup2(int fd1,int fd2)
0a753a76 1188{
390b85e7 1189 return dup2(fd1,fd2);
0a753a76 1190}
1191
68dc0745 1192DllExport int
3e3baf6d 1193win32_read(int fd, void *buf, unsigned int cnt)
0a753a76 1194{
390b85e7 1195 return read(fd, buf, cnt);
0a753a76 1196}
1197
68dc0745 1198DllExport int
3e3baf6d 1199win32_write(int fd, const void *buf, unsigned int cnt)
0a753a76 1200{
390b85e7 1201 return write(fd, buf, cnt);
0a753a76 1202}
1203
68dc0745 1204DllExport int
5aabfad6 1205win32_mkdir(const char *dir, int mode)
1206{
390b85e7 1207 return mkdir(dir); /* just ignore mode */
5aabfad6 1208}
96e4d5b1 1209
5aabfad6 1210DllExport int
1211win32_rmdir(const char *dir)
1212{
390b85e7 1213 return rmdir(dir);
5aabfad6 1214}
96e4d5b1 1215
5aabfad6 1216DllExport int
1217win32_chdir(const char *dir)
1218{
390b85e7 1219 return chdir(dir);
5aabfad6 1220}
96e4d5b1 1221
5aabfad6 1222DllExport int
3e3baf6d 1223win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
0a753a76 1224{
390b85e7 1225 return spawnvp(mode, cmdname, (char * const *) argv);
0a753a76 1226}
1227
6890e559 1228DllExport int
1229win32_execvp(const char *cmdname, const char *const *argv)
1230{
390b85e7 1231 return execvp(cmdname, (char *const *)argv);
6890e559 1232}
1233
84902520 1234DllExport void
1235win32_perror(const char *str)
1236{
390b85e7 1237 perror(str);
84902520 1238}
1239
1240DllExport void
1241win32_setbuf(FILE *pf, char *buf)
1242{
390b85e7 1243 setbuf(pf, buf);
84902520 1244}
1245
1246DllExport int
1247win32_setvbuf(FILE *pf, char *buf, int type, size_t size)
1248{
390b85e7 1249 return setvbuf(pf, buf, type, size);
84902520 1250}
1251
1252DllExport int
1253win32_flushall(void)
1254{
390b85e7 1255 return flushall();
84902520 1256}
1257
1258DllExport int
1259win32_fcloseall(void)
1260{
390b85e7 1261 return fcloseall();
84902520 1262}
1263
1264DllExport char*
1265win32_fgets(char *s, int n, FILE *pf)
1266{
390b85e7 1267 return fgets(s, n, pf);
84902520 1268}
1269
1270DllExport char*
1271win32_gets(char *s)
1272{
390b85e7 1273 return gets(s);
84902520 1274}
1275
1276DllExport int
1277win32_fgetc(FILE *pf)
1278{
390b85e7 1279 return fgetc(pf);
84902520 1280}
1281
1282DllExport int
1283win32_putc(int c, FILE *pf)
1284{
390b85e7 1285 return putc(c,pf);
84902520 1286}
1287
1288DllExport int
1289win32_puts(const char *s)
1290{
390b85e7 1291 return puts(s);
84902520 1292}
1293
1294DllExport int
1295win32_getchar(void)
1296{
390b85e7 1297 return getchar();
84902520 1298}
1299
1300DllExport int
1301win32_putchar(int c)
1302{
390b85e7 1303 return putchar(c);
84902520 1304}
1305
bbc8f9de 1306#ifdef MYMALLOC
1307
1308#ifndef USE_PERL_SBRK
1309
1310static char *committed = NULL;
1311static char *base = NULL;
1312static char *reserved = NULL;
1313static char *brk = NULL;
1314static DWORD pagesize = 0;
1315static DWORD allocsize = 0;
1316
1317void *
1318sbrk(int need)
1319{
1320 void *result;
1321 if (!pagesize)
1322 {SYSTEM_INFO info;
1323 GetSystemInfo(&info);
1324 /* Pretend page size is larger so we don't perpetually
1325 * call the OS to commit just one page ...
1326 */
1327 pagesize = info.dwPageSize << 3;
1328 allocsize = info.dwAllocationGranularity;
1329 }
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 ...
1333 */
1334 if (brk+need >= reserved)
1335 {
1336 DWORD size = 64*1024*1024;
1337 char *addr;
1338 if (committed && reserved && committed < reserved)
1339 {
1340 /* Commit last of previous chunk cannot span allocations */
1341 addr = VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
1342 if (addr)
1343 committed = reserved;
1344 }
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
1349 */
1350 addr = VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
1351 if (addr)
1352 {
1353 reserved = addr+size;
1354 if (!base)
1355 base = addr;
1356 if (!committed)
1357 committed = base;
1358 if (!brk)
1359 brk = committed;
1360 }
1361 else
1362 {
1363 return (void *) -1;
1364 }
1365 }
1366 result = brk;
1367 brk += need;
1368 if (brk > committed)
1369 {
1370 DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
1371 char *addr = VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
1372 if (addr)
1373 {
1374 committed += size;
1375 }
1376 else
1377 return (void *) -1;
1378 }
1379 return result;
1380}
1381
1382#endif
1383#endif
1384
84902520 1385DllExport void*
1386win32_malloc(size_t size)
1387{
390b85e7 1388 return malloc(size);
84902520 1389}
1390
1391DllExport void*
1392win32_calloc(size_t numitems, size_t size)
1393{
390b85e7 1394 return calloc(numitems,size);
84902520 1395}
1396
1397DllExport void*
1398win32_realloc(void *block, size_t size)
1399{
390b85e7 1400 return realloc(block,size);
84902520 1401}
1402
1403DllExport void
1404win32_free(void *block)
1405{
390b85e7 1406 free(block);
84902520 1407}
1408
bbc8f9de 1409
68dc0745 1410int
65e48ea9 1411win32_open_osfhandle(long handle, int flags)
0a753a76 1412{
390b85e7 1413 return _open_osfhandle(handle, flags);
0a753a76 1414}
1415
68dc0745 1416long
65e48ea9 1417win32_get_osfhandle(int fd)
0a753a76 1418{
390b85e7 1419 return _get_osfhandle(fd);
0a753a76 1420}
7bac28a0 1421
7bac28a0 1422/*
1423 * Extras.
1424 */
1425
ad2e33dc 1426static
1427XS(w32_GetCwd)
1428{
1429 dXSARGS;
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);
1434 SvGROW(sv,len);
1435 SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
1436 /*
1437 * If result != 0
1438 * then it worked, set PV valid,
1439 * else leave it 'undef'
1440 */
1441 if (SvCUR(sv))
1442 SvPOK_on(sv);
1443 EXTEND(sp,1);
1444 ST(0) = sv;
1445 XSRETURN(1);
1446}
1447
1448static
1449XS(w32_SetCwd)
1450{
1451 dXSARGS;
1452 if (items != 1)
1453 croak("usage: Win32::SetCurrentDirectory($cwd)");
1454 if (SetCurrentDirectory(SvPV(ST(0),na)))
1455 XSRETURN_YES;
1456
1457 XSRETURN_NO;
1458}
1459
1460static
1461XS(w32_GetNextAvailDrive)
1462{
1463 dXSARGS;
1464 char ix = 'C';
1465 char root[] = "_:\\";
1466 while (ix <= 'Z') {
1467 root[0] = ix++;
1468 if (GetDriveType(root) == 1) {
1469 root[2] = '\0';
1470 XSRETURN_PV(root);
1471 }
1472 }
1473 XSRETURN_UNDEF;
1474}
1475
1476static
1477XS(w32_GetLastError)
1478{
1479 dXSARGS;
1480 XSRETURN_IV(GetLastError());
1481}
1482
1483static
1484XS(w32_LoginName)
1485{
1486 dXSARGS;
1487 char name[256];
1488 DWORD size = sizeof(name);
1489 if (GetUserName(name,&size)) {
1490 /* size includes NULL */
1491 ST(0) = sv_2mortal(newSVpv(name,size-1));
1492 XSRETURN(1);
1493 }
1494 XSRETURN_UNDEF;
1495}
1496
1497static
1498XS(w32_NodeName)
1499{
1500 dXSARGS;
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));
1506 XSRETURN(1);
1507 }
1508 XSRETURN_UNDEF;
1509}
1510
1511
1512static
1513XS(w32_DomainName)
1514{
1515 dXSARGS;
1516 char name[256];
1517 DWORD size = sizeof(name);
1518 if (GetUserName(name,&size)) {
1519 char sid[1024];
1520 DWORD sidlen = sizeof(sid);
1521 char dname[256];
1522 DWORD dnamelen = sizeof(dname);
1523 SID_NAME_USE snu;
1524 if (LookupAccountName(NULL, name, &sid, &sidlen,
1525 dname, &dnamelen, &snu)) {
1526 XSRETURN_PV(dname); /* all that for this */
1527 }
1528 }
1529 XSRETURN_UNDEF;
1530}
1531
1532static
1533XS(w32_FsType)
1534{
1535 dXSARGS;
1536 char fsname[256];
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)));
1544 PUTBACK;
1545 return;
1546 }
1547 XSRETURN_PV(fsname);
1548 }
1549 XSRETURN_UNDEF;
1550}
1551
1552static
1553XS(w32_GetOSVersion)
1554{
1555 dXSARGS;
1556 OSVERSIONINFO osver;
1557
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));
1565 PUTBACK;
1566 return;
1567 }
1568 XSRETURN_UNDEF;
1569}
1570
1571static
1572XS(w32_IsWinNT)
1573{
1574 dXSARGS;
1575 XSRETURN_IV(IsWinNT());
1576}
1577
1578static
1579XS(w32_IsWin95)
1580{
1581 dXSARGS;
1582 XSRETURN_IV(IsWin95());
1583}
1584
1585static
1586XS(w32_FormatMessage)
1587{
1588 dXSARGS;
1589 DWORD source = 0;
1590 char msgbuf[1024];
1591
1592 if (items != 1)
1593 croak("usage: Win32::FormatMessage($errno)");
1594
1595 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1596 &source, SvIV(ST(0)), 0,
1597 msgbuf, sizeof(msgbuf)-1, NULL))
1598 XSRETURN_PV(msgbuf);
1599
1600 XSRETURN_UNDEF;
1601}
1602
1603static
1604XS(w32_Spawn)
1605{
1606 dXSARGS;
1607 char *cmd, *args;
1608 PROCESS_INFORMATION stProcInfo;
1609 STARTUPINFO stStartInfo;
1610 BOOL bSuccess = FALSE;
1611
1612 if(items != 3)
1613 croak("usage: Win32::Spawn($cmdName, $args, $PID)");
1614
1615 cmd = SvPV(ST(0),na);
1616 args = SvPV(ST(1), na);
1617
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) */
1622
1623 if(CreateProcess(
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) */
1634 {
1635 CloseHandle(stProcInfo.hThread);/* library source code does this. */
1636 sv_setiv(ST(2), stProcInfo.dwProcessId);
1637 bSuccess = TRUE;
1638 }
1639 XSRETURN_IV(bSuccess);
1640}
1641
1642static
1643XS(w32_GetTickCount)
1644{
1645 dXSARGS;
1646 XSRETURN_IV(GetTickCount());
1647}
1648
1649static
1650XS(w32_GetShortPathName)
1651{
1652 dXSARGS;
1653 SV *shortpath;
e8bab181 1654 DWORD len;
ad2e33dc 1655
1656 if(items != 1)
1657 croak("usage: Win32::GetShortPathName($longPathName)");
1658
1659 shortpath = sv_mortalcopy(ST(0));
1660 SvUPGRADE(shortpath, SVt_PV);
1661 /* src == target is allowed */
e8bab181 1662 do {
1663 len = GetShortPathName(SvPVX(shortpath),
1664 SvPVX(shortpath),
1665 SvLEN(shortpath));
1666 } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1));
1667 if (len) {
1668 SvCUR_set(shortpath,len);
ad2e33dc 1669 ST(0) = shortpath;
e8bab181 1670 }
ad2e33dc 1671 else
1672 ST(0) = &sv_undef;
1673 XSRETURN(1);
1674}
1675
1676void
f3986ebb 1677Perl_init_os_extras()
ad2e33dc 1678{
1679 char *file = __FILE__;
1680 dXSUB_SYS;
1681
1682 /* XXX should be removed after checking with Nick */
1683 newXS("Win32::GetCurrentDirectory", w32_GetCwd, file);
1684
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);
1701
1702 /* XXX Bloat Alert! The following Activeware preloads really
1703 * ought to be part of Win32::Sys::*, so they're not included
1704 * here.
1705 */
1706 /* LookupAccountName
1707 * LookupAccountSID
1708 * InitiateSystemShutdown
1709 * AbortSystemShutdown
1710 * ExpandEnvrironmentStrings
1711 */
1712}
1713
1714void
1715Perl_win32_init(int *argcp, char ***argvp)
1716{
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.
1722 */
3dc9191e 1723#if !defined(_ALPHA_)
ad2e33dc 1724 _control87(MCW_EM, MCW_EM);
3dc9191e 1725#endif
ad2e33dc 1726}
d55594ae 1727
1728
1729
1730