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