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 (strnEQ (cmd,"/bin/sh",7) && 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 (strnEQ (cmd,"exec",4) && 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)
157 char *oldlocale = setlocale(LC_CTYPE, NULL);
158 setlocale(LC_CTYPE, "utf-8");
160 /* uvchr_to_utf8(buf, chr) or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
161 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
162 buf = (char *) safemalloc(wlen+1);
163 wcsrtombs(buf, (const wchar_t **)&wbuf, wlen, NULL);
165 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
166 else setlocale(LC_CTYPE, "C");
171 utf8_to_wide(const char *buf)
175 char *oldlocale = setlocale(LC_CTYPE, NULL);
176 int wlen = sizeof(wchar_t)*strlen(buf);
178 setlocale(LC_CTYPE, "utf-8");
179 wbuf = (wchar_t *) safemalloc(wlen);
180 /* utf8_to_uvchr_buf(pathname, pathname + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
181 wlen = mbsrtowcs(wbuf, (const char**)&buf, wlen, &mbs);
183 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
184 else setlocale(LC_CTYPE, "C");
187 #endif /* cygwin 1.7 */
189 /* see also Cwd.pm */
195 /* See http://rt.perl.org/rt3/Ticket/Display.html?id=38628
196 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
199 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
200 if((cwd = getcwd(NULL, -1))) {
201 ST(0) = sv_2mortal(newSVpv(cwd, 0));
209 XS(XS_Cygwin_pid_to_winpid)
216 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
218 pid = (pid_t)SvIV(ST(0));
220 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
221 XSprePUSH; PUSHi((IV)RETVAL);
227 XS(XS_Cygwin_winpid_to_pid)
234 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
236 pid = (pid_t)SvIV(ST(0));
238 #if (CYGWIN_VERSION_API_MINOR >= 181)
239 RETVAL = cygwin_winpid_to_pid(pid);
241 RETVAL = cygwin32_winpid_to_pid(pid);
244 XSprePUSH; PUSHi((IV)RETVAL);
250 XS(XS_Cygwin_win_to_posix_path)
254 int absolute_flag = 0;
261 if (items < 1 || items > 2)
262 Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
264 src_path = SvPV(ST(0), len);
266 absolute_flag = SvTRUE(ST(1));
269 Perl_croak(aTHX_ "can't convert empty path");
270 isutf8 = SvUTF8(ST(0));
272 #if (CYGWIN_VERSION_API_MINOR >= 181)
273 /* Check utf8 flag and use wide api then.
274 Size calculation: On overflow let cygwin_conv_path calculate the final size.
277 int what = absolute_flag ? CCP_WIN_W_TO_POSIX : CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
278 int wlen = sizeof(wchar_t)*(len + 260 + 1001);
279 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
280 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
283 char *oldlocale = setlocale(LC_CTYPE, NULL);
284 setlocale(LC_CTYPE, "utf-8");
285 /* utf8_to_uvchr_buf(src_path, src_path + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
286 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
288 err = cygwin_conv_path(what, wpath, wbuf, wlen);
289 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
290 else setlocale(LC_CTYPE, "C");
291 } else { /* use bytes; assume already ucs-2 encoded bytestream */
292 err = cygwin_conv_path(what, src_path, wbuf, wlen);
294 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
295 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
296 wbuf = (wchar_t *) realloc(&wbuf, newlen);
297 err = cygwin_conv_path(what, wpath, wbuf, newlen);
300 /* utf16_to_utf8(*p, *d, bytlen, *newlen) */
301 posix_path = (char *) safemalloc(wlen*3);
302 Perl_utf16_to_utf8(aTHX_ (U8*)&wpath, (U8*)posix_path, (I32)wlen*2, (I32*)&len);
304 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
305 posix_path = (char *) safemalloc(wlen+1);
306 wcsrtombs(posix_path, (const wchar_t **)&wbuf, wlen, NULL);
309 int what = absolute_flag ? CCP_WIN_A_TO_POSIX : CCP_WIN_A_TO_POSIX | CCP_RELATIVE;
310 posix_path = (char *) safemalloc (len + 260 + 1001);
311 err = cygwin_conv_path(what, src_path, posix_path, len + 260 + 1001);
312 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
313 int newlen = cygwin_conv_path(what, src_path, posix_path, 0);
314 posix_path = (char *) realloc(&posix_path, newlen);
315 err = cygwin_conv_path(what, src_path, posix_path, newlen);
319 posix_path = (char *) safemalloc (len + 260 + 1001);
321 err = cygwin_conv_to_full_posix_path(src_path, posix_path);
323 err = cygwin_conv_to_posix_path(src_path, posix_path);
327 ST(0) = sv_2mortal(newSVpv(posix_path, 0));
328 if (isutf8) { /* src was utf-8, so result should also */
329 /* TODO: convert ANSI (local windows encoding) to utf-8 on cygwin-1.5 */
332 safefree(posix_path);
335 safefree(posix_path);
340 XS(XS_Cygwin_posix_to_win_path)
343 int absolute_flag = 0;
346 char *src_path, *win_path;
349 if (items < 1 || items > 2)
350 Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
352 src_path = SvPVx(ST(0), len);
354 absolute_flag = SvTRUE(ST(1));
357 Perl_croak(aTHX_ "can't convert empty path");
358 isutf8 = SvUTF8(ST(0));
359 #if (CYGWIN_VERSION_API_MINOR >= 181)
360 /* Check utf8 flag and use wide api then.
361 Size calculation: On overflow let cygwin_conv_path calculate the final size.
364 int what = absolute_flag ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_W | CCP_RELATIVE;
365 int wlen = sizeof(wchar_t)*(len + 260 + 1001);
366 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
367 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
368 char *oldlocale = setlocale(LC_CTYPE, NULL);
369 setlocale(LC_CTYPE, "utf-8");
372 /* utf8_to_uvchr_buf(src_path, src_path + wlen, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
373 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
375 err = cygwin_conv_path(what, wpath, wbuf, wlen);
376 } else { /* use bytes; assume already ucs-2 encoded bytestream */
377 err = cygwin_conv_path(what, src_path, wbuf, wlen);
379 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
380 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
381 wbuf = (wchar_t *) realloc(&wbuf, newlen);
382 err = cygwin_conv_path(what, wpath, wbuf, newlen);
385 /* also see utf8.c: Perl_utf16_to_utf8() or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
386 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
387 win_path = (char *) safemalloc(wlen+1);
388 wcsrtombs(win_path, (const wchar_t **)&wbuf, wlen, NULL);
389 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
390 else setlocale(LC_CTYPE, "C");
392 int what = absolute_flag ? CCP_POSIX_TO_WIN_A : CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
393 win_path = (char *) safemalloc(len + 260 + 1001);
394 err = cygwin_conv_path(what, src_path, win_path, len + 260 + 1001);
395 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
396 int newlen = cygwin_conv_path(what, src_path, win_path, 0);
397 win_path = (char *) realloc(&win_path, newlen);
398 err = cygwin_conv_path(what, src_path, win_path, newlen);
403 Perl_warn(aTHX_ "can't convert utf8 path");
404 win_path = (char *) safemalloc(len + 260 + 1001);
406 err = cygwin_conv_to_full_win32_path(src_path, win_path);
408 err = cygwin_conv_to_win32_path(src_path, win_path);
412 ST(0) = sv_2mortal(newSVpv(win_path, 0));
424 XS(XS_Cygwin_mount_table)
430 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
431 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
434 while ((mnt = getmntent (0))) {
436 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
437 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
438 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
439 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
440 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
446 XS(XS_Cygwin_mount_flags)
450 char flags[PATH_MAX];
454 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags( mnt_dir | '/cygdrive' )");
456 pathname = SvPV_nolen(ST(0));
458 if (!strcmp(pathname, "/cygdrive")) {
460 char system[PATH_MAX];
461 char user_flags[PATH_MAX];
462 char system_flags[PATH_MAX];
464 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
465 user_flags, system_flags);
467 if (strlen(user) > 0) {
468 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
470 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
473 ST(0) = sv_2mortal(newSVpv(flags, 0));
480 while ((mnt = getmntent (0))) {
481 if (!strcmp(pathname, mnt->mnt_dir)) {
482 strcpy(flags, mnt->mnt_type);
483 if (strlen(mnt->mnt_opts) > 0) {
485 strcat(flags, mnt->mnt_opts);
493 /* Check if arg is the current volume moint point if not default,
494 * and then use CW_GET_CYGDRIVE_INFO also.
498 char system[PATH_MAX];
499 char user_flags[PATH_MAX];
500 char system_flags[PATH_MAX];
502 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
503 user_flags, system_flags);
505 if (strlen(user) > 0) {
506 if (strcmp(user,pathname)) {
507 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
511 if (strcmp(user,pathname)) {
512 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
518 ST(0) = sv_2mortal(newSVpv(flags, 0));
526 XS(XS_Cygwin_is_binmount)
532 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
534 pathname = SvPV_nolen(ST(0));
536 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
540 XS(XS_Cygwin_sync_winenv){ cygwin_internal(CW_SYNC_WINENV); }
546 char const *file = __FILE__;
549 newXS("Cwd::cwd", Cygwin_cwd, file);
550 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
551 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
552 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
553 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
554 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
555 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
556 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
557 newXS("Cygwin::sync_winenv", XS_Cygwin_sync_winenv, file);
559 /* Initialize Win32CORE if it has been statically linked. */
560 handle = dlopen(NULL, RTLD_LAZY);
562 void (*pfn_init)(pTHX);
563 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");