This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Document that the -Ci, -Co, and -CD options have file scope
[perl5.git] / cygwin / cygwin.c
index 57f3b6a..aa6938d 100644 (file)
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <process.h>
 #include <sys/cygwin.h>
+#include <mntent.h>
 #include <alloca.h>
 #include <dlfcn.h>
 
@@ -50,19 +51,21 @@ do_aspawn (SV *really, void **mark, void **sp)
 {
     dTHX;
     int  rc;
-    char **a,*tmps,**argv; 
-    STRLEN n_a; 
+    char const **a;
+    char *tmps,**argv;
+    STRLEN n_a;
 
     if (sp<=mark)
         return -1;
-    a=argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
+    argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
+    a=(char const **)argv;
 
     while (++mark <= sp)
         if (*mark)
             *a++ = SvPVx((SV *)*mark, n_a);
         else
             *a++ = "";
-    *a = Nullch;
+    *a = (char*)NULL;
 
     if (argv[0][0] != '/' && argv[0][0] != '\\'
         && !(argv[0][0] && argv[0][1] == ':'
@@ -82,7 +85,9 @@ int
 do_spawn (char *cmd)
 {
     dTHX;
-    char **a,*s,*metachars = "$&*(){}[]'\";\\?>|<~`\n";
+    char const **a;
+    char *s;
+    char const *metachars = "$&*(){}[]'\";\\?>|<~`\n";
     const char *command[4];
 
     while (*cmd && isSPACE(*cmd))
@@ -120,7 +125,7 @@ do_spawn (char *cmd)
            return do_spawnvp("sh",command);
        }
 
-    Newx (PL_Argv,(s-cmd)/2+2,char*);
+    Newx (PL_Argv,(s-cmd)/2+2,const char*);
     PL_Cmd=savepvn (cmd,s-cmd);
     a=PL_Argv;
     for (s=PL_Cmd; *s;) {
@@ -131,7 +136,7 @@ do_spawn (char *cmd)
        if (*s)
            *s++='\0';
     }
-    *a=Nullch;
+    *a = (char*)NULL;
     if (!PL_Argv[0])
         return -1;
 
@@ -144,7 +149,10 @@ XS(Cygwin_cwd)
     dXSARGS;
     char *cwd;
 
-    if(items != 0)
+    /* See http://rt.perl.org/rt3/Ticket/Display.html?id=38628 
+       There is Cwd->cwd() usage in the wild, and previous versions didn't die.
+     */
+    if(items > 1)
        Perl_croak(aTHX_ "Usage: Cwd::cwd()");
     if((cwd = getcwd(NULL, -1))) {
        ST(0) = sv_2mortal(newSVpv(cwd, 0));
@@ -259,31 +267,90 @@ XS(XS_Cygwin_posix_to_win_path)
     }
 }
 
-XS(XS_Cygwin_is_binmount)
+XS(XS_Cygwin_mount_table)
+{
+    dXSARGS;
+    struct mntent *mnt;
+
+    if (items != 0)
+        Perl_croak(aTHX_ "Usage: Cygwin::mount_table");
+    /* => array of [mnt_dir mnt_fsname mnt_type mnt_opts] */
+
+    setmntent (0, 0);
+    while ((mnt = getmntent (0))) {
+       AV* av = newAV();
+       av_push(av, newSVpvn(mnt->mnt_dir, strlen(mnt->mnt_dir)));
+       av_push(av, newSVpvn(mnt->mnt_fsname, strlen(mnt->mnt_fsname)));
+       av_push(av, newSVpvn(mnt->mnt_type, strlen(mnt->mnt_type)));
+       av_push(av, newSVpvn(mnt->mnt_opts, strlen(mnt->mnt_opts)));
+       XPUSHs(sv_2mortal(newRV_noinc((SV*)av)));
+    }
+    endmntent (0);
+    PUTBACK;
+}
+
+XS(XS_Cygwin_mount_flags)
 {
     dXSARGS;
     char *pathname;
+    char flags[260];
 
     if (items != 1)
-        Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
+        Perl_croak(aTHX_ "Usage: Cygwin::mount_flags(mnt_dir|'/cygwin')");
 
     pathname = SvPV_nolen(ST(0));
 
-    ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
-    XSRETURN(1);
+    /* TODO: Check for cygdrive registry setting,
+     *       and then use CW_GET_CYGDRIVE_INFO
+     */
+    if (!strcmp(pathname, "/cygdrive")) {
+       char user[260];
+       char system[260];
+       char user_flags[260];
+       char system_flags[260];
+
+       cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system, user_flags,
+                        system_flags);
+
+        if (strlen(user) > 0) {
+            sprintf(flags, "%s,cygdrive,%s", user_flags, user);
+        } else {
+            sprintf(flags, "%s,cygdrive,%s", system_flags, system);
+        }
+
+       ST(0) = sv_2mortal(newSVpv(flags, 0));
+       XSRETURN(1);
+
+    } else {
+       struct mntent *mnt;
+       setmntent (0, 0);
+       while ((mnt = getmntent (0))) {
+           if (!strcmp(pathname, mnt->mnt_dir)) {
+               strcpy(flags, mnt->mnt_type);
+               if (strlen(mnt->mnt_opts) > 0) {
+                   strcat(flags, ",");
+                   strcat(flags, mnt->mnt_opts);
+               }
+               break;
+           }
+       }
+       endmntent (0);
+       ST(0) = sv_2mortal(newSVpv(flags, 0));
+       XSRETURN(1);
+    }
 }
 
-XS(XS_Cygwin_is_textmount)
+XS(XS_Cygwin_is_binmount)
 {
     dXSARGS;
     char *pathname;
 
     if (items != 1)
-        Perl_croak(aTHX_ "Usage: Cygwin::is_textmount(pathname)");
+        Perl_croak(aTHX_ "Usage: Cygwin::is_binmount(pathname)");
 
     pathname = SvPV_nolen(ST(0));
 
-    ST(0) = boolSV(!cygwin_internal(CW_GET_BINMODE, pathname));
+    ST(0) = boolSV(cygwin_internal(CW_GET_BINMODE, pathname));
     XSRETURN(1);
 }
 
@@ -291,7 +358,7 @@ void
 init_os_extras(void)
 {
     dTHX;
-    char *file = __FILE__;
+    char const *file = __FILE__;
     void *handle;
 
     newXS("Cwd::cwd", Cygwin_cwd, file);
@@ -299,8 +366,9 @@ init_os_extras(void)
     newXSproto("Cygwin::pid_to_winpid", XS_Cygwin_pid_to_winpid, file, "$");
     newXSproto("Cygwin::win_to_posix_path", XS_Cygwin_win_to_posix_path, file, "$;$");
     newXSproto("Cygwin::posix_to_win_path", XS_Cygwin_posix_to_win_path, file, "$;$");
+    newXSproto("Cygwin::mount_table", XS_Cygwin_mount_table, file, "");
+    newXSproto("Cygwin::mount_flags", XS_Cygwin_mount_flags, file, "$");
     newXSproto("Cygwin::is_binmount", XS_Cygwin_is_binmount, file, "$");
-    newXSproto("Cygwin::is_textmount", XS_Cygwin_is_textmount, file, "$");
 
     /* Initialize Win32CORE if it has been statically linked. */
     handle = dlopen(NULL, RTLD_LAZY);