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