1 /*
2 * Cygwin extras
3 */
4
5 #define PERLIO_NOT_STDIO 0
6 #include "EXTERN.h"
7 #include "perl.h"
8 #undef USE_DYNAMIC_LOADING
9 #include "XSUB.h"
10
11 #include <unistd.h>
12 #include <process.h>
13 #include <sys/cygwin.h>
14 #include <cygwin/version.h>
15 #include <mntent.h>
16 #include <alloca.h>
17 #include <dlfcn.h>
18 #if (CYGWIN_VERSION_API_MINOR >= 181)
19 #include <wchar.h>
20 #endif
21
22 #define PATH_LEN_GUESS (260 + 1001)
23
24 /*
25 * pp_system() implemented via spawn()
26 * - more efficient and useful when embedding Perl in non-Cygwin apps
27 * - code mostly borrowed from djgpp.c
28 */
29 static int
do_spawnvp(const char * path,const char * const * argv)30 do_spawnvp (const char *path, const char * const *argv)
31 {
32 dTHX;
33 Sigsave_t ihand,qhand;
34 int childpid, result, status;
35
36 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
37 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
38 childpid = spawnvp(_P_NOWAIT,path,argv);
39 if (childpid < 0) {
40 status = -1;
41 if(ckWARN(WARN_EXEC))
42 Perl_warner(aTHX_ packWARN(WARN_EXEC),"Can't spawn \"%s\": %s",
43 path,Strerror (errno));
44 } else {
45 do {
46 result = wait4pid(childpid, &status, 0);
47 } while (result == -1 && errno == EINTR);
48 if(result < 0)
49 status = -1;
50 }
51 (void)rsignal_restore(SIGINT, &ihand);
52 (void)rsignal_restore(SIGQUIT, &qhand);
53 return status;
54 }
55
56 int
do_aspawn(SV * really,void ** mark,void ** sp)57 do_aspawn (SV *really, void **mark, void **sp)
58 {
59 dTHX;
60 int rc;
61 char const **a;
62 char *tmps,**argv;
63 STRLEN n_a;
64
65 if (sp<=mark)
66 return -1;
67 argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
68 a=(char const **)argv;
69
70 while (++mark <= sp)
71 if (*mark)
72 *a++ = SvPVx((SV *)*mark, n_a);
73 else
74 *a++ = "";
75 *a = (char*)NULL;
76
77 if (argv[0][0] != '/' && argv[0][0] != '\\'
78 && !(argv[0][0] && argv[0][1] == ':'
79 && (argv[0][2] == '/' || argv[0][2] != '\\'))
80 ) /* will swawnvp use PATH? */
81 TAINT_ENV(); /* testing IFS here is overkill, probably */
82
83 if (really && *(tmps = SvPV(really, n_a)))
84 rc=do_spawnvp (tmps,(const char * const *)argv);
85 else
86 rc=do_spawnvp (argv[0],(const char *const *)argv);
87
88 return rc;
89 }
90
91 int
do_spawn(char * cmd)92 do_spawn (char *cmd)
93 {
94 dTHX;
95 char const **argv, **a;
96 char *s;
97 char const *metachars = "$&*(){}[]'\";\\?>|<~`\n";
98 const char *command[4];
99 int result;
100
101 ENTER;
102 while (*cmd && isSPACE(*cmd))
103 cmd++;
104
105 if (strBEGINs (cmd,"/bin/sh") && isSPACE (cmd[7]))
106 cmd+=5;
107
108 /* save an extra exec if possible */
109 /* see if there are shell metacharacters in it */
110 if (strstr (cmd,"..."))
111 goto doshell;
112 if (*cmd=='.' && isSPACE (cmd[1]))
113 goto doshell;
114 if (strBEGINs (cmd,"exec") && isSPACE (cmd[4]))
115 goto doshell;
116 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
117 if (*s=='=')
118 goto doshell;
119
120 for (s=cmd; *s; s++)
121 if (strchr (metachars,*s))
122 {
123 if (*s=='\n' && s[1]=='\0')
124 {
125 *s='\0';
126 break;
127 }
128 doshell:
129 command[0] = "sh";
130 command[1] = "-c";
131 command[2] = cmd;
132 command[3] = NULL;
133
134 result = do_spawnvp("sh",command);
135 goto leave;
136 }
137
138 Newx (argv, (s-cmd)/2+2, const char*);
139 SAVEFREEPV(argv);
140 cmd=savepvn (cmd,s-cmd);
141 SAVEFREEPV(cmd);
142 a=argv;
143 for (s=cmd; *s;) {
144 while (*s && isSPACE (*s)) s++;
145 if (*s)
146 *(a++)=s;
147 while (*s && !isSPACE (*s)) s++;
148 if (*s)
149 *s++='\0';
150 }
151 *a = (char*)NULL;
152 if (!argv[0])
153 result = -1;
154 else
155 result = do_spawnvp(argv[0],(const char * const *)argv);
156 leave:
157 LEAVE;
158 return result;
159 }
160
161 #if (CYGWIN_VERSION_API_MINOR >= 181)
162 char*
wide_to_utf8(const wchar_t * wsrc)163 wide_to_utf8(const wchar_t *wsrc)
164 {
165 dTHX;
166 const Size_t wlen = (wcslen(wsrc) + 1) * sizeof(wchar_t);
167
168 /* Max expansion factor is 3/2 */
169 Size_t blen = wlen * 3 / 2;
170
171 char *buf = (char *) safemalloc(blen);
172
173 utf16_to_utf8((U8 *) wsrc, buf, wlen, &blen);
174
175 return buf;
176 }
177
178 wchar_t*
utf8_to_wide_extra_len(const char * buf,Size_t * extra_len)179 utf8_to_wide_extra_len(const char *buf, Size_t *extra_len)
180 {
181 /* Return the conversion to UTF-16 of the UTF-8 string 'buf'
182 * (terminated by a NUL), making sure to have space for at least *extra_len
183 * extra (wide) characters in the result. The result must be freed by the
184 * caller when no longer needed */
185
186 dTHX;
187 Size_t len = strlen(buf) + extra_len + 1;
188
189 /* Max expansion factor is sizeof(wchar_t) */
190 Size_t wlen = sizeof(wchar_t) * len;
191
192 wchar_t* wsrc = (wchar_t *) safemalloc(wlen);
193
194 utf8_to_utf16(buf, (U8 *) wsrc, len, &wlen);
195
196 return wsrc;
197 }
198
199 wchar_t*
utf8_to_wide(const char * buf)200 utf8_to_wide(const char *buf)
201 {
202 Size_t extra_len = 0;
203
204 return utf8_to_wide_extra_len(buf, &extra_len);
205 }
206
207 #endif /* cygwin 1.7 */
208
209 /* see also Cwd.pm */
XS(Cygwin_cwd)210 XS(Cygwin_cwd)
211 {
212 dXSARGS;
213 char *cwd;
214
215 /* See https://github.com/Perl/perl5/issues/8345
216 There is Cwd->cwd() usage in the wild, and previous versions didn't die.
217 */
218 if(items > 1)
219 Perl_croak(aTHX_ "Usage: Cwd::cwd()");
220 if((cwd = getcwd(NULL, -1))) {
221 ST(0) = sv_2mortal(newSVpv(cwd, 0));
222 free(cwd);
223 SvTAINTED_on(ST(0));
224 XSRETURN(1);
225 }
226 XSRETURN_UNDEF;
227 }
228
XS(XS_Cygwin_pid_to_winpid)229 XS(XS_Cygwin_pid_to_winpid)
230 {
231 dXSARGS;
232 dXSTARG;
233 pid_t pid, RETVAL;
234
235 if (items != 1)
236 Perl_croak(aTHX_ "Usage: Cygwin::pid_to_winpid(pid)");
237
238 pid = (pid_t)SvIV(ST(0));
239
240 if ((RETVAL = cygwin_internal(CW_CYGWIN_PID_TO_WINPID, pid)) > 0) {
241 XSprePUSH; PUSHi((IV)RETVAL);
242 XSRETURN(1);
243 }
244 XSRETURN_UNDEF;
245 }
246
XS(XS_Cygwin_winpid_to_pid)247 XS(XS_Cygwin_winpid_to_pid)
248 {
249 dXSARGS;
250 dXSTARG;
251 pid_t pid, RETVAL;
252
253 if (items != 1)
254 Perl_croak(aTHX_ "Usage: Cygwin::winpid_to_pid(pid)");
255
256 pid = (pid_t)SvIV(ST(0));
257
258 #if (CYGWIN_VERSION_API_MINOR >= 181)
259 RETVAL = cygwin_winpid_to_pid(pid);
260 #else
261 RETVAL = cygwin32_winpid_to_pid(pid);
262 #endif
263 if (RETVAL > 0) {
264 XSprePUSH; PUSHi((IV)RETVAL);
265 XSRETURN(1);
266 }
267 XSRETURN_UNDEF;
268 }
269
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
272 * in.
273 *
274 * These numbers are chosen so can be or'd with absolute flag to get 0..3 */
275 typedef enum {
276 to_posix = 0,
277 to_win = 2
278 } direction_t;
279
280 static void
S_convert_path_common(pTHX_ const direction_t direction)281 S_convert_path_common(pTHX_ const direction_t direction)
282 {
283 dXSARGS;
284 bool absolute_flag = 0;
285 STRLEN len;
286 int err = 0;
287 char *src_path;
288 char *converted_path;
289 int isutf8 = 0;
290
291 if (items < 1 || items > 2) {
292 char *name = (direction == to_posix)
293 ? "win::win_to_posix_path"
294 : "posix_to_win_path";
295 Perl_croak(aTHX_ "Usage: Cygwin::%s(pathname, [absolute])", name);
296 }
297
298 src_path = SvPVx(ST(0), len);
299 if (items == 2)
300 absolute_flag = SvTRUE(ST(1));
301
302 if (!len)
303 Perl_croak(aTHX_ "can't convert empty path");
304 isutf8 = SvUTF8(ST(0));
305
306 #if (CYGWIN_VERSION_API_MINOR >= 181)
307 /* Check utf8 flag and use wide api then.
308 Size calculation: On overflow let cygwin_conv_path calculate the final size.
309 */
310 if (isutf8) {
311 int what = ((absolute_flag) ? 0 : CCP_RELATIVE)
312 | ((direction == to_posix)
313 ? CCP_WIN_W_TO_POSIX
314 : CCP_POSIX_TO_WIN_W);
315 STRLEN wlen;
316 wchar_t *wsrc = NULL; /* The source, as a wchar_t */
317 wchar_t *wconverted = NULL; /* wsrc, converted to the destination */
318
319 /* ptr to either wsrc, or under BYTES, the src_path so can have common
320 * code below */
321 wchar_t *which_src = (wchar_t *) src_path;
322
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);
326 which_src = wsrc;
327 }
328 else { /* use bytes; assume already UTF-16 encoded bytestream */
329 wlen = sizeof(wchar_t) * (len + PATH_LEN_GUESS);
330 }
331
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);
335 }
336
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);
341 }
342
343 converted_path = wide_to_utf8(wconverted);
344
345 safefree(wconverted);
346 safefree(wsrc);
347 } else {
348 int what = ((absolute_flag) ? 0 : CCP_RELATIVE)
349 | ((direction == to_posix)
350 ? CCP_WIN_A_TO_POSIX
351 : CCP_POSIX_TO_WIN_A);
352
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);
359 }
360 }
361
362 #else
363 converted_path = (char *) safemalloc (len + PATH_LEN_GUESS);
364
365 switch (absolute_flag | direction) {
366 case (1|to_posix):
367 err = cygwin_conv_to_full_posix_path(src_path, converted_path);
368 break;
369 case (0|to_posix):
370 err = cygwin_conv_to_posix_path(src_path, converted_path);
371 break;
372 case (1|to_win):
373 err = cygwin_conv_to_full_win32_path(src_path, converted_path);
374 break;
375 case (0|to_win):
376 err = cygwin_conv_to_win32_path(src_path, converted_path);
377 break;
378 }
379
380 #endif
381
382 if (!err) {
383 EXTEND(SP, 1);
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 */
387 SvUTF8_on(ST(0));
388 }
389 safefree(converted_path);
390 XSRETURN(1);
391 } else {
392 safefree(converted_path);
393 XSRETURN_UNDEF;
394 }
395 }
396
XS(XS_Cygwin_win_to_posix_path)397 XS(XS_Cygwin_win_to_posix_path)
398 {
399 S_convert_path_common(aTHX_ to_posix);
400 }
401
XS(XS_Cygwin_posix_to_win_path)402 XS(XS_Cygwin_posix_to_win_path)
403 {
404 S_convert_path_common(aTHX_ to_win);
405 }
406
XS(XS_Cygwin_mount_table)407 XS(XS_Cygwin_mount_table)
408 {
409 dXSARGS;
410 struct mntent *mnt;
411
412 if (items != 0)
413 Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
414 /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
415
416 setmntent (0, 0);
417 while ((mnt = getmntent (0))) {
418 AV* av = newAV();
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)));
424 }
425 endmntent (0);
426 PUTBACK;
427 }
428
XS(XS_Cygwin_mount_flags)429 XS(XS_Cygwin_mount_flags)
430 {
431 dXSARGS;
432 char *pathname;
433 char flags[PATH_MAX];
434 flags[0] = '\0';
435
436 if (items != 1)
437 Perl_croak(aTHX_ "Usage: Cygwin::mount_flags( mnt_dir | '/cygdrive' )");
438
439 pathname = SvPV_nolen(ST(0));
440
441 if (strEQ(pathname, "/cygdrive")) {
442 char user[PATH_MAX];
443 char system[PATH_MAX];
444 char user_flags[PATH_MAX];
445 char system_flags[PATH_MAX];
446
447 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
448 user_flags, system_flags);
449
450 if (strlen(user) > 0) {
451 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
452 } else {
453 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
454 }
455
456 ST(0) = sv_2mortal(newSVpv(flags, 0));
457 XSRETURN(1);
458
459 } else {
460 struct mntent *mnt;
461 int found = 0;
462 setmntent (0, 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) {
467 strcat(flags, ",");
468 strcat(flags, mnt->mnt_opts);
469 }
470 found++;
471 break;
472 }
473 }
474 endmntent (0);
475
476 /* Check if arg is the current volume moint point if not default,
477 * and then use CW_GET_CYGDRIVE_INFO also.
478 */
479 if (!found) {
480 char user[PATH_MAX];
481 char system[PATH_MAX];
482 char user_flags[PATH_MAX];
483 char system_flags[PATH_MAX];
484
485 cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
486 user_flags, system_flags);
487
488 if (strlen(user) > 0) {
489 if (strNE(user,pathname)) {
490 sprintf(flags, "%s,cygdrive,%s", user_flags, user);
491 found++;
492 }
493 } else {
494 if (strNE(user,pathname)) {
495 sprintf(flags, "%s,cygdrive,%s", system_flags, system);
496 found++;
497 }
498 }
499 }
500 if (found) {
501 ST(0) = sv_2mortal(newSVpv(flags, 0));
502 XSRETURN(1);
503 } else {
504 XSRETURN_UNDEF;
505 }
506 }
507 }
508
XS(XS_Cygwin_is_binmount)509 XS(XS_Cygwin_is_binmount)
510 {
511 dXSARGS;
512 char *pathname;
513
514 if (items != 1)
515 Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
516
517 pathname = SvPV_nolen(ST(0));
518
519 ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
520 XSRETURN(1);
521 }
522
XS(XS_Cygwin_sync_winenv)523 XS(XS_Cygwin_sync_winenv){ cygwin_internal(CW_SYNC_WINENV); }
524
525 void
init_os_extras(void)526 init_os_extras(void)
527 {
528 dTHX;
529 char const *file = __FILE__;
530 void *handle;
531
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);
541
542 /* Initialize Win32CORE if it has been statically linked. */
543 handle = dlopen(NULL, RTLD_LAZY);
544 if (handle) {
545 void (*pfn_init)(pTHX);
546 pfn_init = (void (*)(pTHX))dlsym(handle, "init_Win32CORE");
547 if (pfn_init)
548 pfn_init(aTHX);
549 dlclose(handle);
550 }
551 }
552