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 */
148 /* See http://rt.perl.org/rt3/Ticket/Display.html?id=38628
149 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
152 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
153 if((cwd = getcwd(NULL, -1))) {
154 ST(0) = sv_2mortal(newSVpv(cwd, 0));
156 #ifndef INCOMPLETE_TAINTS
164 XS(XS_Cygwin_pid_to_winpid)
171 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
173 pid = (pid_t)SvIV(ST(0));
175 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
176 XSprePUSH; PUSHi((IV)RETVAL);
182 XS(XS_Cygwin_winpid_to_pid)
189 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
191 pid = (pid_t)SvIV(ST(0));
193 if ((RETVAL = cygwin32_winpid_to_pid(pid)) > 0) {
194 XSprePUSH; PUSHi((IV)RETVAL);
200 XS(XS_Cygwin_win_to_posix_path)
203 int absolute_flag = 0;
206 char *pathname, *buf;
208 if (items < 1 || items > 2)
209 Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
211 pathname = SvPV(ST(0), len);
213 absolute_flag = SvTRUE(ST(1));
216 Perl_croak(aTHX_ "can't convert empty path");
217 buf = (char *) safemalloc (len + 260 + 1001);
220 err = cygwin_conv_to_full_posix_path(pathname, buf);
222 err = cygwin_conv_to_posix_path(pathname, buf);
224 ST(0) = sv_2mortal(newSVpv(buf, 0));
233 XS(XS_Cygwin_posix_to_win_path)
236 int absolute_flag = 0;
239 char *pathname, *buf;
241 if (items < 1 || items > 2)
242 Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
244 pathname = SvPV(ST(0), len);
246 absolute_flag = SvTRUE(ST(1));
249 Perl_croak(aTHX_ "can't convert empty path");
250 buf = (char *) safemalloc(len + 260 + 1001);
253 err = cygwin_conv_to_full_win32_path(pathname, buf);
255 err = cygwin_conv_to_win32_path(pathname, buf);
257 ST(0) = sv_2mortal(newSVpv(buf, 0));
266 XS(XS_Cygwin_mount_table)
272 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
273 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
276 while ((mnt = getmntent (0))) {
278 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
279 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
280 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
281 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
282 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
288 XS(XS_Cygwin_mount_flags)
295 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags(mnt_dir|'/cygwin')");
297 pathname = SvPV_nolen(ST(0));
299 /* TODO: Check for cygdrive registry setting,
300 * and then use CW_GET_CYGDRIVE_INFO
302 if (!strcmp(pathname, "/cygdrive")) {
305 char user_flags[260];
306 char system_flags[260];
308 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system, user_flags,
311 if (strlen(user) > 0) {
312 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
314 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
317 ST(0) = sv_2mortal(newSVpv(flags, 0));
323 while ((mnt = getmntent (0))) {
324 if (!strcmp(pathname, mnt->mnt_dir)) {
325 strcpy(flags, mnt->mnt_type);
326 if (strlen(mnt->mnt_opts) > 0) {
328 strcat(flags, mnt->mnt_opts);
334 ST(0) = sv_2mortal(newSVpv(flags, 0));
339 XS(XS_Cygwin_is_binmount)
345 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
347 pathname = SvPV_nolen(ST(0));
349 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
357 char *file = __FILE__;
360 newXS("Cwd::cwd", Cygwin_cwd, file);
361 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
362 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
363 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
364 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
365 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
366 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
367 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
369 /* Initialize Win32CORE if it has been statically linked. */
370 handle = dlopen(NULL, RTLD_LAZY);
372 void (*pfn_init)(pTHX);
373 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");