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