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