Commit | Line | Data |
---|---|---|
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" | |
ad2e33dc | 25 | #include "XSUB.h" |
0a753a76 | 26 | #include <fcntl.h> |
27 | #include <sys/stat.h> | |
28 | #include <assert.h> | |
29 | #include <string.h> | |
30 | #include <stdarg.h> | |
ad2e33dc | 31 | #include <float.h> |
0a753a76 | 32 | |
6890e559 GS |
33 | #define EXECF_EXEC 1 |
34 | #define EXECF_SPAWN 2 | |
35 | #define EXECF_SPAWN_NOWAIT 3 | |
36 | ||
8b10511d GS |
37 | static DWORD IdOS(void); |
38 | ||
0a753a76 | 39 | extern WIN32_IOSUBSYSTEM win32stdio; |
4dd614da | 40 | static PWIN32_IOSUBSYSTEM pIOSubSystem = &win32stdio; |
0a753a76 | 41 | |
42 | BOOL ProbeEnv = FALSE; | |
8b10511d | 43 | DWORD Win32System = (DWORD)-1; |
0a753a76 | 44 | char szShellPath[MAX_PATH+1]; |
45 | char szPerlLibRoot[MAX_PATH+1]; | |
46 | HANDLE PerlDllHandle = INVALID_HANDLE_VALUE; | |
47 | ||
6890e559 GS |
48 | static int do_spawn2(char *cmd, int exectype); |
49 | ||
3fe9a6f1 | 50 | int |
51 | IsWin95(void) { | |
8b10511d | 52 | return (IdOS() == VER_PLATFORM_WIN32_WINDOWS); |
3fe9a6f1 | 53 | } |
54 | ||
55 | int | |
56 | IsWinNT(void) { | |
8b10511d | 57 | return (IdOS() == VER_PLATFORM_WIN32_NT); |
3fe9a6f1 | 58 | } |
0a753a76 | 59 | |
84902520 | 60 | DllExport PWIN32_IOSUBSYSTEM |
68dc0745 | 61 | SetIOSubSystem(void *p) |
0a753a76 | 62 | { |
137443ea | 63 | PWIN32_IOSUBSYSTEM old = pIOSubSystem; |
68dc0745 | 64 | if (p) { |
65 | PWIN32_IOSUBSYSTEM pio = (PWIN32_IOSUBSYSTEM)p; | |
68dc0745 | 66 | if (pio->signature_begin == 12345678L |
67 | && pio->signature_end == 87654321L) { | |
68dc0745 | 68 | pIOSubSystem = pio; |
68dc0745 | 69 | } |
70 | } | |
71 | else { | |
137443ea | 72 | pIOSubSystem = &win32stdio; |
68dc0745 | 73 | } |
137443ea | 74 | return old; |
68dc0745 | 75 | } |
76 | ||
84902520 TB |
77 | DllExport PWIN32_IOSUBSYSTEM |
78 | GetIOSubSystem(void) | |
79 | { | |
80 | return pIOSubSystem; | |
81 | } | |
82 | ||
68dc0745 | 83 | char * |
84 | win32PerlLibPath(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 | } | |
0a753a76 | 99 | |
b4793f7f GS |
100 | char * |
101 | win32SiteLibPath(void) | |
102 | { | |
2bc71ef8 | 103 | static char szPerlSiteLib[MAX_PATH+1]; |
b4793f7f GS |
104 | strcpy(szPerlSiteLib, win32PerlLibPath()); |
105 | strcat(szPerlSiteLib, "\\site"); | |
106 | return (szPerlSiteLib); | |
107 | } | |
108 | ||
68dc0745 | 109 | BOOL |
110 | HasRedirection(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'; | |
0a753a76 | 127 | } |
68dc0745 | 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; | |
0a753a76 | 141 | } |
68dc0745 | 142 | ++ptr; |
143 | } | |
144 | return FALSE; | |
0a753a76 | 145 | } |
146 | ||
68dc0745 | 147 | /* since the current process environment is being updated in util.c |
148 | * the library functions will get the correct environment | |
149 | */ | |
150 | PerlIO * | |
151 | my_popen(char *cmd, char *mode) | |
0a753a76 | 152 | { |
153 | #ifdef FIXCMD | |
68dc0745 | 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 | } | |
0a753a76 | 165 | #else |
166 | #define fixcmd(x) | |
167 | #endif | |
68dc0745 | 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); |
0a753a76 | 174 | } |
175 | ||
68dc0745 | 176 | long |
177 | my_pclose(PerlIO *fp) | |
0a753a76 | 178 | { |
179 | return win32_pclose(fp); | |
180 | } | |
181 | ||
8b10511d | 182 | static DWORD |
68dc0745 | 183 | IdOS(void) |
0a753a76 | 184 | { |
8b10511d | 185 | static OSVERSIONINFO osver; |
0a753a76 | 186 | |
8b10511d GS |
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); | |
0a753a76 | 194 | } |
195 | ||
68dc0745 | 196 | static char * |
197 | GetShell(void) | |
0a753a76 | 198 | { |
68dc0745 | 199 | if (!ProbeEnv) { |
174c211a GS |
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 | ||
8b10511d | 210 | ProbeEnv = TRUE; |
174c211a | 211 | strcpy(szShellPath, usershell ? usershell : defaultshell); |
68dc0745 | 212 | } |
213 | return szShellPath; | |
0a753a76 | 214 | } |
215 | ||
68dc0745 | 216 | int |
d55594ae | 217 | do_aspawn(void* really, void ** mark, void ** arglast) |
0a753a76 | 218 | { |
68dc0745 | 219 | char **argv; |
220 | char *strPtr; | |
221 | char *cmd; | |
222 | int status; | |
223 | unsigned int length; | |
224 | int index = 0; | |
0a753a76 | 225 | SV *sv = (SV*)really; |
68dc0745 | 226 | SV** pSv = (SV**)mark; |
227 | ||
fc36a67e | 228 | New(1310, argv, (arglast - mark) + 4, char*); |
68dc0745 | 229 | |
230 | if(sv != Nullsv) { | |
231 | cmd = SvPV(sv, length); | |
232 | } | |
233 | else { | |
3fe9a6f1 | 234 | argv[index++] = cmd = GetShell(); |
0a9a032c GS |
235 | if (IsWinNT()) |
236 | argv[index++] = "/x"; /* always enable command extensions */ | |
68dc0745 | 237 | argv[index++] = "/c"; |
238 | } | |
239 | ||
5aabfad6 | 240 | while(++pSv <= (SV**)arglast) { |
241 | sv = *pSv; | |
68dc0745 | 242 | strPtr = SvPV(sv, length); |
243 | if(strPtr != NULL && *strPtr != '\0') | |
244 | argv[index++] = strPtr; | |
245 | } | |
246 | argv[index++] = 0; | |
247 | ||
3e3baf6d | 248 | status = win32_spawnvp(P_WAIT, cmd, (const char* const*)argv); |
68dc0745 | 249 | |
250 | Safefree(argv); | |
251 | ||
5aabfad6 | 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); | |
68dc0745 | 258 | } |
259 | ||
260 | int | |
6890e559 | 261 | do_spawn2(char *cmd, int exectype) |
68dc0745 | 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)) { | |
fc36a67e | 275 | New(1301,argv, strlen(cmd) / 2 + 2, char*); |
276 | New(1302,cmd2, strlen(cmd) + 1, char); | |
68dc0745 | 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'; | |
0a753a76 | 288 | } |
68dc0745 | 289 | *a = Nullch; |
290 | if(argv[0]) { | |
6890e559 GS |
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 | } | |
68dc0745 | 304 | if(status != -1 || errno == 0) |
305 | needToTry = FALSE; | |
0a753a76 | 306 | } |
0a753a76 | 307 | Safefree(argv); |
68dc0745 | 308 | Safefree(cmd2); |
309 | } | |
310 | if(needToTry) { | |
3e3baf6d | 311 | char *argv[5]; |
0a9a032c GS |
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; | |
6890e559 GS |
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 | } | |
68dc0745 | 330 | } |
5aabfad6 | 331 | if (status < 0) { |
332 | if (dowarn) | |
6890e559 GS |
333 | warn("Can't %s \"%s\": %s", |
334 | (exectype == EXECF_EXEC ? "exec" : "spawn"), | |
335 | needToTry ? shell : argv[0], | |
5aabfad6 | 336 | strerror(errno)); |
337 | status = 255 << 8; | |
338 | } | |
339 | return (status); | |
0a753a76 | 340 | } |
341 | ||
6890e559 GS |
342 | int |
343 | do_spawn(char *cmd) | |
344 | { | |
345 | return do_spawn2(cmd, EXECF_SPAWN); | |
346 | } | |
347 | ||
348 | bool | |
349 | do_exec(char *cmd) | |
350 | { | |
351 | do_spawn2(cmd, EXECF_EXEC); | |
352 | return FALSE; | |
353 | } | |
354 | ||
0a753a76 | 355 | |
356 | #define PATHLEN 1024 | |
357 | ||
68dc0745 | 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 | */ | |
362 | DIR * | |
363 | opendir(char *filename) | |
0a753a76 | 364 | { |
365 | DIR *p; | |
68dc0745 | 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 */ | |
d55594ae | 380 | if (win32_stat(filename, &sbuf) < 0 || (sbuf.st_mode & S_IFDIR) == 0) { |
68dc0745 | 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 */ | |
fc36a67e | 398 | Newz(1303, p, 1, DIR); |
68dc0745 | 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; | |
fc36a67e | 420 | New(1304, p->start, idx, char); |
68dc0745 | 421 | if(p->start == NULL) { |
65e48ea9 | 422 | croak("opendir: malloc failed!\n"); |
68dc0745 | 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) { | |
65e48ea9 | 442 | croak("opendir: malloc failed!\n"); |
0a753a76 | 443 | } |
68dc0745 | 444 | strcpy(&p->start[idx], FindData.cFileName); |
445 | /* if (downcase) | |
446 | * strlwr(&p->start[idx]); | |
447 | */ | |
0a753a76 | 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 | ||
68dc0745 | 458 | /* Readdir just returns the current string pointer and bumps the |
459 | * string pointer to the nDllExport entry. | |
460 | */ | |
461 | struct direct * | |
462 | readdir(DIR *dirp) | |
0a753a76 | 463 | { |
68dc0745 | 464 | int len; |
465 | static int dummy = 0; | |
0a753a76 | 466 | |
68dc0745 | 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; | |
0a753a76 | 472 | |
68dc0745 | 473 | /* Fake an inode */ |
474 | dirp->dirstr.d_ino = dummy++; | |
0a753a76 | 475 | |
68dc0745 | 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 | } | |
0a753a76 | 481 | |
68dc0745 | 482 | return &(dirp->dirstr); |
483 | } | |
484 | else | |
485 | return NULL; | |
0a753a76 | 486 | } |
487 | ||
68dc0745 | 488 | /* Telldir returns the current string pointer position */ |
489 | long | |
490 | telldir(DIR *dirp) | |
0a753a76 | 491 | { |
492 | return (long) dirp->curr; | |
493 | } | |
494 | ||
495 | ||
68dc0745 | 496 | /* Seekdir moves the string pointer to a previously saved position |
497 | *(Saved by telldir). | |
498 | */ | |
499 | void | |
500 | seekdir(DIR *dirp, long loc) | |
0a753a76 | 501 | { |
502 | dirp->curr = (char *)loc; | |
503 | } | |
504 | ||
68dc0745 | 505 | /* Rewinddir resets the string pointer to the start */ |
506 | void | |
507 | rewinddir(DIR *dirp) | |
0a753a76 | 508 | { |
509 | dirp->curr = dirp->start; | |
510 | } | |
511 | ||
68dc0745 | 512 | /* free the memory allocated by opendir */ |
513 | int | |
514 | closedir(DIR *dirp) | |
0a753a76 | 515 | { |
516 | Safefree(dirp->start); | |
517 | Safefree(dirp); | |
68dc0745 | 518 | return 1; |
0a753a76 | 519 | } |
520 | ||
521 | ||
68dc0745 | 522 | /* |
523 | * various stubs | |
524 | */ | |
0a753a76 | 525 | |
526 | ||
68dc0745 | 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 | */ | |
0a753a76 | 532 | |
533 | #define ROOT_UID ((uid_t)0) | |
534 | #define ROOT_GID ((gid_t)0) | |
535 | ||
68dc0745 | 536 | uid_t |
537 | getuid(void) | |
0a753a76 | 538 | { |
68dc0745 | 539 | return ROOT_UID; |
0a753a76 | 540 | } |
541 | ||
68dc0745 | 542 | uid_t |
543 | geteuid(void) | |
0a753a76 | 544 | { |
68dc0745 | 545 | return ROOT_UID; |
0a753a76 | 546 | } |
547 | ||
68dc0745 | 548 | gid_t |
549 | getgid(void) | |
0a753a76 | 550 | { |
68dc0745 | 551 | return ROOT_GID; |
0a753a76 | 552 | } |
553 | ||
68dc0745 | 554 | gid_t |
555 | getegid(void) | |
0a753a76 | 556 | { |
68dc0745 | 557 | return ROOT_GID; |
0a753a76 | 558 | } |
559 | ||
68dc0745 | 560 | int |
561 | setuid(uid_t uid) | |
0a753a76 | 562 | { |
68dc0745 | 563 | return (uid == ROOT_UID ? 0 : -1); |
0a753a76 | 564 | } |
565 | ||
68dc0745 | 566 | int |
567 | setgid(gid_t gid) | |
0a753a76 | 568 | { |
68dc0745 | 569 | return (gid == ROOT_GID ? 0 : -1); |
0a753a76 | 570 | } |
571 | ||
68dc0745 | 572 | /* |
573 | * pretended kill | |
574 | */ | |
575 | int | |
576 | kill(int pid, int sig) | |
0a753a76 | 577 | { |
68dc0745 | 578 | HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); |
0a753a76 | 579 | |
580 | if (hProcess == NULL) { | |
65e48ea9 | 581 | croak("kill process failed!\n"); |
68dc0745 | 582 | } |
583 | else { | |
584 | if (!TerminateProcess(hProcess, sig)) | |
65e48ea9 | 585 | croak("kill process failed!\n"); |
68dc0745 | 586 | CloseHandle(hProcess); |
587 | } | |
588 | return 0; | |
0a753a76 | 589 | } |
590 | ||
68dc0745 | 591 | /* |
592 | * File system stuff | |
593 | */ | |
0a753a76 | 594 | |
3e3baf6d | 595 | #if 0 |
68dc0745 | 596 | int |
597 | ioctl(int i, unsigned int u, char *data) | |
0a753a76 | 598 | { |
65e48ea9 | 599 | croak("ioctl not implemented!\n"); |
68dc0745 | 600 | return -1; |
0a753a76 | 601 | } |
3e3baf6d | 602 | #endif |
0a753a76 | 603 | |
68dc0745 | 604 | unsigned int |
605 | sleep(unsigned int t) | |
0a753a76 | 606 | { |
68dc0745 | 607 | Sleep(t*1000); |
608 | return 0; | |
0a753a76 | 609 | } |
610 | ||
611 | ||
612 | #undef rename | |
613 | ||
68dc0745 | 614 | int |
615 | myrename(char *OldFileName, char *newname) | |
0a753a76 | 616 | { |
68dc0745 | 617 | if(_access(newname, 0) != -1) { /* file exists */ |
618 | _unlink(newname); | |
619 | } | |
620 | return rename(OldFileName, newname); | |
0a753a76 | 621 | } |
622 | ||
623 | ||
68dc0745 | 624 | DllExport int |
625 | win32_stat(const char *path, struct stat *buffer) | |
0a753a76 | 626 | { |
68dc0745 | 627 | char t[MAX_PATH]; |
628 | const char *p = path; | |
629 | int l = strlen(path); | |
67fbe06e | 630 | int res; |
0a753a76 | 631 | |
68dc0745 | 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 | } | |
67fbe06e GS |
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; | |
0a753a76 | 665 | } |
666 | ||
0551aaa8 GS |
667 | #ifndef USE_WIN32_RTL_ENV |
668 | ||
669 | DllExport char * | |
670 | win32_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 | ||
d55594ae GS |
689 | static long |
690 | FileTimeToClock(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 | ||
0a753a76 | 699 | #undef times |
68dc0745 | 700 | int |
701 | mytimes(struct tms *timebuf) | |
0a753a76 | 702 | { |
d55594ae GS |
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 | } | |
68dc0745 | 721 | return 0; |
0a753a76 | 722 | } |
723 | ||
d55594ae GS |
724 | static UINT timerid = 0; |
725 | ||
726 | ||
727 | static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time) | |
728 | { | |
729 | KillTimer(NULL,timerid); | |
730 | timerid=0; | |
731 | sighandler(14); | |
732 | } | |
733 | ||
0a753a76 | 734 | #undef alarm |
68dc0745 | 735 | unsigned int |
736 | myalarm(unsigned int sec) | |
0a753a76 | 737 | { |
d55594ae GS |
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 | } | |
68dc0745 | 761 | return 0; |
0a753a76 | 762 | } |
763 | ||
68dc0745 | 764 | /* |
765 | * redirected io subsystem for all XS modules | |
766 | * | |
767 | */ | |
0a753a76 | 768 | |
68dc0745 | 769 | DllExport int * |
770 | win32_errno(void) | |
0a753a76 | 771 | { |
68dc0745 | 772 | return (pIOSubSystem->pfnerrno()); |
0a753a76 | 773 | } |
774 | ||
dcb2879a GS |
775 | DllExport char *** |
776 | win32_environ(void) | |
777 | { | |
778 | return (pIOSubSystem->pfnenviron()); | |
779 | } | |
780 | ||
68dc0745 | 781 | /* the rest are the remapped stdio routines */ |
782 | DllExport FILE * | |
783 | win32_stderr(void) | |
0a753a76 | 784 | { |
68dc0745 | 785 | return (pIOSubSystem->pfnstderr()); |
0a753a76 | 786 | } |
787 | ||
68dc0745 | 788 | DllExport FILE * |
789 | win32_stdin(void) | |
0a753a76 | 790 | { |
68dc0745 | 791 | return (pIOSubSystem->pfnstdin()); |
0a753a76 | 792 | } |
793 | ||
68dc0745 | 794 | DllExport FILE * |
795 | win32_stdout() | |
0a753a76 | 796 | { |
68dc0745 | 797 | return (pIOSubSystem->pfnstdout()); |
0a753a76 | 798 | } |
799 | ||
68dc0745 | 800 | DllExport int |
801 | win32_ferror(FILE *fp) | |
0a753a76 | 802 | { |
68dc0745 | 803 | return (pIOSubSystem->pfnferror(fp)); |
0a753a76 | 804 | } |
805 | ||
806 | ||
68dc0745 | 807 | DllExport int |
808 | win32_feof(FILE *fp) | |
0a753a76 | 809 | { |
68dc0745 | 810 | return (pIOSubSystem->pfnfeof(fp)); |
0a753a76 | 811 | } |
812 | ||
68dc0745 | 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 | */ | |
0a753a76 | 818 | |
819 | __declspec(thread) char strerror_buffer[512]; | |
820 | ||
68dc0745 | 821 | DllExport char * |
822 | win32_strerror(int e) | |
0a753a76 | 823 | { |
3e3baf6d | 824 | #ifndef __BORLANDC__ /* Borland intolerance */ |
68dc0745 | 825 | extern int sys_nerr; |
3e3baf6d | 826 | #endif |
68dc0745 | 827 | DWORD source = 0; |
0a753a76 | 828 | |
68dc0745 | 829 | if(e < 0 || e > sys_nerr) { |
830 | if(e < 0) | |
831 | e = GetLastError(); | |
0a753a76 | 832 | |
68dc0745 | 833 | if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0, |
834 | strerror_buffer, sizeof(strerror_buffer), NULL) == 0) | |
835 | strcpy(strerror_buffer, "Unknown Error"); | |
0a753a76 | 836 | |
68dc0745 | 837 | return strerror_buffer; |
838 | } | |
839 | return pIOSubSystem->pfnstrerror(e); | |
0a753a76 | 840 | } |
841 | ||
68dc0745 | 842 | DllExport int |
843 | win32_fprintf(FILE *fp, const char *format, ...) | |
0a753a76 | 844 | { |
68dc0745 | 845 | va_list marker; |
846 | va_start(marker, format); /* Initialize variable arguments. */ | |
0a753a76 | 847 | |
68dc0745 | 848 | return (pIOSubSystem->pfnvfprintf(fp, format, marker)); |
0a753a76 | 849 | } |
850 | ||
68dc0745 | 851 | DllExport int |
852 | win32_printf(const char *format, ...) | |
0a753a76 | 853 | { |
68dc0745 | 854 | va_list marker; |
855 | va_start(marker, format); /* Initialize variable arguments. */ | |
0a753a76 | 856 | |
68dc0745 | 857 | return (pIOSubSystem->pfnvprintf(format, marker)); |
0a753a76 | 858 | } |
859 | ||
68dc0745 | 860 | DllExport int |
861 | win32_vfprintf(FILE *fp, const char *format, va_list args) | |
0a753a76 | 862 | { |
68dc0745 | 863 | return (pIOSubSystem->pfnvfprintf(fp, format, args)); |
0a753a76 | 864 | } |
865 | ||
96e4d5b1 | 866 | DllExport int |
867 | win32_vprintf(const char *format, va_list args) | |
868 | { | |
869 | return (pIOSubSystem->pfnvprintf(format, args)); | |
870 | } | |
871 | ||
68dc0745 | 872 | DllExport size_t |
873 | win32_fread(void *buf, size_t size, size_t count, FILE *fp) | |
0a753a76 | 874 | { |
68dc0745 | 875 | return pIOSubSystem->pfnfread(buf, size, count, fp); |
0a753a76 | 876 | } |
877 | ||
68dc0745 | 878 | DllExport size_t |
879 | win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp) | |
0a753a76 | 880 | { |
68dc0745 | 881 | return pIOSubSystem->pfnfwrite(buf, size, count, fp); |
0a753a76 | 882 | } |
883 | ||
68dc0745 | 884 | DllExport FILE * |
885 | win32_fopen(const char *filename, const char *mode) | |
0a753a76 | 886 | { |
68dc0745 | 887 | if (stricmp(filename, "/dev/null")==0) |
888 | return pIOSubSystem->pfnfopen("NUL", mode); | |
889 | return pIOSubSystem->pfnfopen(filename, mode); | |
0a753a76 | 890 | } |
891 | ||
68dc0745 | 892 | DllExport FILE * |
893 | win32_fdopen( int handle, const char *mode) | |
0a753a76 | 894 | { |
d55594ae | 895 | return pIOSubSystem->pfnfdopen(handle, (char *) mode); |
0a753a76 | 896 | } |
897 | ||
68dc0745 | 898 | DllExport FILE * |
899 | win32_freopen( const char *path, const char *mode, FILE *stream) | |
0a753a76 | 900 | { |
68dc0745 | 901 | if (stricmp(path, "/dev/null")==0) |
902 | return pIOSubSystem->pfnfreopen("NUL", mode, stream); | |
903 | return pIOSubSystem->pfnfreopen(path, mode, stream); | |
0a753a76 | 904 | } |
905 | ||
68dc0745 | 906 | DllExport int |
907 | win32_fclose(FILE *pf) | |
0a753a76 | 908 | { |
68dc0745 | 909 | return pIOSubSystem->pfnfclose(pf); |
0a753a76 | 910 | } |
911 | ||
68dc0745 | 912 | DllExport int |
913 | win32_fputs(const char *s,FILE *pf) | |
0a753a76 | 914 | { |
68dc0745 | 915 | return pIOSubSystem->pfnfputs(s, pf); |
0a753a76 | 916 | } |
917 | ||
68dc0745 | 918 | DllExport int |
919 | win32_fputc(int c,FILE *pf) | |
0a753a76 | 920 | { |
68dc0745 | 921 | return pIOSubSystem->pfnfputc(c,pf); |
0a753a76 | 922 | } |
923 | ||
68dc0745 | 924 | DllExport int |
925 | win32_ungetc(int c,FILE *pf) | |
0a753a76 | 926 | { |
68dc0745 | 927 | return pIOSubSystem->pfnungetc(c,pf); |
0a753a76 | 928 | } |
929 | ||
68dc0745 | 930 | DllExport int |
931 | win32_getc(FILE *pf) | |
0a753a76 | 932 | { |
68dc0745 | 933 | return pIOSubSystem->pfngetc(pf); |
0a753a76 | 934 | } |
935 | ||
68dc0745 | 936 | DllExport int |
937 | win32_fileno(FILE *pf) | |
0a753a76 | 938 | { |
68dc0745 | 939 | return pIOSubSystem->pfnfileno(pf); |
0a753a76 | 940 | } |
941 | ||
68dc0745 | 942 | DllExport void |
943 | win32_clearerr(FILE *pf) | |
0a753a76 | 944 | { |
68dc0745 | 945 | pIOSubSystem->pfnclearerr(pf); |
946 | return; | |
0a753a76 | 947 | } |
948 | ||
68dc0745 | 949 | DllExport int |
950 | win32_fflush(FILE *pf) | |
0a753a76 | 951 | { |
68dc0745 | 952 | return pIOSubSystem->pfnfflush(pf); |
0a753a76 | 953 | } |
954 | ||
68dc0745 | 955 | DllExport long |
956 | win32_ftell(FILE *pf) | |
0a753a76 | 957 | { |
68dc0745 | 958 | return pIOSubSystem->pfnftell(pf); |
0a753a76 | 959 | } |
960 | ||
68dc0745 | 961 | DllExport int |
962 | win32_fseek(FILE *pf,long offset,int origin) | |
0a753a76 | 963 | { |
68dc0745 | 964 | return pIOSubSystem->pfnfseek(pf, offset, origin); |
0a753a76 | 965 | } |
966 | ||
68dc0745 | 967 | DllExport int |
968 | win32_fgetpos(FILE *pf,fpos_t *p) | |
0a753a76 | 969 | { |
68dc0745 | 970 | return pIOSubSystem->pfnfgetpos(pf, p); |
0a753a76 | 971 | } |
972 | ||
68dc0745 | 973 | DllExport int |
974 | win32_fsetpos(FILE *pf,const fpos_t *p) | |
0a753a76 | 975 | { |
68dc0745 | 976 | return pIOSubSystem->pfnfsetpos(pf, p); |
0a753a76 | 977 | } |
978 | ||
68dc0745 | 979 | DllExport void |
980 | win32_rewind(FILE *pf) | |
0a753a76 | 981 | { |
68dc0745 | 982 | pIOSubSystem->pfnrewind(pf); |
983 | return; | |
0a753a76 | 984 | } |
985 | ||
68dc0745 | 986 | DllExport FILE* |
987 | win32_tmpfile(void) | |
0a753a76 | 988 | { |
68dc0745 | 989 | return pIOSubSystem->pfntmpfile(); |
0a753a76 | 990 | } |
991 | ||
68dc0745 | 992 | DllExport void |
993 | win32_abort(void) | |
0a753a76 | 994 | { |
68dc0745 | 995 | pIOSubSystem->pfnabort(); |
996 | return; | |
0a753a76 | 997 | } |
998 | ||
68dc0745 | 999 | DllExport int |
1000 | win32_fstat(int fd,struct stat *bufptr) | |
0a753a76 | 1001 | { |
68dc0745 | 1002 | return pIOSubSystem->pfnfstat(fd,bufptr); |
0a753a76 | 1003 | } |
1004 | ||
68dc0745 | 1005 | DllExport int |
1006 | win32_pipe(int *pfd, unsigned int size, int mode) | |
0a753a76 | 1007 | { |
68dc0745 | 1008 | return pIOSubSystem->pfnpipe(pfd, size, mode); |
0a753a76 | 1009 | } |
1010 | ||
68dc0745 | 1011 | DllExport FILE* |
1012 | win32_popen(const char *command, const char *mode) | |
0a753a76 | 1013 | { |
68dc0745 | 1014 | return pIOSubSystem->pfnpopen(command, mode); |
0a753a76 | 1015 | } |
1016 | ||
68dc0745 | 1017 | DllExport int |
1018 | win32_pclose(FILE *pf) | |
0a753a76 | 1019 | { |
68dc0745 | 1020 | return pIOSubSystem->pfnpclose(pf); |
0a753a76 | 1021 | } |
1022 | ||
68dc0745 | 1023 | DllExport int |
1024 | win32_setmode(int fd, int mode) | |
0a753a76 | 1025 | { |
68dc0745 | 1026 | return pIOSubSystem->pfnsetmode(fd, mode); |
0a753a76 | 1027 | } |
1028 | ||
96e4d5b1 | 1029 | DllExport long |
1030 | win32_lseek(int fd, long offset, int origin) | |
1031 | { | |
1032 | return pIOSubSystem->pfnlseek(fd, offset, origin); | |
1033 | } | |
1034 | ||
1035 | DllExport long | |
1036 | win32_tell(int fd) | |
1037 | { | |
1038 | return pIOSubSystem->pfntell(fd); | |
1039 | } | |
1040 | ||
68dc0745 | 1041 | DllExport int |
1042 | win32_open(const char *path, int flag, ...) | |
0a753a76 | 1043 | { |
68dc0745 | 1044 | va_list ap; |
1045 | int pmode; | |
0a753a76 | 1046 | |
1047 | va_start(ap, flag); | |
1048 | pmode = va_arg(ap, int); | |
1049 | va_end(ap); | |
1050 | ||
68dc0745 | 1051 | if (stricmp(path, "/dev/null")==0) |
1052 | return pIOSubSystem->pfnopen("NUL", flag, pmode); | |
1053 | return pIOSubSystem->pfnopen(path,flag,pmode); | |
0a753a76 | 1054 | } |
1055 | ||
68dc0745 | 1056 | DllExport int |
1057 | win32_close(int fd) | |
0a753a76 | 1058 | { |
68dc0745 | 1059 | return pIOSubSystem->pfnclose(fd); |
0a753a76 | 1060 | } |
1061 | ||
68dc0745 | 1062 | DllExport int |
96e4d5b1 | 1063 | win32_eof(int fd) |
1064 | { | |
1065 | return pIOSubSystem->pfneof(fd); | |
1066 | } | |
1067 | ||
1068 | DllExport int | |
68dc0745 | 1069 | win32_dup(int fd) |
0a753a76 | 1070 | { |
68dc0745 | 1071 | return pIOSubSystem->pfndup(fd); |
0a753a76 | 1072 | } |
1073 | ||
68dc0745 | 1074 | DllExport int |
1075 | win32_dup2(int fd1,int fd2) | |
0a753a76 | 1076 | { |
68dc0745 | 1077 | return pIOSubSystem->pfndup2(fd1,fd2); |
0a753a76 | 1078 | } |
1079 | ||
68dc0745 | 1080 | DllExport int |
3e3baf6d | 1081 | win32_read(int fd, void *buf, unsigned int cnt) |
0a753a76 | 1082 | { |
68dc0745 | 1083 | return pIOSubSystem->pfnread(fd, buf, cnt); |
0a753a76 | 1084 | } |
1085 | ||
68dc0745 | 1086 | DllExport int |
3e3baf6d | 1087 | win32_write(int fd, const void *buf, unsigned int cnt) |
0a753a76 | 1088 | { |
68dc0745 | 1089 | return pIOSubSystem->pfnwrite(fd, buf, cnt); |
0a753a76 | 1090 | } |
1091 | ||
68dc0745 | 1092 | DllExport int |
5aabfad6 | 1093 | win32_mkdir(const char *dir, int mode) |
1094 | { | |
1095 | return pIOSubSystem->pfnmkdir(dir); /* just ignore mode */ | |
1096 | } | |
96e4d5b1 | 1097 | |
5aabfad6 | 1098 | DllExport int |
1099 | win32_rmdir(const char *dir) | |
1100 | { | |
1101 | return pIOSubSystem->pfnrmdir(dir); | |
1102 | } | |
96e4d5b1 | 1103 | |
5aabfad6 | 1104 | DllExport int |
1105 | win32_chdir(const char *dir) | |
1106 | { | |
1107 | return pIOSubSystem->pfnchdir(dir); | |
1108 | } | |
96e4d5b1 | 1109 | |
5aabfad6 | 1110 | DllExport int |
3e3baf6d | 1111 | win32_spawnvp(int mode, const char *cmdname, const char *const *argv) |
0a753a76 | 1112 | { |
d55594ae | 1113 | return pIOSubSystem->pfnspawnvp(mode, cmdname, (char * const *) argv); |
0a753a76 | 1114 | } |
1115 | ||
6890e559 GS |
1116 | DllExport int |
1117 | win32_execvp(const char *cmdname, const char *const *argv) | |
1118 | { | |
d55594ae | 1119 | return pIOSubSystem->pfnexecvp(cmdname, (char *const *)argv); |
6890e559 GS |
1120 | } |
1121 | ||
84902520 TB |
1122 | DllExport void |
1123 | win32_perror(const char *str) | |
1124 | { | |
1125 | pIOSubSystem->pfnperror(str); | |
1126 | } | |
1127 | ||
1128 | DllExport void | |
1129 | win32_setbuf(FILE *pf, char *buf) | |
1130 | { | |
1131 | pIOSubSystem->pfnsetbuf(pf, buf); | |
1132 | } | |
1133 | ||
1134 | DllExport int | |
1135 | win32_setvbuf(FILE *pf, char *buf, int type, size_t size) | |
1136 | { | |
1137 | return pIOSubSystem->pfnsetvbuf(pf, buf, type, size); | |
1138 | } | |
1139 | ||
1140 | DllExport int | |
1141 | win32_flushall(void) | |
1142 | { | |
1143 | return pIOSubSystem->pfnflushall(); | |
1144 | } | |
1145 | ||
1146 | DllExport int | |
1147 | win32_fcloseall(void) | |
1148 | { | |
1149 | return pIOSubSystem->pfnfcloseall(); | |
1150 | } | |
1151 | ||
1152 | DllExport char* | |
1153 | win32_fgets(char *s, int n, FILE *pf) | |
1154 | { | |
1155 | return pIOSubSystem->pfnfgets(s, n, pf); | |
1156 | } | |
1157 | ||
1158 | DllExport char* | |
1159 | win32_gets(char *s) | |
1160 | { | |
1161 | return pIOSubSystem->pfngets(s); | |
1162 | } | |
1163 | ||
1164 | DllExport int | |
1165 | win32_fgetc(FILE *pf) | |
1166 | { | |
1167 | return pIOSubSystem->pfnfgetc(pf); | |
1168 | } | |
1169 | ||
1170 | DllExport int | |
1171 | win32_putc(int c, FILE *pf) | |
1172 | { | |
1173 | return pIOSubSystem->pfnputc(c,pf); | |
1174 | } | |
1175 | ||
1176 | DllExport int | |
1177 | win32_puts(const char *s) | |
1178 | { | |
1179 | return pIOSubSystem->pfnputs(s); | |
1180 | } | |
1181 | ||
1182 | DllExport int | |
1183 | win32_getchar(void) | |
1184 | { | |
1185 | return pIOSubSystem->pfngetchar(); | |
1186 | } | |
1187 | ||
1188 | DllExport int | |
1189 | win32_putchar(int c) | |
1190 | { | |
1191 | return pIOSubSystem->pfnputchar(c); | |
1192 | } | |
1193 | ||
1194 | DllExport void* | |
1195 | win32_malloc(size_t size) | |
1196 | { | |
1197 | return pIOSubSystem->pfnmalloc(size); | |
1198 | } | |
1199 | ||
1200 | DllExport void* | |
1201 | win32_calloc(size_t numitems, size_t size) | |
1202 | { | |
1203 | return pIOSubSystem->pfncalloc(numitems,size); | |
1204 | } | |
1205 | ||
1206 | DllExport void* | |
1207 | win32_realloc(void *block, size_t size) | |
1208 | { | |
1209 | return pIOSubSystem->pfnrealloc(block,size); | |
1210 | } | |
1211 | ||
1212 | DllExport void | |
1213 | win32_free(void *block) | |
1214 | { | |
1215 | pIOSubSystem->pfnfree(block); | |
1216 | } | |
1217 | ||
68dc0745 | 1218 | int |
65e48ea9 | 1219 | win32_open_osfhandle(long handle, int flags) |
0a753a76 | 1220 | { |
68dc0745 | 1221 | return pIOSubSystem->pfn_open_osfhandle(handle, flags); |
0a753a76 | 1222 | } |
1223 | ||
68dc0745 | 1224 | long |
65e48ea9 | 1225 | win32_get_osfhandle(int fd) |
0a753a76 | 1226 | { |
68dc0745 | 1227 | return pIOSubSystem->pfn_get_osfhandle(fd); |
0a753a76 | 1228 | } |
7bac28a0 | 1229 | |
7bac28a0 | 1230 | /* |
1231 | * Extras. | |
1232 | */ | |
1233 | ||
7bac28a0 | 1234 | DllExport int |
1235 | win32_flock(int fd, int oper) | |
1236 | { | |
7bac28a0 | 1237 | if (!IsWinNT()) { |
1238 | croak("flock() unimplemented on this platform"); | |
1239 | return -1; | |
1240 | } | |
c90c0ff4 | 1241 | return pIOSubSystem->pfnflock(fd, oper); |
7bac28a0 | 1242 | } |
1243 | ||
ad2e33dc GS |
1244 | static |
1245 | XS(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 | ||
1266 | static | |
1267 | XS(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 | ||
1278 | static | |
1279 | XS(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 | ||
1294 | static | |
1295 | XS(w32_GetLastError) | |
1296 | { | |
1297 | dXSARGS; | |
1298 | XSRETURN_IV(GetLastError()); | |
1299 | } | |
1300 | ||
1301 | static | |
1302 | XS(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 | ||
1315 | static | |
1316 | XS(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 | ||
1330 | static | |
1331 | XS(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 | ||
1350 | static | |
1351 | XS(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 | ||
1370 | static | |
1371 | XS(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 | ||
1389 | static | |
1390 | XS(w32_IsWinNT) | |
1391 | { | |
1392 | dXSARGS; | |
1393 | XSRETURN_IV(IsWinNT()); | |
1394 | } | |
1395 | ||
1396 | static | |
1397 | XS(w32_IsWin95) | |
1398 | { | |
1399 | dXSARGS; | |
1400 | XSRETURN_IV(IsWin95()); | |
1401 | } | |
1402 | ||
1403 | static | |
1404 | XS(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 | ||
1421 | static | |
1422 | XS(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 | ||
1460 | static | |
1461 | XS(w32_GetTickCount) | |
1462 | { | |
1463 | dXSARGS; | |
1464 | XSRETURN_IV(GetTickCount()); | |
1465 | } | |
1466 | ||
1467 | static | |
1468 | XS(w32_GetShortPathName) | |
1469 | { | |
1470 | dXSARGS; | |
1471 | SV *shortpath; | |
e8bab181 | 1472 | DWORD len; |
ad2e33dc GS |
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 */ | |
e8bab181 GS |
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); | |
ad2e33dc | 1487 | ST(0) = shortpath; |
e8bab181 | 1488 | } |
ad2e33dc GS |
1489 | else |
1490 | ST(0) = &sv_undef; | |
1491 | XSRETURN(1); | |
1492 | } | |
1493 | ||
1494 | void | |
1495 | init_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 | ||
1532 | void | |
1533 | Perl_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 | */ | |
3dc9191e | 1541 | #if !defined(_ALPHA_) |
ad2e33dc | 1542 | _control87(MCW_EM, MCW_EM); |
3dc9191e | 1543 | #endif |
ad2e33dc | 1544 | } |
d55594ae GS |
1545 | |
1546 | ||
1547 | ||
1548 |