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);
86 char **a,*s,*metachars = "$&*(){}[]'\";\\?>|<~`\n";
87 const char *command[4];
89 while (*cmd && isSPACE(*cmd))
92 if (strnEQ (cmd,"/bin/sh",7) && isSPACE (cmd[7]))
95 /* save an extra exec if possible */
96 /* see if there are shell metacharacters in it */
97 if (strstr (cmd,"..."))
99 if (*cmd=='.' && isSPACE (cmd[1]))
101 if (strnEQ (cmd,"exec",4) && isSPACE (cmd[4]))
103 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
108 if (strchr (metachars,*s))
110 if (*s=='\n' && s[1]=='\0')
121 return do_spawnvp("sh",command);
124 Newx (PL_Argv,(s-cmd)/2+2,char*);
125 PL_Cmd=savepvn (cmd,s-cmd);
127 for (s=PL_Cmd; *s;) {
128 while (*s && isSPACE (*s)) s++;
131 while (*s && !isSPACE (*s)) s++;
139 return do_spawnvp(PL_Argv[0],(const char * const *)PL_Argv);
142 /* see also Cwd.pm */
149 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
150 if((cwd = getcwd(NULL, -1))) {
151 ST(0) = sv_2mortal(newSVpv(cwd, 0));
153 #ifndef INCOMPLETE_TAINTS
161 XS(XS_Cygwin_pid_to_winpid)
168 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
170 pid = (pid_t)SvIV(ST(0));
172 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
173 XSprePUSH; PUSHi((IV)RETVAL);
179 XS(XS_Cygwin_winpid_to_pid)
186 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
188 pid = (pid_t)SvIV(ST(0));
190 if ((RETVAL = cygwin32_winpid_to_pid(pid)) > 0) {
191 XSprePUSH; PUSHi((IV)RETVAL);
197 XS(XS_Cygwin_win_to_posix_path)
200 int absolute_flag = 0;
203 char *pathname, *buf;
205 if (items < 1 || items > 2)
206 Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
208 pathname = SvPV(ST(0), len);
210 absolute_flag = SvTRUE(ST(1));
213 Perl_croak(aTHX_ "can't convert empty path");
214 buf = (char *) safemalloc (len + 260 + 1001);
217 err = cygwin_conv_to_full_posix_path(pathname, buf);
219 err = cygwin_conv_to_posix_path(pathname, buf);
221 ST(0) = sv_2mortal(newSVpv(buf, 0));
230 XS(XS_Cygwin_posix_to_win_path)
233 int absolute_flag = 0;
236 char *pathname, *buf;
238 if (items < 1 || items > 2)
239 Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
241 pathname = SvPV(ST(0), len);
243 absolute_flag = SvTRUE(ST(1));
246 Perl_croak(aTHX_ "can't convert empty path");
247 buf = (char *) safemalloc(len + 260 + 1001);
250 err = cygwin_conv_to_full_win32_path(pathname, buf);
252 err = cygwin_conv_to_win32_path(pathname, buf);
254 ST(0) = sv_2mortal(newSVpv(buf, 0));
263 XS(XS_Cygwin_mount_table)
269 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
270 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
273 while ((mnt = getmntent (0))) {
275 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
276 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
277 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
278 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
279 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
285 XS(XS_Cygwin_mount_flags)
292 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags(mnt_dir)");
294 pathname = SvPV_nolen(ST(0));
296 /* TODO: check for cygdrive registry setting. use CW_GET_CYGDRIVE_INFO then
298 if (!strcmp(pathname, "/cygdrive")) {
301 char user_flags[260];
302 char system_flags[260];
303 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system, user_flags,
305 if (strlen(system) > 0)
306 strcpy (flags, system_flags);
307 if (strlen(user) > 0)
308 strcpy(flags, user_flags);
309 if (strlen(flags) > 0)
311 strcat(flags, "cygdrive");
312 ST(0) = sv_2mortal(newSVpv(flags, 0));
317 while ((mnt = getmntent (0))) {
318 if (!strcmp(pathname, mnt->mnt_dir)) {
319 strcpy(flags, mnt->mnt_type);
320 if (strlen(mnt->mnt_opts) > 0) {
322 strcat(flags, mnt->mnt_opts);
328 ST(0) = sv_2mortal(newSVpv(flags, 0));
333 XS(XS_Cygwin_is_binmount)
339 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
341 pathname = SvPV_nolen(ST(0));
343 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
347 XS(XS_Cygwin_is_textmount)
353 Perl_croak(aTHX_ "Usage: Cygwin::is_textmount(pathname)");
355 pathname = SvPV_nolen(ST(0));
357 ST(0) = boolSV(!cygwin_internal(CW_GET_BINMODE, pathname));
365 char *file = __FILE__;
368 newXS("Cwd::cwd", Cygwin_cwd, file);
369 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
370 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
371 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
372 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
373 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
374 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
375 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
376 newXSproto("Cygwin::is_textmount", XS_Cygwin_is_textmount, file, "$");
378 /* Initialize Win32CORE if it has been statically linked. */
379 handle = dlopen(NULL, RTLD_LAZY);
381 void (*pfn_init)(pTHX);
382 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");