7 #undef USE_DYNAMIC_LOADING
12 #include <sys/cygwin.h>
13 #include <cygwin/version.h>
17 #if (CYGWIN_VERSION_API_MINOR >= 181)
22 * pp_system() implemented via spawn()
23 * - more efficient and useful when embedding Perl in non-Cygwin apps
24 * - code mostly borrowed from djgpp.c
27 do_spawnvp (const char *path, const char * const *argv)
30 Sigsave_t ihand,qhand;
31 int childpid, result, status;
33 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
34 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
35 childpid = spawnvp(_P_NOWAIT,path,argv);
39 Perl_warner(aTHX_ packWARN(WARN_EXEC),"Can't spawn \"%s\": %s",
40 path,Strerror (errno));
43 result = wait4pid(childpid, &status, 0);
44 } while (result == -1 && errno == EINTR);
48 (void)rsignal_restore(SIGINT, &ihand);
49 (void)rsignal_restore(SIGQUIT, &qhand);
54 do_aspawn (SV *really, void **mark, void **sp)
64 argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
65 a=(char const **)argv;
69 *a++ = SvPVx((SV *)*mark, n_a);
74 if (argv[0][0] != '/' && argv[0][0] != '\\'
75 && !(argv[0][0] && argv[0][1] == ':'
76 && (argv[0][2] == '/' || argv[0][2] != '\\'))
77 ) /* will swawnvp use PATH? */
78 TAINT_ENV(); /* testing IFS here is overkill, probably */
80 if (really && *(tmps = SvPV(really, n_a)))
81 rc=do_spawnvp (tmps,(const char * const *)argv);
83 rc=do_spawnvp (argv[0],(const char *const *)argv);
94 char const *metachars = "$&*(){}[]'\";\\?>|<~`\n";
95 const char *command[4];
97 while (*cmd && isSPACE(*cmd))
100 if (strnEQ (cmd,"/bin/sh",7) && isSPACE (cmd[7]))
103 /* save an extra exec if possible */
104 /* see if there are shell metacharacters in it */
105 if (strstr (cmd,"..."))
107 if (*cmd=='.' && isSPACE (cmd[1]))
109 if (strnEQ (cmd,"exec",4) && isSPACE (cmd[4]))
111 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
116 if (strchr (metachars,*s))
118 if (*s=='\n' && s[1]=='\0')
129 return do_spawnvp("sh",command);
132 Newx (PL_Argv,(s-cmd)/2+2,char*);
133 PL_Cmd=savepvn (cmd,s-cmd);
135 for (s=PL_Cmd; *s;) {
136 while (*s && isSPACE (*s)) s++;
139 while (*s && !isSPACE (*s)) s++;
147 return do_spawnvp(PL_Argv[0],(const char * const *)PL_Argv);
150 #if (CYGWIN_VERSION_API_MINOR >= 181)
152 wide_to_utf8(const wchar_t *wbuf)
156 char *oldlocale = setlocale(LC_CTYPE, NULL);
157 setlocale(LC_CTYPE, "utf-8");
159 /* uvuni_to_utf8(buf, chr) or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
160 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
161 buf = (char *) safemalloc(wlen+1);
162 wcsrtombs(buf, (const wchar_t **)&wbuf, wlen, NULL);
164 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
165 else setlocale(LC_CTYPE, "C");
170 utf8_to_wide(const char *buf)
174 char *oldlocale = setlocale(LC_CTYPE, NULL);
175 int wlen = sizeof(wchar_t)*strlen(buf);
177 setlocale(LC_CTYPE, "utf-8");
178 wbuf = (wchar_t *) safemalloc(wlen);
179 /* utf8_to_uvuni(pathname, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
180 wlen = mbsrtowcs(wbuf, (const char**)&buf, wlen, &mbs);
182 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
183 else setlocale(LC_CTYPE, "C");
186 #endif /* cygwin 1.7 */
188 /* see also Cwd.pm */
194 /* See http://rt.perl.org/rt3/Ticket/Display.html?id=38628
195 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
198 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
199 if((cwd = getcwd(NULL, -1))) {
200 ST(0) = sv_2mortal(newSVpv(cwd, 0));
202 #ifndef INCOMPLETE_TAINTS
210 XS(XS_Cygwin_pid_to_winpid)
217 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
219 pid = (pid_t)SvIV(ST(0));
221 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
222 XSprePUSH; PUSHi((IV)RETVAL);
228 XS(XS_Cygwin_winpid_to_pid)
235 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
237 pid = (pid_t)SvIV(ST(0));
239 #if (CYGWIN_VERSION_API_MINOR >= 181)
240 RETVAL = cygwin_winpid_to_pid(pid);
242 RETVAL = cygwin32_winpid_to_pid(pid);
245 XSprePUSH; PUSHi((IV)RETVAL);
251 XS(XS_Cygwin_win_to_posix_path)
255 int absolute_flag = 0;
262 if (items < 1 || items > 2)
263 Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
265 src_path = SvPV(ST(0), len);
267 absolute_flag = SvTRUE(ST(1));
270 Perl_croak(aTHX_ "can't convert empty path");
271 isutf8 = SvUTF8(ST(0));
273 #if (CYGWIN_VERSION_API_MINOR >= 181)
274 /* Check utf8 flag and use wide api then.
275 Size calculation: On overflow let cygwin_conv_path calculate the final size.
278 int what = absolute_flag ? CCP_WIN_W_TO_POSIX : CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
279 int wlen = sizeof(wchar_t)*(len + 260 + 1001);
280 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
281 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
284 char *oldlocale = setlocale(LC_CTYPE, NULL);
285 setlocale(LC_CTYPE, "utf-8");
286 /* utf8_to_uvuni(src_path, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
287 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
289 err = cygwin_conv_path(what, wpath, wbuf, wlen);
290 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
291 else setlocale(LC_CTYPE, "C");
292 } else { /* use bytes; assume already ucs-2 encoded bytestream */
293 err = cygwin_conv_path(what, src_path, wbuf, wlen);
295 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
296 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
297 wbuf = (wchar_t *) realloc(&wbuf, newlen);
298 err = cygwin_conv_path(what, wpath, wbuf, newlen);
301 /* utf16_to_utf8(*p, *d, bytlen, *newlen) */
302 posix_path = (char *) safemalloc(wlen*3);
303 Perl_utf16_to_utf8(aTHX_ (U8*)&wpath, (U8*)posix_path, (I32)wlen*2, (I32*)&len);
305 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
306 posix_path = (char *) safemalloc(wlen+1);
307 wcsrtombs(posix_path, (const wchar_t **)&wbuf, wlen, NULL);
310 int what = absolute_flag ? CCP_WIN_A_TO_POSIX : CCP_WIN_A_TO_POSIX | CCP_RELATIVE;
311 posix_path = (char *) safemalloc (len + 260 + 1001);
312 err = cygwin_conv_path(what, src_path, posix_path, len + 260 + 1001);
313 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
314 int newlen = cygwin_conv_path(what, src_path, posix_path, 0);
315 posix_path = (char *) realloc(&posix_path, newlen);
316 err = cygwin_conv_path(what, src_path, posix_path, newlen);
320 posix_path = (char *) safemalloc (len + 260 + 1001);
322 err = cygwin_conv_to_full_posix_path(src_path, posix_path);
324 err = cygwin_conv_to_posix_path(src_path, posix_path);
328 ST(0) = sv_2mortal(newSVpv(posix_path, 0));
329 if (isutf8) { /* src was utf-8, so result should also */
330 /* TODO: convert ANSI (local windows encoding) to utf-8 on cygwin-1.5 */
333 safefree(posix_path);
336 safefree(posix_path);
341 XS(XS_Cygwin_posix_to_win_path)
344 int absolute_flag = 0;
347 char *src_path, *win_path;
350 if (items < 1 || items > 2)
351 Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
353 src_path = SvPVx(ST(0), len);
355 absolute_flag = SvTRUE(ST(1));
358 Perl_croak(aTHX_ "can't convert empty path");
359 isutf8 = SvUTF8(ST(0));
360 #if (CYGWIN_VERSION_API_MINOR >= 181)
361 /* Check utf8 flag and use wide api then.
362 Size calculation: On overflow let cygwin_conv_path calculate the final size.
365 int what = absolute_flag ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_W | CCP_RELATIVE;
366 int wlen = sizeof(wchar_t)*(len + 260 + 1001);
367 wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
368 wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
369 char *oldlocale = setlocale(LC_CTYPE, NULL);
370 setlocale(LC_CTYPE, "utf-8");
373 /* utf8_to_uvuni(src_path, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
374 wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
376 err = cygwin_conv_path(what, wpath, wbuf, wlen);
377 } else { /* use bytes; assume already ucs-2 encoded bytestream */
378 err = cygwin_conv_path(what, src_path, wbuf, wlen);
380 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
381 int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
382 wbuf = (wchar_t *) realloc(&wbuf, newlen);
383 err = cygwin_conv_path(what, wpath, wbuf, newlen);
386 /* also see utf8.c: Perl_utf16_to_utf8() or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
387 wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
388 win_path = (char *) safemalloc(wlen+1);
389 wcsrtombs(win_path, (const wchar_t **)&wbuf, wlen, NULL);
390 if (oldlocale) setlocale(LC_CTYPE, oldlocale);
391 else setlocale(LC_CTYPE, "C");
393 int what = absolute_flag ? CCP_POSIX_TO_WIN_A : CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
394 win_path = (char *) safemalloc(len + 260 + 1001);
395 err = cygwin_conv_path(what, src_path, win_path, len + 260 + 1001);
396 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
397 int newlen = cygwin_conv_path(what, src_path, win_path, 0);
398 win_path = (char *) realloc(&win_path, newlen);
399 err = cygwin_conv_path(what, src_path, win_path, newlen);
404 Perl_warn(aTHX_ "can't convert utf8 path");
405 win_path = (char *) safemalloc(len + 260 + 1001);
407 err = cygwin_conv_to_full_win32_path(src_path, win_path);
409 err = cygwin_conv_to_win32_path(src_path, win_path);
413 ST(0) = sv_2mortal(newSVpv(win_path, 0));
425 XS(XS_Cygwin_mount_table)
431 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
432 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
435 while ((mnt = getmntent (0))) {
437 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
438 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
439 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
440 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
441 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
447 XS(XS_Cygwin_mount_flags)
451 char flags[PATH_MAX];
455 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags( mnt_dir | '/cygdrive' )");
457 pathname = SvPV_nolen(ST(0));
459 if (!strcmp(pathname, "/cygdrive")) {
461 char system[PATH_MAX];
462 char user_flags[PATH_MAX];
463 char system_flags[PATH_MAX];
465 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
466 user_flags, system_flags);
468 if (strlen(user) > 0) {
469 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
471 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
474 ST(0) = sv_2mortal(newSVpv(flags, 0));
481 while ((mnt = getmntent (0))) {
482 if (!strcmp(pathname, mnt->mnt_dir)) {
483 strcpy(flags, mnt->mnt_type);
484 if (strlen(mnt->mnt_opts) > 0) {
486 strcat(flags, mnt->mnt_opts);
494 /* Check if arg is the current volume moint point if not default,
495 * and then use CW_GET_CYGDRIVE_INFO also.
499 char system[PATH_MAX];
500 char user_flags[PATH_MAX];
501 char system_flags[PATH_MAX];
503 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
504 user_flags, system_flags);
506 if (strlen(user) > 0) {
507 if (strcmp(user,pathname)) {
508 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
512 if (strcmp(user,pathname)) {
513 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
519 ST(0) = sv_2mortal(newSVpv(flags, 0));
527 XS(XS_Cygwin_is_binmount)
533 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
535 pathname = SvPV_nolen(ST(0));
537 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
545 char const *file = __FILE__;
548 newXS("Cwd::cwd", Cygwin_cwd, file);
549 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
550 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
551 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
552 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
553 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
554 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
555 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
557 /* Initialize Win32CORE if it has been statically linked. */
558 handle = dlopen(NULL, RTLD_LAZY);
560 void (*pfn_init)(pTHX);
561 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");