5 #define PERLIO_NOT_STDIO 0
8 #undef USE_DYNAMIC_LOADING
13 #include <sys/cygwin.h>
14 #include <cygwin/version.h>
18 #if (CYGWIN_VERSION_API_MINOR >= 181)
23 * pp_system() implemented via spawn()
24 * - more efficient and useful when embedding Perl in non-Cygwin apps
25 * - code mostly borrowed from djgpp.c
28 do_spawnvp (const char *path, const char * const *argv)
31 Sigsave_t ihand,qhand;
32 int childpid, result, status;
34 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
35 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
36 childpid = spawnvp(_P_NOWAIT,path,argv);
40 Perl_warner(aTHX_ packWARN(WARN_EXEC),"Can't spawn \"%s\": %s",
41 path,Strerror (errno));
44 result = wait4pid(childpid, &status, 0);
45 } while (result == -1 && errno == EINTR);
49 (void)rsignal_restore(SIGINT, &ihand);
50 (void)rsignal_restore(SIGQUIT, &qhand);
55 do_aspawn (SV *really, void **mark, void **sp)
65 argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
66 a=(char const **)argv;
70 *a++ = SvPVx((SV *)*mark, n_a);
75 if (argv[0][0] != '/' && argv[0][0] != '\\'
76 && !(argv[0][0] && argv[0][1] == ':'
77 && (argv[0][2] == '/' || argv[0][2] != '\\'))
78 ) /* will swawnvp use PATH? */
79 TAINT_ENV(); /* testing IFS here is overkill, probably */
81 if (really && *(tmps = SvPV(really, n_a)))
82 rc=do_spawnvp (tmps,(const char * const *)argv);
84 rc=do_spawnvp (argv[0],(const char *const *)argv);
95 char const *metachars = "$&*(){}[]'\";\\?>|<~`\n";
96 const char *command[4];
98 while (*cmd && isSPACE(*cmd))
101 if (strBEGINs (cmd,"/bin/sh") && isSPACE (cmd[7]))
104 /* save an extra exec if possible */
105 /* see if there are shell metacharacters in it */
106 if (strstr (cmd,"..."))
108 if (*cmd=='.' && isSPACE (cmd[1]))
110 if (strBEGINs (cmd,"exec") && isSPACE (cmd[4]))
112 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
117 if (strchr (metachars,*s))
119 if (*s=='\n' && s[1]=='\0')
130 return do_spawnvp("sh",command);
133 Newx (PL_Argv, (s-cmd)/2+2, const char*);
134 PL_Cmd=savepvn (cmd,s-cmd);
136 for (s=PL_Cmd; *s;) {
137 while (*s && isSPACE (*s)) s++;
140 while (*s && !isSPACE (*s)) s++;
148 return do_spawnvp(PL_Argv[0],(const char * const *)PL_Argv);
151 #if (CYGWIN_VERSION_API_MINOR >= 181)
153 wide_to_utf8(const wchar_t *wbuf)
160 /* Here and elsewhere in this file, we have a critical section to prevent
161 * another thread from changing the locale out from under us. XXX But why
162 * not just use uvchr_to_utf8? */
165 oldlocale = setlocale(LC_CTYPE, NULL);
166 setlocale(LC_CTYPE, "utf-8");
168 /* uvchr_to_utf8(buf, chr) or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
169 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
170 buf = (char *) safemalloc(wlen+1);
171 wcsrtombs(buf, (const wchar_t **)&wbuf, wlen, NULL);
173 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
174 else setlocale(LC_CTYPE, "C");
182 utf8_to_wide(const char *buf)
187 int wlen = sizeof(wchar_t)*strlen(buf);
192 oldlocale = setlocale(LC_CTYPE, NULL);
194 setlocale(LC_CTYPE, "utf-8");
195 wbuf = (wchar_t *) safemalloc(wlen);
196 /* utf8_to_uvchr_buf(pathname, pathname + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
197 wlen = mbsrtowcs(wbuf, (const char**)&buf, wlen, &mbs);
199 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
200 else setlocale(LC_CTYPE, "C");
206 #endif /* cygwin 1.7 */
208 /* see also Cwd.pm */
214 /* See http://rt.perl.org/rt3/Ticket/Display.html?id=38628
215 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
218 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
219 if((cwd = getcwd(NULL, -1))) {
220 ST(0) = sv_2mortal(newSVpv(cwd, 0));
228 XS(XS_Cygwin_pid_to_winpid)
235 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
237 pid = (pid_t)SvIV(ST(0));
239 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
240 XSprePUSH; PUSHi((IV)RETVAL);
246 XS(XS_Cygwin_winpid_to_pid)
253 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
255 pid = (pid_t)SvIV(ST(0));
257 #if (CYGWIN_VERSION_API_MINOR >= 181)
258 RETVAL = cygwin_winpid_to_pid(pid);
260 RETVAL = cygwin32_winpid_to_pid(pid);
263 XSprePUSH; PUSHi((IV)RETVAL);
269 XS(XS_Cygwin_win_to_posix_path)
273 int absolute_flag = 0;
280 if (items < 1 || items > 2)
281 Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
283 src_path = SvPV(ST(0), len);
285 absolute_flag = SvTRUE(ST(1));
288 Perl_croak(aTHX_ "can't convert empty path");
289 isutf8 = SvUTF8(ST(0));
291 #if (CYGWIN_VERSION_API_MINOR >= 181)
292 /* Check utf8 flag and use wide api then.
293 Size calculation: On overflow let cygwin_conv_path calculate the final size.
296 int what = absolute_flag ? CCP_WIN_W_TO_POSIX : CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
297 int wlen = sizeof(wchar_t)*(len + 260 + 1001);
298 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
299 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
307 oldlocale = setlocale(LC_CTYPE, NULL);
308 setlocale(LC_CTYPE, "utf-8");
309 /* utf8_to_uvchr_buf(src_path, src_path + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
310 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
312 err = cygwin_conv_path(what, wpath, wbuf, wlen);
313 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
314 else setlocale(LC_CTYPE, "C");
317 } else { /* use bytes; assume already ucs-2 encoded bytestream */
318 err = cygwin_conv_path(what, src_path, wbuf, wlen);
320 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
321 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
322 wbuf = (wchar_t *) realloc(&wbuf, newlen);
323 err = cygwin_conv_path(what, wpath, wbuf, newlen);
326 /* utf16_to_utf8(*p, *d, bytlen, *newlen) */
327 posix_path = (char *) safemalloc(wlen*3);
328 Perl_utf16_to_utf8(aTHX_ (U8*)&wpath, (U8*)posix_path, (I32)wlen*2, (I32*)&len);
330 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
331 posix_path = (char *) safemalloc(wlen+1);
332 wcsrtombs(posix_path, (const wchar_t **)&wbuf, wlen, NULL);
335 int what = absolute_flag ? CCP_WIN_A_TO_POSIX : CCP_WIN_A_TO_POSIX | CCP_RELATIVE;
336 posix_path = (char *) safemalloc (len + 260 + 1001);
337 err = cygwin_conv_path(what, src_path, posix_path, len + 260 + 1001);
338 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
339 int newlen = cygwin_conv_path(what, src_path, posix_path, 0);
340 posix_path = (char *) realloc(&posix_path, newlen);
341 err = cygwin_conv_path(what, src_path, posix_path, newlen);
345 posix_path = (char *) safemalloc (len + 260 + 1001);
347 err = cygwin_conv_to_full_posix_path(src_path, posix_path);
349 err = cygwin_conv_to_posix_path(src_path, posix_path);
353 ST(0) = sv_2mortal(newSVpv(posix_path, 0));
354 if (isutf8) { /* src was utf-8, so result should also */
355 /* TODO: convert ANSI (local windows encoding) to utf-8 on cygwin-1.5 */
358 safefree(posix_path);
361 safefree(posix_path);
366 XS(XS_Cygwin_posix_to_win_path)
369 int absolute_flag = 0;
372 char *src_path, *win_path;
375 if (items < 1 || items > 2)
376 Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
378 src_path = SvPVx(ST(0), len);
380 absolute_flag = SvTRUE(ST(1));
383 Perl_croak(aTHX_ "can't convert empty path");
384 isutf8 = SvUTF8(ST(0));
385 #if (CYGWIN_VERSION_API_MINOR >= 181)
386 /* Check utf8 flag and use wide api then.
387 Size calculation: On overflow let cygwin_conv_path calculate the final size.
390 int what = absolute_flag ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_W | CCP_RELATIVE;
391 int wlen = sizeof(wchar_t)*(len + 260 + 1001);
392 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
393 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
399 oldlocale = setlocale(LC_CTYPE, NULL);
400 setlocale(LC_CTYPE, "utf-8");
403 /* utf8_to_uvchr_buf(src_path, src_path + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
404 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
406 err = cygwin_conv_path(what, wpath, wbuf, wlen);
407 } else { /* use bytes; assume already ucs-2 encoded bytestream */
408 err = cygwin_conv_path(what, src_path, wbuf, wlen);
410 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
411 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
412 wbuf = (wchar_t *) realloc(&wbuf, newlen);
413 err = cygwin_conv_path(what, wpath, wbuf, newlen);
416 /* also see utf8.c: Perl_utf16_to_utf8() or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
417 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
418 win_path = (char *) safemalloc(wlen+1);
419 wcsrtombs(win_path, (const wchar_t **)&wbuf, wlen, NULL);
420 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
421 else setlocale(LC_CTYPE, "C");
425 int what = absolute_flag ? CCP_POSIX_TO_WIN_A : CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
426 win_path = (char *) safemalloc(len + 260 + 1001);
427 err = cygwin_conv_path(what, src_path, win_path, len + 260 + 1001);
428 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
429 int newlen = cygwin_conv_path(what, src_path, win_path, 0);
430 win_path = (char *) realloc(&win_path, newlen);
431 err = cygwin_conv_path(what, src_path, win_path, newlen);
436 Perl_warn(aTHX_ "can't convert utf8 path");
437 win_path = (char *) safemalloc(len + 260 + 1001);
439 err = cygwin_conv_to_full_win32_path(src_path, win_path);
441 err = cygwin_conv_to_win32_path(src_path, win_path);
445 ST(0) = sv_2mortal(newSVpv(win_path, 0));
457 XS(XS_Cygwin_mount_table)
463 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
464 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
467 while ((mnt = getmntent (0))) {
469 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
470 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
471 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
472 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
473 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
479 XS(XS_Cygwin_mount_flags)
483 char flags[PATH_MAX];
487 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags( mnt_dir | '/cygdrive' )");
489 pathname = SvPV_nolen(ST(0));
491 if (strEQ(pathname, "/cygdrive")) {
493 char system[PATH_MAX];
494 char user_flags[PATH_MAX];
495 char system_flags[PATH_MAX];
497 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
498 user_flags, system_flags);
500 if (strlen(user) > 0) {
501 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
503 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
506 ST(0) = sv_2mortal(newSVpv(flags, 0));
513 while ((mnt = getmntent (0))) {
514 if (strEQ(pathname, mnt->mnt_dir)) {
515 strcpy(flags, mnt->mnt_type);
516 if (strlen(mnt->mnt_opts) > 0) {
518 strcat(flags, mnt->mnt_opts);
526 /* Check if arg is the current volume moint point if not default,
527 * and then use CW_GET_CYGDRIVE_INFO also.
531 char system[PATH_MAX];
532 char user_flags[PATH_MAX];
533 char system_flags[PATH_MAX];
535 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
536 user_flags, system_flags);
538 if (strlen(user) > 0) {
539 if (strNE(user,pathname)) {
540 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
544 if (strNE(user,pathname)) {
545 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
551 ST(0) = sv_2mortal(newSVpv(flags, 0));
559 XS(XS_Cygwin_is_binmount)
565 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
567 pathname = SvPV_nolen(ST(0));
569 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
573 XS(XS_Cygwin_sync_winenv){ cygwin_internal(CW_SYNC_WINENV); }
579 char const *file = __FILE__;
582 newXS("Cwd::cwd", Cygwin_cwd, file);
583 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
584 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
585 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
586 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
587 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
588 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
589 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
590 newXS("Cygwin::sync_winenv", XS_Cygwin_sync_winenv, file);
592 /* Initialize Win32CORE if it has been statically linked. */
593 handle = dlopen(NULL, RTLD_LAZY);
595 void (*pfn_init)(pTHX);
596 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");