This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
$#a>>=1 relies on malloc wrap to avoid the segfault, so need to
[perl5.git] / doio.c
diff --git a/doio.c b/doio.c
index a1cc42a..70b3535 100644 (file)
--- a/doio.c
+++ b/doio.c
@@ -1,6 +1,7 @@
 /*    doio.c
  *
- *    Copyright (c) 1991-2003, Larry Wall
+ *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
+ *    2000, 2001, 2002, 2003, 2004, by Larry Wall and others
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
  * chattering, into calmer and more level reaches."
  */
 
+/* This file contains functions that do the actual I/O on behalf of ops.
+ * For example, pp_print() calls the do_print() function in this file for
+ * each argument needing printing.
+ */
+
 #include "EXTERN.h"
 #define PERL_IN_DOIO_C
 #include "perl.h"
@@ -47,9 +53,7 @@
 #  define OPEN_EXCL 0
 #endif
 
-#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
 #include <signal.h>
-#endif
 
 bool
 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
@@ -93,7 +97,7 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
 
     /* Collect default raw/crlf info from the op */
     if (PL_op && PL_op->op_type == OP_OPEN) {
-       /* set up disciplines */
+       /* set up IO layers */
        U8 flags = PL_op->op_private;
        in_raw = (flags & OPpOPEN_IN_RAW);
        in_crlf = (flags & OPpOPEN_IN_CRLF);
@@ -178,7 +182,7 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
                 (ismodifying & (O_CREAT|appendtrunc)))
                  TAINT_PROPER("sysopen");
        }
-       mode[ix++] = '#'; /* Marker to openn to use numeric "sysopen" */
+       mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
 
 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
        rawmode |= O_LARGEFILE; /* Transparently largefiley. */
@@ -211,7 +215,7 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
            *--tend = '\0';
 
        if (num_svs) {
-           /* New style explict name, type is just mode and discipline/layer info */
+           /* New style explicit name, type is just mode and layer info */
            STRLEN l = 0;
 #ifdef USE_STDIO
            if (SvROK(*svp) && !strchr(name,'&')) {
@@ -235,7 +239,7 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
        if ((*type == IoTYPE_RDWR) && /* scary */
            (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
            ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
-        TAINT_PROPER("open");
+           TAINT_PROPER("open");
            mode[1] = *type++;
            writing = 1;
        }
@@ -243,7 +247,7 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
        if (*type == IoTYPE_PIPE) {
            if (num_svs) {
                if (type[1] != IoTYPE_STD) {
-                 unknown_desr:
+                 unknown_open_mode:
                    Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
                }
                type++;
@@ -288,7 +292,7 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
                    }
                }
            }
-       }
+       } /* IoTYPE_PIPE */
        else if (*type == IoTYPE_WRONLY) {
            TAINT_PROPER("open");
            type++;
@@ -421,7 +425,9 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
                    fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
                }
            } /* !& */
-       }
+           if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
+              goto unknown_open_mode;
+       } /* IoTYPE_WRONLY */
        else if (*type == IoTYPE_RDONLY) {
            /*SUPPRESS 530*/
            for (type++; isSPACE(*type); type++) ;
@@ -452,8 +458,11 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
                }
                fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
            }
-       }
-       else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
+           if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
+              goto unknown_open_mode;
+       } /* IoTYPE_RDONLY */
+       else if ((num_svs && /* '-|...' or '...|' */
+                 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
                 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
            if (num_svs) {
                type += 2;   /* skip over '-|' */
@@ -498,9 +507,9 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
                }
            }
        }
-       else {
+       else { /* layer(Args) */
            if (num_svs)
-               goto unknown_desr;
+               goto unknown_open_mode;
            name = type;
            IoTYPE(io) = IoTYPE_RDONLY;
            /*SUPPRESS 530*/
@@ -640,8 +649,16 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
                 /* need to close fp without closing underlying fd */
                 int ofd = PerlIO_fileno(fp);
                 int dupfd = PerlLIO_dup(ofd);
+#if defined(HAS_FCNTL) && defined(F_SETFD)
+               /* Assume if we have F_SETFD we have F_GETFD */
+                int coe = fcntl(ofd,F_GETFD);
+#endif
                 PerlIO_close(fp);
                 PerlLIO_dup2(dupfd,ofd);
+#if defined(HAS_FCNTL) && defined(F_SETFD)
+               /* The dup trick has lost close-on-exec on ofd */
+               fcntl(ofd,F_SETFD, coe);
+#endif
                 PerlLIO_close(dupfd);
            }
             else
@@ -665,8 +682,8 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
        if (IoTYPE(io) == IoTYPE_SOCKET
            || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
            char *s = mode;
-           if (*s == 'I' || *s == '#')
-            s++;
+           if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
+             s++;
            *s = 'w';
            if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
                PerlIO_close(fp);
@@ -711,12 +728,16 @@ Perl_nextargv(pTHX_ register GV *gv)
     if (PL_filemode & (S_ISUID|S_ISGID)) {
        PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv)));  /* chmod must follow last write */
 #ifdef HAS_FCHMOD
-       (void)fchmod(PL_lastfd,PL_filemode);
+       if (PL_lastfd != -1)
+           (void)fchmod(PL_lastfd,PL_filemode);
 #else
        (void)PerlLIO_chmod(PL_oldname,PL_filemode);
 #endif
     }
+    PL_lastfd = -1;
     PL_filemode = 0;
+    if (!GvAV(gv))
+        return Nullfp;
     while (av_len(GvAV(gv)) >= 0) {
        STRLEN oldlen;
        sv = av_shift(GvAV(gv));
@@ -921,8 +942,8 @@ Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
 
     if (PerlProc_pipe(fd) < 0)
        goto badexit;
-    IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
-    IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
+    IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
+    IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
     IoOFP(rstio) = IoIFP(rstio);
     IoIFP(wstio) = IoOFP(wstio);
     IoTYPE(rstio) = IoTYPE_RDONLY;
@@ -1027,17 +1048,21 @@ Perl_do_eof(pTHX_ GV *gv)
        report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
 
     while (IoIFP(io)) {
+        int saverrno;
 
         if (PerlIO_has_cntptr(IoIFP(io))) {    /* (the code works without this) */
            if (PerlIO_get_cnt(IoIFP(io)) > 0)  /* cheat a little, since */
                return FALSE;                   /* this is the most usual case */
         }
 
+       saverrno = errno; /* getc and ungetc can stomp on errno */
        ch = PerlIO_getc(IoIFP(io));
        if (ch != EOF) {
            (void)PerlIO_ungetc(IoIFP(io),ch);
+           errno = saverrno;
            return FALSE;
        }
+       errno = saverrno;
 
         if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
            if (PerlIO_get_cnt(IoIFP(io)) < -1)
@@ -1150,8 +1175,9 @@ fail_discipline:
                if (!end)
                    end = s+len;
 #ifndef PERLIO_LAYERS
-               Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
+               Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
 #else
+               len -= end-s;
                s = end;
 #endif
            }
@@ -1250,7 +1276,7 @@ Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
     switch (SvTYPE(sv)) {
     case SVt_NULL:
        if (ckWARN(WARN_UNINITIALIZED))
-           report_uninit();
+           report_uninit(sv);
        return TRUE;
     case SVt_IV:
        if (SvIOK(sv)) {
@@ -1318,6 +1344,9 @@ Perl_my_stat(pTHX)
            return (PL_laststatval = -1);
        }
     }
+    else if (PL_op->op_private & OPpFT_STACKED) {
+       return PL_laststatval;
+    }
     else {
        SV* sv = POPs;
        char *s;
@@ -1344,6 +1373,8 @@ Perl_my_stat(pTHX)
     }
 }
 
+static char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
+
 I32
 Perl_my_lstat(pTHX)
 {
@@ -1354,7 +1385,7 @@ Perl_my_lstat(pTHX)
        EXTEND(SP,1);
        if (cGVOP_gv == PL_defgv) {
            if (PL_laststype != OP_LSTAT)
-               Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
+               Perl_croak(aTHX_ no_prev_lstat);
            return PL_laststatval;
        }
        if (ckWARN(WARN_IO)) {
@@ -1363,6 +1394,9 @@ Perl_my_lstat(pTHX)
            return (PL_laststatval = -1);
        }
     }
+    else if (ckWARN(WARN_IO) && PL_laststype != OP_LSTAT
+           && (PL_op->op_private & OPpFT_STACKED))
+       Perl_croak(aTHX_ no_prev_lstat);
 
     PL_laststype = OP_LSTAT;
     PL_statgv = Nullgv;
@@ -1380,11 +1414,13 @@ Perl_my_lstat(pTHX)
     return PL_laststatval;
 }
 
+#ifndef OS2
 bool
 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
 {
     return do_aexec5(really, mark, sp, 0, 0);
 }
+#endif
 
 bool
 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
@@ -1412,10 +1448,12 @@ Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
        if ((!really && *PL_Argv[0] != '/') ||
            (really && *tmps != '/'))           /* will execvp use PATH? */
            TAINT_ENV();                /* testing IFS here is overkill, probably */
+       PERL_FPU_PRE_EXEC
        if (really && *tmps)
            PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
        else
            PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
+       PERL_FPU_POST_EXEC
        if (ckWARN(WARN_EXEC))
            Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
                (really ? tmps : PL_Argv[0]), Strerror(errno));
@@ -1485,7 +1523,9 @@ Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
                  *--s = '\0';
              if (s[-1] == '\'') {
                  *--s = '\0';
+                 PERL_FPU_PRE_EXEC
                  PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
+                 PERL_FPU_POST_EXEC
                  *s = '\'';
                  return FALSE;
              }
@@ -1522,13 +1562,15 @@ Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
 
                while (*t && isSPACE(*t))
                    ++t;
-               if (!*t && (dup2(1,2) != -1)) {
+               if (!*t && (PerlLIO_dup2(1,2) != -1)) {
                    s[-2] = '\0';
                    break;
                }
            }
          doshell:
+           PERL_FPU_PRE_EXEC
            PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
+           PERL_FPU_POST_EXEC
            return FALSE;
        }
     }
@@ -1546,7 +1588,9 @@ Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
     }
     *a = Nullch;
     if (PL_Argv[0]) {
+       PERL_FPU_PRE_EXEC
        PerlProc_execvp(PL_Argv[0],PL_Argv);
+       PERL_FPU_POST_EXEC
        if (errno == ENOEXEC) {         /* for system V NIH syndrome */
            do_execfree();
            goto doshell;
@@ -2260,8 +2304,9 @@ Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
                if (*cp == '?') *cp = '%';  /* VMS style single-char wildcard */
            while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
                                               &dfltdsc,NULL,NULL,NULL))&1)) {
-               end = rstr + (unsigned long int) *rslt;
-               if (!hasver) while (*end != ';') end--;
+               /* with varying string, 1st word of buffer contains result length */
+               end = rstr + *((unsigned short int*)rslt);
+               if (!hasver) while (*end != ';' && end > rstr) end--;
                *(end++) = '\n';  *end = '\0';
                for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
                if (hasdir) {