forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin32.c
4811 lines (4294 loc) · 118 KB
/
win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* WIN32.C
*
* (c) 1995 Microsoft Corporation. All rights reserved.
* Developed by hip communications inc.
* Portions (c) 1993 Intergraph Corporation. All rights reserved.
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*/
#define PERLIO_NOT_STDIO 0
#define WIN32_LEAN_AND_MEAN
#define WIN32IO_IS_STDIO
#include <tchar.h>
#ifdef __GNUC__
# define Win32_Winsock
#endif
#ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x0500 /* needed for CreateHardlink() etc. */
#endif
#include <windows.h>
#ifndef HWND_MESSAGE
# define HWND_MESSAGE ((HWND)-3)
#endif
#ifndef PROCESSOR_ARCHITECTURE_AMD64
# define PROCESSOR_ARCHITECTURE_AMD64 9
#endif
#ifndef WC_NO_BEST_FIT_CHARS
# define WC_NO_BEST_FIT_CHARS 0x00000400
#endif
#include <winnt.h>
#include <commctrl.h>
#include <tlhelp32.h>
#include <io.h>
#include <signal.h>
/* #include "config.h" */
#define PerlIO FILE
#include <sys/stat.h>
#include "EXTERN.h"
#include "perl.h"
#define NO_XSLOCKS
#define PERL_NO_GET_CONTEXT
#include "XSUB.h"
#include <fcntl.h>
#ifndef __GNUC__
/* assert.h conflicts with #define of assert in perl.h */
# include <assert.h>
#endif
#include <string.h>
#include <stdarg.h>
#include <float.h>
#include <time.h>
#include <sys/utime.h>
#ifdef __GNUC__
/* Mingw32 defaults to globing command line
* So we turn it off like this:
*/
int _CRT_glob = 0;
#endif
#if defined(__MINGW32__) && (__MINGW32_MAJOR_VERSION==1)
/* Mingw32-1.1 is missing some prototypes */
START_EXTERN_C
FILE * _wfopen(LPCWSTR wszFileName, LPCWSTR wszMode);
FILE * _wfdopen(int nFd, LPCWSTR wszMode);
FILE * _freopen(LPCWSTR wszFileName, LPCWSTR wszMode, FILE * pOldStream);
int _flushall();
int _fcloseall();
END_EXTERN_C
#endif
#define EXECF_EXEC 1
#define EXECF_SPAWN 2
#define EXECF_SPAWN_NOWAIT 3
#if defined(PERL_IMPLICIT_SYS)
# undef getlogin
# define getlogin g_getlogin
#endif
/* VS2005 (MSC version 14) provides a mechanism to set an invalid
* parameter handler. This functionality is not available in the
* 64-bit compiler from the Platform SDK, which unfortunately also
* believes itself to be MSC version 14.
*
* There is no #define related to _set_invalid_parameter_handler(),
* but we can check for one of the constants defined for
* _set_abort_behavior(), which was introduced into stdlib.h at
* the same time.
*/
#if _MSC_VER >= 1400 && defined(_WRITE_ABORT_MSG)
# define SET_INVALID_PARAMETER_HANDLER
#endif
#ifdef SET_INVALID_PARAMETER_HANDLER
static BOOL set_silent_invalid_parameter_handler(BOOL newvalue);
static void my_invalid_parameter_handler(const wchar_t* expression,
const wchar_t* function, const wchar_t* file,
unsigned int line, uintptr_t pReserved);
#endif
#ifndef WIN32_NO_REGISTRY
static char* get_regstr_from(HKEY hkey, const char *valuename, SV **svp);
static char* get_regstr(const char *valuename, SV **svp);
#endif
static char* get_emd_part(SV **prev_pathp, STRLEN *const len,
char *trailing, ...);
static char* win32_get_xlib(const char *pl,
WIN32_NO_REGISTRY_M_(const char *xlib)
const char *libname, STRLEN *const len);
static BOOL has_shell_metachars(const char *ptr);
static long tokenize(const char *str, char **dest, char ***destv);
static void get_shell(void);
static char* find_next_space(const char *s);
static int do_spawn2(pTHX_ const char *cmd, int exectype);
static int do_spawn2_handles(pTHX_ const char *cmd, int exectype,
const int *handles);
static int do_spawnvp_handles(int mode, const char *cmdname,
const char * const *argv, const int *handles);
static PerlIO * do_popen(const char *mode, const char *command, IV narg,
SV **args);
static long find_pid(pTHX_ int pid);
static void remove_dead_process(long child);
static int terminate_process(DWORD pid, HANDLE process_handle, int sig);
static int my_killpg(int pid, int sig);
static int my_kill(int pid, int sig);
static void out_of_memory(void);
static char* wstr_to_str(const wchar_t* wstr);
static long filetime_to_clock(PFILETIME ft);
static BOOL filetime_from_time(PFILETIME ft, time_t t);
static char* create_command_line(char *cname, STRLEN clen,
const char * const *args);
static char* qualified_path(const char *cmd, bool other_exts);
static void ansify_path(void);
static LRESULT win32_process_message(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam);
#ifdef USE_ITHREADS
static long find_pseudo_pid(pTHX_ int pid);
static void remove_dead_pseudo_process(long child);
static HWND get_hwnd_delay(pTHX, long child, DWORD tries);
#endif
#ifdef HAVE_INTERP_INTERN
static void win32_csighandler(int sig);
#endif
START_EXTERN_C
HANDLE w32_perldll_handle = INVALID_HANDLE_VALUE;
char w32_module_name[MAX_PATH+1];
#ifdef WIN32_DYN_IOINFO_SIZE
Size_t w32_ioinfo_size;/* avoid 0 extend op b4 mul, otherwise could be a U8 */
#endif
END_EXTERN_C
static OSVERSIONINFO g_osver = {0, 0, 0, 0, 0, ""};
#ifndef WIN32_NO_REGISTRY
/* initialized by Perl_win32_init/PERL_SYS_INIT */
static HKEY HKCU_Perl_hnd;
static HKEY HKLM_Perl_hnd;
#endif
#ifdef SET_INVALID_PARAMETER_HANDLER
static BOOL silent_invalid_parameter_handler = FALSE;
static BOOL
set_silent_invalid_parameter_handler(BOOL newvalue)
{
BOOL oldvalue = silent_invalid_parameter_handler;
# ifdef _DEBUG
silent_invalid_parameter_handler = newvalue;
# endif
return oldvalue;
}
static void
my_invalid_parameter_handler(const wchar_t* expression,
const wchar_t* function,
const wchar_t* file,
unsigned int line,
uintptr_t pReserved)
{
# ifdef _DEBUG
char* ansi_expression;
char* ansi_function;
char* ansi_file;
if (silent_invalid_parameter_handler)
return;
ansi_expression = wstr_to_str(expression);
ansi_function = wstr_to_str(function);
ansi_file = wstr_to_str(file);
fprintf(stderr, "Invalid parameter detected in function %s. "
"File: %s, line: %d\n", ansi_function, ansi_file, line);
fprintf(stderr, "Expression: %s\n", ansi_expression);
free(ansi_expression);
free(ansi_function);
free(ansi_file);
# endif
}
#endif
EXTERN_C void
set_w32_module_name(void)
{
/* this function may be called at DLL_PROCESS_ATTACH time */
char* ptr;
HMODULE module = (HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
? GetModuleHandle(NULL)
: w32_perldll_handle);
WCHAR modulename[MAX_PATH];
WCHAR fullname[MAX_PATH];
char *ansi;
DWORD (__stdcall *pfnGetLongPathNameW)(LPCWSTR, LPWSTR, DWORD) =
(DWORD (__stdcall *)(LPCWSTR, LPWSTR, DWORD))
GetProcAddress(GetModuleHandle("kernel32.dll"), "GetLongPathNameW");
GetModuleFileNameW(module, modulename, sizeof(modulename)/sizeof(WCHAR));
/* Make sure we get an absolute pathname in case the module was loaded
* explicitly by LoadLibrary() with a relative path. */
GetFullPathNameW(modulename, sizeof(fullname)/sizeof(WCHAR), fullname, NULL);
/* Make sure we start with the long path name of the module because we
* later scan for pathname components to match "5.xx" to locate
* compatible sitelib directories, and the short pathname might mangle
* this path segment (e.g. by removing the dot on NTFS to something
* like "5xx~1.yy") */
if (pfnGetLongPathNameW)
pfnGetLongPathNameW(fullname, fullname, sizeof(fullname)/sizeof(WCHAR));
/* remove \\?\ prefix */
if (memcmp(fullname, L"\\\\?\\", 4*sizeof(WCHAR)) == 0)
memmove(fullname, fullname+4, (wcslen(fullname+4)+1)*sizeof(WCHAR));
ansi = win32_ansipath(fullname);
my_strlcpy(w32_module_name, ansi, sizeof(w32_module_name));
win32_free(ansi);
/* normalize to forward slashes */
ptr = w32_module_name;
while (*ptr) {
if (*ptr == '\\')
*ptr = '/';
++ptr;
}
}
#ifndef WIN32_NO_REGISTRY
/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
static char*
get_regstr_from(HKEY handle, const char *valuename, SV **svp)
{
/* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
DWORD type;
char *str = NULL;
long retval;
DWORD datalen;
retval = RegQueryValueEx(handle, valuename, 0, &type, NULL, &datalen);
if (retval == ERROR_SUCCESS
&& (type == REG_SZ || type == REG_EXPAND_SZ))
{
dTHX;
if (!*svp)
*svp = sv_2mortal(newSVpvs(""));
SvGROW(*svp, datalen);
retval = RegQueryValueEx(handle, valuename, 0, NULL,
(PBYTE)SvPVX(*svp), &datalen);
if (retval == ERROR_SUCCESS) {
str = SvPVX(*svp);
SvCUR_set(*svp,datalen-1);
}
}
return str;
}
/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
static char*
get_regstr(const char *valuename, SV **svp)
{
char *str;
if (HKCU_Perl_hnd) {
str = get_regstr_from(HKCU_Perl_hnd, valuename, svp);
if (!str)
goto try_HKLM;
}
else {
try_HKLM:
if (HKLM_Perl_hnd)
str = get_regstr_from(HKLM_Perl_hnd, valuename, svp);
else
str = NULL;
}
return str;
}
#endif /* ifndef WIN32_NO_REGISTRY */
/* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
static char *
get_emd_part(SV **prev_pathp, STRLEN *const len, char *trailing_path, ...)
{
char base[10];
va_list ap;
char mod_name[MAX_PATH+1];
char *ptr;
char *optr;
char *strip;
STRLEN baselen;
va_start(ap, trailing_path);
strip = va_arg(ap, char *);
sprintf(base, "%d.%d", (int)PERL_REVISION, (int)PERL_VERSION);
baselen = strlen(base);
if (!*w32_module_name) {
set_w32_module_name();
}
strcpy(mod_name, w32_module_name);
ptr = strrchr(mod_name, '/');
while (ptr && strip) {
/* look for directories to skip back */
optr = ptr;
*ptr = '\0';
ptr = strrchr(mod_name, '/');
/* avoid stripping component if there is no slash,
* or it doesn't match ... */
if (!ptr || stricmp(ptr+1, strip) != 0) {
/* ... but not if component matches m|5\.$patchlevel.*| */
if (!ptr || !(*strip == '5' && *(ptr+1) == '5'
&& strnEQ(strip, base, baselen)
&& strnEQ(ptr+1, base, baselen)))
{
*optr = '/';
ptr = optr;
}
}
strip = va_arg(ap, char *);
}
if (!ptr) {
ptr = mod_name;
*ptr++ = '.';
*ptr = '/';
}
va_end(ap);
strcpy(++ptr, trailing_path);
/* only add directory if it exists */
if (GetFileAttributes(mod_name) != (DWORD) -1) {
/* directory exists */
dTHX;
if (!*prev_pathp)
*prev_pathp = sv_2mortal(newSVpvs(""));
else if (SvPVX(*prev_pathp))
sv_catpvs(*prev_pathp, ";");
sv_catpv(*prev_pathp, mod_name);
if(len)
*len = SvCUR(*prev_pathp);
return SvPVX(*prev_pathp);
}
return NULL;
}
EXTERN_C char *
win32_get_privlib(WIN32_NO_REGISTRY_M_(const char *pl) STRLEN *const len)
{
char *stdlib = "lib";
SV *sv = NULL;
#ifndef WIN32_NO_REGISTRY
char buffer[MAX_PATH+1];
/* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
sprintf(buffer, "%s-%s", stdlib, pl);
if (!get_regstr(buffer, &sv))
(void)get_regstr(stdlib, &sv);
#endif
/* $stdlib .= ";$EMD/../../lib" */
return get_emd_part(&sv, len, stdlib, ARCHNAME, "bin", NULL);
}
static char *
win32_get_xlib(const char *pl, WIN32_NO_REGISTRY_M_(const char *xlib)
const char *libname, STRLEN *const len)
{
#ifndef WIN32_NO_REGISTRY
char regstr[40];
#endif
char pathstr[MAX_PATH+1];
SV *sv1 = NULL;
SV *sv2 = NULL;
#ifndef WIN32_NO_REGISTRY
/* $HKCU{"$xlib-$]"} || $HKLM{"$xlib-$]"} . ---; */
sprintf(regstr, "%s-%s", xlib, pl);
(void)get_regstr(regstr, &sv1);
#endif
/* $xlib .=
* ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/$]/lib"; */
sprintf(pathstr, "%s/%s/lib", libname, pl);
(void)get_emd_part(&sv1, NULL, pathstr, ARCHNAME, "bin", pl, NULL);
#ifndef WIN32_NO_REGISTRY
/* $HKCU{$xlib} || $HKLM{$xlib} . ---; */
(void)get_regstr(xlib, &sv2);
#endif
/* $xlib .=
* ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/$libname/lib"; */
sprintf(pathstr, "%s/lib", libname);
(void)get_emd_part(&sv2, NULL, pathstr, ARCHNAME, "bin", pl, NULL);
if (!sv1 && !sv2)
return NULL;
if (!sv1) {
sv1 = sv2;
} else if (sv2) {
dTHX;
sv_catpvs(sv1, ";");
sv_catsv(sv1, sv2);
}
if (len)
*len = SvCUR(sv1);
return SvPVX(sv1);
}
EXTERN_C char *
win32_get_sitelib(const char *pl, STRLEN *const len)
{
return win32_get_xlib(pl, WIN32_NO_REGISTRY_M_("sitelib") "site", len);
}
#ifndef PERL_VENDORLIB_NAME
# define PERL_VENDORLIB_NAME "vendor"
#endif
EXTERN_C char *
win32_get_vendorlib(const char *pl, STRLEN *const len)
{
return win32_get_xlib(pl, WIN32_NO_REGISTRY_M_("vendorlib") PERL_VENDORLIB_NAME, len);
}
static BOOL
has_shell_metachars(const char *ptr)
{
int inquote = 0;
char quote = '\0';
/*
* Scan string looking for redirection (< or >) or pipe
* characters (|) that are not in a quoted string.
* Shell variable interpolation (%VAR%) can also happen inside strings.
*/
while (*ptr) {
switch(*ptr) {
case '%':
return TRUE;
case '\'':
case '\"':
if (inquote) {
if (quote == *ptr) {
inquote = 0;
quote = '\0';
}
}
else {
quote = *ptr;
inquote++;
}
break;
case '>':
case '<':
case '|':
if (!inquote)
return TRUE;
default:
break;
}
++ptr;
}
return FALSE;
}
#if !defined(PERL_IMPLICIT_SYS)
/* since the current process environment is being updated in util.c
* the library functions will get the correct environment
*/
PerlIO *
Perl_my_popen(pTHX_ const char *cmd, const char *mode)
{
PERL_FLUSHALL_FOR_CHILD;
return win32_popen(cmd, mode);
}
long
Perl_my_pclose(pTHX_ PerlIO *fp)
{
return win32_pclose(fp);
}
#endif
DllExport unsigned long
win32_os_id(void)
{
return (unsigned long)g_osver.dwPlatformId;
}
DllExport int
win32_getpid(void)
{
#ifdef USE_ITHREADS
dTHX;
if (w32_pseudo_id)
return -((int)w32_pseudo_id);
#endif
return _getpid();
}
/* Tokenize a string. Words are null-separated, and the list
* ends with a doubled null. Any character (except null and
* including backslash) may be escaped by preceding it with a
* backslash (the backslash will be stripped).
* Returns number of words in result buffer.
*/
static long
tokenize(const char *str, char **dest, char ***destv)
{
char *retstart = NULL;
char **retvstart = 0;
int items = -1;
if (str) {
int slen = strlen(str);
char *ret;
char **retv;
Newx(ret, slen+2, char);
Newx(retv, (slen+3)/2, char*);
retstart = ret;
retvstart = retv;
*retv = ret;
items = 0;
while (*str) {
*ret = *str++;
if (*ret == '\\' && *str)
*ret = *str++;
else if (*ret == ' ') {
while (*str == ' ')
str++;
if (ret == retstart)
ret--;
else {
*ret = '\0';
++items;
if (*str)
*++retv = ret+1;
}
}
else if (!*str)
++items;
ret++;
}
retvstart[items] = NULL;
*ret++ = '\0';
*ret = '\0';
}
*dest = retstart;
*destv = retvstart;
return items;
}
static void
get_shell(void)
{
dTHX;
if (!w32_perlshell_tokens) {
/* we don't use COMSPEC here for two reasons:
* 1. the same reason perl on UNIX doesn't use SHELL--rampant and
* uncontrolled unportability of the ensuing scripts.
* 2. PERL5SHELL could be set to a shell that may not be fit for
* interactive use (which is what most programs look in COMSPEC
* for).
*/
const char* defaultshell = "cmd.exe /x/d/c";
const char *usershell = PerlEnv_getenv("PERL5SHELL");
w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
&w32_perlshell_tokens,
&w32_perlshell_vec);
}
}
int
Perl_do_aspawn(pTHX_ SV *really, SV **mark, SV **sp)
{
char **argv;
char *str;
int status;
int flag = P_WAIT;
int index = 0;
int eno;
PERL_ARGS_ASSERT_DO_ASPAWN;
if (sp <= mark)
return -1;
get_shell();
Newx(argv, (sp - mark) + w32_perlshell_items + 2, char*);
if (SvNIOKp(*(mark+1)) && !SvPOKp(*(mark+1))) {
++mark;
flag = SvIVx(*mark);
}
while (++mark <= sp) {
if (*mark && (str = SvPV_nolen(*mark)))
argv[index++] = str;
else
argv[index++] = "";
}
argv[index++] = 0;
status = win32_spawnvp(flag,
(const char*)(really ? SvPV_nolen(really) : argv[0]),
(const char* const*)argv);
if (status < 0 && (eno = errno, (eno == ENOEXEC || eno == ENOENT))) {
/* possible shell-builtin, invoke with shell */
int sh_items;
sh_items = w32_perlshell_items;
while (--index >= 0)
argv[index+sh_items] = argv[index];
while (--sh_items >= 0)
argv[sh_items] = w32_perlshell_vec[sh_items];
status = win32_spawnvp(flag,
(const char*)(really ? SvPV_nolen(really) : argv[0]),
(const char* const*)argv);
}
if (flag == P_NOWAIT) {
PL_statusvalue = -1; /* >16bits hint for pp_system() */
}
else {
if (status < 0) {
if (ckWARN(WARN_EXEC))
Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't spawn \"%s\": %s", argv[0], strerror(errno));
status = 255 * 256;
}
else
status *= 256;
PL_statusvalue = status;
}
Safefree(argv);
return (status);
}
/* returns pointer to the next unquoted space or the end of the string */
static char*
find_next_space(const char *s)
{
bool in_quotes = FALSE;
while (*s) {
/* ignore doubled backslashes, or backslash+quote */
if (*s == '\\' && (s[1] == '\\' || s[1] == '"')) {
s += 2;
}
/* keep track of when we're within quotes */
else if (*s == '"') {
s++;
in_quotes = !in_quotes;
}
/* break it up only at spaces that aren't in quotes */
else if (!in_quotes && isSPACE(*s))
return (char*)s;
else
s++;
}
return (char*)s;
}
static int
do_spawn2(pTHX_ const char *cmd, int exectype) {
return do_spawn2_handles(aTHX_ cmd, exectype, NULL);
}
static int
do_spawn2_handles(pTHX_ const char *cmd, int exectype, const int *handles)
{
char **a;
char *s;
char **argv;
int status = -1;
BOOL needToTry = TRUE;
char *cmd2;
/* Save an extra exec if possible. See if there are shell
* metacharacters in it */
if (!has_shell_metachars(cmd)) {
Newx(argv, strlen(cmd) / 2 + 2, char*);
Newx(cmd2, strlen(cmd) + 1, char);
strcpy(cmd2, cmd);
a = argv;
for (s = cmd2; *s;) {
while (*s && isSPACE(*s))
s++;
if (*s)
*(a++) = s;
s = find_next_space(s);
if (*s)
*s++ = '\0';
}
*a = NULL;
if (argv[0]) {
switch (exectype) {
case EXECF_SPAWN:
status = win32_spawnvp(P_WAIT, argv[0],
(const char* const*)argv);
break;
case EXECF_SPAWN_NOWAIT:
status = do_spawnvp_handles(P_NOWAIT, argv[0],
(const char* const*)argv, handles);
break;
case EXECF_EXEC:
status = win32_execvp(argv[0], (const char* const*)argv);
break;
}
if (status != -1 || errno == 0)
needToTry = FALSE;
}
Safefree(argv);
Safefree(cmd2);
}
if (needToTry) {
char **argv;
int i = -1;
get_shell();
Newx(argv, w32_perlshell_items + 2, char*);
while (++i < w32_perlshell_items)
argv[i] = w32_perlshell_vec[i];
argv[i++] = (char *)cmd;
argv[i] = NULL;
switch (exectype) {
case EXECF_SPAWN:
status = win32_spawnvp(P_WAIT, argv[0],
(const char* const*)argv);
break;
case EXECF_SPAWN_NOWAIT:
status = do_spawnvp_handles(P_NOWAIT, argv[0],
(const char* const*)argv, handles);
break;
case EXECF_EXEC:
status = win32_execvp(argv[0], (const char* const*)argv);
break;
}
cmd = argv[0];
Safefree(argv);
}
if (exectype == EXECF_SPAWN_NOWAIT) {
PL_statusvalue = -1; /* >16bits hint for pp_system() */
}
else {
if (status < 0) {
if (ckWARN(WARN_EXEC))
Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't %s \"%s\": %s",
(exectype == EXECF_EXEC ? "exec" : "spawn"),
cmd, strerror(errno));
status = 255 * 256;
}
else
status *= 256;
PL_statusvalue = status;
}
return (status);
}
int
Perl_do_spawn(pTHX_ char *cmd)
{
PERL_ARGS_ASSERT_DO_SPAWN;
return do_spawn2(aTHX_ cmd, EXECF_SPAWN);
}
int
Perl_do_spawn_nowait(pTHX_ char *cmd)
{
PERL_ARGS_ASSERT_DO_SPAWN_NOWAIT;
return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT);
}
bool
Perl_do_exec(pTHX_ const char *cmd)
{
PERL_ARGS_ASSERT_DO_EXEC;
do_spawn2(aTHX_ cmd, EXECF_EXEC);
return FALSE;
}
/* The idea here is to read all the directory names into a string table
* (separated by nulls) and when one of the other dir functions is called
* return the pointer to the current file name.
*/
DllExport DIR *
win32_opendir(const char *filename)
{
dTHXa(NULL);
DIR *dirp;
long len;
long idx;
char scanname[MAX_PATH+3];
WCHAR wscanname[sizeof(scanname)];
WIN32_FIND_DATAW wFindData;
char buffer[MAX_PATH*2];
BOOL use_default;
len = strlen(filename);
if (len == 0) {
errno = ENOENT;
return NULL;
}
if (len > MAX_PATH) {
errno = ENAMETOOLONG;
return NULL;
}
/* Get us a DIR structure */
Newxz(dirp, 1, DIR);
/* Create the search pattern */
strcpy(scanname, filename);
/* bare drive name means look in cwd for drive */
if (len == 2 && isALPHA(scanname[0]) && scanname[1] == ':') {
scanname[len++] = '.';
scanname[len++] = '/';
}
else if (scanname[len-1] != '/' && scanname[len-1] != '\\') {
scanname[len++] = '/';
}
scanname[len++] = '*';
scanname[len] = '\0';
/* do the FindFirstFile call */
MultiByteToWideChar(CP_ACP, 0, scanname, -1, wscanname, sizeof(wscanname)/sizeof(WCHAR));
aTHXa(PERL_GET_THX);
dirp->handle = FindFirstFileW(PerlDir_mapW(wscanname), &wFindData);
if (dirp->handle == INVALID_HANDLE_VALUE) {
DWORD err = GetLastError();
/* FindFirstFile() fails on empty drives! */
switch (err) {
case ERROR_FILE_NOT_FOUND:
return dirp;
case ERROR_NO_MORE_FILES:
case ERROR_PATH_NOT_FOUND:
errno = ENOENT;
break;
case ERROR_NOT_ENOUGH_MEMORY:
errno = ENOMEM;
break;
default:
errno = EINVAL;
break;
}
Safefree(dirp);
return NULL;
}
use_default = FALSE;
WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
wFindData.cFileName, -1,
buffer, sizeof(buffer), NULL, &use_default);
if (use_default && *wFindData.cAlternateFileName) {
WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
wFindData.cAlternateFileName, -1,
buffer, sizeof(buffer), NULL, NULL);
}
/* now allocate the first part of the string table for
* the filenames that we find.
*/
idx = strlen(buffer)+1;
if (idx < 256)
dirp->size = 256;
else
dirp->size = idx;
Newx(dirp->start, dirp->size, char);
strcpy(dirp->start, buffer);
dirp->nfiles++;
dirp->end = dirp->curr = dirp->start;
dirp->end += idx;
return dirp;
}
/* Readdir just returns the current string pointer and bumps the
* string pointer to the nDllExport entry.
*/
DllExport struct direct *
win32_readdir(DIR *dirp)
{
long len;
if (dirp->curr) {
/* first set up the structure to return */
len = strlen(dirp->curr);
strcpy(dirp->dirstr.d_name, dirp->curr);
dirp->dirstr.d_namlen = len;
/* Fake an inode */
dirp->dirstr.d_ino = dirp->curr - dirp->start;
/* Now set up for the next call to readdir */
dirp->curr += len + 1;
if (dirp->curr >= dirp->end) {
BOOL res;
char buffer[MAX_PATH*2];
if (dirp->handle == INVALID_HANDLE_VALUE) {
res = 0;
}
/* finding the next file that matches the wildcard
* (which should be all of them in this directory!).
*/
else {
WIN32_FIND_DATAW wFindData;
res = FindNextFileW(dirp->handle, &wFindData);
if (res) {
BOOL use_default = FALSE;
WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
wFindData.cFileName, -1,
buffer, sizeof(buffer), NULL, &use_default);
if (use_default && *wFindData.cAlternateFileName) {
WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
wFindData.cAlternateFileName, -1,
buffer, sizeof(buffer), NULL, NULL);
}
}
}
if (res) {
long endpos = dirp->end - dirp->start;
long newsize = endpos + strlen(buffer) + 1;
/* bump the string table size by enough for the
* new name and its null terminator */
while (newsize > dirp->size) {
long curpos = dirp->curr - dirp->start;
Renew(dirp->start, dirp->size * 2, char);
dirp->size *= 2;
dirp->curr = dirp->start + curpos;
}
strcpy(dirp->start + endpos, buffer);
dirp->end = dirp->start + newsize;
dirp->nfiles++;
}
else {
dirp->curr = NULL;
if (dirp->handle != INVALID_HANDLE_VALUE) {
FindClose(dirp->handle);
dirp->handle = INVALID_HANDLE_VALUE;
}
}
}
return &(dirp->dirstr);
}
else
return NULL;
}
/* Telldir returns the current string pointer position */
DllExport long
win32_telldir(DIR *dirp)
{
return dirp->curr ? (dirp->curr - dirp->start) : -1;
}