This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
RE: Problem in Win32CORE when building PAR-Packer-0.975 with bleadperl on Win32
[perl5.git] / cygwin / cygwin.c
CommitLineData
5db16f6a
FE
1/*
2 * Cygwin extras
3 */
4
5#include "EXTERN.h"
6#include "perl.h"
7#undef USE_DYNAMIC_LOADING
8#include "XSUB.h"
9
6b49d266 10#include <unistd.h>
b4bcd662 11#include <process.h>
49fd6edc 12#include <sys/cygwin.h>
6d7e4387 13#include <alloca.h>
9fb265f7 14#include <dlfcn.h>
5db16f6a 15
b4bcd662
GS
16/*
17 * pp_system() implemented via spawn()
18 * - more efficient and useful when embedding Perl in non-Cygwin apps
19 * - code mostly borrowed from djgpp.c
20 */
21static int
22do_spawnvp (const char *path, const char * const *argv)
23{
acfe0abc 24 dTHX;
b4bcd662
GS
25 Sigsave_t ihand,qhand;
26 int childpid, result, status;
27
ec1aec95
YST
28 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
29 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
b4bcd662
GS
30 childpid = spawnvp(_P_NOWAIT,path,argv);
31 if (childpid < 0) {
32 status = -1;
411caa50 33 if(ckWARN(WARN_EXEC))
f98bc0c6 34 Perl_warner(aTHX_ packWARN(WARN_EXEC),"Can't spawn \"%s\": %s",
b4bcd662 35 path,Strerror (errno));
b4bcd662
GS
36 } else {
37 do {
38 result = wait4pid(childpid, &status, 0);
39 } while (result == -1 && errno == EINTR);
40 if(result < 0)
41 status = -1;
42 }
43 (void)rsignal_restore(SIGINT, &ihand);
44 (void)rsignal_restore(SIGQUIT, &qhand);
45 return status;
46}
47
48int
49do_aspawn (SV *really, void **mark, void **sp)
50{
acfe0abc 51 dTHX;
b4bcd662
GS
52 int rc;
53 char **a,*tmps,**argv;
54 STRLEN n_a;
55
56 if (sp<=mark)
57 return -1;
58 a=argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
59
60 while (++mark <= sp)
61 if (*mark)
667e2948 62 *a++ = SvPVx((SV *)*mark, n_a);
b4bcd662
GS
63 else
64 *a++ = "";
65 *a = Nullch;
66
67 if (argv[0][0] != '/' && argv[0][0] != '\\'
68 && !(argv[0][0] && argv[0][1] == ':'
69 && (argv[0][2] == '/' || argv[0][2] != '\\'))
70 ) /* will swawnvp use PATH? */
71 TAINT_ENV(); /* testing IFS here is overkill, probably */
72
73 if (really && *(tmps = SvPV(really, n_a)))
74 rc=do_spawnvp (tmps,(const char * const *)argv);
75 else
76 rc=do_spawnvp (argv[0],(const char *const *)argv);
77
78 return rc;
79}
80
81int
82do_spawn (char *cmd)
83{
acfe0abc 84 dTHX;
b4bcd662
GS
85 char **a,*s,*metachars = "$&*(){}[]'\";\\?>|<~`\n";
86 const char *command[4];
87
88 while (*cmd && isSPACE(*cmd))
89 cmd++;
90
91 if (strnEQ (cmd,"/bin/sh",7) && isSPACE (cmd[7]))
92 cmd+=5;
93
94 /* save an extra exec if possible */
95 /* see if there are shell metacharacters in it */
96 if (strstr (cmd,"..."))
97 goto doshell;
98 if (*cmd=='.' && isSPACE (cmd[1]))
99 goto doshell;
100 if (strnEQ (cmd,"exec",4) && isSPACE (cmd[4]))
101 goto doshell;
102 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
103 if (*s=='=')
104 goto doshell;
105
106 for (s=cmd; *s; s++)
107 if (strchr (metachars,*s))
108 {
109 if (*s=='\n' && s[1]=='\0')
110 {
111 *s='\0';
112 break;
113 }
114 doshell:
115 command[0] = "sh";
116 command[1] = "-c";
117 command[2] = cmd;
118 command[3] = NULL;
119
120 return do_spawnvp("sh",command);
121 }
122
a02a5408 123 Newx (PL_Argv,(s-cmd)/2+2,char*);
b4bcd662
GS
124 PL_Cmd=savepvn (cmd,s-cmd);
125 a=PL_Argv;
126 for (s=PL_Cmd; *s;) {
127 while (*s && isSPACE (*s)) s++;
128 if (*s)
129 *(a++)=s;
130 while (*s && !isSPACE (*s)) s++;
131 if (*s)
132 *s++='\0';
133 }
134 *a=Nullch;
135 if (!PL_Argv[0])
136 return -1;
137
138 return do_spawnvp(PL_Argv[0],(const char * const *)PL_Argv);
139}
5db16f6a
FE
140
141/* see also Cwd.pm */
5db16f6a
FE
142XS(Cygwin_cwd)
143{
144 dXSARGS;
145 char *cwd;
146
147 if(items != 0)
148 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
47dafe4d 149 if((cwd = getcwd(NULL, -1))) {
5db16f6a 150 ST(0) = sv_2mortal(newSVpv(cwd, 0));
a236cecb 151 free(cwd);
6be3b590
JH
152#ifndef INCOMPLETE_TAINTS
153 SvTAINTED_on(ST(0));
154#endif
5db16f6a
FE
155 XSRETURN(1);
156 }
157 XSRETURN_UNDEF;
158}
159
49fd6edc
YST
160XS(XS_Cygwin_pid_to_winpid)
161{
162 dXSARGS;
d2dc0126
YST
163 dXSTARG;
164 pid_t pid, RETVAL;
165
49fd6edc
YST
166 if (items != 1)
167 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
d2dc0126
YST
168
169 pid = (pid_t)SvIV(ST(0));
170
49fd6edc
YST
171 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
172 XSprePUSH; PUSHi((IV)RETVAL);
173 XSRETURN(1);
174 }
175 XSRETURN_UNDEF;
176}
177
49fd6edc
YST
178XS(XS_Cygwin_winpid_to_pid)
179{
180 dXSARGS;
d2dc0126
YST
181 dXSTARG;
182 pid_t pid, RETVAL;
183
49fd6edc
YST
184 if (items != 1)
185 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
d2dc0126
YST
186
187 pid = (pid_t)SvIV(ST(0));
188
49fd6edc
YST
189 if ((RETVAL = cygwin32_winpid_to_pid(pid)) > 0) {
190 XSprePUSH; PUSHi((IV)RETVAL);
191 XSRETURN(1);
192 }
193 XSRETURN_UNDEF;
194}
195
196
5db16f6a
FE
197void
198init_os_extras(void)
199{
5db16f6a 200 dTHX;
9fb265f7
JD
201 char *file = __FILE__;
202 void *handle;
5db16f6a
FE
203
204 newXS("Cwd::cwd", Cygwin_cwd, file);
49fd6edc
YST
205 newXS("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file);
206 newXS("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file);
78ff2d7b 207
9fb265f7
JD
208 /* Initialize Win32CORE if it has been statically linked. */
209 handle = dlopen(NULL, RTLD_LAZY);
210 if (handle) {
211 void (*pfn_init)(pTHX);
212 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");
213 if (pfn_init)
214 pfn_init(aTHX);
215 dlclose(handle);
78ff2d7b 216 }
5db16f6a 217}