Commit | Line | Data |
e1caacb4 |
1 | /* WINCE.C - stuff for Windows CE |
2 | * |
ca6c63e1 |
3 | * Time-stamp: <26/10/01 15:25:20 keuchel@keuchelnt> |
e1caacb4 |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. |
7 | */ |
8 | |
9 | #define WIN32_LEAN_AND_MEAN |
10 | #define WIN32IO_IS_STDIO |
11 | #include <windows.h> |
216db7ee |
12 | #include <signal.h> |
e1caacb4 |
13 | |
14 | #define PERLIO_NOT_STDIO 0 |
15 | |
16 | #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO) |
17 | #define PerlIO FILE |
18 | #endif |
19 | |
20 | #define wince_private |
21 | #include "errno.h" |
22 | |
23 | #include "EXTERN.h" |
24 | #include "perl.h" |
25 | |
26 | #define NO_XSLOCKS |
27 | #define PERL_NO_GET_CONTEXT |
28 | #include "XSUB.h" |
29 | |
30 | #include "win32iop.h" |
31 | #include <string.h> |
32 | #include <stdarg.h> |
33 | #include <float.h> |
34 | #include <shellapi.h> |
814ffeea |
35 | #include <process.h> |
e1caacb4 |
36 | |
37 | #define perl |
38 | #include "celib_defs.h" |
39 | #include "cewin32.h" |
40 | #include "cecrt.h" |
41 | #include "cewin32_defs.h" |
42 | #include "cecrt_defs.h" |
43 | |
94081db1 |
44 | #define GetCurrentDirectoryW XCEGetCurrentDirectoryW |
45 | |
e1caacb4 |
46 | #ifdef PALM_SIZE |
47 | #include "stdio-palmsize.h" |
48 | #endif |
49 | |
50 | #define EXECF_EXEC 1 |
51 | #define EXECF_SPAWN 2 |
52 | #define EXECF_SPAWN_NOWAIT 3 |
53 | |
54 | #if defined(PERL_IMPLICIT_SYS) |
55 | # undef win32_get_privlib |
56 | # define win32_get_privlib g_win32_get_privlib |
57 | # undef win32_get_sitelib |
58 | # define win32_get_sitelib g_win32_get_sitelib |
59 | # undef win32_get_vendorlib |
60 | # define win32_get_vendorlib g_win32_get_vendorlib |
61 | # undef do_spawn |
62 | # define do_spawn g_do_spawn |
63 | # undef getlogin |
64 | # define getlogin g_getlogin |
65 | #endif |
66 | |
814ffeea |
67 | static void get_shell(void); |
68 | static long tokenize(const char *str, char **dest, char ***destv); |
69 | static int do_spawn2(pTHX_ char *cmd, int exectype); |
70 | static BOOL has_shell_metachars(char *ptr); |
e1caacb4 |
71 | static long filetime_to_clock(PFILETIME ft); |
72 | static BOOL filetime_from_time(PFILETIME ft, time_t t); |
73 | static char * get_emd_part(SV **leading, char *trailing, ...); |
814ffeea |
74 | static void remove_dead_process(long deceased); |
75 | static long find_pid(int pid); |
76 | static char * qualified_path(const char *cmd); |
e1caacb4 |
77 | static char * win32_get_xlib(const char *pl, const char *xlib, |
78 | const char *libname); |
79 | |
814ffeea |
80 | #ifdef USE_ITHREADS |
81 | static void remove_dead_pseudo_process(long child); |
82 | static long find_pseudo_pid(int pid); |
83 | #endif |
84 | |
85 | int _fmode = O_TEXT; /* celib do not provide _fmode, so we define it here */ |
86 | |
e1caacb4 |
87 | START_EXTERN_C |
88 | HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE; |
89 | char w32_module_name[MAX_PATH+1]; |
90 | END_EXTERN_C |
91 | |
92 | static DWORD w32_platform = (DWORD)-1; |
93 | |
94 | int |
95 | IsWin95(void) |
96 | { |
97 | return (win32_os_id() == VER_PLATFORM_WIN32_WINDOWS); |
98 | } |
99 | |
100 | int |
101 | IsWinNT(void) |
102 | { |
103 | return (win32_os_id() == VER_PLATFORM_WIN32_NT); |
104 | } |
105 | |
106 | int |
107 | IsWinCE(void) |
108 | { |
109 | return (win32_os_id() == VER_PLATFORM_WIN32_CE); |
110 | } |
111 | |
112 | EXTERN_C void |
113 | set_w32_module_name(void) |
114 | { |
115 | char* ptr; |
116 | XCEGetModuleFileNameA((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE) |
117 | ? XCEGetModuleHandleA(NULL) |
118 | : w32_perldll_handle), |
119 | w32_module_name, sizeof(w32_module_name)); |
120 | |
121 | /* normalize to forward slashes */ |
122 | ptr = w32_module_name; |
123 | while (*ptr) { |
124 | if (*ptr == '\\') |
125 | *ptr = '/'; |
126 | ++ptr; |
127 | } |
128 | } |
129 | |
130 | /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */ |
131 | static char* |
132 | get_regstr_from(HKEY hkey, const char *valuename, SV **svp) |
133 | { |
134 | /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */ |
135 | HKEY handle; |
136 | DWORD type; |
137 | const char *subkey = "Software\\Perl"; |
138 | char *str = Nullch; |
139 | long retval; |
140 | |
141 | retval = XCERegOpenKeyExA(hkey, subkey, 0, KEY_READ, &handle); |
142 | if (retval == ERROR_SUCCESS) { |
143 | DWORD datalen; |
144 | retval = XCERegQueryValueExA(handle, valuename, 0, &type, NULL, &datalen); |
145 | if (retval == ERROR_SUCCESS && type == REG_SZ) { |
acfe0abc |
146 | dTHX; |
e1caacb4 |
147 | if (!*svp) |
148 | *svp = sv_2mortal(newSVpvn("",0)); |
149 | SvGROW(*svp, datalen); |
150 | retval = XCERegQueryValueExA(handle, valuename, 0, NULL, |
151 | (PBYTE)SvPVX(*svp), &datalen); |
152 | if (retval == ERROR_SUCCESS) { |
153 | str = SvPVX(*svp); |
154 | SvCUR_set(*svp,datalen-1); |
155 | } |
156 | } |
157 | RegCloseKey(handle); |
158 | } |
159 | return str; |
160 | } |
161 | |
162 | /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */ |
163 | static char* |
164 | get_regstr(const char *valuename, SV **svp) |
165 | { |
166 | char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp); |
167 | if (!str) |
168 | str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp); |
169 | return str; |
170 | } |
171 | |
172 | /* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */ |
173 | static char * |
174 | get_emd_part(SV **prev_pathp, char *trailing_path, ...) |
175 | { |
176 | char base[10]; |
177 | va_list ap; |
178 | char mod_name[MAX_PATH+1]; |
179 | char *ptr; |
180 | char *optr; |
181 | char *strip; |
182 | int oldsize, newsize; |
183 | STRLEN baselen; |
184 | |
185 | va_start(ap, trailing_path); |
186 | strip = va_arg(ap, char *); |
187 | |
188 | sprintf(base, "%d.%d", (int)PERL_REVISION, (int)PERL_VERSION); |
189 | baselen = strlen(base); |
190 | |
191 | if (!*w32_module_name) { |
192 | set_w32_module_name(); |
193 | } |
194 | strcpy(mod_name, w32_module_name); |
195 | ptr = strrchr(mod_name, '/'); |
196 | while (ptr && strip) { |
197 | /* look for directories to skip back */ |
198 | optr = ptr; |
199 | *ptr = '\0'; |
200 | ptr = strrchr(mod_name, '/'); |
201 | /* avoid stripping component if there is no slash, |
202 | * or it doesn't match ... */ |
203 | if (!ptr || stricmp(ptr+1, strip) != 0) { |
204 | /* ... but not if component matches m|5\.$patchlevel.*| */ |
205 | if (!ptr || !(*strip == '5' && *(ptr+1) == '5' |
206 | && strncmp(strip, base, baselen) == 0 |
207 | && strncmp(ptr+1, base, baselen) == 0)) |
208 | { |
209 | *optr = '/'; |
210 | ptr = optr; |
211 | } |
212 | } |
213 | strip = va_arg(ap, char *); |
214 | } |
215 | if (!ptr) { |
216 | ptr = mod_name; |
217 | *ptr++ = '.'; |
218 | *ptr = '/'; |
219 | } |
220 | va_end(ap); |
221 | strcpy(++ptr, trailing_path); |
222 | |
223 | /* only add directory if it exists */ |
224 | if (XCEGetFileAttributesA(mod_name) != (DWORD) -1) { |
225 | /* directory exists */ |
acfe0abc |
226 | dTHX; |
e1caacb4 |
227 | if (!*prev_pathp) |
228 | *prev_pathp = sv_2mortal(newSVpvn("",0)); |
229 | sv_catpvn(*prev_pathp, ";", 1); |
230 | sv_catpv(*prev_pathp, mod_name); |
231 | return SvPVX(*prev_pathp); |
232 | } |
233 | |
234 | return Nullch; |
235 | } |
236 | |
237 | char * |
238 | win32_get_privlib(const char *pl) |
239 | { |
acfe0abc |
240 | dTHX; |
e1caacb4 |
241 | char *stdlib = "lib"; |
242 | char buffer[MAX_PATH+1]; |
243 | SV *sv = Nullsv; |
244 | |
245 | /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */ |
246 | sprintf(buffer, "%s-%s", stdlib, pl); |
247 | if (!get_regstr(buffer, &sv)) |
248 | (void)get_regstr(stdlib, &sv); |
249 | |
250 | /* $stdlib .= ";$EMD/../../lib" */ |
251 | return get_emd_part(&sv, stdlib, ARCHNAME, "bin", Nullch); |
252 | } |
253 | |
254 | static char * |
255 | win32_get_xlib(const char *pl, const char *xlib, const char *libname) |
256 | { |
acfe0abc |
257 | dTHX; |
e1caacb4 |
258 | char regstr[40]; |
259 | char pathstr[MAX_PATH+1]; |
260 | DWORD datalen; |
261 | int len, newsize; |
262 | SV *sv1 = Nullsv; |
263 | SV *sv2 = Nullsv; |
264 | |
265 | /* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */ |
266 | sprintf(regstr, "%s-%s", xlib, pl); |
267 | (void)get_regstr(regstr, &sv1); |
268 | |
269 | /* $xlib .= |
270 | * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */ |
271 | sprintf(pathstr, "%s/%s/lib", libname, pl); |
272 | (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch); |
273 | |
274 | /* $HKCU{$xlib} || $HKLM{$xlib} . ---; */ |
275 | (void)get_regstr(xlib, &sv2); |
276 | |
277 | /* $xlib .= |
278 | * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */ |
279 | sprintf(pathstr, "%s/lib", libname); |
280 | (void)get_emd_part(&sv2, pathstr, ARCHNAME, "bin", pl, Nullch); |
281 | |
282 | if (!sv1 && !sv2) |
283 | return Nullch; |
284 | if (!sv1) |
285 | return SvPVX(sv2); |
286 | if (!sv2) |
287 | return SvPVX(sv1); |
288 | |
289 | sv_catpvn(sv1, ";", 1); |
290 | sv_catsv(sv1, sv2); |
291 | |
292 | return SvPVX(sv1); |
293 | } |
294 | |
295 | char * |
296 | win32_get_sitelib(const char *pl) |
297 | { |
298 | return win32_get_xlib(pl, "sitelib", "site"); |
299 | } |
300 | |
301 | #ifndef PERL_VENDORLIB_NAME |
302 | # define PERL_VENDORLIB_NAME "vendor" |
303 | #endif |
304 | |
305 | char * |
306 | win32_get_vendorlib(const char *pl) |
307 | { |
308 | return win32_get_xlib(pl, "vendorlib", PERL_VENDORLIB_NAME); |
309 | } |
310 | |
814ffeea |
311 | static BOOL |
312 | has_shell_metachars(char *ptr) |
313 | { |
314 | int inquote = 0; |
315 | char quote = '\0'; |
316 | |
317 | /* |
318 | * Scan string looking for redirection (< or >) or pipe |
319 | * characters (|) that are not in a quoted string. |
320 | * Shell variable interpolation (%VAR%) can also happen inside strings. |
321 | */ |
322 | while (*ptr) { |
323 | switch(*ptr) { |
324 | case '%': |
325 | return TRUE; |
326 | case '\'': |
327 | case '\"': |
328 | if (inquote) { |
329 | if (quote == *ptr) { |
330 | inquote = 0; |
331 | quote = '\0'; |
332 | } |
333 | } |
334 | else { |
335 | quote = *ptr; |
336 | inquote++; |
337 | } |
338 | break; |
339 | case '>': |
340 | case '<': |
341 | case '|': |
342 | if (!inquote) |
343 | return TRUE; |
344 | default: |
345 | break; |
346 | } |
347 | ++ptr; |
348 | } |
349 | return FALSE; |
350 | } |
351 | |
e1caacb4 |
352 | #if !defined(PERL_IMPLICIT_SYS) |
353 | /* since the current process environment is being updated in util.c |
354 | * the library functions will get the correct environment |
355 | */ |
356 | PerlIO * |
357 | Perl_my_popen(pTHX_ char *cmd, char *mode) |
358 | { |
359 | printf("popen(%s)\n", cmd); |
360 | |
361 | Perl_croak(aTHX_ PL_no_func, "popen"); |
362 | return NULL; |
363 | } |
364 | |
365 | long |
366 | Perl_my_pclose(pTHX_ PerlIO *fp) |
367 | { |
368 | Perl_croak(aTHX_ PL_no_func, "pclose"); |
369 | return -1; |
370 | } |
371 | #endif |
372 | |
373 | DllExport unsigned long |
374 | win32_os_id(void) |
375 | { |
376 | static OSVERSIONINFOA osver; |
377 | |
378 | if (osver.dwPlatformId != w32_platform) { |
379 | memset(&osver, 0, sizeof(OSVERSIONINFOA)); |
380 | osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA); |
381 | XCEGetVersionExA(&osver); |
382 | w32_platform = osver.dwPlatformId; |
383 | } |
384 | return (unsigned long)w32_platform; |
385 | } |
386 | |
387 | DllExport int |
388 | win32_getpid(void) |
389 | { |
814ffeea |
390 | int pid; |
391 | #ifdef USE_ITHREADS |
392 | dTHX; |
393 | if (w32_pseudo_id) |
394 | return -((int)w32_pseudo_id); |
395 | #endif |
396 | pid = xcegetpid(); |
397 | return pid; |
e1caacb4 |
398 | } |
399 | |
814ffeea |
400 | /* Tokenize a string. Words are null-separated, and the list |
401 | * ends with a doubled null. Any character (except null and |
402 | * including backslash) may be escaped by preceding it with a |
403 | * backslash (the backslash will be stripped). |
404 | * Returns number of words in result buffer. |
405 | */ |
406 | static long |
407 | tokenize(const char *str, char **dest, char ***destv) |
e1caacb4 |
408 | { |
814ffeea |
409 | char *retstart = Nullch; |
410 | char **retvstart = 0; |
411 | int items = -1; |
412 | if (str) { |
413 | dTHX; |
414 | int slen = strlen(str); |
415 | register char *ret; |
416 | register char **retv; |
a02a5408 |
417 | Newx(ret, slen+2, char); |
418 | Newx(retv, (slen+3)/2, char*); |
814ffeea |
419 | |
420 | retstart = ret; |
421 | retvstart = retv; |
422 | *retv = ret; |
423 | items = 0; |
424 | while (*str) { |
425 | *ret = *str++; |
426 | if (*ret == '\\' && *str) |
427 | *ret = *str++; |
428 | else if (*ret == ' ') { |
429 | while (*str == ' ') |
430 | str++; |
431 | if (ret == retstart) |
432 | ret--; |
433 | else { |
434 | *ret = '\0'; |
435 | ++items; |
436 | if (*str) |
437 | *++retv = ret+1; |
438 | } |
439 | } |
440 | else if (!*str) |
441 | ++items; |
442 | ret++; |
443 | } |
444 | retvstart[items] = Nullch; |
445 | *ret++ = '\0'; |
446 | *ret = '\0'; |
447 | } |
448 | *dest = retstart; |
449 | *destv = retvstart; |
450 | return items; |
e1caacb4 |
451 | } |
452 | |
453 | DllExport int |
454 | win32_pipe(int *pfd, unsigned int size, int mode) |
455 | { |
216db7ee |
456 | dTHX; |
e1caacb4 |
457 | Perl_croak(aTHX_ PL_no_func, "pipe"); |
458 | return -1; |
459 | } |
460 | |
461 | DllExport int |
462 | win32_times(struct tms *timebuf) |
463 | { |
216db7ee |
464 | dTHX; |
e1caacb4 |
465 | Perl_croak(aTHX_ PL_no_func, "times"); |
466 | return -1; |
467 | } |
468 | |
42165d27 |
469 | /* TODO */ |
216db7ee |
470 | Sighandler_t |
471 | win32_signal(int sig, Sighandler_t subcode) |
42165d27 |
472 | { |
1829b0dc |
473 | return xcesignal(sig, subcode); |
42165d27 |
474 | } |
216db7ee |
475 | |
814ffeea |
476 | static void |
477 | get_shell(void) |
478 | { |
479 | dTHX; |
480 | if (!w32_perlshell_tokens) { |
481 | /* we don't use COMSPEC here for two reasons: |
482 | * 1. the same reason perl on UNIX doesn't use SHELL--rampant and |
483 | * uncontrolled unportability of the ensuing scripts. |
484 | * 2. PERL5SHELL could be set to a shell that may not be fit for |
485 | * interactive use (which is what most programs look in COMSPEC |
486 | * for). |
487 | */ |
488 | const char* defaultshell = (IsWinNT() |
489 | ? "cmd.exe /x/d/c" : "command.com /c"); |
490 | const char *usershell = PerlEnv_getenv("PERL5SHELL"); |
491 | w32_perlshell_items = tokenize(usershell ? usershell : defaultshell, |
492 | &w32_perlshell_tokens, |
493 | &w32_perlshell_vec); |
494 | } |
495 | } |
496 | |
497 | int |
498 | Perl_do_aspawn(pTHX_ SV *really, SV **mark, SV **sp) |
499 | { |
500 | Perl_croak(aTHX_ PL_no_func, "aspawn"); |
501 | return -1; |
502 | } |
42165d27 |
503 | |
814ffeea |
504 | /* returns pointer to the next unquoted space or the end of the string */ |
505 | static char* |
506 | find_next_space(const char *s) |
e1caacb4 |
507 | { |
814ffeea |
508 | bool in_quotes = FALSE; |
509 | while (*s) { |
510 | /* ignore doubled backslashes, or backslash+quote */ |
511 | if (*s == '\\' && (s[1] == '\\' || s[1] == '"')) { |
512 | s += 2; |
513 | } |
514 | /* keep track of when we're within quotes */ |
515 | else if (*s == '"') { |
516 | s++; |
517 | in_quotes = !in_quotes; |
518 | } |
519 | /* break it up only at spaces that aren't in quotes */ |
520 | else if (!in_quotes && isSPACE(*s)) |
521 | return (char*)s; |
522 | else |
523 | s++; |
524 | } |
525 | return (char*)s; |
526 | } |
527 | |
528 | #if 1 |
529 | static int |
530 | do_spawn2(pTHX_ char *cmd, int exectype) |
531 | { |
532 | char **a; |
533 | char *s; |
534 | char **argv; |
535 | int status = -1; |
536 | BOOL needToTry = TRUE; |
537 | char *cmd2; |
538 | |
539 | /* Save an extra exec if possible. See if there are shell |
540 | * metacharacters in it */ |
541 | if (!has_shell_metachars(cmd)) { |
a02a5408 |
542 | Newx(argv, strlen(cmd) / 2 + 2, char*); |
543 | Newx(cmd2, strlen(cmd) + 1, char); |
814ffeea |
544 | strcpy(cmd2, cmd); |
545 | a = argv; |
546 | for (s = cmd2; *s;) { |
547 | while (*s && isSPACE(*s)) |
548 | s++; |
549 | if (*s) |
550 | *(a++) = s; |
551 | s = find_next_space(s); |
552 | if (*s) |
553 | *s++ = '\0'; |
554 | } |
555 | *a = Nullch; |
556 | if (argv[0]) { |
557 | switch (exectype) { |
558 | case EXECF_SPAWN: |
559 | status = win32_spawnvp(P_WAIT, argv[0], |
560 | (const char* const*)argv); |
561 | break; |
562 | case EXECF_SPAWN_NOWAIT: |
563 | status = win32_spawnvp(P_NOWAIT, argv[0], |
564 | (const char* const*)argv); |
565 | break; |
566 | case EXECF_EXEC: |
567 | status = win32_execvp(argv[0], (const char* const*)argv); |
568 | break; |
569 | } |
570 | if (status != -1 || errno == 0) |
571 | needToTry = FALSE; |
572 | } |
573 | Safefree(argv); |
574 | Safefree(cmd2); |
575 | } |
576 | if (needToTry) { |
577 | char **argv; |
578 | int i = -1; |
579 | get_shell(); |
a02a5408 |
580 | Newx(argv, w32_perlshell_items + 2, char*); |
814ffeea |
581 | while (++i < w32_perlshell_items) |
582 | argv[i] = w32_perlshell_vec[i]; |
583 | argv[i++] = cmd; |
584 | argv[i] = Nullch; |
585 | switch (exectype) { |
586 | case EXECF_SPAWN: |
587 | status = win32_spawnvp(P_WAIT, argv[0], |
588 | (const char* const*)argv); |
589 | break; |
590 | case EXECF_SPAWN_NOWAIT: |
591 | status = win32_spawnvp(P_NOWAIT, argv[0], |
592 | (const char* const*)argv); |
593 | break; |
594 | case EXECF_EXEC: |
595 | status = win32_execvp(argv[0], (const char* const*)argv); |
596 | break; |
597 | } |
598 | cmd = argv[0]; |
599 | Safefree(argv); |
600 | } |
601 | if (exectype == EXECF_SPAWN_NOWAIT) { |
602 | if (IsWin95()) |
603 | PL_statusvalue = -1; /* >16bits hint for pp_system() */ |
604 | } |
605 | else { |
606 | if (status < 0) { |
607 | if (ckWARN(WARN_EXEC)) |
608 | Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't %s \"%s\": %s", |
609 | (exectype == EXECF_EXEC ? "exec" : "spawn"), |
610 | cmd, strerror(errno)); |
611 | status = 255 * 256; |
612 | } |
613 | else |
614 | status *= 256; |
615 | PL_statusvalue = status; |
616 | } |
617 | return (status); |
618 | } |
619 | |
620 | int |
621 | Perl_do_spawn(pTHX_ char *cmd) |
622 | { |
623 | return do_spawn2(aTHX_ cmd, EXECF_SPAWN); |
624 | } |
625 | |
626 | int |
627 | Perl_do_spawn_nowait(pTHX_ char *cmd) |
628 | { |
629 | return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT); |
630 | } |
631 | |
632 | bool |
633 | Perl_do_exec(pTHX_ char *cmd) |
634 | { |
635 | do_spawn2(aTHX_ cmd, EXECF_EXEC); |
636 | return FALSE; |
637 | } |
638 | |
639 | /* The idea here is to read all the directory names into a string table |
640 | * (separated by nulls) and when one of the other dir functions is called |
641 | * return the pointer to the current file name. |
642 | */ |
643 | DllExport DIR * |
0e06f75d |
644 | win32_opendir(const char *filename) |
814ffeea |
645 | { |
646 | dTHX; |
647 | DIR *dirp; |
648 | long len; |
649 | long idx; |
650 | char scanname[MAX_PATH+3]; |
651 | Stat_t sbuf; |
652 | WIN32_FIND_DATAA aFindData; |
653 | WIN32_FIND_DATAW wFindData; |
654 | HANDLE fh; |
655 | char buffer[MAX_PATH*2]; |
656 | WCHAR wbuffer[MAX_PATH+1]; |
657 | char* ptr; |
658 | |
659 | len = strlen(filename); |
660 | if (len > MAX_PATH) |
661 | return NULL; |
662 | |
663 | /* check to see if filename is a directory */ |
664 | if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode)) |
665 | return NULL; |
666 | |
667 | /* Get us a DIR structure */ |
a02a5408 |
668 | Newxz(dirp, 1, DIR); |
814ffeea |
669 | |
670 | /* Create the search pattern */ |
671 | strcpy(scanname, filename); |
672 | |
673 | /* bare drive name means look in cwd for drive */ |
674 | if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') { |
675 | scanname[len++] = '.'; |
676 | scanname[len++] = '/'; |
677 | } |
678 | else if (scanname[len-1] != '/' && scanname[len-1] != '\\') { |
679 | scanname[len++] = '/'; |
680 | } |
681 | scanname[len++] = '*'; |
682 | scanname[len] = '\0'; |
683 | |
684 | /* do the FindFirstFile call */ |
685 | fh = FindFirstFile(PerlDir_mapA(scanname), &aFindData); |
686 | dirp->handle = fh; |
687 | if (fh == INVALID_HANDLE_VALUE) { |
688 | DWORD err = GetLastError(); |
689 | /* FindFirstFile() fails on empty drives! */ |
690 | switch (err) { |
691 | case ERROR_FILE_NOT_FOUND: |
692 | return dirp; |
693 | case ERROR_NO_MORE_FILES: |
694 | case ERROR_PATH_NOT_FOUND: |
695 | errno = ENOENT; |
696 | break; |
697 | case ERROR_NOT_ENOUGH_MEMORY: |
698 | errno = ENOMEM; |
699 | break; |
700 | default: |
701 | errno = EINVAL; |
702 | break; |
703 | } |
704 | Safefree(dirp); |
705 | return NULL; |
706 | } |
707 | |
708 | /* now allocate the first part of the string table for |
709 | * the filenames that we find. |
710 | */ |
711 | ptr = aFindData.cFileName; |
712 | idx = strlen(ptr)+1; |
713 | if (idx < 256) |
714 | dirp->size = 128; |
715 | else |
716 | dirp->size = idx; |
a02a5408 |
717 | Newx(dirp->start, dirp->size, char); |
814ffeea |
718 | strcpy(dirp->start, ptr); |
719 | dirp->nfiles++; |
720 | dirp->end = dirp->curr = dirp->start; |
721 | dirp->end += idx; |
722 | return dirp; |
723 | } |
724 | |
725 | |
726 | /* Readdir just returns the current string pointer and bumps the |
727 | * string pointer to the nDllExport entry. |
728 | */ |
729 | DllExport struct direct * |
730 | win32_readdir(DIR *dirp) |
731 | { |
732 | long len; |
733 | |
734 | if (dirp->curr) { |
735 | /* first set up the structure to return */ |
736 | len = strlen(dirp->curr); |
737 | strcpy(dirp->dirstr.d_name, dirp->curr); |
738 | dirp->dirstr.d_namlen = len; |
739 | |
740 | /* Fake an inode */ |
741 | dirp->dirstr.d_ino = dirp->curr - dirp->start; |
742 | |
743 | /* Now set up for the next call to readdir */ |
744 | dirp->curr += len + 1; |
745 | if (dirp->curr >= dirp->end) { |
746 | dTHX; |
747 | char* ptr; |
748 | BOOL res; |
749 | WIN32_FIND_DATAW wFindData; |
750 | WIN32_FIND_DATAA aFindData; |
751 | char buffer[MAX_PATH*2]; |
752 | |
753 | /* finding the next file that matches the wildcard |
754 | * (which should be all of them in this directory!). |
755 | */ |
756 | res = FindNextFile(dirp->handle, &aFindData); |
757 | if (res) |
758 | ptr = aFindData.cFileName; |
759 | if (res) { |
760 | long endpos = dirp->end - dirp->start; |
761 | long newsize = endpos + strlen(ptr) + 1; |
762 | /* bump the string table size by enough for the |
763 | * new name and its null terminator */ |
764 | while (newsize > dirp->size) { |
765 | long curpos = dirp->curr - dirp->start; |
766 | dirp->size *= 2; |
767 | Renew(dirp->start, dirp->size, char); |
768 | dirp->curr = dirp->start + curpos; |
769 | } |
770 | strcpy(dirp->start + endpos, ptr); |
771 | dirp->end = dirp->start + newsize; |
772 | dirp->nfiles++; |
773 | } |
774 | else |
775 | dirp->curr = NULL; |
776 | } |
777 | return &(dirp->dirstr); |
778 | } |
779 | else |
780 | return NULL; |
781 | } |
782 | |
783 | /* Telldir returns the current string pointer position */ |
784 | DllExport long |
785 | win32_telldir(DIR *dirp) |
786 | { |
787 | return (dirp->curr - dirp->start); |
788 | } |
789 | |
790 | |
791 | /* Seekdir moves the string pointer to a previously saved position |
792 | * (returned by telldir). |
793 | */ |
794 | DllExport void |
795 | win32_seekdir(DIR *dirp, long loc) |
796 | { |
797 | dirp->curr = dirp->start + loc; |
798 | } |
799 | |
800 | /* Rewinddir resets the string pointer to the start */ |
801 | DllExport void |
802 | win32_rewinddir(DIR *dirp) |
803 | { |
804 | dirp->curr = dirp->start; |
805 | } |
806 | |
807 | /* free the memory allocated by opendir */ |
808 | DllExport int |
809 | win32_closedir(DIR *dirp) |
810 | { |
811 | dTHX; |
812 | if (dirp->handle != INVALID_HANDLE_VALUE) |
813 | FindClose(dirp->handle); |
814 | Safefree(dirp->start); |
815 | Safefree(dirp); |
816 | return 1; |
e1caacb4 |
817 | } |
818 | |
814ffeea |
819 | #else |
820 | /////!!!!!!!!!!! return here and do right stuff!!!! |
821 | |
e1caacb4 |
822 | DllExport DIR * |
0e06f75d |
823 | win32_opendir(const char *filename) |
e1caacb4 |
824 | { |
825 | return opendir(filename); |
826 | } |
827 | |
828 | DllExport struct direct * |
829 | win32_readdir(DIR *dirp) |
830 | { |
831 | return readdir(dirp); |
832 | } |
833 | |
834 | DllExport long |
835 | win32_telldir(DIR *dirp) |
836 | { |
216db7ee |
837 | dTHX; |
e1caacb4 |
838 | Perl_croak(aTHX_ PL_no_func, "telldir"); |
839 | return -1; |
840 | } |
841 | |
842 | DllExport void |
843 | win32_seekdir(DIR *dirp, long loc) |
844 | { |
216db7ee |
845 | dTHX; |
e1caacb4 |
846 | Perl_croak(aTHX_ PL_no_func, "seekdir"); |
847 | } |
848 | |
849 | DllExport void |
850 | win32_rewinddir(DIR *dirp) |
851 | { |
216db7ee |
852 | dTHX; |
e1caacb4 |
853 | Perl_croak(aTHX_ PL_no_func, "rewinddir"); |
854 | } |
855 | |
856 | DllExport int |
857 | win32_closedir(DIR *dirp) |
858 | { |
859 | closedir(dirp); |
860 | return 0; |
861 | } |
814ffeea |
862 | #endif // 1 |
e1caacb4 |
863 | |
864 | DllExport int |
865 | win32_kill(int pid, int sig) |
866 | { |
216db7ee |
867 | dTHX; |
e1caacb4 |
868 | Perl_croak(aTHX_ PL_no_func, "kill"); |
869 | return -1; |
870 | } |
871 | |
e1caacb4 |
872 | DllExport int |
873 | win32_stat(const char *path, struct stat *sbuf) |
874 | { |
875 | return xcestat(path, sbuf); |
876 | } |
877 | |
878 | DllExport char * |
879 | win32_longpath(char *path) |
880 | { |
881 | return path; |
882 | } |
883 | |
884 | #ifndef USE_WIN32_RTL_ENV |
885 | |
886 | DllExport char * |
887 | win32_getenv(const char *name) |
888 | { |
889 | return xcegetenv(name); |
890 | } |
891 | |
892 | DllExport int |
893 | win32_putenv(const char *name) |
894 | { |
895 | return xceputenv(name); |
896 | } |
897 | |
898 | #endif |
899 | |
900 | static long |
901 | filetime_to_clock(PFILETIME ft) |
902 | { |
903 | __int64 qw = ft->dwHighDateTime; |
904 | qw <<= 32; |
905 | qw |= ft->dwLowDateTime; |
906 | qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */ |
907 | return (long) qw; |
908 | } |
909 | |
910 | /* fix utime() so it works on directories in NT */ |
911 | static BOOL |
912 | filetime_from_time(PFILETIME pFileTime, time_t Time) |
913 | { |
914 | struct tm *pTM = localtime(&Time); |
915 | SYSTEMTIME SystemTime; |
916 | FILETIME LocalTime; |
917 | |
918 | if (pTM == NULL) |
919 | return FALSE; |
920 | |
921 | SystemTime.wYear = pTM->tm_year + 1900; |
922 | SystemTime.wMonth = pTM->tm_mon + 1; |
923 | SystemTime.wDay = pTM->tm_mday; |
924 | SystemTime.wHour = pTM->tm_hour; |
925 | SystemTime.wMinute = pTM->tm_min; |
926 | SystemTime.wSecond = pTM->tm_sec; |
927 | SystemTime.wMilliseconds = 0; |
928 | |
929 | return SystemTimeToFileTime(&SystemTime, &LocalTime) && |
930 | LocalFileTimeToFileTime(&LocalTime, pFileTime); |
931 | } |
932 | |
933 | DllExport int |
934 | win32_unlink(const char *filename) |
935 | { |
936 | return xceunlink(filename); |
937 | } |
938 | |
939 | DllExport int |
940 | win32_utime(const char *filename, struct utimbuf *times) |
941 | { |
942 | return xceutime(filename, (struct _utimbuf *) times); |
943 | } |
944 | |
945 | DllExport int |
e2a02c1e |
946 | win32_gettimeofday(struct timeval *tp, void *not_used) |
947 | { |
948 | return xcegettimeofday(tp,not_used); |
949 | } |
950 | |
951 | DllExport int |
e1caacb4 |
952 | win32_uname(struct utsname *name) |
953 | { |
954 | struct hostent *hep; |
955 | STRLEN nodemax = sizeof(name->nodename)-1; |
956 | OSVERSIONINFOA osver; |
957 | |
958 | memset(&osver, 0, sizeof(OSVERSIONINFOA)); |
959 | osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA); |
960 | if (XCEGetVersionExA(&osver)) { |
961 | /* sysname */ |
962 | switch (osver.dwPlatformId) { |
963 | case VER_PLATFORM_WIN32_CE: |
964 | strcpy(name->sysname, "Windows CE"); |
965 | break; |
966 | case VER_PLATFORM_WIN32_WINDOWS: |
967 | strcpy(name->sysname, "Windows"); |
968 | break; |
969 | case VER_PLATFORM_WIN32_NT: |
970 | strcpy(name->sysname, "Windows NT"); |
971 | break; |
972 | case VER_PLATFORM_WIN32s: |
973 | strcpy(name->sysname, "Win32s"); |
974 | break; |
975 | default: |
976 | strcpy(name->sysname, "Win32 Unknown"); |
977 | break; |
978 | } |
979 | |
980 | /* release */ |
981 | sprintf(name->release, "%d.%d", |
982 | osver.dwMajorVersion, osver.dwMinorVersion); |
983 | |
984 | /* version */ |
985 | sprintf(name->version, "Build %d", |
986 | osver.dwPlatformId == VER_PLATFORM_WIN32_NT |
987 | ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff)); |
988 | if (osver.szCSDVersion[0]) { |
989 | char *buf = name->version + strlen(name->version); |
990 | sprintf(buf, " (%s)", osver.szCSDVersion); |
991 | } |
992 | } |
993 | else { |
994 | *name->sysname = '\0'; |
995 | *name->version = '\0'; |
996 | *name->release = '\0'; |
997 | } |
998 | |
999 | /* nodename */ |
1000 | hep = win32_gethostbyname("localhost"); |
1001 | if (hep) { |
1002 | STRLEN len = strlen(hep->h_name); |
1003 | if (len <= nodemax) { |
1004 | strcpy(name->nodename, hep->h_name); |
1005 | } |
1006 | else { |
1007 | strncpy(name->nodename, hep->h_name, nodemax); |
1008 | name->nodename[nodemax] = '\0'; |
1009 | } |
1010 | } |
1011 | else { |
1012 | DWORD sz = nodemax; |
1013 | if (!XCEGetComputerNameA(name->nodename, &sz)) |
1014 | *name->nodename = '\0'; |
1015 | } |
1016 | |
1017 | /* machine (architecture) */ |
1018 | { |
1019 | SYSTEM_INFO info; |
1020 | char *arch; |
1021 | GetSystemInfo(&info); |
1022 | |
e1caacb4 |
1023 | switch (info.wProcessorArchitecture) { |
e1caacb4 |
1024 | case PROCESSOR_ARCHITECTURE_INTEL: |
1025 | arch = "x86"; break; |
1026 | case PROCESSOR_ARCHITECTURE_MIPS: |
1027 | arch = "mips"; break; |
1028 | case PROCESSOR_ARCHITECTURE_ALPHA: |
1029 | arch = "alpha"; break; |
1030 | case PROCESSOR_ARCHITECTURE_PPC: |
1031 | arch = "ppc"; break; |
1032 | case PROCESSOR_ARCHITECTURE_ARM: |
1033 | arch = "arm"; break; |
1034 | case PROCESSOR_HITACHI_SH3: |
1035 | arch = "sh3"; break; |
1036 | case PROCESSOR_SHx_SH3: |
1037 | arch = "sh3"; break; |
1038 | |
1039 | default: |
1040 | arch = "unknown"; break; |
1041 | } |
1042 | strcpy(name->machine, arch); |
1043 | } |
1044 | return 0; |
1045 | } |
1046 | |
814ffeea |
1047 | /* Timing related stuff */ |
1048 | |
1049 | int |
1050 | do_raise(pTHX_ int sig) |
1051 | { |
1052 | if (sig < SIG_SIZE) { |
1053 | Sighandler_t handler = w32_sighandler[sig]; |
1054 | if (handler == SIG_IGN) { |
1055 | return 0; |
1056 | } |
1057 | else if (handler != SIG_DFL) { |
1058 | (*handler)(sig); |
1059 | return 0; |
1060 | } |
1061 | else { |
1062 | /* Choose correct default behaviour */ |
1063 | switch (sig) { |
1064 | #ifdef SIGCLD |
1065 | case SIGCLD: |
1066 | #endif |
1067 | #ifdef SIGCHLD |
1068 | case SIGCHLD: |
1069 | #endif |
1070 | case 0: |
1071 | return 0; |
1072 | case SIGTERM: |
1073 | default: |
1074 | break; |
1075 | } |
1076 | } |
1077 | } |
1078 | /* Tell caller to exit thread/process as approriate */ |
1079 | return 1; |
1080 | } |
1081 | |
216db7ee |
1082 | void |
1083 | sig_terminate(pTHX_ int sig) |
1084 | { |
2e332e71 |
1085 | Perl_warn(aTHX_ "Terminating on signal SIG%s(%d)\n",PL_sig_name[sig], sig); |
216db7ee |
1086 | /* exit() seems to be safe, my_exit() or die() is a problem in ^C |
1087 | thread |
1088 | */ |
1089 | exit(sig); |
1090 | } |
1091 | |
1092 | DllExport int |
1093 | win32_async_check(pTHX) |
1094 | { |
1095 | MSG msg; |
1096 | int ours = 1; |
1097 | /* Passing PeekMessage -1 as HWND (2nd arg) only get PostThreadMessage() messages |
1098 | * and ignores window messages - should co-exist better with windows apps e.g. Tk |
1099 | */ |
1100 | while (PeekMessage(&msg, (HWND)-1, 0, 0, PM_REMOVE|PM_NOYIELD)) { |
1101 | int sig; |
1102 | switch(msg.message) { |
1103 | |
1104 | #if 0 |
1105 | /* Perhaps some other messages could map to signals ? ... */ |
1106 | case WM_CLOSE: |
1107 | case WM_QUIT: |
1108 | /* Treat WM_QUIT like SIGHUP? */ |
1109 | sig = SIGHUP; |
1110 | goto Raise; |
1111 | break; |
1112 | #endif |
2e332e71 |
1113 | |
216db7ee |
1114 | /* We use WM_USER to fake kill() with other signals */ |
1115 | case WM_USER: { |
1116 | sig = msg.wParam; |
1117 | Raise: |
1118 | if (do_raise(aTHX_ sig)) { |
1119 | sig_terminate(aTHX_ sig); |
1120 | } |
1121 | break; |
1122 | } |
1123 | |
1124 | case WM_TIMER: { |
1125 | /* alarm() is a one-shot but SetTimer() repeats so kill it */ |
1126 | if (w32_timerid) { |
1127 | KillTimer(NULL,w32_timerid); |
1128 | w32_timerid=0; |
1129 | } |
1130 | /* Now fake a call to signal handler */ |
1131 | if (do_raise(aTHX_ 14)) { |
1132 | sig_terminate(aTHX_ 14); |
1133 | } |
1134 | break; |
1135 | } |
1136 | |
1137 | /* Otherwise do normal Win32 thing - in case it is useful */ |
1138 | default: |
1139 | TranslateMessage(&msg); |
1140 | DispatchMessage(&msg); |
1141 | ours = 0; |
1142 | break; |
1143 | } |
1144 | } |
1145 | w32_poll_count = 0; |
1146 | |
1147 | /* Above or other stuff may have set a signal flag */ |
1148 | if (PL_sig_pending) { |
1149 | despatch_signals(); |
1150 | } |
1151 | return ours; |
1152 | } |
1153 | |
1154 | /* This function will not return until the timeout has elapsed, or until |
1155 | * one of the handles is ready. */ |
1156 | DllExport DWORD |
1157 | win32_msgwait(pTHX_ DWORD count, LPHANDLE handles, DWORD timeout, LPDWORD resultp) |
1158 | { |
1159 | /* We may need several goes at this - so compute when we stop */ |
1160 | DWORD ticks = 0; |
1161 | if (timeout != INFINITE) { |
1162 | ticks = GetTickCount(); |
1163 | timeout += ticks; |
1164 | } |
1165 | while (1) { |
1166 | DWORD result = MsgWaitForMultipleObjects(count,handles,FALSE,timeout-ticks, QS_ALLEVENTS); |
1167 | if (resultp) |
1168 | *resultp = result; |
1169 | if (result == WAIT_TIMEOUT) { |
1170 | /* Ran out of time - explicit return of zero to avoid -ve if we |
1171 | have scheduling issues |
1172 | */ |
1173 | return 0; |
1174 | } |
1175 | if (timeout != INFINITE) { |
1176 | ticks = GetTickCount(); |
1177 | } |
1178 | if (result == WAIT_OBJECT_0 + count) { |
1179 | /* Message has arrived - check it */ |
1180 | (void)win32_async_check(aTHX); |
1181 | } |
1182 | else { |
1183 | /* Not timeout or message - one of handles is ready */ |
1184 | break; |
1185 | } |
1186 | } |
1187 | /* compute time left to wait */ |
1188 | ticks = timeout - ticks; |
1189 | /* If we are past the end say zero */ |
1190 | return (ticks > 0) ? ticks : 0; |
1191 | } |
1192 | |
e1caacb4 |
1193 | static UINT timerid = 0; |
1194 | |
1195 | static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time) |
1196 | { |
acfe0abc |
1197 | dTHX; |
e1caacb4 |
1198 | KillTimer(NULL,timerid); |
1199 | timerid=0; |
1200 | sighandler(14); |
1201 | } |
e1caacb4 |
1202 | |
1203 | DllExport unsigned int |
814ffeea |
1204 | win32_sleep(unsigned int t) |
1205 | { |
1206 | return xcesleep(t); |
1207 | } |
1208 | |
1209 | DllExport unsigned int |
e1caacb4 |
1210 | win32_alarm(unsigned int sec) |
1211 | { |
e1caacb4 |
1212 | /* |
1213 | * the 'obvious' implentation is SetTimer() with a callback |
1214 | * which does whatever receiving SIGALRM would do |
1215 | * we cannot use SIGALRM even via raise() as it is not |
1216 | * one of the supported codes in <signal.h> |
1217 | * |
1218 | * Snag is unless something is looking at the message queue |
1219 | * nothing happens :-( |
1220 | */ |
acfe0abc |
1221 | dTHX; |
e1caacb4 |
1222 | if (sec) |
1223 | { |
1224 | timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc); |
1225 | if (!timerid) |
1226 | Perl_croak_nocontext("Cannot set timer"); |
1227 | } |
1228 | else |
1229 | { |
1230 | if (timerid) |
1231 | { |
1232 | KillTimer(NULL,timerid); |
1233 | timerid=0; |
1234 | } |
1235 | } |
e1caacb4 |
1236 | return 0; |
1237 | } |
1238 | |
1239 | #ifdef HAVE_DES_FCRYPT |
1240 | extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf); |
1241 | #endif |
1242 | |
1243 | DllExport char * |
1244 | win32_crypt(const char *txt, const char *salt) |
1245 | { |
acfe0abc |
1246 | dTHX; |
e1caacb4 |
1247 | #ifdef HAVE_DES_FCRYPT |
1248 | dTHR; |
1249 | return des_fcrypt(txt, salt, w32_crypt_buffer); |
1250 | #else |
1251 | Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia."); |
1252 | return Nullch; |
1253 | #endif |
1254 | } |
1255 | |
e1caacb4 |
1256 | |
1257 | /* |
1258 | * redirected io subsystem for all XS modules |
1259 | * |
1260 | */ |
1261 | |
1262 | DllExport int * |
1263 | win32_errno(void) |
1264 | { |
1265 | return (&errno); |
1266 | } |
1267 | |
814ffeea |
1268 | DllExport char *** |
1269 | win32_environ(void) |
1270 | { |
1271 | return (&(environ)); |
1272 | } |
1273 | |
e1caacb4 |
1274 | /* the rest are the remapped stdio routines */ |
1275 | DllExport FILE * |
1276 | win32_stderr(void) |
1277 | { |
1278 | return (stderr); |
1279 | } |
1280 | |
216db7ee |
1281 | char *g_getlogin() { |
1282 | return "no-getlogin"; |
1283 | } |
1284 | |
e1caacb4 |
1285 | DllExport FILE * |
1286 | win32_stdin(void) |
1287 | { |
1288 | return (stdin); |
1289 | } |
1290 | |
1291 | DllExport FILE * |
1292 | win32_stdout() |
1293 | { |
1294 | return (stdout); |
1295 | } |
1296 | |
1297 | DllExport int |
1298 | win32_ferror(FILE *fp) |
1299 | { |
1300 | return (ferror(fp)); |
1301 | } |
1302 | |
1303 | |
1304 | DllExport int |
1305 | win32_feof(FILE *fp) |
1306 | { |
1307 | return (feof(fp)); |
1308 | } |
1309 | |
1310 | /* |
1311 | * Since the errors returned by the socket error function |
1312 | * WSAGetLastError() are not known by the library routine strerror |
1313 | * we have to roll our own. |
1314 | */ |
1315 | |
1316 | DllExport char * |
814ffeea |
1317 | win32_strerror(int e) |
e1caacb4 |
1318 | { |
1319 | return xcestrerror(e); |
1320 | } |
1321 | |
1322 | DllExport void |
1323 | win32_str_os_error(void *sv, DWORD dwErr) |
1324 | { |
acfe0abc |
1325 | dTHX; |
e1caacb4 |
1326 | |
1327 | sv_setpvn((SV*)sv, "Error", 5); |
1328 | } |
1329 | |
1330 | |
1331 | DllExport int |
1332 | win32_fprintf(FILE *fp, const char *format, ...) |
1333 | { |
1334 | va_list marker; |
1335 | va_start(marker, format); /* Initialize variable arguments. */ |
1336 | |
1337 | return (vfprintf(fp, format, marker)); |
1338 | } |
1339 | |
1340 | DllExport int |
1341 | win32_printf(const char *format, ...) |
1342 | { |
1343 | va_list marker; |
1344 | va_start(marker, format); /* Initialize variable arguments. */ |
1345 | |
1346 | return (vprintf(format, marker)); |
1347 | } |
1348 | |
1349 | DllExport int |
1350 | win32_vfprintf(FILE *fp, const char *format, va_list args) |
1351 | { |
1352 | return (vfprintf(fp, format, args)); |
1353 | } |
1354 | |
1355 | DllExport int |
1356 | win32_vprintf(const char *format, va_list args) |
1357 | { |
1358 | return (vprintf(format, args)); |
1359 | } |
1360 | |
1361 | DllExport size_t |
1362 | win32_fread(void *buf, size_t size, size_t count, FILE *fp) |
1363 | { |
1364 | return fread(buf, size, count, fp); |
1365 | } |
1366 | |
1367 | DllExport size_t |
1368 | win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp) |
1369 | { |
1370 | return fwrite(buf, size, count, fp); |
1371 | } |
1372 | |
1373 | DllExport FILE * |
1374 | win32_fopen(const char *filename, const char *mode) |
1375 | { |
1376 | return xcefopen(filename, mode); |
1377 | } |
1378 | |
1379 | DllExport FILE * |
1380 | win32_fdopen(int handle, const char *mode) |
1381 | { |
1382 | return palm_fdopen(handle, mode); |
1383 | } |
1384 | |
1385 | DllExport FILE * |
1386 | win32_freopen(const char *path, const char *mode, FILE *stream) |
1387 | { |
1388 | return xcefreopen(path, mode, stream); |
1389 | } |
1390 | |
1391 | DllExport int |
1392 | win32_fclose(FILE *pf) |
1393 | { |
1394 | return xcefclose(pf); |
1395 | } |
1396 | |
1397 | DllExport int |
1398 | win32_fputs(const char *s,FILE *pf) |
1399 | { |
1400 | return fputs(s, pf); |
1401 | } |
1402 | |
1403 | DllExport int |
1404 | win32_fputc(int c,FILE *pf) |
1405 | { |
1406 | return fputc(c,pf); |
1407 | } |
1408 | |
1409 | DllExport int |
1410 | win32_ungetc(int c,FILE *pf) |
1411 | { |
1412 | return ungetc(c,pf); |
1413 | } |
1414 | |
1415 | DllExport int |
1416 | win32_getc(FILE *pf) |
1417 | { |
1418 | return getc(pf); |
1419 | } |
1420 | |
1421 | DllExport int |
1422 | win32_fileno(FILE *pf) |
1423 | { |
1424 | return palm_fileno(pf); |
1425 | } |
1426 | |
1427 | DllExport void |
1428 | win32_clearerr(FILE *pf) |
1429 | { |
1430 | clearerr(pf); |
1431 | return; |
1432 | } |
1433 | |
1434 | DllExport int |
1435 | win32_fflush(FILE *pf) |
1436 | { |
1437 | return fflush(pf); |
1438 | } |
1439 | |
1440 | DllExport long |
1441 | win32_ftell(FILE *pf) |
1442 | { |
1443 | return ftell(pf); |
1444 | } |
1445 | |
1446 | DllExport int |
814ffeea |
1447 | win32_fseek(FILE *pf, Off_t offset,int origin) |
e1caacb4 |
1448 | { |
1449 | return fseek(pf, offset, origin); |
1450 | } |
1451 | |
cb69f87a |
1452 | /* fpos_t seems to be int64 on hpc pro! Really stupid. */ |
1453 | /* But maybe someday there will be such large disks in a hpc... */ |
e1caacb4 |
1454 | DllExport int |
1455 | win32_fgetpos(FILE *pf, fpos_t *p) |
1456 | { |
1457 | return fgetpos(pf, p); |
1458 | } |
1459 | |
1460 | DllExport int |
1461 | win32_fsetpos(FILE *pf, const fpos_t *p) |
1462 | { |
1463 | return fsetpos(pf, p); |
1464 | } |
1465 | |
1466 | DllExport void |
1467 | win32_rewind(FILE *pf) |
1468 | { |
1469 | fseek(pf, 0, SEEK_SET); |
1470 | return; |
1471 | } |
1472 | |
81003058 |
1473 | DllExport int |
1474 | win32_tmpfd(void) |
1475 | { |
1476 | dTHX; |
1477 | char prefix[MAX_PATH+1]; |
1478 | char filename[MAX_PATH+1]; |
1479 | DWORD len = GetTempPath(MAX_PATH, prefix); |
1480 | if (len && len < MAX_PATH) { |
1481 | if (GetTempFileName(prefix, "plx", 0, filename)) { |
1482 | HANDLE fh = CreateFile(filename, |
1483 | DELETE | GENERIC_READ | GENERIC_WRITE, |
1484 | 0, |
1485 | NULL, |
1486 | CREATE_ALWAYS, |
1487 | FILE_ATTRIBUTE_NORMAL |
1488 | | FILE_FLAG_DELETE_ON_CLOSE, |
1489 | NULL); |
1490 | if (fh != INVALID_HANDLE_VALUE) { |
1491 | int fd = win32_open_osfhandle((intptr_t)fh, 0); |
1492 | if (fd >= 0) { |
1493 | #if defined(__BORLANDC__) |
1494 | setmode(fd,O_BINARY); |
1495 | #endif |
1496 | DEBUG_p(PerlIO_printf(Perl_debug_log, |
1497 | "Created tmpfile=%s\n",filename)); |
1498 | return fd; |
1499 | } |
1500 | } |
1501 | } |
1502 | } |
1503 | return -1; |
1504 | } |
1505 | |
e1caacb4 |
1506 | DllExport FILE* |
1507 | win32_tmpfile(void) |
1508 | { |
81003058 |
1509 | int fd = win32_tmpfd(); |
1510 | if (fd >= 0) |
1511 | return win32_fdopen(fd, "w+b"); |
1512 | return NULL; |
e1caacb4 |
1513 | } |
1514 | |
1515 | DllExport void |
1516 | win32_abort(void) |
1517 | { |
1518 | xceabort(); |
1519 | |
1520 | return; |
1521 | } |
1522 | |
1523 | DllExport int |
1524 | win32_fstat(int fd, struct stat *sbufptr) |
1525 | { |
1526 | return xcefstat(fd, sbufptr); |
1527 | } |
1528 | |
1529 | DllExport int |
1530 | win32_link(const char *oldname, const char *newname) |
1531 | { |
216db7ee |
1532 | dTHX; |
e1caacb4 |
1533 | Perl_croak(aTHX_ PL_no_func, "link"); |
1534 | |
1535 | return -1; |
1536 | } |
1537 | |
1538 | DllExport int |
1539 | win32_rename(const char *oname, const char *newname) |
1540 | { |
1541 | return xcerename(oname, newname); |
1542 | } |
1543 | |
1544 | DllExport int |
1545 | win32_setmode(int fd, int mode) |
1546 | { |
bcdf844e |
1547 | /* currently 'celib' seem to have this function in src, but not |
1548 | * exported. When it will be, we'll uncomment following line. |
1549 | */ |
1550 | /* return xcesetmode(fd, mode); */ |
1551 | return 0; |
e1caacb4 |
1552 | } |
1553 | |
216db7ee |
1554 | DllExport int |
1555 | win32_chsize(int fd, Off_t size) |
1556 | { |
1557 | return chsize(fd, size); |
1558 | } |
1559 | |
e1caacb4 |
1560 | DllExport long |
814ffeea |
1561 | win32_lseek(int fd, Off_t offset, int origin) |
e1caacb4 |
1562 | { |
1563 | return xcelseek(fd, offset, origin); |
1564 | } |
1565 | |
1566 | DllExport long |
1567 | win32_tell(int fd) |
1568 | { |
1569 | return xcelseek(fd, 0, SEEK_CUR); |
1570 | } |
1571 | |
1572 | DllExport int |
1573 | win32_open(const char *path, int flag, ...) |
1574 | { |
1575 | int pmode; |
1576 | va_list ap; |
1577 | |
1578 | va_start(ap, flag); |
1579 | pmode = va_arg(ap, int); |
1580 | va_end(ap); |
1581 | |
1582 | return xceopen(path, flag, pmode); |
1583 | } |
1584 | |
1585 | DllExport int |
1586 | win32_close(int fd) |
1587 | { |
1588 | return xceclose(fd); |
1589 | } |
1590 | |
1591 | DllExport int |
1592 | win32_eof(int fd) |
1593 | { |
216db7ee |
1594 | dTHX; |
e1caacb4 |
1595 | Perl_croak(aTHX_ PL_no_func, "eof"); |
1596 | return -1; |
1597 | } |
1598 | |
1599 | DllExport int |
1600 | win32_dup(int fd) |
1601 | { |
216db7ee |
1602 | return xcedup(fd); /* from celib/ceio.c; requires some more work on it */ |
e1caacb4 |
1603 | } |
1604 | |
1605 | DllExport int |
1606 | win32_dup2(int fd1,int fd2) |
1607 | { |
aebd5ec7 |
1608 | return xcedup2(fd1,fd2); |
e1caacb4 |
1609 | } |
1610 | |
1611 | DllExport int |
1612 | win32_read(int fd, void *buf, unsigned int cnt) |
1613 | { |
1614 | return xceread(fd, buf, cnt); |
1615 | } |
1616 | |
1617 | DllExport int |
1618 | win32_write(int fd, const void *buf, unsigned int cnt) |
1619 | { |
1620 | return xcewrite(fd, (void *) buf, cnt); |
1621 | } |
1622 | |
1623 | DllExport int |
1624 | win32_mkdir(const char *dir, int mode) |
1625 | { |
1626 | return xcemkdir(dir); |
1627 | } |
1628 | |
1629 | DllExport int |
1630 | win32_rmdir(const char *dir) |
1631 | { |
1632 | return xcermdir(dir); |
1633 | } |
1634 | |
1635 | DllExport int |
1636 | win32_chdir(const char *dir) |
1637 | { |
1638 | return xcechdir(dir); |
1639 | } |
1640 | |
1641 | DllExport int |
1642 | win32_access(const char *path, int mode) |
1643 | { |
1644 | return xceaccess(path, mode); |
1645 | } |
1646 | |
1647 | DllExport int |
1648 | win32_chmod(const char *path, int mode) |
1649 | { |
1650 | return xcechmod(path, mode); |
1651 | } |
1652 | |
814ffeea |
1653 | static char * |
1654 | create_command_line(char *cname, STRLEN clen, const char * const *args) |
216db7ee |
1655 | { |
1656 | dTHX; |
1657 | int index, argc; |
1658 | char *cmd, *ptr; |
1659 | const char *arg; |
1660 | STRLEN len = 0; |
1661 | bool bat_file = FALSE; |
1662 | bool cmd_shell = FALSE; |
1663 | bool dumb_shell = FALSE; |
1664 | bool extra_quotes = FALSE; |
1665 | bool quote_next = FALSE; |
1666 | |
1667 | if (!cname) |
1668 | cname = (char*)args[0]; |
1669 | |
1670 | /* The NT cmd.exe shell has the following peculiarity that needs to be |
1671 | * worked around. It strips a leading and trailing dquote when any |
1672 | * of the following is true: |
1673 | * 1. the /S switch was used |
1674 | * 2. there are more than two dquotes |
1675 | * 3. there is a special character from this set: &<>()@^| |
1676 | * 4. no whitespace characters within the two dquotes |
1677 | * 5. string between two dquotes isn't an executable file |
1678 | * To work around this, we always add a leading and trailing dquote |
1679 | * to the string, if the first argument is either "cmd.exe" or "cmd", |
1680 | * and there were at least two or more arguments passed to cmd.exe |
1681 | * (not including switches). |
1682 | * XXX the above rules (from "cmd /?") don't seem to be applied |
1683 | * always, making for the convolutions below :-( |
1684 | */ |
1685 | if (cname) { |
1686 | if (!clen) |
1687 | clen = strlen(cname); |
1688 | |
1689 | if (clen > 4 |
1690 | && (stricmp(&cname[clen-4], ".bat") == 0 |
1691 | || (IsWinNT() && stricmp(&cname[clen-4], ".cmd") == 0))) |
1692 | { |
1693 | bat_file = TRUE; |
1694 | len += 3; |
1695 | } |
1696 | else { |
1697 | char *exe = strrchr(cname, '/'); |
1698 | char *exe2 = strrchr(cname, '\\'); |
1699 | if (exe2 > exe) |
1700 | exe = exe2; |
1701 | if (exe) |
1702 | ++exe; |
1703 | else |
1704 | exe = cname; |
1705 | if (stricmp(exe, "cmd.exe") == 0 || stricmp(exe, "cmd") == 0) { |
1706 | cmd_shell = TRUE; |
1707 | len += 3; |
1708 | } |
1709 | else if (stricmp(exe, "command.com") == 0 |
1710 | || stricmp(exe, "command") == 0) |
1711 | { |
1712 | dumb_shell = TRUE; |
1713 | } |
1714 | } |
1715 | } |
1716 | |
1717 | DEBUG_p(PerlIO_printf(Perl_debug_log, "Args ")); |
1718 | for (index = 0; (arg = (char*)args[index]) != NULL; ++index) { |
1719 | STRLEN curlen = strlen(arg); |
1720 | if (!(arg[0] == '"' && arg[curlen-1] == '"')) |
1721 | len += 2; /* assume quoting needed (worst case) */ |
1722 | len += curlen + 1; |
1723 | DEBUG_p(PerlIO_printf(Perl_debug_log, "[%s]",arg)); |
1724 | } |
1725 | DEBUG_p(PerlIO_printf(Perl_debug_log, "\n")); |
1726 | |
1727 | argc = index; |
a02a5408 |
1728 | Newx(cmd, len, char); |
216db7ee |
1729 | ptr = cmd; |
1730 | |
1731 | if (bat_file) { |
1732 | *ptr++ = '"'; |
1733 | extra_quotes = TRUE; |
1734 | } |
1735 | |
1736 | for (index = 0; (arg = (char*)args[index]) != NULL; ++index) { |
1737 | bool do_quote = 0; |
1738 | STRLEN curlen = strlen(arg); |
1739 | |
1740 | /* we want to protect empty arguments and ones with spaces with |
1741 | * dquotes, but only if they aren't already there */ |
1742 | if (!dumb_shell) { |
1743 | if (!curlen) { |
1744 | do_quote = 1; |
1745 | } |
1746 | else if (quote_next) { |
1747 | /* see if it really is multiple arguments pretending to |
1748 | * be one and force a set of quotes around it */ |
1749 | if (*find_next_space(arg)) |
1750 | do_quote = 1; |
1751 | } |
1752 | else if (!(arg[0] == '"' && curlen > 1 && arg[curlen-1] == '"')) { |
1753 | STRLEN i = 0; |
1754 | while (i < curlen) { |
1755 | if (isSPACE(arg[i])) { |
1756 | do_quote = 1; |
1757 | } |
1758 | else if (arg[i] == '"') { |
1759 | do_quote = 0; |
1760 | break; |
1761 | } |
1762 | i++; |
1763 | } |
1764 | } |
1765 | } |
1766 | |
1767 | if (do_quote) |
1768 | *ptr++ = '"'; |
1769 | |
1770 | strcpy(ptr, arg); |
1771 | ptr += curlen; |
1772 | |
1773 | if (do_quote) |
1774 | *ptr++ = '"'; |
1775 | |
1776 | if (args[index+1]) |
1777 | *ptr++ = ' '; |
1778 | |
1779 | if (!extra_quotes |
1780 | && cmd_shell |
1781 | && curlen >= 2 |
1782 | && *arg == '/' /* see if arg is "/c", "/x/c", "/x/d/c" etc. */ |
1783 | && stricmp(arg+curlen-2, "/c") == 0) |
1784 | { |
1785 | /* is there a next argument? */ |
1786 | if (args[index+1]) { |
1787 | /* are there two or more next arguments? */ |
1788 | if (args[index+2]) { |
1789 | *ptr++ = '"'; |
1790 | extra_quotes = TRUE; |
1791 | } |
1792 | else { |
1793 | /* single argument, force quoting if it has spaces */ |
1794 | quote_next = TRUE; |
1795 | } |
1796 | } |
1797 | } |
1798 | } |
1799 | |
1800 | if (extra_quotes) |
1801 | *ptr++ = '"'; |
1802 | |
1803 | *ptr = '\0'; |
1804 | |
1805 | return cmd; |
1806 | } |
1807 | |
1808 | static char * |
1809 | qualified_path(const char *cmd) |
1810 | { |
1811 | dTHX; |
1812 | char *pathstr; |
1813 | char *fullcmd, *curfullcmd; |
1814 | STRLEN cmdlen = 0; |
1815 | int has_slash = 0; |
1816 | |
1817 | if (!cmd) |
1818 | return Nullch; |
1819 | fullcmd = (char*)cmd; |
1820 | while (*fullcmd) { |
1821 | if (*fullcmd == '/' || *fullcmd == '\\') |
1822 | has_slash++; |
1823 | fullcmd++; |
1824 | cmdlen++; |
1825 | } |
1826 | |
1827 | /* look in PATH */ |
1828 | pathstr = PerlEnv_getenv("PATH"); |
a02a5408 |
1829 | Newx(fullcmd, MAX_PATH+1, char); |
216db7ee |
1830 | curfullcmd = fullcmd; |
1831 | |
1832 | while (1) { |
1833 | DWORD res; |
1834 | |
1835 | /* start by appending the name to the current prefix */ |
1836 | strcpy(curfullcmd, cmd); |
1837 | curfullcmd += cmdlen; |
1838 | |
1839 | /* if it doesn't end with '.', or has no extension, try adding |
1840 | * a trailing .exe first */ |
1841 | if (cmd[cmdlen-1] != '.' |
1842 | && (cmdlen < 4 || cmd[cmdlen-4] != '.')) |
1843 | { |
1844 | strcpy(curfullcmd, ".exe"); |
1845 | res = GetFileAttributes(fullcmd); |
1846 | if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY)) |
1847 | return fullcmd; |
1848 | *curfullcmd = '\0'; |
1849 | } |
1850 | |
1851 | /* that failed, try the bare name */ |
1852 | res = GetFileAttributes(fullcmd); |
1853 | if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY)) |
1854 | return fullcmd; |
1855 | |
1856 | /* quit if no other path exists, or if cmd already has path */ |
1857 | if (!pathstr || !*pathstr || has_slash) |
1858 | break; |
1859 | |
1860 | /* skip leading semis */ |
1861 | while (*pathstr == ';') |
1862 | pathstr++; |
1863 | |
1864 | /* build a new prefix from scratch */ |
1865 | curfullcmd = fullcmd; |
1866 | while (*pathstr && *pathstr != ';') { |
1867 | if (*pathstr == '"') { /* foo;"baz;etc";bar */ |
1868 | pathstr++; /* skip initial '"' */ |
1869 | while (*pathstr && *pathstr != '"') { |
1870 | if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5) |
1871 | *curfullcmd++ = *pathstr; |
1872 | pathstr++; |
1873 | } |
1874 | if (*pathstr) |
1875 | pathstr++; /* skip trailing '"' */ |
1876 | } |
1877 | else { |
1878 | if ((STRLEN)(curfullcmd-fullcmd) < MAX_PATH-cmdlen-5) |
1879 | *curfullcmd++ = *pathstr; |
1880 | pathstr++; |
1881 | } |
1882 | } |
1883 | if (*pathstr) |
1884 | pathstr++; /* skip trailing semi */ |
1885 | if (curfullcmd > fullcmd /* append a dir separator */ |
1886 | && curfullcmd[-1] != '/' && curfullcmd[-1] != '\\') |
1887 | { |
1888 | *curfullcmd++ = '\\'; |
1889 | } |
1890 | } |
1891 | |
1892 | Safefree(fullcmd); |
1893 | return Nullch; |
1894 | } |
1895 | |
8f33b42a |
1896 | /* The following are just place holders. |
1897 | * Some hosts may provide and environment that the OS is |
1898 | * not tracking, therefore, these host must provide that |
1899 | * environment and the current directory to CreateProcess |
1900 | */ |
1901 | |
1902 | DllExport void* |
1903 | win32_get_childenv(void) |
1904 | { |
1905 | return NULL; |
1906 | } |
1907 | |
1908 | DllExport void |
1909 | win32_free_childenv(void* d) |
1910 | { |
1911 | } |
1912 | |
1913 | DllExport void |
1914 | win32_clearenv(void) |
1915 | { |
1916 | char *envv = GetEnvironmentStrings(); |
1917 | char *cur = envv; |
1918 | STRLEN len; |
1919 | while (*cur) { |
1920 | char *end = strchr(cur,'='); |
1921 | if (end && end != cur) { |
1922 | *end = '\0'; |
1923 | xcesetenv(cur, "", 0); |
1924 | *end = '='; |
1925 | cur = end + strlen(end+1)+2; |
1926 | } |
1927 | else if ((len = strlen(cur))) |
1928 | cur += len+1; |
1929 | } |
1930 | FreeEnvironmentStrings(envv); |
1931 | } |
1932 | |
1933 | DllExport char* |
1934 | win32_get_childdir(void) |
1935 | { |
1936 | dTHX; |
1937 | char* ptr; |
8c56068e |
1938 | char szfilename[MAX_PATH+1]; |
1939 | GetCurrentDirectoryA(MAX_PATH+1, szfilename); |
8f33b42a |
1940 | |
a02a5408 |
1941 | Newx(ptr, strlen(szfilename)+1, char); |
8f33b42a |
1942 | strcpy(ptr, szfilename); |
1943 | return ptr; |
1944 | } |
1945 | |
1946 | DllExport void |
1947 | win32_free_childdir(char* d) |
1948 | { |
1949 | dTHX; |
1950 | Safefree(d); |
1951 | } |
1952 | |
216db7ee |
1953 | /* XXX this needs to be made more compatible with the spawnvp() |
1954 | * provided by the various RTLs. In particular, searching for |
1955 | * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented. |
1956 | * This doesn't significantly affect perl itself, because we |
1957 | * always invoke things using PERL5SHELL if a direct attempt to |
1958 | * spawn the executable fails. |
1959 | * |
1960 | * XXX splitting and rejoining the commandline between do_aspawn() |
1961 | * and win32_spawnvp() could also be avoided. |
1962 | */ |
1963 | |
216db7ee |
1964 | DllExport int |
1965 | win32_spawnvp(int mode, const char *cmdname, const char *const *argv) |
1966 | { |
1967 | #ifdef USE_RTL_SPAWNVP |
1968 | return spawnvp(mode, cmdname, (char * const *)argv); |
1969 | #else |
1970 | dTHX; |
1971 | int ret; |
1972 | void* env; |
1973 | char* dir; |
1974 | child_IO_table tbl; |
1975 | STARTUPINFO StartupInfo; |
1976 | PROCESS_INFORMATION ProcessInformation; |
1977 | DWORD create = 0; |
1978 | char *cmd; |
1979 | char *fullcmd = Nullch; |
1980 | char *cname = (char *)cmdname; |
1981 | STRLEN clen = 0; |
1982 | |
1983 | if (cname) { |
1984 | clen = strlen(cname); |
1985 | /* if command name contains dquotes, must remove them */ |
1986 | if (strchr(cname, '"')) { |
1987 | cmd = cname; |
a02a5408 |
1988 | Newx(cname,clen+1,char); |
216db7ee |
1989 | clen = 0; |
1990 | while (*cmd) { |
1991 | if (*cmd != '"') { |
1992 | cname[clen] = *cmd; |
1993 | ++clen; |
1994 | } |
1995 | ++cmd; |
1996 | } |
1997 | cname[clen] = '\0'; |
1998 | } |
1999 | } |
2000 | |
2001 | cmd = create_command_line(cname, clen, argv); |
2002 | |
2003 | env = PerlEnv_get_childenv(); |
2004 | dir = PerlEnv_get_childdir(); |
2005 | |
2006 | switch(mode) { |
2007 | case P_NOWAIT: /* asynch + remember result */ |
2008 | if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) { |
2009 | errno = EAGAIN; |
2010 | ret = -1; |
2011 | goto RETVAL; |
2012 | } |
2013 | /* Create a new process group so we can use GenerateConsoleCtrlEvent() |
2014 | * in win32_kill() |
2015 | */ |
2016 | /* not supported on CE create |= CREATE_NEW_PROCESS_GROUP; */ |
2017 | /* FALL THROUGH */ |
2018 | |
2019 | case P_WAIT: /* synchronous execution */ |
2020 | break; |
2021 | default: /* invalid mode */ |
2022 | errno = EINVAL; |
2023 | ret = -1; |
2024 | goto RETVAL; |
2025 | } |
2026 | memset(&StartupInfo,0,sizeof(StartupInfo)); |
2027 | StartupInfo.cb = sizeof(StartupInfo); |
2028 | memset(&tbl,0,sizeof(tbl)); |
2029 | PerlEnv_get_child_IO(&tbl); |
2030 | StartupInfo.dwFlags = tbl.dwFlags; |
2031 | StartupInfo.dwX = tbl.dwX; |
2032 | StartupInfo.dwY = tbl.dwY; |
2033 | StartupInfo.dwXSize = tbl.dwXSize; |
2034 | StartupInfo.dwYSize = tbl.dwYSize; |
2035 | StartupInfo.dwXCountChars = tbl.dwXCountChars; |
2036 | StartupInfo.dwYCountChars = tbl.dwYCountChars; |
2037 | StartupInfo.dwFillAttribute = tbl.dwFillAttribute; |
2038 | StartupInfo.wShowWindow = tbl.wShowWindow; |
2039 | StartupInfo.hStdInput = tbl.childStdIn; |
2040 | StartupInfo.hStdOutput = tbl.childStdOut; |
2041 | StartupInfo.hStdError = tbl.childStdErr; |
2042 | if (StartupInfo.hStdInput == INVALID_HANDLE_VALUE && |
2043 | StartupInfo.hStdOutput == INVALID_HANDLE_VALUE && |
2044 | StartupInfo.hStdError == INVALID_HANDLE_VALUE) |
2045 | { |
2046 | create |= CREATE_NEW_CONSOLE; |
2047 | } |
2048 | else { |
2049 | StartupInfo.dwFlags |= STARTF_USESTDHANDLES; |
2050 | } |
2051 | if (w32_use_showwindow) { |
2052 | StartupInfo.dwFlags |= STARTF_USESHOWWINDOW; |
2053 | StartupInfo.wShowWindow = w32_showwindow; |
2054 | } |
2055 | |
2056 | DEBUG_p(PerlIO_printf(Perl_debug_log, "Spawning [%s] with [%s]\n", |
2057 | cname,cmd)); |
2058 | RETRY: |
2059 | if (!CreateProcess(cname, /* search PATH to find executable */ |
2060 | cmd, /* executable, and its arguments */ |
2061 | NULL, /* process attributes */ |
2062 | NULL, /* thread attributes */ |
2063 | TRUE, /* inherit handles */ |
2064 | create, /* creation flags */ |
2065 | (LPVOID)env, /* inherit environment */ |
2066 | dir, /* inherit cwd */ |
2067 | &StartupInfo, |
2068 | &ProcessInformation)) |
2069 | { |
2070 | /* initial NULL argument to CreateProcess() does a PATH |
2071 | * search, but it always first looks in the directory |
2072 | * where the current process was started, which behavior |
2073 | * is undesirable for backward compatibility. So we |
2074 | * jump through our own hoops by picking out the path |
2075 | * we really want it to use. */ |
2076 | if (!fullcmd) { |
2077 | fullcmd = qualified_path(cname); |
2078 | if (fullcmd) { |
2079 | if (cname != cmdname) |
2080 | Safefree(cname); |
2081 | cname = fullcmd; |
2082 | DEBUG_p(PerlIO_printf(Perl_debug_log, |
2083 | "Retrying [%s] with same args\n", |
2084 | cname)); |
2085 | goto RETRY; |
2086 | } |
2087 | } |
2088 | errno = ENOENT; |
2089 | ret = -1; |
2090 | goto RETVAL; |
2091 | } |
2092 | |
2093 | if (mode == P_NOWAIT) { |
2094 | /* asynchronous spawn -- store handle, return PID */ |
2095 | ret = (int)ProcessInformation.dwProcessId; |
2096 | if (IsWin95() && ret < 0) |
2097 | ret = -ret; |
2098 | |
2099 | w32_child_handles[w32_num_children] = ProcessInformation.hProcess; |
2100 | w32_child_pids[w32_num_children] = (DWORD)ret; |
2101 | ++w32_num_children; |
2102 | } |
2103 | else { |
2104 | DWORD status; |
2105 | win32_msgwait(aTHX_ 1, &ProcessInformation.hProcess, INFINITE, NULL); |
2106 | /* FIXME: if msgwait returned due to message perhaps forward the |
2107 | "signal" to the process |
2108 | */ |
2109 | GetExitCodeProcess(ProcessInformation.hProcess, &status); |
2110 | ret = (int)status; |
2111 | CloseHandle(ProcessInformation.hProcess); |
2112 | } |
2113 | |
2114 | CloseHandle(ProcessInformation.hThread); |
2115 | |
2116 | RETVAL: |
2117 | PerlEnv_free_childenv(env); |
2118 | PerlEnv_free_childdir(dir); |
2119 | Safefree(cmd); |
2120 | if (cname != cmdname) |
2121 | Safefree(cname); |
2122 | return ret; |
2123 | #endif |
2124 | } |
2125 | |
e1caacb4 |
2126 | DllExport int |
2127 | win32_execv(const char *cmdname, const char *const *argv) |
2128 | { |
216db7ee |
2129 | dTHX; |
e1caacb4 |
2130 | Perl_croak(aTHX_ PL_no_func, "execv"); |
2131 | return -1; |
2132 | } |
2133 | |
2134 | DllExport int |
2135 | win32_execvp(const char *cmdname, const char *const *argv) |
2136 | { |
216db7ee |
2137 | dTHX; |
e1caacb4 |
2138 | Perl_croak(aTHX_ PL_no_func, "execvp"); |
2139 | return -1; |
2140 | } |
2141 | |
814ffeea |
2142 | DllExport void |
2143 | win32_perror(const char *str) |
e1caacb4 |
2144 | { |
814ffeea |
2145 | xceperror(str); |
2146 | } |
e1caacb4 |
2147 | |
814ffeea |
2148 | DllExport void |
2149 | win32_setbuf(FILE *pf, char *buf) |
2150 | { |
2151 | dTHX; |
2152 | Perl_croak(aTHX_ PL_no_func, "setbuf"); |
2153 | } |
e1caacb4 |
2154 | |
814ffeea |
2155 | DllExport int |
2156 | win32_setvbuf(FILE *pf, char *buf, int type, size_t size) |
2157 | { |
2158 | return setvbuf(pf, buf, type, size); |
e1caacb4 |
2159 | } |
2160 | |
814ffeea |
2161 | DllExport int |
2162 | win32_flushall(void) |
2163 | { |
2164 | return flushall(); |
2165 | } |
e1caacb4 |
2166 | |
814ffeea |
2167 | DllExport int |
2168 | win32_fcloseall(void) |
e1caacb4 |
2169 | { |
814ffeea |
2170 | return fcloseall(); |
2171 | } |
e1caacb4 |
2172 | |
814ffeea |
2173 | DllExport char* |
2174 | win32_fgets(char *s, int n, FILE *pf) |
2175 | { |
2176 | return fgets(s, n, pf); |
2177 | } |
e1caacb4 |
2178 | |
814ffeea |
2179 | DllExport char* |
2180 | win32_gets(char *s) |
2181 | { |
2182 | return gets(s); |
e1caacb4 |
2183 | } |
2184 | |
814ffeea |
2185 | DllExport int |
2186 | win32_fgetc(FILE *pf) |
e1caacb4 |
2187 | { |
814ffeea |
2188 | return fgetc(pf); |
2189 | } |
e1caacb4 |
2190 | |
814ffeea |
2191 | DllExport int |
2192 | win32_putc(int c, FILE *pf) |
2193 | { |
2194 | return putc(c,pf); |
2195 | } |
2196 | |
2197 | DllExport int |
2198 | win32_puts(const char *s) |
2199 | { |
2200 | return puts(s); |
2201 | } |
2202 | |
2203 | DllExport int |
2204 | win32_getchar(void) |
2205 | { |
2206 | return getchar(); |
2207 | } |
2208 | |
2209 | DllExport int |
2210 | win32_putchar(int c) |
2211 | { |
2212 | return putchar(c); |
2213 | } |
2214 | |
2215 | #ifdef MYMALLOC |
2216 | |
2217 | #ifndef USE_PERL_SBRK |
2218 | |
2219 | static char *committed = NULL; |
2220 | static char *base = NULL; |
2221 | static char *reserved = NULL; |
2222 | static char *brk = NULL; |
2223 | static DWORD pagesize = 0; |
2224 | static DWORD allocsize = 0; |
2225 | |
2226 | void * |
2227 | sbrk(int need) |
2228 | { |
2229 | void *result; |
2230 | if (!pagesize) |
2231 | {SYSTEM_INFO info; |
2232 | GetSystemInfo(&info); |
2233 | /* Pretend page size is larger so we don't perpetually |
2234 | * call the OS to commit just one page ... |
2235 | */ |
2236 | pagesize = info.dwPageSize << 3; |
2237 | allocsize = info.dwAllocationGranularity; |
2238 | } |
2239 | /* This scheme fails eventually if request for contiguous |
2240 | * block is denied so reserve big blocks - this is only |
2241 | * address space not memory ... |
2242 | */ |
2243 | if (brk+need >= reserved) |
2244 | { |
2245 | DWORD size = 64*1024*1024; |
2246 | char *addr; |
2247 | if (committed && reserved && committed < reserved) |
2248 | { |
2249 | /* Commit last of previous chunk cannot span allocations */ |
2250 | addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE); |
2251 | if (addr) |
2252 | committed = reserved; |
2253 | } |
2254 | /* Reserve some (more) space |
2255 | * Note this is a little sneaky, 1st call passes NULL as reserved |
2256 | * so lets system choose where we start, subsequent calls pass |
2257 | * the old end address so ask for a contiguous block |
2258 | */ |
2259 | addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS); |
2260 | if (addr) |
2261 | { |
2262 | reserved = addr+size; |
2263 | if (!base) |
2264 | base = addr; |
2265 | if (!committed) |
2266 | committed = base; |
2267 | if (!brk) |
2268 | brk = committed; |
2269 | } |
2270 | else |
2271 | { |
2272 | return (void *) -1; |
2273 | } |
2274 | } |
2275 | result = brk; |
2276 | brk += need; |
2277 | if (brk > committed) |
2278 | { |
2279 | DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize; |
2280 | char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE); |
2281 | if (addr) |
2282 | { |
2283 | committed += size; |
2284 | } |
2285 | else |
2286 | return (void *) -1; |
2287 | } |
2288 | return result; |
2289 | } |
2290 | |
2291 | #endif |
2292 | #endif |
2293 | |
2294 | DllExport void* |
2295 | win32_malloc(size_t size) |
2296 | { |
2297 | return malloc(size); |
2298 | } |
2299 | |
2300 | DllExport void* |
2301 | win32_calloc(size_t numitems, size_t size) |
2302 | { |
2303 | return calloc(numitems,size); |
2304 | } |
2305 | |
2306 | DllExport void* |
2307 | win32_realloc(void *block, size_t size) |
2308 | { |
2309 | return realloc(block,size); |
2310 | } |
2311 | |
2312 | DllExport void |
2313 | win32_free(void *block) |
2314 | { |
2315 | free(block); |
2316 | } |
2317 | |
2318 | int |
2319 | win32_open_osfhandle(intptr_t osfhandle, int flags) |
2320 | { |
2321 | int fh; |
2322 | char fileflags=0; /* _osfile flags */ |
2323 | |
2324 | Perl_croak_nocontext("win32_open_osfhandle() TBD on this platform"); |
2325 | return 0; |
2326 | } |
2327 | |
2328 | int |
2329 | win32_get_osfhandle(int fd) |
2330 | { |
2331 | int fh; |
2332 | char fileflags=0; /* _osfile flags */ |
2333 | |
2334 | Perl_croak_nocontext("win32_get_osfhandle() TBD on this platform"); |
2335 | return 0; |
2336 | } |
2337 | |
2338 | FILE * |
2339 | win32_fdupopen(FILE *pf) |
2340 | { |
2341 | FILE* pfdup; |
2342 | fpos_t pos; |
2343 | char mode[3]; |
2344 | int fileno = win32_dup(win32_fileno(pf)); |
2345 | int fmode = palm_fgetmode(pfdup); |
2346 | |
2347 | fprintf(stderr,"DEBUG for win32_fdupopen()\n"); |
2348 | |
2349 | /* open the file in the same mode */ |
2350 | if(fmode & O_RDONLY) { |
2351 | mode[0] = 'r'; |
2352 | mode[1] = 0; |
2353 | } |
2354 | else if(fmode & O_APPEND) { |
2355 | mode[0] = 'a'; |
2356 | mode[1] = 0; |
2357 | } |
2358 | else if(fmode & O_RDWR) { |
2359 | mode[0] = 'r'; |
2360 | mode[1] = '+'; |
2361 | mode[2] = 0; |
2362 | } |
2363 | |
2364 | /* it appears that the binmode is attached to the |
2365 | * file descriptor so binmode files will be handled |
2366 | * correctly |
2367 | */ |
2368 | pfdup = win32_fdopen(fileno, mode); |
2369 | |
2370 | /* move the file pointer to the same position */ |
2371 | if (!fgetpos(pf, &pos)) { |
2372 | fsetpos(pfdup, &pos); |
2373 | } |
2374 | return pfdup; |
2375 | } |
2376 | |
2377 | DllExport void* |
2378 | win32_dynaload(const char* filename) |
2379 | { |
2380 | dTHX; |
2381 | HMODULE hModule; |
2382 | |
2383 | hModule = XCELoadLibraryA(filename); |
2384 | |
2385 | return hModule; |
2386 | } |
2387 | |
2388 | /* this is needed by Cwd.pm... */ |
2389 | |
2390 | static |
2391 | XS(w32_GetCwd) |
2392 | { |
2393 | dXSARGS; |
2394 | char buf[MAX_PATH]; |
2395 | SV *sv = sv_newmortal(); |
2396 | |
2397 | xcegetcwd(buf, sizeof(buf)); |
2398 | |
2399 | sv_setpv(sv, xcestrdup(buf)); |
2400 | EXTEND(SP,1); |
2401 | SvPOK_on(sv); |
2402 | ST(0) = sv; |
2403 | #ifndef INCOMPLETE_TAINTS |
2404 | SvTAINTED_on(ST(0)); |
2405 | #endif |
2406 | XSRETURN(1); |
2407 | } |
2408 | |
2409 | static |
2410 | XS(w32_SetCwd) |
2411 | { |
2412 | dXSARGS; |
2413 | |
2414 | if (items != 1) |
e1caacb4 |
2415 | Perl_croak(aTHX_ "usage: Win32::SetCwd($cwd)"); |
2416 | |
2417 | if (!xcechdir(SvPV_nolen(ST(0)))) |
2418 | XSRETURN_YES; |
2419 | |
2420 | XSRETURN_NO; |
2421 | } |
2422 | |
2423 | static |
2424 | XS(w32_GetTickCount) |
2425 | { |
2426 | dXSARGS; |
2427 | DWORD msec = GetTickCount(); |
2428 | EXTEND(SP,1); |
2429 | if ((IV)msec > 0) |
2430 | XSRETURN_IV(msec); |
2431 | XSRETURN_NV(msec); |
2432 | } |
2433 | |
2434 | static |
2435 | XS(w32_GetOSVersion) |
2436 | { |
2437 | dXSARGS; |
2438 | OSVERSIONINFOA osver; |
2439 | |
2440 | osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA); |
2441 | if (!XCEGetVersionExA(&osver)) { |
2442 | XSRETURN_EMPTY; |
2443 | } |
2444 | XPUSHs(newSVpvn(osver.szCSDVersion, strlen(osver.szCSDVersion))); |
2445 | XPUSHs(newSViv(osver.dwMajorVersion)); |
2446 | XPUSHs(newSViv(osver.dwMinorVersion)); |
2447 | XPUSHs(newSViv(osver.dwBuildNumber)); |
cb69f87a |
2448 | /* WINCE = 3 */ |
e1caacb4 |
2449 | XPUSHs(newSViv(osver.dwPlatformId)); |
2450 | PUTBACK; |
2451 | } |
2452 | |
2453 | static |
2454 | XS(w32_IsWinNT) |
2455 | { |
2456 | dXSARGS; |
2457 | EXTEND(SP,1); |
2458 | XSRETURN_IV(IsWinNT()); |
2459 | } |
2460 | |
2461 | static |
2462 | XS(w32_IsWin95) |
2463 | { |
2464 | dXSARGS; |
2465 | EXTEND(SP,1); |
2466 | XSRETURN_IV(IsWin95()); |
2467 | } |
2468 | |
2469 | static |
2470 | XS(w32_IsWinCE) |
2471 | { |
2472 | dXSARGS; |
2473 | EXTEND(SP,1); |
2474 | XSRETURN_IV(IsWinCE()); |
2475 | } |
2476 | |
2477 | static |
2478 | XS(w32_GetOemInfo) |
2479 | { |
2480 | dXSARGS; |
2481 | wchar_t wbuf[126]; |
2482 | char buf[126]; |
2483 | |
2484 | if(SystemParametersInfoW(SPI_GETOEMINFO, sizeof(wbuf), wbuf, FALSE)) |
2485 | WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buf, sizeof(buf), 0, 0); |
2486 | else |
2487 | sprintf(buf, "SystemParametersInfo failed: %d", GetLastError()); |
2488 | |
2489 | EXTEND(SP,1); |
2490 | XSRETURN_PV(buf); |
2491 | } |
2492 | |
2493 | static |
2494 | XS(w32_Sleep) |
2495 | { |
2496 | dXSARGS; |
2497 | if (items != 1) |
2498 | Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)"); |
2499 | Sleep(SvIV(ST(0))); |
2500 | XSRETURN_YES; |
2501 | } |
2502 | |
2503 | static |
2504 | XS(w32_CopyFile) |
2505 | { |
2506 | dXSARGS; |
2507 | BOOL bResult; |
2508 | if (items != 3) |
2509 | Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)"); |
2510 | |
2511 | { |
2512 | char szSourceFile[MAX_PATH+1]; |
2513 | strcpy(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(0)))); |
2514 | bResult = XCECopyFileA(szSourceFile, SvPV_nolen(ST(1)), |
2515 | !SvTRUE(ST(2))); |
2516 | } |
2517 | |
2518 | if (bResult) |
2519 | XSRETURN_YES; |
2520 | |
2521 | XSRETURN_NO; |
2522 | } |
2523 | |
2524 | static |
2525 | XS(w32_MessageBox) |
2526 | { |
2527 | dXSARGS; |
2528 | |
2529 | char *txt; |
2530 | unsigned int res; |
2531 | unsigned int flags = MB_OK; |
2532 | |
2533 | txt = SvPV_nolen(ST(0)); |
2534 | |
2535 | if (items < 1 || items > 2) |
2536 | Perl_croak(aTHX_ "usage: Win32::MessageBox($txt, [$flags])"); |
2537 | |
2538 | if(items == 2) |
2539 | flags = SvIV(ST(1)); |
2540 | |
2541 | res = XCEMessageBoxA(NULL, txt, "Perl", flags); |
2542 | |
2543 | XSRETURN_IV(res); |
2544 | } |
2545 | |
2546 | static |
2547 | XS(w32_GetPowerStatus) |
2548 | { |
2549 | dXSARGS; |
2550 | |
2551 | SYSTEM_POWER_STATUS_EX sps; |
2552 | |
2553 | if(GetSystemPowerStatusEx(&sps, TRUE) == FALSE) |
2554 | { |
2555 | XSRETURN_EMPTY; |
2556 | } |
2557 | |
2558 | XPUSHs(newSViv(sps.ACLineStatus)); |
2559 | XPUSHs(newSViv(sps.BatteryFlag)); |
2560 | XPUSHs(newSViv(sps.BatteryLifePercent)); |
2561 | XPUSHs(newSViv(sps.BatteryLifeTime)); |
2562 | XPUSHs(newSViv(sps.BatteryFullLifeTime)); |
2563 | XPUSHs(newSViv(sps.BackupBatteryFlag)); |
2564 | XPUSHs(newSViv(sps.BackupBatteryLifePercent)); |
2565 | XPUSHs(newSViv(sps.BackupBatteryLifeTime)); |
2566 | XPUSHs(newSViv(sps.BackupBatteryFullLifeTime)); |
2567 | |
2568 | PUTBACK; |
2569 | } |
2570 | |
2571 | #if UNDER_CE > 200 |
2572 | static |
2573 | XS(w32_ShellEx) |
2574 | { |
2575 | dXSARGS; |
2576 | |
2577 | char buf[126]; |
2578 | SHELLEXECUTEINFO si; |
2579 | char *file, *verb; |
2580 | wchar_t wfile[MAX_PATH]; |
2581 | wchar_t wverb[20]; |
2582 | |
2583 | if (items != 2) |
2584 | Perl_croak(aTHX_ "usage: Win32::ShellEx($file, $verb)"); |
2585 | |
2586 | file = SvPV_nolen(ST(0)); |
2587 | verb = SvPV_nolen(ST(1)); |
2588 | |
2589 | memset(&si, 0, sizeof(si)); |
2590 | si.cbSize = sizeof(si); |
2591 | si.fMask = SEE_MASK_FLAG_NO_UI; |
2592 | |
2593 | MultiByteToWideChar(CP_ACP, 0, verb, -1, |
2594 | wverb, sizeof(wverb)/2); |
2595 | si.lpVerb = (TCHAR *)wverb; |
2596 | |
2597 | MultiByteToWideChar(CP_ACP, 0, file, -1, |
2598 | wfile, sizeof(wfile)/2); |
2599 | si.lpFile = (TCHAR *)wfile; |
2600 | |
2601 | if(ShellExecuteEx(&si) == FALSE) |
2602 | { |
2603 | XSRETURN_NO; |
2604 | } |
2605 | XSRETURN_YES; |
2606 | } |
2607 | #endif |
2608 | |
2609 | void |
2610 | Perl_init_os_extras(void) |
2611 | { |
acfe0abc |
2612 | dTHX; |
e1caacb4 |
2613 | char *file = __FILE__; |
2614 | dXSUB_SYS; |
2615 | |
2616 | w32_perlshell_tokens = Nullch; |
2617 | w32_perlshell_items = -1; |
2618 | w32_fdpid = newAV(); /* XX needs to be in Perl_win32_init()? */ |
a02a5408 |
2619 | Newx(w32_children, 1, child_tab); |
e1caacb4 |
2620 | w32_num_children = 0; |
2621 | |
2622 | newXS("Win32::GetCwd", w32_GetCwd, file); |
2623 | newXS("Win32::SetCwd", w32_SetCwd, file); |
2624 | newXS("Win32::GetTickCount", w32_GetTickCount, file); |
2625 | newXS("Win32::GetOSVersion", w32_GetOSVersion, file); |
2626 | #if UNDER_CE > 200 |
2627 | newXS("Win32::ShellEx", w32_ShellEx, file); |
2628 | #endif |
2629 | newXS("Win32::IsWinNT", w32_IsWinNT, file); |
2630 | newXS("Win32::IsWin95", w32_IsWin95, file); |
2631 | newXS("Win32::IsWinCE", w32_IsWinCE, file); |
2632 | newXS("Win32::CopyFile", w32_CopyFile, file); |
2633 | newXS("Win32::Sleep", w32_Sleep, file); |
2634 | newXS("Win32::MessageBox", w32_MessageBox, file); |
2635 | newXS("Win32::GetPowerStatus", w32_GetPowerStatus, file); |
2636 | newXS("Win32::GetOemInfo", w32_GetOemInfo, file); |
2637 | } |
2638 | |
2639 | void |
2640 | myexit(void) |
2641 | { |
2642 | char buf[126]; |
2643 | |
2644 | puts("Hit return"); |
2645 | fgets(buf, sizeof(buf), stdin); |
2646 | } |
2647 | |
2648 | void |
2649 | Perl_win32_init(int *argcp, char ***argvp) |
2650 | { |
2651 | #ifdef UNDER_CE |
2652 | char *p; |
2653 | |
2654 | if((p = xcegetenv("PERLDEBUG")) && (p[0] == 'y' || p[0] == 'Y')) |
2655 | atexit(myexit); |
2656 | #endif |
2657 | |
2658 | MALLOC_INIT; |
2659 | } |
2660 | |
216db7ee |
2661 | DllExport void |
2662 | Perl_win32_term(void) |
2663 | { |
2664 | OP_REFCNT_TERM; |
2665 | MALLOC_TERM; |
2666 | } |
2667 | |
2668 | void |
2669 | win32_get_child_IO(child_IO_table* ptbl) |
2670 | { |
2671 | ptbl->childStdIn = GetStdHandle(STD_INPUT_HANDLE); |
2672 | ptbl->childStdOut = GetStdHandle(STD_OUTPUT_HANDLE); |
2673 | ptbl->childStdErr = GetStdHandle(STD_ERROR_HANDLE); |
2674 | } |
2675 | |
e1caacb4 |
2676 | win32_flock(int fd, int oper) |
2677 | { |
216db7ee |
2678 | dTHX; |
e1caacb4 |
2679 | Perl_croak(aTHX_ PL_no_func, "flock"); |
2680 | return -1; |
2681 | } |
2682 | |
2683 | DllExport int |
2684 | win32_waitpid(int pid, int *status, int flags) |
2685 | { |
216db7ee |
2686 | dTHX; |
e1caacb4 |
2687 | Perl_croak(aTHX_ PL_no_func, "waitpid"); |
2688 | return -1; |
2689 | } |
2690 | |
2691 | DllExport int |
2692 | win32_wait(int *status) |
2693 | { |
216db7ee |
2694 | dTHX; |
e1caacb4 |
2695 | Perl_croak(aTHX_ PL_no_func, "wait"); |
2696 | return -1; |
2697 | } |
2698 | |
2699 | int |
e1caacb4 |
2700 | wce_reopen_stdout(char *fname) |
2701 | { |
2702 | if(xcefreopen(fname, "w", stdout) == NULL) |
2703 | return -1; |
2704 | |
2705 | return 0; |
2706 | } |
2707 | |
2708 | void |
2709 | wce_hitreturn() |
2710 | { |
2711 | char buf[126]; |
2712 | |
2713 | printf("Hit RETURN"); |
2714 | fflush(stdout); |
2715 | fgets(buf, sizeof(buf), stdin); |
2716 | return; |
2717 | } |
2718 | |
cb69f87a |
2719 | /* //////////////////////////////////////////////////////////////////// */ |
e1caacb4 |
2720 | |
ca6c63e1 |
2721 | #undef getcwd |
2722 | |
2723 | char * |
2724 | getcwd(char *buf, size_t size) |
2725 | { |
2726 | return xcegetcwd(buf, size); |
2727 | } |
2728 | |
2729 | int |
2730 | isnan(double d) |
2731 | { |
2732 | return _isnan(d); |
2733 | } |
2734 | |
18f68570 |
2735 | |
814ffeea |
2736 | DllExport PerlIO* |
2737 | win32_popenlist(const char *mode, IV narg, SV **args) |
18f68570 |
2738 | { |
814ffeea |
2739 | dTHX; |
2740 | Perl_croak(aTHX_ "List form of pipe open not implemented"); |
2741 | return NULL; |
18f68570 |
2742 | } |
2743 | |
2744 | /* |
2745 | * a popen() clone that respects PERL5SHELL |
2746 | * |
2747 | * changed to return PerlIO* rather than FILE * by BKS, 11-11-2000 |
2748 | */ |
2749 | |
2750 | DllExport PerlIO* |
2751 | win32_popen(const char *command, const char *mode) |
2752 | { |
814ffeea |
2753 | #ifdef USE_RTL_POPEN |
2754 | return _popen(command, mode); |
2755 | #else |
2756 | dTHX; |
2757 | int p[2]; |
2758 | int parent, child; |
2759 | int stdfd, oldfd; |
2760 | int ourmode; |
2761 | int childpid; |
2762 | DWORD nhandle; |
2763 | HANDLE old_h; |
2764 | int lock_held = 0; |
2765 | |
2766 | /* establish which ends read and write */ |
2767 | if (strchr(mode,'w')) { |
2768 | stdfd = 0; /* stdin */ |
2769 | parent = 1; |
2770 | child = 0; |
2771 | nhandle = STD_INPUT_HANDLE; |
2772 | } |
2773 | else if (strchr(mode,'r')) { |
2774 | stdfd = 1; /* stdout */ |
2775 | parent = 0; |
2776 | child = 1; |
2777 | nhandle = STD_OUTPUT_HANDLE; |
2778 | } |
2779 | else |
2780 | return NULL; |
18f68570 |
2781 | |
814ffeea |
2782 | /* set the correct mode */ |
2783 | if (strchr(mode,'b')) |
2784 | ourmode = O_BINARY; |
2785 | else if (strchr(mode,'t')) |
2786 | ourmode = O_TEXT; |
2787 | else |
2788 | ourmode = _fmode & (O_TEXT | O_BINARY); |
2789 | |
2790 | /* the child doesn't inherit handles */ |
2791 | ourmode |= O_NOINHERIT; |
2792 | |
2793 | if (win32_pipe(p, 512, ourmode) == -1) |
2794 | return NULL; |
2795 | |
2796 | /* save current stdfd */ |
2797 | if ((oldfd = win32_dup(stdfd)) == -1) |
2798 | goto cleanup; |
2799 | |
2800 | /* save the old std handle (this needs to happen before the |
2801 | * dup2(), since that might call SetStdHandle() too) */ |
2802 | OP_REFCNT_LOCK; |
2803 | lock_held = 1; |
2804 | old_h = GetStdHandle(nhandle); |
2805 | |
2806 | /* make stdfd go to child end of pipe (implicitly closes stdfd) */ |
2807 | /* stdfd will be inherited by the child */ |
2808 | if (win32_dup2(p[child], stdfd) == -1) |
2809 | goto cleanup; |
2810 | |
2811 | /* close the child end in parent */ |
2812 | win32_close(p[child]); |
2813 | |
2814 | /* set the new std handle (in case dup2() above didn't) */ |
2815 | SetStdHandle(nhandle, (HANDLE)_get_osfhandle(stdfd)); |
2816 | |
2817 | /* start the child */ |
2818 | { |
2819 | dTHX; |
2820 | if ((childpid = do_spawn_nowait((char*)command)) == -1) |
2821 | goto cleanup; |
2822 | |
2823 | /* revert stdfd to whatever it was before */ |
2824 | if (win32_dup2(oldfd, stdfd) == -1) |
2825 | goto cleanup; |
2826 | |
2827 | /* restore the old std handle (this needs to happen after the |
2828 | * dup2(), since that might call SetStdHandle() too */ |
2829 | if (lock_held) { |
2830 | SetStdHandle(nhandle, old_h); |
2831 | OP_REFCNT_UNLOCK; |
2832 | lock_held = 0; |
2833 | } |
2834 | |
2835 | /* close saved handle */ |
2836 | win32_close(oldfd); |
2837 | |
2838 | LOCK_FDPID_MUTEX; |
2839 | sv_setiv(*av_fetch(w32_fdpid, p[parent], TRUE), childpid); |
2840 | UNLOCK_FDPID_MUTEX; |
2841 | |
2842 | /* set process id so that it can be returned by perl's open() */ |
2843 | PL_forkprocess = childpid; |
2844 | } |
2845 | |
2846 | /* we have an fd, return a file stream */ |
2847 | return (PerlIO_fdopen(p[parent], (char *)mode)); |
2848 | |
2849 | cleanup: |
2850 | /* we don't need to check for errors here */ |
2851 | win32_close(p[0]); |
2852 | win32_close(p[1]); |
2853 | if (lock_held) { |
2854 | SetStdHandle(nhandle, old_h); |
2855 | OP_REFCNT_UNLOCK; |
2856 | lock_held = 0; |
2857 | } |
2858 | if (oldfd != -1) { |
2859 | win32_dup2(oldfd, stdfd); |
2860 | win32_close(oldfd); |
2861 | } |
2862 | return (NULL); |
2863 | |
2864 | #endif /* USE_RTL_POPEN */ |
216db7ee |
2865 | } |
2866 | |
18f68570 |
2867 | /* |
2868 | * pclose() clone |
2869 | */ |
2870 | |
2871 | DllExport int |
2872 | win32_pclose(PerlIO *pf) |
2873 | { |
2874 | #ifdef USE_RTL_POPEN |
2875 | return _pclose(pf); |
2876 | #else |
2877 | dTHX; |
2878 | int childpid, status; |
2879 | SV *sv; |
2880 | |
2881 | LOCK_FDPID_MUTEX; |
2882 | sv = *av_fetch(w32_fdpid, PerlIO_fileno(pf), TRUE); |
2883 | |
2884 | if (SvIOK(sv)) |
2885 | childpid = SvIVX(sv); |
2886 | else |
2887 | childpid = 0; |
2888 | |
2889 | if (!childpid) { |
2890 | errno = EBADF; |
2891 | return -1; |
2892 | } |
2893 | |
2894 | #ifdef USE_PERLIO |
2895 | PerlIO_close(pf); |
2896 | #else |
2897 | fclose(pf); |
2898 | #endif |
2899 | SvIVX(sv) = 0; |
2900 | UNLOCK_FDPID_MUTEX; |
2901 | |
2902 | if (win32_waitpid(childpid, &status, 0) == -1) |
2903 | return -1; |
2904 | |
2905 | return status; |
2906 | |
2907 | #endif /* USE_RTL_POPEN */ |
2908 | } |
2909 | |
216db7ee |
2910 | #ifdef HAVE_INTERP_INTERN |
2911 | |
2912 | |
2913 | static void |
2914 | win32_csighandler(int sig) |
2915 | { |
2916 | #if 0 |
2917 | dTHXa(PERL_GET_SIG_CONTEXT); |
2918 | Perl_warn(aTHX_ "Got signal %d",sig); |
2919 | #endif |
2920 | /* Does nothing */ |
2921 | } |
2922 | |
2923 | void |
2924 | Perl_sys_intern_init(pTHX) |
2925 | { |
2926 | int i; |
2927 | w32_perlshell_tokens = Nullch; |
2928 | w32_perlshell_vec = (char**)NULL; |
2929 | w32_perlshell_items = 0; |
2930 | w32_fdpid = newAV(); |
a02a5408 |
2931 | Newx(w32_children, 1, child_tab); |
216db7ee |
2932 | w32_num_children = 0; |
2933 | # ifdef USE_ITHREADS |
2934 | w32_pseudo_id = 0; |
a02a5408 |
2935 | Newx(w32_pseudo_children, 1, child_tab); |
216db7ee |
2936 | w32_num_pseudo_children = 0; |
2937 | # endif |
2938 | w32_init_socktype = 0; |
2939 | w32_timerid = 0; |
2940 | w32_poll_count = 0; |
2941 | } |
2942 | |
2943 | void |
2944 | Perl_sys_intern_clear(pTHX) |
2945 | { |
2946 | Safefree(w32_perlshell_tokens); |
2947 | Safefree(w32_perlshell_vec); |
2948 | /* NOTE: w32_fdpid is freed by sv_clean_all() */ |
2949 | Safefree(w32_children); |
2950 | if (w32_timerid) { |
2951 | KillTimer(NULL,w32_timerid); |
2952 | w32_timerid=0; |
2953 | } |
2954 | # ifdef USE_ITHREADS |
2955 | Safefree(w32_pseudo_children); |
2956 | # endif |
2957 | } |
2958 | |
2959 | # ifdef USE_ITHREADS |
2960 | |
2961 | void |
2962 | Perl_sys_intern_dup(pTHX_ struct interp_intern *src, struct interp_intern *dst) |
2963 | { |
2964 | dst->perlshell_tokens = Nullch; |
2965 | dst->perlshell_vec = (char**)NULL; |
2966 | dst->perlshell_items = 0; |
2967 | dst->fdpid = newAV(); |
a02a5408 |
2968 | Newxz(dst->children, 1, child_tab); |
216db7ee |
2969 | dst->pseudo_id = 0; |
a02a5408 |
2970 | Newxz(dst->pseudo_children, 1, child_tab); |
216db7ee |
2971 | dst->thr_intern.Winit_socktype = 0; |
2972 | dst->timerid = 0; |
2973 | dst->poll_count = 0; |
2974 | Copy(src->sigtable,dst->sigtable,SIG_SIZE,Sighandler_t); |
2975 | } |
2976 | # endif /* USE_ITHREADS */ |
2977 | #endif /* HAVE_INTERP_INTERN */ |
2978 | |
2979 | static void |
2980 | win32_free_argvw(pTHX_ void *ptr) |
2981 | { |
2982 | char** argv = (char**)ptr; |
2983 | while(*argv) { |
2984 | Safefree(*argv); |
2985 | *argv++ = Nullch; |
2986 | } |
2987 | } |
2988 | |
2989 | void |
2990 | win32_argv2utf8(int argc, char** argv) |
2991 | { |
2992 | /* do nothing, since we're not aware of command line arguments |
2993 | * currently ... |
2994 | */ |
2995 | } |
2996 | |
2997 | #if 0 |
2998 | void |
2999 | Perl_sys_intern_clear(pTHX) |
3000 | { |
3001 | Safefree(w32_perlshell_tokens); |
3002 | Safefree(w32_perlshell_vec); |
3003 | /* NOTE: w32_fdpid is freed by sv_clean_all() */ |
3004 | Safefree(w32_children); |
3005 | # ifdef USE_ITHREADS |
3006 | Safefree(w32_pseudo_children); |
3007 | # endif |
3008 | } |
3009 | |
3010 | #endif |
3011 | // added to remove undefied symbol error in CodeWarrior compilation |
3012 | int |
3013 | Perl_Ireentrant_buffer_ptr(aTHX) |
3014 | { |
3015 | return 0; |
3016 | } |