win32 tweaks
[p5sagit/p5-mst-13.2.git] / win32 / win32.c
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  */
10
11 #define WIN32_LEAN_AND_MEAN
12 #define WIN32IO_IS_STDIO
13 #include <tchar.h>
14 #include <windows.h>
15
16 /* #include "config.h" */
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"
25 #include "XSUB.h"
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <assert.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <float.h>
32
33 #define CROAK croak
34 #define WARN warn
35
36 #define EXECF_EXEC 1
37 #define EXECF_SPAWN 2
38 #define EXECF_SPAWN_NOWAIT 3
39
40 static DWORD IdOS(void);
41
42 extern WIN32_IOSUBSYSTEM        win32stdio;
43 static PWIN32_IOSUBSYSTEM pIOSubSystem = &win32stdio;
44
45 BOOL  ProbeEnv = FALSE;
46 DWORD Win32System = (DWORD)-1;
47 char  szShellPath[MAX_PATH+1];
48 char  szPerlLibRoot[MAX_PATH+1];
49 HANDLE PerlDllHandle = INVALID_HANDLE_VALUE;
50
51 static int do_spawn2(char *cmd, int exectype);
52
53 int 
54 IsWin95(void) {
55     return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
56 }
57
58 int
59 IsWinNT(void) {
60     return (IdOS() == VER_PLATFORM_WIN32_NT);
61 }
62
63 void *
64 SetIOSubSystem(void *p)
65 {
66     PWIN32_IOSUBSYSTEM old = pIOSubSystem;
67     if (p) {
68         PWIN32_IOSUBSYSTEM pio = (PWIN32_IOSUBSYSTEM)p;
69         if (pio->signature_begin == 12345678L
70             && pio->signature_end == 87654321L) {
71             pIOSubSystem = pio;
72         }
73     }
74     else {
75         pIOSubSystem = &win32stdio;
76     }
77     return old;
78 }
79
80 char *
81 win32PerlLibPath(void)
82 {
83     char *end;
84     GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE) 
85                       ? GetModuleHandle(NULL)
86                       : PerlDllHandle,
87                       szPerlLibRoot, 
88                       sizeof(szPerlLibRoot));
89
90     *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
91     if (stricmp(end-4,"\\bin") == 0)
92      end -= 4;
93     strcpy(end,"\\lib");
94     return (szPerlLibRoot);
95 }
96
97 char *
98 win32SiteLibPath(void)
99 {
100     static char szPerlSiteLib[MAX_PATH+1];
101     strcpy(szPerlSiteLib, win32PerlLibPath());
102     strcat(szPerlSiteLib, "\\site");
103     return (szPerlSiteLib);
104 }
105
106 BOOL
107 HasRedirection(char *ptr)
108 {
109     int inquote = 0;
110     char quote = '\0';
111
112     /*
113      * Scan string looking for redirection (< or >) or pipe
114      * characters (|) that are not in a quoted string
115      */
116     while(*ptr) {
117         switch(*ptr) {
118         case '\'':
119         case '\"':
120             if(inquote) {
121                 if(quote == *ptr) {
122                     inquote = 0;
123                     quote = '\0';
124                 }
125             }
126             else {
127                 quote = *ptr;
128                 inquote++;
129             }
130             break;
131         case '>':
132         case '<':
133         case '|':
134             if(!inquote)
135                 return TRUE;
136         default:
137             break;
138         }
139         ++ptr;
140     }
141     return FALSE;
142 }
143
144 /* since the current process environment is being updated in util.c
145  * the library functions will get the correct environment
146  */
147 PerlIO *
148 my_popen(char *cmd, char *mode)
149 {
150 #ifdef FIXCMD
151 #define fixcmd(x)       {                                       \
152                             char *pspace = strchr((x),' ');     \
153                             if (pspace) {                       \
154                                 char *p = (x);                  \
155                                 while (p < pspace) {            \
156                                     if (*p == '/')              \
157                                         *p = '\\';              \
158                                     p++;                        \
159                                 }                               \
160                             }                                   \
161                         }
162 #else
163 #define fixcmd(x)
164 #endif
165
166 #if 1
167 /* was #ifndef PERLDLL, but the #else stuff doesn't work on NT
168  * GSAR 97/03/13
169  */
170     fixcmd(cmd);
171 #ifdef __BORLANDC__ /* workaround a Borland stdio bug */
172     win32_fflush(stdout);
173     win32_fflush(stderr);
174 #endif
175     return win32_popen(cmd, mode);
176 #else
177 /*
178  * There seems to be some problems for the _popen call in a DLL
179  * this trick at the moment seems to work but it is never test
180  * on NT yet
181  *
182  */ 
183 #       ifdef __cplusplus
184 #define EXT_C_FUNC      extern "C"
185 #       else
186 #define EXT_C_FUNC      extern
187 #       endif
188
189     EXT_C_FUNC int __cdecl _set_osfhnd(int fh, long value);
190     EXT_C_FUNC void __cdecl _lock_fhandle(int);
191     EXT_C_FUNC void __cdecl _unlock_fhandle(int);
192
193     BOOL        fSuccess;
194     PerlIO      *pf;            /* to store the _popen return value */
195     int         tm = 0;         /* flag indicating tDllExport or binary mode */
196     int         fhNeeded, fhInherited, fhDup;
197     int         ineeded, iinherited;
198     DWORD       dwDup;
199     int         phdls[2];       /* I/O handles for pipe */
200     HANDLE      hPIn, hPOut, hPErr,
201                 hSaveStdin, hSaveStdout, hSaveStderr,
202                 hPNeeded, hPInherited, hPDuped;
203      
204     /* first check for errors in the arguments */
205     if ( (cmd == NULL) || (mode == NULL)
206          || ((*mode != 'w') && (*mode != _T('r'))) )
207         goto error1;
208
209     if ( *(mode + 1) == _T('t') )
210         tm = O_TEXT;
211     else if ( *(mode + 1) == _T('b') )
212         tm = O_BINARY;
213     else
214         tm = (*mode == 'w' ? O_BINARY : O_TEXT);
215
216
217     fixcmd(cmd);
218     if (&win32stdio != pIOSubSystem)
219         return win32_popen(cmd, mode);
220
221 #ifdef EFG
222     if ( _pipe( phdls, 1024, tm ) == -1 )
223 #else
224     if ( win32_pipe( phdls, 1024, tm ) == -1 )
225 #endif
226         goto error1;
227
228     /* save the current situation */
229     hSaveStdin = GetStdHandle(STD_INPUT_HANDLE); 
230     hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
231     hSaveStderr = GetStdHandle(STD_ERROR_HANDLE); 
232
233     if (*mode == _T('w')) {
234         ineeded = 1;
235         dwDup   = STD_INPUT_HANDLE;
236         iinherited = 0;
237     }
238     else {
239         ineeded = 0;
240         dwDup   = STD_OUTPUT_HANDLE;
241         iinherited = 1;
242     }
243
244     fhNeeded = phdls[ineeded];
245     fhInherited = phdls[iinherited];
246
247     fSuccess = DuplicateHandle(GetCurrentProcess(), 
248                                (HANDLE) stolen_get_osfhandle(fhNeeded), 
249                                GetCurrentProcess(), 
250                                &hPNeeded, 
251                                0, 
252                                FALSE,       /* not inherited */ 
253                                DUPLICATE_SAME_ACCESS); 
254
255     if (!fSuccess)
256         goto error2;
257
258     fhDup = stolen_open_osfhandle((long) hPNeeded, tm);
259     win32_dup2(fhDup, fhNeeded);
260     win32_close(fhDup);
261
262 #ifdef AAA
263     /* Close the Out pipe, child won't need it */
264     hPDuped = (HANDLE) stolen_get_osfhandle(fhNeeded);
265
266     _lock_fhandle(fhNeeded);
267     _set_osfhnd(fhNeeded, (long)hPNeeded); /* put in ours duplicated one */
268     _unlock_fhandle(fhNeeded);
269
270     CloseHandle(hPDuped);       /* close the handle first */
271 #endif
272
273     if (!SetStdHandle(dwDup, (HANDLE) stolen_get_osfhandle(fhInherited)))
274         goto error2;
275
276     /*
277      * make sure the child see the same stderr as the calling program
278      */
279     if (!SetStdHandle(STD_ERROR_HANDLE,
280                       (HANDLE)stolen_get_osfhandle(win32_fileno(win32_stderr()))))
281         goto error2;
282
283     pf = win32_popen(cmd, mode);        /* ask _popen to do the job */
284
285     /* restore to where we were */
286     SetStdHandle(STD_INPUT_HANDLE, hSaveStdin);
287     SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout);
288     SetStdHandle(STD_ERROR_HANDLE, hSaveStderr);
289
290     /* we don't need it any more, that's for the child */
291     win32_close(fhInherited);
292
293     if (NULL == pf) {
294         /* something wrong */
295         win32_close(fhNeeded);
296         goto error1;
297     }
298     else {
299         /*
300          * here we steal the file handle in pf and stuff ours in
301          */
302         win32_dup2(fhNeeded, win32_fileno(pf));
303         win32_close(fhNeeded);
304     }
305     return (pf);
306
307 error2:
308     win32_close(fhNeeded);
309     win32_close(fhInherited);
310
311 error1:
312     return (NULL);
313
314 #endif
315 }
316
317 long
318 my_pclose(PerlIO *fp)
319 {
320     return win32_pclose(fp);
321 }
322
323 static DWORD
324 IdOS(void)
325 {
326     static OSVERSIONINFO osver;
327
328     if (osver.dwPlatformId != Win32System) {
329         memset(&osver, 0, sizeof(OSVERSIONINFO));
330         osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
331         GetVersionEx(&osver);
332         Win32System = osver.dwPlatformId;
333     }
334     return (Win32System);
335 }
336
337 static char *
338 GetShell(void)
339 {
340     if (!ProbeEnv) {
341         char* defaultshell = (IsWinNT() ? "cmd.exe" : "command.com");
342         /* we don't use COMSPEC here for two reasons:
343          *  1. the same reason perl on UNIX doesn't use SHELL--rampant and
344          *     uncontrolled unportability of the ensuing scripts.
345          *  2. PERL5SHELL could be set to a shell that may not be fit for
346          *     interactive use (which is what most programs look in COMSPEC
347          *     for).
348          */
349         char *usershell = getenv("PERL5SHELL");  
350
351         ProbeEnv = TRUE;
352         strcpy(szShellPath, usershell ? usershell : defaultshell);
353     }
354     return szShellPath;
355 }
356
357 int
358 do_aspawn(void* really, void** mark, void** arglast)
359 {
360     char **argv;
361     char *strPtr;
362     char *cmd;
363     int status;
364     unsigned int length;
365     int index = 0;
366     SV *sv = (SV*)really;
367     SV** pSv = (SV**)mark;
368
369     New(1310, argv, (arglast - mark) + 4, char*);
370
371     if(sv != Nullsv) {
372         cmd = SvPV(sv, length);
373     }
374     else {
375         argv[index++] = cmd = GetShell();
376         argv[index++] = "/x";   /* always enable command extensions */
377         argv[index++] = "/c";
378     }
379
380     while(++pSv <= (SV**)arglast) {
381         sv = *pSv;
382         strPtr = SvPV(sv, length);
383         if(strPtr != NULL && *strPtr != '\0')
384             argv[index++] = strPtr;
385     }
386     argv[index++] = 0;
387    
388     status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv);
389
390     Safefree(argv);
391
392     if (status < 0) {
393         if (dowarn)
394             warn("Can't spawn \"%s\": %s", cmd, strerror(errno));
395         status = 255 << 8;
396     }
397     return (status);
398 }
399
400 int
401 do_spawn2(char *cmd, int exectype)
402 {
403     char **a;
404     char *s;
405     char **argv;
406     int status = -1;
407     BOOL needToTry = TRUE;
408     char *shell, *cmd2;
409
410     /* save an extra exec if possible */
411     shell = GetShell();
412
413     /* see if there are shell metacharacters in it */
414     if(!HasRedirection(cmd)) {
415         New(1301,argv, strlen(cmd) / 2 + 2, char*);
416         New(1302,cmd2, strlen(cmd) + 1, char);
417         strcpy(cmd2, cmd);
418         a = argv;
419         for (s = cmd2; *s;) {
420             while (*s && isspace(*s))
421                 s++;
422             if (*s)
423                 *(a++) = s;
424             while(*s && !isspace(*s))
425                 s++;
426             if(*s)
427                 *s++ = '\0';
428         }
429         *a = Nullch;
430         if(argv[0]) {
431             switch (exectype) {
432             case EXECF_SPAWN:
433                 status = win32_spawnvp(P_WAIT, argv[0],
434                                        (const char* const*)argv);
435                 break;
436             case EXECF_SPAWN_NOWAIT:
437                 status = win32_spawnvp(P_NOWAIT, argv[0],
438                                        (const char* const*)argv);
439                 break;
440             case EXECF_EXEC:
441                 status = win32_execvp(argv[0], (const char* const*)argv);
442                 break;
443             }
444             if(status != -1 || errno == 0)
445                 needToTry = FALSE;
446         }
447         Safefree(argv);
448         Safefree(cmd2);
449     }
450     if(needToTry) {
451         char *argv[5];
452         argv[0] = shell; argv[1] = "/x"; argv[2] = "/c";
453         argv[3] = cmd; argv[4] = Nullch;
454         switch (exectype) {
455         case EXECF_SPAWN:
456             status = win32_spawnvp(P_WAIT, argv[0],
457                                    (const char* const*)argv);
458             break;
459         case EXECF_SPAWN_NOWAIT:
460             status = win32_spawnvp(P_NOWAIT, argv[0],
461                                    (const char* const*)argv);
462             break;
463         case EXECF_EXEC:
464             status = win32_execvp(argv[0], (const char* const*)argv);
465             break;
466         }
467     }
468     if (status < 0) {
469         if (dowarn)
470             warn("Can't %s \"%s\": %s",
471                  (exectype == EXECF_EXEC ? "exec" : "spawn"),
472                  needToTry ? shell : argv[0],
473                  strerror(errno));
474         status = 255 << 8;
475     }
476     return (status);
477 }
478
479 int
480 do_spawn(char *cmd)
481 {
482     return do_spawn2(cmd, EXECF_SPAWN);
483 }
484
485 bool
486 do_exec(char *cmd)
487 {
488     do_spawn2(cmd, EXECF_EXEC);
489     return FALSE;
490 }
491
492
493 #define PATHLEN 1024
494
495 /* The idea here is to read all the directory names into a string table
496  * (separated by nulls) and when one of the other dir functions is called
497  * return the pointer to the current file name.
498  */
499 DIR *
500 opendir(char *filename)
501 {
502     DIR            *p;
503     long            len;
504     long            idx;
505     char            scannamespc[PATHLEN];
506     char       *scanname = scannamespc;
507     struct stat     sbuf;
508     WIN32_FIND_DATA FindData;
509     HANDLE          fh;
510 /*  char            root[_MAX_PATH];*/
511 /*  char            volname[_MAX_PATH];*/
512 /*  DWORD           serial, maxname, flags;*/
513 /*  BOOL            downcase;*/
514 /*  char           *dummy;*/
515
516     /* check to see if filename is a directory */
517     if(stat(filename, &sbuf) < 0 || sbuf.st_mode & S_IFDIR == 0) {
518         return NULL;
519     }
520
521     /* get the file system characteristics */
522 /*  if(GetFullPathName(filename, MAX_PATH, root, &dummy)) {
523  *      if(dummy = strchr(root, '\\'))
524  *          *++dummy = '\0';
525  *      if(GetVolumeInformation(root, volname, MAX_PATH, &serial,
526  *                              &maxname, &flags, 0, 0)) {
527  *          downcase = !(flags & FS_CASE_IS_PRESERVED);
528  *      }
529  *  }
530  *  else {
531  *      downcase = TRUE;
532  *  }
533  */
534     /* Get us a DIR structure */
535     Newz(1303, p, 1, DIR);
536     if(p == NULL)
537         return NULL;
538
539     /* Create the search pattern */
540     strcpy(scanname, filename);
541
542     if(index("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
543         strcat(scanname, "/*");
544     else
545         strcat(scanname, "*");
546
547     /* do the FindFirstFile call */
548     fh = FindFirstFile(scanname, &FindData);
549     if(fh == INVALID_HANDLE_VALUE) {
550         return NULL;
551     }
552
553     /* now allocate the first part of the string table for
554      * the filenames that we find.
555      */
556     idx = strlen(FindData.cFileName)+1;
557     New(1304, p->start, idx, char);
558     if(p->start == NULL) {
559         CROAK("opendir: malloc failed!\n");
560     }
561     strcpy(p->start, FindData.cFileName);
562 /*  if(downcase)
563  *      strlwr(p->start);
564  */
565     p->nfiles++;
566
567     /* loop finding all the files that match the wildcard
568      * (which should be all of them in this directory!).
569      * the variable idx should point one past the null terminator
570      * of the previous string found.
571      */
572     while (FindNextFile(fh, &FindData)) {
573         len = strlen(FindData.cFileName);
574         /* bump the string table size by enough for the
575          * new name and it's null terminator
576          */
577         Renew(p->start, idx+len+1, char);
578         if(p->start == NULL) {
579             CROAK("opendir: malloc failed!\n");
580         }
581         strcpy(&p->start[idx], FindData.cFileName);
582 /*      if (downcase) 
583  *          strlwr(&p->start[idx]);
584  */
585                 p->nfiles++;
586                 idx += len+1;
587         }
588         FindClose(fh);
589         p->size = idx;
590         p->curr = p->start;
591         return p;
592 }
593
594
595 /* Readdir just returns the current string pointer and bumps the
596  * string pointer to the nDllExport entry.
597  */
598 struct direct *
599 readdir(DIR *dirp)
600 {
601     int         len;
602     static int  dummy = 0;
603
604     if (dirp->curr) {
605         /* first set up the structure to return */
606         len = strlen(dirp->curr);
607         strcpy(dirp->dirstr.d_name, dirp->curr);
608         dirp->dirstr.d_namlen = len;
609
610         /* Fake an inode */
611         dirp->dirstr.d_ino = dummy++;
612
613         /* Now set up for the nDllExport call to readdir */
614         dirp->curr += len + 1;
615         if (dirp->curr >= (dirp->start + dirp->size)) {
616             dirp->curr = NULL;
617         }
618
619         return &(dirp->dirstr);
620     } 
621     else
622         return NULL;
623 }
624
625 /* Telldir returns the current string pointer position */
626 long
627 telldir(DIR *dirp)
628 {
629     return (long) dirp->curr;
630 }
631
632
633 /* Seekdir moves the string pointer to a previously saved position
634  *(Saved by telldir).
635  */
636 void
637 seekdir(DIR *dirp, long loc)
638 {
639     dirp->curr = (char *)loc;
640 }
641
642 /* Rewinddir resets the string pointer to the start */
643 void
644 rewinddir(DIR *dirp)
645 {
646     dirp->curr = dirp->start;
647 }
648
649 /* free the memory allocated by opendir */
650 int
651 closedir(DIR *dirp)
652 {
653     Safefree(dirp->start);
654     Safefree(dirp);
655     return 1;
656 }
657
658
659 /*
660  * various stubs
661  */
662
663
664 /* Ownership
665  *
666  * Just pretend that everyone is a superuser. NT will let us know if
667  * we don\'t really have permission to do something.
668  */
669
670 #define ROOT_UID    ((uid_t)0)
671 #define ROOT_GID    ((gid_t)0)
672
673 uid_t
674 getuid(void)
675 {
676     return ROOT_UID;
677 }
678
679 uid_t
680 geteuid(void)
681 {
682     return ROOT_UID;
683 }
684
685 gid_t
686 getgid(void)
687 {
688     return ROOT_GID;
689 }
690
691 gid_t
692 getegid(void)
693 {
694     return ROOT_GID;
695 }
696
697 int
698 setuid(uid_t uid)
699
700     return (uid == ROOT_UID ? 0 : -1);
701 }
702
703 int
704 setgid(gid_t gid)
705 {
706     return (gid == ROOT_GID ? 0 : -1);
707 }
708
709 /*
710  * pretended kill
711  */
712 int
713 kill(int pid, int sig)
714 {
715     HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
716
717     if (hProcess == NULL) {
718         CROAK("kill process failed!\n");
719     }
720     else {
721         if (!TerminateProcess(hProcess, sig))
722             CROAK("kill process failed!\n");
723         CloseHandle(hProcess);
724     }
725     return 0;
726 }
727       
728 /*
729  * File system stuff
730  */
731
732 #if 0
733 int
734 ioctl(int i, unsigned int u, char *data)
735 {
736     CROAK("ioctl not implemented!\n");
737     return -1;
738 }
739 #endif
740
741 unsigned int
742 sleep(unsigned int t)
743 {
744     Sleep(t*1000);
745     return 0;
746 }
747
748
749 #undef rename
750
751 int
752 myrename(char *OldFileName, char *newname)
753 {
754     if(_access(newname, 0) != -1) {     /* file exists */
755         _unlink(newname);
756     }
757     return rename(OldFileName, newname);
758 }
759
760
761 DllExport int
762 win32_stat(const char *path, struct stat *buffer)
763 {
764     char                t[MAX_PATH]; 
765     const char  *p = path;
766     int         l = strlen(path);
767
768     if (l > 1) {
769         switch(path[l - 1]) {
770         case '\\':
771         case '/':
772             if (path[l - 2] != ':') {
773                 strncpy(t, path, l - 1);
774                 t[l - 1] = 0;
775                 p = t;
776             };
777         }
778     }
779     return stat(p, buffer);
780 }
781
782 #ifndef USE_WIN32_RTL_ENV
783
784 DllExport char *
785 win32_getenv(const char *name)
786 {
787     static char *curitem = Nullch;
788     static DWORD curlen = 512;
789     DWORD needlen;
790     if (!curitem)
791         New(1305,curitem,curlen,char);
792     if (!(needlen = GetEnvironmentVariable(name,curitem,curlen)))
793         return Nullch;
794     while (needlen > curlen) {
795         Renew(curitem,needlen,char);
796         curlen = needlen;
797         needlen = GetEnvironmentVariable(name,curitem,curlen);
798     }
799     return curitem;
800 }
801
802 #endif
803
804 #undef times
805 int
806 mytimes(struct tms *timebuf)
807 {
808     clock_t     t = clock();
809     timebuf->tms_utime = t;
810     timebuf->tms_stime = 0;
811     timebuf->tms_cutime = 0;
812     timebuf->tms_cstime = 0;
813
814     return 0;
815 }
816
817 #undef alarm
818 unsigned int
819 myalarm(unsigned int sec)
820 {
821     /* we warn the usuage of alarm function */
822     if (sec != 0)
823         WARN("dummy function alarm called, program might not function as expected\n");
824     return 0;
825 }
826
827 /*
828  *  redirected io subsystem for all XS modules
829  *
830  */
831
832 DllExport int *
833 win32_errno(void)
834 {
835     return (pIOSubSystem->pfnerrno());
836 }
837
838 DllExport char ***
839 win32_environ(void)
840 {
841     return (pIOSubSystem->pfnenviron());
842 }
843
844 /* the rest are the remapped stdio routines */
845 DllExport FILE *
846 win32_stderr(void)
847 {
848     return (pIOSubSystem->pfnstderr());
849 }
850
851 DllExport FILE *
852 win32_stdin(void)
853 {
854     return (pIOSubSystem->pfnstdin());
855 }
856
857 DllExport FILE *
858 win32_stdout()
859 {
860     return (pIOSubSystem->pfnstdout());
861 }
862
863 DllExport int
864 win32_ferror(FILE *fp)
865 {
866     return (pIOSubSystem->pfnferror(fp));
867 }
868
869
870 DllExport int
871 win32_feof(FILE *fp)
872 {
873     return (pIOSubSystem->pfnfeof(fp));
874 }
875
876 /*
877  * Since the errors returned by the socket error function 
878  * WSAGetLastError() are not known by the library routine strerror
879  * we have to roll our own.
880  */
881
882 __declspec(thread) char strerror_buffer[512];
883
884 DllExport char *
885 win32_strerror(int e) 
886 {
887 #ifndef __BORLANDC__            /* Borland intolerance */
888     extern int sys_nerr;
889 #endif
890     DWORD source = 0;
891
892     if(e < 0 || e > sys_nerr) {
893         if(e < 0)
894             e = GetLastError();
895
896         if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
897                          strerror_buffer, sizeof(strerror_buffer), NULL) == 0) 
898             strcpy(strerror_buffer, "Unknown Error");
899
900         return strerror_buffer;
901     }
902     return pIOSubSystem->pfnstrerror(e);
903 }
904
905 DllExport int
906 win32_fprintf(FILE *fp, const char *format, ...)
907 {
908     va_list marker;
909     va_start(marker, format);     /* Initialize variable arguments. */
910
911     return (pIOSubSystem->pfnvfprintf(fp, format, marker));
912 }
913
914 DllExport int
915 win32_printf(const char *format, ...)
916 {
917     va_list marker;
918     va_start(marker, format);     /* Initialize variable arguments. */
919
920     return (pIOSubSystem->pfnvprintf(format, marker));
921 }
922
923 DllExport int
924 win32_vfprintf(FILE *fp, const char *format, va_list args)
925 {
926     return (pIOSubSystem->pfnvfprintf(fp, format, args));
927 }
928
929 DllExport int
930 win32_vprintf(const char *format, va_list args)
931 {
932     return (pIOSubSystem->pfnvprintf(format, args));
933 }
934
935 DllExport size_t
936 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
937 {
938     return pIOSubSystem->pfnfread(buf, size, count, fp);
939 }
940
941 DllExport size_t
942 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
943 {
944     return pIOSubSystem->pfnfwrite(buf, size, count, fp);
945 }
946
947 DllExport FILE *
948 win32_fopen(const char *filename, const char *mode)
949 {
950     if (stricmp(filename, "/dev/null")==0)
951         return pIOSubSystem->pfnfopen("NUL", mode);
952     return pIOSubSystem->pfnfopen(filename, mode);
953 }
954
955 DllExport FILE *
956 win32_fdopen( int handle, const char *mode)
957 {
958     return pIOSubSystem->pfnfdopen(handle, mode);
959 }
960
961 DllExport FILE *
962 win32_freopen( const char *path, const char *mode, FILE *stream)
963 {
964     if (stricmp(path, "/dev/null")==0)
965         return pIOSubSystem->pfnfreopen("NUL", mode, stream);
966     return pIOSubSystem->pfnfreopen(path, mode, stream);
967 }
968
969 DllExport int
970 win32_fclose(FILE *pf)
971 {
972     return pIOSubSystem->pfnfclose(pf);
973 }
974
975 DllExport int
976 win32_fputs(const char *s,FILE *pf)
977 {
978     return pIOSubSystem->pfnfputs(s, pf);
979 }
980
981 DllExport int
982 win32_fputc(int c,FILE *pf)
983 {
984     return pIOSubSystem->pfnfputc(c,pf);
985 }
986
987 DllExport int
988 win32_ungetc(int c,FILE *pf)
989 {
990     return pIOSubSystem->pfnungetc(c,pf);
991 }
992
993 DllExport int
994 win32_getc(FILE *pf)
995 {
996     return pIOSubSystem->pfngetc(pf);
997 }
998
999 DllExport int
1000 win32_fileno(FILE *pf)
1001 {
1002     return pIOSubSystem->pfnfileno(pf);
1003 }
1004
1005 DllExport void
1006 win32_clearerr(FILE *pf)
1007 {
1008     pIOSubSystem->pfnclearerr(pf);
1009     return;
1010 }
1011
1012 DllExport int
1013 win32_fflush(FILE *pf)
1014 {
1015     return pIOSubSystem->pfnfflush(pf);
1016 }
1017
1018 DllExport long
1019 win32_ftell(FILE *pf)
1020 {
1021     return pIOSubSystem->pfnftell(pf);
1022 }
1023
1024 DllExport int
1025 win32_fseek(FILE *pf,long offset,int origin)
1026 {
1027     return pIOSubSystem->pfnfseek(pf, offset, origin);
1028 }
1029
1030 DllExport int
1031 win32_fgetpos(FILE *pf,fpos_t *p)
1032 {
1033     return pIOSubSystem->pfnfgetpos(pf, p);
1034 }
1035
1036 DllExport int
1037 win32_fsetpos(FILE *pf,const fpos_t *p)
1038 {
1039     return pIOSubSystem->pfnfsetpos(pf, p);
1040 }
1041
1042 DllExport void
1043 win32_rewind(FILE *pf)
1044 {
1045     pIOSubSystem->pfnrewind(pf);
1046     return;
1047 }
1048
1049 DllExport FILE*
1050 win32_tmpfile(void)
1051 {
1052     return pIOSubSystem->pfntmpfile();
1053 }
1054
1055 DllExport void
1056 win32_abort(void)
1057 {
1058     pIOSubSystem->pfnabort();
1059     return;
1060 }
1061
1062 DllExport int
1063 win32_fstat(int fd,struct stat *bufptr)
1064 {
1065     return pIOSubSystem->pfnfstat(fd,bufptr);
1066 }
1067
1068 DllExport int
1069 win32_pipe(int *pfd, unsigned int size, int mode)
1070 {
1071     return pIOSubSystem->pfnpipe(pfd, size, mode);
1072 }
1073
1074 DllExport FILE*
1075 win32_popen(const char *command, const char *mode)
1076 {
1077     return pIOSubSystem->pfnpopen(command, mode);
1078 }
1079
1080 DllExport int
1081 win32_pclose(FILE *pf)
1082 {
1083     return pIOSubSystem->pfnpclose(pf);
1084 }
1085
1086 DllExport int
1087 win32_setmode(int fd, int mode)
1088 {
1089     return pIOSubSystem->pfnsetmode(fd, mode);
1090 }
1091
1092 DllExport long
1093 win32_lseek(int fd, long offset, int origin)
1094 {
1095     return pIOSubSystem->pfnlseek(fd, offset, origin);
1096 }
1097
1098 DllExport long
1099 win32_tell(int fd)
1100 {
1101     return pIOSubSystem->pfntell(fd);
1102 }
1103
1104 DllExport int
1105 win32_open(const char *path, int flag, ...)
1106 {
1107     va_list ap;
1108     int pmode;
1109
1110     va_start(ap, flag);
1111     pmode = va_arg(ap, int);
1112     va_end(ap);
1113
1114     if (stricmp(path, "/dev/null")==0)
1115         return pIOSubSystem->pfnopen("NUL", flag, pmode);
1116     return pIOSubSystem->pfnopen(path,flag,pmode);
1117 }
1118
1119 DllExport int
1120 win32_close(int fd)
1121 {
1122     return pIOSubSystem->pfnclose(fd);
1123 }
1124
1125 DllExport int
1126 win32_eof(int fd)
1127 {
1128     return pIOSubSystem->pfneof(fd);
1129 }
1130
1131 DllExport int
1132 win32_dup(int fd)
1133 {
1134     return pIOSubSystem->pfndup(fd);
1135 }
1136
1137 DllExport int
1138 win32_dup2(int fd1,int fd2)
1139 {
1140     return pIOSubSystem->pfndup2(fd1,fd2);
1141 }
1142
1143 DllExport int
1144 win32_read(int fd, void *buf, unsigned int cnt)
1145 {
1146     return pIOSubSystem->pfnread(fd, buf, cnt);
1147 }
1148
1149 DllExport int
1150 win32_write(int fd, const void *buf, unsigned int cnt)
1151 {
1152     return pIOSubSystem->pfnwrite(fd, buf, cnt);
1153 }
1154
1155 DllExport int
1156 win32_mkdir(const char *dir, int mode)
1157 {
1158     return pIOSubSystem->pfnmkdir(dir); /* just ignore mode */
1159 }
1160
1161 DllExport int
1162 win32_rmdir(const char *dir)
1163 {
1164     return pIOSubSystem->pfnrmdir(dir);
1165 }
1166
1167 DllExport int
1168 win32_chdir(const char *dir)
1169 {
1170     return pIOSubSystem->pfnchdir(dir);
1171 }
1172
1173 DllExport int
1174 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1175 {
1176     return pIOSubSystem->pfnspawnvp(mode, cmdname, argv);
1177 }
1178
1179 DllExport int
1180 win32_execvp(const char *cmdname, const char *const *argv)
1181 {
1182     return pIOSubSystem->pfnexecvp(cmdname, argv);
1183 }
1184
1185 int
1186 stolen_open_osfhandle(long handle, int flags)
1187 {
1188     return pIOSubSystem->pfn_open_osfhandle(handle, flags);
1189 }
1190
1191 long
1192 stolen_get_osfhandle(int fd)
1193 {
1194     return pIOSubSystem->pfn_get_osfhandle(fd);
1195 }
1196
1197 /*
1198  * Extras.
1199  */
1200
1201 DllExport int
1202 win32_flock(int fd, int oper)
1203 {
1204     if (!IsWinNT()) {
1205         croak("flock() unimplemented on this platform");
1206         return -1;
1207     }
1208     return pIOSubSystem->pfnflock(fd, oper);
1209 }
1210
1211 static
1212 XS(w32_GetCwd)
1213 {
1214     dXSARGS;
1215     SV *sv = sv_newmortal();
1216     /* Make one call with zero size - return value is required size */
1217     DWORD len = GetCurrentDirectory((DWORD)0,NULL);
1218     SvUPGRADE(sv,SVt_PV);
1219     SvGROW(sv,len);
1220     SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv));
1221     /* 
1222      * If result != 0 
1223      *   then it worked, set PV valid, 
1224      *   else leave it 'undef' 
1225      */
1226     if (SvCUR(sv))
1227         SvPOK_on(sv);
1228     EXTEND(sp,1);
1229     ST(0) = sv;
1230     XSRETURN(1);
1231 }
1232
1233 static
1234 XS(w32_SetCwd)
1235 {
1236     dXSARGS;
1237     if (items != 1)
1238         croak("usage: Win32::SetCurrentDirectory($cwd)");
1239     if (SetCurrentDirectory(SvPV(ST(0),na)))
1240         XSRETURN_YES;
1241
1242     XSRETURN_NO;
1243 }
1244
1245 static
1246 XS(w32_GetNextAvailDrive)
1247 {
1248     dXSARGS;
1249     char ix = 'C';
1250     char root[] = "_:\\";
1251     while (ix <= 'Z') {
1252         root[0] = ix++;
1253         if (GetDriveType(root) == 1) {
1254             root[2] = '\0';
1255             XSRETURN_PV(root);
1256         }
1257     }
1258     XSRETURN_UNDEF;
1259 }
1260
1261 static
1262 XS(w32_GetLastError)
1263 {
1264     dXSARGS;
1265     XSRETURN_IV(GetLastError());
1266 }
1267
1268 static
1269 XS(w32_LoginName)
1270 {
1271     dXSARGS;
1272     char name[256];
1273     DWORD size = sizeof(name);
1274     if (GetUserName(name,&size)) {
1275         /* size includes NULL */
1276         ST(0) = sv_2mortal(newSVpv(name,size-1));
1277         XSRETURN(1);
1278     }
1279     XSRETURN_UNDEF;
1280 }
1281
1282 static
1283 XS(w32_NodeName)
1284 {
1285     dXSARGS;
1286     char name[MAX_COMPUTERNAME_LENGTH+1];
1287     DWORD size = sizeof(name);
1288     if (GetComputerName(name,&size)) {
1289         /* size does NOT include NULL :-( */
1290         ST(0) = sv_2mortal(newSVpv(name,size));
1291         XSRETURN(1);
1292     }
1293     XSRETURN_UNDEF;
1294 }
1295
1296
1297 static
1298 XS(w32_DomainName)
1299 {
1300     dXSARGS;
1301     char name[256];
1302     DWORD size = sizeof(name);
1303     if (GetUserName(name,&size)) {
1304         char sid[1024];
1305         DWORD sidlen = sizeof(sid);
1306         char dname[256];
1307         DWORD dnamelen = sizeof(dname);
1308         SID_NAME_USE snu;
1309         if (LookupAccountName(NULL, name, &sid, &sidlen,
1310                               dname, &dnamelen, &snu)) {
1311             XSRETURN_PV(dname);         /* all that for this */
1312         }
1313     }
1314     XSRETURN_UNDEF;
1315 }
1316
1317 static
1318 XS(w32_FsType)
1319 {
1320     dXSARGS;
1321     char fsname[256];
1322     DWORD flags, filecomplen;
1323     if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen,
1324                          &flags, fsname, sizeof(fsname))) {
1325         if (GIMME == G_ARRAY) {
1326             XPUSHs(sv_2mortal(newSVpv(fsname,0)));
1327             XPUSHs(sv_2mortal(newSViv(flags)));
1328             XPUSHs(sv_2mortal(newSViv(filecomplen)));
1329             PUTBACK;
1330             return;
1331         }
1332         XSRETURN_PV(fsname);
1333     }
1334     XSRETURN_UNDEF;
1335 }
1336
1337 static
1338 XS(w32_GetOSVersion)
1339 {
1340     dXSARGS;
1341     OSVERSIONINFO osver;
1342
1343     osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1344     if (GetVersionEx(&osver)) {
1345         XPUSHs(newSVpv(osver.szCSDVersion, 0));
1346         XPUSHs(newSViv(osver.dwMajorVersion));
1347         XPUSHs(newSViv(osver.dwMinorVersion));
1348         XPUSHs(newSViv(osver.dwBuildNumber));
1349         XPUSHs(newSViv(osver.dwPlatformId));
1350         PUTBACK;
1351         return;
1352     }
1353     XSRETURN_UNDEF;
1354 }
1355
1356 static
1357 XS(w32_IsWinNT)
1358 {
1359     dXSARGS;
1360     XSRETURN_IV(IsWinNT());
1361 }
1362
1363 static
1364 XS(w32_IsWin95)
1365 {
1366     dXSARGS;
1367     XSRETURN_IV(IsWin95());
1368 }
1369
1370 static
1371 XS(w32_FormatMessage)
1372 {
1373     dXSARGS;
1374     DWORD source = 0;
1375     char msgbuf[1024];
1376
1377     if (items != 1)
1378         croak("usage: Win32::FormatMessage($errno)");
1379
1380     if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1381                       &source, SvIV(ST(0)), 0,
1382                       msgbuf, sizeof(msgbuf)-1, NULL))
1383         XSRETURN_PV(msgbuf);
1384
1385     XSRETURN_UNDEF;
1386 }
1387
1388 static
1389 XS(w32_Spawn)
1390 {
1391     dXSARGS;
1392     char *cmd, *args;
1393     PROCESS_INFORMATION stProcInfo;
1394     STARTUPINFO stStartInfo;
1395     BOOL bSuccess = FALSE;
1396
1397     if(items != 3)
1398         croak("usage: Win32::Spawn($cmdName, $args, $PID)");
1399
1400     cmd = SvPV(ST(0),na);
1401     args = SvPV(ST(1), na);
1402
1403     memset(&stStartInfo, 0, sizeof(stStartInfo));   /* Clear the block */
1404     stStartInfo.cb = sizeof(stStartInfo);           /* Set the structure size */
1405     stStartInfo.dwFlags = STARTF_USESHOWWINDOW;     /* Enable wShowWindow control */
1406     stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE;   /* Start min (normal) */
1407
1408     if(CreateProcess(
1409                 cmd,                    /* Image path */
1410                 args,                   /* Arguments for command line */
1411                 NULL,                   /* Default process security */
1412                 NULL,                   /* Default thread security */
1413                 FALSE,                  /* Must be TRUE to use std handles */
1414                 NORMAL_PRIORITY_CLASS,  /* No special scheduling */
1415                 NULL,                   /* Inherit our environment block */
1416                 NULL,                   /* Inherit our currrent directory */
1417                 &stStartInfo,           /* -> Startup info */
1418                 &stProcInfo))           /* <- Process info (if OK) */
1419     {
1420         CloseHandle(stProcInfo.hThread);/* library source code does this. */
1421         sv_setiv(ST(2), stProcInfo.dwProcessId);
1422         bSuccess = TRUE;
1423     }
1424     XSRETURN_IV(bSuccess);
1425 }
1426
1427 static
1428 XS(w32_GetTickCount)
1429 {
1430     dXSARGS;
1431     XSRETURN_IV(GetTickCount());
1432 }
1433
1434 static
1435 XS(w32_GetShortPathName)
1436 {
1437     dXSARGS;
1438     SV *shortpath;
1439
1440     if(items != 1)
1441         croak("usage: Win32::GetShortPathName($longPathName)");
1442
1443     shortpath = sv_mortalcopy(ST(0));
1444     SvUPGRADE(shortpath, SVt_PV);
1445     /* src == target is allowed */
1446     if (GetShortPathName(SvPVX(shortpath), SvPVX(shortpath), SvCUR(shortpath)))
1447         ST(0) = shortpath;
1448     else
1449         ST(0) = &sv_undef;
1450     XSRETURN(1);
1451 }
1452
1453 void
1454 init_os_extras()
1455 {
1456     char *file = __FILE__;
1457     dXSUB_SYS;
1458
1459     /* XXX should be removed after checking with Nick */
1460     newXS("Win32::GetCurrentDirectory", w32_GetCwd, file);
1461
1462     /* these names are Activeware compatible */
1463     newXS("Win32::GetCwd", w32_GetCwd, file);
1464     newXS("Win32::SetCwd", w32_SetCwd, file);
1465     newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
1466     newXS("Win32::GetLastError", w32_GetLastError, file);
1467     newXS("Win32::LoginName", w32_LoginName, file);
1468     newXS("Win32::NodeName", w32_NodeName, file);
1469     newXS("Win32::DomainName", w32_DomainName, file);
1470     newXS("Win32::FsType", w32_FsType, file);
1471     newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
1472     newXS("Win32::IsWinNT", w32_IsWinNT, file);
1473     newXS("Win32::IsWin95", w32_IsWin95, file);
1474     newXS("Win32::FormatMessage", w32_FormatMessage, file);
1475     newXS("Win32::Spawn", w32_Spawn, file);
1476     newXS("Win32::GetTickCount", w32_GetTickCount, file);
1477     newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
1478
1479     /* XXX Bloat Alert! The following Activeware preloads really
1480      * ought to be part of Win32::Sys::*, so they're not included
1481      * here.
1482      */
1483     /* LookupAccountName
1484      * LookupAccountSID
1485      * InitiateSystemShutdown
1486      * AbortSystemShutdown
1487      * ExpandEnvrironmentStrings
1488      */
1489 }
1490
1491 void
1492 Perl_win32_init(int *argcp, char ***argvp)
1493 {
1494     /* Disable floating point errors, Perl will trap the ones we
1495      * care about.  VC++ RTL defaults to switching these off
1496      * already, but the Borland RTL doesn't.  Since we don't
1497      * want to be at the vendor's whim on the default, we set
1498      * it explicitly here.
1499      */
1500     _control87(MCW_EM, MCW_EM);
1501 }