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