Tease apart the keyword/subroutine test in S_checkcomma.
[p5sagit/p5-mst-13.2.git] / vms / vms.c
index b2c47d9..82169ad 100644 (file)
--- a/vms/vms.c
+++ b/vms/vms.c
  * storage is put on the stack need to be changed to use
  * New()/SafeFree() instead.
  */
-#define VMS_MAXRSS NAM$C_MAXRSS
 #ifndef __VAX
-#if 0
+#ifndef VMS_MAXRSS
 #ifdef NAML$C_MAXRSS
-#undef VMS_MAXRSS
-#define VMS_MAXRSS NAML$C_MAXRSS
+#define VMS_MAXRSS (NAML$C_MAXRSS+1)
+#ifndef VMS_LONGNAME_SUPPORT
+#define VMS_LONGNAME_SUPPORT 1
+#endif /* VMS_LONGNAME_SUPPORT */
+#endif /* NAML$C_MAXRSS */
+#endif /* VMS_MAXRSS */
 #endif
+
+/* temporary hack until support is complete */
+#ifdef VMS_LONGNAME_SUPPORT
+#undef VMS_LONGNAME_SUPPORT
+#undef VMS_MAXRSS
 #endif
+/* end of temporary hack until support is complete */
+
+#ifndef VMS_MAXRSS
+#define VMS_MAXRSS (NAM$C_MAXRSS + 1)
 #endif
 
 #if  __CRTL_VER < 70301000 && __CRTL_VER >= 70300000
@@ -110,7 +122,7 @@ return 0;
 /* We implement I/O here, so we will be mixing PerlIO and stdio calls. */
 #define PERLIO_NOT_STDIO 0 
 
-/* Don't replace system definitions of vfork, getenv, and stat, 
+/* Don't replace system definitions of vfork, getenv, lstat, and stat, 
  * code below needs to get to the underlying CRTL routines. */
 #define DONT_MASK_RTL_CALLS
 #include "EXTERN.h"
@@ -155,6 +167,18 @@ struct itmlst_3 {
   void *bufadr;
   unsigned short int *retlen;
 };
+
+struct filescan_itmlst_2 {
+    unsigned short length;
+    unsigned short itmcode;
+    char * component;
+};
+
+struct vs_str_st {
+    unsigned short length;
+    char str[65536];
+};
+
 #ifdef __DECC
 #pragma message restore
 #pragma member_alignment restore
@@ -186,8 +210,14 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts);
  */
 #define PERL_LNM_MAX_ITER 10
 
-#define MAX_DCL_SYMBOL              255     /* well, what *we* can set, at least*/
-#define MAX_DCL_LINE_LENGTH        (4*MAX_DCL_SYMBOL-4)
+  /* New values with 7.3-2*/ /* why does max DCL have 4 byte subtracted from it? */
+#if __CRTL_VER >= 70302000 && !defined(__VAX)
+#define MAX_DCL_SYMBOL         (8192)
+#define MAX_DCL_LINE_LENGTH    (4096 - 4)
+#else
+#define MAX_DCL_SYMBOL         (1024)
+#define MAX_DCL_LINE_LENGTH    (1024 - 4)
+#endif
 
 static char *__mystrtolower(char *str)
 {
@@ -226,6 +256,14 @@ int decc_posix_compliant_pathnames = 0;
 int decc_readdir_dropdotnotype = 0;
 static int vms_process_case_tolerant = 1;
 
+/* bug workarounds if needed */
+int decc_bug_readdir_efs1 = 0;
+int decc_bug_devnull = 1;
+int decc_bug_fgetname = 0;
+int decc_dir_barename = 0;
+
+static int vms_debug_on_exception = 0;
+
 /* Is this a UNIX file specification?
  *   No longer a simple check with EFS file specs
  *   For now, not a full check, but need to
@@ -234,7 +272,7 @@ static int vms_process_case_tolerant = 1;
  *   changes to many other conversion routines.
  */
 
-static is_unix_filespec(const char *path)
+static int is_unix_filespec(const char *path)
 {
 int ret_val;
 const char * pch1;
@@ -256,6 +294,254 @@ const char * pch1;
     return ret_val;
 }
 
+/* This handles the expansion of a '^' prefix to the proper character
+ * in a UNIX file specification.
+ *
+ * The output count variable contains the number of characters added
+ * to the output string.
+ *
+ * The return value is the number of characters read from the input
+ * string
+ */
+static int copy_expand_vms_filename_escape
+  (char *outspec, const char *inspec, int *output_cnt)
+{
+int count;
+int scnt;
+
+    count = 0;
+    *output_cnt = 0;
+    if (*inspec == '^') {
+       inspec++;
+       switch (*inspec) {
+       case '.':
+           /* Non trailing dots should just be passed through */
+           *outspec = *inspec;
+           count++;
+           (*output_cnt)++;
+           break;
+       case '_': /* space */
+           *outspec = ' ';
+           inspec++;
+           count++;
+           (*output_cnt)++;
+           break;
+       case 'U': /* Unicode */
+           inspec++;
+           count++;
+           scnt = strspn(inspec, "0123456789ABCDEFabcdef");
+           if (scnt == 4) {
+               unsigned int c1, c2;
+               scnt = sscanf(inspec, "%2x%2x", &c1, &c2);
+               outspec[0] == c1 & 0xff;
+               outspec[1] == c2 & 0xff;
+               if (scnt > 1) {
+                   (*output_cnt) += 2;
+                   count += 4;
+               }
+           }
+           else {
+               /* Error - do best we can to continue */
+               *outspec = 'U';
+               outspec++;
+               (*output_cnt++);
+               *outspec = *inspec;
+               count++;
+               (*output_cnt++);
+           }
+           break;
+       default:
+           scnt = strspn(inspec, "0123456789ABCDEFabcdef");
+           if (scnt == 2) {
+               /* Hex encoded */
+               unsigned int c1;
+               scnt = sscanf(inspec, "%2x", &c1);
+               outspec[0] = c1 & 0xff;
+               if (scnt > 0) {
+                   (*output_cnt++);
+                   count += 2;
+               }
+           }
+           else {
+               *outspec = *inspec;
+               count++;
+               (*output_cnt++);
+           }
+       }
+    }
+    else {
+       *outspec = *inspec;
+       count++;
+       (*output_cnt)++;
+    }
+    return count;
+}
+
+
+int SYS$FILESCAN
+   (const struct dsc$descriptor_s * srcstr,
+    struct filescan_itmlst_2 * valuelist,
+    unsigned long * fldflags,
+    struct dsc$descriptor_s *auxout,
+    unsigned short * retlen);
+
+/* vms_split_path - Verify that the input file specification is a
+ * VMS format file specification, and provide pointers to the components of
+ * it.  With EFS format filenames, this is virtually the only way to
+ * parse a VMS path specification into components.
+ *
+ * If the sum of the components do not add up to the length of the
+ * string, then the passed file specification is probably a UNIX style
+ * path.
+ */
+static int vms_split_path
+   (pTHX_ const char * path,
+    char * * volume,
+    int * vol_len,
+    char * * root,
+    int * root_len,
+    char * * dir,
+    int * dir_len,
+    char * * name,
+    int * name_len,
+    char * * ext,
+    int * ext_len,
+    char * * version,
+    int * ver_len)
+{
+struct dsc$descriptor path_desc;
+int status;
+unsigned long flags;
+int ret_stat;
+struct filescan_itmlst_2 item_list[9];
+const int filespec = 0;
+const int nodespec = 1;
+const int devspec = 2;
+const int rootspec = 3;
+const int dirspec = 4;
+const int namespec = 5;
+const int typespec = 6;
+const int verspec = 7;
+
+    /* Assume the worst for an easy exit */
+    ret_stat = -1;
+    *volume = NULL;
+    *vol_len = 0;
+    *root = NULL;
+    *root_len = 0;
+    *dir = NULL;
+    *dir_len;
+    *name = NULL;
+    *name_len = 0;
+    *ext = NULL;
+    *ext_len = 0;
+    *version = NULL;
+    *ver_len = 0;
+
+    path_desc.dsc$a_pointer = (char *)path; /* cast ok */
+    path_desc.dsc$w_length = strlen(path);
+    path_desc.dsc$b_dtype = DSC$K_DTYPE_T;
+    path_desc.dsc$b_class = DSC$K_CLASS_S;
+
+    /* Get the total length, if it is shorter than the string passed
+     * then this was probably not a VMS formatted file specification
+     */
+    item_list[filespec].itmcode = FSCN$_FILESPEC;
+    item_list[filespec].length = 0;
+    item_list[filespec].component = NULL;
+
+    /* If the node is present, then it gets considered as part of the
+     * volume name to hopefully make things simple.
+     */
+    item_list[nodespec].itmcode = FSCN$_NODE;
+    item_list[nodespec].length = 0;
+    item_list[nodespec].component = NULL;
+
+    item_list[devspec].itmcode = FSCN$_DEVICE;
+    item_list[devspec].length = 0;
+    item_list[devspec].component = NULL;
+
+    /* root is a special case,  adding it to either the directory or
+     * the device components will probalby complicate things for the
+     * callers of this routine, so leave it separate.
+     */
+    item_list[rootspec].itmcode = FSCN$_ROOT;
+    item_list[rootspec].length = 0;
+    item_list[rootspec].component = NULL;
+
+    item_list[dirspec].itmcode = FSCN$_DIRECTORY;
+    item_list[dirspec].length = 0;
+    item_list[dirspec].component = NULL;
+
+    item_list[namespec].itmcode = FSCN$_NAME;
+    item_list[namespec].length = 0;
+    item_list[namespec].component = NULL;
+
+    item_list[typespec].itmcode = FSCN$_TYPE;
+    item_list[typespec].length = 0;
+    item_list[typespec].component = NULL;
+
+    item_list[verspec].itmcode = FSCN$_VERSION;
+    item_list[verspec].length = 0;
+    item_list[verspec].component = NULL;
+
+    item_list[8].itmcode = 0;
+    item_list[8].length = 0;
+    item_list[8].component = NULL;
+
+    status = SYS$FILESCAN
+       ((const struct dsc$descriptor_s *)&path_desc, item_list,
+       &flags, NULL, NULL);
+    _ckvmssts(status); /* All failure status values indicate a coding error */
+
+    /* If we parsed it successfully these two lengths should be the same */
+    if (path_desc.dsc$w_length != item_list[filespec].length)
+       return ret_stat;
+
+    /* If we got here, then it is a VMS file specification */
+    ret_stat = 0;
+
+    /* set the volume name */
+    if (item_list[nodespec].length > 0) {
+       *volume = item_list[nodespec].component;
+       *vol_len = item_list[nodespec].length + item_list[devspec].length;
+    }
+    else {
+       *volume = item_list[devspec].component;
+       *vol_len = item_list[devspec].length;
+    }
+
+    *root = item_list[rootspec].component;
+    *root_len = item_list[rootspec].length;
+
+    *dir = item_list[dirspec].component;
+    *dir_len = item_list[dirspec].length;
+
+    /* Now fun with versions and EFS file specifications
+     * The parser can not tell the difference when a "." is a version
+     * delimiter or a part of the file specification.
+     */
+    if ((decc_efs_charset) && 
+       (item_list[verspec].length > 0) &&
+       (item_list[verspec].component[0] == '.')) {
+       *name = item_list[namespec].component;
+       *name_len = item_list[namespec].length + item_list[typespec].length;
+       *ext = item_list[verspec].component;
+       *ext_len = item_list[verspec].length;
+       *version = NULL;
+       *ver_len = 0;
+    }
+    else {
+       *name = item_list[namespec].component;
+       *name_len = item_list[namespec].length;
+       *ext = item_list[typespec].component;
+       *ext_len = item_list[typespec].length;
+       *version = item_list[verspec].component;
+       *ver_len = item_list[verspec].length;
+    }
+    return ret_stat;
+}
+
 
 /* my_maxidx
  * Routine to retrieve the maximum equivalence index for an input
@@ -364,9 +650,9 @@ Perl_vmstrnenv(const char *lnm, char *eqv, unsigned long int idx,
           _ckvmssts(lib$sget1_dd(&deflen,&eqvdsc));
           retsts = lib$get_symbol(&lnmdsc,&eqvdsc,&eqvlen,0);
           if (retsts & 1) { 
-            if (eqvlen > 1024) {
+            if (eqvlen > MAX_DCL_SYMBOL) {
               set_errno(EVMSERR); set_vaxc_errno(LIB$_STRTRU);
-              eqvlen = 1024;
+              eqvlen = MAX_DCL_SYMBOL;
              /* Special hack--we might be called before the interpreter's */
              /* fully initialized, in which case either thr or PL_curcop */
              /* might be bogus. We have to check, since ckWARN needs them */
@@ -402,7 +688,7 @@ Perl_vmstrnenv(const char *lnm, char *eqv, unsigned long int idx,
                    (uplnm[4] == 'I' && !strcmp(uplnm,"SYS$INPUT"))   ||
                    (uplnm[4] == 'E' && !strcmp(uplnm,"SYS$ERROR"))   ||
                    (uplnm[4] == 'C' && !strcmp(uplnm,"SYS$COMMAND")) )  ) {
-              memcpy(eqv,eqv+4,eqvlen-4);
+              memmove(eqv,eqv+4,eqvlen-4);
               eqvlen -= 4;
             }
             cp2 += eqvlen;
@@ -488,7 +774,23 @@ Perl_my_getenv(pTHX_ const char *lnm, bool sys)
 
     for (cp1 = lnm, cp2 = eqv; *cp1; cp1++,cp2++) *cp2 = _toupper(*cp1);
     if (cp1 - lnm == 7 && !strncmp(eqv,"DEFAULT",7)) {
+      int len;
       getcwd(eqv,LNM$C_NAMLENGTH);
+
+      len = strlen(eqv);
+
+      /* Get rid of "000000/ in rooted filespecs */
+      if (len > 7) {
+        char * zeros;
+       zeros = strstr(eqv, "/000000/");
+       if (zeros != NULL) {
+         int mlen;
+         mlen = len - (zeros - eqv) - 7;
+         memmove(zeros, &zeros[7], mlen);
+         len = len - 7;
+         eqv[len] = '\0';
+       }
+      }
       return eqv;
     }
     else {
@@ -821,7 +1123,7 @@ prime_env_iter(void)
          * to indicate a zero-length value.  Get the actual value to make sure.
          */
         char lnm[LNM$C_NAMLENGTH+1];
-        char eqv[LNM$C_NAMLENGTH+1];
+        char eqv[MAX_DCL_SYMBOL+1];
         strncpy(lnm, key, keylen);
         int trnlen = vmstrnenv(lnm, eqv, 0, fildev, 0);
         sv = newSVpvn(eqv, strlen(eqv));
@@ -1049,7 +1351,7 @@ Perl_my_setenv(pTHX_ const char *lnm, const char *eqv)
         int i;
         for (i = 0; lnm[i]; i++) uplnm[i] = _toupper(lnm[i]);
         if (!strcmp(uplnm,"DEFAULT")) {
-          if (eqv && *eqv) chdir(eqv);
+          if (eqv && *eqv) my_chdir(eqv);
           return;
         }
     } 
@@ -1103,6 +1405,8 @@ Perl_vmssetuserlnm(pTHX_ const char *name, const char *eqv)
  * the case of its string arguments; in order to match the behavior
  * of LOGINOUT et al., alphabetic characters in both arguments must
  *  be upcased by the caller.
+ *
+ * - fix me to call ACM services when available
  */
 char *
 Perl_my_crypt(pTHX_ const char *textpasswd, const char *usrname)
@@ -1159,6 +1463,206 @@ static char *mp_do_rmsexpand(pTHX_ const char *, char *, int, const char *, unsi
 static char *mp_do_fileify_dirspec(pTHX_ const char *, char *, int);
 static char *mp_do_tovmsspec(pTHX_ const char *, char *, int);
 
+/* fixup barenames that are directories for internal use.
+ * There have been problems with the consistent handling of UNIX
+ * style directory names when routines are presented with a name that
+ * has no directory delimitors at all.  So this routine will eventually
+ * fix the issue.
+ */
+static char * fixup_bare_dirnames(const char * name)
+{
+  if (decc_disable_to_vms_logname_translation) {
+/* fix me */
+  }
+  return NULL;
+}
+
+/* mp_do_kill_file
+ * A little hack to get around a bug in some implemenation of remove()
+ * that do not know how to delete a directory
+ *
+ * Delete any file to which user has control access, regardless of whether
+ * delete access is explicitly allowed.
+ * Limitations: User must have write access to parent directory.
+ *              Does not block signals or ASTs; if interrupted in midstream
+ *              may leave file with an altered ACL.
+ * HANDLE WITH CARE!
+ */
+/*{{{int mp_do_kill_file(const char *name, int dirflag)*/
+static int
+mp_do_kill_file(pTHX_ const char *name, int dirflag)
+{
+    char *vmsname, *rspec;
+    char *remove_name;
+    unsigned long int jpicode = JPI$_UIC, type = ACL$C_FILE;
+    unsigned long int cxt = 0, aclsts, fndsts, rmsts = -1;
+    struct dsc$descriptor_s fildsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
+    struct myacedef {
+      unsigned char myace$b_length;
+      unsigned char myace$b_type;
+      unsigned short int myace$w_flags;
+      unsigned long int myace$l_access;
+      unsigned long int myace$l_ident;
+    } newace = { sizeof(struct myacedef), ACE$C_KEYID, 0,
+                 ACE$M_READ | ACE$M_WRITE | ACE$M_DELETE | ACE$M_CONTROL, 0},
+      oldace = { sizeof(struct myacedef), ACE$C_KEYID, 0, 0, 0};
+     struct itmlst_3
+       findlst[3] = {{sizeof oldace, ACL$C_FNDACLENT, &oldace, 0},
+                     {sizeof oldace, ACL$C_READACE,   &oldace, 0},{0,0,0,0}},
+       addlst[2] = {{sizeof newace, ACL$C_ADDACLENT, &newace, 0},{0,0,0,0}},
+       dellst[2] = {{sizeof newace, ACL$C_DELACLENT, &newace, 0},{0,0,0,0}},
+       lcklst[2] = {{sizeof newace, ACL$C_WLOCK_ACL, &newace, 0},{0,0,0,0}},
+       ulklst[2] = {{sizeof newace, ACL$C_UNLOCK_ACL, &newace, 0},{0,0,0,0}};
+
+    /* Expand the input spec using RMS, since the CRTL remove() and
+     * system services won't do this by themselves, so we may miss
+     * a file "hiding" behind a logical name or search list. */
+    vmsname = PerlMem_malloc(NAM$C_MAXRSS+1);
+    if (vmsname == NULL) _ckvmssts(SS$_INSFMEM);
+
+    if (do_rmsexpand(name, vmsname, 0, NULL, PERL_RMSEXPAND_M_VMS) == NULL) {
+      PerlMem_free(vmsname);
+      return -1;
+    }
+
+    if (decc_posix_compliant_pathnames) {
+      /* In POSIX mode, we prefer to remove the UNIX name */
+      rspec = vmsname;
+      remove_name = (char *)name;
+    }
+    else {
+      rspec = PerlMem_malloc(NAM$C_MAXRSS+1);
+      if (rspec == NULL) _ckvmssts(SS$_INSFMEM);
+      if (do_rmsexpand(vmsname, rspec, 0, NULL, PERL_RMSEXPAND_M_VMS) == NULL) {
+       PerlMem_free(rspec);
+        PerlMem_free(vmsname);
+       return -1;
+      }
+      PerlMem_free(vmsname);
+      remove_name = rspec;
+    }
+
+#if defined(__CRTL_VER) && __CRTL_VER >= 70000000
+    if (dirflag != 0) {
+       if (decc_dir_barename && decc_posix_compliant_pathnames) {
+         remove_name = PerlMem_malloc(NAM$C_MAXRSS+1);
+         if (remove_name == NULL) _ckvmssts(SS$_INSFMEM);
+
+         do_pathify_dirspec(name, remove_name, 0);
+         if (!rmdir(remove_name)) {
+
+           PerlMem_free(remove_name);
+           PerlMem_free(rspec);
+           return 0;   /* Can we just get rid of it? */
+         }
+       }
+        else {
+         if (!rmdir(remove_name)) {
+           PerlMem_free(rspec);
+           return 0;   /* Can we just get rid of it? */
+         }
+       }
+    }
+    else
+#endif
+      if (!remove(remove_name)) {
+       PerlMem_free(rspec);
+       return 0;   /* Can we just get rid of it? */
+      }
+
+    /* If not, can changing protections help? */
+    if (vaxc$errno != RMS$_PRV) {
+      PerlMem_free(rspec);
+      return -1;
+    }
+
+    /* No, so we get our own UIC to use as a rights identifier,
+     * and the insert an ACE at the head of the ACL which allows us
+     * to delete the file.
+     */
+    _ckvmssts(lib$getjpi(&jpicode,0,0,&(oldace.myace$l_ident),0,0));
+    fildsc.dsc$w_length = strlen(rspec);
+    fildsc.dsc$a_pointer = rspec;
+    cxt = 0;
+    newace.myace$l_ident = oldace.myace$l_ident;
+    if (!((aclsts = sys$change_acl(0,&type,&fildsc,lcklst,0,0,0)) & 1)) {
+      switch (aclsts) {
+        case RMS$_FNF: case RMS$_DNF: case SS$_NOSUCHOBJECT:
+          set_errno(ENOENT); break;
+        case RMS$_DIR:
+          set_errno(ENOTDIR); break;
+        case RMS$_DEV:
+          set_errno(ENODEV); break;
+        case RMS$_SYN: case SS$_INVFILFOROP:
+          set_errno(EINVAL); break;
+        case RMS$_PRV:
+          set_errno(EACCES); break;
+        default:
+          _ckvmssts(aclsts);
+      }
+      set_vaxc_errno(aclsts);
+      PerlMem_free(rspec);
+      return -1;
+    }
+    /* Grab any existing ACEs with this identifier in case we fail */
+    aclsts = fndsts = sys$change_acl(0,&type,&fildsc,findlst,0,0,&cxt);
+    if ( fndsts & 1 || fndsts == SS$_ACLEMPTY || fndsts == SS$_NOENTRY
+                    || fndsts == SS$_NOMOREACE ) {
+      /* Add the new ACE . . . */
+      if (!((aclsts = sys$change_acl(0,&type,&fildsc,addlst,0,0,0)) & 1))
+        goto yourroom;
+
+#if defined(__CRTL_VER) && __CRTL_VER >= 70000000
+      if (dirflag != 0)
+       if (decc_dir_barename && decc_posix_compliant_pathnames) {
+         remove_name = PerlMem_malloc(NAM$C_MAXRSS+1);
+         if (remove_name == NULL) _ckvmssts(SS$_INSFMEM);
+
+         do_pathify_dirspec(name, remove_name, 0);
+         rmsts = rmdir(remove_name);
+         PerlMem_free(remove_name);
+       }
+       else {
+       rmsts = rmdir(remove_name);
+       }
+      else
+#endif
+        rmsts = remove(remove_name);
+      if (rmsts) {
+        /* We blew it - dir with files in it, no write priv for
+         * parent directory, etc.  Put things back the way they were. */
+        if (!((aclsts = sys$change_acl(0,&type,&fildsc,dellst,0,0,0)) & 1))
+          goto yourroom;
+        if (fndsts & 1) {
+          addlst[0].bufadr = &oldace;
+          if (!((aclsts = sys$change_acl(0,&type,&fildsc,addlst,0,0,&cxt)) & 1))
+            goto yourroom;
+        }
+      }
+    }
+
+    yourroom:
+    fndsts = sys$change_acl(0,&type,&fildsc,ulklst,0,0,0);
+    /* We just deleted it, so of course it's not there.  Some versions of
+     * VMS seem to return success on the unlock operation anyhow (after all
+     * the unlock is successful), but others don't.
+     */
+    if (fndsts == RMS$_FNF || fndsts == SS$_NOSUCHOBJECT) fndsts = SS$_NORMAL;
+    if (aclsts & 1) aclsts = fndsts;
+    if (!(aclsts & 1)) {
+      set_errno(EVMSERR);
+      set_vaxc_errno(aclsts);
+      PerlMem_free(rspec);
+      return -1;
+    }
+
+    PerlMem_free(rspec);
+    return rmsts;
+
+}  /* end of kill_file() */
+/*}}}*/
+
+
 /*{{{int do_rmdir(char *name)*/
 int
 Perl_do_rmdir(pTHX_ const char *name)
@@ -1169,7 +1673,7 @@ Perl_do_rmdir(pTHX_ const char *name)
 
     if (do_fileify_dirspec(name,dirfile,0) == NULL) return -1;
     if (flex_stat(dirfile,&st) || !S_ISDIR(st.st_mode)) retval = -1;
-    else retval = kill_file(dirfile);
+    else retval = mp_do_kill_file(aTHX_ dirfile, 1);
     return retval;
 
 }  /* end of do_rmdir */
@@ -1187,7 +1691,8 @@ Perl_do_rmdir(pTHX_ const char *name)
 int
 Perl_kill_file(pTHX_ const char *name)
 {
-    char vmsname[NAM$C_MAXRSS+1], rspec[NAM$C_MAXRSS+1];
+    char rspec[NAM$C_MAXRSS+1];
+    char *tspec;
     unsigned long int jpicode = JPI$_UIC, type = ACL$C_FILE;
     unsigned long int cxt = 0, aclsts, fndsts, rmsts = -1;
     struct dsc$descriptor_s fildsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
@@ -1211,8 +1716,8 @@ Perl_kill_file(pTHX_ const char *name)
     /* Expand the input spec using RMS, since the CRTL remove() and
      * system services won't do this by themselves, so we may miss
      * a file "hiding" behind a logical name or search list. */
-    if (do_tovmsspec(name,vmsname,0) == NULL) return -1;
-    if (do_rmsexpand(vmsname,rspec,1,NULL,0) == NULL) return -1;
+    tspec = do_rmsexpand(name, rspec, 0, NULL, PERL_RMSEXPAND_M_VMS);
+    if (tspec == NULL) return -1;
     if (!remove(rspec)) return 0;   /* Can we just get rid of it? */
     /* If not, can changing protections help? */
     if (vaxc$errno != RMS$_PRV) return -1;
@@ -1335,12 +1840,12 @@ Perl_my_chdir(pTHX_ const char *dir)
    * - Preview- '/' will be valid soon on VMS
    */
   if ((dirlen > 1) && (dir1[dirlen-1] == '/')) {
-    char *newdir = savepvn(dir,dirlen-1);
+    char *newdir = savepvn(dir1,dirlen-1);
     int ret = chdir(newdir);
     Safefree(newdir);
     return ret;
   }
-  else return chdir(dir);
+  else return chdir(dir1);
 }  /* end of my_chdir */
 /*}}}*/
 
@@ -1354,12 +1859,17 @@ my_tmpfile(void)
 
   if ((fp = tmpfile())) return fp;
 
-  Newx(cp,L_tmpnam+24,char);
-  strcpy(cp,"Sys$Scratch:");
+  cp = PerlMem_malloc(L_tmpnam+24);
+  if (cp == NULL) _ckvmssts_noperl(SS$_INSFMEM);
+
+  if (decc_filename_unix_only == 0)
+    strcpy(cp,"Sys$Scratch:");
+  else
+    strcpy(cp,"/tmp/");
   tmpnam(cp+strlen(cp));
   strcat(cp,".Perltmp");
   fp = fopen(cp,"w+","fop=dlt");
-  Safefree(cp);
+  PerlMem_free(cp);
   return fp;
 }
 /*}}}*/
@@ -1422,8 +1932,8 @@ Perl_my_sigaction (pTHX_ int sig, const struct sigaction* act,
 
 #define _MY_SIG_MAX 17
 
-unsigned int
-Perl_sig_to_vmscondition(int sig)
+static unsigned int
+Perl_sig_to_vmscondition_int(int sig)
 {
     static unsigned int sig_code[_MY_SIG_MAX+1] = 
     {
@@ -1465,6 +1975,17 @@ Perl_sig_to_vmscondition(int sig)
     return sig_code[sig];
 }
 
+unsigned int
+Perl_sig_to_vmscondition(int sig)
+{
+#ifdef SS$_DEBUG
+    if (vms_debug_on_exception != 0)
+       lib$signal(SS$_DEBUG);
+#endif
+    return Perl_sig_to_vmscondition_int(sig);
+}
+
+
 int
 Perl_my_kill(int pid, int sig)
 {
@@ -1500,7 +2021,7 @@ Perl_my_kill(int pid, int sig)
        return -1;
     }
 
-    code = Perl_sig_to_vmscondition(sig);
+    code = Perl_sig_to_vmscondition_int(sig);
 
     if (!code) {
        SETERRNO(EINVAL, SS$_BADPARAM);
@@ -1583,7 +2104,7 @@ int unix_status;
   fac_sp = vms_status & STS$M_FAC_SP;
   msg_no = vms_status & (STS$M_MSG_NO | STS$M_SEVERITY);
 
-  if ((facility == 0) || (fac_sp == 0)  && (child_flag == 0)) {
+  if (((facility == 0) || (fac_sp == 0))  && (child_flag == 0)) {
     switch(msg_no) {
     case SS$_NORMAL:
        unix_status = 0;
@@ -1627,7 +2148,9 @@ int unix_status;
     case SS$_NOSUCHOBJECT:
        unix_status = ENOENT;
        break;
-    case SS$_ABORT:
+    case SS$_ABORT:                                /* Fatal case */
+    case ((SS$_ABORT & STS$M_COND_ID) | STS$K_ERROR): /* Error case */
+    case ((SS$_ABORT & STS$M_COND_ID) | STS$K_WARNING): /* Warning case */
        unix_status = EINTR;
        break;
     case SS$_BUFFEROVF:
@@ -1785,7 +2308,7 @@ int test_unix_status;
     case EACCES: return SS$_FILACCERR;
     case EFAULT: return SS$_ACCVIO;
     /* case ENOTBLK */
-    case EBUSY: SS$_DEVOFFLINE;
+    case EBUSY: return SS$_DEVOFFLINE;
     case EEXIST: return RMS$_FEX;
     /* case EXDEV */
     case ENODEV: return SS$_NOSUCHDEV;
@@ -2052,14 +2575,14 @@ pipe_exit_routine(pTHX)
 
     while (info) {
       int need_eof;
-      _ckvmssts(sys$setast(0));
+      _ckvmssts_noperl(sys$setast(0));
       if (info->in && !info->in->shut_on_empty) {
-        _ckvmssts(sys$qio(0,info->in->chan_in,IO$_WRITEOF,0,0,0,
+        _ckvmssts_noperl(sys$qio(0,info->in->chan_in,IO$_WRITEOF,0,0,0,
                           0, 0, 0, 0, 0, 0));
         info->waiting = 1;
         did_stuff = 1;
       }
-      _ckvmssts(sys$setast(1));
+      _ckvmssts_noperl(sys$setast(1));
       info = info->next;
     }
 
@@ -2070,11 +2593,11 @@ pipe_exit_routine(pTHX)
 
         info = open_pipes;
         while (info) {
-          _ckvmssts(sys$setast(0));
+          _ckvmssts_noperl(sys$setast(0));
           if (info->waiting && info->done) 
                 info->waiting = 0;
           nwait += info->waiting;
-          _ckvmssts(sys$setast(1));
+          _ckvmssts_noperl(sys$setast(1));
           info = info->next;
         }
         if (!nwait) break;
@@ -2084,13 +2607,13 @@ pipe_exit_routine(pTHX)
     did_stuff = 0;
     info = open_pipes;
     while (info) {
-      _ckvmssts(sys$setast(0));
+      _ckvmssts_noperl(sys$setast(0));
       if (!info->done) { /* Tap them gently on the shoulder . . .*/
         sts = sys$forcex(&info->pid,0,&abort);
-        if (!(sts&1) && sts != SS$_NONEXPR) _ckvmssts(sts); 
+        if (!(sts&1) && sts != SS$_NONEXPR) _ckvmssts_noperl(sts); 
         did_stuff = 1;
       }
-      _ckvmssts(sys$setast(1));
+      _ckvmssts_noperl(sys$setast(1));
       info = info->next;
     }
 
@@ -2101,11 +2624,11 @@ pipe_exit_routine(pTHX)
 
         info = open_pipes;
         while (info) {
-          _ckvmssts(sys$setast(0));
+          _ckvmssts_noperl(sys$setast(0));
           if (info->waiting && info->done) 
                 info->waiting = 0;
           nwait += info->waiting;
-          _ckvmssts(sys$setast(1));
+          _ckvmssts_noperl(sys$setast(1));
           info = info->next;
         }
         if (!nwait) break;
@@ -2114,12 +2637,12 @@ pipe_exit_routine(pTHX)
 
     info = open_pipes;
     while (info) {
-      _ckvmssts(sys$setast(0));
+      _ckvmssts_noperl(sys$setast(0));
       if (!info->done) {  /* We tried to be nice . . . */
         sts = sys$delprc(&info->pid,0);
-        if (!(sts&1) && sts != SS$_NONEXPR) _ckvmssts(sts); 
+        if (!(sts&1) && sts != SS$_NONEXPR) _ckvmssts_noperl(sts); 
       }
-      _ckvmssts(sys$setast(1));
+      _ckvmssts_noperl(sys$setast(1));
       info = info->next;
     }
 
@@ -2253,7 +2776,7 @@ popen_translate(pTHX_ char *logical, char *result)
 */
     ifi  = 0;
     if (result[0] == 0x1b && result[1] == 0x00) {
-        memcpy(&ifi,result+2,2);
+        memmove(&ifi,result+2,2);
         strcpy(result,result+4);
     }
     return ifi;     /* this is the RMS internal file id */
@@ -2285,7 +2808,8 @@ pipe_tochild_setup(pTHX_ char *rmbx, char *wmbx)
     unsigned int dviitm = DVI$_DEVBUFSIZ;
     int j, n;
 
-    Newx(p, 1, Pipe);
+    n = sizeof(Pipe);
+    _ckvmssts(lib$get_vm(&n, &p));
 
     create_mbx(aTHX_ &p->chan_in , &d_mbx1);
     create_mbx(aTHX_ &p->chan_out, &d_mbx2);
@@ -2455,12 +2979,14 @@ pipe_infromchild_setup(pTHX_ char *rmbx, char *wmbx)
                                       DSC$K_CLASS_S, mbx2};
     unsigned int dviitm = DVI$_DEVBUFSIZ;
 
-    Newx(p, 1, Pipe);
+    int n = sizeof(Pipe);
+    _ckvmssts(lib$get_vm(&n, &p));
     create_mbx(aTHX_ &p->chan_in , &d_mbx1);
     create_mbx(aTHX_ &p->chan_out, &d_mbx2);
 
     _ckvmssts(lib$getdvi(&dviitm, &p->chan_in, 0, &p->bufsize));
-    Newx(p->buf, p->bufsize, char);
+    n = p->bufsize * sizeof(char);
+    _ckvmssts(lib$get_vm(&n, &p->buf));
     p->shut_on_empty = FALSE;
     p->info   = 0;
     p->type   = 0;
@@ -2564,6 +3090,7 @@ pipe_mbxtofd_setup(pTHX_ int fd, char *out)
     struct stat s;
     struct dsc$descriptor_s d_mbx = {sizeof mbx, DSC$K_DTYPE_T,
                                       DSC$K_CLASS_S, mbx};
+    int n = sizeof(Pipe);
 
     /* things like terminals and mbx's don't need this filter */
     if (fd && fstat(fd,&s) == 0) {
@@ -2578,11 +3105,12 @@ pipe_mbxtofd_setup(pTHX_ int fd, char *out)
         }
     }
 
-    Newx(p, 1, Pipe);
+    _ckvmssts(lib$get_vm(&n, &p));
     p->fd_out = dup(fd);
     create_mbx(aTHX_ &p->chan_in, &d_mbx);
     _ckvmssts(lib$getdvi(&dviitm, &p->chan_in, 0, &p->bufsize));
-    Newx(p->buf, p->bufsize+1, char);
+    n = (p->bufsize+1) * sizeof(char);
+    _ckvmssts(lib$get_vm(&n, &p->buf));
     p->shut_on_empty = FALSE;
     p->retry = 0;
     p->info  = 0;
@@ -2658,7 +3186,7 @@ free_pipelocs(pTHX_ void *head)
     p = *pHead;
     while (p) {
         pnext = p->next;
-        Safefree(p);
+        PerlMem_free(p);
         p = pnext;
     }
     *pHead = 0;
@@ -2682,13 +3210,17 @@ store_pipelocs(pTHX)
 
 /*  the . directory from @INC comes last */
 
-    Newx(p,1,PLOC);
+    p = (pPLOC) PerlMem_malloc(sizeof(PLOC));
+    if (p == NULL) _ckvmssts(SS$_INSFMEM);
     p->next = head_PLOC;
     head_PLOC = p;
     strcpy(p->dir,"./");
 
 /*  get the directory from $^X */
 
+    unixdir = PerlMem_malloc(VMS_MAXRSS);
+    if (unixdir == NULL) _ckvmssts(SS$_INSFMEM);
+
 #ifdef PERL_IMPLICIT_CONTEXT
     if (aTHX && PL_origargv && PL_origargv[0]) {    /* maybe nul if embedded Perl */
 #else
@@ -2696,15 +3228,29 @@ store_pipelocs(pTHX)
 #endif
         strcpy(temp, PL_origargv[0]);
         x = strrchr(temp,']');
-        if (x) x[1] = '\0';
+       if (x == NULL) {
+       x = strrchr(temp,'>');
+         if (x == NULL) {
+           /* It could be a UNIX path */
+           x = strrchr(temp,'/');
+         }
+       }
+       if (x)
+         x[1] = '\0';
+       else {
+         /* Got a bare name, so use default directory */
+         temp[0] = '.';
+         temp[1] = '\0';
+       }
 
-        if ((unixdir = tounixpath(temp, Nullch)) != Nullch) {
-            Newx(p,1,PLOC);
+        if ((tounixpath(temp, unixdir)) != Nullch) {
+            p = (pPLOC) PerlMem_malloc(sizeof(PLOC));
+           if (p == NULL) _ckvmssts(SS$_INSFMEM);
             p->next = head_PLOC;
             head_PLOC = p;
             strncpy(p->dir,unixdir,sizeof(p->dir)-1);
             p->dir[NAM$C_MAXRSS] = '\0';
-        }
+       }
     }
 
 /*  reverse order of @INC entries, skip "." since entered above */
@@ -2720,10 +3266,10 @@ store_pipelocs(pTHX)
         if (SvROK(dirsv)) continue;
         dir = SvPVx(dirsv,n_a);
         if (strcmp(dir,".") == 0) continue;
-        if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
+        if ((tounixpath(dir, unixdir)) == Nullch)
             continue;
 
-        Newx(p,1,PLOC);
+        p = (pPLOC) PerlMem_malloc(sizeof(PLOC));
         p->next = head_PLOC;
         head_PLOC = p;
         strncpy(p->dir,unixdir,sizeof(p->dir)-1);
@@ -2733,14 +3279,16 @@ store_pipelocs(pTHX)
 /* most likely spot (ARCHLIB) put first in the list */
 
 #ifdef ARCHLIB_EXP
-    if ((unixdir = tounixpath(ARCHLIB_EXP, Nullch)) != Nullch) {
-        Newx(p,1,PLOC);
+    if ((tounixpath(ARCHLIB_EXP, unixdir)) != Nullch) {
+        p = (pPLOC) PerlMem_malloc(sizeof(PLOC));
+       if (p == NULL) _ckvmssts(SS$_INSFMEM);
         p->next = head_PLOC;
         head_PLOC = p;
         strncpy(p->dir,unixdir,sizeof(p->dir)-1);
         p->dir[NAM$C_MAXRSS] = '\0';
     }
 #endif
+    PerlMem_free(unixdir);
 }
 
 
@@ -2767,12 +3315,15 @@ find_vmspipe(pTHX)
         pPLOC  p = head_PLOC;
 
         while (p) {
+           char * exp_res;
             strcpy(file, p->dir);
             strncat(file, "vmspipe.com",NAM$C_MAXRSS);
             file[NAM$C_MAXRSS] = '\0';
             p = p->next;
 
-            if (!do_tovmsspec(file,vmspipe_file,0)) continue;
+            exp_res = do_rmsexpand
+               (file, vmspipe_file, 0, NULL, PERL_RMSEXPAND_M_VMS);
+            if (!exp_res) continue;
 
             if (cando_by_name(S_IRUSR, 0, vmspipe_file)
              && cando_by_name(S_IXUSR, 0, vmspipe_file)) {
@@ -2792,7 +3343,8 @@ vmspipe_tempfile(pTHX)
     char file[NAM$C_MAXRSS+1];
     FILE *fp;
     static int index = 0;
-    stat_t s0, s1;
+    Stat_t s0, s1;
+    int cmp_result;
 
     /* create a tempfile */
 
@@ -2807,15 +3359,29 @@ vmspipe_tempfile(pTHX)
     */
 
     index++;
-    sprintf(file,"sys$scratch:perlpipe_%08.8x_%d.com",mypid,index);
-    fp = fopen(file,"w");
-    if (!fp) {
+    if (!decc_filename_unix_only) {
+      sprintf(file,"sys$scratch:perlpipe_%08.8x_%d.com",mypid,index);
+      fp = fopen(file,"w");
+      if (!fp) {
         sprintf(file,"sys$login:perlpipe_%08.8x_%d.com",mypid,index);
         fp = fopen(file,"w");
         if (!fp) {
             sprintf(file,"sys$disk:[]perlpipe_%08.8x_%d.com",mypid,index);
             fp = fopen(file,"w");
-        }
+       }
+      }
+     }
+     else {
+      sprintf(file,"/tmp/perlpipe_%08.8x_%d.com",mypid,index);
+      fp = fopen(file,"w");
+      if (!fp) {
+       sprintf(file,"/sys$login/perlpipe_%08.8x_%d.com",mypid,index);
+       fp = fopen(file,"w");
+       if (!fp) {
+         sprintf(file,"./perlpipe_%08.8x_%d.com",mypid,index);
+         fp = fopen(file,"w");
+       }
+      }
     }
     if (!fp) return 0;  /* we're hosed */
 
@@ -2845,17 +3411,17 @@ vmspipe_tempfile(pTHX)
     fsync(fileno(fp));
 
     fgetname(fp, file, 1);
-    fstat(fileno(fp), &s0);
+    fstat(fileno(fp), (struct stat *)&s0);
     fclose(fp);
 
+    if (decc_filename_unix_only)
+       do_tounixspec(file, file, 0);
     fp = fopen(file,"r","shr=get");
     if (!fp) return 0;
-    fstat(fileno(fp), &s1);
+    fstat(fileno(fp), (struct stat *)&s1);
 
-    if (s0.st_ino[0] != s1.st_ino[0] ||
-        s0.st_ino[1] != s1.st_ino[1] ||
-        s0.st_ino[2] != s1.st_ino[2] ||
-        s0.st_ctime  != s1.st_ctime  )  {
+    cmp_result = VMS_INO_T_COMPARE(s0.crtl_stat.st_ino, s1.crtl_stat.st_ino);
+    if ((cmp_result != 0) && (s0.st_ctime != s1.st_ctime))  {
         fclose(fp);
         return 0;
     }
@@ -2875,12 +3441,12 @@ safe_popen(pTHX_ const char *cmd, const char *in_mode, int *psts)
      * environment.  Hence we've switched to LOCAL symbol table.
      */
     unsigned int table = LIB$K_CLI_LOCAL_SYM;
-    int j, wait = 0;
+    int j, wait = 0, n;
     char *p, mode[10], symbol[MAX_DCL_SYMBOL+1], *vmspipe;
     char in[512], out[512], err[512], mbx[512];
     FILE *tpipe = 0;
     char tfilebuf[NAM$C_MAXRSS+1];
-    pInfo info;
+    pInfo info = NULL;
     char cmd_sym_name[20];
     struct dsc$descriptor_s d_symbol= {0, DSC$K_DTYPE_T,
                                       DSC$K_CLASS_S, symbol};
@@ -2967,7 +3533,8 @@ safe_popen(pTHX_ const char *cmd, const char *in_mode, int *psts)
       *psts = sts;
       return Nullfp; 
     }
-    Newx(info,1,Info);
+    n = sizeof(Info);
+    _ckvmssts(lib$get_vm(&n, &info));
         
     strcpy(mode,in_mode);
     info->mode = *mode;
@@ -3021,9 +3588,14 @@ safe_popen(pTHX_ const char *cmd, const char *in_mode, int *psts)
                 if (!done) _ckvmssts(sys$waitfr(pipe_ef));
             }
 
-            if (info->out->buf) Safefree(info->out->buf);
-            Safefree(info->out);
-            Safefree(info);
+            if (info->out->buf) {
+                n = info->out->bufsize * sizeof(char);
+                _ckvmssts(lib$free_vm(&n, &info->out->buf));
+            }
+            n = sizeof(Pipe);
+            _ckvmssts(lib$free_vm(&n, &info->out));
+            n = sizeof(Info);
+            _ckvmssts(lib$free_vm(&n, &info));
             *psts = RMS$_FNF;
             return Nullfp;
         }
@@ -3053,7 +3625,7 @@ safe_popen(pTHX_ const char *cmd, const char *in_mode, int *psts)
 
         info->in = pipe_tochild_setup(aTHX_ in,mbx);
         if (!info->useFILE) {
-        info->fp  = PerlIO_open(mbx, mode);
+           info->fp  = PerlIO_open(mbx, mode);
         } else {
             info->fp = (PerlIO *) freopen(mbx, mode, stdout);
             Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT",mbx);
@@ -3080,9 +3652,14 @@ safe_popen(pTHX_ const char *cmd, const char *in_mode, int *psts)
                 if (!done) _ckvmssts(sys$waitfr(pipe_ef));
             }
 
-            if (info->in->buf) Safefree(info->in->buf);
-            Safefree(info->in);
-            Safefree(info);
+            if (info->in->buf) {
+                n = info->in->bufsize * sizeof(char);
+                _ckvmssts(lib$free_vm(&n, &info->in->buf));
+            }
+            n = sizeof(Pipe);
+            _ckvmssts(lib$free_vm(&n, &info->in));
+            n = sizeof(Info);
+            _ckvmssts(lib$free_vm(&n, &info));
             *psts = RMS$_FNF;
             return Nullfp;
         }
@@ -3119,9 +3696,6 @@ safe_popen(pTHX_ const char *cmd, const char *in_mode, int *psts)
     _ckvmssts(lib$set_symbol(&d_sym_out, &d_symbol, &table));
 
     p = vmscmd->dsc$a_pointer;
-    while (*p && *p != '\n') p++;
-    *p = '\0';                                  /* truncate on \n */
-    p = vmscmd->dsc$a_pointer;
     while (*p == ' ' || *p == '\t') p++;        /* remove leading whitespace */
     if (*p == '$') p++;                         /* remove leading $ */
     while (*p == ' ' || *p == '\t') p++;
@@ -3212,7 +3786,7 @@ I32 Perl_my_pclose(pTHX_ PerlIO *fp)
 {
     pInfo info, last = NULL;
     unsigned long int retsts;
-    int done, iss;
+    int done, iss, n;
     
     for (info = open_pipes; info != NULL; last = info, info = info->next)
         if (info->fp == fp) break;
@@ -3232,7 +3806,7 @@ I32 Perl_my_pclose(pTHX_ PerlIO *fp)
      */
      if (info->fp) {
         if (!info->useFILE) 
-     PerlIO_flush(info->fp);   /* first, flush data */
+            PerlIO_flush(info->fp);   /* first, flush data */
         else 
             fflush((FILE *)info->fp);
     }
@@ -3255,7 +3829,7 @@ I32 Perl_my_pclose(pTHX_ PerlIO *fp)
     _ckvmssts(sys$setast(1));
     if (info->fp) {
      if (!info->useFILE) 
-    PerlIO_close(info->fp);
+        PerlIO_close(info->fp);
      else 
         fclose((FILE *)info->fp);
     }
@@ -3283,18 +3857,31 @@ I32 Perl_my_pclose(pTHX_ PerlIO *fp)
     /* free buffers and structures */
 
     if (info->in) {
-        if (info->in->buf) Safefree(info->in->buf);
-        Safefree(info->in);
+        if (info->in->buf) {
+            n = info->in->bufsize * sizeof(char);
+            _ckvmssts(lib$free_vm(&n, &info->in->buf));
+        }
+        n = sizeof(Pipe);
+        _ckvmssts(lib$free_vm(&n, &info->in));
     }
     if (info->out) {
-        if (info->out->buf) Safefree(info->out->buf);
-        Safefree(info->out);
+        if (info->out->buf) {
+            n = info->out->bufsize * sizeof(char);
+            _ckvmssts(lib$free_vm(&n, &info->out->buf));
+        }
+        n = sizeof(Pipe);
+        _ckvmssts(lib$free_vm(&n, &info->out));
     }
     if (info->err) {
-        if (info->err->buf) Safefree(info->err->buf);
-        Safefree(info->err);
+        if (info->err->buf) {
+            n = info->err->bufsize * sizeof(char);
+            _ckvmssts(lib$free_vm(&n, &info->err->buf));
+        }
+        n = sizeof(Pipe);
+        _ckvmssts(lib$free_vm(&n, &info->err));
     }
-    Safefree(info);
+    n = sizeof(Info);
+    _ckvmssts(lib$free_vm(&n, &info));
 
     return retsts;
 
@@ -3465,21 +4052,125 @@ my_gconvert(double val, int ndig, int trail, char *buf)
 }
 /*}}}*/
 
+#if 1 /* defined(__VAX) || !defined(NAML$C_MAXRSS) */
+static int rms_free_search_context(struct FAB * fab)
+{
+struct NAM * nam;
 
-/*{{{char *do_rmsexpand(char *fspec, char *out, int ts, char *def, unsigned opts)*/
-/* Shortcut for common case of simple calls to $PARSE and $SEARCH
- * to expand file specification.  Allows for a single default file
- * specification and a simple mask of options.  If outbuf is non-NULL,
- * it must point to a buffer at least NAM$C_MAXRSS bytes long, into which
- * the resultant file specification is placed.  If outbuf is NULL, the
- * resultant file specification is placed into a static buffer.
- * The third argument, if non-NULL, is taken to be a default file
- * specification string.  The fourth argument is unused at present.
- * rmesexpand() returns the address of the resultant string if
+    nam = fab->fab$l_nam;
+    nam->nam$b_nop |= NAM$M_SYNCHK;
+    nam->nam$l_rlf = NULL;
+    fab->fab$b_dns = 0;
+    return sys$parse(fab, NULL, NULL);
+}
+
+#define rms_setup_nam(nam) struct NAM nam = cc$rms_nam
+#define rms_set_nam_nop(nam, opt) nam.nam$b_nop |= (opt)
+#define rms_set_nam_fnb(nam, opt) nam.nam$l_fnb |= (opt)
+#define rms_is_nam_fnb(nam, opt) (nam.nam$l_fnb & (opt))
+#define rms_nam_esll(nam) nam.nam$b_esl
+#define rms_nam_esl(nam) nam.nam$b_esl
+#define rms_nam_name(nam) nam.nam$l_name
+#define rms_nam_namel(nam) nam.nam$l_name
+#define rms_nam_type(nam) nam.nam$l_type
+#define rms_nam_typel(nam) nam.nam$l_type
+#define rms_nam_ver(nam) nam.nam$l_ver
+#define rms_nam_verl(nam) nam.nam$l_ver
+#define rms_nam_rsll(nam) nam.nam$b_rsl
+#define rms_nam_rsl(nam) nam.nam$b_rsl
+#define rms_bind_fab_nam(fab, nam) fab.fab$l_nam = &nam
+#define rms_set_fna(fab, nam, name, size) \
+       fab.fab$b_fns = size; fab.fab$l_fna = name;
+#define rms_get_fna(fab, nam) fab.fab$l_fna
+#define rms_set_dna(fab, nam, name, size) \
+       fab.fab$b_dns = size; fab.fab$l_dna = name;
+#define rms_nam_dns(fab, nam) fab.fab$b_dns;
+#define rms_set_esa(fab, nam, name, size) \
+       nam.nam$b_ess = size; nam.nam$l_esa = name;
+#define rms_set_esal(nam, s_name, s_size, l_name, l_size) \
+       nam.nam$l_esa = s_name; nam.nam$b_ess = s_size;
+#define rms_set_rsa(nam, name, size) \
+       nam.nam$l_rsa = name; nam.nam$b_rss = size;
+#define rms_set_rsal(nam, s_name, s_size, l_name, l_size) \
+       nam.nam$l_rsa = s_name; nam.nam$b_rss = s_size;
+
+#else
+static int rms_free_search_context(struct FAB * fab)
+{
+struct NAML * nam;
+
+    nam = fab->fab$l_naml;
+    nam->naml$b_nop |= NAM$M_SYNCHK;
+    nam->naml$l_rlf = NULL;
+    nam->naml$l_long_defname_size = 0;
+    fab->fab$b_dns = 0;
+    return sys$parse(fab, NULL, NULL);
+}
+
+#define rms_setup_nam(nam) struct NAML nam = cc$rms_naml
+#define rms_set_nam_nop(nam, opt) nam.naml$b_nop |= (opt)
+#define rms_set_nam_fnb(nam, opt) nam.naml$l_fnb |= (opt)
+#define rms_is_nam_fnb(nam, opt) (nam.naml$l_fnb & (opt))
+#define rms_nam_esll(nam) nam.naml$l_long_expand_size
+#define rms_nam_esl(nam) nam.naml$b_esl
+#define rms_nam_name(nam) nam.naml$l_name
+#define rms_nam_namel(nam) nam.naml$l_long_name
+#define rms_nam_type(nam) nam.naml$l_type
+#define rms_nam_typel(nam) nam.naml$l_long_type
+#define rms_nam_ver(nam) nam.naml$l_ver
+#define rms_nam_verl(nam) nam.naml$l_long_ver
+#define rms_nam_rsll(nam) nam.naml$l_long_result_size
+#define rms_nam_rsl(nam) nam.naml$b_rsl
+#define rms_bind_fab_nam(fab, nam) fab.fab$l_naml = &nam
+#define rms_set_fna(fab, nam, name, size) \
+       fab.fab$b_fns = 0; fab.fab$l_fna = (char *) -1; \
+       nam.naml$l_long_filename_size = size; \
+       nam.naml$l_long_filename = name
+#define rms_get_fna(fab, nam) nam.naml$l_long_filename
+#define rms_set_dna(fab, nam, name, size) \
+       fab.fab$b_dns = 0; fab.fab$l_dna = (char *) -1; \
+       nam.naml$l_long_defname_size = size; \
+       nam.naml$l_long_defname = name
+#define rms_nam_dns(fab, nam) nam.naml$l_long_defname_size
+#define rms_set_esa(fab, nam, name, size) \
+       nam.naml$b_ess = 0; nam.naml$l_esa = (char *) -1; \
+       nam.naml$l_long_expand_alloc = size; \
+       nam.naml$l_long_expand = name
+#define rms_set_esal(nam, s_name, s_size, l_name, l_size) \
+       nam.naml$l_esa = s_name; nam.naml$b_ess = s_size; \
+       nam.naml$l_long_expand = l_name; \
+       nam.naml$l_long_expand_alloc = l_size;
+#define rms_set_rsa(nam, name, size) \
+       nam.naml$l_rsa = NULL; nam.naml$b_rss = 0; \
+       nam.naml$l_long_result = name; \
+       nam.naml$l_long_result_alloc = size;
+#define rms_set_rsal(nam, s_name, s_size, l_name, l_size) \
+       nam.naml$l_rsa = s_name; nam.naml$b_rss = s_size; \
+       nam.naml$l_long_result = l_name; \
+       nam.naml$l_long_result_alloc = l_size;
+
+#endif
+
+
+/*{{{char *do_rmsexpand(char *fspec, char *out, int ts, char *def, unsigned opts)*/
+/* Shortcut for common case of simple calls to $PARSE and $SEARCH
+ * to expand file specification.  Allows for a single default file
+ * specification and a simple mask of options.  If outbuf is non-NULL,
+ * it must point to a buffer at least NAM$C_MAXRSS bytes long, into which
+ * the resultant file specification is placed.  If outbuf is NULL, the
+ * resultant file specification is placed into a static buffer.
+ * The third argument, if non-NULL, is taken to be a default file
+ * specification string.  The fourth argument is unused at present.
+ * rmesexpand() returns the address of the resultant string if
  * successful, and NULL on error.
+ *
+ * New functionality for previously unused opts value:
+ *  PERL_RMSEXPAND_M_VMS - Force output file specification to VMS format.
  */
 static char *mp_do_tounixspec(pTHX_ const char *, char *, int);
 
+#if defined(__VAX) || !defined(NAML$C_MAXRSS)
+/* ODS-2 only version */
 static char *
 mp_do_rmsexpand(pTHX_ const char *filespec, char *outbuf, int ts, const char *defspec, unsigned opts)
 {
@@ -3500,8 +4191,13 @@ mp_do_rmsexpand(pTHX_ const char *filespec, char *outbuf, int ts, const char *de
     if (ts) out = Newx(outbuf,NAM$C_MAXRSS+1,char);
     else    outbuf = __rmsexpand_retbuf;
   }
-  if ((isunix = (strchr(filespec,'/') != NULL))) {
-    if (do_tovmsspec(filespec,vmsfspec,0) == NULL) return NULL;
+  isunix = is_unix_filespec(filespec);
+  if (isunix) {
+    if (do_tovmsspec(filespec,vmsfspec,0) == NULL) {
+       if (out)
+          Safefree(out);
+       return NULL;
+    }
     filespec = vmsfspec;
   }
 
@@ -3511,7 +4207,11 @@ mp_do_rmsexpand(pTHX_ const char *filespec, char *outbuf, int ts, const char *de
 
   if (defspec && *defspec) {
     if (strchr(defspec,'/') != NULL) {
-      if (do_tovmsspec(defspec,tmpfspec,0) == NULL) return NULL;
+      if (do_tovmsspec(defspec,tmpfspec,0) == NULL) {
+       if (out)
+          Safefree(out);
+       return NULL;
+      }
       defspec = tmpfspec;
     }
     myfab.fab$l_dna = (char *)defspec; /* cast ok for read only pointer */
@@ -3523,13 +4223,14 @@ mp_do_rmsexpand(pTHX_ const char *filespec, char *outbuf, int ts, const char *de
   mynam.nam$l_rsa = outbuf;
   mynam.nam$b_rss = NAM$C_MAXRSS;
 
+#ifdef NAM$M_NO_SHORT_UPCASE
+  if (decc_efs_case_preserve)
+    mynam.nam$b_nop |= NAM$M_NO_SHORT_UPCASE;
+#endif
+
   retsts = sys$parse(&myfab,0,0);
   if (!(retsts & 1)) {
     mynam.nam$b_nop |= NAM$M_SYNCHK;
-#ifdef NAM$M_NO_SHORT_UPCASE
-    if (decc_efs_case_preserve)
-      mynam.nam$b_nop |= NAM$M_NO_SHORT_UPCASE;
-#endif
     if (retsts == RMS$_DNF || retsts == RMS$_DIR || retsts == RMS$_DEV) {
       retsts = sys$parse(&myfab,0,0);
       if (retsts & 1) goto expanded;
@@ -3547,10 +4248,6 @@ mp_do_rmsexpand(pTHX_ const char *filespec, char *outbuf, int ts, const char *de
   retsts = sys$search(&myfab,0,0);
   if (!(retsts & 1) && retsts != RMS$_FNF) {
     mynam.nam$b_nop |= NAM$M_SYNCHK; mynam.nam$l_rlf = NULL;
-#ifdef NAM$M_NO_SHORT_UPCASE
-    if (decc_efs_case_preserve)
-      mynam.nam$b_nop |= NAM$M_NO_SHORT_UPCASE;
-#endif
     myfab.fab$b_dns = 0; sts = sys$parse(&myfab,0,0);  /* Free search context */
     if (out) Safefree(out);
     set_vaxc_errno(retsts);
@@ -3595,11 +4292,14 @@ mp_do_rmsexpand(pTHX_ const char *filespec, char *outbuf, int ts, const char *de
         if (trimtype) trimtype = !(defnam.nam$l_fnb & NAM$M_EXP_TYPE); 
       }
     }
-    if (trimver) speclen = mynam.nam$l_ver - out;
+    if (trimver) {
+      if (*mynam.nam$l_ver != '\"')
+       speclen = mynam.nam$l_ver - out;
+    }
     if (trimtype) {
       /* If we didn't already trim version, copy down */
       if (speclen > mynam.nam$l_ver - out)
-        memcpy(mynam.nam$l_type, mynam.nam$l_ver, 
+        memmove(mynam.nam$l_type, mynam.nam$l_ver, 
                speclen - (mynam.nam$l_ver - out));
       speclen -= mynam.nam$l_ver - mynam.nam$l_type; 
     }
@@ -3610,11 +4310,23 @@ mp_do_rmsexpand(pTHX_ const char *filespec, char *outbuf, int ts, const char *de
       mynam.nam$l_ver  == mynam.nam$l_type + 1 &&
       !(mynam.nam$l_fnb & NAM$M_EXP_NAME))
     speclen = mynam.nam$l_name - out;
+
+  /* Posix format specifications must have matching quotes */
+  if (decc_posix_compliant_pathnames && (out[0] == '\"')) {
+    if ((speclen > 1) && (out[speclen-1] != '\"')) {
+      out[speclen] = '\"';
+      speclen++;
+    }
+  }
+
   out[speclen] = '\0';
   if (haslower && !decc_efs_case_preserve) __mystrtolower(out);
 
   /* Have we been working with an expanded, but not resultant, spec? */
   /* Also, convert back to Unix syntax if necessary. */
+  if ((opts & PERL_RMSEXPAND_M_VMS) != 0)
+    isunix = 0;
+
   if (!mynam.nam$b_rsl) {
     if (isunix) {
       if (do_tounixspec(esa,outbuf,0) == NULL) return NULL;
@@ -3626,14 +4338,338 @@ mp_do_rmsexpand(pTHX_ const char *filespec, char *outbuf, int ts, const char *de
     strcpy(outbuf,tmpfspec);
   }
   mynam.nam$b_nop |= NAM$M_SYNCHK; mynam.nam$l_rlf = NULL;
+  mynam.nam$l_rsa = NULL;
+  mynam.nam$b_rss = 0;
+  myfab.fab$b_dns = 0;  sts = sys$parse(&myfab,0,0);  /* Free search context */
+  return outbuf;
+}
+#else
+/* ODS-5 supporting routine */
+static char *
+mp_do_rmsexpand(pTHX_ const char *filespec, char *outbuf, int ts, const char *defspec, unsigned opts)
+{
+  static char __rmsexpand_retbuf[NAML$C_MAXRSS+1];
+  char * vmsfspec, *tmpfspec;
+  char * esa, *cp, *out = NULL;
+  char * tbuf;
+  char * esal;
+  char * outbufl;
+  struct FAB myfab = cc$rms_fab;
+  rms_setup_nam(mynam);
+  STRLEN speclen;
+  unsigned long int retsts, trimver, trimtype, haslower = 0, isunix = 0;
+  int sts;
+
+  if (!filespec || !*filespec) {
+    set_vaxc_errno(LIB$_INVARG); set_errno(EINVAL);
+    return NULL;
+  }
+  if (!outbuf) {
+    if (ts) out = Newx(outbuf,VMS_MAXRSS,char);
+    else    outbuf = __rmsexpand_retbuf;
+  }
+
+  vmsfspec = NULL;
+  tmpfspec = NULL;
+  outbufl = NULL;
+  isunix = is_unix_filespec(filespec);
+  if (isunix) {
+    vmsfspec = PerlMem_malloc(VMS_MAXRSS);
+    if (vmsfspec == NULL) _ckvmssts(SS$_INSFMEM);
+    if (do_tovmsspec(filespec,vmsfspec,0) == NULL) {
+       PerlMem_free(vmsfspec);
+       if (out)
+          Safefree(out);
+       return NULL;
+    }
+    filespec = vmsfspec;
+
+     /* Unless we are forcing to VMS format, a UNIX input means
+      * UNIX output, and that requires long names to be used
+      */
+    if ((opts & PERL_RMSEXPAND_M_VMS) == 0)
+       opts |= PERL_RMSEXPAND_M_LONG;
+    else {
+       isunix = 0;
+    }
+  }
+
+  rms_set_fna(myfab, mynam, (char *)filespec, strlen(filespec)); /* cast ok */
+  rms_bind_fab_nam(myfab, mynam);
+
+  if (defspec && *defspec) {
+    int t_isunix;
+    t_isunix = is_unix_filespec(defspec);
+    if (t_isunix) {
+      tmpfspec = PerlMem_malloc(VMS_MAXRSS);
+      if (tmpfspec == NULL) _ckvmssts(SS$_INSFMEM);
+      if (do_tovmsspec(defspec,tmpfspec,0) == NULL) {
+       PerlMem_free(tmpfspec);
+       if (vmsfspec != NULL)
+           PerlMem_free(vmsfspec);
+       if (out)
+          Safefree(out);
+       return NULL;
+      }
+      defspec = tmpfspec;
+    }
+    rms_set_dna(myfab, mynam, (char *)defspec, strlen(defspec)); /* cast ok */
+  }
+
+  esa = PerlMem_malloc(NAM$C_MAXRSS + 1);
+  if (esa == NULL) _ckvmssts(SS$_INSFMEM);
+#if !defined(__VAX) && defined(NAML$C_MAXRSS)
+  esal = PerlMem_malloc(NAML$C_MAXRSS + 1);
+  if (esal == NULL) _ckvmssts(SS$_INSFMEM);
+#endif
+  rms_set_esal(mynam, esa, NAM$C_MAXRSS, esal, NAML$C_MAXRSS);
+
+  if ((opts & PERL_RMSEXPAND_M_LONG) != 0) {
+    rms_set_rsa(mynam, outbuf, (VMS_MAXRSS - 1));
+  }
+  else {
+#if !defined(__VAX) && defined(NAML$C_MAXRSS)
+    outbufl = PerlMem_malloc(VMS_MAXRSS);
+    if (outbufl == NULL) _ckvmssts(SS$_INSFMEM);
+    rms_set_rsal(mynam, outbuf, NAM$C_MAXRSS, outbufl, (VMS_MAXRSS - 1));
+#else
+    rms_set_rsa(mynam, outbuf, NAM$C_MAXRSS);
+#endif
+  }
+
 #ifdef NAM$M_NO_SHORT_UPCASE
   if (decc_efs_case_preserve)
-    mynam.nam$b_nop |= NAM$M_NO_SHORT_UPCASE;
+    rms_set_nam_nop(mynam, NAM$M_NO_SHORT_UPCASE);
+#endif
+
+  /* First attempt to parse as an existing file */
+  retsts = sys$parse(&myfab,0,0);
+  if (!(retsts & STS$K_SUCCESS)) {
+
+    /* Could not find the file, try as syntax only if error is not fatal */
+    rms_set_nam_nop(mynam, NAM$M_SYNCHK);
+    if (retsts == RMS$_DNF || retsts == RMS$_DIR || retsts == RMS$_DEV) {
+      retsts = sys$parse(&myfab,0,0);
+      if (retsts & STS$K_SUCCESS) goto expanded;
+    }  
+
+     /* Still could not parse the file specification */
+    /*----------------------------------------------*/
+    sts = rms_free_search_context(&myfab); /* Free search context */
+    if (out) Safefree(out);
+    if (tmpfspec != NULL)
+       PerlMem_free(tmpfspec);
+    if (vmsfspec != NULL)
+       PerlMem_free(vmsfspec);
+    if (outbufl != NULL)
+       PerlMem_free(outbufl);
+    PerlMem_free(esa);
+    PerlMem_free(esal);
+    set_vaxc_errno(retsts);
+    if      (retsts == RMS$_PRV) set_errno(EACCES);
+    else if (retsts == RMS$_DEV) set_errno(ENODEV);
+    else if (retsts == RMS$_DIR) set_errno(ENOTDIR);
+    else                         set_errno(EVMSERR);
+    return NULL;
+  }
+  retsts = sys$search(&myfab,0,0);
+  if (!(retsts & STS$K_SUCCESS) && retsts != RMS$_FNF) {
+    sts = rms_free_search_context(&myfab); /* Free search context */
+    if (out) Safefree(out);
+    if (tmpfspec != NULL)
+       PerlMem_free(tmpfspec);
+    if (vmsfspec != NULL)
+       PerlMem_free(vmsfspec);
+    if (outbufl != NULL)
+       PerlMem_free(outbufl);
+    PerlMem_free(esa);
+    PerlMem_free(esal);
+    set_vaxc_errno(retsts);
+    if      (retsts == RMS$_PRV) set_errno(EACCES);
+    else                         set_errno(EVMSERR);
+    return NULL;
+  }
+
+  /* If the input filespec contained any lowercase characters,
+   * downcase the result for compatibility with Unix-minded code. */
+  expanded:
+  if (!decc_efs_case_preserve) {
+    for (tbuf = rms_get_fna(myfab, mynam); *tbuf; tbuf++)
+      if (islower(*tbuf)) { haslower = 1; break; }
+  }
+
+   /* Is a long or a short name expected */
+  /*------------------------------------*/
+  if ((opts & PERL_RMSEXPAND_M_LONG) != 0) {
+    if (rms_nam_rsll(mynam)) {
+       tbuf = outbuf;
+       speclen = rms_nam_rsll(mynam);
+    }
+    else {
+       tbuf = esal; /* Not esa */
+       speclen = rms_nam_esll(mynam);
+    }
+  }
+  else {
+    if (rms_nam_rsl(mynam)) {
+       tbuf = outbuf;
+       speclen = rms_nam_rsl(mynam);
+    }
+    else {
+       tbuf = esa; /* Not esal */
+       speclen = rms_nam_esl(mynam);
+    }
+  }
+  /* Trim off null fields added by $PARSE
+   * If type > 1 char, must have been specified in original or default spec
+   * (not true for version; $SEARCH may have added version of existing file).
+   */
+  trimver  = !rms_is_nam_fnb(mynam, NAM$M_EXP_VER);
+  if ((opts & PERL_RMSEXPAND_M_LONG) != 0) {
+    trimtype = !rms_is_nam_fnb(mynam, NAM$M_EXP_TYPE) &&
+             ((rms_nam_verl(mynam) - rms_nam_typel(mynam)) == 1);
+  }
+  else {
+    trimtype = !rms_is_nam_fnb(mynam, NAM$M_EXP_TYPE) &&
+             ((rms_nam_ver(mynam) - rms_nam_type(mynam)) == 1);
+  }
+  if (trimver || trimtype) {
+    if (defspec && *defspec) {
+      char *defesal = NULL;
+      defesal = PerlMem_malloc(NAML$C_MAXRSS + 1);
+      if (defesal != NULL) {
+       struct FAB deffab = cc$rms_fab;
+       rms_setup_nam(defnam);
+     
+       rms_bind_fab_nam(deffab, defnam);
+
+       /* Cast ok */ 
+       rms_set_fna
+           (deffab, defnam, (char *)defspec, rms_nam_dns(myfab, mynam)); 
+
+       rms_set_esa(deffab, defnam, defesal, VMS_MAXRSS - 1);
+
+       rms_set_nam_nop(defnam, 0);
+       rms_set_nam_nop(defnam, NAM$M_SYNCHK);
+#ifdef NAM$M_NO_SHORT_UPCASE
+       if (decc_efs_case_preserve)
+         rms_set_nam_nop(defnam, NAM$M_NO_SHORT_UPCASE);
 #endif
-  mynam.nam$l_rsa = NULL; mynam.nam$b_rss = 0;
-  myfab.fab$b_dns = 0; sts = sys$parse(&myfab,0,0);  /* Free search context */
+       if (sys$parse(&deffab,0,0) & STS$K_SUCCESS) {
+         if (trimver) {
+            trimver  = !rms_is_nam_fnb(defnam, NAM$M_EXP_VER);
+         }
+         if (trimtype) {
+           trimtype = !rms_is_nam_fnb(defnam, NAM$M_EXP_TYPE); 
+         }
+       }
+       PerlMem_free(defesal);
+      }
+    }
+    if (trimver) {
+      if ((opts & PERL_RMSEXPAND_M_LONG) != 0) {
+       if (*(rms_nam_verl(mynam)) != '\"')
+         speclen = rms_nam_verl(mynam) - tbuf;
+      }
+      else {
+       if (*(rms_nam_ver(mynam)) != '\"')
+         speclen = rms_nam_ver(mynam) - tbuf;
+      }
+    }
+    if (trimtype) {
+      /* If we didn't already trim version, copy down */
+      if ((opts & PERL_RMSEXPAND_M_LONG) != 0) {
+       if (speclen > rms_nam_verl(mynam) - tbuf)
+         memmove
+          (rms_nam_typel(mynam),
+           rms_nam_verl(mynam),
+           speclen - (rms_nam_verl(mynam) - tbuf));
+         speclen -= rms_nam_verl(mynam) - rms_nam_typel(mynam);
+      }
+      else {
+       if (speclen > rms_nam_ver(mynam) - tbuf)
+         memmove
+          (rms_nam_type(mynam),
+           rms_nam_ver(mynam),
+           speclen - (rms_nam_ver(mynam) - tbuf));
+         speclen -= rms_nam_ver(mynam) - rms_nam_type(mynam);
+      }
+    }
+  }
+
+   /* Done with these copies of the input files */
+  /*-------------------------------------------*/
+  if (vmsfspec != NULL)
+       PerlMem_free(vmsfspec);
+  if (tmpfspec != NULL)
+       PerlMem_free(tmpfspec);
+
+  /* If we just had a directory spec on input, $PARSE "helpfully"
+   * adds an empty name and type for us */
+  if ((opts & PERL_RMSEXPAND_M_LONG) != 0) {
+    if (rms_nam_namel(mynam) == rms_nam_typel(mynam) &&
+       rms_nam_verl(mynam)  == rms_nam_typel(mynam) + 1 &&
+       !(rms_is_nam_fnb(mynam, NAM$M_EXP_NAME)))
+      speclen = rms_nam_namel(mynam) - tbuf;
+  }
+  else {
+    if (rms_nam_name(mynam) == rms_nam_type(mynam) &&
+       rms_nam_ver(mynam)  == rms_nam_ver(mynam) + 1 &&
+       !(rms_is_nam_fnb(mynam, NAM$M_EXP_NAME)))
+      speclen = rms_nam_name(mynam) - tbuf;
+  }
+
+  /* Posix format specifications must have matching quotes */
+  if (decc_posix_compliant_pathnames && (tbuf[0] == '\"')) {
+    if ((speclen > 1) && (tbuf[speclen-1] != '\"')) {
+      tbuf[speclen] = '\"';
+      speclen++;
+    }
+  }
+  tbuf[speclen] = '\0';
+  if (haslower && !decc_efs_case_preserve) __mystrtolower(tbuf);
+
+  /* Have we been working with an expanded, but not resultant, spec? */
+  /* Also, convert back to Unix syntax if necessary. */
+
+  if (!rms_nam_rsll(mynam)) {
+    if (isunix) {
+      if (do_tounixspec(esa,outbuf,0) == NULL) {
+       if (out) Safefree(out);
+       PerlMem_free(esal);
+       PerlMem_free(esa);
+       if (outbufl != NULL)
+           PerlMem_free(outbufl);
+       return NULL;
+      }
+    }
+    else strcpy(outbuf,esa);
+  }
+  else if (isunix) {
+    tmpfspec = PerlMem_malloc(VMS_MAXRSS);
+    if (tmpfspec == NULL) _ckvmssts(SS$_INSFMEM);
+    if (do_tounixspec(outbuf,tmpfspec,0) == NULL) {
+       if (out) Safefree(out);
+       PerlMem_free(esa);
+       PerlMem_free(esal);
+       PerlMem_free(tmpfspec);
+       if (outbufl != NULL)
+           PerlMem_free(outbufl);
+       return NULL;
+    }
+    strcpy(outbuf,tmpfspec);
+    PerlMem_free(tmpfspec);
+  }
+
+  rms_set_rsal(mynam, NULL, 0, NULL, 0);
+  sts = rms_free_search_context(&myfab); /* Free search context */
+  PerlMem_free(esa);
+  PerlMem_free(esal);
+  if (outbufl != NULL)
+     PerlMem_free(outbufl);
   return outbuf;
 }
+#endif
 /*}}}*/
 /* External entry points */
 char *Perl_rmsexpand(pTHX_ const char *spec, char *buf, const char *def, unsigned opt)
@@ -3676,13 +4712,13 @@ char *Perl_rmsexpand_ts(pTHX_ const char *spec, char *buf, const char *def, unsi
 ** found in the Perl standard distribution.
  */
 
-/*{{{ char *fileify_dirspec[_ts](char *path, char *buf)*/
+/*{{{ char *fileify_dirspec[_ts](char *dir, char *buf)*/
 static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
 {
-    static char __fileify_retbuf[NAM$C_MAXRSS+1];
+    static char __fileify_retbuf[VMS_MAXRSS];
     unsigned long int dirlen, retlen, addmfd = 0, hasfilename = 0;
     char *retspec, *cp1, *cp2, *lastdir;
-    char trndir[NAM$C_MAXRSS+2], vmsdir[NAM$C_MAXRSS+1];
+    char *trndir, *vmsdir;
     unsigned short int trnlnm_iter_count;
     int sts;
 
@@ -3699,9 +4735,12 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
       else
        dirlen = 1;
     }
-    if (dirlen > NAM$C_MAXRSS) {
-      set_errno(ENAMETOOLONG); set_vaxc_errno(RMS$_SYN); return NULL;
+    if (dirlen > (VMS_MAXRSS - 1)) {
+      set_errno(ENAMETOOLONG); set_vaxc_errno(RMS$_SYN);
+      return NULL;
     }
+    trndir = PerlMem_malloc(VMS_MAXRSS + 1);
+    if (trndir == NULL) _ckvmssts(SS$_INSFMEM);
     if (!strpbrk(dir+1,"/]>:")  &&
        (!decc_posix_compliant_pathnames && decc_disable_posix_root)) {
       strcpy(trndir,*dir == '/' ? dir + 1: dir);
@@ -3745,6 +4784,7 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
         for (cp2 = cp1; cp2 > trndir; cp2--) {
          if (*cp2 == '.') {
            if ((cp2 - 1 > trndir) && (*(cp2 - 1) != '^')) {
+/* fix-me, can not scan EFS file specs backward like this */
               *cp2 = *cp1; *cp1 = '\0';
               hasfilename = 1;
              break;
@@ -3755,14 +4795,22 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
       }
     }
 
-    cp1 = strpbrk(trndir,"]:>"); /* Prepare for future change */
+    vmsdir = PerlMem_malloc(VMS_MAXRSS + 1);
+    if (vmsdir == NULL) _ckvmssts(SS$_INSFMEM);
+    cp1 = strpbrk(trndir,"]:>");
     if (hasfilename || !cp1) { /* Unix-style path or filename */
       if (trndir[0] == '.') {
-        if (trndir[1] == '\0' || (trndir[1] == '/' && trndir[2] == '\0'))
+        if (trndir[1] == '\0' || (trndir[1] == '/' && trndir[2] == '\0')) {
+         PerlMem_free(trndir);
+         PerlMem_free(vmsdir);
           return do_fileify_dirspec("[]",buf,ts);
+       }
         else if (trndir[1] == '.' &&
-                 (trndir[2] == '\0' || (trndir[2] == '/' && trndir[3] == '\0')))
+               (trndir[2] == '\0' || (trndir[2] == '/' && trndir[3] == '\0'))) {
+         PerlMem_free(trndir);
+         PerlMem_free(vmsdir);
           return do_fileify_dirspec("[-]",buf,ts);
+       }
       }
       if (dirlen && trndir[dirlen-1] == '/') {    /* path ends with '/'; just add .dir;1 */
         dirlen -= 1;                 /* to last element */
@@ -3775,23 +4823,39 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
         do {
           if (*(cp1+2) == '.') cp1++;
           if (*(cp1+2) == '/' || *(cp1+2) == '\0') {
-            if (do_tovmsspec(trndir,vmsdir,0) == NULL) return NULL;
+           char * ret_chr;
+            if (do_tovmsspec(trndir,vmsdir,0) == NULL) {
+               PerlMem_free(trndir);
+               PerlMem_free(vmsdir);
+               return NULL;
+           }
             if (strchr(vmsdir,'/') != NULL) {
               /* If do_tovmsspec() returned it, it must have VMS syntax
                * delimiters in it, so it's a mixed VMS/Unix spec.  We take
                * the time to check this here only so we avoid a recursion
                * loop; otherwise, gigo.
                */
-              set_errno(EINVAL);  set_vaxc_errno(RMS$_SYN);  return NULL;
+             PerlMem_free(trndir);
+             PerlMem_free(vmsdir);
+              set_errno(EINVAL);  set_vaxc_errno(RMS$_SYN);
+             return NULL;
             }
-            if (do_fileify_dirspec(vmsdir,trndir,0) == NULL) return NULL;
-            return do_tounixspec(trndir,buf,ts);
+            if (do_fileify_dirspec(vmsdir,trndir,0) == NULL) {
+               PerlMem_free(trndir);
+               PerlMem_free(vmsdir);
+               return NULL;
+           }
+           ret_chr = do_tounixspec(trndir,buf,ts);
+           PerlMem_free(trndir);
+           PerlMem_free(vmsdir);
+            return ret_chr;
           }
           cp1++;
         } while ((cp1 = strstr(cp1,"/.")) != NULL);
         lastdir = strrchr(trndir,'/');
       }
       else if (dirlen >= 7 && !strcmp(&trndir[dirlen-7],"/000000")) {
+       char * ret_chr;
         /* Ditto for specs that end in an MFD -- let the VMS code
          * figure out whether it's a real device or a rooted logical. */
 
@@ -3802,9 +4866,20 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
          */
 
         trndir[dirlen] = '/'; trndir[dirlen+1] = '\0';
-        if (do_tovmsspec(trndir,vmsdir,0) == NULL) return NULL;
-        if (do_fileify_dirspec(vmsdir,trndir,0) == NULL) return NULL;
-        return do_tounixspec(trndir,buf,ts);
+        if (do_tovmsspec(trndir,vmsdir,0) == NULL) {
+           PerlMem_free(trndir);
+           PerlMem_free(vmsdir);
+           return NULL;
+       }
+        if (do_fileify_dirspec(vmsdir,trndir,0) == NULL) {
+           PerlMem_free(trndir);
+           PerlMem_free(vmsdir);
+           return NULL;
+       }
+       ret_chr = do_tounixspec(trndir,buf,ts);
+       PerlMem_free(trndir);
+       PerlMem_free(vmsdir);
+        return ret_chr;
       }
       else {
 
@@ -3825,6 +4900,8 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
                   (*(cp2+4) && ((*(cp2+4) != ';' && *(cp2+4) != '.')  ||
                   (*(cp2+5) && ((ver = strtol(cp2+5,&cp3,10)) != 1 &&
                             (ver || *cp3)))))) {
+                 PerlMem_free(trndir);
+                 PerlMem_free(vmsdir);
                   set_errno(ENOTDIR);
                   set_vaxc_errno(RMS$_DIR);
                   return NULL;
@@ -3837,6 +4914,8 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
                   (*(cp2+4) && ((*(cp2+4) != ';' && *(cp2+4) != '.')  ||
                   (*(cp2+5) && ((ver = strtol(cp2+5,&cp3,10)) != 1 &&
                             (ver || *cp3)))))) {
+                PerlMem_free(trndir);
+                PerlMem_free(vmsdir);
                  set_errno(ENOTDIR);
                  set_vaxc_errno(RMS$_DIR);
                  return NULL;
@@ -3859,34 +4938,42 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
        strcat(retspec,".dir;1");
       else
        strcat(retspec,".DIR;1");
+      PerlMem_free(trndir);
+      PerlMem_free(vmsdir);
       return retspec;
     }
     else {  /* VMS-style directory spec */
-      char esa[NAM$C_MAXRSS+1], term, *cp;
+
+      char *esa, term, *cp;
       unsigned long int sts, cmplen, haslower = 0;
+      unsigned int nam_fnb;
+      char * nam_type;
       struct FAB dirfab = cc$rms_fab;
-      struct NAM savnam, dirnam = cc$rms_nam;
-
-      dirfab.fab$b_fns = strlen(trndir);
-      dirfab.fab$l_fna = trndir;
-      dirfab.fab$l_nam = &dirnam;
-      dirfab.fab$l_dna = ".DIR;1";
-      dirfab.fab$b_dns = 6;
-      dirnam.nam$b_ess = NAM$C_MAXRSS;
-      dirnam.nam$l_esa = esa;
+      rms_setup_nam(savnam);
+      rms_setup_nam(dirnam);
+
+      esa = PerlMem_malloc(VMS_MAXRSS + 1);
+      if (esa == NULL) _ckvmssts(SS$_INSFMEM);
+      rms_set_fna(dirfab, dirnam, trndir, strlen(trndir));
+      rms_bind_fab_nam(dirfab, dirnam);
+      rms_set_dna(dirfab, dirnam, ".DIR;1", 6);
+      rms_set_esa(dirfab, dirnam, esa, (VMS_MAXRSS - 1));
 #ifdef NAM$M_NO_SHORT_UPCASE
       if (decc_efs_case_preserve)
-       dirnam.nam$b_nop |= NAM$M_NO_SHORT_UPCASE;
+       rms_set_nam_nop(dirnam, NAM$M_NO_SHORT_UPCASE);
 #endif
 
       for (cp = trndir; *cp; cp++)
         if (islower(*cp)) { haslower = 1; break; }
-      if (!((sts = sys$parse(&dirfab))&1)) {
+      if (!((sts = sys$parse(&dirfab)) & STS$K_SUCCESS)) {
         if ((dirfab.fab$l_sts == RMS$_DIR) || (dirfab.fab$l_sts == RMS$_DNF)) {
-          dirnam.nam$b_nop |= NAM$M_SYNCHK;
-          sts = sys$parse(&dirfab) & 1;
+         rms_set_nam_nop(dirnam, NAM$M_SYNCHK);
+          sts = sys$parse(&dirfab) & STS$K_SUCCESS;
         }
         if (!sts) {
+         PerlMem_free(esa);
+         PerlMem_free(trndir);
+         PerlMem_free(vmsdir);
           set_errno(EVMSERR);
           set_vaxc_errno(dirfab.fab$l_sts);
           return NULL;
@@ -3894,60 +4981,69 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
       }
       else {
         savnam = dirnam;
-        if (sys$search(&dirfab)&1) {  /* Does the file really exist? */
+       /* Does the file really exist? */
+        if (sys$search(&dirfab)& STS$K_SUCCESS) { 
           /* Yes; fake the fnb bits so we'll check type below */
-          dirnam.nam$l_fnb |= NAM$M_EXP_TYPE | NAM$M_EXP_VER;
+       rms_set_nam_fnb(dirnam, (NAM$M_EXP_TYPE | NAM$M_EXP_VER));
         }
         else { /* No; just work with potential name */
           if (dirfab.fab$l_sts == RMS$_FNF) dirnam = savnam;
           else { 
+           PerlMem_free(esa);
+           PerlMem_free(trndir);
+           PerlMem_free(vmsdir);
             set_errno(EVMSERR);  set_vaxc_errno(dirfab.fab$l_sts);
-            dirnam.nam$b_nop |= NAM$M_SYNCHK;  dirnam.nam$l_rlf = NULL;
-            dirfab.fab$b_dns = 0;  sts = sys$parse(&dirfab,0,0);
+           sts = rms_free_search_context(&dirfab);
             return NULL;
           }
         }
       }
-      if (!(dirnam.nam$l_fnb & (NAM$M_EXP_DEV | NAM$M_EXP_DIR))) {
+      if (!rms_is_nam_fnb(dirnam, (NAM$M_EXP_DEV | NAM$M_EXP_DIR))) {
         cp1 = strchr(esa,']');
         if (!cp1) cp1 = strchr(esa,'>');
         if (cp1) {  /* Should always be true */
-          dirnam.nam$b_esl -= cp1 - esa - 1;
-          memcpy(esa,cp1 + 1,dirnam.nam$b_esl);
+          rms_nam_esll(dirnam) -= cp1 - esa - 1;
+          memmove(esa,cp1 + 1, rms_nam_esll(dirnam));
         }
       }
-      if (dirnam.nam$l_fnb & NAM$M_EXP_TYPE) {  /* Was type specified? */
+      if (rms_is_nam_fnb(dirnam, NAM$M_EXP_TYPE)) {  /* Was type specified? */
         /* Yep; check version while we're at it, if it's there. */
-        cmplen = (dirnam.nam$l_fnb & NAM$M_EXP_VER) ? 6 : 4;
-        if (strncmp(dirnam.nam$l_type,".DIR;1",cmplen)) { 
+        cmplen = rms_is_nam_fnb(dirnam, NAM$M_EXP_VER) ? 6 : 4;
+        if (strncmp(rms_nam_typel(dirnam), ".DIR;1", cmplen)) { 
           /* Something other than .DIR[;1].  Bzzt. */
-          dirnam.nam$b_nop |= NAM$M_SYNCHK;  dirnam.nam$l_rlf = NULL;
-          dirfab.fab$b_dns = 0;  sts = sys$parse(&dirfab,0,0);
+         sts = rms_free_search_context(&dirfab);
+         PerlMem_free(esa);
+         PerlMem_free(trndir);
+         PerlMem_free(vmsdir);
           set_errno(ENOTDIR);
           set_vaxc_errno(RMS$_DIR);
           return NULL;
         }
       }
-      esa[dirnam.nam$b_esl] = '\0';
-      if (dirnam.nam$l_fnb & NAM$M_EXP_NAME) {
+      esa[rms_nam_esll(dirnam)] = '\0';
+      if (rms_is_nam_fnb(dirnam, NAM$M_EXP_NAME)) {
         /* They provided at least the name; we added the type, if necessary, */
         if (buf) retspec = buf;                            /* in sys$parse() */
-        else if (ts) Newx(retspec,dirnam.nam$b_esl+1,char);
+        else if (ts) Newx(retspec, rms_nam_esll(dirnam)+1, char);
         else retspec = __fileify_retbuf;
         strcpy(retspec,esa);
-        dirnam.nam$b_nop |= NAM$M_SYNCHK;  dirnam.nam$l_rlf = NULL;
-        dirfab.fab$b_dns = 0;  sts = sys$parse(&dirfab,0,0);
+       sts = rms_free_search_context(&dirfab);
+       PerlMem_free(trndir);
+       PerlMem_free(esa);
+       PerlMem_free(vmsdir);
         return retspec;
       }
       if ((cp1 = strstr(esa,".][000000]")) != NULL) {
         for (cp2 = cp1 + 9; *cp2; cp1++,cp2++) *cp1 = *cp2;
         *cp1 = '\0';
-        dirnam.nam$b_esl -= 9;
+        rms_nam_esll(dirnam) -= 9;
       }
       if ((cp1 = strrchr(esa,']')) == NULL) cp1 = strrchr(esa,'>');
       if (cp1 == NULL) { /* should never happen */
-        dirnam.nam$b_nop |= NAM$M_SYNCHK;  dirnam.nam$l_rlf = NULL;
-        dirfab.fab$b_dns = 0;  sts = sys$parse(&dirfab,0,0);
+       sts = rms_free_search_context(&dirfab);
+       PerlMem_free(trndir);
+       PerlMem_free(esa);
+       PerlMem_free(vmsdir);
         return NULL;
       }
       term = *cp1;
@@ -3955,6 +5051,7 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
       retlen = strlen(esa);
       cp1 = strrchr(esa,'.');
       /* ODS-5 directory specifications can have extra "." in them. */
+      /* Fix-me, can not scan EFS file specifications backwards */
       while (cp1 != NULL) {
         if ((cp1-1 == esa) || (*(cp1-1) != '^'))
          break;
@@ -3976,21 +5073,23 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
         strcpy(retspec,esa);
       }
       else {
-        if (dirnam.nam$l_fnb & NAM$M_ROOT_DIR) {
+        if (rms_is_nam_fnb(dirnam, NAM$M_ROOT_DIR)) {
           /* Go back and expand rooted logical name */
-          dirnam.nam$b_nop = NAM$M_SYNCHK | NAM$M_NOCONCEAL;
+          rms_set_nam_nop(dirnam, NAM$M_SYNCHK | NAM$M_NOCONCEAL);
 #ifdef NAM$M_NO_SHORT_UPCASE
          if (decc_efs_case_preserve)
-           dirnam.nam$b_nop |= NAM$M_NO_SHORT_UPCASE;
+           rms_set_nam_nop(dirnam, NAM$M_NO_SHORT_UPCASE);
 #endif
-          if (!(sys$parse(&dirfab) & 1)) {
-            dirnam.nam$l_rlf = NULL;
-            dirfab.fab$b_dns = 0;  sts = sys$parse(&dirfab,0,0);
+          if (!(sys$parse(&dirfab) & STS$K_SUCCESS)) {
+           sts = rms_free_search_context(&dirfab);
+           PerlMem_free(esa);
+           PerlMem_free(trndir);
+           PerlMem_free(vmsdir);
             set_errno(EVMSERR);
             set_vaxc_errno(dirfab.fab$l_sts);
             return NULL;
           }
-          retlen = dirnam.nam$b_esl - 9; /* esa - '][' - '].DIR;1' */
+          retlen = rms_nam_esll(dirnam) - 9; /* esa - '][' - '].DIR;1' */
           if (buf) retspec = buf;
           else if (ts) Newx(retspec,retlen+16,char);
           else retspec = __fileify_retbuf;
@@ -4000,7 +5099,7 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
           memcpy(retspec,esa,dirlen);
           if (!strncmp(cp1+2,"000000]",7)) {
             retspec[dirlen-1] = '\0';
-           /* Not full ODS-5, just extra dots in directories for now */
+           /* fix-me Not full ODS-5, just extra dots in directories for now */
            cp1 = retspec + dirlen - 1;
            while (cp1 > retspec)
            {
@@ -4015,11 +5114,11 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
             if (*cp1 == '.') *cp1 = ']';
             else {
               memmove(cp1+8,cp1+1,retspec+dirlen-cp1);
-              memcpy(cp1+1,"000000]",7);
+              memmove(cp1+1,"000000]",7);
             }
           }
           else {
-            memcpy(retspec+dirlen,cp1+2,retlen-dirlen);
+            memmove(retspec+dirlen,cp1+2,retlen-dirlen);
             retspec[retlen] = '\0';
             /* Convert last '.' to ']' */
             cp1 = retspec+retlen-1;
@@ -4034,7 +5133,7 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
             if (*cp1 == '.') *cp1 = ']';
             else {
               memmove(cp1+8,cp1+1,retspec+dirlen-cp1);
-              memcpy(cp1+1,"000000]",7);
+              memmove(cp1+1,"000000]",7);
             }
           }
         }
@@ -4044,14 +5143,13 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
           else retspec = __fileify_retbuf;
           cp1 = esa;
           cp2 = retspec;
-          while (*cp1 != ':') *(cp2++) = *(cp1++);
+          while ((*cp1 != ':')  && (*cp1 != '\0')) *(cp2++) = *(cp1++);
           strcpy(cp2,":[000000]");
           cp1 += 2;
           strcpy(cp2+9,cp1);
         }
       }
-      dirnam.nam$b_nop |= NAM$M_SYNCHK;  dirnam.nam$l_rlf = NULL;
-      dirfab.fab$b_dns = 0;  sts = sys$parse(&dirfab,0,0);
+      sts = rms_free_search_context(&dirfab);
       /* We've set up the string up through the filename.  Add the
          type and version, and we're done. */
       strcat(retspec,".DIR;1");
@@ -4059,6 +5157,9 @@ static char *mp_do_fileify_dirspec(pTHX_ const char *dir,char *buf,int ts)
       /* $PARSE may have upcased filespec, so convert output to lower
        * case if input contained any lowercase characters. */
       if (haslower && !decc_efs_case_preserve) __mystrtolower(retspec);
+      PerlMem_free(trndir);
+      PerlMem_free(esa);
+      PerlMem_free(vmsdir);
       return retspec;
     }
 }  /* end of do_fileify_dirspec() */
@@ -4072,9 +5173,9 @@ char *Perl_fileify_dirspec_ts(pTHX_ const char *dir, char *buf)
 /*{{{ char *pathify_dirspec[_ts](char *path, char *buf)*/
 static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
 {
-    static char __pathify_retbuf[NAM$C_MAXRSS+1];
+    static char __pathify_retbuf[VMS_MAXRSS];
     unsigned long int retlen;
-    char *retpath, *cp1, *cp2, trndir[NAM$C_MAXRSS+1];
+    char *retpath, *cp1, *cp2, *trndir;
     unsigned short int trnlnm_iter_count;
     STRLEN trnlen;
     int sts;
@@ -4083,8 +5184,10 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
       set_errno(EINVAL); set_vaxc_errno(SS$_BADPARAM); return NULL;
     }
 
+    trndir = PerlMem_malloc(VMS_MAXRSS);
+    if (trndir == NULL) _ckvmssts(SS$_INSFMEM);
     if (*dir) strcpy(trndir,dir);
-    else getcwd(trndir,sizeof trndir - 1);
+    else getcwd(trndir,VMS_MAXRSS - 1);
 
     trnlnm_iter_count = 0;
     while (!strpbrk(trndir,"/]:>") && !no_translate_barewords
@@ -4100,6 +5203,7 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
         else retpath = __pathify_retbuf;
         strcpy(retpath,dir);
         strcat(retpath,":[000000]");
+       PerlMem_free(trndir);
         return retpath;
       }
     }
@@ -4134,6 +5238,7 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
                   (*(cp2+4) && ((*(cp2+4) != ';' && *(cp2+4) != '.')  ||
                   (*(cp2+5) && ((ver = strtol(cp2+5,&cp3,10)) != 1 &&
                             (ver || *cp3)))))) {
+               PerlMem_free(trndir);
                 set_errno(ENOTDIR);
                 set_vaxc_errno(RMS$_DIR);
                 return NULL;
@@ -4146,6 +5251,7 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
                   (*(cp2+4) && ((*(cp2+4) != ';' && *(cp2+4) != '.')  ||
                   (*(cp2+5) && ((ver = strtol(cp2+5,&cp3,10)) != 1 &&
                             (ver || *cp3)))))) {
+               PerlMem_free(trndir);
                 set_errno(ENOTDIR);
                 set_vaxc_errno(RMS$_DIR);
                 return NULL;
@@ -4168,10 +5274,12 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
       else retpath[retlen-1] = '\0';
     }
     else {  /* VMS-style directory spec */
-      char esa[NAM$C_MAXRSS+1], *cp;
+      char *esa, *cp;
       unsigned long int sts, cmplen, haslower;
       struct FAB dirfab = cc$rms_fab;
-      struct NAM savnam, dirnam = cc$rms_nam;
+      int dirlen;
+      rms_setup_nam(savnam);
+      rms_setup_nam(dirnam);
 
       /* If we've got an explicit filename, we can just shuffle the string. */
       if ( ( (cp1 = strrchr(trndir,']')) != NULL ||
@@ -4185,6 +5293,7 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
                   (*(cp2+4) && ((*(cp2+4) != ';' && *(cp2+4) != '.')  ||
                   (*(cp2+5) && ((ver = strtol(cp2+5,&cp3,10)) != 1 &&
                             (ver || *cp3)))))) {
+              PerlMem_free(trndir);
                set_errno(ENOTDIR);
                set_vaxc_errno(RMS$_DIR);
                return NULL;
@@ -4197,6 +5306,7 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
                   (*(cp2+4) && ((*(cp2+4) != ';' && *(cp2+4) != '.')  ||
                   (*(cp2+5) && ((ver = strtol(cp2+5,&cp3,10)) != 1 &&
                             (ver || *cp3)))))) {
+              PerlMem_free(trndir);
                set_errno(ENOTDIR);
                set_vaxc_errno(RMS$_DIR);
                return NULL;
@@ -4211,36 +5321,40 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
         *cp1 = '.';
         /* We've now got a VMS 'path'; fall through */
       }
-      dirfab.fab$b_fns = strlen(trndir);
-      dirfab.fab$l_fna = trndir;
-      if (trndir[dirfab.fab$b_fns-1] == ']' ||
-          trndir[dirfab.fab$b_fns-1] == '>' ||
-          trndir[dirfab.fab$b_fns-1] == ':') { /* It's already a VMS 'path' */
+
+      dirlen = strlen(trndir);
+      if (trndir[dirlen-1] == ']' ||
+          trndir[dirlen-1] == '>' ||
+          trndir[dirlen-1] == ':') { /* It's already a VMS 'path' */
         if (buf) retpath = buf;
         else if (ts) Newx(retpath,strlen(trndir)+1,char);
         else retpath = __pathify_retbuf;
         strcpy(retpath,trndir);
+       PerlMem_free(trndir);
         return retpath;
-      } 
-      dirfab.fab$l_dna = ".DIR;1";
-      dirfab.fab$b_dns = 6;
-      dirfab.fab$l_nam = &dirnam;
-      dirnam.nam$b_ess = (unsigned char) sizeof esa - 1;
-      dirnam.nam$l_esa = esa;
+      }
+      rms_set_fna(dirfab, dirnam, trndir, dirlen);
+      esa = PerlMem_malloc(VMS_MAXRSS);
+      if (esa == NULL) _ckvmssts(SS$_INSFMEM);
+      rms_set_dna(dirfab, dirnam, ".DIR;1", 6);
+      rms_bind_fab_nam(dirfab, dirnam);
+      rms_set_esa(dirfab, dirnam, esa, VMS_MAXRSS - 1);
 #ifdef NAM$M_NO_SHORT_UPCASE
       if (decc_efs_case_preserve)
-         dirnam.nam$b_nop |= NAM$M_NO_SHORT_UPCASE;
+         rms_set_nam_nop(dirnam, NAM$M_NO_SHORT_UPCASE);
 #endif
 
       for (cp = trndir; *cp; cp++)
         if (islower(*cp)) { haslower = 1; break; }
 
-      if (!(sts = (sys$parse(&dirfab)&1))) {
+      if (!(sts = (sys$parse(&dirfab)& STS$K_SUCCESS))) {
         if ((dirfab.fab$l_sts == RMS$_DIR) || (dirfab.fab$l_sts == RMS$_DNF)) {
-          dirnam.nam$b_nop |= NAM$M_SYNCHK;
-          sts = sys$parse(&dirfab) & 1;
+         rms_set_nam_nop(dirnam, NAM$M_SYNCHK);
+          sts = sys$parse(&dirfab) & STS$K_SUCCESS;
         }
         if (!sts) {
+         PerlMem_free(trndir);
+         PerlMem_free(esa);
           set_errno(EVMSERR);
           set_vaxc_errno(dirfab.fab$l_sts);
           return NULL;
@@ -4248,12 +5362,13 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
       }
       else {
         savnam = dirnam;
-        if (!(sys$search(&dirfab)&1)) {  /* Does the file really exist? */
+       /* Does the file really exist? */
+        if (!(sys$search(&dirfab)&STS$K_SUCCESS)) {
           if (dirfab.fab$l_sts != RMS$_FNF) {
            int sts1;
-            dirnam.nam$b_nop |= NAM$M_SYNCHK;  dirnam.nam$l_rlf = NULL;
-            dirfab.fab$b_dns = 0;
-           sts1 = sys$parse(&dirfab,0,0);
+           sts1 = rms_free_search_context(&dirfab);
+           PerlMem_free(trndir);
+           PerlMem_free(esa);
             set_errno(EVMSERR);
             set_vaxc_errno(dirfab.fab$l_sts);
             return NULL;
@@ -4261,15 +5376,15 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
           dirnam = savnam; /* No; just work with potential name */
         }
       }
-      if (dirnam.nam$l_fnb & NAM$M_EXP_TYPE) {  /* Was type specified? */
+      if (rms_is_nam_fnb(dirnam, NAM$M_EXP_TYPE)) {  /* Was type specified? */
         /* Yep; check version while we're at it, if it's there. */
-        cmplen = (dirnam.nam$l_fnb & NAM$M_EXP_VER) ? 6 : 4;
-        if (strncmp(dirnam.nam$l_type,".DIR;1",cmplen)) { 
+        cmplen = rms_is_nam_fnb(dirnam, NAM$M_EXP_VER) ? 6 : 4;
+        if (strncmp(rms_nam_typel(dirnam),".DIR;1",cmplen)) {
          int sts2;
           /* Something other than .DIR[;1].  Bzzt. */
-          dirnam.nam$b_nop |= NAM$M_SYNCHK;  dirnam.nam$l_rlf = NULL;
-          dirfab.fab$b_dns = 0;
-         sts2 = sys$parse(&dirfab,0,0);
+         sts2 = rms_free_search_context(&dirfab);
+         PerlMem_free(trndir);
+         PerlMem_free(esa);
           set_errno(ENOTDIR);
           set_vaxc_errno(RMS$_DIR);
           return NULL;
@@ -4277,25 +5392,26 @@ static char *mp_do_pathify_dirspec(pTHX_ const char *dir,char *buf, int ts)
       }
       /* OK, the type was fine.  Now pull any file name into the
          directory path. */
-      if ((cp1 = strrchr(esa,']'))) *dirnam.nam$l_type = ']';
+      if ((cp1 = strrchr(esa,']'))) *(rms_nam_typel(dirnam)) = ']';
       else {
         cp1 = strrchr(esa,'>');
-        *dirnam.nam$l_type = '>';
+        *(rms_nam_typel(dirnam)) = '>';
       }
       *cp1 = '.';
-      *(dirnam.nam$l_type + 1) = '\0';
-      retlen = dirnam.nam$l_type - esa + 2;
+      *(rms_nam_typel(dirnam) + 1) = '\0';
+      retlen = (rms_nam_typel(dirnam)) - esa + 2;
       if (buf) retpath = buf;
       else if (ts) Newx(retpath,retlen,char);
       else retpath = __pathify_retbuf;
       strcpy(retpath,esa);
-      dirnam.nam$b_nop |= NAM$M_SYNCHK;  dirnam.nam$l_rlf = NULL;
-      dirfab.fab$b_dns = 0;  sts = sys$parse(&dirfab,0,0);
+      PerlMem_free(esa);
+      sts = rms_free_search_context(&dirfab);
       /* $PARSE may have upcased filespec, so convert output to lower
        * case if input contained any lowercase characters. */
       if (haslower && !decc_efs_case_preserve) __mystrtolower(retpath);
     }
 
+    PerlMem_free(trndir);
     return retpath;
 }  /* end of do_pathify_dirspec() */
 /*}}}*/
@@ -4305,13 +5421,13 @@ char *Perl_pathify_dirspec(pTHX_ const char *dir, char *buf)
 char *Perl_pathify_dirspec_ts(pTHX_ const char *dir, char *buf)
 { return do_pathify_dirspec(dir,buf,1); }
 
-/*{{{ char *tounixspec[_ts](char *path, char *buf)*/
+/*{{{ char *tounixspec[_ts](char *spec, char *buf)*/
 static char *mp_do_tounixspec(pTHX_ const char *spec, char *buf, int ts)
 {
-  static char __tounixspec_retbuf[NAM$C_MAXRSS+1];
-  char *dirend, *rslt, *cp1, *cp3, tmp[NAM$C_MAXRSS+1];
+  static char __tounixspec_retbuf[VMS_MAXRSS];
+  char *dirend, *rslt, *cp1, *cp3, *tmp;
   const char *cp2;
-  int devlen, dirlen, retlen = NAM$C_MAXRSS+1;
+  int devlen, dirlen, retlen = VMS_MAXRSS;
   int expand = 1; /* guarantee room for leading and trailing slashes */
   unsigned short int trnlnm_iter_count;
   int cmp_rslt;
@@ -4334,6 +5450,43 @@ static char *mp_do_tounixspec(pTHX_ const char *spec, char *buf, int ts)
   }
   else rslt = __tounixspec_retbuf;
 
+  /* New VMS specific format needs translation
+   * glob passes filenames with trailing '\n' and expects this preserved.
+   */
+  if (decc_posix_compliant_pathnames) {
+    if (strncmp(spec, "\"^UP^", 5) == 0) {
+      char * uspec;
+      char *tunix;
+      int tunix_len;
+      int nl_flag;
+
+      tunix = PerlMem_malloc(VMS_MAXRSS);
+      if (tunix == NULL) _ckvmssts(SS$_INSFMEM);
+      strcpy(tunix, spec);
+      tunix_len = strlen(tunix);
+      nl_flag = 0;
+      if (tunix[tunix_len - 1] == '\n') {
+       tunix[tunix_len - 1] = '\"';
+       tunix[tunix_len] = '\0';
+       tunix_len--;
+       nl_flag = 1;
+      }
+      uspec = decc$translate_vms(tunix);
+      PerlMem_free(tunix);
+      if ((int)uspec > 0) {
+       strcpy(rslt,uspec);
+       if (nl_flag) {
+         strcat(rslt,"\n");
+       }
+       else {
+         /* If we can not translate it, makemaker wants as-is */
+         strcpy(rslt, spec);
+       }
+       return rslt;
+      }
+    }
+  }
+
   cmp_rslt = 0; /* Presume VMS */
   cp1 = strchr(spec, '/');
   if (cp1 == NULL)
@@ -4420,6 +5573,8 @@ static char *mp_do_tounixspec(pTHX_ const char *spec, char *buf, int ts)
 #else
   cmp_rslt = strncasecmp(spec,"SYS$SCRATCH:", 12);
 #endif
+  tmp = PerlMem_malloc(VMS_MAXRSS);
+  if (tmp == NULL) _ckvmssts(SS$_INSFMEM);
   if (cmp_rslt == 0) {
   int islnm;
 
@@ -4443,11 +5598,13 @@ static char *mp_do_tounixspec(pTHX_ const char *spec, char *buf, int ts)
     cp2++;
     if (*cp2 == ']' || *cp2 == '>') {
       *(cp1++) = '.'; *(cp1++) = '/'; *(cp1++) = '\0';
+      PerlMem_free(tmp);
       return rslt;
     }
     else if ( *cp2 != '^' && *cp2 != '.' && *cp2 != '-') { /* add the implied device */
-      if (getcwd(tmp,sizeof tmp,1) == NULL) {
+      if (getcwd(tmp, VMS_MAXRSS-1 ,1) == NULL) {
         if (ts) Safefree(rslt);
+       PerlMem_free(tmp);
         return NULL;
       }
       trnlnm_iter_count = 0;
@@ -4469,7 +5626,10 @@ static char *mp_do_tounixspec(pTHX_ const char *spec, char *buf, int ts)
       *(cp1++) = '/';
       while (*cp3) {
         *(cp1++) = *(cp3++);
-        if (cp1 - rslt > NAM$C_MAXRSS && !ts && !buf) return NULL; /* No room */
+        if (cp1 - rslt > (VMS_MAXRSS - 1) && !ts && !buf) {
+           PerlMem_free(tmp);
+           return NULL; /* No room */
+       }
       }
       *(cp1++) = '/';
     }
@@ -4486,6 +5646,7 @@ static char *mp_do_tounixspec(pTHX_ const char *spec, char *buf, int ts)
       else cp2++;
     }
   }
+  PerlMem_free(tmp);
   for (; cp2 <= dirend; cp2++) {
     if ((*cp2 == '^')) {
        /* EFS file escape, pass the next character as is */
@@ -4562,63 +5723,764 @@ static char *mp_do_tounixspec(pTHX_ const char *spec, char *buf, int ts)
 char *Perl_tounixspec(pTHX_ const char *spec, char *buf) { return do_tounixspec(spec,buf,0); }
 char *Perl_tounixspec_ts(pTHX_ const char *spec, char *buf) { return do_tounixspec(spec,buf,1); }
 
-/*{{{ char *tovmsspec[_ts](char *path, char *buf)*/
-static char *mp_do_tovmsspec(pTHX_ const char *path, char *buf, int ts) {
-  static char __tovmsspec_retbuf[NAM$C_MAXRSS+1];
-  char *rslt, *dirend;
-  char *lastdot;
-  char *vms_delim;
-  register char *cp1;
-  const char *cp2;
-  unsigned long int infront = 0, hasdir = 1;
-  int rslt_len;
-  int no_type_seen;
-
-  if (path == NULL) return NULL;
-  if (buf) rslt = buf;
-  else if (ts) Newx(rslt,NAM$C_MAXRSS+1,char);
-  else rslt = __tovmsspec_retbuf;
-  if (strpbrk(path,"]:>") ||
-      (dirend = strrchr(path,'/')) == NULL) {
-    if (path[0] == '.') {
-      if (path[1] == '\0') strcpy(rslt,"[]");
-      else if (path[1] == '.' && path[2] == '\0') strcpy(rslt,"[-]");
-      else strcpy(rslt,path); /* probably garbage */
-    }
-    else strcpy(rslt,path);
-    return rslt;
+#if __CRTL_VER >= 80200000 && !defined(__VAX)
+
+static int posix_to_vmsspec
+  (char *vmspath, int vmspath_len, const char *unixpath) {
+int sts;
+struct FAB myfab = cc$rms_fab;
+struct NAML mynam = cc$rms_naml;
+struct dsc$descriptor_s dvidsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
+ struct dsc$descriptor_s specdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
+char *esa;
+char *vms_delim;
+int dir_flag;
+int unixlen;
+
+  /* If not a posix spec already, convert it */
+  dir_flag = 0;
+  unixlen = strlen(unixpath);
+  if (unixlen == 0) {
+    vmspath[0] = '\0';
+    return SS$_NORMAL;
   }
+  if (strncmp(unixpath,"\"^UP^",5) != 0) {
+    sprintf(vmspath,"\"^UP^%s\"",unixpath);
+  }
+  else {
+    /* This is already a VMS specification, no conversion */
+    unixlen--;
+    strncpy(vmspath,unixpath, vmspath_len);
+  }
+  vmspath[vmspath_len] = 0;
+  if (unixpath[unixlen - 1] == '/')
+  dir_flag = 1;
+  esa = PerlMem_malloc(VMS_MAXRSS);
+  if (esa == NULL) _ckvmssts_noperl(SS$_INSFMEM);
+  myfab.fab$l_fna = vmspath;
+  myfab.fab$b_fns = strlen(vmspath);
+  myfab.fab$l_naml = &mynam;
+  mynam.naml$l_esa = NULL;
+  mynam.naml$b_ess = 0;
+  mynam.naml$l_long_expand = esa;
+  mynam.naml$l_long_expand_alloc = (unsigned char) VMS_MAXRSS - 1;
+  mynam.naml$l_rsa = NULL;
+  mynam.naml$b_rss = 0;
+  if (decc_efs_case_preserve)
+    mynam.naml$b_nop |= NAM$M_NO_SHORT_UPCASE;
+  mynam.naml$l_input_flags |= NAML$M_OPEN_SPECIAL;
 
-  vms_delim = strpbrk(path,"]:>");
+  /* Set up the remaining naml fields */
+  sts = sys$parse(&myfab);
 
+  /* It failed! Try again as a UNIX filespec */
+  if (!(sts & 1)) {
+    PerlMem_free(esa);
+    return sts;
+  }
 
-  if (*(dirend+1) == '.') {  /* do we have trailing "/." or "/.." or "/..."? */
-    if (!*(dirend+2)) dirend +=2;
-    if (*(dirend+2) == '.' && !*(dirend+3)) dirend += 3;
-    if (*(dirend+2) == '.' && *(dirend+3) == '.' && !*(dirend+4)) dirend += 4;
+   /* get the Device ID and the FID */
+   sts = sys$search(&myfab);
+   /* on any failure, returned the POSIX ^UP^ filespec */
+   if (!(sts & 1)) {
+      PerlMem_free(esa);
+      return sts;
+   }
+   specdsc.dsc$a_pointer = vmspath;
+   specdsc.dsc$w_length = vmspath_len;
+   dvidsc.dsc$a_pointer = &mynam.naml$t_dvi[1];
+   dvidsc.dsc$w_length = mynam.naml$t_dvi[0];
+   sts = lib$fid_to_name
+      (&dvidsc, mynam.naml$w_fid, &specdsc, &specdsc.dsc$w_length);
+
+  /* on any failure, returned the POSIX ^UP^ filespec */
+  if (!(sts & 1)) {
+     /* This can happen if user does not have permission to read directories */
+     if (strncmp(unixpath,"\"^UP^",5) != 0)
+       sprintf(vmspath,"\"^UP^%s\"",unixpath);
+     else
+       strcpy(vmspath, unixpath);
   }
+  else {
+    vmspath[specdsc.dsc$w_length] = 0;
 
-  cp1 = rslt;
-  cp2 = path;
-  lastdot = strrchr(cp2,'.');
-  if (*cp2 == '/') {
-    char trndev[NAM$C_MAXRSS+1];
-    int islnm, rooted;
-    STRLEN trnend;
+    /* Are we expecting a directory? */
+    if (dir_flag != 0) {
+    int i;
+    char *eptr;
 
-    while (*(cp2+1) == '/') cp2++;  /* Skip multiple /s */
-    if (!*(cp2+1)) {
-      if (decc_disable_posix_root) {
-       strcpy(rslt,"sys$disk:[000000]");
-      }
-      else {
-       strcpy(rslt,"sys$posix_root:[000000]");
-      }
-      return rslt;
-    }
-    while (*(++cp2) != '/' && *cp2) *(cp1++) = *cp2;
-    *cp1 = '\0';
-    islnm =  my_trnlnm(rslt,trndev,0);
+      eptr = NULL;
+
+      i = specdsc.dsc$w_length - 1;
+      while (i > 0) {
+      int zercnt;
+       zercnt = 0;
+       /* Version must be '1' */
+       if (vmspath[i--] != '1')
+         break;
+       /* Version delimiter is one of ".;" */
+       if ((vmspath[i] != '.') && (vmspath[i] != ';'))
+         break;
+       i--;
+       if (vmspath[i--] != 'R')
+         break;
+       if (vmspath[i--] != 'I')
+         break;
+       if (vmspath[i--] != 'D')
+         break;
+       if (vmspath[i--] != '.')
+         break;
+       eptr = &vmspath[i+1];
+       while (i > 0) {
+         if ((vmspath[i] == ']') || (vmspath[i] == '>')) {
+           if (vmspath[i-1] != '^') {
+             if (zercnt != 6) {
+               *eptr = vmspath[i];
+               eptr[1] = '\0';
+               vmspath[i] = '.';
+               break;
+             }
+             else {
+               /* Get rid of 6 imaginary zero directory filename */
+               vmspath[i+1] = '\0';
+             }
+           }
+         }
+         if (vmspath[i] == '0')
+           zercnt++;
+         else
+           zercnt = 10;
+         i--;
+       }
+       break;
+      }
+    }
+  }
+  PerlMem_free(esa);
+  return sts;
+}
+
+/* Can not use LIB$FID_TO_NAME, so doing a manual conversion */
+static int posix_to_vmsspec_hardway
+  (char *vmspath, int vmspath_len, const char *unixpath) {
+
+char *esa;
+const char *unixptr;
+char *vmsptr;
+const char *lastslash;
+const char *lastdot;
+int unixlen;
+int vmslen;
+int dir_start;
+int dir_dot;
+int quoted;
+
+
+  unixptr = unixpath;
+  dir_dot = 0;
+
+  /* Ignore leading "/" characters */
+  while((unixptr[0] == '/') && (unixptr[1] == '/')) {
+    unixptr++;
+  }
+  unixlen = strlen(unixptr);
+
+  /* Do nothing with blank paths */
+  if (unixlen == 0) {
+    vmspath[0] = '\0';
+    return SS$_NORMAL;
+  }
+
+  lastslash = strrchr(unixptr,'/');
+  lastdot = strrchr(unixptr,'.');
+
+
+  /* last dot is last dot or past end of string */
+  if (lastdot == NULL)
+    lastdot = unixptr + unixlen;
+
+  /* if no directories, set last slash to beginning of string */
+  if (lastslash == NULL) {
+    lastslash = unixptr;
+  }
+  else {
+    /* Watch out for trailing "." after last slash, still a directory */
+    if ((lastslash[1] == '.') && (lastslash[2] == '\0')) {
+      lastslash = unixptr + unixlen;
+    }
+
+    /* Watch out for traiing ".." after last slash, still a directory */
+    if ((lastslash[1] == '.')&&(lastslash[2] == '.')&&(lastslash[3] == '\0')) {
+      lastslash = unixptr + unixlen;
+    }
+
+    /* dots in directories are aways escaped */
+    if (lastdot < lastslash)
+      lastdot = unixptr + unixlen;
+  }
+
+  /* if (unixptr < lastslash) then we are in a directory */
+
+  dir_start = 0;
+  quoted = 0;
+
+  vmsptr = vmspath;
+  vmslen = 0;
+
+  /* This could have a "^UP^ on the front */
+  if (strncmp(unixptr,"\"^UP^",5) == 0) {
+    quoted = 1;
+    unixptr+= 5;
+  }
+
+  /* Start with the UNIX path */
+  if (*unixptr != '/') {
+    /* relative paths */
+    if (lastslash > unixptr) {
+    int dotdir_seen;
+
+      /* skip leading ./ */
+      dotdir_seen = 0;
+      while ((unixptr[0] == '.') && (unixptr[1] == '/')) {
+       dotdir_seen = 1;
+       unixptr++;
+       unixptr++;
+      }
+
+      /* Are we still in a directory? */
+      if (unixptr <= lastslash) {
+       *vmsptr++ = '[';
+       vmslen = 1;
+       dir_start = 1;
+       /* if not backing up, then it is relative forward. */
+       if (!((*unixptr == '.') && (unixptr[1] == '.') &&
+             ((unixptr[2] == '/') || (unixptr[2] == '\0')))) {
+         *vmsptr++ = '.';
+         vmslen++;
+         dir_dot = 1;
+       }
+       }
+       else {
+        if (dotdir_seen) {
+          /* Perl wants an empty directory here to tell the difference
+           * between a DCL commmand and a filename
+           */
+         *vmsptr++ = '[';
+         *vmsptr++ = ']';
+         vmslen = 2;
+       }
+      }
+    }
+    else {
+      /* Handle two special files . and .. */
+      if (unixptr[0] == '.') {
+        if (unixptr[1] == '\0') {
+         *vmsptr++ = '[';
+         *vmsptr++ = ']';
+         vmslen += 2;
+         *vmsptr++ = '\0';
+         return SS$_NORMAL;
+       }
+        if ((unixptr[1] == '.') && (unixptr[2] == '\0')) {
+         *vmsptr++ = '[';
+         *vmsptr++ = '-';
+         *vmsptr++ = ']';
+         vmslen += 3;
+         *vmsptr++ = '\0';
+         return SS$_NORMAL;
+       }
+      }
+    }
+  }
+  else {       /* Absolute PATH handling */
+  int sts;
+  char * nextslash;
+  int seg_len;
+    /* Need to find out where root is */
+
+    /* In theory, this procedure should never get an absolute POSIX pathname
+     * that can not be found on the POSIX root.
+     * In practice, that can not be relied on, and things will show up
+     * here that are a VMS device name or concealed logical name instead.
+     * So to make things work, this procedure must be tolerant.
+     */
+    esa = PerlMem_malloc(vmspath_len);
+    if (esa == NULL) _ckvmssts_noperl(SS$_INSFMEM);
+
+    sts = SS$_NORMAL;
+    nextslash = strchr(&unixptr[1],'/');
+    seg_len = 0;
+    if (nextslash != NULL) {
+      seg_len = nextslash - &unixptr[1];
+      strncpy(vmspath, unixptr, seg_len + 1);
+      vmspath[seg_len+1] = 0;
+      sts = posix_to_vmsspec(esa, vmspath_len, vmspath);
+    }
+
+    if (sts & 1) {
+      /* This is verified to be a real path */
+
+      sts = posix_to_vmsspec(esa, vmspath_len, "/");
+      strcpy(vmspath, esa);
+      vmslen = strlen(vmspath);
+      vmsptr = vmspath + vmslen;
+      unixptr++;
+      if (unixptr < lastslash) {
+      char * rptr;
+       vmsptr--;
+       *vmsptr++ = '.';
+       dir_start = 1;
+       dir_dot = 1;
+       if (vmslen > 7) {
+       int cmp;
+         rptr = vmsptr - 7;
+         cmp = strcmp(rptr,"000000.");
+         if (cmp == 0) {
+           vmslen -= 7;
+           vmsptr -= 7;
+           vmsptr[1] = '\0';
+         } /* removing 6 zeros */
+       } /* vmslen < 7, no 6 zeros possible */
+      } /* Not in a directory */
+    } /* end of verified real path handling */
+    else {
+    int add_6zero;
+    int islnm;
+
+      /* Ok, we have a device or a concealed root that is not in POSIX
+       * or we have garbage.  Make the best of it.
+       */
+
+      /* Posix to VMS destroyed this, so copy it again */
+      strncpy(vmspath, &unixptr[1], seg_len);
+      vmspath[seg_len] = 0;
+      vmslen = seg_len;
+      vmsptr = &vmsptr[vmslen];
+      islnm = 0;
+
+      /* Now do we need to add the fake 6 zero directory to it? */
+      add_6zero = 1;
+      if ((*lastslash == '/') && (nextslash < lastslash)) {
+       /* No there is another directory */
+       add_6zero = 0;
+      }
+      else {
+      int trnend;
+
+       /* now we have foo:bar or foo:[000000]bar to decide from */
+       islnm = vmstrnenv(vmspath, esa, 0, fildev, 0);
+        trnend = islnm ? islnm - 1 : 0;
+
+       /* if this was a logical name, ']' or '>' must be present */
+       /* if not a logical name, then assume a device and hope. */
+       islnm =  trnend ? (esa[trnend] == ']' || esa[trnend] == '>') : 0;
+
+       /* if log name and trailing '.' then rooted - treat as device */
+       add_6zero = islnm ? (esa[trnend-1] == '.') : 0;
+
+       /* Fix me, if not a logical name, a device lookup should be
+         * done to see if the device is file structured.  If the device
+         * is not file structured, the 6 zeros should not be put on.
+         *
+         * As it is, perl is occasionally looking for dev:[000000]tty.
+        * which looks a little strange.
+         */
+
+       if ((add_6zero == 0) && (*nextslash == '/') && (nextslash[1] == '\0')) {
+         /* No real directory present */
+         add_6zero = 1;
+       }
+      }
+
+      /* Put the device delimiter on */
+      *vmsptr++ = ':';
+      vmslen++;
+      unixptr = nextslash;
+      unixptr++;
+
+      /* Start directory if needed */
+      if (!islnm || add_6zero) {
+       *vmsptr++ = '[';
+       vmslen++;
+       dir_start = 1;
+      }
+
+      /* add fake 000000] if needed */
+      if (add_6zero) {
+       *vmsptr++ = '0';
+       *vmsptr++ = '0';
+       *vmsptr++ = '0';
+       *vmsptr++ = '0';
+       *vmsptr++ = '0';
+       *vmsptr++ = '0';
+       *vmsptr++ = ']';
+       vmslen += 7;
+       dir_start = 0;
+      }
+
+    } /* non-POSIX translation */
+    PerlMem_free(esa);
+  } /* End of relative/absolute path handling */
+
+  while ((*unixptr) && (vmslen < vmspath_len)){
+  int dash_flag;
+
+    dash_flag = 0;
+
+    if (dir_start != 0) {
+
+      /* First characters in a directory are handled special */
+      while ((*unixptr == '/') ||
+            ((*unixptr == '.') &&
+             ((unixptr[1]=='.') || (unixptr[1]=='/') || (unixptr[1]=='\0')))) {
+      int loop_flag;
+
+       loop_flag = 0;
+
+        /* Skip redundant / in specification */
+        while ((*unixptr == '/') && (dir_start != 0)) {
+         loop_flag = 1;
+         unixptr++;
+         if (unixptr == lastslash)
+           break;
+       }
+       if (unixptr == lastslash)
+         break;
+
+        /* Skip redundant ./ characters */
+       while ((*unixptr == '.') &&
+              ((unixptr[1] == '/')||(unixptr[1] == '\0'))) {
+         loop_flag = 1;
+         unixptr++;
+         if (unixptr == lastslash)
+           break;
+         if (*unixptr == '/')
+           unixptr++;
+       }
+       if (unixptr == lastslash)
+         break;
+
+       /* Skip redundant ../ characters */
+       while ((*unixptr == '.') && (unixptr[1] == '.') &&
+            ((unixptr[2] == '/') || (unixptr[2] == '\0'))) {
+         /* Set the backing up flag */
+         loop_flag = 1;
+         dir_dot = 0;
+         dash_flag = 1;
+         *vmsptr++ = '-';
+         vmslen++;
+         unixptr++; /* first . */
+         unixptr++; /* second . */
+         if (unixptr == lastslash)
+           break;
+         if (*unixptr == '/') /* The slash */
+           unixptr++;
+       }
+       if (unixptr == lastslash)
+         break;
+
+       /* To do: Perl expects /.../ to be translated to [...] on VMS */
+       /* Not needed when VMS is pretending to be UNIX. */
+
+       /* Is this loop stuck because of too many dots? */
+       if (loop_flag == 0) {
+         /* Exit the loop and pass the rest through */
+         break;
+       }
+      }
+
+      /* Are we done with directories yet? */
+      if (unixptr >= lastslash) {
+
+       /* Watch out for trailing dots */
+       if (dir_dot != 0) {
+           vmslen --;
+           vmsptr--;
+       }
+       *vmsptr++ = ']';
+       vmslen++;
+       dash_flag = 0;
+       dir_start = 0;
+       if (*unixptr == '/')
+         unixptr++;
+      }
+      else {
+       /* Have we stopped backing up? */
+       if (dash_flag) {
+         *vmsptr++ = '.';
+         vmslen++;
+         dash_flag = 0;
+         /* dir_start continues to be = 1 */
+       }
+       if (*unixptr == '-') {
+         *vmsptr++ = '^';
+         *vmsptr++ = *unixptr++;
+         vmslen += 2;
+         dir_start = 0;
+
+         /* Now are we done with directories yet? */
+         if (unixptr >= lastslash) {
+
+           /* Watch out for trailing dots */
+           if (dir_dot != 0) {
+             vmslen --;
+             vmsptr--;
+           }
+
+           *vmsptr++ = ']';
+           vmslen++;
+           dash_flag = 0;
+           dir_start = 0;
+         }
+       }
+      }
+    }
+
+    /* All done? */
+    if (*unixptr == '\0')
+      break;
+
+    /* Normal characters - More EFS work probably needed */
+    dir_start = 0;
+    dir_dot = 0;
+
+    switch(*unixptr) {
+    case '/':
+       /* remove multiple / */
+       while (unixptr[1] == '/') {
+          unixptr++;
+       }
+       if (unixptr == lastslash) {
+         /* Watch out for trailing dots */
+         if (dir_dot != 0) {
+           vmslen --;
+           vmsptr--;
+         }
+         *vmsptr++ = ']';
+       }
+       else {
+         dir_start = 1;
+         *vmsptr++ = '.';
+         dir_dot = 1;
+
+         /* To do: Perl expects /.../ to be translated to [...] on VMS */
+         /* Not needed when VMS is pretending to be UNIX. */
+
+       }
+       dash_flag = 0;
+       if (*unixptr != '\0')
+         unixptr++;
+       vmslen++;
+       break;
+    case '?':
+       *vmsptr++ = '%';
+       vmslen++;
+       unixptr++;
+       break;
+    case ' ':
+       *vmsptr++ = '^';
+       *vmsptr++ = '_';
+       vmslen += 2;
+       unixptr++;
+       break;
+    case '.':
+       if ((unixptr < lastdot) || (unixptr[1] == '\0')) {
+         *vmsptr++ = '^';
+         *vmsptr++ = '.';
+         vmslen += 2;
+         unixptr++;
+
+         /* trailing dot ==> '^..' on VMS */
+         if (*unixptr == '\0') {
+           *vmsptr++ = '.';
+           vmslen++;
+         }
+         *vmsptr++ = *unixptr++;
+         vmslen ++;
+       }
+       if (quoted && (unixptr[1] == '\0')) {
+         unixptr++;
+         break;
+       }
+       *vmsptr++ = '^';
+       *vmsptr++ = *unixptr++;
+       vmslen += 2;
+       break;
+    case '~':
+    case ';':
+    case '\\':
+       *vmsptr++ = '^';
+       *vmsptr++ = *unixptr++;
+       vmslen += 2;
+       break;
+    default:
+       if (*unixptr != '\0') {
+         *vmsptr++ = *unixptr++;
+         vmslen++;
+       }
+       break;
+    }
+  }
+
+  /* Make sure directory is closed */
+  if (unixptr == lastslash) {
+    char *vmsptr2;
+    vmsptr2 = vmsptr - 1;
+
+    if (*vmsptr2 != ']') {
+      *vmsptr2--;
+
+      /* directories do not end in a dot bracket */
+      if (*vmsptr2 == '.') {
+       vmsptr2--;
+
+       /* ^. is allowed */
+        if (*vmsptr2 != '^') {
+         vmsptr--; /* back up over the dot */
+       }
+      }
+      *vmsptr++ = ']';
+    }
+  }
+  else {
+    char *vmsptr2;
+    /* Add a trailing dot if a file with no extension */
+    vmsptr2 = vmsptr - 1;
+    if ((*vmsptr2 != ']') && (*vmsptr2 != '*') && (*vmsptr2 != '%') &&
+        (*lastdot != '.')) {
+       *vmsptr++ = '.';
+        vmslen++;
+    }
+  }
+
+  *vmsptr = '\0';
+  return SS$_NORMAL;
+}
+#endif
+
+/*{{{ char *tovmsspec[_ts](char *path, char *buf)*/
+static char *mp_do_tovmsspec(pTHX_ const char *path, char *buf, int ts) {
+  static char __tovmsspec_retbuf[VMS_MAXRSS];
+  char *rslt, *dirend;
+  char *lastdot;
+  char *vms_delim;
+  register char *cp1;
+  const char *cp2;
+  unsigned long int infront = 0, hasdir = 1;
+  int rslt_len;
+  int no_type_seen;
+
+  if (path == NULL) return NULL;
+  rslt_len = VMS_MAXRSS;
+  if (buf) rslt = buf;
+  else if (ts) Newx(rslt, VMS_MAXRSS, char);
+  else rslt = __tovmsspec_retbuf;
+  if (strpbrk(path,"]:>") ||
+      (dirend = strrchr(path,'/')) == NULL) {
+    if (path[0] == '.') {
+      if (path[1] == '\0') strcpy(rslt,"[]");
+      else if (path[1] == '.' && path[2] == '\0') strcpy(rslt,"[-]");
+      else strcpy(rslt,path); /* probably garbage */
+    }
+    else strcpy(rslt,path);
+    return rslt;
+  }
+
+   /* Posix specifications are now a native VMS format */
+  /*--------------------------------------------------*/
+#if __CRTL_VER >= 80200000 && !defined(__VAX)
+  if (decc_posix_compliant_pathnames) {
+    if (strncmp(path,"\"^UP^",5) == 0) {
+      posix_to_vmsspec_hardway(rslt, rslt_len, path);
+      return rslt;
+    }
+  }
+#endif
+
+  vms_delim = strpbrk(path,"]:>");
+
+  if ((vms_delim != NULL) ||
+      ((dirend = strrchr(path,'/')) == NULL)) {
+
+    /* VMS special characters found! */
+
+    if (path[0] == '.') {
+      if (path[1] == '\0') strcpy(rslt,"[]");
+      else if (path[1] == '.' && path[2] == '\0')
+       strcpy(rslt,"[-]");
+
+      /* Dot preceeding a device or directory ? */
+      else {
+       /* If not in POSIX mode, pass it through and hope it works */
+#if __CRTL_VER >= 80200000 && !defined(__VAX)
+       if (!decc_posix_compliant_pathnames)
+         strcpy(rslt,path); /* probably garbage */
+       else
+         posix_to_vmsspec_hardway(rslt, rslt_len, path);
+#else
+        strcpy(rslt,path); /* probably garbage */
+#endif
+      }
+    }
+    else {
+
+       /* If no VMS characters and in POSIX mode, convert it!
+        * This is the easiest way to get directory specifications
+        * handled correctly in POSIX mode
+        */
+#if __CRTL_VER >= 80200000 && !defined(__VAX)
+      if ((vms_delim == NULL) && decc_posix_compliant_pathnames)
+       posix_to_vmsspec_hardway(rslt, rslt_len, path);
+      else {
+        /* No unix path separators - presume VMS already */
+       strcpy(rslt,path);
+      }
+#else
+      strcpy(rslt,path); /* probably garbage */
+#endif
+    }
+    return rslt;
+  }
+
+/* If POSIX mode active, handle the conversion */
+#if __CRTL_VER >= 80200000 && !defined(__VAX)
+  if (decc_posix_compliant_pathnames) {
+    posix_to_vmsspec_hardway(rslt, rslt_len, path);
+    return rslt;
+  }
+#endif
+
+  if (*(dirend+1) == '.') {  /* do we have trailing "/." or "/.." or "/..."? */
+    if (!*(dirend+2)) dirend +=2;
+    if (*(dirend+2) == '.' && !*(dirend+3)) dirend += 3;
+    if (*(dirend+2) == '.' && *(dirend+3) == '.' && !*(dirend+4)) dirend += 4;
+  }
+
+  cp1 = rslt;
+  cp2 = path;
+  lastdot = strrchr(cp2,'.');
+  if (*cp2 == '/') {
+    char *trndev;
+    int islnm, rooted;
+    STRLEN trnend;
+
+    while (*(cp2+1) == '/') cp2++;  /* Skip multiple /s */
+    if (!*(cp2+1)) {
+      if (decc_disable_posix_root) {
+       strcpy(rslt,"sys$disk:[000000]");
+      }
+      else {
+       strcpy(rslt,"sys$posix_root:[000000]");
+      }
+      return rslt;
+    }
+    while (*(++cp2) != '/' && *cp2) *(cp1++) = *cp2;
+    *cp1 = '\0';
+    trndev = PerlMem_malloc(VMS_MAXRSS);
+    if (trndev == NULL) _ckvmssts(SS$_INSFMEM);
+    islnm =  my_trnlnm(rslt,trndev,0);
 
      /* DECC special handling */
     if (!islnm) {
@@ -4668,7 +6530,6 @@ static char *mp_do_tovmsspec(pTHX_ const char *path, char *buf, int ts) {
     }
     else {
       if (cp2 != dirend) {
-        if (!buf && ts) Renew(rslt,strlen(path)-strlen(rslt)+trnend+4,char);
         strcpy(rslt,trndev);
         cp1 = rslt + trnend;
        if (*cp2 != 0) {
@@ -4683,6 +6544,7 @@ static char *mp_do_tovmsspec(pTHX_ const char *path, char *buf, int ts) {
        }
       }
     }
+    PerlMem_free(trndev);
   }
   else {
     *(cp1++) = '[';
@@ -4828,9 +6690,11 @@ static char *mp_do_tovmsspec(pTHX_ const char *path, char *buf, int ts) {
        break;
     case ';':
        /* FIXME: This needs fixing as Perl is putting ".dir;" on UNIX filespecs
-        * which is wrong.  UNIX notation should be ".dir. unless
+        * which is wrong.  UNIX notation should be ".dir." unless
         * the DECC$FILENAME_UNIX_NO_VERSION is enabled.
         * changing this behavior could break more things at this time.
+        * efs character set effectively does not allow "." to be a version
+        * delimiter as a further complication about changing this.
         */
        if (decc_filename_unix_report != 0) {
          *(cp1++) = '^';
@@ -4863,23 +6727,41 @@ char *Perl_tovmsspec_ts(pTHX_ const char *path, char *buf) { return do_tovmsspec
 
 /*{{{ char *tovmspath[_ts](char *path, char *buf)*/
 static char *mp_do_tovmspath(pTHX_ const char *path, char *buf, int ts) {
-  static char __tovmspath_retbuf[NAM$C_MAXRSS+1];
+  static char __tovmspath_retbuf[VMS_MAXRSS];
   int vmslen;
-  char pathified[NAM$C_MAXRSS+1], vmsified[NAM$C_MAXRSS+1], *cp;
+  char *pathified, *vmsified, *cp;
 
   if (path == NULL) return NULL;
-  if (do_pathify_dirspec(path,pathified,0) == NULL) return NULL;
-  if (do_tovmsspec(pathified,buf ? buf : vmsified,0) == NULL) return NULL;
-  if (buf) return buf;
+  pathified = PerlMem_malloc(VMS_MAXRSS);
+  if (pathified == NULL) _ckvmssts(SS$_INSFMEM);
+  if (do_pathify_dirspec(path,pathified,0) == NULL) {
+    PerlMem_free(pathified);
+    return NULL;
+  }
+
+  vmsified = NULL;
+  if (buf == NULL)
+     Newx(vmsified, VMS_MAXRSS, char);
+  if (do_tovmsspec(pathified, buf ? buf : vmsified, 0) == NULL) {
+    PerlMem_free(pathified);
+    if (vmsified) Safefree(vmsified);
+    return NULL;
+  }
+  PerlMem_free(pathified);
+  if (buf) {
+    return buf;
+  }
   else if (ts) {
     vmslen = strlen(vmsified);
     Newx(cp,vmslen+1,char);
     memcpy(cp,vmsified,vmslen);
     cp[vmslen] = '\0';
+    Safefree(vmsified);
     return cp;
   }
   else {
     strcpy(__tovmspath_retbuf,vmsified);
+    Safefree(vmsified);
     return __tovmspath_retbuf;
   }
 
@@ -4892,23 +6774,42 @@ char *Perl_tovmspath_ts(pTHX_ const char *path, char *buf) { return do_tovmspath
 
 /*{{{ char *tounixpath[_ts](char *path, char *buf)*/
 static char *mp_do_tounixpath(pTHX_ const char *path, char *buf, int ts) {
-  static char __tounixpath_retbuf[NAM$C_MAXRSS+1];
+  static char __tounixpath_retbuf[VMS_MAXRSS];
   int unixlen;
-  char pathified[NAM$C_MAXRSS+1], unixified[NAM$C_MAXRSS+1], *cp;
+  char *pathified, *unixified, *cp;
 
   if (path == NULL) return NULL;
-  if (do_pathify_dirspec(path,pathified,0) == NULL) return NULL;
-  if (do_tounixspec(pathified,buf ? buf : unixified,0) == NULL) return NULL;
-  if (buf) return buf;
+  pathified = PerlMem_malloc(VMS_MAXRSS);
+  if (pathified == NULL) _ckvmssts(SS$_INSFMEM);
+  if (do_pathify_dirspec(path,pathified,0) == NULL) {
+    PerlMem_free(pathified);
+    return NULL;
+  }
+
+  unixified = NULL;
+  if (buf == NULL) {
+      Newx(unixified, VMS_MAXRSS, char);
+  }
+  if (do_tounixspec(pathified,buf ? buf : unixified,0) == NULL) {
+    PerlMem_free(pathified);
+    if (unixified) Safefree(unixified);
+    return NULL;
+  }
+  PerlMem_free(pathified);
+  if (buf) {
+    return buf;
+  }
   else if (ts) {
     unixlen = strlen(unixified);
     Newx(cp,unixlen+1,char);
     memcpy(cp,unixified,unixlen);
     cp[unixlen] = '\0';
+    Safefree(unixified);
     return cp;
   }
   else {
     strcpy(__tounixpath_retbuf,unixified);
+    Safefree(unixified);
     return __tounixpath_retbuf;
   }
 
@@ -5120,7 +7021,8 @@ mp_getredirection(pTHX_ int *ac, char ***av)
      * Allocate and fill in the new argument vector, Some Unix's terminate
      * the list with an extra null pointer.
      */
-    Newx(argv, item_count+1, char *);
+    argv = (char **) PerlMem_malloc((item_count+1) * sizeof(char *));
+    if (argv == NULL) _ckvmssts_noperl(SS$_INSFMEM);
     *av = argv;
     for (j = 0; j < item_count; ++j, list_head = list_head->next)
        argv[j] = list_head->value;
@@ -5215,11 +7117,13 @@ static void add_item(struct list_item **head,
 {
     if (*head == 0)
        {
-       Newx(*head,1,struct list_item);
+       *head = (struct list_item *) PerlMem_malloc(sizeof(struct list_item));
+       if (head == NULL) _ckvmssts_noperl(SS$_INSFMEM);
        *tail = *head;
        }
     else {
-       Newx((*tail)->next,1,struct list_item);
+       (*tail)->next = (struct list_item *) PerlMem_malloc(sizeof(struct list_item));
+       if ((*tail)->next == NULL) _ckvmssts_noperl(SS$_INSFMEM);
        *tail = (*tail)->next;
        }
     (*tail)->value = value;
@@ -5239,11 +7143,17 @@ char *had_version;
 char *had_device;
 int had_directory;
 char *devdir,*cp;
-char vmsspec[NAM$C_MAXRSS+1];
+char *vmsspec;
 $DESCRIPTOR(filespec, "");
 $DESCRIPTOR(defaultspec, "SYS$DISK:[]");
 $DESCRIPTOR(resultspec, "");
-unsigned long int zero = 0, sts;
+unsigned long int lff_flags = 0;
+int sts;
+int rms_sts;
+
+#ifdef VMS_LONGNAME_SUPPORT
+    lff_flags = LIB$M_FIL_LONG_NAMES;
+#endif
 
     for (cp = item; *cp; cp++) {
        if (*cp == '*' || *cp == '%' || isspace(*cp)) break;
@@ -5271,6 +7181,8 @@ unsigned long int zero = 0, sts;
     resultspec.dsc$b_dtype = DSC$K_DTYPE_T;
     resultspec.dsc$b_class = DSC$K_CLASS_D;
     resultspec.dsc$a_pointer = NULL;
+    vmsspec = PerlMem_malloc(VMS_MAXRSS);
+    if (vmsspec == NULL) _ckvmssts_noperl(SS$_INSFMEM);
     if ((isunix = (int) strchr(item,'/')) != (int) NULL)
       filespec.dsc$a_pointer = do_tovmsspec(item,vmsspec,0);
     if (!isunix || !filespec.dsc$a_pointer)
@@ -5286,13 +7198,15 @@ unsigned long int zero = 0, sts;
     had_device = strchr(item, ':');
     had_directory = (isunix || NULL != strchr(item, '[')) || (NULL != strchr(item, '<'));
     
-    while (1 == (1 & (sts = lib$find_file(&filespec, &resultspec, &context,
-                                 &defaultspec, 0, 0, &zero))))
+    while ($VMS_STATUS_SUCCESS(sts = lib$find_file
+                                (&filespec, &resultspec, &context,
+                                 &defaultspec, 0, &rms_sts, &lff_flags)))
        {
        char *string;
        char *c;
 
-       Newx(string,resultspec.dsc$w_length+1,char);
+       string = PerlMem_malloc(resultspec.dsc$w_length+1);
+        if (string == NULL) _ckvmssts_noperl(SS$_INSFMEM);
        strncpy(string, resultspec.dsc$a_pointer, resultspec.dsc$w_length);
        string[resultspec.dsc$w_length] = '\0';
        if (NULL == had_version)
@@ -5315,7 +7229,8 @@ unsigned long int zero = 0, sts;
        if (isunix) trim_unixpath(string,item,1);
        add_item(head, tail, string, count);
        ++expcount;
-       }
+    }
+    PerlMem_free(vmsspec);
     if (sts != RMS$_NMF)
        {
        set_vaxc_errno(sts);
@@ -5435,7 +7350,7 @@ pipe_and_fork(pTHX_ char **cmargv)
 
 static int background_process(pTHX_ int argc, char **argv)
 {
-char command[2048] = "$";
+char command[MAX_DCL_SYMBOL + 1] = "$";
 $DESCRIPTOR(value, "");
 static $DESCRIPTOR(cmd, "BACKGROUND$COMMAND");
 static $DESCRIPTOR(null, "NLA0:");
@@ -5444,13 +7359,16 @@ char pidstring[80];
 $DESCRIPTOR(pidstr, "");
 int pid;
 unsigned long int flags = 17, one = 1, retsts;
+int len;
 
     strcat(command, argv[0]);
-    while (--argc)
+    len = strlen(command);
+    while (--argc && (len < MAX_DCL_SYMBOL))
        {
        strcat(command, " \"");
        strcat(command, *(++argv));
        strcat(command, "\"");
+       len = strlen(command);
        }
     value.dsc$a_pointer = command;
     value.dsc$w_length = strlen(value.dsc$a_pointer);
@@ -5484,6 +7402,8 @@ unsigned long int flags = 17, one = 1, retsts;
 #ifndef KGB$M_SUBSYSTEM
 #  define KGB$M_SUBSYSTEM 0x8
 #endif
+/* Avoid Newx() in vms_image_init as thread context has not been initialized. */
 
 /*{{{void vms_image_init(int *, char ***)*/
 void
@@ -5533,8 +7453,9 @@ vms_image_init(int *argcp, char ***argvp)
                          "Check your rights database for corruption.\n");
          exit(SS$_ABORT);
       }
-      if (jpilist[1].bufadr != rlst) Safefree(jpilist[1].bufadr);
-      jpilist[1].bufadr = Newx(mask,rsz,unsigned long int);
+      if (jpilist[1].bufadr != rlst) PerlMem_free(jpilist[1].bufadr);
+      jpilist[1].bufadr = mask = (unsigned long int *) PerlMem_malloc(rsz * sizeof(unsigned long int));
+      if (mask == NULL) _ckvmssts_noperl(SS$_INSFMEM);
       jpilist[1].buflen = rsz * sizeof(unsigned long int);
       _ckvmssts_noperl(sys$getjpiw(0,NULL,NULL,&jpilist[1],iosb,NULL,NULL));
       _ckvmssts_noperl(iosb[0]);
@@ -5549,7 +7470,7 @@ vms_image_init(int *argcp, char ***argvp)
         break;
       }
     }
-    if (mask != rlst) Safefree(mask);
+    if (mask != rlst) PerlMem_free(mask);
   }
 
   /* When Perl is in decc_filename_unix_report mode and is run from a concealed
@@ -5585,9 +7506,11 @@ vms_image_init(int *argcp, char ***argvp)
   if (will_taint) {
     char **newargv, **oldargv;
     oldargv = *argvp;
-    Newx(newargv,(*argcp)+2,char *);
+    newargv = (char **) PerlMem_malloc(((*argcp)+2) * sizeof(char *));
+    if (newargv == NULL) _ckvmssts_noperl(SS$_INSFMEM);
     newargv[0] = oldargv[0];
-    Newx(newargv[1],3,char);
+    newargv[1] = PerlMem_malloc(3 * sizeof(char));
+    if (newargv[1] == NULL) _ckvmssts_noperl(SS$_INSFMEM);
     strcpy(newargv[1], "-T");
     Copy(&oldargv[1],&newargv[2],(*argcp)-1,char **);
     (*argcp)++;
@@ -5614,12 +7537,18 @@ vms_image_init(int *argcp, char ***argvp)
   for (tabidx = 0;
        len = my_trnlnm("PERL_ENV_TABLES",eqv,tabidx);
        tabidx++) {
-    if (!tabidx) Newx(tabvec,tabct,struct dsc$descriptor_s *);
+    if (!tabidx) {
+      tabvec = (struct dsc$descriptor_s **)
+           PerlMem_malloc(tabct * sizeof(struct dsc$descriptor_s *));
+      if (tabvec == NULL) _ckvmssts_noperl(SS$_INSFMEM);
+    }
     else if (tabidx >= tabct) {
       tabct += 8;
-      Renew(tabvec,tabct,struct dsc$descriptor_s *);
+      tabvec = (struct dsc$descriptor_s **) PerlMem_realloc(tabvec, tabct * sizeof(struct dsc$descriptor_s *));
+      if (tabvec == NULL) _ckvmssts_noperl(SS$_INSFMEM);
     }
-    Newx(tabvec[tabidx],1,struct dsc$descriptor_s);
+    tabvec[tabidx] = (struct dsc$descriptor_s *) PerlMem_malloc(sizeof(struct dsc$descriptor_s));
+    if (tabvec[tabidx] == NULL) _ckvmssts_noperl(SS$_INSFMEM);
     tabvec[tabidx]->dsc$w_length  = 0;
     tabvec[tabidx]->dsc$b_dtype   = DSC$K_DTYPE_T;
     tabvec[tabidx]->dsc$b_class   = DSC$K_CLASS_D;
@@ -5659,21 +7588,32 @@ vms_image_init(int *argcp, char ***argvp)
 int
 Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
 {
-  char unixified[NAM$C_MAXRSS+1], unixwild[NAM$C_MAXRSS+1],
+  char *unixified, *unixwild,
        *template, *base, *end, *cp1, *cp2;
   register int tmplen, reslen = 0, dirs = 0;
 
+  unixwild = PerlMem_malloc(VMS_MAXRSS);
+  if (unixwild == NULL) _ckvmssts(SS$_INSFMEM);
   if (!wildspec || !fspec) return 0;
   template = unixwild;
   if (strpbrk(wildspec,"]>:") != NULL) {
-    if (do_tounixspec(wildspec,unixwild,0) == NULL) return 0;
+    if (do_tounixspec(wildspec,unixwild,0) == NULL) {
+        PerlMem_free(unixwild);
+       return 0;
+    }
   }
   else {
-    strncpy(unixwild, wildspec, NAM$C_MAXRSS);
-    unixwild[NAM$C_MAXRSS] = 0;
+    strncpy(unixwild, wildspec, VMS_MAXRSS-1);
+    unixwild[VMS_MAXRSS-1] = 0;
   }
+  unixified = PerlMem_malloc(VMS_MAXRSS);
+  if (unixified == NULL) _ckvmssts(SS$_INSFMEM);
   if (strpbrk(fspec,"]>:") != NULL) {
-    if (do_tounixspec(fspec,unixified,0) == NULL) return 0;
+    if (do_tounixspec(fspec,unixified,0) == NULL) {
+        PerlMem_free(unixwild);
+        PerlMem_free(unixified);
+       return 0;
+    }
     else base = unixified;
     /* reslen != 0 ==> we had to unixify resultant filespec, so we must
      * check to see that final result fits into (isn't longer than) fspec */
@@ -5683,11 +7623,19 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
 
   /* No prefix or absolute path on wildcard, so nothing to remove */
   if (!*template || *template == '/') {
-    if (base == fspec) return 1;
+    PerlMem_free(unixwild);
+    if (base == fspec) {
+        PerlMem_free(unixified);
+       return 1;
+    }
     tmplen = strlen(unixified);
-    if (tmplen > reslen) return 0;  /* not enough space */
+    if (tmplen > reslen) {
+        PerlMem_free(unixified);
+       return 0;  /* not enough space */
+    }
     /* Copy unixified resultant, including trailing NUL */
     memmove(fspec,unixified,tmplen+1);
+    PerlMem_free(unixified);
     return 1;
   }
 
@@ -5698,18 +7646,22 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
       if ((*cp1 == '/') && !dirs--) /* postdec so we get front of rel path */
         { cp1++; break; }
     if (cp1 != fspec) memmove(fspec,cp1, end - cp1 + 1);
+    PerlMem_free(unixified);
+    PerlMem_free(unixwild);
     return 1;
   }
   else {
-    char tpl[NAM$C_MAXRSS+1], lcres[NAM$C_MAXRSS+1];
+    char *tpl, *lcres;
     char *front, *nextell, *lcend, *lcfront, *ellipsis = cp1;
     int ells = 1, totells, segdirs, match;
-    struct dsc$descriptor_s wilddsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, tpl},
+    struct dsc$descriptor_s wilddsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, NULL},
                             resdsc =  {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
 
     while ((cp1 = strstr(ellipsis+4,".../")) != NULL) {ellipsis = cp1; ells++;}
     totells = ells;
     for (cp1 = ellipsis+4; *cp1; cp1++) if (*cp1 == '/') dirs++;
+    tpl = PerlMem_malloc(VMS_MAXRSS);
+    if (tpl == NULL) _ckvmssts(SS$_INSFMEM);
     if (ellipsis == template && opts & 1) {
       /* Template begins with an ellipsis.  Since we can't tell how many
        * directory names at the front of the resultant to keep for an
@@ -5719,7 +7671,12 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
        * ellipsis weren't there (i.e. return shortest possible path that
        * could match template).
        */
-      if (getcwd(tpl, sizeof tpl,0) == NULL) return 0;
+      if (getcwd(tpl, (VMS_MAXRSS - 1),0) == NULL) {
+         PerlMem_free(tpl);
+         PerlMem_free(unixified);
+         PerlMem_free(unixwild);
+         return 0;
+      }
       if (!decc_efs_case_preserve) {
        for (cp1 = tpl, cp2 = base; *cp1 && *cp2; cp1++,cp2++)
          if (_tolower(*cp1) != _tolower(*cp2)) break;
@@ -5727,7 +7684,10 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
       segdirs = dirs - totells;  /* Min # of dirs we must have left */
       for (front = cp2+1; *front; front++) if (*front == '/') segdirs--;
       if (*cp1 == '\0' && *cp2 == '/' && segdirs < 1) {
-        memcpy(fspec,cp2+1,end - cp2);
+        memmove(fspec,cp2+1,end - cp2);
+       PerlMem_free(tpl);
+       PerlMem_free(unixified);
+       PerlMem_free(unixwild);
         return 1;
       }
     }
@@ -5736,11 +7696,24 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
       for (front = end ; front >= base; front--)
          if (*front == '/' && !dirs--) { front++; break; }
     }
-    if (!decc_efs_case_preserve) {
-      for (cp1=template,cp2=lcres; *cp1 && cp2 <= lcres + sizeof lcres;
-         cp1++,cp2++) *cp2 = _tolower(*cp1);  /* Make lc copy for match */
+    lcres = PerlMem_malloc(VMS_MAXRSS);
+    if (lcres == NULL) _ckvmssts(SS$_INSFMEM);
+    for (cp1=template,cp2=lcres; *cp1 && cp2 <= lcres + (VMS_MAXRSS - 1);
+         cp1++,cp2++) {
+           if (!decc_efs_case_preserve) {
+               *cp2 = _tolower(*cp1);  /* Make lc copy for match */
+           }
+           else {
+               *cp2 = *cp1;
+           }
+    }
+    if (cp1 != '\0') {
+       PerlMem_free(tpl);
+       PerlMem_free(unixified);
+       PerlMem_free(unixwild);
+       PerlMem_free(lcres);
+       return 0;  /* Path too long. */
     }
-    if (cp1 != '\0') return 0;  /* Path too long. */
     lcend = cp2;
     *cp2 = '\0';  /* Pick up with memcpy later */
     lcfront = lcres + (front - base);
@@ -5753,10 +7726,11 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
       if (cp1 + 4 == ellipsis) { /* Consecutive ellipses */
         ellipsis = cp1; continue;
       }
+      wilddsc.dsc$a_pointer = tpl;
       wilddsc.dsc$w_length = resdsc.dsc$w_length = ellipsis - 1 - cp1;
       nextell = cp1;
       for (segdirs = 0, cp2 = tpl;
-           cp1 <= ellipsis - 1 && cp2 <= tpl + sizeof tpl;
+           cp1 <= ellipsis - 1 && cp2 <= tpl + (VMS_MAXRSS-1);
            cp1++, cp2++) {
          if (*cp1 == '?') *cp2 = '%'; /* Substitute VMS' wildcard for Unix' */
          else {
@@ -5769,7 +7743,13 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
         }
          if (*cp2 == '/') segdirs++;
       }
-      if (cp1 != ellipsis - 1) return 0; /* Path too long */
+      if (cp1 != ellipsis - 1) {
+         PerlMem_free(tpl);
+         PerlMem_free(unixified);
+         PerlMem_free(unixwild);
+         PerlMem_free(lcres);
+         return 0; /* Path too long */
+      }
       /* Back up at least as many dirs as in template before matching */
       for (cp1 = lcfront - 1; segdirs && cp1 >= lcres; cp1--)
         if (*cp1 == '/' && !segdirs--) { cp1++; break; }
@@ -5781,7 +7761,13 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
         }
         for ( ; cp1 >= lcres; cp1--) if (*cp1 == '/') { cp1++; break; }
       }
-      if (!match) return 0;  /* Can't find prefix ??? */
+      if (!match) {
+       PerlMem_free(tpl);
+       PerlMem_free(unixified);
+       PerlMem_free(unixwild);
+       PerlMem_free(lcres);
+       return 0;  /* Can't find prefix ??? */
+      }
       if (match > 1 && opts & 1) {
         /* This ... wildcard could cover more than one set of dirs (i.e.
          * a set of similar dir names is repeated).  If the template
@@ -5793,7 +7779,13 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
          */
         char def[NAM$C_MAXRSS+1], *st;
 
-        if (getcwd(def, sizeof def,0) == NULL) return 0;
+        if (getcwd(def, sizeof def,0) == NULL) {
+           Safefree(unixified);
+           Safefree(unixwild);
+           Safefree(lcres);
+           Safefree(tpl);
+           return 0;
+       }
        if (!decc_efs_case_preserve) {
          for (cp1 = def, cp2 = base; *cp1 && *cp2; cp1++,cp2++)
            if (_tolower(*cp1) != _tolower(*cp2)) break;
@@ -5801,13 +7793,21 @@ Perl_trim_unixpath(pTHX_ char *fspec, const char *wildspec, int opts)
         segdirs = dirs - totells;  /* Min # of dirs we must have left */
         for (st = cp2+1; *st; st++) if (*st == '/') segdirs--;
         if (*cp1 == '\0' && *cp2 == '/') {
-          memcpy(fspec,cp2+1,end - cp2);
+          memmove(fspec,cp2+1,end - cp2);
+         PerlMem_free(tpl);
+         PerlMem_free(unixified);
+         PerlMem_free(unixwild);
+         PerlMem_free(lcres);
           return 1;
         }
         /* Nope -- stick with lcfront from above and keep going. */
       }
     }
-    memcpy(fspec,base + (lcfront - lcres), lcend - lcfront + 1);
+    memmove(fspec,base + (lcfront - lcres), lcend - lcfront + 1);
+    PerlMem_free(tpl);
+    PerlMem_free(unixified);
+    PerlMem_free(unixwild);
+    PerlMem_free(lcres);
     return 1;
     ellipsis = nextell;
   }
@@ -5847,10 +7847,18 @@ DIR *
 Perl_opendir(pTHX_ const char *name)
 {
     DIR *dd;
-    char dir[NAM$C_MAXRSS+1];
+    char *dir;
     Stat_t sb;
+    int unix_flag;
+
+    unix_flag = 0;
+    if (decc_efs_charset) {
+        unix_flag = is_unix_filespec(name);
+    }
 
+    Newx(dir, VMS_MAXRSS, char);
     if (do_tovmspath(name,dir,0) == NULL) {
+      Safefree(dir);
       return NULL;
     }
     /* Check access before stat; otherwise stat does not
@@ -5858,10 +7866,12 @@ Perl_opendir(pTHX_ const char *name)
      */
     if (!cando_by_name(S_IRUSR,0,dir)) {
       /* cando_by_name has already set errno */
+      Safefree(dir);
       return NULL;
     }
     if (flex_stat(dir,&sb) == -1) return NULL;
     if (!S_ISDIR(sb.st_mode)) {
+      Safefree(dir);
       set_errno(ENOTDIR);  set_vaxc_errno(RMS$_DIR);
       return NULL;
     }
@@ -5871,9 +7881,12 @@ Perl_opendir(pTHX_ const char *name)
 
     /* Fill in the fields; mainly playing with the descriptor. */
     sprintf(dd->pattern, "%s*.*",dir);
+    Safefree(dir);
     dd->context = 0;
     dd->count = 0;
-    dd->vms_wantversions = 0;
+    dd->flags = 0;
+    if (unix_flag)
+       dd->flags = PERL_VMSDIR_M_UNIXSPECS;
     dd->pat.dsc$a_pointer = dd->pattern;
     dd->pat.dsc$w_length = strlen(dd->pattern);
     dd->pat.dsc$b_dtype = DSC$K_DTYPE_T;
@@ -5896,7 +7909,10 @@ Perl_opendir(pTHX_ const char *name)
 void
 vmsreaddirversions(DIR *dd, int flag)
 {
-    dd->vms_wantversions = flag;
+    if (flag)
+       dd->flags |= PERL_VMSDIR_M_VERSIONS;
+    else
+       dd->flags &= ~PERL_VMSDIR_M_VERSIONS;
 }
 /*}}}*/
 
@@ -5905,7 +7921,7 @@ vmsreaddirversions(DIR *dd, int flag)
  */
 /*{{{ void closedir(DIR *dd)*/
 void
-closedir(DIR *dd)
+Perl_closedir(DIR *dd)
 {
     int sts;
 
@@ -5928,7 +7944,7 @@ collectversions(pTHX_ DIR *dd)
     struct dsc$descriptor_s    pat;
     struct dsc$descriptor_s    res;
     struct dirent *e;
-    char *p, *text, buff[sizeof dd->entry.d_name];
+    char *p, *text, *buff;
     int i;
     unsigned long context, tmpsts;
 
@@ -5948,8 +7964,9 @@ collectversions(pTHX_ DIR *dd)
     pat.dsc$b_class = DSC$K_CLASS_S;
 
     /* Set up result descriptor. */
+    Newx(buff, VMS_MAXRSS, char);
     res.dsc$a_pointer = buff;
-    res.dsc$w_length = sizeof buff - 2;
+    res.dsc$w_length = VMS_MAXRSS - 1;
     res.dsc$b_dtype = DSC$K_DTYPE_T;
     res.dsc$b_class = DSC$K_CLASS_S;
 
@@ -5957,10 +7974,16 @@ collectversions(pTHX_ DIR *dd)
     for (context = 0, e->vms_verscount = 0;
          e->vms_verscount < VERSIZE(e);
          e->vms_verscount++) {
-       tmpsts = lib$find_file(&pat, &res, &context);
+       unsigned long rsts;
+       unsigned long flags = 0;
+
+#ifdef VMS_LONGNAME_SUPPORT
+       flags = LIB$M_FIL_LONG_NAMES
+#endif
+       tmpsts = lib$find_file(&pat, &res, &context, NULL, NULL, &rsts, &flags);
        if (tmpsts == RMS$_NMF || context == 0) break;
        _ckvmssts(tmpsts);
-       buff[sizeof buff - 1] = '\0';
+       buff[VMS_MAXRSS - 1] = '\0';
        if ((p = strchr(buff, ';')))
            e->vms_versions[e->vms_verscount] = atoi(p + 1);
        else
@@ -5969,6 +7992,7 @@ collectversions(pTHX_ DIR *dd)
 
     _ckvmssts(lib$find_file_end(&context));
     Safefree(text);
+    Safefree(buff);
 
 }  /* end of collectversions() */
 
@@ -5980,15 +8004,26 @@ struct dirent *
 Perl_readdir(pTHX_ DIR *dd)
 {
     struct dsc$descriptor_s    res;
-    char *p, buff[sizeof dd->entry.d_name];
+    char *p, *buff;
     unsigned long int tmpsts;
+    unsigned long rsts;
+    unsigned long flags = 0;
+    char * v_spec, * r_spec, * d_spec, * n_spec, * e_spec, * vs_spec;
+    int sts, v_len, r_len, d_len, n_len, e_len, vs_len;
 
     /* Set up result descriptor, and get next file. */
+    Newx(buff, VMS_MAXRSS, char);
     res.dsc$a_pointer = buff;
-    res.dsc$w_length = sizeof buff - 2;
+    res.dsc$w_length = VMS_MAXRSS - 1;
     res.dsc$b_dtype = DSC$K_DTYPE_T;
     res.dsc$b_class = DSC$K_CLASS_S;
-    tmpsts = lib$find_file(&dd->pat, &res, &dd->context);
+
+#ifdef VMS_LONGNAME_SUPPORT
+    flags = LIB$M_FIL_LONG_NAMES
+#endif
+
+    tmpsts = lib$find_file
+       (&dd->pat, &res, &dd->context, NULL, NULL, &rsts, &flags);
     if ( tmpsts == RMS$_NMF || dd->context == 0) return NULL;  /* None left. */
     if (!(tmpsts & 1)) {
       set_vaxc_errno(tmpsts);
@@ -6004,34 +8039,78 @@ Perl_readdir(pTHX_ DIR *dd)
         default:
           set_errno(EVMSERR);
       }
+      Safefree(buff);
       return NULL;
     }
     dd->count++;
     /* Force the buffer to end with a NUL, and downcase name to match C convention. */
     if (!decc_efs_case_preserve) {
-      buff[sizeof buff - 1] = '\0';
+      buff[VMS_MAXRSS - 1] = '\0';
       for (p = buff; *p; p++) *p = _tolower(*p);
-      while (--p >= buff) if (!isspace(*p)) break; /* Do we really need this? */
-      *p = '\0';
     }
     else {
       /* we don't want to force to lowercase, just null terminate */
       buff[res.dsc$w_length] = '\0';
     }
-    for (p = buff; *p; p++) *p = _tolower(*p);
     while (--p >= buff) if (!isspace(*p)) break;  /* Do we really need this? */
     *p = '\0';
 
     /* Skip any directory component and just copy the name. */
-    if ((p = strchr(buff, ']'))) strcpy(dd->entry.d_name, p + 1);
-    else strcpy(dd->entry.d_name, buff);
+    sts = vms_split_path
+       (aTHX_ buff,
+       &v_spec,
+       &v_len,
+       &r_spec,
+       &r_len,
+       &d_spec,
+       &d_len,
+       &n_spec,
+       &n_len,
+       &e_spec,
+       &e_len,
+       &vs_spec,
+       &vs_len);
+
+    /* Drop NULL extensions on UNIX file specification */
+    if ((dd->flags & PERL_VMSDIR_M_UNIXSPECS &&
+       (e_len == 1) && decc_readdir_dropdotnotype)) {
+       e_len = 0;
+       e_spec[0] = '\0';
+    }
+
+    strncpy(dd->entry.d_name, n_spec, n_len + e_len);
+    dd->entry.d_name[n_len + e_len] = '\0';
+    dd->entry.d_namlen = strlen(dd->entry.d_name);
 
-    /* Clobber the version. */
-    if ((p = strchr(dd->entry.d_name, ';'))) *p = '\0';
+    /* Convert the filename to UNIX format if needed */
+    if (dd->flags & PERL_VMSDIR_M_UNIXSPECS) {
+
+       /* Translate the encoded characters. */
+       /* Fixme: unicode handling could result in embedded 0 characters */
+       if (strchr(dd->entry.d_name, '^') != NULL) {
+           char new_name[256];
+           char * q;
+           int cnt;
+           p = dd->entry.d_name;
+           q = new_name;
+           while (*p != 0) {
+               int x, y;
+               x = copy_expand_vms_filename_escape(q, p, &y);
+               p += x;
+               q += y;
+               /* fix-me */
+               /* if y > 1, then this is a wide file specification */
+               /* Wide file specifications need to be passed in Perl */
+               /* counted strings apparently with a unicode flag */
+           }
+           *q = 0;
+           strcpy(dd->entry.d_name, new_name);
+       }
+    }
 
-    dd->entry.d_namlen = strlen(dd->entry.d_name);
     dd->entry.vms_verscount = 0;
-    if (dd->vms_wantversions) collectversions(aTHX_ dd);
+    if (dd->flags & PERL_VMSDIR_M_VERSIONS) collectversions(aTHX_ dd);
+    Safefree(buff);
     return &dd->entry;
 
 }  /* end of readdir() */
@@ -6064,7 +8143,7 @@ Perl_readdir_r(pTHX_ DIR *dd, struct dirent *entry, struct dirent **result)
  */
 /*{{{ long telldir(DIR *dd)*/
 long
-telldir(DIR *dd)
+Perl_telldir(DIR *dd)
 {
     return dd->count;
 }
@@ -6077,15 +8156,15 @@ telldir(DIR *dd)
 void
 Perl_seekdir(pTHX_ DIR *dd, long count)
 {
-    int vms_wantversions;
+    int old_flags;
 
     /* If we haven't done anything yet... */
     if (dd->count == 0)
        return;
 
     /* Remember some state, and clear it. */
-    vms_wantversions = dd->vms_wantversions;
-    dd->vms_wantversions = 0;
+    old_flags = dd->flags;
+    dd->flags &= ~PERL_VMSDIR_M_VERSIONS;
     _ckvmssts(lib$find_file_end(&dd->context));
     dd->context = 0;
 
@@ -6093,7 +8172,7 @@ Perl_seekdir(pTHX_ DIR *dd, long count)
     for (dd->count = 0; dd->count < count; )
        readdir(dd);
 
-    dd->vms_wantversions = vms_wantversions;
+    dd->flags = old_flags;
 
 }  /* end of seekdir() */
 /*}}}*/
@@ -6145,9 +8224,9 @@ vms_execfree(struct dsc$descriptor_s *vmscmd)
 {
   if (vmscmd) {
       if (vmscmd->dsc$a_pointer) {
-          Safefree(vmscmd->dsc$a_pointer);
+          PerlMem_free(vmscmd->dsc$a_pointer);
       }
-      Safefree(vmscmd);
+      PerlMem_free(vmscmd);
   }
 }
 
@@ -6175,7 +8254,7 @@ setup_argstr(pTHX_ SV *really, SV **mark, SV **sp)
       cmdlen += rlen ? rlen + 1 : 0;
     }
   }
-  Newx(PL_Cmd,cmdlen+1,char);
+  Newx(PL_Cmd, cmdlen+1, char);
 
   if (tmps && *tmps) {
     strcpy(PL_Cmd,tmps);
@@ -6200,6 +8279,8 @@ setup_cmddsc(pTHX_ const char *incmd, int check_img, int *suggest_quote,
                    struct dsc$descriptor_s **pvmscmd)
 {
   char vmsspec[NAM$C_MAXRSS+1], resspec[NAM$C_MAXRSS+1];
+  char image_name[NAM$C_MAXRSS+1];
+  char image_argv[NAM$C_MAXRSS+1];
   $DESCRIPTOR(defdsc,".EXE");
   $DESCRIPTOR(defdsc2,".");
   $DESCRIPTOR(resdsc,resspec);
@@ -6211,13 +8292,17 @@ setup_cmddsc(pTHX_ const char *incmd, int check_img, int *suggest_quote,
   int cmdlen;
   register int isdcl;
 
-  Newx(vmscmd,sizeof(struct dsc$descriptor_s),struct dsc$descriptor_s);
+  vmscmd = PerlMem_malloc(sizeof(struct dsc$descriptor_s));
+  if (vmscmd == NULL) _ckvmssts(SS$_INSFMEM);
 
   /* Make a copy for modification */
   cmdlen = strlen(incmd);
-  Newx(cmd, cmdlen+1, char);
+  cmd = PerlMem_malloc(cmdlen+1);
+  if (cmd == NULL) _ckvmssts(SS$_INSFMEM);
   strncpy(cmd, incmd, cmdlen);
   cmd[cmdlen] = 0;
+  image_name[0] = 0;
+  image_argv[0] = 0;
 
   vmscmd->dsc$a_pointer = NULL;
   vmscmd->dsc$b_dtype  = DSC$K_DTYPE_T;
@@ -6228,8 +8313,8 @@ setup_cmddsc(pTHX_ const char *incmd, int check_img, int *suggest_quote,
   if (suggest_quote) *suggest_quote = 0;
 
   if (strlen(cmd) > MAX_DCL_LINE_LENGTH) {
+    PerlMem_free(cmd);
     return CLI$_BUFOVF;                /* continuation lines currently unsupported */
-    Safefree(cmd);
   }
 
   s = cmd;
@@ -6277,21 +8362,22 @@ setup_cmddsc(pTHX_ const char *incmd, int check_img, int *suggest_quote,
   }
 
   if (!isdcl) {
+    int rsts;
     imgdsc.dsc$a_pointer = s;
     imgdsc.dsc$w_length = wordbreak - s;
-    retsts = lib$find_file(&imgdsc,&resdsc,&cxt,&defdsc,0,0,&flags);
+    retsts = lib$find_file(&imgdsc,&resdsc,&cxt,&defdsc,0,&rsts,&flags);
     if (!(retsts&1)) {
         _ckvmssts(lib$find_file_end(&cxt));
-        retsts = lib$find_file(&imgdsc,&resdsc,&cxt,&defdsc2,0,0,&flags);
-    if (!(retsts & 1) && *s == '$') {
-          _ckvmssts(lib$find_file_end(&cxt));
-      imgdsc.dsc$a_pointer++; imgdsc.dsc$w_length--;
-      retsts = lib$find_file(&imgdsc,&resdsc,&cxt,&defdsc,0,0,&flags);
-          if (!(retsts&1)) {
-      _ckvmssts(lib$find_file_end(&cxt));
-            retsts = lib$find_file(&imgdsc,&resdsc,&cxt,&defdsc2,0,0,&flags);
-          }
-    }
+        retsts = lib$find_file(&imgdsc,&resdsc,&cxt,&defdsc2,0,&rsts,&flags);
+      if (!(retsts & 1) && *s == '$') {
+        _ckvmssts(lib$find_file_end(&cxt));
+       imgdsc.dsc$a_pointer++; imgdsc.dsc$w_length--;
+       retsts = lib$find_file(&imgdsc,&resdsc,&cxt,&defdsc,0,&rsts,&flags);
+       if (!(retsts&1)) {
+         _ckvmssts(lib$find_file_end(&cxt));
+          retsts = lib$find_file(&imgdsc,&resdsc,&cxt,&defdsc2,0,&rsts,&flags);
+        }
+      }
     }
     _ckvmssts(lib$find_file_end(&cxt));
 
@@ -6302,43 +8388,169 @@ setup_cmddsc(pTHX_ const char *incmd, int check_img, int *suggest_quote,
       *s = '\0';
 
       /* check that it's really not DCL with no file extension */
-      fp = fopen(resspec,"r","ctx=bin","shr=get");
+      fp = fopen(resspec,"r","ctx=bin","ctx=rec","shr=get");
       if (fp) {
-        char b[4] = {0,0,0,0};
-        read(fileno(fp),b,4);
+        char b[256] = {0,0,0,0};
+        read(fileno(fp), b, 256);
         isdcl = isprint(b[0]) && isprint(b[1]) && isprint(b[2]) && isprint(b[3]);
+       if (isdcl) {
+         int shebang_len;
+
+         /* Check for script */
+         shebang_len = 0;
+         if ((b[0] == '#') && (b[1] == '!'))
+            shebang_len = 2;
+#ifdef ALTERNATE_SHEBANG
+         else {
+           shebang_len = strlen(ALTERNATE_SHEBANG);
+           if (strncmp(b, ALTERNATE_SHEBANG, shebang_len) == 0) {
+             char * perlstr;
+               perlstr = strstr("perl",b);
+               if (perlstr == NULL)
+                 shebang_len = 0;
+           }
+           else
+             shebang_len = 0;
+         }
+#endif
+
+         if (shebang_len > 0) {
+         int i;
+         int j;
+         char tmpspec[NAM$C_MAXRSS + 1];
+
+           i = shebang_len;
+            /* Image is following after white space */
+           /*--------------------------------------*/
+           while (isprint(b[i]) && isspace(b[i]))
+               i++;
+
+           j = 0;
+           while (isprint(b[i]) && !isspace(b[i])) {
+               tmpspec[j++] = b[i++];
+               if (j >= NAM$C_MAXRSS)
+                  break;
+           }
+           tmpspec[j] = '\0';
+
+            /* There may be some default parameters to the image */
+           /*---------------------------------------------------*/
+           j = 0;
+           while (isprint(b[i])) {
+               image_argv[j++] = b[i++];
+               if (j >= NAM$C_MAXRSS)
+                  break;
+           }
+           while ((j > 0) && !isprint(image_argv[j-1]))
+               j--;
+           image_argv[j] = 0;
+
+           /* It will need to be converted to VMS format and validated */
+           if (tmpspec[0] != '\0') {
+             char * iname;
+
+              /* Try to find the exact program requested to be run */
+             /*---------------------------------------------------*/
+             iname = do_rmsexpand
+                 (tmpspec, image_name, 0, ".exe", PERL_RMSEXPAND_M_VMS);
+             if (iname != NULL) {
+               if (cando_by_name(S_IXUSR,0,image_name)) {
+                 /* MCR prefix needed */
+                 isdcl = 0;
+               }
+               else {
+                  /* Try again with a null type */
+                 /*----------------------------*/
+                 iname = do_rmsexpand
+                   (tmpspec, image_name, 0, ".", PERL_RMSEXPAND_M_VMS);
+                 if (iname != NULL) {
+                   if (cando_by_name(S_IXUSR,0,image_name)) {
+                     /* MCR prefix needed */
+                     isdcl = 0;
+                   }
+                 }
+               }
+
+                /* Did we find the image to run the script? */
+               /*------------------------------------------*/
+               if (isdcl) {
+                 char *tchr;
+
+                  /* Assume DCL or foreign command exists */
+                 /*--------------------------------------*/
+                 tchr = strrchr(tmpspec, '/');
+                 if (tchr != NULL) {
+                   tchr++;
+                 }
+                 else {
+                   tchr = tmpspec;
+                 }
+                 strcpy(image_name, tchr);
+               }
+             }
+           }
+         }
+       }
         fclose(fp);
       }
       if (check_img && isdcl) return RMS$_FNF;
 
       if (cando_by_name(S_IXUSR,0,resspec)) {
-        Newx(vmscmd->dsc$a_pointer,7 + s - resspec + (rest ? strlen(rest) : 0),char);
+        vmscmd->dsc$a_pointer = PerlMem_malloc(MAX_DCL_LINE_LENGTH);
+       if (vmscmd->dsc$a_pointer == NULL) _ckvmssts(SS$_INSFMEM);
         if (!isdcl) {
             strcpy(vmscmd->dsc$a_pointer,"$ MCR ");
-            if (suggest_quote) *suggest_quote = 1;
+           if (image_name[0] != 0) {
+               strcat(vmscmd->dsc$a_pointer, image_name);
+               strcat(vmscmd->dsc$a_pointer, " ");
+           }
+       } else if (image_name[0] != 0) {
+           strcpy(vmscmd->dsc$a_pointer, image_name);
+           strcat(vmscmd->dsc$a_pointer, " ");
         } else {
             strcpy(vmscmd->dsc$a_pointer,"@");
-            if (suggest_quote) *suggest_quote = 1;
         }
-        strcat(vmscmd->dsc$a_pointer,resspec);
-        if (rest) strcat(vmscmd->dsc$a_pointer,rest);
+        if (suggest_quote) *suggest_quote = 1;
+
+       /* If there is an image name, use original command */
+       if (image_name[0] == 0)
+           strcat(vmscmd->dsc$a_pointer,resspec);
+       else {
+           rest = cmd;
+           while (*rest && isspace(*rest)) rest++;
+       }
+
+       if (image_argv[0] != 0) {
+         strcat(vmscmd->dsc$a_pointer,image_argv);
+         strcat(vmscmd->dsc$a_pointer, " ");
+       }
+        if (rest) {
+          int rest_len;
+          int vmscmd_len;
+
+          rest_len = strlen(rest);
+          vmscmd_len = strlen(vmscmd->dsc$a_pointer);
+          if ((rest_len + vmscmd_len) < MAX_DCL_LINE_LENGTH)
+             strcat(vmscmd->dsc$a_pointer,rest);
+          else
+            retsts = CLI$_BUFOVF;
+       }
         vmscmd->dsc$w_length = strlen(vmscmd->dsc$a_pointer);
-        Safefree(cmd);
+        PerlMem_free(cmd);
         return (vmscmd->dsc$w_length > MAX_DCL_LINE_LENGTH ? CLI$_BUFOVF : retsts);
       }
-      else retsts = RMS$_PRV;
+      else
+       retsts = RMS$_PRV;
     }
   }
   /* It's either a DCL command or we couldn't find a suitable image */
   vmscmd->dsc$w_length = strlen(cmd);
-/*  if (cmd == PL_Cmd) {
-      vmscmd->dsc$a_pointer = PL_Cmd;
-      if (suggest_quote) *suggest_quote = 1;
-  }
-  else  */
-      vmscmd->dsc$a_pointer = savepvn(cmd,vmscmd->dsc$w_length);
 
-  Safefree(cmd);
+  vmscmd->dsc$a_pointer = PerlMem_malloc(vmscmd->dsc$w_length);
+  strncpy(vmscmd->dsc$a_pointer,cmd,vmscmd->dsc$w_length);
+  vmscmd->dsc$a_pointer[vmscmd->dsc$w_length];
+
+  PerlMem_free(cmd);
 
   /* check if it's a symbol (for quoting purposes) */
   if (suggest_quote && !*suggest_quote) { 
@@ -6367,6 +8579,9 @@ setup_cmddsc(pTHX_ const char *incmd, int check_img, int *suggest_quote,
 bool
 Perl_vms_do_aexec(pTHX_ SV *really,SV **mark,SV **sp)
 {
+bool exec_sts;
+char * cmd;
+
   if (sp > mark) {
     if (vfork_called) {           /* this follows a vfork - act Unixish */
       vfork_called--;
@@ -6377,8 +8592,10 @@ Perl_vms_do_aexec(pTHX_ SV *really,SV **mark,SV **sp)
       else return do_aexec(really,mark,sp);
     }
                                            /* no vfork - act VMSish */
-    return vms_do_exec(setup_argstr(aTHX_ really,mark,sp));
-
+    cmd = setup_argstr(aTHX_ really,mark,sp);
+    exec_sts = vms_do_exec(cmd);
+    Safefree(cmd);  /* Clean up from setup_argstr() */
+    return exec_sts;
   }
 
   return FALSE;
@@ -6445,8 +8662,15 @@ unsigned long int Perl_do_spawn(pTHX_ const char *);
 unsigned long int
 Perl_do_aspawn(pTHX_ void *really,void **mark,void **sp)
 {
-  if (sp > mark) return do_spawn(setup_argstr(aTHX_ (SV *)really,(SV **)mark,(SV **)sp));
+unsigned long int sts;
+char * cmd;
 
+  if (sp > mark) {
+    cmd = setup_argstr(aTHX_ (SV *)really,(SV **)mark,(SV **)sp);
+    sts = do_spawn(cmd);
+    /* pp_sys will clean up cmd */
+    return sts;
+  }
   return SS$_ABORT;
 }  /* end of do_aspawn() */
 /*}}}*/
@@ -6457,6 +8681,9 @@ Perl_do_spawn(pTHX_ const char *cmd)
 {
   unsigned long int sts, substs;
 
+  /* The caller of this routine expects to Safefree(PL_Cmd) */
+  Newx(PL_Cmd,10,char);
+
   TAINT_ENV();
   TAINT_PROPER("spawn");
   if (!cmd || !*cmd) {
@@ -6514,14 +8741,14 @@ FILE *my_fdopen(int fd, const char *mode)
 
   if (fp) {
     unsigned int fdoff = fd / sizeof(unsigned int);
-    struct stat sbuf; /* native stat; we don't need flex_stat */
+    Stat_t sbuf; /* native stat; we don't need flex_stat */
     if (!sockflagsize || fdoff > sockflagsize) {
       if (sockflags) Renew(     sockflags,fdoff+2,unsigned int);
       else           Newx  (sockflags,fdoff+2,unsigned int);
       memset(sockflags+sockflagsize,0,fdoff + 2 - sockflagsize);
       sockflagsize = fdoff + 2;
     }
-    if (fstat(fd,&sbuf) == 0 && S_ISSOCK(sbuf.st_mode))
+    if (fstat(fd, (struct stat *)&sbuf) == 0 && S_ISSOCK(sbuf.st_mode))
       sockflags[fdoff] |= 1 << (fd % sizeof(unsigned int));
   }
   return fp;
@@ -7637,11 +9864,12 @@ int Perl_my_utime(pTHX_ const char *file, const struct utimbuf *utimes)
 /*}}}*/
 
 /*
- * flex_stat, flex_fstat
+ * flex_stat, flex_lstat, flex_fstat
  * basic stat, but gets it right when asked to stat
  * a Unix-style path ending in a directory name (e.g. dir1/dir2/dir3)
  */
 
+#ifndef _USE_STD_STAT
 /* encode_dev packs a VMS device name string into an integer to allow
  * simple comparisons. This can be used, for example, to check whether two
  * files are located on the same device, by comparing their encoded device
@@ -7716,6 +9944,7 @@ static mydev_t encode_dev (pTHX_ const char *dev)
   return (enc | LOCKID_MASK);  /* May have already overflowed into bit 31 */
 
 }  /* end of encode_dev() */
+#endif
 
 static char namecache[NAM$C_MAXRSS+1];
 
@@ -7723,6 +9952,10 @@ static int
 is_null_device(name)
     const char *name;
 {
+  if (decc_bug_devnull != 0) {
+    if (strncmp("/dev/null", name, 9) == 0)
+      return 1;
+  }
     /* The VMS null device is named "_NLA0:", usually abbreviated as "NL:".
        The underscore prefix, controller letter, and unit number are
        independently optional; for our purposes, the colon punctuation
@@ -7742,11 +9975,22 @@ is_null_device(name)
  * subset of the applicable information.
  */
 bool
-Perl_cando(pTHX_ Mode_t bit, Uid_t effective, const Stat_t *statbufp)
+Perl_cando(pTHX_ Mode_t bit, bool effective, const Stat_t *statbufp)
 {
   char fname_phdev[NAM$C_MAXRSS+1];
-  if (statbufp == &PL_statcache) return cando_by_name(bit,effective,namecache);
-  else {
+#if __CRTL_VER >= 80200000 && !defined(__VAX)
+  /* Namecache not workable with symbolic links, as symbolic links do
+   *  not have extensions and directories do in VMS mode.  So in order
+   *  to test this, the did and ino_t must be used.
+   *
+   * Fix-me - Hide the information in the new stat structure
+   *         Get rid of the namecache.
+   */
+  if (decc_posix_compliant_pathnames == 0)
+#endif
+      if (statbufp == &PL_statcache)
+         return cando_by_name(bit,effective,namecache);
+  {
     char fname[NAM$C_MAXRSS+1];
     unsigned long int retsts;
     struct dsc$descriptor_s devdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
@@ -7784,14 +10028,15 @@ Perl_cando(pTHX_ Mode_t bit, Uid_t effective, const Stat_t *statbufp)
 /*}}}*/
 
 
-/*{{{I32 cando_by_name(I32 bit, Uid_t effective, char *fname)*/
+/*{{{I32 cando_by_name(I32 bit, bool effective, char *fname)*/
 I32
-Perl_cando_by_name(pTHX_ I32 bit, Uid_t effective, const char *fname)
+Perl_cando_by_name(pTHX_ I32 bit, bool effective, const char *fname)
 {
   static char usrname[L_cuserid];
   static struct dsc$descriptor_s usrdsc =
          {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, usrname};
-  char vmsname[NAM$C_MAXRSS+1], fileified[NAM$C_MAXRSS+1];
+  char vmsname[NAM$C_MAXRSS+1];
+  char *fileified;
   unsigned long int objtyp = ACL$C_FILE, access, retsts, privused, iosb[2];
   unsigned short int retlen, trnlnm_iter_count;
   struct dsc$descriptor_s namdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
@@ -7807,6 +10052,7 @@ Perl_cando_by_name(pTHX_ I32 bit, Uid_t effective, const char *fname)
 
   if (!fname || !*fname) return FALSE;
   /* Make sure we expand logical names, since sys$check_access doesn't */
+  fileified = PerlMem_malloc(VMS_MAXRSS);
   if (!strpbrk(fname,"/]>:")) {
     strcpy(fileified,fname);
     trnlnm_iter_count = 0;
@@ -7816,7 +10062,10 @@ Perl_cando_by_name(pTHX_ I32 bit, Uid_t effective, const char *fname)
     }
     fname = fileified;
   }
-  if (!do_tovmsspec(fname,vmsname,1)) return FALSE;
+  if (!do_rmsexpand(fname, vmsname, 0, NULL, PERL_RMSEXPAND_M_VMS)) {
+    PerlMem_free(fileified);
+    return FALSE;
+  }
   retlen = namdsc.dsc$w_length = strlen(vmsname);
   namdsc.dsc$a_pointer = vmsname;
   if (vmsname[retlen-1] == ']' || vmsname[retlen-1] == '>' ||
@@ -7836,6 +10085,7 @@ Perl_cando_by_name(pTHX_ I32 bit, Uid_t effective, const char *fname)
     case S_IDUSR: case S_IDGRP: case S_IDOTH:
       access = ARM$M_DELETE; break;
     default:
+      PerlMem_free(fileified);
       return FALSE;
   }
 
@@ -7857,13 +10107,14 @@ Perl_cando_by_name(pTHX_ I32 bit, Uid_t effective, const char *fname)
                                     &usrprodsc.dsc$w_length,0));
 
   /* allocate space for the profile and get it filled in */
-  Newx(usrprodsc.dsc$a_pointer,usrprodsc.dsc$w_length,char);
+  usrprodsc.dsc$a_pointer = PerlMem_malloc(usrprodsc.dsc$w_length);
+  if (usrprodsc.dsc$a_pointer == NULL) _ckvmssts(SS$_INSFMEM);
   _ckvmssts(sys$create_user_profile(&usrdsc,&usrprolst,0,usrprodsc.dsc$a_pointer,
                                     &usrprodsc.dsc$w_length,0));
 
   /* use the profile to check access to the file; free profile & analyze results */
   retsts = sys$check_access(&objtyp,&namdsc,0,armlst,0,0,0,&usrprodsc);
-  Safefree(usrprodsc.dsc$a_pointer);
+  PerlMem_free(usrprodsc.dsc$a_pointer);
   if (retsts == SS$_NOCALLPRIV) retsts = SS$_NOPRIV; /* not really 3rd party */
 
 #else
@@ -7879,13 +10130,16 @@ Perl_cando_by_name(pTHX_ I32 bit, Uid_t effective, const char *fname)
     if (retsts == SS$_NOPRIV) set_errno(EACCES);
     else if (retsts == SS$_INVFILFOROP) set_errno(EINVAL);
     else set_errno(ENOENT);
+    PerlMem_free(fileified);
     return FALSE;
   }
   if (retsts == SS$_NORMAL || retsts == SS$_ACCONFLICT) {
+    PerlMem_free(fileified);
     return TRUE;
   }
   _ckvmssts(retsts);
 
+  PerlMem_free(fileified);
   return FALSE;  /* Should never get here */
 
 }  /* end of cando_by_name() */
@@ -7907,7 +10161,22 @@ Perl_flex_fstat(pTHX_ int fd, Stat_t *statbufp)
        if (cptr == NULL)
           namecache[0] = '\0';
     }
+
+    VMS_INO_T_COPY(statbufp->st_ino, statbufp->crtl_stat.st_ino);
+#ifndef _USE_STD_STAT
+    strncpy(statbufp->st_devnam, statbufp->crtl_stat.st_dev, 63);
+    statbufp->st_devnam[63] = 0;
     statbufp->st_dev = encode_dev(aTHX_ statbufp->st_devnam);
+#else
+    /* todo:
+     * The device is only encoded so that Perl_cando can use it to
+     * look up ACLS.  So rmsexpand it to the 255 character version
+     * and store it in ->st_devnam.  rmsexpand needs to be fixed
+     * for long filenames and symbolic links first.  This also seems
+     * to remove the need for a namecache that could be stale.
+     */
+#endif
+
 #   ifdef RTL_USES_UTC
 #   ifdef VMSISH_TIME
     if (VMSISH_TIME) {
@@ -7934,9 +10203,21 @@ Perl_flex_fstat(pTHX_ int fd, Stat_t *statbufp)
 }  /* end of flex_fstat() */
 /*}}}*/
 
-/*{{{ int flex_stat(const char *fspec, Stat_t *statbufp)*/
-int
-Perl_flex_stat(pTHX_ const char *fspec, Stat_t *statbufp)
+#if !defined(__VAX) && __CRTL_VER >= 80200000
+#ifdef lstat
+#undef lstat
+#endif
+#else
+#ifdef lstat
+#undef lstat
+#endif
+#define lstat(_x, _y) stat(_x, _y)
+#endif
+
+#define flex_stat_int(a,b,c)           Perl_flex_stat_int(aTHX_ a,b,c)
+
+static int
+Perl_flex_stat_int(pTHX_ const char *fspec, Stat_t *statbufp, int lstat_flag)
 {
     char fileified[NAM$C_MAXRSS+1];
     char temp_fspec[NAM$C_MAXRSS+300];
@@ -7948,15 +10229,17 @@ Perl_flex_stat(pTHX_ const char *fspec, Stat_t *statbufp)
     strcpy(temp_fspec, fspec);
     if (statbufp == (Stat_t *) &PL_statcache)
       do_tovmsspec(temp_fspec,namecache,0);
-    if (is_null_device(temp_fspec)) { /* Fake a stat() for the null device */
-      memset(statbufp,0,sizeof *statbufp);
-      statbufp->st_dev = encode_dev(aTHX_ "_NLA0:");
-      statbufp->st_mode = S_IFBLK | S_IREAD | S_IWRITE | S_IEXEC;
-      statbufp->st_uid = 0x00010001;
-      statbufp->st_gid = 0x0001;
-      time((time_t *)&statbufp->st_mtime);
-      statbufp->st_atime = statbufp->st_ctime = statbufp->st_mtime;
-      return 0;
+    if (decc_bug_devnull != 0) {
+      if (is_null_device(temp_fspec)) { /* Fake a stat() for the null device */
+       memset(statbufp,0,sizeof *statbufp);
+       statbufp->st_dev = encode_dev(aTHX_ "_NLA0:");
+       statbufp->st_mode = S_IFBLK | S_IREAD | S_IWRITE | S_IEXEC;
+       statbufp->st_uid = 0x00010001;
+       statbufp->st_gid = 0x0001;
+       time((time_t *)&statbufp->st_mtime);
+       statbufp->st_atime = statbufp->st_ctime = statbufp->st_mtime;
+       return 0;
+      }
     }
 
     /* Try for a directory name first.  If fspec contains a filename without
@@ -7966,15 +10249,49 @@ Perl_flex_stat(pTHX_ const char *fspec, Stat_t *statbufp)
      * not sea:[wine.dark]., if the latter exists.  If the intended target is
      * the file with null type, specify this by calling flex_stat() with
      * a '.' at the end of fspec.
+     *
+     * If we are in Posix filespec mode, accept the filename as is.
      */
+#if __CRTL_VER >= 80200000 && !defined(__VAX)
+  if (decc_posix_compliant_pathnames == 0) {
+#endif
     if (do_fileify_dirspec(temp_fspec,fileified,0) != NULL) {
-      retval = stat(fileified,(stat_t *) statbufp);
+      if (lstat_flag == 0)
+       retval = stat(fileified,(stat_t *) statbufp);
+      else
+       retval = lstat(fileified,(stat_t *) statbufp);
       if (!retval && statbufp == (Stat_t *) &PL_statcache)
         strcpy(namecache,fileified);
     }
-    if (retval) retval = stat(temp_fspec,(stat_t *) statbufp);
+    if (retval) {
+      if (lstat_flag == 0)
+       retval = stat(temp_fspec,(stat_t *) statbufp);
+      else
+       retval = lstat(temp_fspec,(stat_t *) statbufp);
+    }
+#if __CRTL_VER >= 80200000 && !defined(__VAX)
+  } else {
+    if (lstat_flag == 0)
+      retval = stat(temp_fspec,(stat_t *) statbufp);
+    else
+      retval = lstat(temp_fspec,(stat_t *) statbufp);
+  }
+#endif
     if (!retval) {
+      VMS_INO_T_COPY(statbufp->st_ino, statbufp->crtl_stat.st_ino);
+#ifndef _USE_STD_STAT
+      strncpy(statbufp->st_devnam, statbufp->crtl_stat.st_dev, 63);
+      statbufp->st_devnam[63] = 0;
       statbufp->st_dev = encode_dev(aTHX_ statbufp->st_devnam);
+#else
+    /* todo:
+     * The device is only encoded so that Perl_cando can use it to
+     * look up ACLS.  So rmsexpand it to the 255 character version
+     * and store it in ->st_devnam.  rmsexpand needs to be fixed
+     * for long filenames and symbolic links first.  This also seems
+     * to remove the need for a namecache that could be stale.
+     */
+#endif
 #     ifdef RTL_USES_UTC
 #     ifdef VMSISH_TIME
       if (VMSISH_TIME) {
@@ -7999,7 +10316,23 @@ Perl_flex_stat(pTHX_ const char *fspec, Stat_t *statbufp)
     if (retval == 0) { errno = saved_errno; vaxc$errno = saved_vaxc_errno; }
     return retval;
 
-}  /* end of flex_stat() */
+}  /* end of flex_stat_int() */
+
+
+/*{{{ int flex_stat(const char *fspec, Stat_t *statbufp)*/
+int
+Perl_flex_stat(pTHX_ const char *fspec, Stat_t *statbufp)
+{
+   return flex_stat_int(fspec, statbufp, 0);
+}
+/*}}}*/
+
+/*{{{ int flex_lstat(const char *fspec, Stat_t *statbufp)*/
+int
+Perl_flex_lstat(pTHX_ const char *fspec, Stat_t *statbufp)
+{
+   return flex_stat_int(fspec, statbufp, 1);
+}
 /*}}}*/
 
 
@@ -8037,8 +10370,9 @@ my_getlogin(void)
  * as part of the Perl standard distribution under the terms of the
  * GNU General Public License or the Perl Artistic License.  Copies
  * of each may be found in the Perl standard distribution.
- */
+ */ /* FIXME */
 /*{{{int rmscopy(char *src, char *dst, int preserve_dates)*/
+#if defined(__VAX) || !defined(NAML$C_MAXRSS)
 int
 Perl_rmscopy(pTHX_ const char *spec_in, const char *spec_out, int preserve_dates)
 {
@@ -8205,6 +10539,259 @@ Perl_rmscopy(pTHX_ const char *spec_in, const char *spec_out, int preserve_dates
     return 1;
 
 }  /* end of rmscopy() */
+#else
+/* ODS-5 support version */
+int
+Perl_rmscopy(pTHX_ const char *spec_in, const char *spec_out, int preserve_dates)
+{
+    char *vmsin, * vmsout, *esa, *esa_out,
+         *rsa, *ubf;
+    unsigned long int i, sts, sts2;
+    struct FAB fab_in, fab_out;
+    struct RAB rab_in, rab_out;
+    struct NAML nam;
+    struct NAML nam_out;
+    struct XABDAT xabdat;
+    struct XABFHC xabfhc;
+    struct XABRDT xabrdt;
+    struct XABSUM xabsum;
+
+    vmsin = PerlMem_malloc(VMS_MAXRSS);
+    if (vmsin == NULL) _ckvmssts(SS$_INSFMEM);
+    vmsout = PerlMem_malloc(VMS_MAXRSS);
+    if (vmsout == NULL) _ckvmssts(SS$_INSFMEM);
+    if (!spec_in  || !*spec_in  || !do_tovmsspec(spec_in,vmsin,1) ||
+        !spec_out || !*spec_out || !do_tovmsspec(spec_out,vmsout,1)) {
+      PerlMem_free(vmsin);
+      PerlMem_free(vmsout);
+      set_errno(EINVAL); set_vaxc_errno(LIB$_INVARG);
+      return 0;
+    }
+
+    esa = PerlMem_malloc(VMS_MAXRSS);
+    if (esa == NULL) _ckvmssts(SS$_INSFMEM);
+    nam = cc$rms_naml;
+    fab_in = cc$rms_fab;
+    fab_in.fab$l_fna = (char *) -1;
+    fab_in.fab$b_fns = 0;
+    nam.naml$l_long_filename = vmsin;
+    nam.naml$l_long_filename_size = strlen(vmsin);
+    fab_in.fab$b_shr = FAB$M_SHRPUT | FAB$M_UPI;
+    fab_in.fab$b_fac = FAB$M_BIO | FAB$M_GET;
+    fab_in.fab$l_fop = FAB$M_SQO;
+    fab_in.fab$l_naml =  &nam;
+    fab_in.fab$l_xab = (void *) &xabdat;
+
+    rsa = PerlMem_malloc(VMS_MAXRSS);
+    if (rsa == NULL) _ckvmssts(SS$_INSFMEM);
+    nam.naml$l_rsa = NULL;
+    nam.naml$b_rss = 0;
+    nam.naml$l_long_result = rsa;
+    nam.naml$l_long_result_alloc = VMS_MAXRSS - 1;
+    nam.naml$l_esa = NULL;
+    nam.naml$b_ess = 0;
+    nam.naml$l_long_expand = esa;
+    nam.naml$l_long_expand_alloc = VMS_MAXRSS - 1;
+    nam.naml$b_esl = nam.naml$b_rsl = 0;
+    nam.naml$l_long_expand_size = 0;
+    nam.naml$l_long_result_size = 0;
+#ifdef NAM$M_NO_SHORT_UPCASE
+    if (decc_efs_case_preserve)
+        nam.naml$b_nop |= NAM$M_NO_SHORT_UPCASE;
+#endif
+
+    xabdat = cc$rms_xabdat;        /* To get creation date */
+    xabdat.xab$l_nxt = (void *) &xabfhc;
+
+    xabfhc = cc$rms_xabfhc;        /* To get record length */
+    xabfhc.xab$l_nxt = (void *) &xabsum;
+
+    xabsum = cc$rms_xabsum;        /* To get key and area information */
+
+    if (!((sts = sys$open(&fab_in)) & 1)) {
+      PerlMem_free(vmsin);
+      PerlMem_free(vmsout);
+      PerlMem_free(esa);
+      PerlMem_free(rsa);
+      set_vaxc_errno(sts);
+      switch (sts) {
+        case RMS$_FNF: case RMS$_DNF:
+          set_errno(ENOENT); break;
+        case RMS$_DIR:
+          set_errno(ENOTDIR); break;
+        case RMS$_DEV:
+          set_errno(ENODEV); break;
+        case RMS$_SYN:
+          set_errno(EINVAL); break;
+        case RMS$_PRV:
+          set_errno(EACCES); break;
+        default:
+          set_errno(EVMSERR);
+      }
+      return 0;
+    }
+
+    nam_out = nam;
+    fab_out = fab_in;
+    fab_out.fab$w_ifi = 0;
+    fab_out.fab$b_fac = FAB$M_BIO | FAB$M_PUT;
+    fab_out.fab$b_shr = FAB$M_SHRGET | FAB$M_UPI;
+    fab_out.fab$l_fop = FAB$M_SQO;
+    fab_out.fab$l_naml = &nam_out;
+    fab_out.fab$l_fna = (char *) -1;
+    fab_out.fab$b_fns = 0;
+    nam_out.naml$l_long_filename = vmsout;
+    nam_out.naml$l_long_filename_size = strlen(vmsout);
+    fab_out.fab$l_dna = (char *) -1;
+    fab_out.fab$b_dns = 0;
+    nam_out.naml$l_long_defname = nam.naml$l_long_name;
+    nam_out.naml$l_long_defname_size =
+       nam.naml$l_long_name ?
+          nam.naml$l_long_name_size + nam.naml$l_long_type_size : 0;
+
+    esa_out = PerlMem_malloc(VMS_MAXRSS);
+    if (esa_out == NULL) _ckvmssts(SS$_INSFMEM);
+    nam_out.naml$l_rsa = NULL;
+    nam_out.naml$b_rss = 0;
+    nam_out.naml$l_long_result = NULL;
+    nam_out.naml$l_long_result_alloc = 0;
+    nam_out.naml$l_esa = NULL;
+    nam_out.naml$b_ess = 0;
+    nam_out.naml$l_long_expand = esa_out;
+    nam_out.naml$l_long_expand_alloc = VMS_MAXRSS - 1;
+
+    if (preserve_dates == 0) {  /* Act like DCL COPY */
+      nam_out.naml$b_nop |= NAM$M_SYNCHK;
+      fab_out.fab$l_xab = NULL;  /* Don't disturb data from input file */
+      if (!((sts = sys$parse(&fab_out)) & 1)) {
+       PerlMem_free(vmsin);
+       PerlMem_free(vmsout);
+       PerlMem_free(esa);
+       PerlMem_free(rsa);
+       PerlMem_free(esa_out);
+        set_errno(sts == RMS$_SYN ? EINVAL : EVMSERR);
+        set_vaxc_errno(sts);
+        return 0;
+      }
+      fab_out.fab$l_xab = (void *) &xabdat;
+      if (nam.naml$l_fnb & (NAM$M_EXP_NAME | NAM$M_EXP_TYPE)) preserve_dates = 1;
+    }
+    if (preserve_dates < 0)   /* Clear all bits; we'll use it as a */
+      preserve_dates =0;      /* bitmask from this point forward   */
+
+    if (!(preserve_dates & 1)) fab_out.fab$l_xab = (void *) &xabfhc;
+    if (!((sts = sys$create(&fab_out)) & 1)) {
+      PerlMem_free(vmsin);
+      PerlMem_free(vmsout);
+      PerlMem_free(esa);
+      PerlMem_free(rsa);
+      PerlMem_free(esa_out);
+      set_vaxc_errno(sts);
+      switch (sts) {
+        case RMS$_DNF:
+          set_errno(ENOENT); break;
+        case RMS$_DIR:
+          set_errno(ENOTDIR); break;
+        case RMS$_DEV:
+          set_errno(ENODEV); break;
+        case RMS$_SYN:
+          set_errno(EINVAL); break;
+        case RMS$_PRV:
+          set_errno(EACCES); break;
+        default:
+          set_errno(EVMSERR);
+      }
+      return 0;
+    }
+    fab_out.fab$l_fop |= FAB$M_DLT;  /* in case we have to bail out */
+    if (preserve_dates & 2) {
+      /* sys$close() will process xabrdt, not xabdat */
+      xabrdt = cc$rms_xabrdt;
+#ifndef __GNUC__
+      xabrdt.xab$q_rdt = xabdat.xab$q_rdt;
+#else
+      /* gcc doesn't like the assignment, since its prototype for xab$q_rdt
+       * is unsigned long[2], while DECC & VAXC use a struct */
+      memcpy(xabrdt.xab$q_rdt,xabdat.xab$q_rdt,sizeof xabrdt.xab$q_rdt);
+#endif
+      fab_out.fab$l_xab = (void *) &xabrdt;
+    }
+
+    ubf = PerlMem_malloc(32256);
+    if (ubf == NULL) _ckvmssts(SS$_INSFMEM);
+    rab_in = cc$rms_rab;
+    rab_in.rab$l_fab = &fab_in;
+    rab_in.rab$l_rop = RAB$M_BIO;
+    rab_in.rab$l_ubf = ubf;
+    rab_in.rab$w_usz = 32256;
+    if (!((sts = sys$connect(&rab_in)) & 1)) {
+      sys$close(&fab_in); sys$close(&fab_out);
+      PerlMem_free(vmsin);
+      PerlMem_free(vmsout);
+      PerlMem_free(esa);
+      PerlMem_free(ubf);
+      PerlMem_free(rsa);
+      PerlMem_free(esa_out);
+      set_errno(EVMSERR); set_vaxc_errno(sts);
+      return 0;
+    }
+
+    rab_out = cc$rms_rab;
+    rab_out.rab$l_fab = &fab_out;
+    rab_out.rab$l_rbf = ubf;
+    if (!((sts = sys$connect(&rab_out)) & 1)) {
+      sys$close(&fab_in); sys$close(&fab_out);
+      PerlMem_free(vmsin);
+      PerlMem_free(vmsout);
+      PerlMem_free(esa);
+      PerlMem_free(ubf);
+      PerlMem_free(rsa);
+      PerlMem_free(esa_out);
+      set_errno(EVMSERR); set_vaxc_errno(sts);
+      return 0;
+    }
+
+    while ((sts = sys$read(&rab_in))) {  /* always true  */
+      if (sts == RMS$_EOF) break;
+      rab_out.rab$w_rsz = rab_in.rab$w_rsz;
+      if (!(sts & 1) || !((sts = sys$write(&rab_out)) & 1)) {
+        sys$close(&fab_in); sys$close(&fab_out);
+       PerlMem_free(vmsin);
+       PerlMem_free(vmsout);
+       PerlMem_free(esa);
+       PerlMem_free(ubf);
+       PerlMem_free(rsa);
+       PerlMem_free(esa_out);
+        set_errno(EVMSERR); set_vaxc_errno(sts);
+        return 0;
+      }
+    }
+
+
+    fab_out.fab$l_fop &= ~FAB$M_DLT;  /* We got this far; keep the output */
+    sys$close(&fab_in);  sys$close(&fab_out);
+    sts = (fab_in.fab$l_sts & 1) ? fab_out.fab$l_sts : fab_in.fab$l_sts;
+    if (!(sts & 1)) {
+      PerlMem_free(vmsin);
+      PerlMem_free(vmsout);
+      PerlMem_free(esa);
+      PerlMem_free(ubf);
+      PerlMem_free(rsa);
+      PerlMem_free(esa_out);
+      set_errno(EVMSERR); set_vaxc_errno(sts);
+      return 0;
+    }
+
+    PerlMem_free(vmsin);
+    PerlMem_free(vmsout);
+    PerlMem_free(esa);
+    PerlMem_free(ubf);
+    PerlMem_free(rsa);
+    PerlMem_free(esa_out);
+    return 1;
+
+}  /* end of rmscopy() */
+#endif
 /*}}}*/
 
 
@@ -8356,7 +10943,7 @@ void
 rmscopy_fromperl(pTHX_ CV *cv)
 {
   dXSARGS;
-  char inspec[NAM$C_MAXRSS+1], outspec[NAM$C_MAXRSS+1], *inp, *outp;
+  char *inspec, *outspec, *inp, *outp;
   int date_flag;
   struct dsc$descriptor indsc  = { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
                         outdsc = { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
@@ -8369,10 +10956,12 @@ rmscopy_fromperl(pTHX_ CV *cv)
     Perl_croak(aTHX_ "Usage: File::Copy::rmscopy(from,to[,date_flag])");
 
   mysv = SvROK(ST(0)) ? SvRV(ST(0)) : ST(0);
+  Newx(inspec, VMS_MAXRSS, char);
   if (SvTYPE(mysv) == SVt_PVGV) {
     if (!(io = GvIOp(mysv)) || !PerlIO_getname(IoIFP(io),inspec)) {
       set_errno(EINVAL); set_vaxc_errno(LIB$_INVARG);
       ST(0) = &PL_sv_no;
+      Safefree(inspec);
       XSRETURN(1);
     }
     inp = inspec;
@@ -8381,14 +10970,18 @@ rmscopy_fromperl(pTHX_ CV *cv)
     if (mysv != ST(0) || !(inp = SvPV(mysv,n_a)) || !*inp) {
       set_errno(EINVAL); set_vaxc_errno(LIB$_INVARG);
       ST(0) = &PL_sv_no;
+      Safefree(inspec);
       XSRETURN(1);
     }
   }
   mysv = SvROK(ST(1)) ? SvRV(ST(1)) : ST(1);
+  Newx(outspec, VMS_MAXRSS, char);
   if (SvTYPE(mysv) == SVt_PVGV) {
     if (!(io = GvIOp(mysv)) || !PerlIO_getname(IoIFP(io),outspec)) {
       set_errno(EINVAL); set_vaxc_errno(LIB$_INVARG);
       ST(0) = &PL_sv_no;
+      Safefree(inspec);
+      Safefree(outspec);
       XSRETURN(1);
     }
     outp = outspec;
@@ -8397,16 +10990,22 @@ rmscopy_fromperl(pTHX_ CV *cv)
     if (mysv != ST(1) || !(outp = SvPV(mysv,n_a)) || !*outp) {
       set_errno(EINVAL); set_vaxc_errno(LIB$_INVARG);
       ST(0) = &PL_sv_no;
+      Safefree(inspec);
+      Safefree(outspec);
       XSRETURN(1);
     }
   }
   date_flag = (items == 3) ? SvIV(ST(2)) : 0;
 
   ST(0) = boolSV(rmscopy(inp,outp,date_flag));
+  Safefree(inspec);
+  Safefree(outspec);
   XSRETURN(1);
 }
 
-
+/* The mod2fname is limited to shorter filenames by design, so it should
+ * not be modified to support longer EFS pathnames
+ */
 void
 mod2fname(pTHX_ CV *cv)
 {
@@ -8494,6 +11093,210 @@ hushexit_fromperl(pTHX_ CV *cv)
     XSRETURN(1);
 }
 
+
+PerlIO * 
+Perl_vms_start_glob
+   (pTHX_ SV *tmpglob,
+    IO *io)
+{
+    PerlIO *fp;
+    struct vs_str_st *rslt;
+    char *vmsspec;
+    char *rstr;
+    char *begin, *cp;
+    $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
+    PerlIO *tmpfp;
+    STRLEN i;
+    struct dsc$descriptor_s wilddsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
+    struct dsc$descriptor_vs rsdsc;
+    unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0;
+    unsigned long hasver = 0, isunix = 0;
+    unsigned long int lff_flags = 0;
+    int rms_sts;
+
+#ifdef VMS_LONGNAME_SUPPORT
+    lff_flags = LIB$M_FIL_LONG_NAMES;
+#endif
+    /* The Newx macro will not allow me to assign a smaller array
+     * to the rslt pointer, so we will assign it to the begin char pointer
+     * and then copy the value into the rslt pointer.
+     */
+    Newx(begin, VMS_MAXRSS + sizeof(unsigned short int), char);
+    rslt = (struct vs_str_st *)begin;
+    rslt->length = 0;
+    rstr = &rslt->str[0];
+    rsdsc.dsc$a_pointer = (char *) rslt; /* cast required */
+    rsdsc.dsc$w_maxstrlen = VMS_MAXRSS + sizeof(unsigned short int);
+    rsdsc.dsc$b_dtype = DSC$K_DTYPE_VT;
+    rsdsc.dsc$b_class = DSC$K_CLASS_VS;
+
+    Newx(vmsspec, VMS_MAXRSS, char);
+
+       /* We could find out if there's an explicit dev/dir or version
+          by peeking into lib$find_file's internal context at
+          ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
+          but that's unsupported, so I don't want to do it now and
+          have it bite someone in the future. */
+       /* Fix-me: vms_split_path() is the only way to do this, the
+          existing method will fail with many legal EFS or UNIX specifications
+        */
+
+    cp = SvPV(tmpglob,i);
+
+    for (; i; i--) {
+       if (cp[i] == ';') hasver = 1;
+       if (cp[i] == '.') {
+           if (sts) hasver = 1;
+           else sts = 1;
+       }
+       if (cp[i] == '/') {
+           hasdir = isunix = 1;
+           break;
+       }
+       if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
+           hasdir = 1;
+           break;
+       }
+    }
+    if ((tmpfp = PerlIO_tmpfile()) != NULL) {
+       Stat_t st;
+       int stat_sts;
+       stat_sts = PerlLIO_stat(SvPVX_const(tmpglob),&st);
+       if (!stat_sts && S_ISDIR(st.st_mode)) {
+           wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec);
+           ok = (wilddsc.dsc$a_pointer != NULL);
+       }
+       else {
+           wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec);
+           ok = (wilddsc.dsc$a_pointer != NULL);
+       }
+       if (ok)
+           wilddsc.dsc$w_length = strlen(wilddsc.dsc$a_pointer);
+
+       /* If not extended character set, replace ? with % */
+       /* With extended character set, ? is a wildcard single character */
+       if (!decc_efs_case_preserve) {
+           for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
+               if (*cp == '?') *cp = '%';
+       }
+       sts = SS$_NORMAL;
+       while (ok && $VMS_STATUS_SUCCESS(sts)) {
+        char * v_spec, * r_spec, * d_spec, * n_spec, * e_spec, * vs_spec;
+        int v_sts, v_len, r_len, d_len, n_len, e_len, vs_len;
+
+           sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
+                               &dfltdsc,NULL,&rms_sts,&lff_flags);
+           if (!$VMS_STATUS_SUCCESS(sts))
+               break;
+
+           /* with varying string, 1st word of buffer contains result length */
+           rstr[rslt->length] = '\0';
+
+            /* Find where all the components are */
+            v_sts = vms_split_path
+                      (aTHX_ rstr,
+                       &v_spec,
+                       &v_len,
+                       &r_spec,
+                       &r_len,
+                       &d_spec,
+                       &d_len,
+                       &n_spec,
+                       &n_len,
+                       &e_spec,
+                       &e_len,
+                       &vs_spec,
+                       &vs_len);
+
+           /* If no version on input, truncate the version on output */
+           if (!hasver && (vs_len > 0)) {
+               *vs_spec = '\0';
+               vs_len = 0;
+
+               /* No version & a null extension on UNIX handling */
+               if (isunix && (e_len == 1) && decc_readdir_dropdotnotype) {
+                   e_len = 0;
+                   *e_spec = '\0';
+               }
+           }
+
+           if (!decc_efs_case_preserve) {
+               for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
+           }
+
+           if (hasdir) {
+               if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
+               begin = rstr;
+           }
+           else {
+               /* Start with the name */
+               begin = n_spec;
+           }
+           strcat(begin,"\n");
+           ok = (PerlIO_puts(tmpfp,begin) != EOF);
+       }
+       if (cxt) (void)lib$find_file_end(&cxt);
+       if (ok && sts != RMS$_NMF &&
+           sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
+       if (!ok) {
+           if (!(sts & 1)) {
+               SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
+           }
+           PerlIO_close(tmpfp);
+           fp = NULL;
+       }
+       else {
+           PerlIO_rewind(tmpfp);
+           IoTYPE(io) = IoTYPE_RDONLY;
+           IoIFP(io) = fp = tmpfp;
+           IoFLAGS(io) &= ~IOf_UNTAINT;  /* maybe redundant */
+       }
+    }
+    Safefree(vmsspec);
+    Safefree(rslt);
+    return fp;
+}
+
+#ifdef HAS_SYMLINK
+static char *
+mp_do_vms_realpath(pTHX_ const char *filespec, char * rslt_spec);
+
+void
+vms_realpath_fromperl(pTHX_ CV *cv)
+{
+  dXSARGS;
+  char *fspec, *rslt_spec, *rslt;
+  STRLEN n_a;
+
+  if (!items || items != 1)
+    Perl_croak(aTHX_ "Usage: VMS::Filespec::vms_realpath(spec)");
+
+  fspec = SvPV(ST(0),n_a);
+  if (!fspec || !*fspec) XSRETURN_UNDEF;
+
+  Newx(rslt_spec, VMS_MAXRSS + 1, char);
+  rslt = do_vms_realpath(fspec, rslt_spec);
+  ST(0) = sv_newmortal();
+  if (rslt != NULL)
+    sv_usepvn(ST(0),rslt,strlen(rslt));
+  else
+    Safefree(rslt_spec);
+  XSRETURN(1);
+}
+#endif
+
+#if __CRTL_VER >= 70301000 && !defined(__VAX)
+int do_vms_case_tolerant(void);
+
+void
+vms_case_tolerant_fromperl(pTHX_ CV *cv)
+{
+  dXSARGS;
+  ST(0) = boolSV(do_vms_case_tolerant());
+  XSRETURN(1);
+}
+#endif
+
 void  
 Perl_sys_intern_dup(pTHX_ struct interp_intern *src, 
                           struct interp_intern *dst)
@@ -8548,11 +11351,9 @@ init_os_extras(void)
 #ifdef HAS_SYMLINK
   newXSproto("VMS::Filespec::vms_realpath",vms_realpath_fromperl,file,"$;$");
 #endif
-#if 0 /* future */
 #if __CRTL_VER >= 70301000 && !defined(__VAX)
   newXSproto("VMS::Filespec::case_tolerant",vms_case_tolerant_fromperl,file,"$;$");
 #endif
-#endif
 
   store_pipelocs(aTHX);         /* will redo any earlier attempts */
 
@@ -8691,10 +11492,65 @@ static int set_features
     int dflt;
     char* str;
     char val_str[10];
+#if defined(JPI$_CASE_LOOKUP_PERM) && !defined(__VAX)
     const unsigned long int jpicode1 = JPI$_CASE_LOOKUP_PERM;
     const unsigned long int jpicode2 = JPI$_CASE_LOOKUP_IMAGE;
     unsigned long case_perm;
     unsigned long case_image;
+#endif
+
+    /* Allow an exception to bring Perl into the VMS debugger */
+    vms_debug_on_exception = 0;
+    status = sys_trnlnm("PERL_VMS_EXCEPTION_DEBUG", val_str, sizeof(val_str));
+    if ($VMS_STATUS_SUCCESS(status)) {
+       if ((val_str[0] == 'E') || (val_str[0] == '1') || (val_str[0] == 'T'))
+        vms_debug_on_exception = 1;
+       else
+        vms_debug_on_exception = 0;
+    }
+
+
+    /* hacks to see if known bugs are still present for testing */
+
+    /* Readdir is returning filenames in VMS syntax always */
+    decc_bug_readdir_efs1 = 1;
+    status = sys_trnlnm("DECC_BUG_READDIR_EFS1", val_str, sizeof(val_str));
+    if ($VMS_STATUS_SUCCESS(status)) {
+       if ((val_str[0] == 'E') || (val_str[0] == '1') || (val_str[0] == 'T'))
+         decc_bug_readdir_efs1 = 1;
+       else
+        decc_bug_readdir_efs1 = 0;
+    }
+
+    /* PCP mode requires creating /dev/null special device file */
+    decc_bug_devnull = 1;
+    status = sys_trnlnm("DECC_BUG_DEVNULL", val_str, sizeof(val_str));
+    if ($VMS_STATUS_SUCCESS(status)) {
+       if ((val_str[0] == 'E') || (val_str[0] == '1') || (val_str[0] == 'T'))
+          decc_bug_devnull = 1;
+       else
+         decc_bug_devnull = 0;
+    }
+
+    /* fgetname returning a VMS name in UNIX mode */
+    decc_bug_fgetname = 1;
+    status = sys_trnlnm("DECC_BUG_FGETNAME", val_str, sizeof(val_str));
+    if ($VMS_STATUS_SUCCESS(status)) {
+      if ((val_str[0] == 'E') || (val_str[0] == '1') || (val_str[0] == 'T'))
+       decc_bug_fgetname = 1;
+      else
+       decc_bug_fgetname = 0;
+    }
+
+    /* UNIX directory names with no paths are broken in a lot of places */
+    decc_dir_barename = 1;
+    status = sys_trnlnm("DECC_DIR_BARENAME", val_str, sizeof(val_str));
+    if ($VMS_STATUS_SUCCESS(status)) {
+      if ((val_str[0] == 'E') || (val_str[0] == '1') || (val_str[0] == 'T'))
+       decc_dir_barename = 1;
+      else
+       decc_dir_barename = 0;
+    }
 
 #if __CRTL_VER >= 70300000 && !defined(__VAX)
     s = decc$feature_get_index("DECC$DISABLE_TO_VMS_LOGNAME_TRANSLATION");
@@ -8834,7 +11690,7 @@ static int set_features
     }
 #endif
 
-#ifndef __VAX
+#if defined(JPI$_CASE_LOOKUP_PERM) && !defined(__VAX)
 
      /* Report true case tolerance */
     /*----------------------------*/