This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
exec() fixed on win32
[perl5.git] / win32 / win32.c
CommitLineData
68dc0745
PP
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
PP
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
PP
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
PP
42
43BOOL ProbeEnv = FALSE;
8b10511d 44DWORD Win32System = (DWORD)-1;
0a753a76
PP
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
PP
51int
52IsWin95(void) {
8b10511d 53 return (IdOS() == VER_PLATFORM_WIN32_WINDOWS);
3fe9a6f1
PP
54}
55
56int
57IsWinNT(void) {
8b10511d 58 return (IdOS() == VER_PLATFORM_WIN32_NT);
3fe9a6f1 59}
0a753a76 60
68dc0745
PP
61void *
62SetIOSubSystem(void *p)
0a753a76 63{
137443ea 64 PWIN32_IOSUBSYSTEM old = pIOSubSystem;
68dc0745
PP
65 if (p) {
66 PWIN32_IOSUBSYSTEM pio = (PWIN32_IOSUBSYSTEM)p;
68dc0745
PP
67 if (pio->signature_begin == 12345678L
68 && pio->signature_end == 87654321L) {
68dc0745 69 pIOSubSystem = pio;
68dc0745
PP
70 }
71 }
72 else {
137443ea 73 pIOSubSystem = &win32stdio;
68dc0745 74 }
137443ea 75 return old;
68dc0745
PP
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
PP
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
PP
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
PP
137 ++ptr;
138 }
139 return FALSE;
0a753a76
PP
140}
141
68dc0745
PP
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
PP
147{
148#ifdef FIXCMD
68dc0745
PP
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
PP
160#else
161#define fixcmd(x)
162#endif
68dc0745
PP
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
PP
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
PP
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
PP
202 /* first check for errors in the arguments */
203 if ( (cmd == NULL) || (mode == NULL)
204 || ((*mode != 'w') && (*mode != _T('r'))) )
205 goto error1;
0a753a76
PP
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
PP
213
214
68dc0745
PP
215 fixcmd(cmd);
216 if (&win32stdio != pIOSubSystem)
217 return win32_popen(cmd, mode);
0a753a76
PP
218
219#ifdef EFG
220 if ( _pipe( phdls, 1024, tm ) == -1 )
221#else
222 if ( win32_pipe( phdls, 1024, tm ) == -1 )
223#endif
68dc0745
PP
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
PP
259
260#ifdef AAA
68dc0745
PP
261 /* Close the Out pipe, child won't need it */
262 hPDuped = (HANDLE) stolen_get_osfhandle(fhNeeded);
0a753a76 263
68dc0745
PP
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
PP
269#endif
270
68dc0745
PP
271 if (!SetStdHandle(dwDup, (HANDLE) stolen_get_osfhandle(fhInherited)))
272 goto error2;
0a753a76 273
68dc0745
PP
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
PP
284 SetStdHandle(STD_INPUT_HANDLE, hSaveStdin);
285 SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout);
286 SetStdHandle(STD_ERROR_HANDLE, hSaveStderr);
287
68dc0745
PP
288 /* we don't need it any more, that's for the child */
289 win32_close(fhInherited);
0a753a76 290
68dc0745
PP
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
PP
304
305error2:
68dc0745
PP
306 win32_close(fhNeeded);
307 win32_close(fhInherited);
0a753a76
PP
308
309error1:
68dc0745 310 return (NULL);
0a753a76
PP
311
312#endif
313}
314
68dc0745
PP
315long
316my_pclose(PerlIO *fp)
0a753a76
PP
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
PP
333}
334
68dc0745
PP
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
PP
351 }
352 return szShellPath;
0a753a76
PP
353}
354
68dc0745
PP
355int
356do_aspawn(void* really, void** mark, void** arglast)
0a753a76 357{
68dc0745
PP
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
PP
365 SV** pSv = (SV**)mark;
366
fc36a67e 367 New(1310, argv, (arglast - mark) + 4, char*);
68dc0745
PP
368
369 if(sv != Nullsv) {
370 cmd = SvPV(sv, length);
371 }
372 else {
3fe9a6f1
PP
373 argv[index++] = cmd = GetShell();
374 argv[index++] = "/x"; /* always enable command extensions */
68dc0745
PP
375 argv[index++] = "/c";
376 }
377
5aabfad6
PP
378 while(++pSv <= (SV**)arglast) {
379 sv = *pSv;
68dc0745
PP
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
PP
387
388 Safefree(argv);
389
5aabfad6
PP
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
PP
396}
397
398int
6890e559 399do_spawn2(char *cmd, int exectype)
68dc0745
PP
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
PP
413 New(1301,argv, strlen(cmd) / 2 + 2, char*);
414 New(1302,cmd2, strlen(cmd) + 1, char);
68dc0745
PP
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
PP
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
PP
442 if(status != -1 || errno == 0)
443 needToTry = FALSE;
0a753a76 444 }
0a753a76 445 Safefree(argv);
68dc0745
PP
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
PP
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
PP
471 strerror(errno));
472 status = 255 << 8;
473 }
474 return (status);
0a753a76
PP
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
PP
490
491#define PATHLEN 1024
492
68dc0745
PP
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
PP
499{
500 DIR *p;
68dc0745
PP
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
PP
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
PP
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
PP
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
PP
579 strcpy(&p->start[idx], FindData.cFileName);
580/* if (downcase)
581 * strlwr(&p->start[idx]);
582 */
0a753a76
PP
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
PP
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
PP
599 int len;
600 static int dummy = 0;
0a753a76 601
68dc0745
PP
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
PP
608 /* Fake an inode */
609 dirp->dirstr.d_ino = dummy++;
0a753a76 610
68dc0745
PP
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
PP
617 return &(dirp->dirstr);
618 }
619 else
620 return NULL;
0a753a76
PP
621}
622
68dc0745
PP
623/* Telldir returns the current string pointer position */
624long
625telldir(DIR *dirp)
0a753a76
PP
626{
627 return (long) dirp->curr;
628}
629
630
68dc0745
PP
631/* Seekdir moves the string pointer to a previously saved position
632 *(Saved by telldir).
633 */
634void
635seekdir(DIR *dirp, long loc)
0a753a76
PP
636{
637 dirp->curr = (char *)loc;
638}
639
68dc0745
PP
640/* Rewinddir resets the string pointer to the start */
641void
642rewinddir(DIR *dirp)
0a753a76
PP
643{
644 dirp->curr = dirp->start;
645}
646
68dc0745
PP
647/* free the memory allocated by opendir */
648int
649closedir(DIR *dirp)
0a753a76
PP
650{
651 Safefree(dirp->start);
652 Safefree(dirp);
68dc0745 653 return 1;
0a753a76
PP
654}
655
656
68dc0745
PP
657/*
658 * various stubs
659 */
0a753a76
PP
660
661
68dc0745
PP
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
PP
667
668#define ROOT_UID ((uid_t)0)
669#define ROOT_GID ((gid_t)0)
670
68dc0745
PP
671uid_t
672getuid(void)
0a753a76 673{
68dc0745 674 return ROOT_UID;
0a753a76
PP
675}
676
68dc0745
PP
677uid_t
678geteuid(void)
0a753a76 679{
68dc0745 680 return ROOT_UID;
0a753a76
PP
681}
682
68dc0745
PP
683gid_t
684getgid(void)
0a753a76 685{
68dc0745 686 return ROOT_GID;
0a753a76
PP
687}
688
68dc0745
PP
689gid_t
690getegid(void)
0a753a76 691{
68dc0745 692 return ROOT_GID;
0a753a76
PP
693}
694
68dc0745
PP
695int
696setuid(uid_t uid)
0a753a76 697{
68dc0745 698 return (uid == ROOT_UID ? 0 : -1);
0a753a76
PP
699}
700
68dc0745
PP
701int
702setgid(gid_t gid)
0a753a76 703{
68dc0745 704 return (gid == ROOT_GID ? 0 : -1);
0a753a76
PP
705}
706
68dc0745
PP
707/*
708 * pretended kill
709 */
710int
711kill(int pid, int sig)
0a753a76 712{
68dc0745 713 HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
0a753a76
PP
714
715 if (hProcess == NULL) {
68dc0745
PP
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
PP
724}
725
68dc0745
PP
726/*
727 * File system stuff
728 */
0a753a76 729
3e3baf6d 730#if 0
68dc0745
PP
731int
732ioctl(int i, unsigned int u, char *data)
0a753a76 733{
68dc0745
PP
734 CROAK("ioctl not implemented!\n");
735 return -1;
0a753a76 736}
3e3baf6d 737#endif
0a753a76 738
68dc0745
PP
739unsigned int
740sleep(unsigned int t)
0a753a76 741{
68dc0745
PP
742 Sleep(t*1000);
743 return 0;
0a753a76
PP
744}
745
746
747#undef rename
748
68dc0745
PP
749int
750myrename(char *OldFileName, char *newname)
0a753a76 751{
68dc0745
PP
752 if(_access(newname, 0) != -1) { /* file exists */
753 _unlink(newname);
754 }
755 return rename(OldFileName, newname);
0a753a76
PP
756}
757
758
68dc0745
PP
759DllExport int
760win32_stat(const char *path, struct stat *buffer)
0a753a76 761{
68dc0745
PP
762 char t[MAX_PATH];
763 const char *p = path;
764 int l = strlen(path);
0a753a76 765
68dc0745
PP
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
PP
778}
779
780#undef times
68dc0745
PP
781int
782mytimes(struct tms *timebuf)
0a753a76 783{
68dc0745
PP
784 clock_t t = clock();
785 timebuf->tms_utime = t;
786 timebuf->tms_stime = 0;
787 timebuf->tms_cutime = 0;
788 timebuf->tms_cstime = 0;
0a753a76 789
68dc0745 790 return 0;
0a753a76
PP
791}
792
793#undef alarm
68dc0745
PP
794unsigned int
795myalarm(unsigned int sec)
0a753a76 796{
68dc0745
PP
797 /* we warn the usuage of alarm function */
798 if (sec != 0)
799 WARN("dummy function alarm called, program might not function as expected\n");
800 return 0;
0a753a76
PP
801}
802
68dc0745
PP
803/*
804 * redirected io subsystem for all XS modules
805 *
806 */
0a753a76 807
68dc0745
PP
808DllExport int *
809win32_errno(void)
0a753a76 810{
68dc0745 811 return (pIOSubSystem->pfnerrno());
0a753a76
PP
812}
813
dcb2879a
GS
814DllExport char ***
815win32_environ(void)
816{
817 return (pIOSubSystem->pfnenviron());
818}
819
68dc0745
PP
820/* the rest are the remapped stdio routines */
821DllExport FILE *
822win32_stderr(void)
0a753a76 823{
68dc0745 824 return (pIOSubSystem->pfnstderr());
0a753a76
PP
825}
826
68dc0745
PP
827DllExport FILE *
828win32_stdin(void)
0a753a76 829{
68dc0745 830 return (pIOSubSystem->pfnstdin());
0a753a76
PP
831}
832
68dc0745
PP
833DllExport FILE *
834win32_stdout()
0a753a76 835{
68dc0745 836 return (pIOSubSystem->pfnstdout());
0a753a76
PP
837}
838
68dc0745
PP
839DllExport int
840win32_ferror(FILE *fp)
0a753a76 841{
68dc0745 842 return (pIOSubSystem->pfnferror(fp));
0a753a76
PP
843}
844
845
68dc0745
PP
846DllExport int
847win32_feof(FILE *fp)
0a753a76 848{
68dc0745 849 return (pIOSubSystem->pfnfeof(fp));
0a753a76
PP
850}
851
68dc0745
PP
852/*
853 * Since the errors returned by the socket error function
854 * WSAGetLastError() are not known by the library routine strerror
855 * we have to roll our own.
856 */
0a753a76
PP
857
858__declspec(thread) char strerror_buffer[512];
859
68dc0745
PP
860DllExport char *
861win32_strerror(int e)
0a753a76 862{
3e3baf6d 863#ifndef __BORLANDC__ /* Borland intolerance */
68dc0745 864 extern int sys_nerr;
3e3baf6d 865#endif
68dc0745 866 DWORD source = 0;
0a753a76 867
68dc0745
PP
868 if(e < 0 || e > sys_nerr) {
869 if(e < 0)
870 e = GetLastError();
0a753a76 871
68dc0745
PP
872 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
873 strerror_buffer, sizeof(strerror_buffer), NULL) == 0)
874 strcpy(strerror_buffer, "Unknown Error");
0a753a76 875
68dc0745
PP
876 return strerror_buffer;
877 }
878 return pIOSubSystem->pfnstrerror(e);
0a753a76
PP
879}
880
68dc0745
PP
881DllExport int
882win32_fprintf(FILE *fp, const char *format, ...)
0a753a76 883{
68dc0745
PP
884 va_list marker;
885 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 886
68dc0745 887 return (pIOSubSystem->pfnvfprintf(fp, format, marker));
0a753a76
PP
888}
889
68dc0745
PP
890DllExport int
891win32_printf(const char *format, ...)
0a753a76 892{
68dc0745
PP
893 va_list marker;
894 va_start(marker, format); /* Initialize variable arguments. */
0a753a76 895
68dc0745 896 return (pIOSubSystem->pfnvprintf(format, marker));
0a753a76
PP
897}
898
68dc0745
PP
899DllExport int
900win32_vfprintf(FILE *fp, const char *format, va_list args)
0a753a76 901{
68dc0745 902 return (pIOSubSystem->pfnvfprintf(fp, format, args));
0a753a76
PP
903}
904
96e4d5b1
PP
905DllExport int
906win32_vprintf(const char *format, va_list args)
907{
908 return (pIOSubSystem->pfnvprintf(format, args));
909}
910
68dc0745
PP
911DllExport size_t
912win32_fread(void *buf, size_t size, size_t count, FILE *fp)
0a753a76 913{
68dc0745 914 return pIOSubSystem->pfnfread(buf, size, count, fp);
0a753a76
PP
915}
916
68dc0745
PP
917DllExport size_t
918win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
0a753a76 919{
68dc0745 920 return pIOSubSystem->pfnfwrite(buf, size, count, fp);
0a753a76
PP
921}
922
68dc0745
PP
923DllExport FILE *
924win32_fopen(const char *filename, const char *mode)
0a753a76 925{
68dc0745
PP
926 if (stricmp(filename, "/dev/null")==0)
927 return pIOSubSystem->pfnfopen("NUL", mode);
928 return pIOSubSystem->pfnfopen(filename, mode);
0a753a76
PP
929}
930
68dc0745
PP
931DllExport FILE *
932win32_fdopen( int handle, const char *mode)
0a753a76 933{
68dc0745 934 return pIOSubSystem->pfnfdopen(handle, mode);
0a753a76
PP
935}
936
68dc0745
PP
937DllExport FILE *
938win32_freopen( const char *path, const char *mode, FILE *stream)
0a753a76 939{
68dc0745
PP
940 if (stricmp(path, "/dev/null")==0)
941 return pIOSubSystem->pfnfreopen("NUL", mode, stream);
942 return pIOSubSystem->pfnfreopen(path, mode, stream);
0a753a76
PP
943}
944
68dc0745
PP
945DllExport int
946win32_fclose(FILE *pf)
0a753a76 947{
68dc0745 948 return pIOSubSystem->pfnfclose(pf);
0a753a76
PP
949}
950
68dc0745
PP
951DllExport int
952win32_fputs(const char *s,FILE *pf)
0a753a76 953{
68dc0745 954 return pIOSubSystem->pfnfputs(s, pf);
0a753a76
PP
955}
956
68dc0745
PP
957DllExport int
958win32_fputc(int c,FILE *pf)
0a753a76 959{
68dc0745 960 return pIOSubSystem->pfnfputc(c,pf);
0a753a76
PP
961}
962
68dc0745
PP
963DllExport int
964win32_ungetc(int c,FILE *pf)
0a753a76 965{
68dc0745 966 return pIOSubSystem->pfnungetc(c,pf);
0a753a76
PP
967}
968
68dc0745
PP
969DllExport int
970win32_getc(FILE *pf)
0a753a76 971{
68dc0745 972 return pIOSubSystem->pfngetc(pf);
0a753a76
PP
973}
974
68dc0745
PP
975DllExport int
976win32_fileno(FILE *pf)
0a753a76 977{
68dc0745 978 return pIOSubSystem->pfnfileno(pf);
0a753a76
PP
979}
980
68dc0745
PP
981DllExport void
982win32_clearerr(FILE *pf)
0a753a76 983{
68dc0745
PP
984 pIOSubSystem->pfnclearerr(pf);
985 return;
0a753a76
PP
986}
987
68dc0745
PP
988DllExport int
989win32_fflush(FILE *pf)
0a753a76 990{
68dc0745 991 return pIOSubSystem->pfnfflush(pf);
0a753a76
PP
992}
993
68dc0745
PP
994DllExport long
995win32_ftell(FILE *pf)
0a753a76 996{
68dc0745 997 return pIOSubSystem->pfnftell(pf);
0a753a76
PP
998}
999
68dc0745
PP
1000DllExport int
1001win32_fseek(FILE *pf,long offset,int origin)
0a753a76 1002{
68dc0745 1003 return pIOSubSystem->pfnfseek(pf, offset, origin);
0a753a76
PP
1004}
1005
68dc0745
PP
1006DllExport int
1007win32_fgetpos(FILE *pf,fpos_t *p)
0a753a76 1008{
68dc0745 1009 return pIOSubSystem->pfnfgetpos(pf, p);
0a753a76
PP
1010}
1011
68dc0745
PP
1012DllExport int
1013win32_fsetpos(FILE *pf,const fpos_t *p)
0a753a76 1014{
68dc0745 1015 return pIOSubSystem->pfnfsetpos(pf, p);
0a753a76
PP
1016}
1017
68dc0745
PP
1018DllExport void
1019win32_rewind(FILE *pf)
0a753a76 1020{
68dc0745
PP
1021 pIOSubSystem->pfnrewind(pf);
1022 return;
0a753a76
PP
1023}
1024
68dc0745
PP
1025DllExport FILE*
1026win32_tmpfile(void)
0a753a76 1027{
68dc0745 1028 return pIOSubSystem->pfntmpfile();
0a753a76
PP
1029}
1030
68dc0745
PP
1031DllExport void
1032win32_abort(void)
0a753a76 1033{
68dc0745
PP
1034 pIOSubSystem->pfnabort();
1035 return;
0a753a76
PP
1036}
1037
68dc0745
PP
1038DllExport int
1039win32_fstat(int fd,struct stat *bufptr)
0a753a76 1040{
68dc0745 1041 return pIOSubSystem->pfnfstat(fd,bufptr);
0a753a76
PP
1042}
1043
68dc0745
PP
1044DllExport int
1045win32_pipe(int *pfd, unsigned int size, int mode)
0a753a76 1046{
68dc0745 1047 return pIOSubSystem->pfnpipe(pfd, size, mode);
0a753a76
PP
1048}
1049
68dc0745
PP
1050DllExport FILE*
1051win32_popen(const char *command, const char *mode)
0a753a76 1052{
68dc0745 1053 return pIOSubSystem->pfnpopen(command, mode);
0a753a76
PP
1054}
1055
68dc0745
PP
1056DllExport int
1057win32_pclose(FILE *pf)
0a753a76 1058{
68dc0745 1059 return pIOSubSystem->pfnpclose(pf);
0a753a76
PP
1060}
1061
68dc0745
PP
1062DllExport int
1063win32_setmode(int fd, int mode)
0a753a76 1064{
68dc0745 1065 return pIOSubSystem->pfnsetmode(fd, mode);
0a753a76
PP
1066}
1067
96e4d5b1
PP
1068DllExport long
1069win32_lseek(int fd, long offset, int origin)
1070{
1071 return pIOSubSystem->pfnlseek(fd, offset, origin);
1072}
1073
1074DllExport long
1075win32_tell(int fd)
1076{
1077 return pIOSubSystem->pfntell(fd);
1078}
1079
68dc0745
PP
1080DllExport int
1081win32_open(const char *path, int flag, ...)
0a753a76 1082{
68dc0745
PP
1083 va_list ap;
1084 int pmode;
0a753a76
PP
1085
1086 va_start(ap, flag);
1087 pmode = va_arg(ap, int);
1088 va_end(ap);
1089
68dc0745
PP
1090 if (stricmp(path, "/dev/null")==0)
1091 return pIOSubSystem->pfnopen("NUL", flag, pmode);
1092 return pIOSubSystem->pfnopen(path,flag,pmode);
0a753a76
PP
1093}
1094
68dc0745
PP
1095DllExport int
1096win32_close(int fd)
0a753a76 1097{
68dc0745 1098 return pIOSubSystem->pfnclose(fd);
0a753a76
PP
1099}
1100
68dc0745 1101DllExport int
96e4d5b1
PP
1102win32_eof(int fd)
1103{
1104 return pIOSubSystem->pfneof(fd);
1105}
1106
1107DllExport int
68dc0745 1108win32_dup(int fd)
0a753a76 1109{
68dc0745 1110 return pIOSubSystem->pfndup(fd);
0a753a76
PP
1111}
1112
68dc0745
PP
1113DllExport int
1114win32_dup2(int fd1,int fd2)
0a753a76 1115{
68dc0745 1116 return pIOSubSystem->pfndup2(fd1,fd2);
0a753a76
PP
1117}
1118
68dc0745 1119DllExport int
3e3baf6d 1120win32_read(int fd, void *buf, unsigned int cnt)
0a753a76 1121{
68dc0745 1122 return pIOSubSystem->pfnread(fd, buf, cnt);
0a753a76
PP
1123}
1124
68dc0745 1125DllExport int
3e3baf6d 1126win32_write(int fd, const void *buf, unsigned int cnt)
0a753a76 1127{
68dc0745 1128 return pIOSubSystem->pfnwrite(fd, buf, cnt);
0a753a76
PP
1129}
1130
68dc0745 1131DllExport int
5aabfad6
PP
1132win32_mkdir(const char *dir, int mode)
1133{
1134 return pIOSubSystem->pfnmkdir(dir); /* just ignore mode */
1135}
96e4d5b1 1136
5aabfad6
PP
1137DllExport int
1138win32_rmdir(const char *dir)
1139{
1140 return pIOSubSystem->pfnrmdir(dir);
1141}
96e4d5b1 1142
5aabfad6
PP
1143DllExport int
1144win32_chdir(const char *dir)
1145{
1146 return pIOSubSystem->pfnchdir(dir);
1147}
96e4d5b1 1148
5aabfad6 1149DllExport int
3e3baf6d 1150win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
0a753a76 1151{
3e3baf6d 1152 return pIOSubSystem->pfnspawnvp(mode, cmdname, argv);
0a753a76
PP
1153}
1154
6890e559
GS
1155DllExport int
1156win32_execvp(const char *cmdname, const char *const *argv)
1157{
1158 return pIOSubSystem->pfnexecvp(cmdname, argv);
1159}
1160
68dc0745
PP
1161int
1162stolen_open_osfhandle(long handle, int flags)
0a753a76 1163{
68dc0745 1164 return pIOSubSystem->pfn_open_osfhandle(handle, flags);
0a753a76
PP
1165}
1166
68dc0745
PP
1167long
1168stolen_get_osfhandle(int fd)
0a753a76 1169{
68dc0745 1170 return pIOSubSystem->pfn_get_osfhandle(fd);
0a753a76 1171}
7bac28a0 1172
7bac28a0
PP
1173/*
1174 * Extras.
1175 */
1176
7bac28a0
PP
1177DllExport int
1178win32_flock(int fd, int oper)
1179{
7bac28a0
PP
1180 if (!IsWinNT()) {
1181 croak("flock() unimplemented on this platform");
1182 return -1;
1183 }
c90c0ff4 1184 return pIOSubSystem->pfnflock(fd, oper);
7bac28a0
PP
1185}
1186