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