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