applied patch, with tab tweak suggest by Peter Prymmer
[p5sagit/p5-mst-13.2.git] / vms / vms.c
index ef00a60..46bad3b 100644 (file)
--- a/vms/vms.c
+++ b/vms/vms.c
@@ -2,8 +2,8 @@
  *
  * VMS-specific routines for perl5
  *
- * Last revised: 15-Feb-1997 by Charles Bailey  bailey@genetics.upenn.edu
- * Version: 5.3.27
+ * Last revised: 27-Feb-1998 by Charles Bailey  bailey@newman.upenn.edu
+ * Version: 5.4.61
  */
 
 #include <acedef.h>
@@ -11,6 +11,7 @@
 #include <armdef.h>
 #include <atrdef.h>
 #include <chpdef.h>
+#include <clidef.h>
 #include <climsgdef.h>
 #include <descrip.h>
 #include <dvidef.h>
@@ -19,6 +20,7 @@
 #include <fscndef.h>
 #include <iodef.h>
 #include <jpidef.h>
+#include <kgbdef.h>
 #include <libdef.h>
 #include <lib$routines.h>
 #include <lnmdef.h>
@@ -113,37 +115,48 @@ my_trnlnm(char *lnm, char *eqv, unsigned long int idx)
  * domain (mostly - my_getenv() need not return a translation from
  * the process logical name table)
  *
- * Note: Uses static buffer -- not thread-safe!
+ * Note: Uses Perl temp to store result so char * can be returned to
+ * caller; this pointer will be invalidated at next Perl statement
+ * transition.
  */
 /*{{{ char *my_getenv(char *lnm)*/
 char *
 my_getenv(char *lnm)
 {
     static char __my_getenv_eqv[LNM$C_NAMLENGTH+1];
-    char uplnm[LNM$C_NAMLENGTH+1], *cp1, *cp2;
+    char uplnm[LNM$C_NAMLENGTH+1], *cp1, *cp2, *eqv;
     unsigned long int idx = 0;
     int trnsuccess;
-
+    SV *tmpsv;
+
+    if (curinterp) {  /* Perl interpreter running -- may be threaded */
+      /* Set up a temporary buffer for the return value; Perl will
+       * clean it up at the next statement transition */
+      tmpsv = sv_2mortal(newSVpv("",LNM$C_NAMLENGTH+1));
+      if (!tmpsv) return NULL;
+      eqv = SvPVX(tmpsv);
+    }
+    else eqv = __my_getenv_eqv;  /* Assume no interpreter ==> single thread */
     for (cp1 = lnm, cp2= uplnm; *cp1; cp1++, cp2++) *cp2 = _toupper(*cp1);
     *cp2 = '\0';
     if (cp1 - lnm == 7 && !strncmp(uplnm,"DEFAULT",7)) {
-      getcwd(__my_getenv_eqv,sizeof __my_getenv_eqv);
-      return __my_getenv_eqv;
+      getcwd(eqv,LNM$C_NAMLENGTH);
+      return eqv;
     }
     else {
       if ((cp2 = strchr(uplnm,';')) != NULL) {
         *cp2 = '\0';
         idx = strtoul(cp2+1,NULL,0);
       }
-      trnsuccess = my_trnlnm(uplnm,__my_getenv_eqv,idx);
+      trnsuccess = my_trnlnm(uplnm,eqv,idx);
       /* If we had a translation index, we're only interested in lnms */
       if (!trnsuccess && cp2 != NULL) return Nullch;
-      if (trnsuccess) return __my_getenv_eqv;
+      if (trnsuccess) return eqv;
       else {
         unsigned long int retsts;
         struct dsc$descriptor_s symdsc = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0},
-                                valdsc = {sizeof __my_getenv_eqv,DSC$K_DTYPE_T,
-                                          DSC$K_CLASS_S, __my_getenv_eqv};
+                                valdsc = {LNM$C_NAMLENGTH,DSC$K_DTYPE_T,
+                                          DSC$K_CLASS_S, eqv};
         symdsc.dsc$w_length = cp1 - lnm;
         symdsc.dsc$a_pointer = uplnm;
         retsts = lib$get_symbol(&symdsc,&valdsc,&(valdsc.dsc$w_length),0);
@@ -162,7 +175,9 @@ my_getenv(char *lnm)
 }  /* end of my_getenv() */
 /*}}}*/
 
-static FILE *safe_popen(char *, char *);
+static void create_mbx(unsigned short int *, struct dsc$descriptor_s *);
+
+static void riseandshine(unsigned long int dummy) { sys$wake(0,0); }
 
 /*{{{ void prime_env_iter() */
 void
@@ -171,37 +186,81 @@ prime_env_iter(void)
  * find, in preparation for iterating over it.
  */
 {
-  static int primed = 0;  /* XXX Not thread-safe!!! */
+  dTHR;
+  static int primed = 0;
   HV *envhv = GvHVn(envgv);
-  FILE *sholog;
-  char eqv[LNM$C_NAMLENGTH+1],*start,*end;
+  PerlIO *sholog;
+  char eqv[LNM$C_NAMLENGTH+1],mbxnam[LNM$C_NAMLENGTH+1],*start,*end;
+  unsigned short int chan;
+#ifndef CLI$M_TRUSTED
+#  define CLI$M_TRUSTED 0x40  /* Missing from VAXC headers */
+#endif
+  unsigned long int flags = CLI$M_NOWAIT | CLI$M_NOCLISYM | CLI$M_NOKEYPAD | CLI$M_TRUSTED;
+  unsigned long int i, retsts, substs = 0, wakect = 0;
   STRLEN eqvlen;
   SV *oldrs, *linesv, *eqvsv;
+  $DESCRIPTOR(cmddsc,"Show Logical *"); $DESCRIPTOR(nldsc,"_NLA0:");
+  $DESCRIPTOR(clidsc,"DCL");            $DESCRIPTOR(tabdsc,"DCLTABLES");
+  $DESCRIPTOR(mbxdsc,mbxnam); 
+#ifdef USE_THREADS
+  static perl_mutex primenv_mutex;
+  MUTEX_INIT(&primenv_mutex);
+#endif
 
   if (primed) return;
+  MUTEX_LOCK(&primenv_mutex);
+  if (primed) { MUTEX_UNLOCK(&primenv_mutex); return; }
   /* Perform a dummy fetch as an lval to insure that the hash table is
-   * set up.  Otherwise, the hv_store() will turn into a nullop */
+   * set up.  Otherwise, the hv_store() will turn into a nullop. */
   (void) hv_fetch(envhv,"DEFAULT",7,TRUE);
-  /* Also, set up the four "special" keys that the CRTL defines,
-   * whether or not underlying logical names exist. */
-  (void) hv_fetch(envhv,"HOME",4,TRUE);
-  (void) hv_fetch(envhv,"TERM",4,TRUE);
-  (void) hv_fetch(envhv,"PATH",4,TRUE);
-  (void) hv_fetch(envhv,"USER",4,TRUE);
+  /* Also, set up any "special" keys that the CRTL defines,
+   * either by itself or becasue we were called from a C program
+   * using exec[lv]e() */
+  for (i = 0; environ[i]; i++) { 
+    if (!(start = strchr(environ[i],'='))) {
+      warn("Ill-formed CRTL environ value \"%s\"\n",environ[i]);
+    }
+    else {
+      start++;
+      (void) hv_store(envhv,environ[i],start - environ[i] - 1,newSVpv(start,0),0);
+    }
+  }
 
   /* Now, go get the logical names */
-  if ((sholog = safe_popen("$ Show Logical *","r")) == Nullfp)
-    _ckvmssts(vaxc$errno);
-  /* We use Perl's sv_gets to read from the pipe, since safe_popen is
+  create_mbx(&chan,&mbxdsc);
+  if ((sholog = PerlIO_open(mbxnam,"r")) != Nullfp) {
+    if ((retsts = sys$dassgn(chan)) & 1) {
+      /* Be certain that subprocess is using the CLI and command tables we
+       * expect, and don't pass symbols through so that we insure that
+       * "Show Logical" can't be subverted.
+       */
+      do {
+        retsts = lib$spawn(&cmddsc,&nldsc,&mbxdsc,&flags,0,0,&substs,
+                           0,&riseandshine,0,0,&clidsc,&tabdsc);
+        flags &= ~CLI$M_TRUSTED; /* Just in case we hit a really old version */
+      } while (retsts == LIB$_INVARG && (flags | CLI$M_TRUSTED));
+    }
+  }
+  if (sholog == Nullfp || !(retsts & 1)) {
+    if (sholog != Nullfp) PerlIO_close(sholog);
+    MUTEX_UNLOCK(&primenv_mutex);
+    _ckvmssts(sholog == Nullfp ? vaxc$errno : retsts);
+  }
+  /* We use Perl's sv_gets to read from the pipe, since PerlIO_open is
    * tied to Perl's I/O layer, so it may not return a simple FILE * */
   oldrs = rs;
   rs = newSVpv("\n",1);
   linesv = newSVpv("",0);
   while (1) {
     if ((start = sv_gets(linesv,sholog,0)) == Nullch) {
-      my_pclose(sholog);
+      PerlIO_close(sholog);
       SvREFCNT_dec(linesv); SvREFCNT_dec(rs); rs = oldrs;
       primed = 1;
+      /* Wait for subprocess to clean up (we know subproc won't return 0) */
+      while (substs == 0) { sys$hiber(); wakect++;}
+      if (wakect > 1) sys$wake(0,0);  /* Stole someone else's wake */
+      _ckvmssts(substs);
+      MUTEX_UNLOCK(&primenv_mutex);
       return;
     }
     while (*start != '"' && *start != '=' && *start) start++;
@@ -215,7 +274,7 @@ prime_env_iter(void)
           warn("Ill-formed logical name |%s| in prime_env_iter",start);
         continue;
       }
-      else _ckvmssts(vaxc$errno);
+      else { MUTEX_UNLOCK(&primenv_mutex); _ckvmssts(vaxc$errno); }
     }
     else {
       eqvsv = newSVpv(eqv,eqvlen);
@@ -335,7 +394,7 @@ do_rmdir(char *name)
 {
     char dirfile[NAM$C_MAXRSS+1];
     int retval;
-    struct mystat st;
+    Stat_t st;
 
     if (do_fileify_dirspec(name,dirfile,0) == NULL) return -1;
     if (flex_stat(dirfile,&st) || !S_ISDIR(st.st_mode)) retval = -1;
@@ -455,6 +514,28 @@ kill_file(char *name)
 }  /* end of kill_file() */
 /*}}}*/
 
+
+/*{{{int my_mkdir(char *,Mode_t)*/
+int
+my_mkdir(char *dir, Mode_t mode)
+{
+  STRLEN dirlen = strlen(dir);
+
+  /* CRTL mkdir() doesn't tolerate trailing /, since that implies
+   * null file name/type.  However, it's commonplace under Unix,
+   * so we'll allow it for a gain in portability.
+   */
+  if (dir[dirlen-1] == '/') {
+    char *newdir = savepvn(dir,dirlen-1);
+    int ret = mkdir(newdir,mode);
+    Safefree(newdir);
+    return ret;
+  }
+  else return mkdir(dir,mode);
+}  /* end of my_mkdir */
+/*}}}*/
+
+
 static void
 create_mbx(unsigned short int *chan, struct dsc$descriptor_s *namdsc)
 {
@@ -535,7 +616,7 @@ popen_completion_ast(struct pipe_details *thispipe)
   }
 }
 
-static FILE *
+static PerlIO *
 safe_popen(char *cmd, char *mode)
 {
     static int handler_set_up = FALSE;
@@ -556,7 +637,7 @@ safe_popen(char *cmd, char *mode)
       return Nullfp;
     }
 
-    New(7001,info,1,struct pipe_details);
+    New(1301,info,1,struct pipe_details);
 
     /* create mailbox */
     create_mbx(&chan,&namdsc);
@@ -752,30 +833,41 @@ my_gconvert(double val, int ndig, int trail, char *buf)
  * rmesexpand() returns the address of the resultant string if
  * successful, and NULL on error.
  */
+static char *do_tounixspec(char *, char *, int);
+
 static char *
 do_rmsexpand(char *filespec, char *outbuf, int ts, char *defspec, unsigned opts)
 {
   static char __rmsexpand_retbuf[NAM$C_MAXRSS+1];
+  char vmsfspec[NAM$C_MAXRSS+1], tmpfspec[NAM$C_MAXRSS+1];
   char esa[NAM$C_MAXRSS], *cp, *out = NULL;
   struct FAB myfab = cc$rms_fab;
   struct NAM mynam = cc$rms_nam;
   STRLEN speclen;
-  unsigned long int retsts, haslower = 0;
+  unsigned long int retsts, haslower = 0, isunix = 0;
 
   if (!filespec || !*filespec) {
     set_vaxc_errno(LIB$_INVARG); set_errno(EINVAL);
     return NULL;
   }
   if (!outbuf) {
-    if (ts) out = New(7019,outbuf,NAM$C_MAXRSS+1,char);
+    if (ts) out = New(1319,outbuf,NAM$C_MAXRSS+1,char);
     else    outbuf = __rmsexpand_retbuf;
   }
+  if ((isunix = (strchr(filespec,'/') != NULL))) {
+    if (do_tovmsspec(filespec,vmsfspec,0) == NULL) return NULL;
+    filespec = vmsfspec;
+  }
 
   myfab.fab$l_fna = filespec;
   myfab.fab$b_fns = strlen(filespec);
   myfab.fab$l_nam = &mynam;
 
   if (defspec && *defspec) {
+    if (strchr(defspec,'/') != NULL) {
+      if (do_tovmsspec(defspec,tmpfspec,0) == NULL) return NULL;
+      defspec = tmpfspec;
+    }
     myfab.fab$l_dna = defspec;
     myfab.fab$b_dns = strlen(defspec);
   }
@@ -787,12 +879,14 @@ do_rmsexpand(char *filespec, char *outbuf, int ts, char *defspec, unsigned opts)
 
   retsts = sys$parse(&myfab,0,0);
   if (!(retsts & 1)) {
+    mynam.nam$b_nop |= NAM$M_SYNCHK;
     if (retsts == RMS$_DNF || retsts == RMS$_DIR ||
         retsts == RMS$_DEV || retsts == RMS$_DEV) {
-      mynam.nam$b_nop |= NAM$M_SYNCHK;
       retsts = sys$parse(&myfab,0,0);
       if (retsts & 1) goto expanded;
     }  
+    mynam.nam$l_rlf = NULL; myfab.fab$b_dns = 0;
+    (void) sys$parse(&myfab,0,0);  /* Free search context */
     if (out) Safefree(out);
     set_vaxc_errno(retsts);
     if      (retsts == RMS$_PRV) set_errno(EACCES);
@@ -803,6 +897,8 @@ do_rmsexpand(char *filespec, char *outbuf, int ts, char *defspec, unsigned opts)
   }
   retsts = sys$search(&myfab,0,0);
   if (!(retsts & 1) && retsts != RMS$_FNF) {
+    mynam.nam$b_nop |= NAM$M_SYNCHK; mynam.nam$l_rlf = NULL;
+    myfab.fab$b_dns = 0; (void) sys$parse(&myfab,0,0);  /* Free search context */
     if (out) Safefree(out);
     set_vaxc_errno(retsts);
     if      (retsts == RMS$_PRV) set_errno(EACCES);
@@ -820,6 +916,10 @@ do_rmsexpand(char *filespec, char *outbuf, int ts, char *defspec, unsigned opts)
   if (!(mynam.nam$l_fnb & NAM$M_EXP_VER) &&
       (!defspec || !*defspec || !strchr(myfab.fab$l_dna,';')))
     speclen = mynam.nam$l_ver - out;
+  if (!(mynam.nam$l_fnb & NAM$M_EXP_TYPE) &&
+      (!defspec || !*defspec || defspec[myfab.fab$b_dns-1] != '.' ||
+       defspec[myfab.fab$b_dns-2] == '.'))
+    speclen = mynam.nam$l_type - out;
   /* If we just had a directory spec on input, $PARSE "helpfully"
    * adds an empty name and type for us */
   if (mynam.nam$l_name == mynam.nam$l_type &&
@@ -830,7 +930,20 @@ do_rmsexpand(char *filespec, char *outbuf, int ts, char *defspec, unsigned opts)
   if (haslower) __mystrtolower(out);
 
   /* Have we been working with an expanded, but not resultant, spec? */
-  if (!mynam.nam$b_rsl) strcpy(outbuf,esa);
+  /* Also, convert back to Unix syntax if necessary. */
+  if (!mynam.nam$b_rsl) {
+    if (isunix) {
+      if (do_tounixspec(esa,outbuf,0) == NULL) return NULL;
+    }
+    else strcpy(outbuf,esa);
+  }
+  else if (isunix) {
+    if (do_tounixspec(outbuf,tmpfspec,0) == NULL) return NULL;
+    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; (void) sys$parse(&myfab,0,0);  /* Free search context */
   return outbuf;
 }
 /*}}}*/
@@ -875,25 +988,26 @@ char *rmsexpand_ts(char *spec, char *buf, char *def, unsigned opt)
 ** found in the Perl standard distribution.
  */
 
-static char *do_tounixspec(char *, char *, int);
-
 /*{{{ char *fileify_dirspec[_ts](char *path, char *buf)*/
 static char *do_fileify_dirspec(char *dir,char *buf,int ts)
 {
     static char __fileify_retbuf[NAM$C_MAXRSS+1];
     unsigned long int dirlen, retlen, addmfd = 0, hasfilename = 0;
     char *retspec, *cp1, *cp2, *lastdir;
-    char trndir[NAM$C_MAXRSS+1], vmsdir[NAM$C_MAXRSS+1];
+    char trndir[NAM$C_MAXRSS+2], vmsdir[NAM$C_MAXRSS+1];
 
     if (!dir || !*dir) {
       set_errno(EINVAL); set_vaxc_errno(SS$_BADPARAM); return NULL;
     }
     dirlen = strlen(dir);
-    if (dir[dirlen-1] == '/') --dirlen;
-    if (!dirlen) {
-      set_errno(ENOTDIR);
-      set_vaxc_errno(RMS$_DIR);
-      return NULL;
+    while (dir[dirlen-1] == '/') --dirlen;
+    if (!dirlen) { /* We had Unixish '/' -- substitute top of current tree */
+      strcpy(trndir,"/sys$disk/000000");
+      dir = trndir;
+      dirlen = 16;
+    }
+    if (dirlen > NAM$C_MAXRSS) {
+      set_errno(ENAMETOOLONG); set_vaxc_errno(RMS$_SYN); return NULL;
     }
     if (!strpbrk(dir+1,"/]>:")) {
       strcpy(trndir,*dir == '/' ? dir + 1: dir);
@@ -954,11 +1068,28 @@ static char *do_fileify_dirspec(char *dir,char *buf,int ts)
           if (*(cp1+2) == '.') cp1++;
           if (*(cp1+2) == '/' || *(cp1+2) == '\0') {
             if (do_tovmsspec(dir,vmsdir,0) == NULL) 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;
+            }
             if (do_fileify_dirspec(vmsdir,trndir,0) == NULL) return NULL;
             return do_tounixspec(trndir,buf,ts);
           }
           cp1++;
         } while ((cp1 = strstr(cp1,"/.")) != NULL);
+        lastdir = strrchr(dir,'/');
+      }
+      else if (!strcmp(&dir[dirlen-7],"/000000")) {
+        /* Ditto for specs that end in an MFD -- let the VMS code
+         * figure out whether it's a real device or a rooted logical. */
+        dir[dirlen] = '/'; dir[dirlen+1] = '\0';
+        if (do_tovmsspec(dir,vmsdir,0) == NULL) return NULL;
+        if (do_fileify_dirspec(vmsdir,trndir,0) == NULL) return NULL;
+        return do_tounixspec(trndir,buf,ts);
       }
       else {
         if ( !(lastdir = cp1 = strrchr(dir,'/')) &&
@@ -992,7 +1123,7 @@ static char *do_fileify_dirspec(char *dir,char *buf,int ts)
       }
       retlen = dirlen + (addmfd ? 13 : 6);
       if (buf) retspec = buf;
-      else if (ts) New(7009,retspec,retlen+1,char);
+      else if (ts) New(1309,retspec,retlen+1,char);
       else retspec = __fileify_retbuf;
       if (addmfd) {
         dirlen = lastdir - dir;
@@ -1073,7 +1204,7 @@ static char *do_fileify_dirspec(char *dir,char *buf,int ts)
       if (dirnam.nam$l_fnb & 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) New(7011,retspec,dirnam.nam$b_esl+1,char);
+        else if (ts) New(1311,retspec,dirnam.nam$b_esl+1,char);
         else retspec = __fileify_retbuf;
         strcpy(retspec,esa);
         return retspec;
@@ -1092,7 +1223,7 @@ static char *do_fileify_dirspec(char *dir,char *buf,int ts)
         /* There's more than one directory in the path.  Just roll back. */
         *cp1 = term;
         if (buf) retspec = buf;
-        else if (ts) New(7011,retspec,retlen+7,char);
+        else if (ts) New(1311,retspec,retlen+7,char);
         else retspec = __fileify_retbuf;
         strcpy(retspec,esa);
       }
@@ -1107,7 +1238,7 @@ static char *do_fileify_dirspec(char *dir,char *buf,int ts)
           }
           retlen = dirnam.nam$b_esl - 9; /* esa - '][' - '].DIR;1' */
           if (buf) retspec = buf;
-          else if (ts) New(7012,retspec,retlen+16,char);
+          else if (ts) New(1312,retspec,retlen+16,char);
           else retspec = __fileify_retbuf;
           cp1 = strstr(esa,"][");
           dirlen = cp1 - esa;
@@ -1135,7 +1266,7 @@ static char *do_fileify_dirspec(char *dir,char *buf,int ts)
         }
         else {  /* This is a top-level dir.  Add the MFD to the path. */
           if (buf) retspec = buf;
-          else if (ts) New(7012,retspec,retlen+16,char);
+          else if (ts) New(1312,retspec,retlen+16,char);
           else retspec = __fileify_retbuf;
           cp1 = esa;
           cp2 = retspec;
@@ -1182,7 +1313,7 @@ static char *do_pathify_dirspec(char *dir,char *buf, int ts)
       /* Trap simple rooted lnms, and return lnm:[000000] */
       if (!strcmp(trndir+trnlen-2,".]")) {
         if (buf) retpath = buf;
-        else if (ts) New(7018,retpath,strlen(dir)+10,char);
+        else if (ts) New(1318,retpath,strlen(dir)+10,char);
         else retpath = __pathify_retbuf;
         strcpy(retpath,dir);
         strcat(retpath,":[000000]");
@@ -1222,7 +1353,7 @@ static char *do_pathify_dirspec(char *dir,char *buf, int ts)
         }
       }
       if (buf) retpath = buf;
-      else if (ts) New(7013,retpath,retlen+1,char);
+      else if (ts) New(1313,retpath,retlen+1,char);
       else retpath = __pathify_retbuf;
       strncpy(retpath,dir,retlen-1);
       if (retpath[retlen-2] != '/') { /* If the path doesn't already end */
@@ -1267,7 +1398,7 @@ static char *do_pathify_dirspec(char *dir,char *buf, int ts)
           dir[dirfab.fab$b_fns-1] == '>' ||
           dir[dirfab.fab$b_fns-1] == ':') { /* It's already a VMS 'path' */
         if (buf) retpath = buf;
-        else if (ts) New(7014,retpath,strlen(dir)+1,char);
+        else if (ts) New(1314,retpath,strlen(dir)+1,char);
         else retpath = __pathify_retbuf;
         strcpy(retpath,dir);
         return retpath;
@@ -1324,7 +1455,7 @@ static char *do_pathify_dirspec(char *dir,char *buf, int ts)
       *(dirnam.nam$l_type + 1) = '\0';
       retlen = dirnam.nam$l_type - esa + 2;
       if (buf) retpath = buf;
-      else if (ts) New(7014,retpath,retlen,char);
+      else if (ts) New(1314,retpath,retlen,char);
       else retpath = __pathify_retbuf;
       strcpy(retpath,esa);
       /* $PARSE may have upcased filespec, so convert output to lower
@@ -1362,7 +1493,7 @@ static char *do_tounixspec(char *spec, char *buf, int ts)
           { expand++; cp1 +=2; } /* VMS '...' ==> Unix '/.../' */
       }
     }
-    New(7015,rslt,retlen+2+2*expand,char);
+    New(1315,rslt,retlen+2+2*expand,char);
   }
   else rslt = __tounixspec_retbuf;
   if (strchr(spec,'/') != NULL) {
@@ -1478,7 +1609,7 @@ static char *do_tovmsspec(char *path, char *buf, int ts) {
 
   if (path == NULL) return NULL;
   if (buf) rslt = buf;
-  else if (ts) New(7016,rslt,strlen(path)+9,char);
+  else if (ts) New(1316,rslt,strlen(path)+9,char);
   else rslt = __tovmsspec_retbuf;
   if (strpbrk(path,"]:>") ||
       (dirend = strrchr(path,'/')) == NULL) {
@@ -1503,6 +1634,11 @@ static char *do_tovmsspec(char *path, char *buf, int ts) {
     STRLEN trnend;
 
     while (*(cp2+1) == '/') cp2++;  /* Skip multiple /s */
+    if (!*(cp2+1)) {
+      if (!buf & ts) Renew(rslt,18,char);
+      strcpy(rslt,"sys$disk:[000000]");
+      return rslt;
+    }
     while (*(++cp2) != '/' && *cp2) *(cp1++) = *cp2;
     *cp1 = '\0';
     islnm =  my_trnlnm(rslt,trndev,0);
@@ -1620,7 +1756,7 @@ static char *do_tovmspath(char *path, char *buf, int ts) {
   if (buf) return buf;
   else if (ts) {
     vmslen = strlen(vmsified);
-    New(7017,cp,vmslen+1,char);
+    New(1317,cp,vmslen+1,char);
     memcpy(cp,vmsified,vmslen);
     cp[vmslen] = '\0';
     return cp;
@@ -1649,7 +1785,7 @@ static char *do_tounixpath(char *path, char *buf, int ts) {
   if (buf) return buf;
   else if (ts) {
     unixlen = strlen(unixified);
-    New(7017,cp,unixlen+1,char);
+    New(1317,cp,unixlen+1,char);
     memcpy(cp,unixified,unixlen);
     cp[unixlen] = '\0';
     return cp;
@@ -1718,7 +1854,7 @@ static int background_process(int argc, char **argv);
 static void pipe_and_fork(char **cmargv);
 
 /*{{{ void getredirection(int *ac, char ***av)*/
-void
+static void
 getredirection(int *ac, char ***av)
 /*
  * Process vms redirection arg's.  Exit if any error is seen.
@@ -1867,7 +2003,7 @@ getredirection(int *ac, char ***av)
      * Allocate and fill in the new argument vector, Some Unix's terminate
      * the list with an extra null pointer.
      */
-    New(7002, argv, item_count+1, char *);
+    New(1302, argv, item_count+1, char *);
     *av = argv;
     for (j = 0; j < item_count; ++j, list_head = list_head->next)
        argv[j] = list_head->value;
@@ -1954,11 +2090,11 @@ static void add_item(struct list_item **head,
 {
     if (*head == 0)
        {
-       New(7003,*head,1,struct list_item);
+       New(1303,*head,1,struct list_item);
        *tail = *head;
        }
     else {
-       New(7004,(*tail)->next,1,struct list_item);
+       New(1304,(*tail)->next,1,struct list_item);
        *tail = (*tail)->next;
        }
     (*tail)->value = value;
@@ -2012,7 +2148,7 @@ unsigned long int zero = 0, sts;
        char *string;
        char *c;
 
-       New(7005,string,resultspec.dsc$w_length+1,char);
+       New(1305,string,resultspec.dsc$w_length+1,char);
        strncpy(string, resultspec.dsc$a_pointer, resultspec.dsc$w_length);
        string[resultspec.dsc$w_length] = '\0';
        if (NULL == had_version)
@@ -2180,6 +2316,90 @@ unsigned long int flags = 17, one = 1, retsts;
 /*}}}*/
 /***** End of code taken from Mark Pizzolato's argproc.c package *****/
 
+
+/* OS-specific initialization at image activation (not thread startup) */
+/* Older VAXC header files lack these constants */
+#ifndef JPI$_RIGHTS_SIZE
+#  define JPI$_RIGHTS_SIZE 817
+#endif
+#ifndef KGB$M_SUBSYSTEM
+#  define KGB$M_SUBSYSTEM 0x8
+#endif
+
+/*{{{void vms_image_init(int *, char ***)*/
+void
+vms_image_init(int *argcp, char ***argvp)
+{
+  unsigned long int *mask, iosb[2], i, rlst[128], rsz, add_taint = FALSE;
+  unsigned long int iprv[(sizeof(union prvdef) + sizeof(unsigned long int) - 1) / sizeof(unsigned long int)];
+  unsigned short int dummy, rlen;
+  struct itmlst_3 jpilist[4] = { {sizeof iprv,    JPI$_IMAGPRIV, iprv, &dummy},
+                                 {sizeof rlst,  JPI$_RIGHTSLIST, rlst,  &rlen},
+                                 { sizeof rsz, JPI$_RIGHTS_SIZE, &rsz, &dummy},
+                                 {          0,                0,    0,      0} };
+
+  _ckvmssts(sys$getjpiw(0,NULL,NULL,jpilist,iosb,NULL,NULL));
+  _ckvmssts(iosb[0]);
+  for (i = 0; i < sizeof iprv / sizeof(unsigned long int); i++) {
+    if (iprv[i]) {           /* Running image installed with privs? */
+      _ckvmssts(sys$setprv(0,iprv,0,NULL));       /* Turn 'em off. */
+      add_taint = TRUE;
+      break;
+    }
+  }
+  /* Rights identifiers might trigger tainting as well. */
+  if (!add_taint && (rlen || rsz)) {
+    while (rlen < rsz) {
+      /* We didn't get all the identifiers on the first pass.  Allocate a
+       * buffer much larger than $GETJPI wants (rsz is size in bytes that
+       * were needed to hold all identifiers at time of last call; we'll
+       * allocate that many unsigned long ints), and go back and get 'em.
+       */
+      if (jpilist[1].bufadr != rlst) Safefree(jpilist[1].bufadr);
+      jpilist[1].bufadr = New(1320,mask,rsz,unsigned long int);
+      jpilist[1].buflen = rsz * sizeof(unsigned long int);
+      _ckvmssts(sys$getjpiw(0,NULL,NULL,&jpilist[1],iosb,NULL,NULL));
+      _ckvmssts(iosb[0]);
+    }
+    mask = jpilist[1].bufadr;
+    /* Check attribute flags for each identifier (2nd longword); protected
+     * subsystem identifiers trigger tainting.
+     */
+    for (i = 1; i < (rlen + sizeof(unsigned long int) - 1) / sizeof(unsigned long int); i += 2) {
+      if (mask[i] & KGB$M_SUBSYSTEM) {
+        add_taint = TRUE;
+        break;
+      }
+    }
+    if (mask != rlst) Safefree(mask);
+  }
+  /* We need to use this hack to tell Perl it should run with tainting,
+   * since its tainting flag may be part of the curinterp struct, which
+   * hasn't been allocated when vms_image_init() is called.
+   */
+  if (add_taint) {
+    char ***newap;
+    New(1320,newap,*argcp+2,char **);
+    newap[0] = argvp[0];
+    *newap[1] = "-T";
+    Copy(argvp[1],newap[2],*argcp-1,char **);
+    /* We orphan the old argv, since we don't know where it's come from,
+     * so we don't know how to free it.
+     */
+    *argcp++; argvp = newap;
+  }
+  getredirection(argcp,argvp);
+#if defined(USE_THREADS) && defined(__DECC)
+  {
+# include <reentrancy.h>
+  (void) decc$set_reentrancy(C$C_MULTITHREAD);
+  }
+#endif
+  return;
+}
+/*}}}*/
+
+
 /* trim_unixpath()
  * Trim Unix-style prefix off filespec, so it looks like what a shell
  * glob expansion would return (i.e. from specified prefix on, not
@@ -2271,7 +2491,7 @@ trim_unixpath(char *fspec, char *wildspec, int opts)
       for (front = end ; front >= base; front--)
          if (*front == '/' && !dirs--) { front++; break; }
     }
-    for (cp1=template,cp2=lcres; *cp1 && cp2 <= lcend + sizeof lcend; 
+    for (cp1=template,cp2=lcres; *cp1 && cp2 <= lcres + sizeof lcres;
          cp1++,cp2++) *cp2 = _tolower(*cp1);  /* Make lc copy for match */
     if (cp1 != '\0') return 0;  /* Path too long. */
     lcend = cp2;
@@ -2360,14 +2580,23 @@ opendir(char *name)
 {
     DIR *dd;
     char dir[NAM$C_MAXRSS+1];
-      
-    /* Get memory for the handle, and the pattern. */
-    New(7006,dd,1,DIR);
+    Stat_t sb;
+
     if (do_tovmspath(name,dir,0) == NULL) {
-      Safefree((char *)dd);
-      return(NULL);
+      return NULL;
+    }
+    if (flex_stat(dir,&sb) == -1) return NULL;
+    if (!S_ISDIR(sb.st_mode)) {
+      set_errno(ENOTDIR);  set_vaxc_errno(RMS$_DIR);
+      return NULL;
+    }
+    if (!cando_by_name(S_IRUSR,0,dir)) {
+      set_errno(EACCES); set_vaxc_errno(RMS$_PRV);
+      return NULL;
     }
-    New(7007,dd->pattern,strlen(dir)+sizeof "*.*" + 1,char);
+    /* Get memory for the handle, and the pattern. */
+    New(1306,dd,1,DIR);
+    New(1307,dd->pattern,strlen(dir)+sizeof "*.*" + 1,char);
 
     /* Fill in the fields; mainly playing with the descriptor. */
     (void)sprintf(dd->pattern, "%s*.*",dir);
@@ -2426,7 +2655,7 @@ collectversions(dd)
 
     /* Add the version wildcard, ignoring the "*.*" put on before */
     i = strlen(dd->pattern);
-    New(7008,text,i + e->d_namlen + 3,char);
+    New(1308,text,i + e->d_namlen + 3,char);
     (void)strcpy(text, dd->pattern);
     (void)sprintf(&text[i - 3], "%s;*", e->d_name);
 
@@ -2614,6 +2843,7 @@ vms_execfree() {
 static char *
 setup_argstr(SV *really, SV **mark, SV **sp)
 {
+  dTHR;
   char *junk, *tmps = Nullch;
   register size_t cmdlen = 0;
   size_t rlen;
@@ -2704,6 +2934,7 @@ setup_cmddsc(char *cmd, int check_img)
       s = resspec;
       while (*s && !isspace(*s)) s++;
       *s = '\0';
+      if (!cando_by_name(S_IXUSR,0,resspec)) return RMS$_PRV;
       New(402,VMScmd.dsc$a_pointer,7 + s - resspec + (rest ? strlen(rest) : 0),char);
       strcpy(VMScmd.dsc$a_pointer,"$ MCR ");
       strcat(VMScmd.dsc$a_pointer,resspec);
@@ -2721,6 +2952,7 @@ setup_cmddsc(char *cmd, int check_img)
 bool
 vms_do_aexec(SV *really,SV **mark,SV **sp)
 {
+  dTHR;
   if (sp > mark) {
     if (vfork_called) {           /* this follows a vfork - act Unixish */
       vfork_called--;
@@ -2761,7 +2993,22 @@ vms_do_exec(char *cmd)
     if ((retsts = setup_cmddsc(cmd,1)) & 1)
       retsts = lib$do_command(&VMScmd);
 
-    set_errno(EVMSERR);
+    switch (retsts) {
+      case RMS$_FNF:
+        set_errno(ENOENT); break;
+      case RMS$_DNF: case RMS$_DIR: case RMS$_DEV:
+        set_errno(ENOTDIR); break;
+      case RMS$_PRV:
+        set_errno(EACCES); break;
+      case RMS$_SYN:
+        set_errno(EINVAL); break;
+      case CLI$_BUFOVF:
+        set_errno(E2BIG); break;
+      case LIB$_INVARG: case LIB$_INVSTRDES: case SS$_ACCVIO: /* shouldn't happen */
+        _ckvmssts(retsts); /* fall through */
+      default:  /* SS$_DUPLNAM, SS$_CLI, resource exhaustion, etc. */
+        set_errno(EVMSERR); 
+    }
     set_vaxc_errno(retsts);
     if (dowarn)
       warn("Can't exec \"%s\": %s", VMScmd.dsc$a_pointer, Strerror(errno));
@@ -2775,11 +3022,12 @@ vms_do_exec(char *cmd)
 
 unsigned long int do_spawn(char *);
 
-/* {{{ unsigned long int do_aspawn(SV *really,SV **mark,SV **sp) */
+/* {{{ unsigned long int do_aspawn(void *really,void **mark,void **sp) */
 unsigned long int
-do_aspawn(SV *really,SV **mark,SV **sp)
+do_aspawn(void *really,void **mark,void **sp)
 {
-  if (sp > mark) return do_spawn(setup_argstr(really,mark,sp));
+  dTHR;
+  if (sp > mark) return do_spawn(setup_argstr((SV *)really,(SV **)mark,(SV **)sp));
 
   return SS$_ABORT;
 }  /* end of do_aspawn() */
@@ -2789,21 +3037,36 @@ do_aspawn(SV *really,SV **mark,SV **sp)
 unsigned long int
 do_spawn(char *cmd)
 {
-  unsigned long int substs, hadcmd = 1;
+  unsigned long int sts, substs, hadcmd = 1;
 
   TAINT_ENV();
   TAINT_PROPER("spawn");
   if (!cmd || !*cmd) {
     hadcmd = 0;
-    _ckvmssts(lib$spawn(0,0,0,0,0,0,&substs,0,0,0,0,0,0));
+    sts = lib$spawn(0,0,0,0,0,0,&substs,0,0,0,0,0,0);
   }
-  else if ((substs = setup_cmddsc(cmd,0)) & 1) {
-    _ckvmssts(lib$spawn(&VMScmd,0,0,0,0,0,&substs,0,0,0,0,0,0));
+  else if ((sts = setup_cmddsc(cmd,0)) & 1) {
+    sts = lib$spawn(&VMScmd,0,0,0,0,0,&substs,0,0,0,0,0,0);
   }
   
-  if (!(substs&1)) {
-    set_errno(EVMSERR);
-    set_vaxc_errno(substs);
+  if (!(sts & 1)) {
+    switch (sts) {
+      case RMS$_FNF:
+        set_errno(ENOENT); break;
+      case RMS$_DNF: case RMS$_DIR: case RMS$_DEV:
+        set_errno(ENOTDIR); break;
+      case RMS$_PRV:
+        set_errno(EACCES); break;
+      case RMS$_SYN:
+        set_errno(EINVAL); break;
+      case CLI$_BUFOVF:
+        set_errno(E2BIG); break;
+      case LIB$_INVARG: case LIB$_INVSTRDES: case SS$_ACCVIO: /* shouldn't happen */
+        _ckvmssts(sts); /* fall through */
+      default:  /* SS$_DUPLNAM, SS$_CLI, resource exhaustion, etc. */
+        set_errno(EVMSERR); 
+    }
+    set_vaxc_errno(sts);
     if (dowarn)
       warn("Can't spawn \"%s\": %s",
            hadcmd ? VMScmd.dsc$a_pointer : "", Strerror(errno));
@@ -2839,6 +3102,22 @@ my_fwrite(void *src, size_t itmsz, size_t nitm, FILE *dest)
 }  /* end of my_fwrite() */
 /*}}}*/
 
+/*{{{ int my_flush(FILE *fp)*/
+int
+my_flush(FILE *fp)
+{
+    int res;
+    if ((res = fflush(fp)) == 0) {
+#ifdef VMS_DO_SOCKETS
+       Stat_t s;
+       if (Fstat(fileno(fp), &s) == 0 && !S_ISSOCK(s.st_mode))
+#endif
+           res = fsync(fileno(fp));
+    }
+    return res;
+}
+/*}}}*/
+
 /*
  * Here are replacements for the following Unix routines in the VMS environment:
  *      getpwuid    Get information for a particular UIC or UID
@@ -3040,7 +3319,7 @@ struct passwd *my_getpwuid(Uid_t uid)
     else {
       uic.uic$l_uic= uid;
       if (!uic.uic$v_group)
-        uic.uic$v_group= getgid();
+        uic.uic$v_group= PerlProc_getgid();
       if (valid_uic(uic))
         status = sys$idtoasc(uic.uic$l_uic, &lname, &name_desc, 0, 0, 0);
       else status = SS$_IVIDENT;
@@ -3092,6 +3371,104 @@ void my_endpwent()
 }
 /*}}}*/
 
+#ifdef HOMEGROWN_POSIX_SIGNALS
+  /* Signal handling routines, pulled into the core from POSIX.xs.
+   *
+   * We need these for threads, so they've been rolled into the core,
+   * rather than left in POSIX.xs.
+   *
+   * (DRS, Oct 23, 1997)
+   */
+
+  /* sigset_t is atomic under VMS, so these routines are easy */
+/*{{{int my_sigemptyset(sigset_t *) */
+int my_sigemptyset(sigset_t *set) {
+    if (!set) { SETERRNO(EFAULT,SS$_ACCVIO); return -1; }
+    *set = 0; return 0;
+}
+/*}}}*/
+
+
+/*{{{int my_sigfillset(sigset_t *)*/
+int my_sigfillset(sigset_t *set) {
+    int i;
+    if (!set) { SETERRNO(EFAULT,SS$_ACCVIO); return -1; }
+    for (i = 0; i < NSIG; i++) *set |= (1 << i);
+    return 0;
+}
+/*}}}*/
+
+
+/*{{{int my_sigaddset(sigset_t *set, int sig)*/
+int my_sigaddset(sigset_t *set, int sig) {
+    if (!set) { SETERRNO(EFAULT,SS$_ACCVIO); return -1; }
+    if (sig > NSIG) { SETERRNO(EINVAL,LIB$_INVARG); return -1; }
+    *set |= (1 << (sig - 1));
+    return 0;
+}
+/*}}}*/
+
+
+/*{{{int my_sigdelset(sigset_t *set, int sig)*/
+int my_sigdelset(sigset_t *set, int sig) {
+    if (!set) { SETERRNO(EFAULT,SS$_ACCVIO); return -1; }
+    if (sig > NSIG) { SETERRNO(EINVAL,LIB$_INVARG); return -1; }
+    *set &= ~(1 << (sig - 1));
+    return 0;
+}
+/*}}}*/
+
+
+/*{{{int my_sigismember(sigset_t *set, int sig)*/
+int my_sigismember(sigset_t *set, int sig) {
+    if (!set) { SETERRNO(EFAULT,SS$_ACCVIO); return -1; }
+    if (sig > NSIG) { SETERRNO(EINVAL,LIB$_INVARG); return -1; }
+    *set & (1 << (sig - 1));
+}
+/*}}}*/
+
+
+/*{{{int my_sigprocmask(int how, sigset_t *set, sigset_t *oset)*/
+int my_sigprocmask(int how, sigset_t *set, sigset_t *oset) {
+    sigset_t tempmask;
+
+    /* If set and oset are both null, then things are badly wrong. Bail out. */
+    if ((oset == NULL) && (set == NULL)) {
+      set_errno(EFAULT); set_vaxc_errno(SS$_ACCVIO);
+      return -1;
+    }
+
+    /* If set's null, then we're just handling a fetch. */
+    if (set == NULL) {
+        tempmask = sigblock(0);
+    }
+    else {
+      switch (how) {
+      case SIG_SETMASK:
+        tempmask = sigsetmask(*set);
+        break;
+      case SIG_BLOCK:
+        tempmask = sigblock(*set);
+        break;
+      case SIG_UNBLOCK:
+        tempmask = sigblock(0);
+        sigsetmask(*oset & ~tempmask);
+        break;
+      default:
+        set_errno(EINVAL); set_vaxc_errno(LIB$_INVARG);
+        return -1;
+      }
+    }
+
+    /* Did they pass us an oset? If so, stick our holding mask into it */
+    if (oset)
+      *oset = tempmask;
+  
+    return 0;
+}
+/*}}}*/
+#endif  /* HOMEGROWN_POSIX_SIGNALS */
+
 
 /* Used for UTC calculation in my_gmtime(), my_localtime(), my_time(),
  * my_utime(), and flex_stat(), all of which operate on UTC unless
@@ -3112,9 +3489,43 @@ static long int utc_offset_secs;
 #undef localtime
 #undef time
 
+#if defined(__VMS_VER) && __VMS_VER >= 70000000 && __DECC_VER >= 50200000
+#  define RTL_USES_UTC 1
+#endif
+
+static time_t toutc_dst(time_t loc) {
+  struct tm *rsltmp;
+
+  if ((rsltmp = localtime(&loc)) == NULL) return -1;
+  loc -= utc_offset_secs;
+  if (rsltmp->tm_isdst) loc -= 3600;
+  return loc;
+}
+#define _toutc(secs)  ((secs) == -1 ? -1 : \
+       ((gmtime_emulation_type || my_time(NULL)), \
+       (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
+       ((secs) - utc_offset_secs))))
+
+static time_t toloc_dst(time_t utc) {
+  struct tm *rsltmp;
+
+  utc += utc_offset_secs;
+  if ((rsltmp = localtime(&utc)) == NULL) return -1;
+  if (rsltmp->tm_isdst) utc += 3600;
+  return utc;
+}
+#define _toloc(secs)  ((secs) == -1 ? -1 : \
+       ((gmtime_emulation_type || my_time(NULL)), \
+       (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
+       ((secs) + utc_offset_secs))))
+
+
 /* my_time(), my_localtime(), my_gmtime()
- * By default traffic in UTC time values, suing CRTL gmtime() or
+ * By default traffic in UTC time values, using CRTL gmtime() or
  * SYS$TIMEZONE_DIFFERENTIAL to determine offset from local time zone.
+ * Note: We need to use these functions even when the CRTL has working
+ * UTC support, since they also handle C<use vmsish qw(times);>
+ *
  * Contributed by Chuck Lane  <lane@duphy4.physics.drexel.edu>
  * Modified by Charles Bailey <bailey@genetics.upenn.edu>
  */
@@ -3122,11 +3533,15 @@ static long int utc_offset_secs;
 /*{{{time_t my_time(time_t *timep)*/
 time_t my_time(time_t *timep)
 {
+  dTHR;
   time_t when;
+  struct tm *tm_p;
 
   if (gmtime_emulation_type == 0) {
-    struct tm *tm_p;
-    time_t base = 15 * 86400; /* 15jan71; to avoid month ends */
+    int dstnow;
+    time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between    */
+                              /* results of calls to gmtime() and localtime() */
+                              /* for same &base */
 
     gmtime_emulation_type++;
     if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
@@ -3153,11 +3568,13 @@ time_t my_time(time_t *timep)
   }
 
   when = time(NULL);
-  if (
-#     ifdef VMSISH_TIME
-      !VMSISH_TIME &&
-#     endif
-                       when != -1) when -= utc_offset_secs;
+# ifdef VMSISH_TIME
+# ifdef RTL_USES_UTC
+  if (VMSISH_TIME) when = _toloc(when);
+# else
+  if (!VMSISH_TIME) when = _toutc(when);
+# endif
+# endif
   if (timep != NULL) *timep = when;
   return when;
 
@@ -3169,18 +3586,29 @@ time_t my_time(time_t *timep)
 struct tm *
 my_gmtime(const time_t *timep)
 {
+  dTHR;
   char *p;
   time_t when;
+  struct tm *rsltmp;
 
-  if (gmtime_emulation_type == 0) (void) my_time(NULL); /* Init UTC */
+  if (timep == NULL) {
+    set_errno(EINVAL); set_vaxc_errno(LIB$_INVARG);
+    return NULL;
+  }
+  if (*timep == 0) gmtime_emulation_type = 0;  /* possibly reset TZ */
 
   when = *timep;
 # ifdef VMSISH_TIME
-  if (VMSISH_TIME) when -= utc_offset_secs; /* Input was local time */
-# endif
+  if (VMSISH_TIME) when = _toutc(when); /* Input was local time */
+#  endif
+# ifdef RTL_USES_UTC  /* this implies that the CRTL has a working gmtime() */
+  return gmtime(&when);
+# else
   /* CRTL localtime() wants local time as input, so does no tz correction */
-  return localtime(&when);
-
+  rsltmp = localtime(&when);
+  if (rsltmp) rsltmp->tm_isdst = 0;  /* We already took DST into account */
+  return rsltmp;
+#endif
 }  /* end of my_gmtime() */
 /*}}}*/
 
@@ -3189,16 +3617,33 @@ my_gmtime(const time_t *timep)
 struct tm *
 my_localtime(const time_t *timep)
 {
+  dTHR;
   time_t when;
+  struct tm *rsltmp;
 
+  if (timep == NULL) {
+    set_errno(EINVAL); set_vaxc_errno(LIB$_INVARG);
+    return NULL;
+  }
+  if (*timep == 0) gmtime_emulation_type = 0;  /* possibly reset TZ */
   if (gmtime_emulation_type == 0) (void) my_time(NULL); /* Init UTC */
 
   when = *timep;
+# ifdef RTL_USES_UTC
 # ifdef VMSISH_TIME
-  if (!VMSISH_TIME) when += utc_offset_secs;  /*  Input was UTC */
+  if (VMSISH_TIME) when = _toutc(when);
 # endif
-  /* CRTL localtime() wants local time as input, so does no tz correction */
+  /* CRTL localtime() wants UTC as input, does tz correction itself */
   return localtime(&when);
+# else
+# ifdef VMSISH_TIME
+  if (!VMSISH_TIME) when = _toloc(when);   /*  Input was UTC */
+# endif
+# endif
+  /* CRTL localtime() wants local time as input, so does no tz correction */
+  rsltmp = localtime(&when);
+  if (rsltmp && gmtime_emulation_type != 1) rsltmp->tm_isdst = -1;
+  return rsltmp;
 
 } /*  end of my_localtime() */
 /*}}}*/
@@ -3229,6 +3674,7 @@ static const long int utime_baseadjust[2] = { 0x4beb4000, 0x7c9567 };
 /*{{{int my_utime(char *path, struct utimbuf *utimes)*/
 int my_utime(char *file, struct utimbuf *utimes)
 {
+  dTHR;
   register int i;
   long int bintime[2], len = 2, lowbit, unixtime,
            secscale = 10000000; /* seconds --> 100 ns intervals */
@@ -3272,10 +3718,8 @@ int my_utime(char *file, struct utimbuf *utimes)
     lowbit = (utimes->modtime & 1) ? secscale : 0;
     unixtime = (long int) utimes->modtime;
 #   ifdef VMSISH_TIME
-    if (!VMSISH_TIME) {  /* Input was UTC; convert to local for sys svc */
-      if (!gmtime_emulation_type) (void) time(NULL);  /* Initialize UTC */
-      unixtime += utc_offset_secs;
-    }
+    /* If input was UTC; convert to local for sys svc */
+    if (!VMSISH_TIME) unixtime = _toloc(unixtime);
 #   endif
     unixtime >> 1;  secscale << 1;
     retsts = lib$emul(&secscale, &unixtime, &lowbit, bintime);
@@ -3475,14 +3919,13 @@ is_null_device(name)
 
 /* Do the permissions allow some operation?  Assumes statcache already set. */
 /* Do this via $Check_Access on VMS, since the CRTL stat() returns only a
- * subset of the applicable information.  (We have to stick with struct
- * stat instead of struct mystat in the prototype since we have to match
- * the one in proto.h.)
+ * subset of the applicable information.
  */
 /*{{{I32 cando(I32 bit, I32 effective, struct stat *statbufp)*/
 I32
-cando(I32 bit, I32 effective, struct stat *statbufp)
+cando(I32 bit, I32 effective, Stat_t *statbufp)
 {
+  dTHR;
   if (statbufp == &statcache) return cando_by_name(bit,effective,namecache);
   else {
     char fname[NAM$C_MAXRSS+1];
@@ -3492,12 +3935,12 @@ cando(I32 bit, I32 effective, struct stat *statbufp)
 
     /* If the struct mystat is stale, we're OOL; stat() overwrites the
        device name on successive calls */
-    devdsc.dsc$a_pointer = ((struct mystat *)statbufp)->st_devnam;
-    devdsc.dsc$w_length = strlen(((struct mystat *)statbufp)->st_devnam);
+    devdsc.dsc$a_pointer = ((Stat_t *)statbufp)->st_devnam;
+    devdsc.dsc$w_length = strlen(((Stat_t *)statbufp)->st_devnam);
     namdsc.dsc$a_pointer = fname;
     namdsc.dsc$w_length = sizeof fname - 1;
 
-    retsts = lib$fid_to_name(&devdsc,&(((struct mystat *)statbufp)->st_ino),
+    retsts = lib$fid_to_name(&devdsc,&(((Stat_t *)statbufp)->st_ino),
                              &namdsc,&namdsc.dsc$w_length,0,0);
     if (retsts & 1) {
       fname[namdsc.dsc$w_length] = '\0';
@@ -3580,7 +4023,7 @@ cando_by_name(I32 bit, I32 effective, char *fname)
 
   retsts = sys$check_access(&objtyp,&namdsc,&usrdsc,armlst);
   if (retsts == SS$_NOPRIV      || retsts == SS$_NOSUCHOBJECT ||
-      retsts == SS$_INVFILFOROP || retsts == RMS$_FNF    ||
+      retsts == SS$_INVFILFOROP || retsts == RMS$_FNF || retsts == RMS$_SYN ||
       retsts == RMS$_DIR        || retsts == RMS$_DEV) {
     set_vaxc_errno(retsts);
     if (retsts == SS$_NOPRIV) set_errno(EACCES);
@@ -3601,6 +4044,9 @@ cando_by_name(I32 bit, I32 effective, char *fname)
     if ((privused & CHP$M_READALL) && !curprv.prv$v_readall) return FALSE;
     return TRUE;
   }
+  if (retsts == SS$_ACCONFLICT) {
+    return TRUE;
+  }
   _ckvmssts(retsts);
 
   return FALSE;  /* Should never get here */
@@ -3609,23 +4055,33 @@ cando_by_name(I32 bit, I32 effective, char *fname)
 /*}}}*/
 
 
-/*{{{ int flex_fstat(int fd, struct mystat *statbuf)*/
+/*{{{ int flex_fstat(int fd, Stat_t *statbuf)*/
 int
-flex_fstat(int fd, struct mystat *statbufp)
+flex_fstat(int fd, Stat_t *statbufp)
 {
+  dTHR;
   if (!fstat(fd,(stat_t *) statbufp)) {
-    if (statbufp == (struct mystat *) &statcache) *namecache == '\0';
+    if (statbufp == (Stat_t *) &statcache) *namecache == '\0';
     statbufp->st_dev = encode_dev(statbufp->st_devnam);
+#   ifdef RTL_USES_UTC
+#   ifdef VMSISH_TIME
+    if (VMSISH_TIME) {
+      statbufp->st_mtime = _toloc(statbufp->st_mtime);
+      statbufp->st_atime = _toloc(statbufp->st_atime);
+      statbufp->st_ctime = _toloc(statbufp->st_ctime);
+    }
+#   endif
+#   else
 #   ifdef VMSISH_TIME
     if (!VMSISH_TIME) { /* Return UTC instead of local time */
 #   else
     if (1) {
 #   endif
-      if (!gmtime_emulation_type) (void)time(NULL);
-      statbufp->st_mtime -= utc_offset_secs;
-      statbufp->st_atime -= utc_offset_secs;
-      statbufp->st_ctime -= utc_offset_secs;
+      statbufp->st_mtime = _toutc(statbufp->st_mtime);
+      statbufp->st_atime = _toutc(statbufp->st_atime);
+      statbufp->st_ctime = _toutc(statbufp->st_ctime);
     }
+#endif
     return 0;
   }
   return -1;
@@ -3633,14 +4089,15 @@ flex_fstat(int fd, struct mystat *statbufp)
 }  /* end of flex_fstat() */
 /*}}}*/
 
-/*{{{ int flex_stat(char *fspec, struct mystat *statbufp)*/
+/*{{{ int flex_stat(char *fspec, Stat_t *statbufp)*/
 int
-flex_stat(char *fspec, struct mystat *statbufp)
+flex_stat(char *fspec, Stat_t *statbufp)
 {
+    dTHR;
     char fileified[NAM$C_MAXRSS+1];
     int retval = -1;
 
-    if (statbufp == (struct mystat *) &statcache)
+    if (statbufp == (Stat_t *) &statcache)
       do_tovmsspec(fspec,namecache,0);
     if (is_null_device(fspec)) { /* Fake a stat() for the null device */
       memset(statbufp,0,sizeof *statbufp);
@@ -3654,7 +4111,7 @@ flex_stat(char *fspec, struct mystat *statbufp)
     }
 
     /* Try for a directory name first.  If fspec contains a filename without
-     * a type (e.g. sea:[dark.dark]water), and both sea:[wine.dark]water.dir
+     * a type (e.g. sea:[wine.dark]water), and both sea:[wine.dark]water.dir
      * and sea:[wine.dark]water. exist, we prefer the directory here.
      * Similarly, sea:[wine.dark] returns the result for sea:[wine]dark.dir,
      * not sea:[wine.dark]., if the latter exists.  If the intended target is
@@ -3663,22 +4120,31 @@ flex_stat(char *fspec, struct mystat *statbufp)
      */
     if (do_fileify_dirspec(fspec,fileified,0) != NULL) {
       retval = stat(fileified,(stat_t *) statbufp);
-      if (!retval && statbufp == (struct mystat *) &statcache)
+      if (!retval && statbufp == (Stat_t *) &statcache)
         strcpy(namecache,fileified);
     }
     if (retval) retval = stat(fspec,(stat_t *) statbufp);
     if (!retval) {
       statbufp->st_dev = encode_dev(statbufp->st_devnam);
+#     ifdef RTL_USES_UTC
+#     ifdef VMSISH_TIME
+      if (VMSISH_TIME) {
+        statbufp->st_mtime = _toloc(statbufp->st_mtime);
+        statbufp->st_atime = _toloc(statbufp->st_atime);
+        statbufp->st_ctime = _toloc(statbufp->st_ctime);
+      }
+#     endif
+#     else
 #     ifdef VMSISH_TIME
       if (!VMSISH_TIME) { /* Return UTC instead of local time */
 #     else
       if (1) {
 #     endif
-        if (!gmtime_emulation_type) (void)time(NULL);
-        statbufp->st_mtime -= utc_offset_secs;
-        statbufp->st_atime -= utc_offset_secs;
-        statbufp->st_ctime -= utc_offset_secs;
+        statbufp->st_mtime = _toutc(statbufp->st_mtime);
+        statbufp->st_atime = _toutc(statbufp->st_atime);
+        statbufp->st_ctime = _toutc(statbufp->st_ctime);
       }
+#     endif
     }
     return retval;
 
@@ -3690,25 +4156,40 @@ flex_stat(char *fspec, struct mystat *statbufp)
 FILE *
 my_binmode(FILE *fp, char iotype)
 {
-    char filespec[NAM$C_MAXRSS], *acmode;
+    char filespec[NAM$C_MAXRSS], *acmode, *s, *colon, *dirend = Nullch;
+    int ret = 0, saverrno = errno, savevmserrno = vaxc$errno;
     fpos_t pos;
 
     if (!fgetname(fp,filespec)) return NULL;
-    if (iotype != '-' && fgetpos(fp,&pos) == -1) return NULL;
+    for (s = filespec; *s; s++) {
+      if (*s == ':') colon = s;
+      else if (*s == ']' || *s == '>') dirend = s;
+    }
+    /* Looks like a tmpfile, which will go away if reopened */
+    if (s == dirend + 3) return fp;
+    /* If we've got a non-file-structured device, clip off the trailing
+     * junk, and don't lose sleep if we can't get a stream position.  */
+    if (dirend == Nullch) *(colon+1) = '\0'; 
+    if (iotype != '-'&& (ret = fgetpos(fp, &pos)) == -1 && dirend) return NULL;
     switch (iotype) {
       case '<': case 'r':           acmode = "rb";                      break;
-      case '>': case 'w':
+      case '>': case 'w': case '|':
         /* use 'a' instead of 'w' to avoid creating new file;
            fsetpos below will take care of restoring file position */
       case 'a':                     acmode = "ab";                      break;
-      case '+': case '|': case 's': acmode = "rb+";                     break;
+      case '+':  case 's':          acmode = "rb+";                     break;
       case '-':                     acmode = fileno(fp) ? "ab" : "rb";  break;
+      /* iotype'll be null for the SYS$INPUT:/SYS$OUTPUT:/SYS$ERROR: files */
+      /* since we didn't really open them and can't really */
+      /* reopen them */
+      case 0:                       return NULL;                        break;
       default:
-        warn("Unrecognized iotype %c in my_binmode",iotype);
+        warn("Unrecognized iotype %x for %s in my_binmode",iotype, filespec);
         acmode = "rb+";
     }
     if (freopen(filespec,acmode,fp) == NULL) return NULL;
-    if (iotype != '-' && fsetpos(fp,&pos) == -1) return NULL;
+    if (iotype != '-' && ret != -1 && fsetpos(fp,&pos) == -1) return NULL;
+    if (ret == -1) { set_errno(saverrno); set_vaxc_errno(savevmserrno); }
     return fp;
 }  /* end of my_binmode() */
 /*}}}*/
@@ -4044,7 +4525,7 @@ candelete_fromperl(CV *cv)
     }
   }
 
-  ST(0) = cando_by_name(S_IDUSR,0,fsp) ? &sv_yes : &sv_no;
+  ST(0) = boolSV(cando_by_name(S_IDUSR,0,fsp));
   XSRETURN(1);
 }
 
@@ -4097,7 +4578,7 @@ rmscopy_fromperl(CV *cv)
   }
   date_flag = (items == 3) ? SvIV(ST(2)) : 0;
 
-  ST(0) = rmscopy(inp,outp,date_flag) ? &sv_yes : &sv_no;
+  ST(0) = boolSV(rmscopy(inp,outp,date_flag));
   XSRETURN(1);
 }
 
@@ -4115,6 +4596,11 @@ init_os_extras()
   newXSproto("VMS::Filespec::unixpath",unixpath_fromperl,file,"$");
   newXSproto("VMS::Filespec::candelete",candelete_fromperl,file,"$");
   newXS("File::Copy::rmscopy",rmscopy_fromperl,file);
+
+#ifdef PRIME_ENV_AT_STARTUP
+  prime_env_iter();
+#endif
+
   return;
 }