5 #define PERLIO_NOT_STDIO 0
8 #undef USE_DYNAMIC_LOADING
13 #include <sys/cygwin.h>
14 #include <cygwin/version.h>
18 #define HAVE_CYGWIN_VERSION(MAJOR, MINOR) \
19 (CYGWIN_VERSION_API_MAJOR > (MAJOR) || \
20 (CYGWIN_VERSION_API_MAJOR == (MAJOR) && CYGWIN_VERSION_API_MINOR >= (MINOR)))
21 #if HAVE_CYGWIN_VERSION(0, 181)
25 #define PATH_LEN_GUESS (260 + 1001)
28 * pp_system() implemented via spawn()
29 * - more efficient and useful when embedding Perl in non-Cygwin apps
30 * - code mostly borrowed from djgpp.c
33 do_spawnvp (const char *path, const char * const *argv)
36 Sigsave_t ihand,qhand;
37 int childpid, result, status;
39 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
40 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
41 childpid = spawnvp(_P_NOWAIT,path,argv);
44 ck_warner(packWARN(WARN_EXEC), "Can't spawn \"%s\": %s",
45 path, Strerror(errno));
48 result = wait4pid(childpid, &status, 0);
49 } while (result == -1 && errno == EINTR);
53 (void)rsignal_restore(SIGINT, &ihand);
54 (void)rsignal_restore(SIGQUIT, &qhand);
59 do_aspawn (SV *really, void **mark, void **sp)
69 argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
70 a=(char const **)argv;
74 *a++ = SvPVx((SV *)*mark, n_a);
79 if (argv[0][0] != '/' && argv[0][0] != '\\'
80 && !(argv[0][0] && argv[0][1] == ':'
81 && (argv[0][2] == '/' || argv[0][2] != '\\'))
82 ) /* will swawnvp use PATH? */
83 TAINT_ENV(); /* testing IFS here is overkill, probably */
85 if (really && *(tmps = SvPV(really, n_a)))
86 rc=do_spawnvp (tmps,(const char * const *)argv);
88 rc=do_spawnvp (argv[0],(const char *const *)argv);
97 char const **argv, **a;
99 char const *metachars = "$&*(){}[]'\";\\?>|<~`\n";
100 const char *command[4];
104 while (*cmd && isSPACE(*cmd))
107 if (strBEGINs (cmd,"/bin/sh") && isSPACE (cmd[7]))
110 /* save an extra exec if possible */
111 /* see if there are shell metacharacters in it */
112 if (strstr (cmd,"..."))
114 if (*cmd=='.' && isSPACE (cmd[1]))
116 if (strBEGINs (cmd,"exec") && isSPACE (cmd[4]))
118 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
123 if (strchr (metachars,*s))
125 if (*s=='\n' && s[1]=='\0')
136 result = do_spawnvp("sh",command);
140 Newx (argv, (s-cmd)/2+2, const char*);
142 cmd=savepvn (cmd,s-cmd);
146 while (*s && isSPACE (*s)) s++;
149 while (*s && !isSPACE (*s)) s++;
157 result = do_spawnvp(argv[0],(const char * const *)argv);
163 #if HAVE_CYGWIN_VERSION(0, 181)
165 wide_to_utf8(const wchar_t *wsrc)
168 const Size_t wlen = (wcslen(wsrc) + 1) * sizeof(wchar_t);
170 /* Max expansion factor is 3/2 */
171 Size_t blen = wlen * 3 / 2;
173 char *buf = (char *) safemalloc(blen);
175 utf16_to_utf8((U8 *) wsrc, (U8 *) buf, wlen, &blen);
181 utf8_to_wide_extra_len(const char *buf, Size_t extra_len)
183 /* Return the conversion to UTF-16 of the UTF-8 string 'buf'
184 * (terminated by a NUL), making sure to have space for at least *extra_len
185 * extra (wide) characters in the result. The result must be freed by the
186 * caller when no longer needed */
189 Size_t len = strlen(buf) + extra_len + 1;
191 /* Max expansion factor is sizeof(wchar_t) */
192 Size_t wlen = sizeof(wchar_t) * len;
194 wchar_t* wsrc = (wchar_t *) safemalloc(wlen);
196 utf8_to_utf16((U8 *) buf, (U8 *) wsrc, len, &wlen);
202 utf8_to_wide(const char *buf)
204 return utf8_to_wide_extra_len(buf, 0);
207 #endif /* cygwin 1.7 */
209 /* see also Cwd.pm */
215 /* See https://github.com/Perl/perl5/issues/8345
216 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
219 croak("Usage: Cwd::cwd()");
220 if((cwd = getcwd(NULL, -1))) {
221 ST(0) = sv_2mortal(newSVpv(cwd, 0));
229 XS(XS_Cygwin_pid_to_winpid)
236 croak("Usage: Cygwin::pid_to_winpid(pid)");
238 pid = (pid_t)SvIV(ST(0));
240 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
241 XSprePUSH; PUSHi((IV)RETVAL);
247 XS(XS_Cygwin_winpid_to_pid)
254 croak("Usage: Cygwin::winpid_to_pid(pid)");
256 pid = (pid_t)SvIV(ST(0));
258 #if HAVE_CYGWIN_VERSION(0, 181)
259 RETVAL = cygwin_winpid_to_pid(pid);
261 RETVAL = cygwin32_winpid_to_pid(pid);
264 XSprePUSH; PUSHi((IV)RETVAL);
270 /* The conversion between Posix and Windows paths is essentially the same in
271 * either direction, so a common function is used, with which direction passed
274 * These numbers are chosen so can be or'd with absolute flag to get 0..3 */
281 S_convert_path_common(pTHX_ const direction_t direction)
284 bool absolute_flag = 0;
288 char *converted_path;
291 if (items < 1 || items > 2) {
292 const char *name = (direction == to_posix)
293 ? "win::win_to_posix_path"
294 : "posix_to_win_path";
295 croak("Usage: Cygwin::%s(pathname, [absolute])", name);
298 src_path = SvPVx(ST(0), len);
300 absolute_flag = SvTRUE(ST(1));
303 croak("can't convert empty path");
304 isutf8 = SvUTF8(ST(0));
306 #if HAVE_CYGWIN_VERSION(0, 181)
307 /* Check utf8 flag and use wide api then.
308 Size calculation: On overflow let cygwin_conv_path calculate the final size.
311 int what = ((absolute_flag) ? 0 : CCP_RELATIVE)
312 | ((direction == to_posix)
314 : CCP_POSIX_TO_WIN_W);
316 wchar_t *wsrc = NULL; /* The source, as a wchar_t */
317 wchar_t *wconverted = NULL; /* wsrc, converted to the destination */
319 /* ptr to either wsrc, or under BYTES, the src_path so can have common
321 wchar_t *which_src = (wchar_t *) src_path;
323 if (LIKELY(! IN_BYTES)) { /* Normal case, convert UTF-8 to UTF-16 */
324 wlen = PATH_LEN_GUESS;
325 wsrc = utf8_to_wide_extra_len(src_path, wlen);
328 else { /* use bytes; assume already UTF-16 encoded bytestream */
329 wlen = sizeof(wchar_t) * (len + PATH_LEN_GUESS);
332 if (LIKELY(wlen > 0)) { /* Make sure didn't get an error */
333 wconverted = (wchar_t *) safemalloc(wlen);
334 err = cygwin_conv_path(what, which_src, wconverted, wlen);
337 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
338 int newlen = cygwin_conv_path(what, which_src, wconverted, 0);
339 wconverted = (wchar_t *) realloc(wconverted, newlen);
340 err = cygwin_conv_path(what, which_src, wconverted, newlen);
343 converted_path = wide_to_utf8(wconverted);
345 safefree(wconverted);
348 int what = ((absolute_flag) ? 0 : CCP_RELATIVE)
349 | ((direction == to_posix)
351 : CCP_POSIX_TO_WIN_A);
353 converted_path = (char *) safemalloc (len + PATH_LEN_GUESS);
354 err = cygwin_conv_path(what, src_path, converted_path, len + PATH_LEN_GUESS);
355 if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
356 int newlen = cygwin_conv_path(what, src_path, converted_path, 0);
357 converted_path = (char *) realloc(converted_path, newlen);
358 err = cygwin_conv_path(what, src_path, converted_path, newlen);
363 converted_path = (char *) safemalloc (len + PATH_LEN_GUESS);
365 switch (absolute_flag | direction) {
367 err = cygwin_conv_to_full_posix_path(src_path, converted_path);
370 err = cygwin_conv_to_posix_path(src_path, converted_path);
373 err = cygwin_conv_to_full_win32_path(src_path, converted_path);
376 err = cygwin_conv_to_win32_path(src_path, converted_path);
384 ST(0) = sv_2mortal(newSVpv(converted_path, 0));
385 if (isutf8) { /* src was utf-8, so result should also */
386 /* TODO: convert ANSI (local windows encoding) to utf-8 on cygwin-1.5 */
389 safefree(converted_path);
392 safefree(converted_path);
397 XS(XS_Cygwin_win_to_posix_path)
399 S_convert_path_common(aTHX_ to_posix);
402 XS(XS_Cygwin_posix_to_win_path)
404 S_convert_path_common(aTHX_ to_win);
407 XS(XS_Cygwin_mount_table)
413 croak("Usage: Cygwin::mount_table");
414 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
417 while ((mnt = getmntent (0))) {
419 av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
420 av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
421 av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
422 av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
423 XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
429 XS(XS_Cygwin_mount_flags)
433 char flags[PATH_MAX];
437 croak("Usage: Cygwin::mount_flags( mnt_dir | '/cygdrive' )");
439 pathname = SvPV_nolen(ST(0));
441 if (strEQ(pathname, "/cygdrive")) {
443 char system[PATH_MAX];
444 char user_flags[PATH_MAX];
445 char system_flags[PATH_MAX];
447 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
448 user_flags, system_flags);
450 if (strlen(user) > 0) {
451 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
453 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
456 ST(0) = sv_2mortal(newSVpv(flags, 0));
463 while ((mnt = getmntent (0))) {
464 if (strEQ(pathname, mnt->mnt_dir)) {
465 strcpy(flags, mnt->mnt_type);
466 if (strlen(mnt->mnt_opts) > 0) {
468 strcat(flags, mnt->mnt_opts);
476 /* Check if arg is the current volume moint point if not default,
477 * and then use CW_GET_CYGDRIVE_INFO also.
481 char system[PATH_MAX];
482 char user_flags[PATH_MAX];
483 char system_flags[PATH_MAX];
485 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
486 user_flags, system_flags);
488 if (strlen(user) > 0) {
489 if (strNE(user,pathname)) {
490 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
494 if (strNE(user,pathname)) {
495 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
501 ST(0) = sv_2mortal(newSVpv(flags, 0));
509 XS(XS_Cygwin_is_binmount)
515 croak("Usage: Cygwin::is_binmount(pathname)");
517 pathname = SvPV_nolen(ST(0));
519 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
523 XS(XS_Cygwin_sync_winenv){ cygwin_internal(CW_SYNC_WINENV); }
529 char const *file = __FILE__;
532 newXS("Cwd::cwd", Cygwin_cwd, file);
533 newXSproto("Cygwin::winpid_to_pid", XS_Cygwin_winpid_to_pid, file, "$");
534 newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
535 newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
536 newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
537 newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
538 newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
539 newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
540 newXS("Cygwin::sync_winenv", XS_Cygwin_sync_winenv, file);
542 /* Initialize Win32CORE if it has been statically linked. */
543 handle = dlopen(NULL, RTLD_LAZY);
545 void (*pfn_init)(pTHX);
546 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");