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