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);
93 char const **argv, **a;
95 char const *metachars = "$&*(){}[]'\";\\?>|<~`\n";
96 const char *command[4];
100 while (*cmd && isSPACE(*cmd))
103 if (strBEGINs (cmd,"/bin/sh") && isSPACE (cmd[7]))
106 /* save an extra exec if possible */
107 /* see if there are shell metacharacters in it */
108 if (strstr (cmd,"..."))
110 if (*cmd=='.' && isSPACE (cmd[1]))
112 if (strBEGINs (cmd,"exec") && isSPACE (cmd[4]))
114 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
119 if (strchr (metachars,*s))
121 if (*s=='\n' && s[1]=='\0')
132 result = do_spawnvp("sh",command);
136 Newx (argv, (s-cmd)/2+2, const char*);
138 cmd=savepvn (cmd,s-cmd);
142 while (*s && isSPACE (*s)) s++;
145 while (*s && !isSPACE (*s)) s++;
153 result = do_spawnvp(argv[0],(const char * const *)argv);
159 #if (CYGWIN_VERSION_API_MINOR >= 181)
161 wide_to_utf8(const wchar_t *wbuf)
167 /* Here and elsewhere in this file, we have a critical section to prevent
168 * another thread from changing the locale out from under us. XXX But why
169 * not just use uvchr_to_utf8? */
172 oldlocale = setlocale(LC_CTYPE, NULL);
173 setlocale(LC_CTYPE, "utf-8");
175 /* uvchr_to_utf8(buf, chr) or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
176 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
177 buf = (char *) safemalloc(wlen+1);
178 wcsrtombs(buf, (const wchar_t **)&wbuf, wlen, NULL);
180 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
181 else setlocale(LC_CTYPE, "C");
189 utf8_to_wide(const char *buf)
194 int wlen = sizeof(wchar_t)*strlen(buf);
198 oldlocale = setlocale(LC_CTYPE, NULL);
200 setlocale(LC_CTYPE, "utf-8");
201 wbuf = (wchar_t *) safemalloc(wlen);
202 /* utf8_to_uvchr_buf(pathname, pathname + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
203 wlen = mbsrtowcs(wbuf, (const char**)&buf, wlen, &mbs);
205 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
206 else setlocale(LC_CTYPE, "C");
212 #endif /* cygwin 1.7 */
214 /* see also Cwd.pm */
220 /* See https://github.com/Perl/perl5/issues/8345
221 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
224 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
225 if((cwd = getcwd(NULL, -1))) {
226 ST(0) = sv_2mortal(newSVpv(cwd, 0));
234 XS(XS_Cygwin_pid_to_winpid)
241 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
243 pid = (pid_t)SvIV(ST(0));
245 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
246 XSprePUSH; PUSHi((IV)RETVAL);
252 XS(XS_Cygwin_winpid_to_pid)
259 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
261 pid = (pid_t)SvIV(ST(0));
263 #if (CYGWIN_VERSION_API_MINOR >= 181)
264 RETVAL = cygwin_winpid_to_pid(pid);
266 RETVAL = cygwin32_winpid_to_pid(pid);
269 XSprePUSH; PUSHi((IV)RETVAL);
275 XS(XS_Cygwin_win_to_posix_path)
279 int absolute_flag = 0;
286 if (items < 1 || items > 2)
287 Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
289 src_path = SvPV(ST(0), len);
291 absolute_flag = SvTRUE(ST(1));
294 Perl_croak(aTHX_ "can't convert empty path");
295 isutf8 = SvUTF8(ST(0));
297 #if (CYGWIN_VERSION_API_MINOR >= 181)
298 /* Check utf8 flag and use wide api then.
299 Size calculation: On overflow let cygwin_conv_path calculate the final size.
302 int what = absolute_flag ? CCP_WIN_W_TO_POSIX : CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
303 STRLEN wlen = sizeof(wchar_t)*(len + 260 + 1001);
304 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
305 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
312 oldlocale = setlocale(LC_CTYPE, NULL);
313 setlocale(LC_CTYPE, "utf-8");
314 /* utf8_to_uvchr_buf(src_path, src_path + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
315 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
317 err = cygwin_conv_path(what, wpath, wbuf, wlen);
318 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
319 else setlocale(LC_CTYPE, "C");
322 } else { /* use bytes; assume already ucs-2 encoded bytestream */
323 err = cygwin_conv_path(what, src_path, wbuf, wlen);
325 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
326 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
327 wbuf = (wchar_t *) realloc(&wbuf, newlen);
328 err = cygwin_conv_path(what, wpath, wbuf, newlen);
331 /* utf16_to_utf8(*p, *d, bytlen, *newlen) */
332 posix_path = (char *) safemalloc(wlen*3);
333 Perl_utf16_to_utf8(aTHX_ (U8*)&wpath, (U8*)posix_path, wlen*2, &len);
335 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
336 posix_path = (char *) safemalloc(wlen+1);
337 wcsrtombs(posix_path, (const wchar_t **)&wbuf, wlen, NULL);
340 int what = absolute_flag ? CCP_WIN_A_TO_POSIX : CCP_WIN_A_TO_POSIX | CCP_RELATIVE;
341 posix_path = (char *) safemalloc (len + 260 + 1001);
342 err = cygwin_conv_path(what, src_path, posix_path, len + 260 + 1001);
343 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
344 int newlen = cygwin_conv_path(what, src_path, posix_path, 0);
345 posix_path = (char *) realloc(&posix_path, newlen);
346 err = cygwin_conv_path(what, src_path, posix_path, newlen);
350 posix_path = (char *) safemalloc (len + 260 + 1001);
352 err = cygwin_conv_to_full_posix_path(src_path, posix_path);
354 err = cygwin_conv_to_posix_path(src_path, posix_path);
358 ST(0) = sv_2mortal(newSVpv(posix_path, 0));
359 if (isutf8) { /* src was utf-8, so result should also */
360 /* TODO: convert ANSI (local windows encoding) to utf-8 on cygwin-1.5 */
363 safefree(posix_path);
366 safefree(posix_path);
371 XS(XS_Cygwin_posix_to_win_path)
374 int absolute_flag = 0;
377 char *src_path, *win_path;
380 if (items < 1 || items > 2)
381 Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
383 src_path = SvPVx(ST(0), len);
385 absolute_flag = SvTRUE(ST(1));
388 Perl_croak(aTHX_ "can't convert empty path");
389 isutf8 = SvUTF8(ST(0));
390 #if (CYGWIN_VERSION_API_MINOR >= 181)
391 /* Check utf8 flag and use wide api then.
392 Size calculation: On overflow let cygwin_conv_path calculate the final size.
395 int what = absolute_flag ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_W | CCP_RELATIVE;
396 int wlen = sizeof(wchar_t)*(len + 260 + 1001);
397 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
398 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
403 oldlocale = setlocale(LC_CTYPE, NULL);
404 setlocale(LC_CTYPE, "utf-8");
407 /* utf8_to_uvchr_buf(src_path, src_path + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
408 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
410 err = cygwin_conv_path(what, wpath, wbuf, wlen);
411 } else { /* use bytes; assume already ucs-2 encoded bytestream */
412 err = cygwin_conv_path(what, src_path, wbuf, wlen);
414 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
415 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
416 wbuf = (wchar_t *) realloc(&wbuf, newlen);
417 err = cygwin_conv_path(what, wpath, wbuf, newlen);
420 /* also see utf8.c: Perl_utf16_to_utf8() or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
421 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
422 win_path = (char *) safemalloc(wlen+1);
423 wcsrtombs(win_path, (const wchar_t **)&wbuf, wlen, NULL);
424 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
425 else setlocale(LC_CTYPE, "C");
429 int what = absolute_flag ? CCP_POSIX_TO_WIN_A : CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
430 win_path = (char *) safemalloc(len + 260 + 1001);
431 err = cygwin_conv_path(what, src_path, win_path, len + 260 + 1001);
432 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
433 int newlen = cygwin_conv_path(what, src_path, win_path, 0);
434 win_path = (char *) realloc(&win_path, newlen);
435 err = cygwin_conv_path(what, src_path, win_path, newlen);
440 Perl_warn(aTHX_ "can't convert utf8 path");
441 win_path = (char *) safemalloc(len + 260 + 1001);
443 err = cygwin_conv_to_full_win32_path(src_path, win_path);
445 err = cygwin_conv_to_win32_path(src_path, win_path);
449 ST(0) = sv_2mortal(newSVpv(win_path, 0));
461 XS(XS_Cygwin_mount_table)
467 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
468 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
471 while ((mnt = getmntent (0))) {
473 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
474 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
475 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
476 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
477 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
483 XS(XS_Cygwin_mount_flags)
487 char flags[PATH_MAX];
491 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags( mnt_dir | '/cygdrive' )");
493 pathname = SvPV_nolen(ST(0));
495 if (strEQ(pathname, "/cygdrive")) {
497 char system[PATH_MAX];
498 char user_flags[PATH_MAX];
499 char system_flags[PATH_MAX];
501 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
502 user_flags, system_flags);
504 if (strlen(user) > 0) {
505 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
507 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
510 ST(0) = sv_2mortal(newSVpv(flags, 0));
517 while ((mnt = getmntent (0))) {
518 if (strEQ(pathname, mnt->mnt_dir)) {
519 strcpy(flags, mnt->mnt_type);
520 if (strlen(mnt->mnt_opts) > 0) {
522 strcat(flags, mnt->mnt_opts);
530 /* Check if arg is the current volume moint point if not default,
531 * and then use CW_GET_CYGDRIVE_INFO also.
535 char system[PATH_MAX];
536 char user_flags[PATH_MAX];
537 char system_flags[PATH_MAX];
539 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
540 user_flags, system_flags);
542 if (strlen(user) > 0) {
543 if (strNE(user,pathname)) {
544 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
548 if (strNE(user,pathname)) {
549 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
555 ST(0) = sv_2mortal(newSVpv(flags, 0));
563 XS(XS_Cygwin_is_binmount)
569 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
571 pathname = SvPV_nolen(ST(0));
573 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
577 XS(XS_Cygwin_sync_winenv){ cygwin_internal(CW_SYNC_WINENV); }
583 char const *file = __FILE__;
586 newXS("Cwd::cwd", Cygwin_cwd, file);
587 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
588 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
589 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
590 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
591 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
592 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
593 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
594 newXS("Cygwin::sync_winenv", XS_Cygwin_sync_winenv, file);
596 /* Initialize Win32CORE if it has been statically linked. */
597 handle = dlopen(NULL, RTLD_LAZY);
599 void (*pfn_init)(pTHX);
600 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");