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)
168 /* Here and elsewhere in this file, we have a critical section to prevent
169 * another thread from changing the locale out from under us. XXX But why
170 * not just use uvchr_to_utf8? */
173 oldlocale = setlocale(LC_CTYPE, NULL);
174 setlocale(LC_CTYPE, "utf-8");
176 /* uvchr_to_utf8(buf, chr) or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
177 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
178 buf = (char *) safemalloc(wlen+1);
179 wcsrtombs(buf, (const wchar_t **)&wbuf, wlen, NULL);
181 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
182 else setlocale(LC_CTYPE, "C");
190 utf8_to_wide(const char *buf)
195 int wlen = sizeof(wchar_t)*strlen(buf);
200 oldlocale = setlocale(LC_CTYPE, NULL);
202 setlocale(LC_CTYPE, "utf-8");
203 wbuf = (wchar_t *) safemalloc(wlen);
204 /* utf8_to_uvchr_buf(pathname, pathname + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
205 wlen = mbsrtowcs(wbuf, (const char**)&buf, wlen, &mbs);
207 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
208 else setlocale(LC_CTYPE, "C");
214 #endif /* cygwin 1.7 */
216 /* see also Cwd.pm */
222 /* See http://rt.perl.org/rt3/Ticket/Display.html?id=38628
223 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
226 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
227 if((cwd = getcwd(NULL, -1))) {
228 ST(0) = sv_2mortal(newSVpv(cwd, 0));
236 XS(XS_Cygwin_pid_to_winpid)
243 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
245 pid = (pid_t)SvIV(ST(0));
247 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
248 XSprePUSH; PUSHi((IV)RETVAL);
254 XS(XS_Cygwin_winpid_to_pid)
261 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
263 pid = (pid_t)SvIV(ST(0));
265 #if (CYGWIN_VERSION_API_MINOR >= 181)
266 RETVAL = cygwin_winpid_to_pid(pid);
268 RETVAL = cygwin32_winpid_to_pid(pid);
271 XSprePUSH; PUSHi((IV)RETVAL);
277 XS(XS_Cygwin_win_to_posix_path)
281 int absolute_flag = 0;
288 if (items < 1 || items > 2)
289 Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
291 src_path = SvPV(ST(0), len);
293 absolute_flag = SvTRUE(ST(1));
296 Perl_croak(aTHX_ "can't convert empty path");
297 isutf8 = SvUTF8(ST(0));
299 #if (CYGWIN_VERSION_API_MINOR >= 181)
300 /* Check utf8 flag and use wide api then.
301 Size calculation: On overflow let cygwin_conv_path calculate the final size.
304 int what = absolute_flag ? CCP_WIN_W_TO_POSIX : CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
305 int wlen = sizeof(wchar_t)*(len + 260 + 1001);
306 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
307 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
315 oldlocale = setlocale(LC_CTYPE, NULL);
316 setlocale(LC_CTYPE, "utf-8");
317 /* utf8_to_uvchr_buf(src_path, src_path + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
318 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
320 err = cygwin_conv_path(what, wpath, wbuf, wlen);
321 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
322 else setlocale(LC_CTYPE, "C");
325 } else { /* use bytes; assume already ucs-2 encoded bytestream */
326 err = cygwin_conv_path(what, src_path, wbuf, wlen);
328 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
329 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
330 wbuf = (wchar_t *) realloc(&wbuf, newlen);
331 err = cygwin_conv_path(what, wpath, wbuf, newlen);
334 /* utf16_to_utf8(*p, *d, bytlen, *newlen) */
335 posix_path = (char *) safemalloc(wlen*3);
336 Perl_utf16_to_utf8(aTHX_ (U8*)&wpath, (U8*)posix_path, (I32)wlen*2, (I32*)&len);
338 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
339 posix_path = (char *) safemalloc(wlen+1);
340 wcsrtombs(posix_path, (const wchar_t **)&wbuf, wlen, NULL);
343 int what = absolute_flag ? CCP_WIN_A_TO_POSIX : CCP_WIN_A_TO_POSIX | CCP_RELATIVE;
344 posix_path = (char *) safemalloc (len + 260 + 1001);
345 err = cygwin_conv_path(what, src_path, posix_path, len + 260 + 1001);
346 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
347 int newlen = cygwin_conv_path(what, src_path, posix_path, 0);
348 posix_path = (char *) realloc(&posix_path, newlen);
349 err = cygwin_conv_path(what, src_path, posix_path, newlen);
353 posix_path = (char *) safemalloc (len + 260 + 1001);
355 err = cygwin_conv_to_full_posix_path(src_path, posix_path);
357 err = cygwin_conv_to_posix_path(src_path, posix_path);
361 ST(0) = sv_2mortal(newSVpv(posix_path, 0));
362 if (isutf8) { /* src was utf-8, so result should also */
363 /* TODO: convert ANSI (local windows encoding) to utf-8 on cygwin-1.5 */
366 safefree(posix_path);
369 safefree(posix_path);
374 XS(XS_Cygwin_posix_to_win_path)
377 int absolute_flag = 0;
380 char *src_path, *win_path;
383 if (items < 1 || items > 2)
384 Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
386 src_path = SvPVx(ST(0), len);
388 absolute_flag = SvTRUE(ST(1));
391 Perl_croak(aTHX_ "can't convert empty path");
392 isutf8 = SvUTF8(ST(0));
393 #if (CYGWIN_VERSION_API_MINOR >= 181)
394 /* Check utf8 flag and use wide api then.
395 Size calculation: On overflow let cygwin_conv_path calculate the final size.
398 int what = absolute_flag ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_W | CCP_RELATIVE;
399 int wlen = sizeof(wchar_t)*(len + 260 + 1001);
400 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
401 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
407 oldlocale = setlocale(LC_CTYPE, NULL);
408 setlocale(LC_CTYPE, "utf-8");
411 /* utf8_to_uvchr_buf(src_path, src_path + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
412 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
414 err = cygwin_conv_path(what, wpath, wbuf, wlen);
415 } else { /* use bytes; assume already ucs-2 encoded bytestream */
416 err = cygwin_conv_path(what, src_path, wbuf, wlen);
418 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
419 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
420 wbuf = (wchar_t *) realloc(&wbuf, newlen);
421 err = cygwin_conv_path(what, wpath, wbuf, newlen);
424 /* also see utf8.c: Perl_utf16_to_utf8() or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
425 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
426 win_path = (char *) safemalloc(wlen+1);
427 wcsrtombs(win_path, (const wchar_t **)&wbuf, wlen, NULL);
428 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
429 else setlocale(LC_CTYPE, "C");
433 int what = absolute_flag ? CCP_POSIX_TO_WIN_A : CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
434 win_path = (char *) safemalloc(len + 260 + 1001);
435 err = cygwin_conv_path(what, src_path, win_path, len + 260 + 1001);
436 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
437 int newlen = cygwin_conv_path(what, src_path, win_path, 0);
438 win_path = (char *) realloc(&win_path, newlen);
439 err = cygwin_conv_path(what, src_path, win_path, newlen);
444 Perl_warn(aTHX_ "can't convert utf8 path");
445 win_path = (char *) safemalloc(len + 260 + 1001);
447 err = cygwin_conv_to_full_win32_path(src_path, win_path);
449 err = cygwin_conv_to_win32_path(src_path, win_path);
453 ST(0) = sv_2mortal(newSVpv(win_path, 0));
465 XS(XS_Cygwin_mount_table)
471 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
472 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
475 while ((mnt = getmntent (0))) {
477 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
478 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
479 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
480 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
481 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
487 XS(XS_Cygwin_mount_flags)
491 char flags[PATH_MAX];
495 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags( mnt_dir | '/cygdrive' )");
497 pathname = SvPV_nolen(ST(0));
499 if (strEQ(pathname, "/cygdrive")) {
501 char system[PATH_MAX];
502 char user_flags[PATH_MAX];
503 char system_flags[PATH_MAX];
505 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
506 user_flags, system_flags);
508 if (strlen(user) > 0) {
509 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
511 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
514 ST(0) = sv_2mortal(newSVpv(flags, 0));
521 while ((mnt = getmntent (0))) {
522 if (strEQ(pathname, mnt->mnt_dir)) {
523 strcpy(flags, mnt->mnt_type);
524 if (strlen(mnt->mnt_opts) > 0) {
526 strcat(flags, mnt->mnt_opts);
534 /* Check if arg is the current volume moint point if not default,
535 * and then use CW_GET_CYGDRIVE_INFO also.
539 char system[PATH_MAX];
540 char user_flags[PATH_MAX];
541 char system_flags[PATH_MAX];
543 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
544 user_flags, system_flags);
546 if (strlen(user) > 0) {
547 if (strNE(user,pathname)) {
548 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
552 if (strNE(user,pathname)) {
553 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
559 ST(0) = sv_2mortal(newSVpv(flags, 0));
567 XS(XS_Cygwin_is_binmount)
573 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
575 pathname = SvPV_nolen(ST(0));
577 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
581 XS(XS_Cygwin_sync_winenv){ cygwin_internal(CW_SYNC_WINENV); }
587 char const *file = __FILE__;
590 newXS("Cwd::cwd", Cygwin_cwd, file);
591 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
592 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
593 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
594 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
595 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
596 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
597 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
598 newXS("Cygwin::sync_winenv", XS_Cygwin_sync_winenv, file);
600 /* Initialize Win32CORE if it has been statically linked. */
601 handle = dlopen(NULL, RTLD_LAZY);
603 void (*pfn_init)(pTHX);
604 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");