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)
60 argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
61 a=(char const **)argv;
65 *a++ = SvPVx((SV *)*mark, n_a);
70 if (argv[0][0] != '/' && argv[0][0] != '\\'
71 && !(argv[0][0] && argv[0][1] == ':'
72 && (argv[0][2] == '/' || argv[0][2] != '\\'))
73 ) /* will swawnvp use PATH? */
74 TAINT_ENV(); /* testing IFS here is overkill, probably */
76 if (really && *(tmps = SvPV(really, n_a)))
77 rc=do_spawnvp (tmps,(const char * const *)argv);
79 rc=do_spawnvp (argv[0],(const char *const *)argv);
90 char const *metachars = "$&*(){}[]'\";\\?>|<~`\n";
91 const char *command[4];
93 while (*cmd && isSPACE(*cmd))
96 if (strnEQ (cmd,"/bin/sh",7) && isSPACE (cmd[7]))
99 /* save an extra exec if possible */
100 /* see if there are shell metacharacters in it */
101 if (strstr (cmd,"..."))
103 if (*cmd=='.' && isSPACE (cmd[1]))
105 if (strnEQ (cmd,"exec",4) && isSPACE (cmd[4]))
107 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
112 if (strchr (metachars,*s))
114 if (*s=='\n' && s[1]=='\0')
125 return do_spawnvp("sh",command);
128 Newx (PL_Argv,(s-cmd)/2+2,const char*);
129 PL_Cmd=savepvn (cmd,s-cmd);
131 for (s=PL_Cmd; *s;) {
132 while (*s && isSPACE (*s)) s++;
135 while (*s && !isSPACE (*s)) s++;
143 return do_spawnvp(PL_Argv[0],(const char * const *)PL_Argv);
146 /* see also Cwd.pm */
152 /* See http://rt.perl.org/rt3/Ticket/Display.html?id=38628
153 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
156 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
157 if((cwd = getcwd(NULL, -1))) {
158 ST(0) = sv_2mortal(newSVpv(cwd, 0));
160 #ifndef INCOMPLETE_TAINTS
168 XS(XS_Cygwin_pid_to_winpid)
175 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
177 pid = (pid_t)SvIV(ST(0));
179 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
180 XSprePUSH; PUSHi((IV)RETVAL);
186 XS(XS_Cygwin_winpid_to_pid)
193 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
195 pid = (pid_t)SvIV(ST(0));
197 if ((RETVAL = cygwin32_winpid_to_pid(pid)) > 0) {
198 XSprePUSH; PUSHi((IV)RETVAL);
204 XS(XS_Cygwin_win_to_posix_path)
207 int absolute_flag = 0;
210 char *pathname, *buf;
212 if (items < 1 || items > 2)
213 Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
215 pathname = SvPV(ST(0), len);
217 absolute_flag = SvTRUE(ST(1));
220 Perl_croak(aTHX_ "can't convert empty path");
221 buf = (char *) safemalloc (len + 260 + 1001);
224 err = cygwin_conv_to_full_posix_path(pathname, buf);
226 err = cygwin_conv_to_posix_path(pathname, buf);
228 ST(0) = sv_2mortal(newSVpv(buf, 0));
237 XS(XS_Cygwin_posix_to_win_path)
240 int absolute_flag = 0;
243 char *pathname, *buf;
245 if (items < 1 || items > 2)
246 Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
248 pathname = SvPV(ST(0), len);
250 absolute_flag = SvTRUE(ST(1));
253 Perl_croak(aTHX_ "can't convert empty path");
254 buf = (char *) safemalloc(len + 260 + 1001);
257 err = cygwin_conv_to_full_win32_path(pathname, buf);
259 err = cygwin_conv_to_win32_path(pathname, buf);
261 ST(0) = sv_2mortal(newSVpv(buf, 0));
270 XS(XS_Cygwin_mount_table)
276 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
277 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
280 while ((mnt = getmntent (0))) {
282 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
283 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
284 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
285 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
286 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
292 XS(XS_Cygwin_mount_flags)
299 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags(mnt_dir|'/cygwin')");
301 pathname = SvPV_nolen(ST(0));
303 /* TODO: Check for cygdrive registry setting,
304 * and then use CW_GET_CYGDRIVE_INFO
306 if (!strcmp(pathname, "/cygdrive")) {
309 char user_flags[260];
310 char system_flags[260];
312 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system, user_flags,
315 if (strlen(user) > 0) {
316 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
318 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
321 ST(0) = sv_2mortal(newSVpv(flags, 0));
327 while ((mnt = getmntent (0))) {
328 if (!strcmp(pathname, mnt->mnt_dir)) {
329 strcpy(flags, mnt->mnt_type);
330 if (strlen(mnt->mnt_opts) > 0) {
332 strcat(flags, mnt->mnt_opts);
338 ST(0) = sv_2mortal(newSVpv(flags, 0));
343 XS(XS_Cygwin_is_binmount)
349 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
351 pathname = SvPV_nolen(ST(0));
353 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
361 char const *file = __FILE__;
364 newXS("Cwd::cwd", Cygwin_cwd, file);
365 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
366 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
367 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
368 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
369 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
370 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
371 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
373 /* Initialize Win32CORE if it has been statically linked. */
374 handle = dlopen(NULL, RTLD_LAZY);
376 void (*pfn_init)(pTHX);
377 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");