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> | |
a835ef8a NIS |
14 | #ifdef __GNUC__ |
15 | #define Win32_Winsock | |
16 | #endif | |
0a753a76 | 17 | #include <windows.h> |
18 | ||
68dc0745 | 19 | /* #include "config.h" */ |
0a753a76 | 20 | |
21 | #define PERLIO_NOT_STDIO 0 | |
22 | #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO) | |
23 | #define PerlIO FILE | |
24 | #endif | |
25 | ||
7a9ec5a3 | 26 | #include <sys/stat.h> |
0a753a76 | 27 | #include "EXTERN.h" |
28 | #include "perl.h" | |
c69f6586 GS |
29 | |
30 | #define NO_XSLOCKS | |
c5be433b | 31 | #define PERL_NO_GET_CONTEXT |
ad2e33dc | 32 | #include "XSUB.h" |
c69f6586 GS |
33 | |
34 | #include "Win32iop.h" | |
0a753a76 | 35 | #include <fcntl.h> |
5b0d9cbe NIS |
36 | #ifndef __GNUC__ |
37 | /* assert.h conflicts with #define of assert in perl.h */ | |
0a753a76 | 38 | #include <assert.h> |
5b0d9cbe | 39 | #endif |
0a753a76 | 40 | #include <string.h> |
41 | #include <stdarg.h> | |
ad2e33dc | 42 | #include <float.h> |
ad0751ec | 43 | #include <time.h> |
3730b96e | 44 | #if defined(_MSC_VER) || defined(__MINGW32__) |
ad0751ec GS |
45 | #include <sys/utime.h> |
46 | #else | |
47 | #include <utime.h> | |
48 | #endif | |
0a753a76 | 49 | |
5b0d9cbe NIS |
50 | #ifdef __GNUC__ |
51 | /* Mingw32 defaults to globing command line | |
52 | * So we turn it off like this: | |
53 | */ | |
54 | int _CRT_glob = 0; | |
55 | #endif | |
56 | ||
2b260de0 GS |
57 | #if defined(__MINGW32__) |
58 | # define _stat stat | |
59 | #endif | |
60 | ||
61 | #if defined(__BORLANDC__) | |
0b94c7bb GS |
62 | # define _stat stat |
63 | # define _utimbuf utimbuf | |
64 | #endif | |
65 | ||
6890e559 GS |
66 | #define EXECF_EXEC 1 |
67 | #define EXECF_SPAWN 2 | |
68 | #define EXECF_SPAWN_NOWAIT 3 | |
69 | ||
32e30700 GS |
70 | #if defined(PERL_IMPLICIT_SYS) |
71 | # undef win32_get_privlib | |
72 | # define win32_get_privlib g_win32_get_privlib | |
73 | # undef win32_get_sitelib | |
74 | # define win32_get_sitelib g_win32_get_sitelib | |
75 | # undef do_spawn | |
76 | # define do_spawn g_do_spawn | |
77 | # undef getlogin | |
78 | # define getlogin g_getlogin | |
79 | #endif | |
80 | ||
c69f6586 | 81 | #if defined(PERL_OBJECT) |
32e30700 GS |
82 | # undef do_aspawn |
83 | # define do_aspawn g_do_aspawn | |
84 | # undef Perl_do_exec | |
85 | # define Perl_do_exec g_do_exec | |
c69f6586 GS |
86 | #endif |
87 | ||
ce1da67e | 88 | static void get_shell(void); |
dff6d3cd | 89 | static long tokenize(const char *str, char **dest, char ***destv); |
c5be433b | 90 | int do_spawn2(char *cmd, int exectype); |
e200fe59 | 91 | static BOOL has_shell_metachars(char *ptr); |
2d7a9237 | 92 | static long filetime_to_clock(PFILETIME ft); |
ad0751ec | 93 | static BOOL filetime_from_time(PFILETIME ft, time_t t); |
c5be433b | 94 | static char * get_emd_part(SV **leading, char *trailing, ...); |
0aaad0ff GS |
95 | static void remove_dead_process(long deceased); |
96 | static long find_pid(int pid); | |
97 | static char * qualified_path(const char *cmd); | |
c69f6586 | 98 | |
2d7a9237 | 99 | HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE; |
8ac9c18d | 100 | char w32_module_name[MAX_PATH+1]; |
4b556e6c | 101 | static DWORD w32_platform = (DWORD)-1; |
50892819 | 102 | |
3fe9a6f1 | 103 | int |
ba106d47 GS |
104 | IsWin95(void) |
105 | { | |
0cb96387 | 106 | return (win32_os_id() == VER_PLATFORM_WIN32_WINDOWS); |
3fe9a6f1 | 107 | } |
108 | ||
109 | int | |
ba106d47 GS |
110 | IsWinNT(void) |
111 | { | |
0cb96387 | 112 | return (win32_os_id() == VER_PLATFORM_WIN32_NT); |
3fe9a6f1 | 113 | } |
0a753a76 | 114 | |
c5be433b | 115 | /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */ |
51371543 | 116 | static char* |
c5be433b | 117 | get_regstr_from(HKEY hkey, const char *valuename, SV **svp) |
349ad1fe GS |
118 | { |
119 | /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */ | |
00dc2f4f GS |
120 | HKEY handle; |
121 | DWORD type; | |
122 | const char *subkey = "Software\\Perl"; | |
349ad1fe | 123 | char *str = Nullch; |
00dc2f4f GS |
124 | long retval; |
125 | ||
126 | retval = RegOpenKeyEx(hkey, subkey, 0, KEY_READ, &handle); | |
349ad1fe | 127 | if (retval == ERROR_SUCCESS) { |
51371543 GS |
128 | DWORD datalen; |
129 | retval = RegQueryValueEx(handle, valuename, 0, &type, NULL, &datalen); | |
ba3eb2af | 130 | if (retval == ERROR_SUCCESS && type == REG_SZ) { |
c5be433b GS |
131 | dTHXo; |
132 | if (!*svp) | |
133 | *svp = sv_2mortal(newSVpvn("",0)); | |
134 | SvGROW(*svp, datalen); | |
51371543 | 135 | retval = RegQueryValueEx(handle, valuename, 0, NULL, |
c5be433b | 136 | (PBYTE)SvPVX(*svp), &datalen); |
51371543 | 137 | if (retval == ERROR_SUCCESS) { |
c5be433b GS |
138 | str = SvPVX(*svp); |
139 | SvCUR_set(*svp,datalen-1); | |
51371543 | 140 | } |
00dc2f4f GS |
141 | } |
142 | RegCloseKey(handle); | |
143 | } | |
349ad1fe | 144 | return str; |
00dc2f4f GS |
145 | } |
146 | ||
c5be433b | 147 | /* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */ |
51371543 | 148 | static char* |
c5be433b | 149 | get_regstr(const char *valuename, SV **svp) |
00dc2f4f | 150 | { |
c5be433b | 151 | char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp); |
349ad1fe | 152 | if (!str) |
c5be433b | 153 | str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp); |
349ad1fe | 154 | return str; |
00dc2f4f GS |
155 | } |
156 | ||
c5be433b | 157 | /* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */ |
e5a95ffb | 158 | static char * |
c5be433b | 159 | get_emd_part(SV **prev_pathp, char *trailing_path, ...) |
00dc2f4f | 160 | { |
dc9e4912 | 161 | char base[10]; |
e5a95ffb | 162 | va_list ap; |
e24c7c18 | 163 | char mod_name[MAX_PATH+1]; |
00dc2f4f | 164 | char *ptr; |
e5a95ffb GS |
165 | char *optr; |
166 | char *strip; | |
167 | int oldsize, newsize; | |
168 | ||
169 | va_start(ap, trailing_path); | |
170 | strip = va_arg(ap, char *); | |
171 | ||
cceca5ed GS |
172 | sprintf(base, "%5.3f", |
173 | (double)PERL_REVISION + ((double)PERL_VERSION / (double)1000)); | |
dc9e4912 | 174 | |
8ac9c18d GS |
175 | if (!*w32_module_name) { |
176 | GetModuleFileName((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE) | |
177 | ? GetModuleHandle(NULL) | |
178 | : w32_perldll_handle), | |
179 | w32_module_name, sizeof(w32_module_name)); | |
180 | ||
181 | /* try to get full path to binary (which may be mangled when perl is | |
182 | * run from a 16-bit app) */ | |
bf49b057 | 183 | /*PerlIO_printf(Perl_debug_log, "Before %s\n", w32_module_name);*/ |
8ac9c18d | 184 | (void)win32_longpath(w32_module_name); |
bf49b057 | 185 | /*PerlIO_printf(Perl_debug_log, "After %s\n", w32_module_name);*/ |
8ac9c18d GS |
186 | |
187 | /* normalize to forward slashes */ | |
188 | ptr = w32_module_name; | |
189 | while (*ptr) { | |
190 | if (*ptr == '\\') | |
191 | *ptr = '/'; | |
192 | ++ptr; | |
193 | } | |
95140b98 | 194 | } |
8ac9c18d | 195 | strcpy(mod_name, w32_module_name); |
95140b98 | 196 | ptr = strrchr(mod_name, '/'); |
e5a95ffb GS |
197 | while (ptr && strip) { |
198 | /* look for directories to skip back */ | |
199 | optr = ptr; | |
00dc2f4f | 200 | *ptr = '\0'; |
95140b98 | 201 | ptr = strrchr(mod_name, '/'); |
1c39adb2 GS |
202 | /* avoid stripping component if there is no slash, |
203 | * or it doesn't match ... */ | |
e5a95ffb | 204 | if (!ptr || stricmp(ptr+1, strip) != 0) { |
1c39adb2 GS |
205 | /* ... but not if component matches 5.00X* */ |
206 | if (!ptr || !(*strip == '5' && *(ptr+1) == '5' | |
207 | && strncmp(strip, base, 5) == 0 | |
208 | && strncmp(ptr+1, base, 5) == 0)) | |
95140b98 GS |
209 | { |
210 | *optr = '/'; | |
80252599 GS |
211 | ptr = optr; |
212 | } | |
00dc2f4f | 213 | } |
e5a95ffb | 214 | strip = va_arg(ap, char *); |
00dc2f4f | 215 | } |
e5a95ffb GS |
216 | if (!ptr) { |
217 | ptr = mod_name; | |
218 | *ptr++ = '.'; | |
95140b98 | 219 | *ptr = '/'; |
00dc2f4f | 220 | } |
e5a95ffb GS |
221 | va_end(ap); |
222 | strcpy(++ptr, trailing_path); | |
223 | ||
dc9e4912 | 224 | /* only add directory if it exists */ |
349ad1fe | 225 | if (GetFileAttributes(mod_name) != (DWORD) -1) { |
dc9e4912 | 226 | /* directory exists */ |
c5be433b GS |
227 | dTHXo; |
228 | if (!*prev_pathp) | |
229 | *prev_pathp = sv_2mortal(newSVpvn("",0)); | |
230 | sv_catpvn(*prev_pathp, ";", 1); | |
231 | sv_catpv(*prev_pathp, mod_name); | |
232 | return SvPVX(*prev_pathp); | |
00dc2f4f | 233 | } |
00dc2f4f | 234 | |
cf11f4bf | 235 | return Nullch; |
00dc2f4f GS |
236 | } |
237 | ||
238 | char * | |
c5be433b | 239 | win32_get_privlib(char *pl) |
00dc2f4f | 240 | { |
c5be433b | 241 | dTHXo; |
e5a95ffb GS |
242 | char *stdlib = "lib"; |
243 | char buffer[MAX_PATH+1]; | |
51371543 | 244 | SV *sv = Nullsv; |
00dc2f4f | 245 | |
e5a95ffb GS |
246 | /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */ |
247 | sprintf(buffer, "%s-%s", stdlib, pl); | |
c5be433b GS |
248 | if (!get_regstr(buffer, &sv)) |
249 | (void)get_regstr(stdlib, &sv); | |
00dc2f4f | 250 | |
e5a95ffb | 251 | /* $stdlib .= ";$EMD/../../lib" */ |
c5be433b | 252 | return get_emd_part(&sv, stdlib, ARCHNAME, "bin", Nullch); |
00dc2f4f GS |
253 | } |
254 | ||
68dc0745 | 255 | char * |
c5be433b | 256 | win32_get_sitelib(char *pl) |
00dc2f4f | 257 | { |
c5be433b | 258 | dTHXo; |
e5a95ffb GS |
259 | char *sitelib = "sitelib"; |
260 | char regstr[40]; | |
e24c7c18 | 261 | char pathstr[MAX_PATH+1]; |
e5a95ffb | 262 | DWORD datalen; |
e5a95ffb | 263 | int len, newsize; |
51371543 GS |
264 | SV *sv1 = Nullsv; |
265 | SV *sv2 = Nullsv; | |
00dc2f4f GS |
266 | |
267 | /* $HKCU{"sitelib-$]"} || $HKLM{"sitelib-$]"} . ---; */ | |
e5a95ffb | 268 | sprintf(regstr, "%s-%s", sitelib, pl); |
c5be433b | 269 | (void)get_regstr(regstr, &sv1); |
e5a95ffb GS |
270 | |
271 | /* $sitelib .= | |
272 | * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/$]/lib"; */ | |
95140b98 | 273 | sprintf(pathstr, "site/%s/lib", pl); |
c5be433b | 274 | (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch); |
51371543 | 275 | if (!sv1 && strlen(pl) == 7) { |
cf11f4bf GS |
276 | /* pl may have been SUBVERSION-specific; try again without |
277 | * SUBVERSION */ | |
278 | sprintf(pathstr, "site/%.5s/lib", pl); | |
c5be433b | 279 | (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch); |
cf11f4bf | 280 | } |
00dc2f4f GS |
281 | |
282 | /* $HKCU{'sitelib'} || $HKLM{'sitelib'} . ---; */ | |
c5be433b | 283 | (void)get_regstr(sitelib, &sv2); |
00dc2f4f | 284 | |
e5a95ffb GS |
285 | /* $sitelib .= |
286 | * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/lib"; */ | |
c5be433b | 287 | (void)get_emd_part(&sv2, "site/lib", ARCHNAME, "bin", pl, Nullch); |
e5a95ffb | 288 | |
51371543 GS |
289 | if (!sv1 && !sv2) |
290 | return Nullch; | |
291 | if (!sv1) | |
292 | return SvPVX(sv2); | |
293 | if (!sv2) | |
294 | return SvPVX(sv1); | |
e5a95ffb | 295 | |
349ad1fe GS |
296 | sv_catpvn(sv1, ";", 1); |
297 | sv_catsv(sv1, sv2); | |
e5a95ffb | 298 | |
349ad1fe | 299 | return SvPVX(sv1); |
68dc0745 | 300 | } |
0a753a76 | 301 | |
b4793f7f | 302 | |
2d7a9237 | 303 | static BOOL |
e200fe59 | 304 | has_shell_metachars(char *ptr) |
68dc0745 | 305 | { |
306 | int inquote = 0; | |
307 | char quote = '\0'; | |
308 | ||
309 | /* | |
310 | * Scan string looking for redirection (< or >) or pipe | |
e200fe59 JD |
311 | * characters (|) that are not in a quoted string. |
312 | * Shell variable interpolation (%VAR%) can also happen inside strings. | |
68dc0745 | 313 | */ |
9404a519 | 314 | while (*ptr) { |
68dc0745 | 315 | switch(*ptr) { |
e200fe59 JD |
316 | case '%': |
317 | return TRUE; | |
68dc0745 | 318 | case '\'': |
319 | case '\"': | |
9404a519 GS |
320 | if (inquote) { |
321 | if (quote == *ptr) { | |
68dc0745 | 322 | inquote = 0; |
323 | quote = '\0'; | |
0a753a76 | 324 | } |
68dc0745 | 325 | } |
326 | else { | |
327 | quote = *ptr; | |
328 | inquote++; | |
329 | } | |
330 | break; | |
331 | case '>': | |
332 | case '<': | |
333 | case '|': | |
9404a519 | 334 | if (!inquote) |
68dc0745 | 335 | return TRUE; |
336 | default: | |
337 | break; | |
0a753a76 | 338 | } |
68dc0745 | 339 | ++ptr; |
340 | } | |
341 | return FALSE; | |
0a753a76 | 342 | } |
343 | ||
32e30700 | 344 | #if !defined(PERL_IMPLICIT_SYS) |
68dc0745 | 345 | /* since the current process environment is being updated in util.c |
346 | * the library functions will get the correct environment | |
347 | */ | |
348 | PerlIO * | |
4f63d024 | 349 | Perl_my_popen(pTHX_ char *cmd, char *mode) |
0a753a76 | 350 | { |
351 | #ifdef FIXCMD | |
68dc0745 | 352 | #define fixcmd(x) { \ |
353 | char *pspace = strchr((x),' '); \ | |
354 | if (pspace) { \ | |
355 | char *p = (x); \ | |
356 | while (p < pspace) { \ | |
357 | if (*p == '/') \ | |
358 | *p = '\\'; \ | |
359 | p++; \ | |
360 | } \ | |
361 | } \ | |
362 | } | |
0a753a76 | 363 | #else |
364 | #define fixcmd(x) | |
365 | #endif | |
68dc0745 | 366 | fixcmd(cmd); |
45bc9206 | 367 | PERL_FLUSHALL_FOR_CHILD; |
0a753a76 | 368 | return win32_popen(cmd, mode); |
0a753a76 | 369 | } |
370 | ||
68dc0745 | 371 | long |
4f63d024 | 372 | Perl_my_pclose(pTHX_ PerlIO *fp) |
0a753a76 | 373 | { |
374 | return win32_pclose(fp); | |
375 | } | |
c69f6586 | 376 | #endif |
0a753a76 | 377 | |
0cb96387 GS |
378 | DllExport unsigned long |
379 | win32_os_id(void) | |
0a753a76 | 380 | { |
8b10511d | 381 | static OSVERSIONINFO osver; |
0a753a76 | 382 | |
2d7a9237 | 383 | if (osver.dwPlatformId != w32_platform) { |
8b10511d GS |
384 | memset(&osver, 0, sizeof(OSVERSIONINFO)); |
385 | osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
386 | GetVersionEx(&osver); | |
2d7a9237 | 387 | w32_platform = osver.dwPlatformId; |
8b10511d | 388 | } |
0cb96387 | 389 | return (unsigned long)w32_platform; |
0a753a76 | 390 | } |
391 | ||
ce1da67e GS |
392 | /* Tokenize a string. Words are null-separated, and the list |
393 | * ends with a doubled null. Any character (except null and | |
394 | * including backslash) may be escaped by preceding it with a | |
395 | * backslash (the backslash will be stripped). | |
396 | * Returns number of words in result buffer. | |
397 | */ | |
398 | static long | |
dff6d3cd | 399 | tokenize(const char *str, char **dest, char ***destv) |
ce1da67e GS |
400 | { |
401 | char *retstart = Nullch; | |
402 | char **retvstart = 0; | |
403 | int items = -1; | |
404 | if (str) { | |
c5be433b | 405 | dTHXo; |
ce1da67e GS |
406 | int slen = strlen(str); |
407 | register char *ret; | |
408 | register char **retv; | |
409 | New(1307, ret, slen+2, char); | |
410 | New(1308, retv, (slen+3)/2, char*); | |
411 | ||
412 | retstart = ret; | |
413 | retvstart = retv; | |
414 | *retv = ret; | |
415 | items = 0; | |
416 | while (*str) { | |
417 | *ret = *str++; | |
418 | if (*ret == '\\' && *str) | |
419 | *ret = *str++; | |
420 | else if (*ret == ' ') { | |
421 | while (*str == ' ') | |
422 | str++; | |
423 | if (ret == retstart) | |
424 | ret--; | |
425 | else { | |
426 | *ret = '\0'; | |
427 | ++items; | |
428 | if (*str) | |
429 | *++retv = ret+1; | |
430 | } | |
431 | } | |
432 | else if (!*str) | |
433 | ++items; | |
434 | ret++; | |
435 | } | |
436 | retvstart[items] = Nullch; | |
437 | *ret++ = '\0'; | |
438 | *ret = '\0'; | |
439 | } | |
440 | *dest = retstart; | |
441 | *destv = retvstart; | |
442 | return items; | |
443 | } | |
444 | ||
445 | static void | |
2d7a9237 | 446 | get_shell(void) |
0a753a76 | 447 | { |
c5be433b | 448 | dTHXo; |
ce1da67e | 449 | if (!w32_perlshell_tokens) { |
174c211a GS |
450 | /* we don't use COMSPEC here for two reasons: |
451 | * 1. the same reason perl on UNIX doesn't use SHELL--rampant and | |
452 | * uncontrolled unportability of the ensuing scripts. | |
453 | * 2. PERL5SHELL could be set to a shell that may not be fit for | |
454 | * interactive use (which is what most programs look in COMSPEC | |
455 | * for). | |
456 | */ | |
dff6d3cd GS |
457 | const char* defaultshell = (IsWinNT() |
458 | ? "cmd.exe /x/c" : "command.com /c"); | |
459 | const char *usershell = getenv("PERL5SHELL"); | |
ce1da67e GS |
460 | w32_perlshell_items = tokenize(usershell ? usershell : defaultshell, |
461 | &w32_perlshell_tokens, | |
462 | &w32_perlshell_vec); | |
68dc0745 | 463 | } |
0a753a76 | 464 | } |
465 | ||
68dc0745 | 466 | int |
c5be433b | 467 | do_aspawn(void *vreally, void **vmark, void **vsp) |
0a753a76 | 468 | { |
c5be433b | 469 | dTHXo; |
2d7a9237 GS |
470 | SV *really = (SV*)vreally; |
471 | SV **mark = (SV**)vmark; | |
472 | SV **sp = (SV**)vsp; | |
68dc0745 | 473 | char **argv; |
2d7a9237 | 474 | char *str; |
68dc0745 | 475 | int status; |
2d7a9237 | 476 | int flag = P_WAIT; |
68dc0745 | 477 | int index = 0; |
68dc0745 | 478 | |
2d7a9237 GS |
479 | if (sp <= mark) |
480 | return -1; | |
68dc0745 | 481 | |
ce1da67e GS |
482 | get_shell(); |
483 | New(1306, argv, (sp - mark) + w32_perlshell_items + 2, char*); | |
2d7a9237 GS |
484 | |
485 | if (SvNIOKp(*(mark+1)) && !SvPOKp(*(mark+1))) { | |
486 | ++mark; | |
487 | flag = SvIVx(*mark); | |
68dc0745 | 488 | } |
489 | ||
9404a519 | 490 | while (++mark <= sp) { |
bb897dfc | 491 | if (*mark && (str = SvPV_nolen(*mark))) |
2d7a9237 GS |
492 | argv[index++] = str; |
493 | else | |
494 | argv[index++] = ""; | |
68dc0745 | 495 | } |
496 | argv[index++] = 0; | |
497 | ||
2d7a9237 | 498 | status = win32_spawnvp(flag, |
bb897dfc | 499 | (const char*)(really ? SvPV_nolen(really) : argv[0]), |
2d7a9237 GS |
500 | (const char* const*)argv); |
501 | ||
80252599 | 502 | if (status < 0 && (errno == ENOEXEC || errno == ENOENT)) { |
2d7a9237 | 503 | /* possible shell-builtin, invoke with shell */ |
ce1da67e GS |
504 | int sh_items; |
505 | sh_items = w32_perlshell_items; | |
2d7a9237 GS |
506 | while (--index >= 0) |
507 | argv[index+sh_items] = argv[index]; | |
ce1da67e GS |
508 | while (--sh_items >= 0) |
509 | argv[sh_items] = w32_perlshell_vec[sh_items]; | |
2d7a9237 GS |
510 | |
511 | status = win32_spawnvp(flag, | |
bb897dfc | 512 | (const char*)(really ? SvPV_nolen(really) : argv[0]), |
2d7a9237 GS |
513 | (const char* const*)argv); |
514 | } | |
68dc0745 | 515 | |
50892819 GS |
516 | if (flag != P_NOWAIT) { |
517 | if (status < 0) { | |
0453d815 PM |
518 | dTHR; |
519 | if (ckWARN(WARN_EXEC)) | |
520 | Perl_warner(aTHX_ WARN_EXEC, "Can't spawn \"%s\": %s", argv[0], strerror(errno)); | |
50892819 GS |
521 | status = 255 * 256; |
522 | } | |
523 | else | |
524 | status *= 256; | |
b28d0864 | 525 | PL_statusvalue = status; |
5aabfad6 | 526 | } |
ce1da67e | 527 | Safefree(argv); |
50892819 | 528 | return (status); |
68dc0745 | 529 | } |
530 | ||
c69f6586 | 531 | int |
c5be433b | 532 | do_spawn2(char *cmd, int exectype) |
68dc0745 | 533 | { |
c5be433b | 534 | dTHXo; |
68dc0745 | 535 | char **a; |
536 | char *s; | |
537 | char **argv; | |
538 | int status = -1; | |
539 | BOOL needToTry = TRUE; | |
2d7a9237 | 540 | char *cmd2; |
68dc0745 | 541 | |
2d7a9237 GS |
542 | /* Save an extra exec if possible. See if there are shell |
543 | * metacharacters in it */ | |
e200fe59 | 544 | if (!has_shell_metachars(cmd)) { |
fc36a67e | 545 | New(1301,argv, strlen(cmd) / 2 + 2, char*); |
546 | New(1302,cmd2, strlen(cmd) + 1, char); | |
68dc0745 | 547 | strcpy(cmd2, cmd); |
548 | a = argv; | |
549 | for (s = cmd2; *s;) { | |
de030af3 | 550 | while (*s && isSPACE(*s)) |
68dc0745 | 551 | s++; |
552 | if (*s) | |
553 | *(a++) = s; | |
de030af3 | 554 | while (*s && !isSPACE(*s)) |
68dc0745 | 555 | s++; |
9404a519 | 556 | if (*s) |
68dc0745 | 557 | *s++ = '\0'; |
0a753a76 | 558 | } |
68dc0745 | 559 | *a = Nullch; |
ce1da67e | 560 | if (argv[0]) { |
6890e559 GS |
561 | switch (exectype) { |
562 | case EXECF_SPAWN: | |
563 | status = win32_spawnvp(P_WAIT, argv[0], | |
564 | (const char* const*)argv); | |
565 | break; | |
566 | case EXECF_SPAWN_NOWAIT: | |
567 | status = win32_spawnvp(P_NOWAIT, argv[0], | |
568 | (const char* const*)argv); | |
569 | break; | |
570 | case EXECF_EXEC: | |
571 | status = win32_execvp(argv[0], (const char* const*)argv); | |
572 | break; | |
573 | } | |
2d7a9237 | 574 | if (status != -1 || errno == 0) |
68dc0745 | 575 | needToTry = FALSE; |
0a753a76 | 576 | } |
0a753a76 | 577 | Safefree(argv); |
68dc0745 | 578 | Safefree(cmd2); |
579 | } | |
2d7a9237 | 580 | if (needToTry) { |
ce1da67e GS |
581 | char **argv; |
582 | int i = -1; | |
583 | get_shell(); | |
584 | New(1306, argv, w32_perlshell_items + 2, char*); | |
585 | while (++i < w32_perlshell_items) | |
586 | argv[i] = w32_perlshell_vec[i]; | |
2d7a9237 GS |
587 | argv[i++] = cmd; |
588 | argv[i] = Nullch; | |
6890e559 GS |
589 | switch (exectype) { |
590 | case EXECF_SPAWN: | |
591 | status = win32_spawnvp(P_WAIT, argv[0], | |
592 | (const char* const*)argv); | |
593 | break; | |
594 | case EXECF_SPAWN_NOWAIT: | |
595 | status = win32_spawnvp(P_NOWAIT, argv[0], | |
596 | (const char* const*)argv); | |
597 | break; | |
598 | case EXECF_EXEC: | |
599 | status = win32_execvp(argv[0], (const char* const*)argv); | |
600 | break; | |
601 | } | |
ce1da67e GS |
602 | cmd = argv[0]; |
603 | Safefree(argv); | |
68dc0745 | 604 | } |
50892819 GS |
605 | if (exectype != EXECF_SPAWN_NOWAIT) { |
606 | if (status < 0) { | |
0453d815 PM |
607 | dTHR; |
608 | if (ckWARN(WARN_EXEC)) | |
609 | Perl_warner(aTHX_ WARN_EXEC, "Can't %s \"%s\": %s", | |
50892819 GS |
610 | (exectype == EXECF_EXEC ? "exec" : "spawn"), |
611 | cmd, strerror(errno)); | |
612 | status = 255 * 256; | |
613 | } | |
614 | else | |
615 | status *= 256; | |
b28d0864 | 616 | PL_statusvalue = status; |
5aabfad6 | 617 | } |
50892819 | 618 | return (status); |
0a753a76 | 619 | } |
620 | ||
6890e559 | 621 | int |
c5be433b | 622 | do_spawn(char *cmd) |
6890e559 | 623 | { |
c5be433b | 624 | return do_spawn2(cmd, EXECF_SPAWN); |
6890e559 GS |
625 | } |
626 | ||
2d7a9237 | 627 | int |
c5be433b | 628 | do_spawn_nowait(char *cmd) |
2d7a9237 | 629 | { |
c5be433b | 630 | return do_spawn2(cmd, EXECF_SPAWN_NOWAIT); |
2d7a9237 GS |
631 | } |
632 | ||
6890e559 | 633 | bool |
4f63d024 | 634 | Perl_do_exec(pTHX_ char *cmd) |
6890e559 | 635 | { |
c5be433b | 636 | do_spawn2(cmd, EXECF_EXEC); |
6890e559 GS |
637 | return FALSE; |
638 | } | |
639 | ||
68dc0745 | 640 | /* The idea here is to read all the directory names into a string table |
641 | * (separated by nulls) and when one of the other dir functions is called | |
642 | * return the pointer to the current file name. | |
643 | */ | |
c5be433b | 644 | DllExport DIR * |
ce2e26e5 | 645 | win32_opendir(char *filename) |
0a753a76 | 646 | { |
c5be433b | 647 | dTHXo; |
95136add | 648 | DIR *dirp; |
9404a519 GS |
649 | long len; |
650 | long idx; | |
651 | char scanname[MAX_PATH+3]; | |
652 | struct stat sbuf; | |
7fac1903 GS |
653 | WIN32_FIND_DATAA aFindData; |
654 | WIN32_FIND_DATAW wFindData; | |
9404a519 | 655 | HANDLE fh; |
7fac1903 GS |
656 | char buffer[MAX_PATH*2]; |
657 | WCHAR wbuffer[MAX_PATH]; | |
95136add | 658 | char* ptr; |
9404a519 GS |
659 | |
660 | len = strlen(filename); | |
661 | if (len > MAX_PATH) | |
662 | return NULL; | |
68dc0745 | 663 | |
664 | /* check to see if filename is a directory */ | |
69d3ab13 | 665 | if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode)) |
24caa93f | 666 | return NULL; |
68dc0745 | 667 | |
68dc0745 | 668 | /* Get us a DIR structure */ |
95136add | 669 | Newz(1303, dirp, 1, DIR); |
68dc0745 | 670 | |
671 | /* Create the search pattern */ | |
672 | strcpy(scanname, filename); | |
23db2e2d GS |
673 | |
674 | /* bare drive name means look in cwd for drive */ | |
675 | if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') { | |
676 | scanname[len++] = '.'; | |
677 | scanname[len++] = '/'; | |
678 | } | |
679 | else if (scanname[len-1] != '/' && scanname[len-1] != '\\') { | |
9404a519 | 680 | scanname[len++] = '/'; |
23db2e2d | 681 | } |
9404a519 GS |
682 | scanname[len++] = '*'; |
683 | scanname[len] = '\0'; | |
68dc0745 | 684 | |
685 | /* do the FindFirstFile call */ | |
7fac1903 | 686 | if (USING_WIDE()) { |
0cb96387 | 687 | A2WHELPER(scanname, wbuffer, sizeof(wbuffer)); |
7fac1903 GS |
688 | fh = FindFirstFileW(wbuffer, &wFindData); |
689 | } | |
690 | else { | |
691 | fh = FindFirstFileA(scanname, &aFindData); | |
692 | } | |
95136add | 693 | dirp->handle = fh; |
9404a519 | 694 | if (fh == INVALID_HANDLE_VALUE) { |
95136add | 695 | DWORD err = GetLastError(); |
21e72512 | 696 | /* FindFirstFile() fails on empty drives! */ |
95136add GS |
697 | switch (err) { |
698 | case ERROR_FILE_NOT_FOUND: | |
699 | return dirp; | |
700 | case ERROR_NO_MORE_FILES: | |
701 | case ERROR_PATH_NOT_FOUND: | |
702 | errno = ENOENT; | |
703 | break; | |
704 | case ERROR_NOT_ENOUGH_MEMORY: | |
705 | errno = ENOMEM; | |
706 | break; | |
707 | default: | |
708 | errno = EINVAL; | |
709 | break; | |
710 | } | |
711 | Safefree(dirp); | |
68dc0745 | 712 | return NULL; |
713 | } | |
714 | ||
715 | /* now allocate the first part of the string table for | |
716 | * the filenames that we find. | |
717 | */ | |
7fac1903 | 718 | if (USING_WIDE()) { |
0cb96387 | 719 | W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer)); |
7fac1903 GS |
720 | ptr = buffer; |
721 | } | |
722 | else { | |
723 | ptr = aFindData.cFileName; | |
724 | } | |
725 | idx = strlen(ptr)+1; | |
95136add GS |
726 | if (idx < 256) |
727 | dirp->size = 128; | |
728 | else | |
729 | dirp->size = idx; | |
730 | New(1304, dirp->start, dirp->size, char); | |
731 | strcpy(dirp->start, ptr); | |
732 | dirp->nfiles++; | |
733 | dirp->end = dirp->curr = dirp->start; | |
734 | dirp->end += idx; | |
735 | return dirp; | |
0a753a76 | 736 | } |
737 | ||
738 | ||
68dc0745 | 739 | /* Readdir just returns the current string pointer and bumps the |
740 | * string pointer to the nDllExport entry. | |
741 | */ | |
c5be433b | 742 | DllExport struct direct * |
ce2e26e5 | 743 | win32_readdir(DIR *dirp) |
0a753a76 | 744 | { |
95136add | 745 | long len; |
0a753a76 | 746 | |
68dc0745 | 747 | if (dirp->curr) { |
748 | /* first set up the structure to return */ | |
749 | len = strlen(dirp->curr); | |
0f38926b | 750 | strcpy(dirp->dirstr.d_name, dirp->curr); |
68dc0745 | 751 | dirp->dirstr.d_namlen = len; |
0a753a76 | 752 | |
68dc0745 | 753 | /* Fake an inode */ |
0f38926b | 754 | dirp->dirstr.d_ino = dirp->curr - dirp->start; |
0a753a76 | 755 | |
95136add | 756 | /* Now set up for the next call to readdir */ |
68dc0745 | 757 | dirp->curr += len + 1; |
95136add GS |
758 | if (dirp->curr >= dirp->end) { |
759 | dTHXo; | |
760 | char* ptr; | |
761 | BOOL res; | |
762 | WIN32_FIND_DATAW wFindData; | |
763 | WIN32_FIND_DATAA aFindData; | |
764 | char buffer[MAX_PATH*2]; | |
765 | ||
766 | /* finding the next file that matches the wildcard | |
767 | * (which should be all of them in this directory!). | |
95136add GS |
768 | */ |
769 | if (USING_WIDE()) { | |
770 | res = FindNextFileW(dirp->handle, &wFindData); | |
771 | if (res) { | |
772 | W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer)); | |
773 | ptr = buffer; | |
774 | } | |
775 | } | |
776 | else { | |
777 | res = FindNextFileA(dirp->handle, &aFindData); | |
778 | if (res) | |
779 | ptr = aFindData.cFileName; | |
780 | } | |
781 | if (res) { | |
0f38926b GS |
782 | long endpos = dirp->end - dirp->start; |
783 | long newsize = endpos + strlen(ptr) + 1; | |
95136add GS |
784 | /* bump the string table size by enough for the |
785 | * new name and it's null terminator */ | |
0f38926b GS |
786 | while (newsize > dirp->size) { |
787 | long curpos = dirp->curr - dirp->start; | |
95136add GS |
788 | dirp->size *= 2; |
789 | Renew(dirp->start, dirp->size, char); | |
0f38926b | 790 | dirp->curr = dirp->start + curpos; |
95136add | 791 | } |
0f38926b GS |
792 | strcpy(dirp->start + endpos, ptr); |
793 | dirp->end = dirp->start + newsize; | |
95136add GS |
794 | dirp->nfiles++; |
795 | } | |
796 | else | |
797 | dirp->curr = NULL; | |
68dc0745 | 798 | } |
68dc0745 | 799 | return &(dirp->dirstr); |
800 | } | |
801 | else | |
802 | return NULL; | |
0a753a76 | 803 | } |
804 | ||
68dc0745 | 805 | /* Telldir returns the current string pointer position */ |
c5be433b | 806 | DllExport long |
ce2e26e5 | 807 | win32_telldir(DIR *dirp) |
0a753a76 | 808 | { |
95136add | 809 | return (dirp->curr - dirp->start); |
0a753a76 | 810 | } |
811 | ||
812 | ||
68dc0745 | 813 | /* Seekdir moves the string pointer to a previously saved position |
95136add | 814 | * (returned by telldir). |
68dc0745 | 815 | */ |
c5be433b | 816 | DllExport void |
ce2e26e5 | 817 | win32_seekdir(DIR *dirp, long loc) |
0a753a76 | 818 | { |
95136add | 819 | dirp->curr = dirp->start + loc; |
0a753a76 | 820 | } |
821 | ||
68dc0745 | 822 | /* Rewinddir resets the string pointer to the start */ |
c5be433b | 823 | DllExport void |
ce2e26e5 | 824 | win32_rewinddir(DIR *dirp) |
0a753a76 | 825 | { |
826 | dirp->curr = dirp->start; | |
827 | } | |
828 | ||
68dc0745 | 829 | /* free the memory allocated by opendir */ |
c5be433b | 830 | DllExport int |
ce2e26e5 | 831 | win32_closedir(DIR *dirp) |
0a753a76 | 832 | { |
c5be433b | 833 | dTHXo; |
95136add | 834 | if (dirp->handle != INVALID_HANDLE_VALUE) |
0f38926b | 835 | FindClose(dirp->handle); |
0a753a76 | 836 | Safefree(dirp->start); |
837 | Safefree(dirp); | |
68dc0745 | 838 | return 1; |
0a753a76 | 839 | } |
840 | ||
841 | ||
68dc0745 | 842 | /* |
843 | * various stubs | |
844 | */ | |
0a753a76 | 845 | |
846 | ||
68dc0745 | 847 | /* Ownership |
848 | * | |
849 | * Just pretend that everyone is a superuser. NT will let us know if | |
850 | * we don\'t really have permission to do something. | |
851 | */ | |
0a753a76 | 852 | |
853 | #define ROOT_UID ((uid_t)0) | |
854 | #define ROOT_GID ((gid_t)0) | |
855 | ||
68dc0745 | 856 | uid_t |
857 | getuid(void) | |
0a753a76 | 858 | { |
68dc0745 | 859 | return ROOT_UID; |
0a753a76 | 860 | } |
861 | ||
68dc0745 | 862 | uid_t |
863 | geteuid(void) | |
0a753a76 | 864 | { |
68dc0745 | 865 | return ROOT_UID; |
0a753a76 | 866 | } |
867 | ||
68dc0745 | 868 | gid_t |
869 | getgid(void) | |
0a753a76 | 870 | { |
68dc0745 | 871 | return ROOT_GID; |
0a753a76 | 872 | } |
873 | ||
68dc0745 | 874 | gid_t |
875 | getegid(void) | |
0a753a76 | 876 | { |
68dc0745 | 877 | return ROOT_GID; |
0a753a76 | 878 | } |
879 | ||
68dc0745 | 880 | int |
22239a37 | 881 | setuid(uid_t auid) |
0a753a76 | 882 | { |
22239a37 | 883 | return (auid == ROOT_UID ? 0 : -1); |
0a753a76 | 884 | } |
885 | ||
68dc0745 | 886 | int |
22239a37 | 887 | setgid(gid_t agid) |
0a753a76 | 888 | { |
22239a37 | 889 | return (agid == ROOT_GID ? 0 : -1); |
0a753a76 | 890 | } |
891 | ||
e34ffe5a GS |
892 | char * |
893 | getlogin(void) | |
894 | { | |
c5be433b | 895 | dTHXo; |
3352bfcb GS |
896 | char *buf = w32_getlogin_buffer; |
897 | DWORD size = sizeof(w32_getlogin_buffer); | |
e34ffe5a GS |
898 | if (GetUserName(buf,&size)) |
899 | return buf; | |
900 | return (char*)NULL; | |
901 | } | |
902 | ||
b990f8c8 GS |
903 | int |
904 | chown(const char *path, uid_t owner, gid_t group) | |
905 | { | |
906 | /* XXX noop */ | |
1c1c7f20 | 907 | return 0; |
b990f8c8 GS |
908 | } |
909 | ||
0aaad0ff GS |
910 | static long |
911 | find_pid(int pid) | |
0a753a76 | 912 | { |
c5be433b | 913 | dTHXo; |
51371543 | 914 | long child; |
f55ee38a | 915 | for (child = 0 ; child < w32_num_children ; ++child) { |
0aaad0ff GS |
916 | if (w32_child_pids[child] == pid) |
917 | return child; | |
918 | } | |
919 | return -1; | |
920 | } | |
921 | ||
922 | static void | |
923 | remove_dead_process(long child) | |
924 | { | |
925 | if (child >= 0) { | |
c5be433b | 926 | dTHXo; |
0aaad0ff GS |
927 | CloseHandle(w32_child_handles[child]); |
928 | Copy(&w32_child_handles[child+1], &w32_child_handles[child], | |
929 | (w32_num_children-child-1), HANDLE); | |
930 | Copy(&w32_child_pids[child+1], &w32_child_pids[child], | |
931 | (w32_num_children-child-1), DWORD); | |
932 | w32_num_children--; | |
f55ee38a | 933 | } |
f55ee38a GS |
934 | } |
935 | ||
936 | DllExport int | |
937 | win32_kill(int pid, int sig) | |
938 | { | |
0aaad0ff GS |
939 | HANDLE hProcess; |
940 | hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); | |
941 | if (hProcess && TerminateProcess(hProcess, sig)) | |
68dc0745 | 942 | CloseHandle(hProcess); |
0aaad0ff GS |
943 | else { |
944 | errno = EINVAL; | |
945 | return -1; | |
68dc0745 | 946 | } |
947 | return 0; | |
0a753a76 | 948 | } |
fbbbcc48 | 949 | |
68dc0745 | 950 | /* |
951 | * File system stuff | |
952 | */ | |
0a753a76 | 953 | |
f3986ebb GS |
954 | DllExport unsigned int |
955 | win32_sleep(unsigned int t) | |
0a753a76 | 956 | { |
68dc0745 | 957 | Sleep(t*1000); |
958 | return 0; | |
0a753a76 | 959 | } |
960 | ||
68dc0745 | 961 | DllExport int |
962 | win32_stat(const char *path, struct stat *buffer) | |
0a753a76 | 963 | { |
c5be433b | 964 | dTHXo; |
24caa93f | 965 | char t[MAX_PATH+1]; |
68dc0745 | 966 | int l = strlen(path); |
67fbe06e | 967 | int res; |
7fac1903 | 968 | WCHAR wbuffer[MAX_PATH]; |
6b980173 JD |
969 | HANDLE handle; |
970 | int nlink = 1; | |
0a753a76 | 971 | |
68dc0745 | 972 | if (l > 1) { |
973 | switch(path[l - 1]) { | |
e1dbac94 GS |
974 | /* FindFirstFile() and stat() are buggy with a trailing |
975 | * backslash, so change it to a forward slash :-( */ | |
68dc0745 | 976 | case '\\': |
e1dbac94 GS |
977 | strncpy(t, path, l-1); |
978 | t[l - 1] = '/'; | |
979 | t[l] = '\0'; | |
980 | path = t; | |
981 | break; | |
23db2e2d | 982 | /* FindFirstFile() is buggy with "x:", so add a dot :-( */ |
e1dbac94 GS |
983 | case ':': |
984 | if (l == 2 && isALPHA(path[0])) { | |
23db2e2d | 985 | t[0] = path[0]; t[1] = ':'; t[2] = '.'; t[3] = '\0'; |
e1dbac94 GS |
986 | l = 3; |
987 | path = t; | |
988 | } | |
989 | break; | |
68dc0745 | 990 | } |
991 | } | |
6b980173 JD |
992 | |
993 | /* We *must* open & close the file once; otherwise file attribute changes */ | |
994 | /* might not yet have propagated to "other" hard links of the same file. */ | |
995 | /* This also gives us an opportunity to determine the number of links. */ | |
7fac1903 | 996 | if (USING_WIDE()) { |
0cb96387 | 997 | A2WHELPER(path, wbuffer, sizeof(wbuffer)); |
6b980173 | 998 | handle = CreateFileW(wbuffer, 0, 0, NULL, OPEN_EXISTING, 0, NULL); |
7fac1903 GS |
999 | } |
1000 | else { | |
6b980173 JD |
1001 | handle = CreateFileA(path, 0, 0, NULL, OPEN_EXISTING, 0, NULL); |
1002 | } | |
1003 | if (handle != INVALID_HANDLE_VALUE) { | |
1004 | BY_HANDLE_FILE_INFORMATION bhi; | |
1005 | if (GetFileInformationByHandle(handle, &bhi)) | |
1006 | nlink = bhi.nNumberOfLinks; | |
1007 | CloseHandle(handle); | |
7fac1903 | 1008 | } |
6b980173 JD |
1009 | |
1010 | if (USING_WIDE()) | |
1011 | res = _wstat(wbuffer, (struct _stat *)buffer); | |
1012 | else | |
1013 | res = stat(path, buffer); | |
1014 | buffer->st_nlink = nlink; | |
1015 | ||
24caa93f GS |
1016 | if (res < 0) { |
1017 | /* CRT is buggy on sharenames, so make sure it really isn't. | |
1018 | * XXX using GetFileAttributesEx() will enable us to set | |
1019 | * buffer->st_*time (but note that's not available on the | |
1020 | * Windows of 1995) */ | |
7fac1903 GS |
1021 | DWORD r; |
1022 | if (USING_WIDE()) { | |
1023 | r = GetFileAttributesW(wbuffer); | |
1024 | } | |
1025 | else { | |
1026 | r = GetFileAttributesA(path); | |
1027 | } | |
24caa93f | 1028 | if (r != 0xffffffff && (r & FILE_ATTRIBUTE_DIRECTORY)) { |
e1dbac94 GS |
1029 | /* buffer may still contain old garbage since stat() failed */ |
1030 | Zero(buffer, 1, struct stat); | |
1031 | buffer->st_mode = S_IFDIR | S_IREAD; | |
24caa93f GS |
1032 | errno = 0; |
1033 | if (!(r & FILE_ATTRIBUTE_READONLY)) | |
1034 | buffer->st_mode |= S_IWRITE | S_IEXEC; | |
1035 | return 0; | |
1036 | } | |
1037 | } | |
24caa93f | 1038 | else { |
e1dbac94 GS |
1039 | if (l == 3 && isALPHA(path[0]) && path[1] == ':' |
1040 | && (path[2] == '\\' || path[2] == '/')) | |
2293b0e9 AB |
1041 | { |
1042 | /* The drive can be inaccessible, some _stat()s are buggy */ | |
7fac1903 GS |
1043 | if (USING_WIDE() |
1044 | ? !GetVolumeInformationW(wbuffer,NULL,0,NULL,NULL,NULL,NULL,0) | |
1045 | : !GetVolumeInformationA(path,NULL,0,NULL,NULL,NULL,NULL,0)) { | |
2293b0e9 AB |
1046 | errno = ENOENT; |
1047 | return -1; | |
1048 | } | |
1049 | } | |
1050 | #ifdef __BORLANDC__ | |
67fbe06e GS |
1051 | if (S_ISDIR(buffer->st_mode)) |
1052 | buffer->st_mode |= S_IWRITE | S_IEXEC; | |
1053 | else if (S_ISREG(buffer->st_mode)) { | |
1054 | if (l >= 4 && path[l-4] == '.') { | |
1055 | const char *e = path + l - 3; | |
1056 | if (strnicmp(e,"exe",3) | |
1057 | && strnicmp(e,"bat",3) | |
1058 | && strnicmp(e,"com",3) | |
1059 | && (IsWin95() || strnicmp(e,"cmd",3))) | |
1060 | buffer->st_mode &= ~S_IEXEC; | |
1061 | else | |
1062 | buffer->st_mode |= S_IEXEC; | |
1063 | } | |
1064 | else | |
1065 | buffer->st_mode &= ~S_IEXEC; | |
1066 | } | |
67fbe06e | 1067 | #endif |
2293b0e9 | 1068 | } |
67fbe06e | 1069 | return res; |
0a753a76 | 1070 | } |
1071 | ||
8ac9c18d GS |
1072 | /* Find the longname of a given path. path is destructively modified. |
1073 | * It should have space for at least MAX_PATH characters. */ | |
1074 | DllExport char * | |
1075 | win32_longpath(char *path) | |
1076 | { | |
1077 | WIN32_FIND_DATA fdata; | |
1078 | HANDLE fhand; | |
1079 | char tmpbuf[MAX_PATH+1]; | |
1080 | char *tmpstart = tmpbuf; | |
1081 | char *start = path; | |
1082 | char sep; | |
1083 | if (!path) | |
1084 | return Nullch; | |
1085 | ||
1086 | /* drive prefix */ | |
1087 | if (isALPHA(path[0]) && path[1] == ':' && | |
1088 | (path[2] == '/' || path[2] == '\\')) | |
1089 | { | |
1090 | start = path + 2; | |
1091 | *tmpstart++ = path[0]; | |
1092 | *tmpstart++ = ':'; | |
1093 | } | |
1094 | /* UNC prefix */ | |
1095 | else if ((path[0] == '/' || path[0] == '\\') && | |
1096 | (path[1] == '/' || path[1] == '\\')) | |
1097 | { | |
1098 | start = path + 2; | |
52fcf7ee GS |
1099 | *tmpstart++ = path[0]; |
1100 | *tmpstart++ = path[1]; | |
8ac9c18d GS |
1101 | /* copy machine name */ |
1102 | while (*start && *start != '/' && *start != '\\') | |
1103 | *tmpstart++ = *start++; | |
1104 | if (*start) { | |
52fcf7ee | 1105 | *tmpstart++ = *start; |
8ac9c18d GS |
1106 | start++; |
1107 | /* copy share name */ | |
1108 | while (*start && *start != '/' && *start != '\\') | |
1109 | *tmpstart++ = *start++; | |
1110 | } | |
1111 | } | |
1112 | sep = *start++; | |
1113 | if (sep == '/' || sep == '\\') | |
52fcf7ee | 1114 | *tmpstart++ = sep; |
8ac9c18d GS |
1115 | *tmpstart = '\0'; |
1116 | while (sep) { | |
1117 | /* walk up to slash */ | |
1118 | while (*start && *start != '/' && *start != '\\') | |
1119 | ++start; | |
1120 | ||
1121 | /* discard doubled slashes */ | |
1122 | while (*start && (start[1] == '/' || start[1] == '\\')) | |
1123 | ++start; | |
1124 | sep = *start; | |
1125 | ||
1126 | /* stop and find full name of component */ | |
1127 | *start = '\0'; | |
1128 | fhand = FindFirstFile(path,&fdata); | |
1129 | if (fhand != INVALID_HANDLE_VALUE) { | |
1130 | strcpy(tmpstart, fdata.cFileName); | |
1131 | tmpstart += strlen(fdata.cFileName); | |
1132 | if (sep) | |
52fcf7ee | 1133 | *tmpstart++ = sep; |
8ac9c18d GS |
1134 | *tmpstart = '\0'; |
1135 | *start++ = sep; | |
1136 | FindClose(fhand); | |
1137 | } | |
1138 | else { | |
1139 | /* failed a step, just return without side effects */ | |
bf49b057 | 1140 | /*PerlIO_printf(Perl_debug_log, "Failed to find %s\n", path);*/ |
8ac9c18d GS |
1141 | *start = sep; |
1142 | return Nullch; | |
1143 | } | |
1144 | } | |
1145 | strcpy(path,tmpbuf); | |
1146 | return path; | |
1147 | } | |
1148 | ||
0551aaa8 GS |
1149 | #ifndef USE_WIN32_RTL_ENV |
1150 | ||
1151 | DllExport char * | |
1152 | win32_getenv(const char *name) | |
1153 | { | |
c5be433b | 1154 | dTHXo; |
7fac1903 | 1155 | WCHAR wBuffer[MAX_PATH]; |
0551aaa8 | 1156 | DWORD needlen; |
51371543 | 1157 | SV *curitem = Nullsv; |
58a50f62 | 1158 | |
7fac1903 | 1159 | if (USING_WIDE()) { |
0cb96387 | 1160 | A2WHELPER(name, wBuffer, sizeof(wBuffer)); |
51371543 | 1161 | needlen = GetEnvironmentVariableW(wBuffer, NULL, 0); |
7fac1903 GS |
1162 | } |
1163 | else | |
51371543 | 1164 | needlen = GetEnvironmentVariableA(name,NULL,0); |
58a50f62 | 1165 | if (needlen != 0) { |
51371543 | 1166 | curitem = sv_2mortal(newSVpvn("", 0)); |
7fac1903 | 1167 | if (USING_WIDE()) { |
51371543 GS |
1168 | SV *acuritem; |
1169 | do { | |
1170 | SvGROW(curitem, (needlen+1)*sizeof(WCHAR)); | |
1171 | needlen = GetEnvironmentVariableW(wBuffer, | |
1172 | (WCHAR*)SvPVX(curitem), | |
1173 | needlen); | |
1174 | } while (needlen >= SvLEN(curitem)/sizeof(WCHAR)); | |
c5be433b | 1175 | SvCUR_set(curitem, (needlen*sizeof(WCHAR))+1); |
51371543 GS |
1176 | acuritem = sv_2mortal(newSVsv(curitem)); |
1177 | W2AHELPER((WCHAR*)SvPVX(acuritem), SvPVX(curitem), SvCUR(curitem)); | |
7fac1903 GS |
1178 | } |
1179 | else { | |
51371543 GS |
1180 | do { |
1181 | SvGROW(curitem, needlen+1); | |
1182 | needlen = GetEnvironmentVariableA(name,SvPVX(curitem), | |
1183 | needlen); | |
1184 | } while (needlen >= SvLEN(curitem)); | |
1185 | SvCUR_set(curitem, needlen); | |
58a50f62 | 1186 | } |
0551aaa8 | 1187 | } |
c934e9d4 | 1188 | else { |
7a5f8e82 | 1189 | /* allow any environment variables that begin with 'PERL' |
c934e9d4 | 1190 | to be stored in the registry */ |
51371543 | 1191 | if (strncmp(name, "PERL", 4) == 0) |
c5be433b | 1192 | (void)get_regstr(name, &curitem); |
c69f6586 | 1193 | } |
51371543 GS |
1194 | if (curitem && SvCUR(curitem)) |
1195 | return SvPVX(curitem); | |
58a50f62 | 1196 | |
51371543 | 1197 | return Nullch; |
0551aaa8 GS |
1198 | } |
1199 | ||
ac5c734f GS |
1200 | DllExport int |
1201 | win32_putenv(const char *name) | |
1202 | { | |
c5be433b | 1203 | dTHXo; |
ac5c734f GS |
1204 | char* curitem; |
1205 | char* val; | |
7fac1903 GS |
1206 | WCHAR* wCuritem; |
1207 | WCHAR* wVal; | |
1208 | int length, relval = -1; | |
51371543 | 1209 | |
73c4f7a1 | 1210 | if (name) { |
7fac1903 GS |
1211 | if (USING_WIDE()) { |
1212 | length = strlen(name)+1; | |
1213 | New(1309,wCuritem,length,WCHAR); | |
c5be433b | 1214 | A2WHELPER(name, wCuritem, length*sizeof(WCHAR)); |
7fac1903 GS |
1215 | wVal = wcschr(wCuritem, '='); |
1216 | if(wVal) { | |
1217 | *wVal++ = '\0'; | |
1218 | if(SetEnvironmentVariableW(wCuritem, *wVal ? wVal : NULL)) | |
1219 | relval = 0; | |
1220 | } | |
1221 | Safefree(wCuritem); | |
1222 | } | |
1223 | else { | |
1224 | New(1309,curitem,strlen(name)+1,char); | |
1225 | strcpy(curitem, name); | |
1226 | val = strchr(curitem, '='); | |
1227 | if(val) { | |
1228 | /* The sane way to deal with the environment. | |
1229 | * Has these advantages over putenv() & co.: | |
1230 | * * enables us to store a truly empty value in the | |
1231 | * environment (like in UNIX). | |
1232 | * * we don't have to deal with RTL globals, bugs and leaks. | |
1233 | * * Much faster. | |
1234 | * Why you may want to enable USE_WIN32_RTL_ENV: | |
1235 | * * environ[] and RTL functions will not reflect changes, | |
1236 | * which might be an issue if extensions want to access | |
1237 | * the env. via RTL. This cuts both ways, since RTL will | |
1238 | * not see changes made by extensions that call the Win32 | |
1239 | * functions directly, either. | |
1240 | * GSAR 97-06-07 | |
1241 | */ | |
1242 | *val++ = '\0'; | |
1243 | if(SetEnvironmentVariableA(curitem, *val ? val : NULL)) | |
1244 | relval = 0; | |
1245 | } | |
1246 | Safefree(curitem); | |
ac5c734f | 1247 | } |
ac5c734f GS |
1248 | } |
1249 | return relval; | |
1250 | } | |
1251 | ||
0551aaa8 GS |
1252 | #endif |
1253 | ||
d55594ae | 1254 | static long |
2d7a9237 | 1255 | filetime_to_clock(PFILETIME ft) |
d55594ae GS |
1256 | { |
1257 | __int64 qw = ft->dwHighDateTime; | |
1258 | qw <<= 32; | |
1259 | qw |= ft->dwLowDateTime; | |
1260 | qw /= 10000; /* File time ticks at 0.1uS, clock at 1mS */ | |
1261 | return (long) qw; | |
1262 | } | |
1263 | ||
f3986ebb GS |
1264 | DllExport int |
1265 | win32_times(struct tms *timebuf) | |
0a753a76 | 1266 | { |
d55594ae GS |
1267 | FILETIME user; |
1268 | FILETIME kernel; | |
1269 | FILETIME dummy; | |
1270 | if (GetProcessTimes(GetCurrentProcess(), &dummy, &dummy, | |
1271 | &kernel,&user)) { | |
2d7a9237 GS |
1272 | timebuf->tms_utime = filetime_to_clock(&user); |
1273 | timebuf->tms_stime = filetime_to_clock(&kernel); | |
d55594ae GS |
1274 | timebuf->tms_cutime = 0; |
1275 | timebuf->tms_cstime = 0; | |
1276 | ||
1277 | } else { | |
1278 | /* That failed - e.g. Win95 fallback to clock() */ | |
1279 | clock_t t = clock(); | |
1280 | timebuf->tms_utime = t; | |
1281 | timebuf->tms_stime = 0; | |
1282 | timebuf->tms_cutime = 0; | |
1283 | timebuf->tms_cstime = 0; | |
1284 | } | |
68dc0745 | 1285 | return 0; |
0a753a76 | 1286 | } |
1287 | ||
9c51cf4c | 1288 | /* fix utime() so it works on directories in NT */ |
ad0751ec GS |
1289 | static BOOL |
1290 | filetime_from_time(PFILETIME pFileTime, time_t Time) | |
1291 | { | |
9c51cf4c | 1292 | struct tm *pTM = localtime(&Time); |
ad0751ec | 1293 | SYSTEMTIME SystemTime; |
9c51cf4c | 1294 | FILETIME LocalTime; |
ad0751ec GS |
1295 | |
1296 | if (pTM == NULL) | |
1297 | return FALSE; | |
1298 | ||
1299 | SystemTime.wYear = pTM->tm_year + 1900; | |
1300 | SystemTime.wMonth = pTM->tm_mon + 1; | |
1301 | SystemTime.wDay = pTM->tm_mday; | |
1302 | SystemTime.wHour = pTM->tm_hour; | |
1303 | SystemTime.wMinute = pTM->tm_min; | |
1304 | SystemTime.wSecond = pTM->tm_sec; | |
1305 | SystemTime.wMilliseconds = 0; | |
1306 | ||
9c51cf4c GS |
1307 | return SystemTimeToFileTime(&SystemTime, &LocalTime) && |
1308 | LocalFileTimeToFileTime(&LocalTime, pFileTime); | |
ad0751ec GS |
1309 | } |
1310 | ||
1311 | DllExport int | |
3b405fc5 | 1312 | win32_utime(const char *filename, struct utimbuf *times) |
ad0751ec | 1313 | { |
c5be433b | 1314 | dTHXo; |
ad0751ec GS |
1315 | HANDLE handle; |
1316 | FILETIME ftCreate; | |
1317 | FILETIME ftAccess; | |
1318 | FILETIME ftWrite; | |
1319 | struct utimbuf TimeBuffer; | |
7fac1903 | 1320 | WCHAR wbuffer[MAX_PATH]; |
ad0751ec | 1321 | |
7fac1903 GS |
1322 | int rc; |
1323 | if (USING_WIDE()) { | |
0cb96387 | 1324 | A2WHELPER(filename, wbuffer, sizeof(wbuffer)); |
7fac1903 GS |
1325 | rc = _wutime(wbuffer, (struct _utimbuf*)times); |
1326 | } | |
1327 | else { | |
1328 | rc = utime(filename, times); | |
1329 | } | |
ad0751ec GS |
1330 | /* EACCES: path specifies directory or readonly file */ |
1331 | if (rc == 0 || errno != EACCES /* || !IsWinNT() */) | |
1332 | return rc; | |
1333 | ||
1334 | if (times == NULL) { | |
1335 | times = &TimeBuffer; | |
1336 | time(×->actime); | |
1337 | times->modtime = times->actime; | |
1338 | } | |
1339 | ||
1340 | /* This will (and should) still fail on readonly files */ | |
7fac1903 GS |
1341 | if (USING_WIDE()) { |
1342 | handle = CreateFileW(wbuffer, GENERIC_READ | GENERIC_WRITE, | |
1343 | FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, | |
1344 | OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | |
1345 | } | |
1346 | else { | |
1347 | handle = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, | |
1348 | FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, | |
1349 | OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | |
1350 | } | |
ad0751ec GS |
1351 | if (handle == INVALID_HANDLE_VALUE) |
1352 | return rc; | |
1353 | ||
1354 | if (GetFileTime(handle, &ftCreate, &ftAccess, &ftWrite) && | |
1355 | filetime_from_time(&ftAccess, times->actime) && | |
1356 | filetime_from_time(&ftWrite, times->modtime) && | |
1357 | SetFileTime(handle, &ftCreate, &ftAccess, &ftWrite)) | |
1358 | { | |
1359 | rc = 0; | |
1360 | } | |
1361 | ||
1362 | CloseHandle(handle); | |
1363 | return rc; | |
1364 | } | |
1365 | ||
2d7a9237 | 1366 | DllExport int |
b2af26b1 GS |
1367 | win32_uname(struct utsname *name) |
1368 | { | |
1369 | struct hostent *hep; | |
1370 | STRLEN nodemax = sizeof(name->nodename)-1; | |
1371 | OSVERSIONINFO osver; | |
1372 | ||
1373 | memset(&osver, 0, sizeof(OSVERSIONINFO)); | |
1374 | osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
1375 | if (GetVersionEx(&osver)) { | |
1376 | /* sysname */ | |
1377 | switch (osver.dwPlatformId) { | |
1378 | case VER_PLATFORM_WIN32_WINDOWS: | |
1379 | strcpy(name->sysname, "Windows"); | |
1380 | break; | |
1381 | case VER_PLATFORM_WIN32_NT: | |
1382 | strcpy(name->sysname, "Windows NT"); | |
1383 | break; | |
1384 | case VER_PLATFORM_WIN32s: | |
1385 | strcpy(name->sysname, "Win32s"); | |
1386 | break; | |
1387 | default: | |
1388 | strcpy(name->sysname, "Win32 Unknown"); | |
1389 | break; | |
1390 | } | |
1391 | ||
cf6cacac GS |
1392 | /* release */ |
1393 | sprintf(name->release, "%d.%d", | |
b2af26b1 GS |
1394 | osver.dwMajorVersion, osver.dwMinorVersion); |
1395 | ||
cf6cacac GS |
1396 | /* version */ |
1397 | sprintf(name->version, "Build %d", | |
b2af26b1 GS |
1398 | osver.dwPlatformId == VER_PLATFORM_WIN32_NT |
1399 | ? osver.dwBuildNumber : (osver.dwBuildNumber & 0xffff)); | |
1400 | if (osver.szCSDVersion[0]) { | |
cf6cacac | 1401 | char *buf = name->version + strlen(name->version); |
b2af26b1 GS |
1402 | sprintf(buf, " (%s)", osver.szCSDVersion); |
1403 | } | |
1404 | } | |
1405 | else { | |
1406 | *name->sysname = '\0'; | |
1407 | *name->version = '\0'; | |
1408 | *name->release = '\0'; | |
1409 | } | |
1410 | ||
1411 | /* nodename */ | |
1412 | hep = win32_gethostbyname("localhost"); | |
1413 | if (hep) { | |
1414 | STRLEN len = strlen(hep->h_name); | |
1415 | if (len <= nodemax) { | |
1416 | strcpy(name->nodename, hep->h_name); | |
1417 | } | |
1418 | else { | |
1419 | strncpy(name->nodename, hep->h_name, nodemax); | |
1420 | name->nodename[nodemax] = '\0'; | |
1421 | } | |
1422 | } | |
1423 | else { | |
1424 | DWORD sz = nodemax; | |
1425 | if (!GetComputerName(name->nodename, &sz)) | |
1426 | *name->nodename = '\0'; | |
1427 | } | |
1428 | ||
1429 | /* machine (architecture) */ | |
1430 | { | |
1431 | SYSTEM_INFO info; | |
1432 | char *arch; | |
1433 | GetSystemInfo(&info); | |
a6c40364 | 1434 | |
2d63fa08 | 1435 | #if defined(__BORLANDC__) || defined(__MINGW32__) |
a6c40364 GS |
1436 | switch (info.u.s.wProcessorArchitecture) { |
1437 | #else | |
b2af26b1 | 1438 | switch (info.wProcessorArchitecture) { |
a6c40364 | 1439 | #endif |
b2af26b1 GS |
1440 | case PROCESSOR_ARCHITECTURE_INTEL: |
1441 | arch = "x86"; break; | |
1442 | case PROCESSOR_ARCHITECTURE_MIPS: | |
1443 | arch = "mips"; break; | |
1444 | case PROCESSOR_ARCHITECTURE_ALPHA: | |
1445 | arch = "alpha"; break; | |
1446 | case PROCESSOR_ARCHITECTURE_PPC: | |
1447 | arch = "ppc"; break; | |
1448 | default: | |
1449 | arch = "unknown"; break; | |
1450 | } | |
1451 | strcpy(name->machine, arch); | |
1452 | } | |
1453 | return 0; | |
1454 | } | |
1455 | ||
1456 | DllExport int | |
f55ee38a GS |
1457 | win32_waitpid(int pid, int *status, int flags) |
1458 | { | |
c5be433b | 1459 | dTHXo; |
0aaad0ff | 1460 | int retval = -1; |
f55ee38a | 1461 | if (pid == -1) |
0aaad0ff | 1462 | return win32_wait(status); |
f55ee38a | 1463 | else { |
0aaad0ff GS |
1464 | long child = find_pid(pid); |
1465 | if (child >= 0) { | |
1466 | HANDLE hProcess = w32_child_handles[child]; | |
1467 | DWORD waitcode = WaitForSingleObject(hProcess, INFINITE); | |
1468 | if (waitcode != WAIT_FAILED) { | |
1469 | if (GetExitCodeProcess(hProcess, &waitcode)) { | |
1470 | *status = (int)((waitcode & 0xff) << 8); | |
1471 | retval = (int)w32_child_pids[child]; | |
1472 | remove_dead_process(child); | |
1473 | return retval; | |
1474 | } | |
1475 | } | |
1476 | else | |
1477 | errno = ECHILD; | |
1478 | } | |
1479 | else { | |
1480 | retval = cwait(status, pid, WAIT_CHILD); | |
1481 | /* cwait() returns "correctly" on Borland */ | |
8f1e745d | 1482 | #ifndef __BORLANDC__ |
0aaad0ff GS |
1483 | if (status) |
1484 | *status *= 256; | |
f55ee38a | 1485 | #endif |
0aaad0ff | 1486 | } |
f55ee38a | 1487 | } |
0aaad0ff | 1488 | return retval >= 0 ? pid : retval; |
f55ee38a GS |
1489 | } |
1490 | ||
1491 | DllExport int | |
2d7a9237 GS |
1492 | win32_wait(int *status) |
1493 | { | |
2d7a9237 GS |
1494 | /* XXX this wait emulation only knows about processes |
1495 | * spawned via win32_spawnvp(P_NOWAIT, ...). | |
1496 | */ | |
c5be433b | 1497 | dTHXo; |
2d7a9237 GS |
1498 | int i, retval; |
1499 | DWORD exitcode, waitcode; | |
1500 | ||
1501 | if (!w32_num_children) { | |
1502 | errno = ECHILD; | |
1503 | return -1; | |
1504 | } | |
1505 | ||
1506 | /* if a child exists, wait for it to die */ | |
1507 | waitcode = WaitForMultipleObjects(w32_num_children, | |
0aaad0ff | 1508 | w32_child_handles, |
2d7a9237 GS |
1509 | FALSE, |
1510 | INFINITE); | |
1511 | if (waitcode != WAIT_FAILED) { | |
1512 | if (waitcode >= WAIT_ABANDONED_0 | |
1513 | && waitcode < WAIT_ABANDONED_0 + w32_num_children) | |
1514 | i = waitcode - WAIT_ABANDONED_0; | |
1515 | else | |
1516 | i = waitcode - WAIT_OBJECT_0; | |
0aaad0ff | 1517 | if (GetExitCodeProcess(w32_child_handles[i], &exitcode) ) { |
2d7a9237 GS |
1518 | *status = (int)((exitcode & 0xff) << 8); |
1519 | retval = (int)w32_child_pids[i]; | |
0aaad0ff | 1520 | remove_dead_process(i); |
2d7a9237 GS |
1521 | return retval; |
1522 | } | |
1523 | } | |
1524 | ||
1525 | FAILED: | |
1526 | errno = GetLastError(); | |
1527 | return -1; | |
2d7a9237 | 1528 | } |
d55594ae | 1529 | |
2d7a9237 | 1530 | static UINT timerid = 0; |
d55594ae GS |
1531 | |
1532 | static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time) | |
1533 | { | |
c5be433b | 1534 | dTHXo; |
0cb96387 GS |
1535 | KillTimer(NULL,timerid); |
1536 | timerid=0; | |
1537 | sighandler(14); | |
d55594ae GS |
1538 | } |
1539 | ||
f3986ebb GS |
1540 | DllExport unsigned int |
1541 | win32_alarm(unsigned int sec) | |
0a753a76 | 1542 | { |
d55594ae GS |
1543 | /* |
1544 | * the 'obvious' implentation is SetTimer() with a callback | |
1545 | * which does whatever receiving SIGALRM would do | |
1546 | * we cannot use SIGALRM even via raise() as it is not | |
1547 | * one of the supported codes in <signal.h> | |
1548 | * | |
1549 | * Snag is unless something is looking at the message queue | |
1550 | * nothing happens :-( | |
1551 | */ | |
c5be433b | 1552 | dTHXo; |
d55594ae GS |
1553 | if (sec) |
1554 | { | |
1555 | timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc); | |
1556 | if (!timerid) | |
4f63d024 | 1557 | Perl_croak_nocontext("Cannot set timer"); |
d55594ae GS |
1558 | } |
1559 | else | |
1560 | { | |
1561 | if (timerid) | |
1562 | { | |
1563 | KillTimer(NULL,timerid); | |
1564 | timerid=0; | |
1565 | } | |
1566 | } | |
68dc0745 | 1567 | return 0; |
0a753a76 | 1568 | } |
1569 | ||
26618a56 | 1570 | #ifdef HAVE_DES_FCRYPT |
2d77217b | 1571 | extern char * des_fcrypt(const char *txt, const char *salt, char *cbuf); |
ff95b63e | 1572 | #endif |
26618a56 GS |
1573 | |
1574 | DllExport char * | |
1575 | win32_crypt(const char *txt, const char *salt) | |
1576 | { | |
0b94c7bb | 1577 | dTHXo; |
ff95b63e | 1578 | #ifdef HAVE_DES_FCRYPT |
26618a56 | 1579 | dTHR; |
3352bfcb | 1580 | return des_fcrypt(txt, salt, w32_crypt_buffer); |
ff95b63e | 1581 | #else |
25dbdbbc | 1582 | Perl_croak(aTHX_ "The crypt() function is unimplemented due to excessive paranoia."); |
b8957cf1 | 1583 | return Nullch; |
ff95b63e | 1584 | #endif |
26618a56 | 1585 | } |
26618a56 | 1586 | |
f3986ebb | 1587 | #ifdef USE_FIXED_OSFHANDLE |
390b85e7 GS |
1588 | |
1589 | EXTERN_C int __cdecl _alloc_osfhnd(void); | |
1590 | EXTERN_C int __cdecl _set_osfhnd(int fh, long value); | |
1591 | EXTERN_C void __cdecl _lock_fhandle(int); | |
1592 | EXTERN_C void __cdecl _unlock_fhandle(int); | |
1593 | EXTERN_C void __cdecl _unlock(int); | |
1594 | ||
1595 | #if (_MSC_VER >= 1000) | |
1596 | typedef struct { | |
1597 | long osfhnd; /* underlying OS file HANDLE */ | |
1598 | char osfile; /* attributes of file (e.g., open in text mode?) */ | |
1599 | char pipech; /* one char buffer for handles opened on pipes */ | |
1600 | #if defined (_MT) && !defined (DLL_FOR_WIN32S) | |
1601 | int lockinitflag; | |
1602 | CRITICAL_SECTION lock; | |
1603 | #endif /* defined (_MT) && !defined (DLL_FOR_WIN32S) */ | |
1604 | } ioinfo; | |
1605 | ||
1606 | EXTERN_C ioinfo * __pioinfo[]; | |
1607 | ||
1608 | #define IOINFO_L2E 5 | |
1609 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) | |
1610 | #define _pioinfo(i) (__pioinfo[i >> IOINFO_L2E] + (i & (IOINFO_ARRAY_ELTS - 1))) | |
1611 | #define _osfile(i) (_pioinfo(i)->osfile) | |
1612 | ||
1613 | #else /* (_MSC_VER >= 1000) */ | |
1614 | extern char _osfile[]; | |
1615 | #endif /* (_MSC_VER >= 1000) */ | |
1616 | ||
1617 | #define FOPEN 0x01 /* file handle open */ | |
1618 | #define FAPPEND 0x20 /* file handle opened O_APPEND */ | |
1619 | #define FDEV 0x40 /* file handle refers to device */ | |
1620 | #define FTEXT 0x80 /* file handle is in text mode */ | |
1621 | ||
1622 | #define _STREAM_LOCKS 26 /* Table of stream locks */ | |
1623 | #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_NSTREAM_-1) /* Last stream lock */ | |
1624 | #define _FH_LOCKS (_LAST_STREAM_LOCK+1) /* Table of fh locks */ | |
1625 | ||
1626 | /*** | |
1627 | *int my_open_osfhandle(long osfhandle, int flags) - open C Runtime file handle | |
1628 | * | |
1629 | *Purpose: | |
1630 | * This function allocates a free C Runtime file handle and associates | |
1631 | * it with the Win32 HANDLE specified by the first parameter. This is a | |
1632 | * temperary fix for WIN95's brain damage GetFileType() error on socket | |
1633 | * we just bypass that call for socket | |
1634 | * | |
1635 | *Entry: | |
1636 | * long osfhandle - Win32 HANDLE to associate with C Runtime file handle. | |
1637 | * int flags - flags to associate with C Runtime file handle. | |
1638 | * | |
1639 | *Exit: | |
1640 | * returns index of entry in fh, if successful | |
1641 | * return -1, if no free entry is found | |
1642 | * | |
1643 | *Exceptions: | |
1644 | * | |
1645 | *******************************************************************************/ | |
1646 | ||
1647 | static int | |
1648 | my_open_osfhandle(long osfhandle, int flags) | |
1649 | { | |
1650 | int fh; | |
1651 | char fileflags; /* _osfile flags */ | |
1652 | ||
1653 | /* copy relevant flags from second parameter */ | |
1654 | fileflags = FDEV; | |
1655 | ||
9404a519 | 1656 | if (flags & O_APPEND) |
390b85e7 GS |
1657 | fileflags |= FAPPEND; |
1658 | ||
9404a519 | 1659 | if (flags & O_TEXT) |
390b85e7 GS |
1660 | fileflags |= FTEXT; |
1661 | ||
1662 | /* attempt to allocate a C Runtime file handle */ | |
9404a519 | 1663 | if ((fh = _alloc_osfhnd()) == -1) { |
390b85e7 GS |
1664 | errno = EMFILE; /* too many open files */ |
1665 | _doserrno = 0L; /* not an OS error */ | |
1666 | return -1; /* return error to caller */ | |
1667 | } | |
1668 | ||
1669 | /* the file is open. now, set the info in _osfhnd array */ | |
1670 | _set_osfhnd(fh, osfhandle); | |
1671 | ||
1672 | fileflags |= FOPEN; /* mark as open */ | |
1673 | ||
1674 | #if (_MSC_VER >= 1000) | |
1675 | _osfile(fh) = fileflags; /* set osfile entry */ | |
1676 | _unlock_fhandle(fh); | |
1677 | #else | |
1678 | _osfile[fh] = fileflags; /* set osfile entry */ | |
1679 | _unlock(fh+_FH_LOCKS); /* unlock handle */ | |
1680 | #endif | |
1681 | ||
1682 | return fh; /* return handle */ | |
1683 | } | |
1684 | ||
1685 | #define _open_osfhandle my_open_osfhandle | |
f3986ebb | 1686 | #endif /* USE_FIXED_OSFHANDLE */ |
390b85e7 GS |
1687 | |
1688 | /* simulate flock by locking a range on the file */ | |
1689 | ||
1690 | #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError())) | |
1691 | #define LK_LEN 0xffff0000 | |
1692 | ||
f3986ebb GS |
1693 | DllExport int |
1694 | win32_flock(int fd, int oper) | |
390b85e7 GS |
1695 | { |
1696 | OVERLAPPED o; | |
1697 | int i = -1; | |
1698 | HANDLE fh; | |
1699 | ||
f3986ebb | 1700 | if (!IsWinNT()) { |
c5be433b | 1701 | dTHXo; |
4f63d024 | 1702 | Perl_croak_nocontext("flock() unimplemented on this platform"); |
f3986ebb GS |
1703 | return -1; |
1704 | } | |
390b85e7 GS |
1705 | fh = (HANDLE)_get_osfhandle(fd); |
1706 | memset(&o, 0, sizeof(o)); | |
1707 | ||
1708 | switch(oper) { | |
1709 | case LOCK_SH: /* shared lock */ | |
1710 | LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i); | |
1711 | break; | |
1712 | case LOCK_EX: /* exclusive lock */ | |
1713 | LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i); | |
1714 | break; | |
1715 | case LOCK_SH|LOCK_NB: /* non-blocking shared lock */ | |
1716 | LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i); | |
1717 | break; | |
1718 | case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */ | |
1719 | LK_ERR(LockFileEx(fh, | |
1720 | LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, | |
1721 | 0, LK_LEN, 0, &o),i); | |
1722 | break; | |
1723 | case LOCK_UN: /* unlock lock */ | |
1724 | LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i); | |
1725 | break; | |
1726 | default: /* unknown */ | |
1727 | errno = EINVAL; | |
1728 | break; | |
1729 | } | |
1730 | return i; | |
1731 | } | |
1732 | ||
1733 | #undef LK_ERR | |
1734 | #undef LK_LEN | |
1735 | ||
68dc0745 | 1736 | /* |
1737 | * redirected io subsystem for all XS modules | |
1738 | * | |
1739 | */ | |
0a753a76 | 1740 | |
68dc0745 | 1741 | DllExport int * |
1742 | win32_errno(void) | |
0a753a76 | 1743 | { |
390b85e7 | 1744 | return (&errno); |
0a753a76 | 1745 | } |
1746 | ||
dcb2879a GS |
1747 | DllExport char *** |
1748 | win32_environ(void) | |
1749 | { | |
390b85e7 | 1750 | return (&(_environ)); |
dcb2879a GS |
1751 | } |
1752 | ||
68dc0745 | 1753 | /* the rest are the remapped stdio routines */ |
1754 | DllExport FILE * | |
1755 | win32_stderr(void) | |
0a753a76 | 1756 | { |
390b85e7 | 1757 | return (stderr); |
0a753a76 | 1758 | } |
1759 | ||
68dc0745 | 1760 | DllExport FILE * |
1761 | win32_stdin(void) | |
0a753a76 | 1762 | { |
390b85e7 | 1763 | return (stdin); |
0a753a76 | 1764 | } |
1765 | ||
68dc0745 | 1766 | DllExport FILE * |
1767 | win32_stdout() | |
0a753a76 | 1768 | { |
390b85e7 | 1769 | return (stdout); |
0a753a76 | 1770 | } |
1771 | ||
68dc0745 | 1772 | DllExport int |
1773 | win32_ferror(FILE *fp) | |
0a753a76 | 1774 | { |
390b85e7 | 1775 | return (ferror(fp)); |
0a753a76 | 1776 | } |
1777 | ||
1778 | ||
68dc0745 | 1779 | DllExport int |
1780 | win32_feof(FILE *fp) | |
0a753a76 | 1781 | { |
390b85e7 | 1782 | return (feof(fp)); |
0a753a76 | 1783 | } |
1784 | ||
68dc0745 | 1785 | /* |
1786 | * Since the errors returned by the socket error function | |
1787 | * WSAGetLastError() are not known by the library routine strerror | |
1788 | * we have to roll our own. | |
1789 | */ | |
0a753a76 | 1790 | |
68dc0745 | 1791 | DllExport char * |
1792 | win32_strerror(int e) | |
0a753a76 | 1793 | { |
3e3baf6d | 1794 | #ifndef __BORLANDC__ /* Borland intolerance */ |
68dc0745 | 1795 | extern int sys_nerr; |
3e3baf6d | 1796 | #endif |
68dc0745 | 1797 | DWORD source = 0; |
0a753a76 | 1798 | |
9404a519 | 1799 | if (e < 0 || e > sys_nerr) { |
c5be433b | 1800 | dTHXo; |
9404a519 | 1801 | if (e < 0) |
68dc0745 | 1802 | e = GetLastError(); |
0a753a76 | 1803 | |
9404a519 | 1804 | if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0, |
3352bfcb GS |
1805 | w32_strerror_buffer, |
1806 | sizeof(w32_strerror_buffer), NULL) == 0) | |
1807 | strcpy(w32_strerror_buffer, "Unknown Error"); | |
0a753a76 | 1808 | |
3352bfcb | 1809 | return w32_strerror_buffer; |
68dc0745 | 1810 | } |
390b85e7 | 1811 | return strerror(e); |
0a753a76 | 1812 | } |
1813 | ||
22fae026 | 1814 | DllExport void |
c5be433b | 1815 | win32_str_os_error(void *sv, DWORD dwErr) |
22fae026 TM |
1816 | { |
1817 | DWORD dwLen; | |
1818 | char *sMsg; | |
1819 | dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
1820 | |FORMAT_MESSAGE_IGNORE_INSERTS | |
1821 | |FORMAT_MESSAGE_FROM_SYSTEM, NULL, | |
1822 | dwErr, 0, (char *)&sMsg, 1, NULL); | |
2ce77adf | 1823 | /* strip trailing whitespace and period */ |
22fae026 | 1824 | if (0 < dwLen) { |
2ce77adf GS |
1825 | do { |
1826 | --dwLen; /* dwLen doesn't include trailing null */ | |
1827 | } while (0 < dwLen && isSPACE(sMsg[dwLen])); | |
22fae026 TM |
1828 | if ('.' != sMsg[dwLen]) |
1829 | dwLen++; | |
2ce77adf | 1830 | sMsg[dwLen] = '\0'; |
22fae026 TM |
1831 | } |
1832 | if (0 == dwLen) { | |
c69f6586 | 1833 | sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/); |
db7c17d7 GS |
1834 | if (sMsg) |
1835 | dwLen = sprintf(sMsg, | |
1836 | "Unknown error #0x%lX (lookup 0x%lX)", | |
1837 | dwErr, GetLastError()); | |
1838 | } | |
1839 | if (sMsg) { | |
c5be433b | 1840 | dTHXo; |
db7c17d7 GS |
1841 | sv_setpvn((SV*)sv, sMsg, dwLen); |
1842 | LocalFree(sMsg); | |
22fae026 | 1843 | } |
22fae026 TM |
1844 | } |
1845 | ||
1846 | ||
68dc0745 | 1847 | DllExport int |
1848 | win32_fprintf(FILE *fp, const char *format, ...) | |
0a753a76 | 1849 | { |
68dc0745 | 1850 | va_list marker; |
1851 | va_start(marker, format); /* Initialize variable arguments. */ | |
0a753a76 | 1852 | |
390b85e7 | 1853 | return (vfprintf(fp, format, marker)); |
0a753a76 | 1854 | } |
1855 | ||
68dc0745 | 1856 | DllExport int |
1857 | win32_printf(const char *format, ...) | |
0a753a76 | 1858 | { |
68dc0745 | 1859 | va_list marker; |
1860 | va_start(marker, format); /* Initialize variable arguments. */ | |
0a753a76 | 1861 | |
390b85e7 | 1862 | return (vprintf(format, marker)); |
0a753a76 | 1863 | } |
1864 | ||
68dc0745 | 1865 | DllExport int |
1866 | win32_vfprintf(FILE *fp, const char *format, va_list args) | |
0a753a76 | 1867 | { |
390b85e7 | 1868 | return (vfprintf(fp, format, args)); |
0a753a76 | 1869 | } |
1870 | ||
96e4d5b1 | 1871 | DllExport int |
1872 | win32_vprintf(const char *format, va_list args) | |
1873 | { | |
390b85e7 | 1874 | return (vprintf(format, args)); |
96e4d5b1 | 1875 | } |
1876 | ||
68dc0745 | 1877 | DllExport size_t |
1878 | win32_fread(void *buf, size_t size, size_t count, FILE *fp) | |
0a753a76 | 1879 | { |
390b85e7 | 1880 | return fread(buf, size, count, fp); |
0a753a76 | 1881 | } |
1882 | ||
68dc0745 | 1883 | DllExport size_t |
1884 | win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp) | |
0a753a76 | 1885 | { |
390b85e7 | 1886 | return fwrite(buf, size, count, fp); |
0a753a76 | 1887 | } |
1888 | ||
7fac1903 GS |
1889 | #define MODE_SIZE 10 |
1890 | ||
68dc0745 | 1891 | DllExport FILE * |
1892 | win32_fopen(const char *filename, const char *mode) | |
0a753a76 | 1893 | { |
c5be433b | 1894 | dTHXo; |
7fac1903 | 1895 | WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH]; |
c5be433b GS |
1896 | |
1897 | if (!*filename) | |
1898 | return NULL; | |
1899 | ||
68dc0745 | 1900 | if (stricmp(filename, "/dev/null")==0) |
7fac1903 GS |
1901 | filename = "NUL"; |
1902 | ||
1903 | if (USING_WIDE()) { | |
0cb96387 GS |
1904 | A2WHELPER(mode, wMode, sizeof(wMode)); |
1905 | A2WHELPER(filename, wBuffer, sizeof(wBuffer)); | |
7fac1903 GS |
1906 | return _wfopen(wBuffer, wMode); |
1907 | } | |
390b85e7 | 1908 | return fopen(filename, mode); |
0a753a76 | 1909 | } |
1910 | ||
f3986ebb GS |
1911 | #ifndef USE_SOCKETS_AS_HANDLES |
1912 | #undef fdopen | |
1913 | #define fdopen my_fdopen | |
1914 | #endif | |
1915 | ||
68dc0745 | 1916 | DllExport FILE * |
7fac1903 | 1917 | win32_fdopen(int handle, const char *mode) |
0a753a76 | 1918 | { |
c5be433b | 1919 | dTHXo; |
51371543 | 1920 | WCHAR wMode[MODE_SIZE]; |
7fac1903 | 1921 | if (USING_WIDE()) { |
0cb96387 | 1922 | A2WHELPER(mode, wMode, sizeof(wMode)); |
7fac1903 GS |
1923 | return _wfdopen(handle, wMode); |
1924 | } | |
390b85e7 | 1925 | return fdopen(handle, (char *) mode); |
0a753a76 | 1926 | } |
1927 | ||
68dc0745 | 1928 | DllExport FILE * |
7fac1903 | 1929 | win32_freopen(const char *path, const char *mode, FILE *stream) |
0a753a76 | 1930 | { |
c5be433b | 1931 | dTHXo; |
51371543 | 1932 | WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH]; |
68dc0745 | 1933 | if (stricmp(path, "/dev/null")==0) |
7fac1903 GS |
1934 | path = "NUL"; |
1935 | ||
1936 | if (USING_WIDE()) { | |
0cb96387 GS |
1937 | A2WHELPER(mode, wMode, sizeof(wMode)); |
1938 | A2WHELPER(path, wBuffer, sizeof(wBuffer)); | |
7fac1903 GS |
1939 | return _wfreopen(wBuffer, wMode, stream); |
1940 | } | |
390b85e7 | 1941 | return freopen(path, mode, stream); |
0a753a76 | 1942 | } |
1943 | ||
68dc0745 | 1944 | DllExport int |
1945 | win32_fclose(FILE *pf) | |
0a753a76 | 1946 | { |
f3986ebb | 1947 | return my_fclose(pf); /* defined in win32sck.c */ |
0a753a76 | 1948 | } |
1949 | ||
68dc0745 | 1950 | DllExport int |
1951 | win32_fputs(const char *s,FILE *pf) | |
0a753a76 | 1952 | { |
390b85e7 | 1953 | return fputs(s, pf); |
0a753a76 | 1954 | } |
1955 | ||
68dc0745 | 1956 | DllExport int |
1957 | win32_fputc(int c,FILE *pf) | |
0a753a76 | 1958 | { |
390b85e7 | 1959 | return fputc(c,pf); |
0a753a76 | 1960 | } |
1961 | ||
68dc0745 | 1962 | DllExport int |
1963 | win32_ungetc(int c,FILE *pf) | |
0a753a76 | 1964 | { |
390b85e7 | 1965 | return ungetc(c,pf); |
0a753a76 | 1966 | } |
1967 | ||
68dc0745 | 1968 | DllExport int |
1969 | win32_getc(FILE *pf) | |
0a753a76 | 1970 | { |
390b85e7 | 1971 | return getc(pf); |
0a753a76 | 1972 | } |
1973 | ||
68dc0745 | 1974 | DllExport int |
1975 | win32_fileno(FILE *pf) | |
0a753a76 | 1976 | { |
390b85e7 | 1977 | return fileno(pf); |
0a753a76 | 1978 | } |
1979 | ||
68dc0745 | 1980 | DllExport void |
1981 | win32_clearerr(FILE *pf) | |
0a753a76 | 1982 | { |
390b85e7 | 1983 | clearerr(pf); |
68dc0745 | 1984 | return; |
0a753a76 | 1985 | } |
1986 | ||
68dc0745 | 1987 | DllExport int |
1988 | win32_fflush(FILE *pf) | |
0a753a76 | 1989 | { |
390b85e7 | 1990 | return fflush(pf); |
0a753a76 | 1991 | } |
1992 | ||
68dc0745 | 1993 | DllExport long |
1994 | win32_ftell(FILE *pf) | |
0a753a76 | 1995 | { |
390b85e7 | 1996 | return ftell(pf); |
0a753a76 | 1997 | } |
1998 | ||
68dc0745 | 1999 | DllExport int |
2000 | win32_fseek(FILE *pf,long offset,int origin) | |
0a753a76 | 2001 | { |
390b85e7 | 2002 | return fseek(pf, offset, origin); |
0a753a76 | 2003 | } |
2004 | ||
68dc0745 | 2005 | DllExport int |
2006 | win32_fgetpos(FILE *pf,fpos_t *p) | |
0a753a76 | 2007 | { |
390b85e7 | 2008 | return fgetpos(pf, p); |
0a753a76 | 2009 | } |
2010 | ||
68dc0745 | 2011 | DllExport int |
2012 | win32_fsetpos(FILE *pf,const fpos_t *p) | |
0a753a76 | 2013 | { |
390b85e7 | 2014 | return fsetpos(pf, p); |
0a753a76 | 2015 | } |
2016 | ||
68dc0745 | 2017 | DllExport void |
2018 | win32_rewind(FILE *pf) | |
0a753a76 | 2019 | { |
390b85e7 | 2020 | rewind(pf); |
68dc0745 | 2021 | return; |
0a753a76 | 2022 | } |
2023 | ||
68dc0745 | 2024 | DllExport FILE* |
2025 | win32_tmpfile(void) | |
0a753a76 | 2026 | { |
390b85e7 | 2027 | return tmpfile(); |
0a753a76 | 2028 | } |
2029 | ||
68dc0745 | 2030 | DllExport void |
2031 | win32_abort(void) | |
0a753a76 | 2032 | { |
390b85e7 | 2033 | abort(); |
68dc0745 | 2034 | return; |
0a753a76 | 2035 | } |
2036 | ||
68dc0745 | 2037 | DllExport int |
22239a37 | 2038 | win32_fstat(int fd,struct stat *sbufptr) |
0a753a76 | 2039 | { |
22239a37 | 2040 | return fstat(fd,sbufptr); |
0a753a76 | 2041 | } |
2042 | ||
68dc0745 | 2043 | DllExport int |
2044 | win32_pipe(int *pfd, unsigned int size, int mode) | |
0a753a76 | 2045 | { |
390b85e7 | 2046 | return _pipe(pfd, size, mode); |
0a753a76 | 2047 | } |
2048 | ||
50892819 GS |
2049 | /* |
2050 | * a popen() clone that respects PERL5SHELL | |
2051 | */ | |
2052 | ||
68dc0745 | 2053 | DllExport FILE* |
2054 | win32_popen(const char *command, const char *mode) | |
0a753a76 | 2055 | { |
4b556e6c | 2056 | #ifdef USE_RTL_POPEN |
390b85e7 | 2057 | return _popen(command, mode); |
50892819 GS |
2058 | #else |
2059 | int p[2]; | |
2060 | int parent, child; | |
2061 | int stdfd, oldfd; | |
2062 | int ourmode; | |
2063 | int childpid; | |
2064 | ||
2065 | /* establish which ends read and write */ | |
2066 | if (strchr(mode,'w')) { | |
2067 | stdfd = 0; /* stdin */ | |
2068 | parent = 1; | |
2069 | child = 0; | |
2070 | } | |
2071 | else if (strchr(mode,'r')) { | |
2072 | stdfd = 1; /* stdout */ | |
2073 | parent = 0; | |
2074 | child = 1; | |
2075 | } | |
2076 | else | |
2077 | return NULL; | |
2078 | ||
2079 | /* set the correct mode */ | |
2080 | if (strchr(mode,'b')) | |
2081 | ourmode = O_BINARY; | |
2082 | else if (strchr(mode,'t')) | |
2083 | ourmode = O_TEXT; | |
2084 | else | |
2085 | ourmode = _fmode & (O_TEXT | O_BINARY); | |
2086 | ||
2087 | /* the child doesn't inherit handles */ | |
2088 | ourmode |= O_NOINHERIT; | |
2089 | ||
2090 | if (win32_pipe( p, 512, ourmode) == -1) | |
2091 | return NULL; | |
2092 | ||
2093 | /* save current stdfd */ | |
2094 | if ((oldfd = win32_dup(stdfd)) == -1) | |
2095 | goto cleanup; | |
2096 | ||
2097 | /* make stdfd go to child end of pipe (implicitly closes stdfd) */ | |
2098 | /* stdfd will be inherited by the child */ | |
2099 | if (win32_dup2(p[child], stdfd) == -1) | |
2100 | goto cleanup; | |
2101 | ||
2102 | /* close the child end in parent */ | |
2103 | win32_close(p[child]); | |
2104 | ||
2105 | /* start the child */ | |
4f63d024 | 2106 | { |
c5be433b GS |
2107 | dTHXo; |
2108 | if ((childpid = do_spawn_nowait((char*)command)) == -1) | |
4f63d024 | 2109 | goto cleanup; |
50892819 | 2110 | |
4f63d024 GS |
2111 | /* revert stdfd to whatever it was before */ |
2112 | if (win32_dup2(oldfd, stdfd) == -1) | |
2113 | goto cleanup; | |
50892819 | 2114 | |
4f63d024 GS |
2115 | /* close saved handle */ |
2116 | win32_close(oldfd); | |
50892819 | 2117 | |
4f63d024 | 2118 | sv_setiv(*av_fetch(w32_fdpid, p[parent], TRUE), childpid); |
d91d68c1 RS |
2119 | |
2120 | /* set process id so that it can be returned by perl's open() */ | |
2121 | PL_forkprocess = childpid; | |
4f63d024 | 2122 | } |
50892819 GS |
2123 | |
2124 | /* we have an fd, return a file stream */ | |
2125 | return (win32_fdopen(p[parent], (char *)mode)); | |
2126 | ||
2127 | cleanup: | |
2128 | /* we don't need to check for errors here */ | |
2129 | win32_close(p[0]); | |
2130 | win32_close(p[1]); | |
2131 | if (oldfd != -1) { | |
2132 | win32_dup2(oldfd, stdfd); | |
2133 | win32_close(oldfd); | |
2134 | } | |
2135 | return (NULL); | |
2136 | ||
4b556e6c | 2137 | #endif /* USE_RTL_POPEN */ |
0a753a76 | 2138 | } |
2139 | ||
50892819 GS |
2140 | /* |
2141 | * pclose() clone | |
2142 | */ | |
2143 | ||
68dc0745 | 2144 | DllExport int |
2145 | win32_pclose(FILE *pf) | |
0a753a76 | 2146 | { |
4b556e6c | 2147 | #ifdef USE_RTL_POPEN |
390b85e7 | 2148 | return _pclose(pf); |
50892819 | 2149 | #else |
c5be433b | 2150 | dTHXo; |
e17cb2a9 JD |
2151 | int childpid, status; |
2152 | SV *sv; | |
2153 | ||
4b556e6c | 2154 | sv = *av_fetch(w32_fdpid, win32_fileno(pf), TRUE); |
e17cb2a9 JD |
2155 | if (SvIOK(sv)) |
2156 | childpid = SvIVX(sv); | |
2157 | else | |
2158 | childpid = 0; | |
50892819 GS |
2159 | |
2160 | if (!childpid) { | |
2161 | errno = EBADF; | |
2162 | return -1; | |
2163 | } | |
2164 | ||
2165 | win32_fclose(pf); | |
e17cb2a9 JD |
2166 | SvIVX(sv) = 0; |
2167 | ||
0aaad0ff GS |
2168 | if (win32_waitpid(childpid, &status, 0) == -1) |
2169 | return -1; | |
50892819 | 2170 | |
0aaad0ff | 2171 | return status; |
50892819 | 2172 | |
4b556e6c | 2173 | #endif /* USE_RTL_POPEN */ |
0a753a76 | 2174 | } |
6b980173 JD |
2175 | |
2176 | static BOOL WINAPI | |
2177 | Nt4CreateHardLinkW( | |
2178 | LPCWSTR lpFileName, | |
2179 | LPCWSTR lpExistingFileName, | |
2180 | LPSECURITY_ATTRIBUTES lpSecurityAttributes) | |
2181 | { | |
2182 | HANDLE handle; | |
2183 | WCHAR wFullName[MAX_PATH+1]; | |
2184 | LPVOID lpContext = NULL; | |
2185 | WIN32_STREAM_ID StreamId; | |
2186 | DWORD dwSize = (char*)&StreamId.cStreamName - (char*)&StreamId; | |
2187 | DWORD dwWritten; | |
2188 | DWORD dwLen; | |
2189 | BOOL bSuccess; | |
2190 | ||
2191 | BOOL (__stdcall *pfnBackupWrite)(HANDLE, LPBYTE, DWORD, LPDWORD, | |
2192 | BOOL, BOOL, LPVOID*) = | |
2193 | (BOOL (__stdcall *)(HANDLE, LPBYTE, DWORD, LPDWORD, | |
2194 | BOOL, BOOL, LPVOID*)) | |
2195 | GetProcAddress(GetModuleHandle("kernel32.dll"), "BackupWrite"); | |
2196 | if (pfnBackupWrite == NULL) | |
2197 | return 0; | |
2198 | ||
2199 | dwLen = GetFullPathNameW(lpFileName, MAX_PATH, wFullName, NULL); | |
2200 | if (dwLen == 0) | |
2201 | return 0; | |
2202 | dwLen = (dwLen+1)*sizeof(WCHAR); | |
2203 | ||
2204 | handle = CreateFileW(lpExistingFileName, FILE_WRITE_ATTRIBUTES, | |
2205 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | |
2206 | NULL, OPEN_EXISTING, 0, NULL); | |
2207 | if (handle == INVALID_HANDLE_VALUE) | |
2208 | return 0; | |
2209 | ||
2210 | StreamId.dwStreamId = BACKUP_LINK; | |
2211 | StreamId.dwStreamAttributes = 0; | |
2212 | StreamId.dwStreamNameSize = 0; | |
2213 | StreamId.Size.HighPart = 0; | |
2214 | StreamId.Size.LowPart = dwLen; | |
2215 | ||
2216 | bSuccess = pfnBackupWrite(handle, (LPBYTE)&StreamId, dwSize, &dwWritten, | |
2217 | FALSE, FALSE, &lpContext); | |
2218 | if (bSuccess) { | |
2219 | bSuccess = pfnBackupWrite(handle, (LPBYTE)wFullName, dwLen, &dwWritten, | |
2220 | FALSE, FALSE, &lpContext); | |
2221 | pfnBackupWrite(handle, NULL, 0, &dwWritten, TRUE, FALSE, &lpContext); | |
2222 | } | |
2223 | ||
2224 | CloseHandle(handle); | |
2225 | return bSuccess; | |
2226 | } | |
2227 | ||
2228 | DllExport int | |
2229 | win32_link(const char *oldname, const char *newname) | |
2230 | { | |
2231 | dTHXo; | |
2232 | BOOL (__stdcall *pfnCreateHardLinkW)(LPCWSTR,LPCWSTR,LPSECURITY_ATTRIBUTES); | |
2233 | WCHAR wOldName[MAX_PATH]; | |
2234 | WCHAR wNewName[MAX_PATH]; | |
2235 | ||
2236 | if (IsWin95()) | |
2237 | Perl_die(aTHX_ PL_no_func, "link"); | |
2238 | ||
2239 | pfnCreateHardLinkW = | |
2240 | (BOOL (__stdcall *)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES)) | |
2241 | GetProcAddress(GetModuleHandle("kernel32.dll"), "CreateHardLinkW"); | |
2242 | if (pfnCreateHardLinkW == NULL) | |
2243 | pfnCreateHardLinkW = Nt4CreateHardLinkW; | |
2244 | ||
2245 | if ((A2WHELPER(oldname, wOldName, sizeof(wOldName))) && | |
2246 | (A2WHELPER(newname, wNewName, sizeof(wNewName))) && | |
2247 | pfnCreateHardLinkW(wNewName, wOldName, NULL)) | |
2248 | { | |
2249 | return 0; | |
2250 | } | |
2251 | errno = (GetLastError() == ERROR_FILE_NOT_FOUND) ? ENOENT : EINVAL; | |
2252 | return -1; | |
2253 | } | |
0a753a76 | 2254 | |
68dc0745 | 2255 | DllExport int |
8d9b2e3c | 2256 | win32_rename(const char *oname, const char *newname) |
e24c7c18 | 2257 | { |
7fac1903 GS |
2258 | WCHAR wOldName[MAX_PATH]; |
2259 | WCHAR wNewName[MAX_PATH]; | |
2260 | BOOL bResult; | |
80252599 GS |
2261 | /* XXX despite what the documentation says about MoveFileEx(), |
2262 | * it doesn't work under Windows95! | |
2263 | */ | |
2264 | if (IsWinNT()) { | |
c5be433b | 2265 | dTHXo; |
7fac1903 | 2266 | if (USING_WIDE()) { |
0cb96387 GS |
2267 | A2WHELPER(oname, wOldName, sizeof(wOldName)); |
2268 | A2WHELPER(newname, wNewName, sizeof(wNewName)); | |
7fac1903 GS |
2269 | bResult = MoveFileExW(wOldName,wNewName, |
2270 | MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING); | |
2271 | } | |
2272 | else { | |
2273 | bResult = MoveFileExA(oname,newname, | |
2274 | MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING); | |
2275 | } | |
2276 | if (!bResult) { | |
80252599 GS |
2277 | DWORD err = GetLastError(); |
2278 | switch (err) { | |
2279 | case ERROR_BAD_NET_NAME: | |
2280 | case ERROR_BAD_NETPATH: | |
2281 | case ERROR_BAD_PATHNAME: | |
2282 | case ERROR_FILE_NOT_FOUND: | |
2283 | case ERROR_FILENAME_EXCED_RANGE: | |
2284 | case ERROR_INVALID_DRIVE: | |
2285 | case ERROR_NO_MORE_FILES: | |
2286 | case ERROR_PATH_NOT_FOUND: | |
2287 | errno = ENOENT; | |
2288 | break; | |
2289 | default: | |
2290 | errno = EACCES; | |
2291 | break; | |
2292 | } | |
2293 | return -1; | |
2294 | } | |
2295 | return 0; | |
e24c7c18 | 2296 | } |
80252599 GS |
2297 | else { |
2298 | int retval = 0; | |
2299 | char tmpname[MAX_PATH+1]; | |
2300 | char dname[MAX_PATH+1]; | |
2301 | char *endname = Nullch; | |
2302 | STRLEN tmplen = 0; | |
2303 | DWORD from_attr, to_attr; | |
2304 | ||
2305 | /* if oname doesn't exist, do nothing */ | |
2306 | from_attr = GetFileAttributes(oname); | |
2307 | if (from_attr == 0xFFFFFFFF) { | |
2308 | errno = ENOENT; | |
2309 | return -1; | |
2310 | } | |
2311 | ||
2312 | /* if newname exists, rename it to a temporary name so that we | |
2313 | * don't delete it in case oname happens to be the same file | |
2314 | * (but perhaps accessed via a different path) | |
2315 | */ | |
2316 | to_attr = GetFileAttributes(newname); | |
2317 | if (to_attr != 0xFFFFFFFF) { | |
2318 | /* if newname is a directory, we fail | |
2319 | * XXX could overcome this with yet more convoluted logic */ | |
2320 | if (to_attr & FILE_ATTRIBUTE_DIRECTORY) { | |
2321 | errno = EACCES; | |
2322 | return -1; | |
2323 | } | |
2324 | tmplen = strlen(newname); | |
2325 | strcpy(tmpname,newname); | |
2326 | endname = tmpname+tmplen; | |
2327 | for (; endname > tmpname ; --endname) { | |
2328 | if (*endname == '/' || *endname == '\\') { | |
2329 | *endname = '\0'; | |
2330 | break; | |
2331 | } | |
2332 | } | |
2333 | if (endname > tmpname) | |
2334 | endname = strcpy(dname,tmpname); | |
e24c7c18 | 2335 | else |
80252599 GS |
2336 | endname = "."; |
2337 | ||
2338 | /* get a temporary filename in same directory | |
2339 | * XXX is this really the best we can do? */ | |
2340 | if (!GetTempFileName((LPCTSTR)endname, "plr", 0, tmpname)) { | |
2341 | errno = ENOENT; | |
2342 | return -1; | |
2343 | } | |
2344 | DeleteFile(tmpname); | |
2345 | ||
2346 | retval = rename(newname, tmpname); | |
2347 | if (retval != 0) { | |
2348 | errno = EACCES; | |
2349 | return retval; | |
e24c7c18 GS |
2350 | } |
2351 | } | |
80252599 GS |
2352 | |
2353 | /* rename oname to newname */ | |
2354 | retval = rename(oname, newname); | |
2355 | ||
2356 | /* if we created a temporary file before ... */ | |
2357 | if (endname != Nullch) { | |
2358 | /* ...and rename succeeded, delete temporary file/directory */ | |
2359 | if (retval == 0) | |
2360 | DeleteFile(tmpname); | |
2361 | /* else restore it to what it was */ | |
2362 | else | |
2363 | (void)rename(tmpname, newname); | |
2364 | } | |
2365 | return retval; | |
e24c7c18 | 2366 | } |
e24c7c18 GS |
2367 | } |
2368 | ||
2369 | DllExport int | |
68dc0745 | 2370 | win32_setmode(int fd, int mode) |
0a753a76 | 2371 | { |
390b85e7 | 2372 | return setmode(fd, mode); |
0a753a76 | 2373 | } |
2374 | ||
96e4d5b1 | 2375 | DllExport long |
2376 | win32_lseek(int fd, long offset, int origin) | |
2377 | { | |
390b85e7 | 2378 | return lseek(fd, offset, origin); |
96e4d5b1 | 2379 | } |
2380 | ||
2381 | DllExport long | |
2382 | win32_tell(int fd) | |
2383 | { | |
390b85e7 | 2384 | return tell(fd); |
96e4d5b1 | 2385 | } |
2386 | ||
68dc0745 | 2387 | DllExport int |
2388 | win32_open(const char *path, int flag, ...) | |
0a753a76 | 2389 | { |
c5be433b | 2390 | dTHXo; |
68dc0745 | 2391 | va_list ap; |
2392 | int pmode; | |
b9010385 | 2393 | WCHAR wBuffer[MAX_PATH]; |
0a753a76 | 2394 | |
2395 | va_start(ap, flag); | |
2396 | pmode = va_arg(ap, int); | |
2397 | va_end(ap); | |
2398 | ||
68dc0745 | 2399 | if (stricmp(path, "/dev/null")==0) |
7fac1903 GS |
2400 | path = "NUL"; |
2401 | ||
2402 | if (USING_WIDE()) { | |
0cb96387 | 2403 | A2WHELPER(path, wBuffer, sizeof(wBuffer)); |
7fac1903 GS |
2404 | return _wopen(wBuffer, flag, pmode); |
2405 | } | |
390b85e7 | 2406 | return open(path,flag,pmode); |
0a753a76 | 2407 | } |
2408 | ||
68dc0745 | 2409 | DllExport int |
2410 | win32_close(int fd) | |
0a753a76 | 2411 | { |
390b85e7 | 2412 | return close(fd); |
0a753a76 | 2413 | } |
2414 | ||
68dc0745 | 2415 | DllExport int |
96e4d5b1 | 2416 | win32_eof(int fd) |
2417 | { | |
390b85e7 | 2418 | return eof(fd); |
96e4d5b1 | 2419 | } |
2420 | ||
2421 | DllExport int | |
68dc0745 | 2422 | win32_dup(int fd) |
0a753a76 | 2423 | { |
390b85e7 | 2424 | return dup(fd); |
0a753a76 | 2425 | } |
2426 | ||
68dc0745 | 2427 | DllExport int |
2428 | win32_dup2(int fd1,int fd2) | |
0a753a76 | 2429 | { |
390b85e7 | 2430 | return dup2(fd1,fd2); |
0a753a76 | 2431 | } |
2432 | ||
68dc0745 | 2433 | DllExport int |
3e3baf6d | 2434 | win32_read(int fd, void *buf, unsigned int cnt) |
0a753a76 | 2435 | { |
390b85e7 | 2436 | return read(fd, buf, cnt); |
0a753a76 | 2437 | } |
2438 | ||
68dc0745 | 2439 | DllExport int |
3e3baf6d | 2440 | win32_write(int fd, const void *buf, unsigned int cnt) |
0a753a76 | 2441 | { |
390b85e7 | 2442 | return write(fd, buf, cnt); |
0a753a76 | 2443 | } |
2444 | ||
68dc0745 | 2445 | DllExport int |
5aabfad6 | 2446 | win32_mkdir(const char *dir, int mode) |
2447 | { | |
390b85e7 | 2448 | return mkdir(dir); /* just ignore mode */ |
5aabfad6 | 2449 | } |
96e4d5b1 | 2450 | |
5aabfad6 | 2451 | DllExport int |
2452 | win32_rmdir(const char *dir) | |
2453 | { | |
390b85e7 | 2454 | return rmdir(dir); |
5aabfad6 | 2455 | } |
96e4d5b1 | 2456 | |
5aabfad6 | 2457 | DllExport int |
2458 | win32_chdir(const char *dir) | |
2459 | { | |
390b85e7 | 2460 | return chdir(dir); |
5aabfad6 | 2461 | } |
96e4d5b1 | 2462 | |
0aaad0ff GS |
2463 | static char * |
2464 | create_command_line(const char* command, const char * const *args) | |
2465 | { | |
c5be433b | 2466 | dTHXo; |
0aaad0ff GS |
2467 | int index; |
2468 | char *cmd, *ptr, *arg; | |
2469 | STRLEN len = strlen(command) + 1; | |
2470 | ||
2471 | for (index = 0; (ptr = (char*)args[index]) != NULL; ++index) | |
2472 | len += strlen(ptr) + 1; | |
2473 | ||
2474 | New(1310, cmd, len, char); | |
2475 | ptr = cmd; | |
2476 | strcpy(ptr, command); | |
0aaad0ff GS |
2477 | |
2478 | for (index = 0; (arg = (char*)args[index]) != NULL; ++index) { | |
0aaad0ff | 2479 | ptr += strlen(ptr); |
18a945d4 GS |
2480 | *ptr++ = ' '; |
2481 | strcpy(ptr, arg); | |
0aaad0ff GS |
2482 | } |
2483 | ||
2484 | return cmd; | |
2485 | } | |
2486 | ||
2487 | static char * | |
2488 | qualified_path(const char *cmd) | |
2489 | { | |
c5be433b | 2490 | dTHXo; |
0aaad0ff GS |
2491 | char *pathstr; |
2492 | char *fullcmd, *curfullcmd; | |
2493 | STRLEN cmdlen = 0; | |
2494 | int has_slash = 0; | |
2495 | ||
2496 | if (!cmd) | |
2497 | return Nullch; | |
2498 | fullcmd = (char*)cmd; | |
2499 | while (*fullcmd) { | |
2500 | if (*fullcmd == '/' || *fullcmd == '\\') | |
2501 | has_slash++; | |
2502 | fullcmd++; | |
2503 | cmdlen++; | |
2504 | } | |
2505 | ||
2506 | /* look in PATH */ | |
2507 | pathstr = win32_getenv("PATH"); | |
2508 | New(0, fullcmd, MAX_PATH+1, char); | |
2509 | curfullcmd = fullcmd; | |
2510 | ||
2511 | while (1) { | |
2512 | DWORD res; | |
2513 | ||
2514 | /* start by appending the name to the current prefix */ | |
2515 | strcpy(curfullcmd, cmd); | |
2516 | curfullcmd += cmdlen; | |
2517 | ||
2518 | /* if it doesn't end with '.', or has no extension, try adding | |
2519 | * a trailing .exe first */ | |
2520 | if (cmd[cmdlen-1] != '.' | |
2521 | && (cmdlen < 4 || cmd[cmdlen-4] != '.')) | |
2522 | { | |
2523 | strcpy(curfullcmd, ".exe"); | |
2524 | res = GetFileAttributes(fullcmd); | |
2525 | if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY)) | |
2526 | return fullcmd; | |
2527 | *curfullcmd = '\0'; | |
2528 | } | |
2529 | ||
2530 | /* that failed, try the bare name */ | |
2531 | res = GetFileAttributes(fullcmd); | |
2532 | if (res != 0xFFFFFFFF && !(res & FILE_ATTRIBUTE_DIRECTORY)) | |
2533 | return fullcmd; | |
2534 | ||
2535 | /* quit if no other path exists, or if cmd already has path */ | |
2536 | if (!pathstr || !*pathstr || has_slash) | |
2537 | break; | |
2538 | ||
2539 | /* skip leading semis */ | |
2540 | while (*pathstr == ';') | |
2541 | pathstr++; | |
2542 | ||
2543 | /* build a new prefix from scratch */ | |
2544 | curfullcmd = fullcmd; | |
2545 | while (*pathstr && *pathstr != ';') { | |
2546 | if (*pathstr == '"') { /* foo;"baz;etc";bar */ | |
2547 | pathstr++; /* skip initial '"' */ | |
2548 | while (*pathstr && *pathstr != '"') { | |
2549 | if (curfullcmd-fullcmd < MAX_PATH-cmdlen-5) | |
2550 | *curfullcmd++ = *pathstr; | |
2551 | pathstr++; | |
2552 | } | |
2553 | if (*pathstr) | |
2554 | pathstr++; /* skip trailing '"' */ | |
2555 | } | |
2556 | else { | |
2557 | if (curfullcmd-fullcmd < MAX_PATH-cmdlen-5) | |
2558 | *curfullcmd++ = *pathstr; | |
2559 | pathstr++; | |
2560 | } | |
2561 | } | |
2562 | if (*pathstr) | |
2563 | pathstr++; /* skip trailing semi */ | |
2564 | if (curfullcmd > fullcmd /* append a dir separator */ | |
2565 | && curfullcmd[-1] != '/' && curfullcmd[-1] != '\\') | |
2566 | { | |
2567 | *curfullcmd++ = '\\'; | |
2568 | } | |
2569 | } | |
2570 | GIVE_UP: | |
2571 | Safefree(fullcmd); | |
2572 | return Nullch; | |
2573 | } | |
2574 | ||
3075ddba GS |
2575 | /* The following are just place holders. |
2576 | * Some hosts may provide and environment that the OS is | |
2577 | * not tracking, therefore, these host must provide that | |
2578 | * environment and the current directory to CreateProcess | |
2579 | */ | |
2580 | ||
2581 | void* | |
2582 | get_childenv(void) | |
2583 | { | |
2584 | return NULL; | |
2585 | } | |
2586 | ||
2587 | void | |
2dd19d29 | 2588 | free_childenv(void* d) |
3075ddba GS |
2589 | { |
2590 | } | |
2591 | ||
2592 | char* | |
2593 | get_childdir(void) | |
2594 | { | |
2595 | return NULL; | |
2596 | } | |
2597 | ||
2598 | void | |
2dd19d29 | 2599 | free_childdir(char* d) |
3075ddba GS |
2600 | { |
2601 | } | |
2602 | ||
2603 | ||
0aaad0ff GS |
2604 | /* XXX this needs to be made more compatible with the spawnvp() |
2605 | * provided by the various RTLs. In particular, searching for | |
2606 | * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented. | |
2607 | * This doesn't significantly affect perl itself, because we | |
2608 | * always invoke things using PERL5SHELL if a direct attempt to | |
2609 | * spawn the executable fails. | |
2610 | * | |
2611 | * XXX splitting and rejoining the commandline between do_aspawn() | |
2612 | * and win32_spawnvp() could also be avoided. | |
2613 | */ | |
2614 | ||
5aabfad6 | 2615 | DllExport int |
3e3baf6d | 2616 | win32_spawnvp(int mode, const char *cmdname, const char *const *argv) |
0a753a76 | 2617 | { |
0aaad0ff GS |
2618 | #ifdef USE_RTL_SPAWNVP |
2619 | return spawnvp(mode, cmdname, (char * const *)argv); | |
2620 | #else | |
c5be433b | 2621 | dTHXo; |
2b260de0 | 2622 | int ret; |
3075ddba GS |
2623 | void* env; |
2624 | char* dir; | |
0aaad0ff GS |
2625 | STARTUPINFO StartupInfo; |
2626 | PROCESS_INFORMATION ProcessInformation; | |
2627 | DWORD create = 0; | |
2628 | ||
2629 | char *cmd = create_command_line(cmdname, strcmp(cmdname, argv[0]) == 0 | |
2630 | ? &argv[1] : argv); | |
2631 | char *fullcmd = Nullch; | |
2632 | ||
3075ddba GS |
2633 | env = PerlEnv_get_childenv(); |
2634 | dir = PerlEnv_get_childdir(); | |
2635 | ||
0aaad0ff GS |
2636 | switch(mode) { |
2637 | case P_NOWAIT: /* asynch + remember result */ | |
2638 | if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) { | |
2639 | errno = EAGAIN; | |
2640 | ret = -1; | |
2641 | goto RETVAL; | |
2642 | } | |
2643 | /* FALL THROUGH */ | |
2644 | case P_WAIT: /* synchronous execution */ | |
2645 | break; | |
2646 | default: /* invalid mode */ | |
2647 | errno = EINVAL; | |
2648 | ret = -1; | |
2649 | goto RETVAL; | |
2650 | } | |
2651 | memset(&StartupInfo,0,sizeof(StartupInfo)); | |
2652 | StartupInfo.cb = sizeof(StartupInfo); | |
3ffaa937 GS |
2653 | StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
2654 | StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | |
2655 | StartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); | |
2656 | if (StartupInfo.hStdInput != INVALID_HANDLE_VALUE && | |
2657 | StartupInfo.hStdOutput != INVALID_HANDLE_VALUE && | |
2658 | StartupInfo.hStdError != INVALID_HANDLE_VALUE) | |
2659 | { | |
2660 | StartupInfo.dwFlags |= STARTF_USESTDHANDLES; | |
2661 | } | |
2662 | else { | |
2663 | create |= CREATE_NEW_CONSOLE; | |
2664 | } | |
2665 | ||
0aaad0ff GS |
2666 | RETRY: |
2667 | if (!CreateProcess(cmdname, /* search PATH to find executable */ | |
2668 | cmd, /* executable, and its arguments */ | |
2669 | NULL, /* process attributes */ | |
2670 | NULL, /* thread attributes */ | |
2671 | TRUE, /* inherit handles */ | |
2672 | create, /* creation flags */ | |
3075ddba GS |
2673 | (LPVOID)env, /* inherit environment */ |
2674 | dir, /* inherit cwd */ | |
0aaad0ff GS |
2675 | &StartupInfo, |
2676 | &ProcessInformation)) | |
2677 | { | |
2678 | /* initial NULL argument to CreateProcess() does a PATH | |
2679 | * search, but it always first looks in the directory | |
2680 | * where the current process was started, which behavior | |
2681 | * is undesirable for backward compatibility. So we | |
2682 | * jump through our own hoops by picking out the path | |
2683 | * we really want it to use. */ | |
2684 | if (!fullcmd) { | |
2685 | fullcmd = qualified_path(cmdname); | |
2686 | if (fullcmd) { | |
2687 | cmdname = fullcmd; | |
2688 | goto RETRY; | |
2689 | } | |
2690 | } | |
2691 | errno = ENOENT; | |
2692 | ret = -1; | |
2693 | goto RETVAL; | |
2694 | } | |
2d7a9237 | 2695 | |
0aaad0ff GS |
2696 | if (mode == P_NOWAIT) { |
2697 | /* asynchronous spawn -- store handle, return PID */ | |
2698 | w32_child_handles[w32_num_children] = ProcessInformation.hProcess; | |
2b260de0 GS |
2699 | w32_child_pids[w32_num_children] = ProcessInformation.dwProcessId; |
2700 | ret = (int)ProcessInformation.dwProcessId; | |
0aaad0ff GS |
2701 | ++w32_num_children; |
2702 | } | |
2703 | else { | |
2b260de0 | 2704 | DWORD status; |
0aaad0ff | 2705 | WaitForSingleObject(ProcessInformation.hProcess, INFINITE); |
2b260de0 GS |
2706 | GetExitCodeProcess(ProcessInformation.hProcess, &status); |
2707 | ret = (int)status; | |
0aaad0ff GS |
2708 | CloseHandle(ProcessInformation.hProcess); |
2709 | } | |
e17cb2a9 | 2710 | |
0aaad0ff | 2711 | CloseHandle(ProcessInformation.hThread); |
3075ddba | 2712 | |
0aaad0ff | 2713 | RETVAL: |
3075ddba GS |
2714 | PerlEnv_free_childenv(env); |
2715 | PerlEnv_free_childdir(dir); | |
0aaad0ff GS |
2716 | Safefree(cmd); |
2717 | Safefree(fullcmd); | |
2b260de0 | 2718 | return ret; |
2d7a9237 | 2719 | #endif |
0a753a76 | 2720 | } |
2721 | ||
6890e559 | 2722 | DllExport int |
eb62e965 JD |
2723 | win32_execv(const char *cmdname, const char *const *argv) |
2724 | { | |
2725 | return execv(cmdname, (char *const *)argv); | |
2726 | } | |
2727 | ||
2728 | DllExport int | |
6890e559 GS |
2729 | win32_execvp(const char *cmdname, const char *const *argv) |
2730 | { | |
390b85e7 | 2731 | return execvp(cmdname, (char *const *)argv); |
6890e559 GS |
2732 | } |
2733 | ||
84902520 TB |
2734 | DllExport void |
2735 | win32_perror(const char *str) | |
2736 | { | |
390b85e7 | 2737 | perror(str); |
84902520 TB |
2738 | } |
2739 | ||
2740 | DllExport void | |
2741 | win32_setbuf(FILE *pf, char *buf) | |
2742 | { | |
390b85e7 | 2743 | setbuf(pf, buf); |
84902520 TB |
2744 | } |
2745 | ||
2746 | DllExport int | |
2747 | win32_setvbuf(FILE *pf, char *buf, int type, size_t size) | |
2748 | { | |
390b85e7 | 2749 | return setvbuf(pf, buf, type, size); |
84902520 TB |
2750 | } |
2751 | ||
2752 | DllExport int | |
2753 | win32_flushall(void) | |
2754 | { | |
390b85e7 | 2755 | return flushall(); |
84902520 TB |
2756 | } |
2757 | ||
2758 | DllExport int | |
2759 | win32_fcloseall(void) | |
2760 | { | |
390b85e7 | 2761 | return fcloseall(); |
84902520 TB |
2762 | } |
2763 | ||
2764 | DllExport char* | |
2765 | win32_fgets(char *s, int n, FILE *pf) | |
2766 | { | |
390b85e7 | 2767 | return fgets(s, n, pf); |
84902520 TB |
2768 | } |
2769 | ||
2770 | DllExport char* | |
2771 | win32_gets(char *s) | |
2772 | { | |
390b85e7 | 2773 | return gets(s); |
84902520 TB |
2774 | } |
2775 | ||
2776 | DllExport int | |
2777 | win32_fgetc(FILE *pf) | |
2778 | { | |
390b85e7 | 2779 | return fgetc(pf); |
84902520 TB |
2780 | } |
2781 | ||
2782 | DllExport int | |
2783 | win32_putc(int c, FILE *pf) | |
2784 | { | |
390b85e7 | 2785 | return putc(c,pf); |
84902520 TB |
2786 | } |
2787 | ||
2788 | DllExport int | |
2789 | win32_puts(const char *s) | |
2790 | { | |
390b85e7 | 2791 | return puts(s); |
84902520 TB |
2792 | } |
2793 | ||
2794 | DllExport int | |
2795 | win32_getchar(void) | |
2796 | { | |
390b85e7 | 2797 | return getchar(); |
84902520 TB |
2798 | } |
2799 | ||
2800 | DllExport int | |
2801 | win32_putchar(int c) | |
2802 | { | |
390b85e7 | 2803 | return putchar(c); |
84902520 TB |
2804 | } |
2805 | ||
bbc8f9de NIS |
2806 | #ifdef MYMALLOC |
2807 | ||
2808 | #ifndef USE_PERL_SBRK | |
2809 | ||
2810 | static char *committed = NULL; | |
2811 | static char *base = NULL; | |
2812 | static char *reserved = NULL; | |
2813 | static char *brk = NULL; | |
2814 | static DWORD pagesize = 0; | |
2815 | static DWORD allocsize = 0; | |
2816 | ||
2817 | void * | |
2818 | sbrk(int need) | |
2819 | { | |
2820 | void *result; | |
2821 | if (!pagesize) | |
2822 | {SYSTEM_INFO info; | |
2823 | GetSystemInfo(&info); | |
2824 | /* Pretend page size is larger so we don't perpetually | |
2825 | * call the OS to commit just one page ... | |
2826 | */ | |
2827 | pagesize = info.dwPageSize << 3; | |
2828 | allocsize = info.dwAllocationGranularity; | |
2829 | } | |
2830 | /* This scheme fails eventually if request for contiguous | |
2831 | * block is denied so reserve big blocks - this is only | |
2832 | * address space not memory ... | |
2833 | */ | |
2834 | if (brk+need >= reserved) | |
2835 | { | |
2836 | DWORD size = 64*1024*1024; | |
2837 | char *addr; | |
2838 | if (committed && reserved && committed < reserved) | |
2839 | { | |
2840 | /* Commit last of previous chunk cannot span allocations */ | |
161b471a | 2841 | addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE); |
bbc8f9de NIS |
2842 | if (addr) |
2843 | committed = reserved; | |
2844 | } | |
2845 | /* Reserve some (more) space | |
2846 | * Note this is a little sneaky, 1st call passes NULL as reserved | |
2847 | * so lets system choose where we start, subsequent calls pass | |
2848 | * the old end address so ask for a contiguous block | |
2849 | */ | |
161b471a | 2850 | addr = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS); |
bbc8f9de NIS |
2851 | if (addr) |
2852 | { | |
2853 | reserved = addr+size; | |
2854 | if (!base) | |
2855 | base = addr; | |
2856 | if (!committed) | |
2857 | committed = base; | |
2858 | if (!brk) | |
2859 | brk = committed; | |
2860 | } | |
2861 | else | |
2862 | { | |
2863 | return (void *) -1; | |
2864 | } | |
2865 | } | |
2866 | result = brk; | |
2867 | brk += need; | |
2868 | if (brk > committed) | |
2869 | { | |
2870 | DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize; | |
161b471a | 2871 | char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE); |
bbc8f9de NIS |
2872 | if (addr) |
2873 | { | |
2874 | committed += size; | |
2875 | } | |
2876 | else | |
2877 | return (void *) -1; | |
2878 | } | |
2879 | return result; | |
2880 | } | |
2881 | ||
2882 | #endif | |
2883 | #endif | |
2884 | ||
84902520 TB |
2885 | DllExport void* |
2886 | win32_malloc(size_t size) | |
2887 | { | |
390b85e7 | 2888 | return malloc(size); |
84902520 TB |
2889 | } |
2890 | ||
2891 | DllExport void* | |
2892 | win32_calloc(size_t numitems, size_t size) | |
2893 | { | |
390b85e7 | 2894 | return calloc(numitems,size); |
84902520 TB |
2895 | } |
2896 | ||
2897 | DllExport void* | |
2898 | win32_realloc(void *block, size_t size) | |
2899 | { | |
390b85e7 | 2900 | return realloc(block,size); |
84902520 TB |
2901 | } |
2902 | ||
2903 | DllExport void | |
2904 | win32_free(void *block) | |
2905 | { | |
390b85e7 | 2906 | free(block); |
84902520 TB |
2907 | } |
2908 | ||
bbc8f9de | 2909 | |
68dc0745 | 2910 | int |
65e48ea9 | 2911 | win32_open_osfhandle(long handle, int flags) |
0a753a76 | 2912 | { |
390b85e7 | 2913 | return _open_osfhandle(handle, flags); |
0a753a76 | 2914 | } |
2915 | ||
68dc0745 | 2916 | long |
65e48ea9 | 2917 | win32_get_osfhandle(int fd) |
0a753a76 | 2918 | { |
390b85e7 | 2919 | return _get_osfhandle(fd); |
0a753a76 | 2920 | } |
7bac28a0 | 2921 | |
0cb96387 | 2922 | DllExport void* |
c5be433b | 2923 | win32_dynaload(const char* filename) |
0cb96387 | 2924 | { |
c5be433b | 2925 | dTHXo; |
51371543 | 2926 | HMODULE hModule; |
0cb96387 GS |
2927 | if (USING_WIDE()) { |
2928 | WCHAR wfilename[MAX_PATH]; | |
2929 | A2WHELPER(filename, wfilename, sizeof(wfilename)); | |
2930 | hModule = LoadLibraryExW(wfilename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | |
2931 | } | |
2932 | else { | |
2933 | hModule = LoadLibraryExA(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | |
2934 | } | |
2935 | return hModule; | |
2936 | } | |
2937 | ||
2938 | DllExport int | |
2939 | win32_add_host(char *nameId, void *data) | |
2940 | { | |
2941 | /* | |
2942 | * This must be called before the script is parsed, | |
2943 | * therefore no locking of threads is needed | |
2944 | */ | |
c5be433b | 2945 | dTHXo; |
0cb96387 GS |
2946 | struct host_link *link; |
2947 | New(1314, link, 1, struct host_link); | |
2948 | link->host_data = data; | |
2949 | link->nameId = nameId; | |
2950 | link->next = w32_host_link; | |
2951 | w32_host_link = link; | |
2952 | return 1; | |
2953 | } | |
2954 | ||
2955 | DllExport void * | |
2956 | win32_get_host_data(char *nameId) | |
2957 | { | |
c5be433b | 2958 | dTHXo; |
0cb96387 GS |
2959 | struct host_link *link = w32_host_link; |
2960 | while(link) { | |
2961 | if(strEQ(link->nameId, nameId)) | |
2962 | return link->host_data; | |
2963 | link = link->next; | |
2964 | } | |
2965 | return Nullch; | |
2966 | } | |
2967 | ||
7bac28a0 | 2968 | /* |
2969 | * Extras. | |
2970 | */ | |
2971 | ||
ad2e33dc GS |
2972 | static |
2973 | XS(w32_GetCwd) | |
2974 | { | |
2975 | dXSARGS; | |
2976 | SV *sv = sv_newmortal(); | |
2977 | /* Make one call with zero size - return value is required size */ | |
2978 | DWORD len = GetCurrentDirectory((DWORD)0,NULL); | |
2979 | SvUPGRADE(sv,SVt_PV); | |
2980 | SvGROW(sv,len); | |
2981 | SvCUR(sv) = GetCurrentDirectory((DWORD) SvLEN(sv), SvPVX(sv)); | |
2982 | /* | |
2983 | * If result != 0 | |
2984 | * then it worked, set PV valid, | |
2985 | * else leave it 'undef' | |
2986 | */ | |
3467312b | 2987 | EXTEND(SP,1); |
bb897dfc | 2988 | if (SvCUR(sv)) { |
ad2e33dc | 2989 | SvPOK_on(sv); |
bb897dfc JD |
2990 | ST(0) = sv; |
2991 | XSRETURN(1); | |
2992 | } | |
3467312b | 2993 | XSRETURN_UNDEF; |
ad2e33dc GS |
2994 | } |
2995 | ||
2996 | static | |
2997 | XS(w32_SetCwd) | |
2998 | { | |
2999 | dXSARGS; | |
3000 | if (items != 1) | |
4f63d024 | 3001 | Perl_croak(aTHX_ "usage: Win32::SetCurrentDirectory($cwd)"); |
bb897dfc | 3002 | if (SetCurrentDirectory(SvPV_nolen(ST(0)))) |
ad2e33dc GS |
3003 | XSRETURN_YES; |
3004 | ||
3005 | XSRETURN_NO; | |
3006 | } | |
3007 | ||
3008 | static | |
3009 | XS(w32_GetNextAvailDrive) | |
3010 | { | |
3011 | dXSARGS; | |
3012 | char ix = 'C'; | |
3013 | char root[] = "_:\\"; | |
3467312b JD |
3014 | |
3015 | EXTEND(SP,1); | |
ad2e33dc GS |
3016 | while (ix <= 'Z') { |
3017 | root[0] = ix++; | |
3018 | if (GetDriveType(root) == 1) { | |
3019 | root[2] = '\0'; | |
3020 | XSRETURN_PV(root); | |
3021 | } | |
3022 | } | |
3467312b | 3023 | XSRETURN_UNDEF; |
ad2e33dc GS |
3024 | } |
3025 | ||
3026 | static | |
3027 | XS(w32_GetLastError) | |
3028 | { | |
3029 | dXSARGS; | |
bb897dfc | 3030 | EXTEND(SP,1); |
ad2e33dc GS |
3031 | XSRETURN_IV(GetLastError()); |
3032 | } | |
3033 | ||
3034 | static | |
ca135624 JD |
3035 | XS(w32_SetLastError) |
3036 | { | |
3037 | dXSARGS; | |
3038 | if (items != 1) | |
4f63d024 | 3039 | Perl_croak(aTHX_ "usage: Win32::SetLastError($error)"); |
ca135624 | 3040 | SetLastError(SvIV(ST(0))); |
bb897dfc | 3041 | XSRETURN_EMPTY; |
ca135624 JD |
3042 | } |
3043 | ||
3044 | static | |
ad2e33dc GS |
3045 | XS(w32_LoginName) |
3046 | { | |
3047 | dXSARGS; | |
3352bfcb GS |
3048 | char *name = w32_getlogin_buffer; |
3049 | DWORD size = sizeof(w32_getlogin_buffer); | |
3467312b | 3050 | EXTEND(SP,1); |
ad2e33dc GS |
3051 | if (GetUserName(name,&size)) { |
3052 | /* size includes NULL */ | |
79cb57f6 | 3053 | ST(0) = sv_2mortal(newSVpvn(name,size-1)); |
ad2e33dc GS |
3054 | XSRETURN(1); |
3055 | } | |
3467312b | 3056 | XSRETURN_UNDEF; |
ad2e33dc GS |
3057 | } |
3058 | ||
3059 | static | |
3060 | XS(w32_NodeName) | |
3061 | { | |
3062 | dXSARGS; | |
3063 | char name[MAX_COMPUTERNAME_LENGTH+1]; | |
3064 | DWORD size = sizeof(name); | |
3467312b | 3065 | EXTEND(SP,1); |
ad2e33dc GS |
3066 | if (GetComputerName(name,&size)) { |
3067 | /* size does NOT include NULL :-( */ | |
79cb57f6 | 3068 | ST(0) = sv_2mortal(newSVpvn(name,size)); |
ad2e33dc GS |
3069 | XSRETURN(1); |
3070 | } | |
3467312b | 3071 | XSRETURN_UNDEF; |
ad2e33dc GS |
3072 | } |
3073 | ||
3074 | ||
3075 | static | |
3076 | XS(w32_DomainName) | |
3077 | { | |
3078 | dXSARGS; | |
625a29bd GS |
3079 | HINSTANCE hNetApi32 = LoadLibrary("netapi32.dll"); |
3080 | DWORD (__stdcall *pfnNetApiBufferFree)(LPVOID Buffer); | |
3081 | DWORD (__stdcall *pfnNetWkstaGetInfo)(LPWSTR servername, DWORD level, | |
3082 | void *bufptr); | |
3083 | ||
3084 | if (hNetApi32) { | |
3085 | pfnNetApiBufferFree = (DWORD (__stdcall *)(void *)) | |
3086 | GetProcAddress(hNetApi32, "NetApiBufferFree"); | |
3087 | pfnNetWkstaGetInfo = (DWORD (__stdcall *)(LPWSTR, DWORD, void *)) | |
3088 | GetProcAddress(hNetApi32, "NetWkstaGetInfo"); | |
3089 | } | |
3467312b | 3090 | EXTEND(SP,1); |
625a29bd GS |
3091 | if (hNetApi32 && pfnNetWkstaGetInfo && pfnNetApiBufferFree) { |
3092 | /* this way is more reliable, in case user has a local account. */ | |
ad2e33dc GS |
3093 | char dname[256]; |
3094 | DWORD dnamelen = sizeof(dname); | |
625a29bd GS |
3095 | struct { |
3096 | DWORD wki100_platform_id; | |
3097 | LPWSTR wki100_computername; | |
3098 | LPWSTR wki100_langroup; | |
3099 | DWORD wki100_ver_major; | |
3100 | DWORD wki100_ver_minor; | |
3101 | } *pwi; | |
3102 | /* NERR_Success *is* 0*/ | |
3103 | if (0 == pfnNetWkstaGetInfo(NULL, 100, &pwi)) { | |
3104 | if (pwi->wki100_langroup && *(pwi->wki100_langroup)) { | |
3105 | WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_langroup, | |
3106 | -1, (LPSTR)dname, dnamelen, NULL, NULL); | |
3107 | } | |
3108 | else { | |
3109 | WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_computername, | |
3110 | -1, (LPSTR)dname, dnamelen, NULL, NULL); | |
3111 | } | |
3112 | pfnNetApiBufferFree(pwi); | |
3113 | FreeLibrary(hNetApi32); | |
3114 | XSRETURN_PV(dname); | |
ad2e33dc | 3115 | } |
625a29bd | 3116 | FreeLibrary(hNetApi32); |
ad2e33dc | 3117 | } |
625a29bd GS |
3118 | else { |
3119 | /* Win95 doesn't have NetWksta*(), so do it the old way */ | |
3120 | char name[256]; | |
3121 | DWORD size = sizeof(name); | |
3122 | if (hNetApi32) | |
3123 | FreeLibrary(hNetApi32); | |
3124 | if (GetUserName(name,&size)) { | |
3125 | char sid[1024]; | |
3126 | DWORD sidlen = sizeof(sid); | |
3127 | char dname[256]; | |
3128 | DWORD dnamelen = sizeof(dname); | |
3129 | SID_NAME_USE snu; | |
3130 | if (LookupAccountName(NULL, name, (PSID)&sid, &sidlen, | |
3131 | dname, &dnamelen, &snu)) { | |
3132 | XSRETURN_PV(dname); /* all that for this */ | |
3133 | } | |
0a2408cf | 3134 | } |
9404a519 | 3135 | } |
3467312b | 3136 | XSRETURN_UNDEF; |
ad2e33dc GS |
3137 | } |
3138 | ||
3139 | static | |
3140 | XS(w32_FsType) | |
3141 | { | |
3142 | dXSARGS; | |
3143 | char fsname[256]; | |
3144 | DWORD flags, filecomplen; | |
3145 | if (GetVolumeInformation(NULL, NULL, 0, NULL, &filecomplen, | |
3146 | &flags, fsname, sizeof(fsname))) { | |
bb897dfc | 3147 | if (GIMME_V == G_ARRAY) { |
79cb57f6 | 3148 | XPUSHs(sv_2mortal(newSVpvn(fsname,strlen(fsname)))); |
ad2e33dc GS |
3149 | XPUSHs(sv_2mortal(newSViv(flags))); |
3150 | XPUSHs(sv_2mortal(newSViv(filecomplen))); | |
3151 | PUTBACK; | |
3152 | return; | |
3153 | } | |
bb897dfc | 3154 | EXTEND(SP,1); |
ad2e33dc GS |
3155 | XSRETURN_PV(fsname); |
3156 | } | |
bb897dfc | 3157 | XSRETURN_EMPTY; |
ad2e33dc GS |
3158 | } |
3159 | ||
3160 | static | |
3161 | XS(w32_GetOSVersion) | |
3162 | { | |
3163 | dXSARGS; | |
3164 | OSVERSIONINFO osver; | |
3165 | ||
3166 | osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
3167 | if (GetVersionEx(&osver)) { | |
79cb57f6 | 3168 | XPUSHs(newSVpvn(osver.szCSDVersion, strlen(osver.szCSDVersion))); |
ad2e33dc GS |
3169 | XPUSHs(newSViv(osver.dwMajorVersion)); |
3170 | XPUSHs(newSViv(osver.dwMinorVersion)); | |
3171 | XPUSHs(newSViv(osver.dwBuildNumber)); | |
3172 | XPUSHs(newSViv(osver.dwPlatformId)); | |
3173 | PUTBACK; | |
3174 | return; | |
3175 | } | |
bb897dfc | 3176 | XSRETURN_EMPTY; |
ad2e33dc GS |
3177 | } |
3178 | ||
3179 | static | |
3180 | XS(w32_IsWinNT) | |
3181 | { | |
3182 | dXSARGS; | |
bb897dfc | 3183 | EXTEND(SP,1); |
ad2e33dc GS |
3184 | XSRETURN_IV(IsWinNT()); |
3185 | } | |
3186 | ||
3187 | static | |
3188 | XS(w32_IsWin95) | |
3189 | { | |
3190 | dXSARGS; | |
bb897dfc | 3191 | EXTEND(SP,1); |
ad2e33dc GS |
3192 | XSRETURN_IV(IsWin95()); |
3193 | } | |
3194 | ||
3195 | static | |
3196 | XS(w32_FormatMessage) | |
3197 | { | |
3198 | dXSARGS; | |
3199 | DWORD source = 0; | |
3200 | char msgbuf[1024]; | |
3201 | ||
3202 | if (items != 1) | |
4f63d024 | 3203 | Perl_croak(aTHX_ "usage: Win32::FormatMessage($errno)"); |
ad2e33dc GS |
3204 | |
3205 | if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, | |
3206 | &source, SvIV(ST(0)), 0, | |
3207 | msgbuf, sizeof(msgbuf)-1, NULL)) | |
3208 | XSRETURN_PV(msgbuf); | |
3209 | ||
3467312b | 3210 | XSRETURN_UNDEF; |
ad2e33dc GS |
3211 | } |
3212 | ||
3213 | static | |
3214 | XS(w32_Spawn) | |
3215 | { | |
3216 | dXSARGS; | |
3217 | char *cmd, *args; | |
3218 | PROCESS_INFORMATION stProcInfo; | |
3219 | STARTUPINFO stStartInfo; | |
3220 | BOOL bSuccess = FALSE; | |
3221 | ||
9404a519 | 3222 | if (items != 3) |
4f63d024 | 3223 | Perl_croak(aTHX_ "usage: Win32::Spawn($cmdName, $args, $PID)"); |
ad2e33dc | 3224 | |
bb897dfc JD |
3225 | cmd = SvPV_nolen(ST(0)); |
3226 | args = SvPV_nolen(ST(1)); | |
ad2e33dc GS |
3227 | |
3228 | memset(&stStartInfo, 0, sizeof(stStartInfo)); /* Clear the block */ | |
3229 | stStartInfo.cb = sizeof(stStartInfo); /* Set the structure size */ | |
3230 | stStartInfo.dwFlags = STARTF_USESHOWWINDOW; /* Enable wShowWindow control */ | |
3231 | stStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; /* Start min (normal) */ | |
3232 | ||
9404a519 | 3233 | if (CreateProcess( |
ad2e33dc GS |
3234 | cmd, /* Image path */ |
3235 | args, /* Arguments for command line */ | |
3236 | NULL, /* Default process security */ | |
3237 | NULL, /* Default thread security */ | |
3238 | FALSE, /* Must be TRUE to use std handles */ | |
3239 | NORMAL_PRIORITY_CLASS, /* No special scheduling */ | |
3240 | NULL, /* Inherit our environment block */ | |
3241 | NULL, /* Inherit our currrent directory */ | |
3242 | &stStartInfo, /* -> Startup info */ | |
3243 | &stProcInfo)) /* <- Process info (if OK) */ | |
3244 | { | |
3245 | CloseHandle(stProcInfo.hThread);/* library source code does this. */ | |
3246 | sv_setiv(ST(2), stProcInfo.dwProcessId); | |
3247 | bSuccess = TRUE; | |
3248 | } | |
3249 | XSRETURN_IV(bSuccess); | |
3250 | } | |
3251 | ||
3252 | static | |
3253 | XS(w32_GetTickCount) | |
3254 | { | |
3255 | dXSARGS; | |
fdb068fa | 3256 | DWORD msec = GetTickCount(); |
a6c40364 | 3257 | EXTEND(SP,1); |
fdb068fa JD |
3258 | if ((IV)msec > 0) |
3259 | XSRETURN_IV(msec); | |
3260 | XSRETURN_NV(msec); | |
ad2e33dc GS |
3261 | } |
3262 | ||
3263 | static | |
3264 | XS(w32_GetShortPathName) | |
3265 | { | |
3266 | dXSARGS; | |
3267 | SV *shortpath; | |
e8bab181 | 3268 | DWORD len; |
ad2e33dc | 3269 | |
9404a519 | 3270 | if (items != 1) |
4f63d024 | 3271 | Perl_croak(aTHX_ "usage: Win32::GetShortPathName($longPathName)"); |
ad2e33dc GS |
3272 | |
3273 | shortpath = sv_mortalcopy(ST(0)); | |
3274 | SvUPGRADE(shortpath, SVt_PV); | |
3275 | /* src == target is allowed */ | |
e8bab181 GS |
3276 | do { |
3277 | len = GetShortPathName(SvPVX(shortpath), | |
3278 | SvPVX(shortpath), | |
3279 | SvLEN(shortpath)); | |
3280 | } while (len >= SvLEN(shortpath) && sv_grow(shortpath,len+1)); | |
3281 | if (len) { | |
3282 | SvCUR_set(shortpath,len); | |
ad2e33dc | 3283 | ST(0) = shortpath; |
bb897dfc | 3284 | XSRETURN(1); |
e8bab181 | 3285 | } |
3467312b | 3286 | XSRETURN_UNDEF; |
ad2e33dc GS |
3287 | } |
3288 | ||
ad0751ec | 3289 | static |
ca135624 JD |
3290 | XS(w32_GetFullPathName) |
3291 | { | |
3292 | dXSARGS; | |
3293 | SV *filename; | |
3294 | SV *fullpath; | |
3295 | char *filepart; | |
3296 | DWORD len; | |
3297 | ||
3298 | if (items != 1) | |
4f63d024 | 3299 | Perl_croak(aTHX_ "usage: Win32::GetFullPathName($filename)"); |
ca135624 JD |
3300 | |
3301 | filename = ST(0); | |
3302 | fullpath = sv_mortalcopy(filename); | |
3303 | SvUPGRADE(fullpath, SVt_PV); | |
3304 | do { | |
3305 | len = GetFullPathName(SvPVX(filename), | |
3306 | SvLEN(fullpath), | |
3307 | SvPVX(fullpath), | |
3308 | &filepart); | |
3309 | } while (len >= SvLEN(fullpath) && sv_grow(fullpath,len+1)); | |
3310 | if (len) { | |
3311 | if (GIMME_V == G_ARRAY) { | |
3312 | EXTEND(SP,1); | |
bb897dfc | 3313 | XST_mPV(1,filepart); |
ca135624 JD |
3314 | len = filepart - SvPVX(fullpath); |
3315 | items = 2; | |
3316 | } | |
3317 | SvCUR_set(fullpath,len); | |
3318 | ST(0) = fullpath; | |
bb897dfc | 3319 | XSRETURN(items); |
ca135624 | 3320 | } |
bb897dfc | 3321 | XSRETURN_EMPTY; |
ca135624 JD |
3322 | } |
3323 | ||
3324 | static | |
8ac9c18d GS |
3325 | XS(w32_GetLongPathName) |
3326 | { | |
3327 | dXSARGS; | |
3328 | SV *path; | |
3329 | char tmpbuf[MAX_PATH+1]; | |
3330 | char *pathstr; | |
3331 | STRLEN len; | |
3332 | ||
3333 | if (items != 1) | |
4f63d024 | 3334 | Perl_croak(aTHX_ "usage: Win32::GetLongPathName($pathname)"); |
8ac9c18d GS |
3335 | |
3336 | path = ST(0); | |
3337 | pathstr = SvPV(path,len); | |
3338 | strcpy(tmpbuf, pathstr); | |
3339 | pathstr = win32_longpath(tmpbuf); | |
3340 | if (pathstr) { | |
3341 | ST(0) = sv_2mortal(newSVpvn(pathstr, strlen(pathstr))); | |
3342 | XSRETURN(1); | |
3343 | } | |
3344 | XSRETURN_EMPTY; | |
3345 | } | |
3346 | ||
3347 | static | |
ad0751ec GS |
3348 | XS(w32_Sleep) |
3349 | { | |
3350 | dXSARGS; | |
3351 | if (items != 1) | |
4f63d024 | 3352 | Perl_croak(aTHX_ "usage: Win32::Sleep($milliseconds)"); |
ad0751ec GS |
3353 | Sleep(SvIV(ST(0))); |
3354 | XSRETURN_YES; | |
3355 | } | |
3356 | ||
7509b657 GS |
3357 | static |
3358 | XS(w32_CopyFile) | |
3359 | { | |
3360 | dXSARGS; | |
3361 | if (items != 3) | |
4f63d024 | 3362 | Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)"); |
7509b657 GS |
3363 | if (CopyFile(SvPV_nolen(ST(0)), SvPV_nolen(ST(1)), !SvTRUE(ST(2)))) |
3364 | XSRETURN_YES; | |
3365 | XSRETURN_NO; | |
3366 | } | |
3367 | ||
ad2e33dc | 3368 | void |
c5be433b | 3369 | Perl_init_os_extras(void) |
ad2e33dc | 3370 | { |
c5be433b | 3371 | dTHXo; |
ad2e33dc GS |
3372 | char *file = __FILE__; |
3373 | dXSUB_SYS; | |
3374 | ||
4b556e6c JD |
3375 | w32_perlshell_tokens = Nullch; |
3376 | w32_perlshell_items = -1; | |
3377 | w32_fdpid = newAV(); /* XXX needs to be in Perl_win32_init()? */ | |
0aaad0ff | 3378 | New(1313, w32_children, 1, child_tab); |
4b556e6c | 3379 | w32_num_children = 0; |
4b556e6c | 3380 | |
ad2e33dc GS |
3381 | /* these names are Activeware compatible */ |
3382 | newXS("Win32::GetCwd", w32_GetCwd, file); | |
3383 | newXS("Win32::SetCwd", w32_SetCwd, file); | |
3384 | newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file); | |
3385 | newXS("Win32::GetLastError", w32_GetLastError, file); | |
ca135624 | 3386 | newXS("Win32::SetLastError", w32_SetLastError, file); |
ad2e33dc GS |
3387 | newXS("Win32::LoginName", w32_LoginName, file); |
3388 | newXS("Win32::NodeName", w32_NodeName, file); | |
3389 | newXS("Win32::DomainName", w32_DomainName, file); | |
3390 | newXS("Win32::FsType", w32_FsType, file); | |
3391 | newXS("Win32::GetOSVersion", w32_GetOSVersion, file); | |
3392 | newXS("Win32::IsWinNT", w32_IsWinNT, file); | |
3393 | newXS("Win32::IsWin95", w32_IsWin95, file); | |
3394 | newXS("Win32::FormatMessage", w32_FormatMessage, file); | |
3395 | newXS("Win32::Spawn", w32_Spawn, file); | |
3396 | newXS("Win32::GetTickCount", w32_GetTickCount, file); | |
3397 | newXS("Win32::GetShortPathName", w32_GetShortPathName, file); | |
ca135624 | 3398 | newXS("Win32::GetFullPathName", w32_GetFullPathName, file); |
8ac9c18d | 3399 | newXS("Win32::GetLongPathName", w32_GetLongPathName, file); |
7509b657 | 3400 | newXS("Win32::CopyFile", w32_CopyFile, file); |
ad0751ec | 3401 | newXS("Win32::Sleep", w32_Sleep, file); |
ad2e33dc GS |
3402 | |
3403 | /* XXX Bloat Alert! The following Activeware preloads really | |
3404 | * ought to be part of Win32::Sys::*, so they're not included | |
3405 | * here. | |
3406 | */ | |
3407 | /* LookupAccountName | |
3408 | * LookupAccountSID | |
3409 | * InitiateSystemShutdown | |
3410 | * AbortSystemShutdown | |
3411 | * ExpandEnvrironmentStrings | |
3412 | */ | |
3413 | } | |
3414 | ||
3415 | void | |
3416 | Perl_win32_init(int *argcp, char ***argvp) | |
3417 | { | |
3418 | /* Disable floating point errors, Perl will trap the ones we | |
3419 | * care about. VC++ RTL defaults to switching these off | |
3420 | * already, but the Borland RTL doesn't. Since we don't | |
3421 | * want to be at the vendor's whim on the default, we set | |
3422 | * it explicitly here. | |
3423 | */ | |
a835ef8a | 3424 | #if !defined(_ALPHA_) && !defined(__GNUC__) |
ad2e33dc | 3425 | _control87(MCW_EM, MCW_EM); |
3dc9191e | 3426 | #endif |
4b556e6c | 3427 | MALLOC_INIT; |
ad2e33dc | 3428 | } |
d55594ae | 3429 | |
d18c6117 GS |
3430 | #ifdef USE_ITHREADS |
3431 | void | |
3432 | Perl_sys_intern_dup(pTHX_ struct interp_intern *src, struct interp_intern *dst) | |
3433 | { | |
3434 | dst->perlshell_tokens = Nullch; | |
3435 | dst->perlshell_vec = (char**)NULL; | |
3436 | dst->perlshell_items = 0; | |
3437 | dst->fdpid = newAV(); | |
3438 | New(1313, dst->children, 1, child_tab); | |
3439 | dst->children->num = 0; | |
3440 | dst->hostlist = src->hostlist; /* XXX */ | |
3441 | dst->thr_intern.Winit_socktype = src->thr_intern.Winit_socktype; | |
3442 | } | |
3443 | #endif | |
3444 | ||
a868473f NIS |
3445 | #ifdef USE_BINMODE_SCRIPTS |
3446 | ||
3447 | void | |
3448 | win32_strip_return(SV *sv) | |
3449 | { | |
3450 | char *s = SvPVX(sv); | |
3451 | char *e = s+SvCUR(sv); | |
3452 | char *d = s; | |
3453 | while (s < e) | |
3454 | { | |
3455 | if (*s == '\r' && s[1] == '\n') | |
3456 | { | |
3457 | *d++ = '\n'; | |
3458 | s += 2; | |
3459 | } | |
3460 | else | |
3461 | { | |
3462 | *d++ = *s++; | |
3463 | } | |
3464 | } | |
3465 | SvCUR_set(sv,d-SvPVX(sv)); | |
3466 | } | |
3467 | ||
3468 | #endif |