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