7 #undef USE_DYNAMIC_LOADING
12 #include <sys/cygwin.h>
18 * pp_system() implemented via spawn()
19 * - more efficient and useful when embedding Perl in non-Cygwin apps
20 * - code mostly borrowed from djgpp.c
23 do_spawnvp (const char *path, const char * const *argv)
26 Sigsave_t ihand,qhand;
27 int childpid, result, status;
29 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
30 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
31 childpid = spawnvp(_P_NOWAIT,path,argv);
35 Perl_warner(aTHX_ packWARN(WARN_EXEC),"Can't spawn \"%s\": %s",
36 path,Strerror (errno));
39 result = wait4pid(childpid, &status, 0);
40 } while (result == -1 && errno == EINTR);
44 (void)rsignal_restore(SIGINT, &ihand);
45 (void)rsignal_restore(SIGQUIT, &qhand);
50 do_aspawn (SV *really, void **mark, void **sp)
54 char **a,*tmps,**argv;
59 a=argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
63 *a++ = SvPVx((SV *)*mark, n_a);
68 if (argv[0][0] != '/' && argv[0][0] != '\\'
69 && !(argv[0][0] && argv[0][1] == ':'
70 && (argv[0][2] == '/' || argv[0][2] != '\\'))
71 ) /* will swawnvp use PATH? */
72 TAINT_ENV(); /* testing IFS here is overkill, probably */
74 if (really && *(tmps = SvPV(really, n_a)))
75 rc=do_spawnvp (tmps,(const char * const *)argv);
77 rc=do_spawnvp (argv[0],(const char *const *)argv);
87 char *s,*metachars = "$&*(){}[]'\";\\?>|<~`\n";
88 const char *command[4];
90 while (*cmd && isSPACE(*cmd))
93 if (strnEQ (cmd,"/bin/sh",7) && isSPACE (cmd[7]))
96 /* save an extra exec if possible */
97 /* see if there are shell metacharacters in it */
98 if (strstr (cmd,"..."))
100 if (*cmd=='.' && isSPACE (cmd[1]))
102 if (strnEQ (cmd,"exec",4) && isSPACE (cmd[4]))
104 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
109 if (strchr (metachars,*s))
111 if (*s=='\n' && s[1]=='\0')
122 return do_spawnvp("sh",command);
125 Newx (PL_Argv,(s-cmd)/2+2,const char*);
126 PL_Cmd=savepvn (cmd,s-cmd);
128 for (s=PL_Cmd; *s;) {
129 while (*s && isSPACE (*s)) s++;
132 while (*s && !isSPACE (*s)) s++;
140 return do_spawnvp(PL_Argv[0],(const char * const *)PL_Argv);
143 /* see also Cwd.pm */
149 /* See http://rt.perl.org/rt3/Ticket/Display.html?id=38628
150 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
153 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
154 if((cwd = getcwd(NULL, -1))) {
155 ST(0) = sv_2mortal(newSVpv(cwd, 0));
157 #ifndef INCOMPLETE_TAINTS
165 XS(XS_Cygwin_pid_to_winpid)
172 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
174 pid = (pid_t)SvIV(ST(0));
176 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
177 XSprePUSH; PUSHi((IV)RETVAL);
183 XS(XS_Cygwin_winpid_to_pid)
190 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
192 pid = (pid_t)SvIV(ST(0));
194 if ((RETVAL = cygwin32_winpid_to_pid(pid)) > 0) {
195 XSprePUSH; PUSHi((IV)RETVAL);
201 XS(XS_Cygwin_win_to_posix_path)
204 int absolute_flag = 0;
207 char *pathname, *buf;
209 if (items < 1 || items > 2)
210 Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
212 pathname = SvPV(ST(0), len);
214 absolute_flag = SvTRUE(ST(1));
217 Perl_croak(aTHX_ "can't convert empty path");
218 buf = (char *) safemalloc (len + 260 + 1001);
221 err = cygwin_conv_to_full_posix_path(pathname, buf);
223 err = cygwin_conv_to_posix_path(pathname, buf);
225 ST(0) = sv_2mortal(newSVpv(buf, 0));
234 XS(XS_Cygwin_posix_to_win_path)
237 int absolute_flag = 0;
240 char *pathname, *buf;
242 if (items < 1 || items > 2)
243 Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
245 pathname = SvPV(ST(0), len);
247 absolute_flag = SvTRUE(ST(1));
250 Perl_croak(aTHX_ "can't convert empty path");
251 buf = (char *) safemalloc(len + 260 + 1001);
254 err = cygwin_conv_to_full_win32_path(pathname, buf);
256 err = cygwin_conv_to_win32_path(pathname, buf);
258 ST(0) = sv_2mortal(newSVpv(buf, 0));
267 XS(XS_Cygwin_mount_table)
273 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
274 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
277 while ((mnt = getmntent (0))) {
279 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
280 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
281 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
282 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
283 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
289 XS(XS_Cygwin_mount_flags)
296 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags(mnt_dir|'/cygwin')");
298 pathname = SvPV_nolen(ST(0));
300 /* TODO: Check for cygdrive registry setting,
301 * and then use CW_GET_CYGDRIVE_INFO
303 if (!strcmp(pathname, "/cygdrive")) {
306 char user_flags[260];
307 char system_flags[260];
309 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system, user_flags,
312 if (strlen(user) > 0) {
313 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
315 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
318 ST(0) = sv_2mortal(newSVpv(flags, 0));
324 while ((mnt = getmntent (0))) {
325 if (!strcmp(pathname, mnt->mnt_dir)) {
326 strcpy(flags, mnt->mnt_type);
327 if (strlen(mnt->mnt_opts) > 0) {
329 strcat(flags, mnt->mnt_opts);
335 ST(0) = sv_2mortal(newSVpv(flags, 0));
340 XS(XS_Cygwin_is_binmount)
346 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
348 pathname = SvPV_nolen(ST(0));
350 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
358 char *file = __FILE__;
361 newXS("Cwd::cwd", Cygwin_cwd, file);
362 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
363 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
364 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
365 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
366 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
367 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
368 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
370 /* Initialize Win32CORE if it has been statically linked. */
371 handle = dlopen(NULL, RTLD_LAZY);
373 void (*pfn_init)(pTHX);
374 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");