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