653cdf79cb8c1a7794b17eb40d11259d3385bca4
[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 <fcntl.h>
26 #include <sys/stat.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <stdarg.h>
30
31 #define CROAK croak
32 #define WARN warn
33
34 static DWORD IdOS(void);
35
36 extern WIN32_IOSUBSYSTEM        win32stdio;
37 static PWIN32_IOSUBSYSTEM pIOSubSystem = &win32stdio;
38
39 BOOL  ProbeEnv = FALSE;
40 DWORD Win32System = (DWORD)-1;
41 char  szShellPath[MAX_PATH+1];
42 char  szPerlLibRoot[MAX_PATH+1];
43 HANDLE PerlDllHandle = INVALID_HANDLE_VALUE;
44
45 int 
46 IsWin95(void) {
47     return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
48 }
49
50 int
51 IsWinNT(void) {
52     return (IdOS() == VER_PLATFORM_WIN32_NT);
53 }
54
55 void *
56 SetIOSubSystem(void *p)
57 {
58     PWIN32_IOSUBSYSTEM old = pIOSubSystem;
59     if (p) {
60         PWIN32_IOSUBSYSTEM pio = (PWIN32_IOSUBSYSTEM)p;
61         if (pio->signature_begin == 12345678L
62             && pio->signature_end == 87654321L) {
63             pIOSubSystem = pio;
64         }
65     }
66     else {
67         pIOSubSystem = &win32stdio;
68     }
69     return old;
70 }
71
72 char *
73 win32PerlLibPath(void)
74 {
75     char *end;
76     GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE) 
77                       ? GetModuleHandle(NULL)
78                       : PerlDllHandle,
79                       szPerlLibRoot, 
80                       sizeof(szPerlLibRoot));
81
82     *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
83     if (stricmp(end-4,"\\bin") == 0)
84      end -= 4;
85     strcpy(end,"\\lib");
86     return (szPerlLibRoot);
87 }
88
89 BOOL
90 HasRedirection(char *ptr)
91 {
92     int inquote = 0;
93     char quote = '\0';
94
95     /*
96      * Scan string looking for redirection (< or >) or pipe
97      * characters (|) that are not in a quoted string
98      */
99     while(*ptr) {
100         switch(*ptr) {
101         case '\'':
102         case '\"':
103             if(inquote) {
104                 if(quote == *ptr) {
105                     inquote = 0;
106                     quote = '\0';
107                 }
108             }
109             else {
110                 quote = *ptr;
111                 inquote++;
112             }
113             break;
114         case '>':
115         case '<':
116         case '|':
117             if(!inquote)
118                 return TRUE;
119         default:
120             break;
121         }
122         ++ptr;
123     }
124     return FALSE;
125 }
126
127 /* since the current process environment is being updated in util.c
128  * the library functions will get the correct environment
129  */
130 PerlIO *
131 my_popen(char *cmd, char *mode)
132 {
133 #ifdef FIXCMD
134 #define fixcmd(x)       {                                       \
135                             char *pspace = strchr((x),' ');     \
136                             if (pspace) {                       \
137                                 char *p = (x);                  \
138                                 while (p < pspace) {            \
139                                     if (*p == '/')              \
140                                         *p = '\\';              \
141                                     p++;                        \
142                                 }                               \
143                             }                                   \
144                         }
145 #else
146 #define fixcmd(x)
147 #endif
148
149 #if 1
150 /* was #ifndef PERLDLL, but the #else stuff doesn't work on NT
151  * GSAR 97/03/13
152  */
153     fixcmd(cmd);
154 #ifdef __BORLANDC__ /* workaround a Borland stdio bug */
155     win32_fflush(stdout);
156     win32_fflush(stderr);
157 #endif
158     return win32_popen(cmd, mode);
159 #else
160 /*
161  * There seems to be some problems for the _popen call in a DLL
162  * this trick at the moment seems to work but it is never test
163  * on NT yet
164  *
165  */ 
166 #       ifdef __cplusplus
167 #define EXT_C_FUNC      extern "C"
168 #       else
169 #define EXT_C_FUNC      extern
170 #       endif
171
172     EXT_C_FUNC int __cdecl _set_osfhnd(int fh, long value);
173     EXT_C_FUNC void __cdecl _lock_fhandle(int);
174     EXT_C_FUNC void __cdecl _unlock_fhandle(int);
175
176     BOOL        fSuccess;
177     PerlIO      *pf;            /* to store the _popen return value */
178     int         tm = 0;         /* flag indicating tDllExport or binary mode */
179     int         fhNeeded, fhInherited, fhDup;
180     int         ineeded, iinherited;
181     DWORD       dwDup;
182     int         phdls[2];       /* I/O handles for pipe */
183     HANDLE      hPIn, hPOut, hPErr,
184                 hSaveStdin, hSaveStdout, hSaveStderr,
185                 hPNeeded, hPInherited, hPDuped;
186      
187     /* first check for errors in the arguments */
188     if ( (cmd == NULL) || (mode == NULL)
189          || ((*mode != 'w') && (*mode != _T('r'))) )
190         goto error1;
191
192     if ( *(mode + 1) == _T('t') )
193         tm = O_TEXT;
194     else if ( *(mode + 1) == _T('b') )
195         tm = O_BINARY;
196     else
197         tm = (*mode == 'w' ? O_BINARY : O_TEXT);
198
199
200     fixcmd(cmd);
201     if (&win32stdio != pIOSubSystem)
202         return win32_popen(cmd, mode);
203
204 #ifdef EFG
205     if ( _pipe( phdls, 1024, tm ) == -1 )
206 #else
207     if ( win32_pipe( phdls, 1024, tm ) == -1 )
208 #endif
209         goto error1;
210
211     /* save the current situation */
212     hSaveStdin = GetStdHandle(STD_INPUT_HANDLE); 
213     hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
214     hSaveStderr = GetStdHandle(STD_ERROR_HANDLE); 
215
216     if (*mode == _T('w')) {
217         ineeded = 1;
218         dwDup   = STD_INPUT_HANDLE;
219         iinherited = 0;
220     }
221     else {
222         ineeded = 0;
223         dwDup   = STD_OUTPUT_HANDLE;
224         iinherited = 1;
225     }
226
227     fhNeeded = phdls[ineeded];
228     fhInherited = phdls[iinherited];
229
230     fSuccess = DuplicateHandle(GetCurrentProcess(), 
231                                (HANDLE) stolen_get_osfhandle(fhNeeded), 
232                                GetCurrentProcess(), 
233                                &hPNeeded, 
234                                0, 
235                                FALSE,       /* not inherited */ 
236                                DUPLICATE_SAME_ACCESS); 
237
238     if (!fSuccess)
239         goto error2;
240
241     fhDup = stolen_open_osfhandle((long) hPNeeded, tm);
242     win32_dup2(fhDup, fhNeeded);
243     win32_close(fhDup);
244
245 #ifdef AAA
246     /* Close the Out pipe, child won't need it */
247     hPDuped = (HANDLE) stolen_get_osfhandle(fhNeeded);
248
249     _lock_fhandle(fhNeeded);
250     _set_osfhnd(fhNeeded, (long)hPNeeded); /* put in ours duplicated one */
251     _unlock_fhandle(fhNeeded);
252
253     CloseHandle(hPDuped);       /* close the handle first */
254 #endif
255
256     if (!SetStdHandle(dwDup, (HANDLE) stolen_get_osfhandle(fhInherited)))
257         goto error2;
258
259     /*
260      * make sure the child see the same stderr as the calling program
261      */
262     if (!SetStdHandle(STD_ERROR_HANDLE,
263                       (HANDLE)stolen_get_osfhandle(win32_fileno(win32_stderr()))))
264         goto error2;
265
266     pf = win32_popen(cmd, mode);        /* ask _popen to do the job */
267
268     /* restore to where we were */
269     SetStdHandle(STD_INPUT_HANDLE, hSaveStdin);
270     SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout);
271     SetStdHandle(STD_ERROR_HANDLE, hSaveStderr);
272
273     /* we don't need it any more, that's for the child */
274     win32_close(fhInherited);
275
276     if (NULL == pf) {
277         /* something wrong */
278         win32_close(fhNeeded);
279         goto error1;
280     }
281     else {
282         /*
283          * here we steal the file handle in pf and stuff ours in
284          */
285         win32_dup2(fhNeeded, win32_fileno(pf));
286         win32_close(fhNeeded);
287     }
288     return (pf);
289
290 error2:
291     win32_close(fhNeeded);
292     win32_close(fhInherited);
293
294 error1:
295     return (NULL);
296
297 #endif
298 }
299
300 long
301 my_pclose(PerlIO *fp)
302 {
303     return win32_pclose(fp);
304 }
305
306 static DWORD
307 IdOS(void)
308 {
309     static OSVERSIONINFO osver;
310
311     if (osver.dwPlatformId != Win32System) {
312         memset(&osver, 0, sizeof(OSVERSIONINFO));
313         osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
314         GetVersionEx(&osver);
315         Win32System = osver.dwPlatformId;
316     }
317     return (Win32System);
318 }
319
320 static char *
321 GetShell(void)
322 {
323     if (!ProbeEnv) {
324         char* defaultshell = (IsWinNT() ? "cmd.exe" : "command.com");
325         /* we don't use COMSPEC here for two reasons:
326          *  1. the same reason perl on UNIX doesn't use SHELL--rampant and
327          *     uncontrolled unportability of the ensuing scripts.
328          *  2. PERL5SHELL could be set to a shell that may not be fit for
329          *     interactive use (which is what most programs look in COMSPEC
330          *     for).
331          */
332         char *usershell = getenv("PERL5SHELL");  
333
334         ProbeEnv = TRUE;
335         strcpy(szShellPath, usershell ? usershell : defaultshell);
336     }
337     return szShellPath;
338 }
339
340 int
341 do_aspawn(void* really, void** mark, void** arglast)
342 {
343     char **argv;
344     char *strPtr;
345     char *cmd;
346     int status;
347     unsigned int length;
348     int index = 0;
349     SV *sv = (SV*)really;
350     SV** pSv = (SV**)mark;
351
352     New(1310, argv, (arglast - mark) + 4, char*);
353
354     if(sv != Nullsv) {
355         cmd = SvPV(sv, length);
356     }
357     else {
358         argv[index++] = cmd = GetShell();
359         argv[index++] = "/x";   /* always enable command extensions */
360         argv[index++] = "/c";
361     }
362
363     while(++pSv <= (SV**)arglast) {
364         sv = *pSv;
365         strPtr = SvPV(sv, length);
366         if(strPtr != NULL && *strPtr != '\0')
367             argv[index++] = strPtr;
368     }
369     argv[index++] = 0;
370    
371     status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv);
372
373     Safefree(argv);
374
375     if (status < 0) {
376         if (dowarn)
377             warn("Can't spawn \"%s\": %s", cmd, strerror(errno));
378         status = 255 << 8;
379     }
380     return (status);
381 }
382
383 int
384 do_spawn(char *cmd)
385 {
386     char **a;
387     char *s;
388     char **argv;
389     int status = -1;
390     BOOL needToTry = TRUE;
391     char *shell, *cmd2;
392
393     /* save an extra exec if possible */
394     shell = GetShell();
395
396     /* see if there are shell metacharacters in it */
397     if(!HasRedirection(cmd)) {
398         New(1301,argv, strlen(cmd) / 2 + 2, char*);
399         New(1302,cmd2, strlen(cmd) + 1, char);
400         strcpy(cmd2, cmd);
401         a = argv;
402         for (s = cmd2; *s;) {
403             while (*s && isspace(*s))
404                 s++;
405             if (*s)
406                 *(a++) = s;
407             while(*s && !isspace(*s))
408                 s++;
409             if(*s)
410                 *s++ = '\0';
411         }
412         *a = Nullch;
413         if(argv[0]) {
414             status = win32_spawnvp(P_WAIT, argv[0], (const char* const*)argv);
415             if(status != -1 || errno == 0)
416                 needToTry = FALSE;
417         }
418         Safefree(argv);
419         Safefree(cmd2);
420     }
421     if(needToTry) {
422         char *argv[5];
423         argv[0] = shell; argv[1] = "/x"; argv[2] = "/c";
424         argv[3] = cmd; argv[4] = Nullch;
425         status = win32_spawnvp(P_WAIT, argv[0], (const char* const*)argv);
426     }
427     if (status < 0) {
428         if (dowarn)
429             warn("Can't spawn \"%s\": %s", needToTry ? shell : argv[0],
430                  strerror(errno));
431         status = 255 << 8;
432     }
433     return (status);
434 }
435
436
437 #define PATHLEN 1024
438
439 /* The idea here is to read all the directory names into a string table
440  * (separated by nulls) and when one of the other dir functions is called
441  * return the pointer to the current file name.
442  */
443 DIR *
444 opendir(char *filename)
445 {
446     DIR            *p;
447     long            len;
448     long            idx;
449     char            scannamespc[PATHLEN];
450     char       *scanname = scannamespc;
451     struct stat     sbuf;
452     WIN32_FIND_DATA FindData;
453     HANDLE          fh;
454 /*  char            root[_MAX_PATH];*/
455 /*  char            volname[_MAX_PATH];*/
456 /*  DWORD           serial, maxname, flags;*/
457 /*  BOOL            downcase;*/
458 /*  char           *dummy;*/
459
460     /* check to see if filename is a directory */
461     if(stat(filename, &sbuf) < 0 || sbuf.st_mode & S_IFDIR == 0) {
462         return NULL;
463     }
464
465     /* get the file system characteristics */
466 /*  if(GetFullPathName(filename, MAX_PATH, root, &dummy)) {
467  *      if(dummy = strchr(root, '\\'))
468  *          *++dummy = '\0';
469  *      if(GetVolumeInformation(root, volname, MAX_PATH, &serial,
470  *                              &maxname, &flags, 0, 0)) {
471  *          downcase = !(flags & FS_CASE_IS_PRESERVED);
472  *      }
473  *  }
474  *  else {
475  *      downcase = TRUE;
476  *  }
477  */
478     /* Get us a DIR structure */
479     Newz(1303, p, 1, DIR);
480     if(p == NULL)
481         return NULL;
482
483     /* Create the search pattern */
484     strcpy(scanname, filename);
485
486     if(index("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
487         strcat(scanname, "/*");
488     else
489         strcat(scanname, "*");
490
491     /* do the FindFirstFile call */
492     fh = FindFirstFile(scanname, &FindData);
493     if(fh == INVALID_HANDLE_VALUE) {
494         return NULL;
495     }
496
497     /* now allocate the first part of the string table for
498      * the filenames that we find.
499      */
500     idx = strlen(FindData.cFileName)+1;
501     New(1304, p->start, idx, char);
502     if(p->start == NULL) {
503         CROAK("opendir: malloc failed!\n");
504     }
505     strcpy(p->start, FindData.cFileName);
506 /*  if(downcase)
507  *      strlwr(p->start);
508  */
509     p->nfiles++;
510
511     /* loop finding all the files that match the wildcard
512      * (which should be all of them in this directory!).
513      * the variable idx should point one past the null terminator
514      * of the previous string found.
515      */
516     while (FindNextFile(fh, &FindData)) {
517         len = strlen(FindData.cFileName);
518         /* bump the string table size by enough for the
519          * new name and it's null terminator
520          */
521         Renew(p->start, idx+len+1, char);
522         if(p->start == NULL) {
523             CROAK("opendir: malloc failed!\n");
524         }
525         strcpy(&p->start[idx], FindData.cFileName);
526 /*      if (downcase) 
527  *          strlwr(&p->start[idx]);
528  */
529                 p->nfiles++;
530                 idx += len+1;
531         }
532         FindClose(fh);
533         p->size = idx;
534         p->curr = p->start;
535         return p;
536 }
537
538
539 /* Readdir just returns the current string pointer and bumps the
540  * string pointer to the nDllExport entry.
541  */
542 struct direct *
543 readdir(DIR *dirp)
544 {
545     int         len;
546     static int  dummy = 0;
547
548     if (dirp->curr) {
549         /* first set up the structure to return */
550         len = strlen(dirp->curr);
551         strcpy(dirp->dirstr.d_name, dirp->curr);
552         dirp->dirstr.d_namlen = len;
553
554         /* Fake an inode */
555         dirp->dirstr.d_ino = dummy++;
556
557         /* Now set up for the nDllExport call to readdir */
558         dirp->curr += len + 1;
559         if (dirp->curr >= (dirp->start + dirp->size)) {
560             dirp->curr = NULL;
561         }
562
563         return &(dirp->dirstr);
564     } 
565     else
566         return NULL;
567 }
568
569 /* Telldir returns the current string pointer position */
570 long
571 telldir(DIR *dirp)
572 {
573     return (long) dirp->curr;
574 }
575
576
577 /* Seekdir moves the string pointer to a previously saved position
578  *(Saved by telldir).
579  */
580 void
581 seekdir(DIR *dirp, long loc)
582 {
583     dirp->curr = (char *)loc;
584 }
585
586 /* Rewinddir resets the string pointer to the start */
587 void
588 rewinddir(DIR *dirp)
589 {
590     dirp->curr = dirp->start;
591 }
592
593 /* free the memory allocated by opendir */
594 int
595 closedir(DIR *dirp)
596 {
597     Safefree(dirp->start);
598     Safefree(dirp);
599     return 1;
600 }
601
602
603 /*
604  * various stubs
605  */
606
607
608 /* Ownership
609  *
610  * Just pretend that everyone is a superuser. NT will let us know if
611  * we don\'t really have permission to do something.
612  */
613
614 #define ROOT_UID    ((uid_t)0)
615 #define ROOT_GID    ((gid_t)0)
616
617 uid_t
618 getuid(void)
619 {
620     return ROOT_UID;
621 }
622
623 uid_t
624 geteuid(void)
625 {
626     return ROOT_UID;
627 }
628
629 gid_t
630 getgid(void)
631 {
632     return ROOT_GID;
633 }
634
635 gid_t
636 getegid(void)
637 {
638     return ROOT_GID;
639 }
640
641 int
642 setuid(uid_t uid)
643
644     return (uid == ROOT_UID ? 0 : -1);
645 }
646
647 int
648 setgid(gid_t gid)
649 {
650     return (gid == ROOT_GID ? 0 : -1);
651 }
652
653 /*
654  * pretended kill
655  */
656 int
657 kill(int pid, int sig)
658 {
659     HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
660
661     if (hProcess == NULL) {
662         CROAK("kill process failed!\n");
663     }
664     else {
665         if (!TerminateProcess(hProcess, sig))
666             CROAK("kill process failed!\n");
667         CloseHandle(hProcess);
668     }
669     return 0;
670 }
671       
672 /*
673  * File system stuff
674  */
675
676 #if 0
677 int
678 ioctl(int i, unsigned int u, char *data)
679 {
680     CROAK("ioctl not implemented!\n");
681     return -1;
682 }
683 #endif
684
685 unsigned int
686 sleep(unsigned int t)
687 {
688     Sleep(t*1000);
689     return 0;
690 }
691
692
693 #undef rename
694
695 int
696 myrename(char *OldFileName, char *newname)
697 {
698     if(_access(newname, 0) != -1) {     /* file exists */
699         _unlink(newname);
700     }
701     return rename(OldFileName, newname);
702 }
703
704
705 DllExport int
706 win32_stat(const char *path, struct stat *buffer)
707 {
708     char                t[MAX_PATH]; 
709     const char  *p = path;
710     int         l = strlen(path);
711
712     if (l > 1) {
713         switch(path[l - 1]) {
714         case '\\':
715         case '/':
716             if (path[l - 2] != ':') {
717                 strncpy(t, path, l - 1);
718                 t[l - 1] = 0;
719                 p = t;
720             };
721         }
722     }
723     return stat(p, buffer);
724 }
725
726 #undef times
727 int
728 mytimes(struct tms *timebuf)
729 {
730     clock_t     t = clock();
731     timebuf->tms_utime = t;
732     timebuf->tms_stime = 0;
733     timebuf->tms_cutime = 0;
734     timebuf->tms_cstime = 0;
735
736     return 0;
737 }
738
739 #undef alarm
740 unsigned int
741 myalarm(unsigned int sec)
742 {
743     /* we warn the usuage of alarm function */
744     if (sec != 0)
745         WARN("dummy function alarm called, program might not function as expected\n");
746     return 0;
747 }
748
749 /*
750  *  redirected io subsystem for all XS modules
751  *
752  */
753
754 DllExport int *
755 win32_errno(void)
756 {
757     return (pIOSubSystem->pfnerrno());
758 }
759
760 DllExport char ***
761 win32_environ(void)
762 {
763     return (pIOSubSystem->pfnenviron());
764 }
765
766 /* the rest are the remapped stdio routines */
767 DllExport FILE *
768 win32_stderr(void)
769 {
770     return (pIOSubSystem->pfnstderr());
771 }
772
773 DllExport FILE *
774 win32_stdin(void)
775 {
776     return (pIOSubSystem->pfnstdin());
777 }
778
779 DllExport FILE *
780 win32_stdout()
781 {
782     return (pIOSubSystem->pfnstdout());
783 }
784
785 DllExport int
786 win32_ferror(FILE *fp)
787 {
788     return (pIOSubSystem->pfnferror(fp));
789 }
790
791
792 DllExport int
793 win32_feof(FILE *fp)
794 {
795     return (pIOSubSystem->pfnfeof(fp));
796 }
797
798 /*
799  * Since the errors returned by the socket error function 
800  * WSAGetLastError() are not known by the library routine strerror
801  * we have to roll our own.
802  */
803
804 __declspec(thread) char strerror_buffer[512];
805
806 DllExport char *
807 win32_strerror(int e) 
808 {
809 #ifndef __BORLANDC__            /* Borland intolerance */
810     extern int sys_nerr;
811 #endif
812     DWORD source = 0;
813
814     if(e < 0 || e > sys_nerr) {
815         if(e < 0)
816             e = GetLastError();
817
818         if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
819                          strerror_buffer, sizeof(strerror_buffer), NULL) == 0) 
820             strcpy(strerror_buffer, "Unknown Error");
821
822         return strerror_buffer;
823     }
824     return pIOSubSystem->pfnstrerror(e);
825 }
826
827 DllExport int
828 win32_fprintf(FILE *fp, const char *format, ...)
829 {
830     va_list marker;
831     va_start(marker, format);     /* Initialize variable arguments. */
832
833     return (pIOSubSystem->pfnvfprintf(fp, format, marker));
834 }
835
836 DllExport int
837 win32_printf(const char *format, ...)
838 {
839     va_list marker;
840     va_start(marker, format);     /* Initialize variable arguments. */
841
842     return (pIOSubSystem->pfnvprintf(format, marker));
843 }
844
845 DllExport int
846 win32_vfprintf(FILE *fp, const char *format, va_list args)
847 {
848     return (pIOSubSystem->pfnvfprintf(fp, format, args));
849 }
850
851 DllExport int
852 win32_vprintf(const char *format, va_list args)
853 {
854     return (pIOSubSystem->pfnvprintf(format, args));
855 }
856
857 DllExport size_t
858 win32_fread(void *buf, size_t size, size_t count, FILE *fp)
859 {
860     return pIOSubSystem->pfnfread(buf, size, count, fp);
861 }
862
863 DllExport size_t
864 win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
865 {
866     return pIOSubSystem->pfnfwrite(buf, size, count, fp);
867 }
868
869 DllExport FILE *
870 win32_fopen(const char *filename, const char *mode)
871 {
872     if (stricmp(filename, "/dev/null")==0)
873         return pIOSubSystem->pfnfopen("NUL", mode);
874     return pIOSubSystem->pfnfopen(filename, mode);
875 }
876
877 DllExport FILE *
878 win32_fdopen( int handle, const char *mode)
879 {
880     return pIOSubSystem->pfnfdopen(handle, mode);
881 }
882
883 DllExport FILE *
884 win32_freopen( const char *path, const char *mode, FILE *stream)
885 {
886     if (stricmp(path, "/dev/null")==0)
887         return pIOSubSystem->pfnfreopen("NUL", mode, stream);
888     return pIOSubSystem->pfnfreopen(path, mode, stream);
889 }
890
891 DllExport int
892 win32_fclose(FILE *pf)
893 {
894     return pIOSubSystem->pfnfclose(pf);
895 }
896
897 DllExport int
898 win32_fputs(const char *s,FILE *pf)
899 {
900     return pIOSubSystem->pfnfputs(s, pf);
901 }
902
903 DllExport int
904 win32_fputc(int c,FILE *pf)
905 {
906     return pIOSubSystem->pfnfputc(c,pf);
907 }
908
909 DllExport int
910 win32_ungetc(int c,FILE *pf)
911 {
912     return pIOSubSystem->pfnungetc(c,pf);
913 }
914
915 DllExport int
916 win32_getc(FILE *pf)
917 {
918     return pIOSubSystem->pfngetc(pf);
919 }
920
921 DllExport int
922 win32_fileno(FILE *pf)
923 {
924     return pIOSubSystem->pfnfileno(pf);
925 }
926
927 DllExport void
928 win32_clearerr(FILE *pf)
929 {
930     pIOSubSystem->pfnclearerr(pf);
931     return;
932 }
933
934 DllExport int
935 win32_fflush(FILE *pf)
936 {
937     return pIOSubSystem->pfnfflush(pf);
938 }
939
940 DllExport long
941 win32_ftell(FILE *pf)
942 {
943     return pIOSubSystem->pfnftell(pf);
944 }
945
946 DllExport int
947 win32_fseek(FILE *pf,long offset,int origin)
948 {
949     return pIOSubSystem->pfnfseek(pf, offset, origin);
950 }
951
952 DllExport int
953 win32_fgetpos(FILE *pf,fpos_t *p)
954 {
955     return pIOSubSystem->pfnfgetpos(pf, p);
956 }
957
958 DllExport int
959 win32_fsetpos(FILE *pf,const fpos_t *p)
960 {
961     return pIOSubSystem->pfnfsetpos(pf, p);
962 }
963
964 DllExport void
965 win32_rewind(FILE *pf)
966 {
967     pIOSubSystem->pfnrewind(pf);
968     return;
969 }
970
971 DllExport FILE*
972 win32_tmpfile(void)
973 {
974     return pIOSubSystem->pfntmpfile();
975 }
976
977 DllExport void
978 win32_abort(void)
979 {
980     pIOSubSystem->pfnabort();
981     return;
982 }
983
984 DllExport int
985 win32_fstat(int fd,struct stat *bufptr)
986 {
987     return pIOSubSystem->pfnfstat(fd,bufptr);
988 }
989
990 DllExport int
991 win32_pipe(int *pfd, unsigned int size, int mode)
992 {
993     return pIOSubSystem->pfnpipe(pfd, size, mode);
994 }
995
996 DllExport FILE*
997 win32_popen(const char *command, const char *mode)
998 {
999     return pIOSubSystem->pfnpopen(command, mode);
1000 }
1001
1002 DllExport int
1003 win32_pclose(FILE *pf)
1004 {
1005     return pIOSubSystem->pfnpclose(pf);
1006 }
1007
1008 DllExport int
1009 win32_setmode(int fd, int mode)
1010 {
1011     return pIOSubSystem->pfnsetmode(fd, mode);
1012 }
1013
1014 DllExport long
1015 win32_lseek(int fd, long offset, int origin)
1016 {
1017     return pIOSubSystem->pfnlseek(fd, offset, origin);
1018 }
1019
1020 DllExport long
1021 win32_tell(int fd)
1022 {
1023     return pIOSubSystem->pfntell(fd);
1024 }
1025
1026 DllExport int
1027 win32_open(const char *path, int flag, ...)
1028 {
1029     va_list ap;
1030     int pmode;
1031
1032     va_start(ap, flag);
1033     pmode = va_arg(ap, int);
1034     va_end(ap);
1035
1036     if (stricmp(path, "/dev/null")==0)
1037         return pIOSubSystem->pfnopen("NUL", flag, pmode);
1038     return pIOSubSystem->pfnopen(path,flag,pmode);
1039 }
1040
1041 DllExport int
1042 win32_close(int fd)
1043 {
1044     return pIOSubSystem->pfnclose(fd);
1045 }
1046
1047 DllExport int
1048 win32_eof(int fd)
1049 {
1050     return pIOSubSystem->pfneof(fd);
1051 }
1052
1053 DllExport int
1054 win32_dup(int fd)
1055 {
1056     return pIOSubSystem->pfndup(fd);
1057 }
1058
1059 DllExport int
1060 win32_dup2(int fd1,int fd2)
1061 {
1062     return pIOSubSystem->pfndup2(fd1,fd2);
1063 }
1064
1065 DllExport int
1066 win32_read(int fd, void *buf, unsigned int cnt)
1067 {
1068     return pIOSubSystem->pfnread(fd, buf, cnt);
1069 }
1070
1071 DllExport int
1072 win32_write(int fd, const void *buf, unsigned int cnt)
1073 {
1074     return pIOSubSystem->pfnwrite(fd, buf, cnt);
1075 }
1076
1077 DllExport int
1078 win32_mkdir(const char *dir, int mode)
1079 {
1080     return pIOSubSystem->pfnmkdir(dir); /* just ignore mode */
1081 }
1082
1083 DllExport int
1084 win32_rmdir(const char *dir)
1085 {
1086     return pIOSubSystem->pfnrmdir(dir);
1087 }
1088
1089 DllExport int
1090 win32_chdir(const char *dir)
1091 {
1092     return pIOSubSystem->pfnchdir(dir);
1093 }
1094
1095 DllExport int
1096 win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
1097 {
1098     return pIOSubSystem->pfnspawnvp(mode, cmdname, argv);
1099 }
1100
1101 int
1102 stolen_open_osfhandle(long handle, int flags)
1103 {
1104     return pIOSubSystem->pfn_open_osfhandle(handle, flags);
1105 }
1106
1107 long
1108 stolen_get_osfhandle(int fd)
1109 {
1110     return pIOSubSystem->pfn_get_osfhandle(fd);
1111 }
1112
1113 /*
1114  * Extras.
1115  */
1116
1117 DllExport int
1118 win32_flock(int fd, int oper)
1119 {
1120     if (!IsWinNT()) {
1121         croak("flock() unimplemented on this platform");
1122         return -1;
1123     }
1124     return pIOSubSystem->pfnflock(fd, oper);
1125 }
1126