]> perl5.git.perl.org Git - perl5.git/blob - cygwin/cygwin.c This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Deparse: exclude two new test files
[perl5.git] / cygwin / cygwin.c
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 #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)
22 #include <wchar.h>
23 #endif
24
25 #define PATH_LEN_GUESS (260 + 1001)
26
27 /*
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
31  */
32 static int
33 do_spawnvp (const char *path, const char * const *argv)
34 {
35     dTHX;
36     Sigsave_t ihand,qhand;
37     int childpid, result, status;
38
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);
42     if (childpid < 0) {
43         status = -1;
44         ck_warner(packWARN(WARN_EXEC), "Can't spawn \"%s\": %s",
45                   path, Strerror(errno));
46     } else {
47         do {
48             result = wait4pid(childpid, &status, 0);
49         } while (result == -1 && errno == EINTR);
50         if(result < 0)
51             status = -1;
52     }
53     (void)rsignal_restore(SIGINT, &ihand);
54     (void)rsignal_restore(SIGQUIT, &qhand);
55     return status;
56 }
57
58 int
59 do_aspawn (SV *really, void **mark, void **sp)
60 {
61     dTHX;
62     int  rc;
63     char const **a;
64     char *tmps,**argv;
65     STRLEN n_a;
66
67     if (sp<=mark)
68         return -1;
69     argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
70     a=(char const **)argv;
71
72     while (++mark <= sp)
73         if (*mark)
74             *a++ = SvPVx((SV *)*mark, n_a);
75         else
76             *a++ = "";
77     *a = (char*)NULL;
78
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 */
84
85     if (really && *(tmps = SvPV(really, n_a)))
86         rc=do_spawnvp (tmps,(const char * const *)argv);
87     else
88         rc=do_spawnvp (argv[0],(const char *const *)argv);
89
90     return rc;
91 }
92
93 int
94 do_spawn (char *cmd)
95 {
96     dTHX;
97     char const **argv, **a;
98     char *s;
99     char const *metachars = "$&*(){}[]'\";\\?>|<~`\n";
100     const char *command[4];
101     int result;
102
103     ENTER;
104     while (*cmd && isSPACE(*cmd))
105         cmd++;
106
107     if (strBEGINs (cmd,"/bin/sh") && isSPACE (cmd[7]))
108         cmd+=5;
109
110     /* save an extra exec if possible */
111     /* see if there are shell metacharacters in it */
112     if (strstr (cmd,"..."))
113         goto doshell;
114     if (*cmd=='.' && isSPACE (cmd[1]))
115         goto doshell;
116     if (strBEGINs (cmd,"exec") && isSPACE (cmd[4]))
117         goto doshell;
118     for (s=cmd; *s && isALPHA (*s); s++) ;      /* catch VAR=val gizmo */
119     if (*s=='=')
120         goto doshell;
121
122     for (s=cmd; *s; s++)
123         if (strchr (metachars,*s))
124         {
125             if (*s=='\n' && s[1]=='\0')
126             {
127                 *s='\0';
128                 break;
129             }
130         doshell:
131             command[0] = "sh";
132             command[1] = "-c";
133             command[2] = cmd;
134             command[3] = NULL;
135
136             result = do_spawnvp("sh",command);
137             goto leave;
138         }
139
140     Newx (argv, (s-cmd)/2+2, const char*);
141     SAVEFREEPV(argv);
142     cmd=savepvn (cmd,s-cmd);
143     SAVEFREEPV(cmd);
144     a=argv;
145     for (s=cmd; *s;) {
146         while (*s && isSPACE (*s)) s++;
147         if (*s)
148             *(a++)=s;
149         while (*s && !isSPACE (*s)) s++;
150         if (*s)
151             *s++='\0';
152     }
153     *a = (char*)NULL;
154     if (!argv[0])
155         result = -1;
156     else
157         result = do_spawnvp(argv[0],(const char * const *)argv);
158 leave:
159     LEAVE;
160     return result;
161 }
162
163 #if HAVE_CYGWIN_VERSION(0, 181)
164 char*
165 wide_to_utf8(const wchar_t *wsrc)
166 {
167     dTHX;
168     const Size_t wlen = (wcslen(wsrc) + 1) * sizeof(wchar_t);
169
170     /* Max expansion factor is 3/2 */
171     Size_t blen = wlen * 3 / 2;
172
173     char *buf = (char *) safemalloc(blen);
174
175     utf16_to_utf8((U8 *) wsrc, (U8 *) buf, wlen, &blen);
176
177     return buf;
178 }
179
180 wchar_t*
181 utf8_to_wide_extra_len(const char *buf, Size_t extra_len)
182 {
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 */
187
188     dTHX;
189     Size_t len = strlen(buf) + extra_len + 1;
190
191     /* Max expansion factor is sizeof(wchar_t) */
192     Size_t wlen = sizeof(wchar_t) * len;
193
194     wchar_t* wsrc = (wchar_t *) safemalloc(wlen);
195
196     utf8_to_utf16((U8 *) buf, (U8 *) wsrc, len, &wlen);
197
198     return wsrc;
199 }
200
201 wchar_t*
202 utf8_to_wide(const char *buf)
203 {
204     return utf8_to_wide_extra_len(buf, 0);
205 }
206
207 #endif /* cygwin 1.7 */
208
209 /* see also Cwd.pm */
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         croak("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
229 XS(XS_Cygwin_pid_to_winpid)
230 {
231     dXSARGS;
232     dXSTARG;
233     pid_t pid, RETVAL;
234
235     if (items != 1)
236         croak("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
247 XS(XS_Cygwin_winpid_to_pid)
248 {
249     dXSARGS;
250     dXSTARG;
251     pid_t pid, RETVAL;
252
253     if (items != 1)
254         croak("Usage: Cygwin::winpid_to_pid(pid)");
255
256     pid = (pid_t)SvIV(ST(0));
257
258 #if HAVE_CYGWIN_VERSION(0, 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
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         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);
296     }
297
298     src_path = SvPVx(ST(0), len);
299     if (items == 2)
300         absolute_flag = SvTRUE(ST(1));
301
302     if (!len)
303         croak("can't convert empty path");
304     isutf8 = SvUTF8(ST(0));
305
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.
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
397 XS(XS_Cygwin_win_to_posix_path)
398 {
399     S_convert_path_common(aTHX_ to_posix);
400 }
401
402 XS(XS_Cygwin_posix_to_win_path)
403 {
404     S_convert_path_common(aTHX_ to_win);
405 }
406
407 XS(XS_Cygwin_mount_table)
408 {
409     dXSARGS;
410     struct mntent *mnt;
411
412     if (items != 0)
413         croak("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
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         croak("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
509 XS(XS_Cygwin_is_binmount)
510 {
511     dXSARGS;
512     char *pathname;
513
514     if (items != 1)
515         croak("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
523 XS(XS_Cygwin_sync_winenv){ cygwin_internal(CW_SYNC_WINENV); }
524
525 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 }