This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
gutsupport for C++ exceptions
[perl5.git] / doio.c
diff --git a/doio.c b/doio.c
index 271218f..52acbde 100644 (file)
--- a/doio.c
+++ b/doio.c
@@ -1,6 +1,6 @@
 /*    doio.c
  *
- *    Copyright (c) 1991-1997, Larry Wall
+ *    Copyright (c) 1991-1999, Larry Wall
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
 #include "perl.h"
 
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
+#ifndef HAS_SEM
 #include <sys/ipc.h>
+#endif
 #ifdef HAS_MSG
 #include <sys/msg.h>
 #endif
-#ifdef HAS_SEM
-#include <sys/sem.h>
-#endif
 #ifdef HAS_SHM
 #include <sys/shm.h>
 # ifndef HAS_SHMAT_PROTOTYPE
@@ -125,9 +124,14 @@ do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawpe
     }
 
     if (as_raw) {
+#if defined(O_LARGEFILE)
+       rawmode |= O_LARGEFILE;
+#endif
+
 #ifndef O_ACCMODE
 #define O_ACCMODE 3            /* Assume traditional implementation */
 #endif
+
        switch (result = rawmode & O_ACCMODE) {
        case O_RDONLY:
             IoTYPE(io) = '<';
@@ -183,6 +187,13 @@ do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawpe
        if (*name == '|') {
            /*SUPPRESS 530*/
            for (name++; isSPACE(*name); name++) ;
+           if (*name == '\0') { /* command is missing 19990114 */
+               dTHR;
+               if (ckWARN(WARN_PIPE))
+                   warner(WARN_PIPE, "Missing command in piped open");
+               errno = EPIPE;
+               goto say_false;
+           }
            if (strNE(name,"-"))
                TAINT_ENV();
            TAINT_PROPER("piped open");
@@ -280,6 +291,13 @@ do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawpe
                name[--len] = '\0';
            /*SUPPRESS 530*/
            for (; isSPACE(*name); name++) ;
+           if (*name == '\0') { /* command is missing 19990114 */
+               dTHR;
+               if (ckWARN(WARN_PIPE))
+                   warner(WARN_PIPE, "Missing command in piped open");
+               errno = EPIPE;
+               goto say_false;
+           }
            if (strNE(name,"-"))
                TAINT_ENV();
            TAINT_PROPER("piped open");
@@ -301,7 +319,7 @@ do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawpe
     if (!fp) {
        dTHR;
        if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == '<' && strchr(name, '\n'))
-           warner(WARN_NEWLINE, warn_nl, "open");
+           warner(WARN_NEWLINE, PL_warn_nl, "open");
        goto say_false;
     }
     if (IoTYPE(io) &&
@@ -361,8 +379,12 @@ do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawpe
        PerlIO_clearerr(fp);
     }
 #if defined(HAS_FCNTL) && defined(F_SETFD)
-    fd = PerlIO_fileno(fp);
-    fcntl(fd,F_SETFD,fd > PL_maxsysfd);
+    {
+       int save_errno = errno;
+       fd = PerlIO_fileno(fp);
+       fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
+       errno = save_errno;
+    }
 #endif
     IoIFP(io) = fp;
     if (writing) {
@@ -395,8 +417,8 @@ nextargv(register GV *gv)
     int filedev;
     int fileino;
 #endif
-    int fileuid;
-    int filegid;
+    Uid_t fileuid;
+    Gid_t filegid;
 
     if (!PL_argvoutgv)
        PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
@@ -461,7 +483,7 @@ nextargv(register GV *gv)
                       || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
 #endif
                       ) {
-                       warn("Can't do inplace edit: %s would not be uniq",
+                       warn("Can't do inplace edit: %s would not be unique",
                          SvPVX(sv) );
                        do_close(gv,FALSE);
                        continue;
@@ -547,7 +569,7 @@ nextargv(register GV *gv)
        }
        else
            PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n",
-             SvPV(sv, PL_na), Strerror(errno));
+             SvPV(sv, oldlen), Strerror(errno));
     }
     if (PL_inplace) {
        (void)do_close(PL_argvoutgv,FALSE);
@@ -705,7 +727,7 @@ do_eof(GV *gv)
     return TRUE;
 }
 
-long
+Off_t
 do_tell(GV *gv)
 {
     register IO *io;
@@ -724,11 +746,11 @@ do_tell(GV *gv)
            warner(WARN_UNOPENED, "tell() on unopened file");
     }
     SETERRNO(EBADF,RMS$_IFI);
-    return -1L;
+    return (Off_t)-1;
 }
 
 bool
-do_seek(GV *gv, long int pos, int whence)
+do_seek(GV *gv, Off_t pos, int whence)
 {
     register IO *io;
     register PerlIO *fp;
@@ -749,8 +771,8 @@ do_seek(GV *gv, long int pos, int whence)
     return FALSE;
 }
 
-long
-do_sysseek(GV *gv, long int pos, int whence)
+Off_t
+do_sysseek(GV *gv, Off_t pos, int whence)
 {
     register IO *io;
     register PerlIO *fp;
@@ -772,7 +794,7 @@ do_binmode(PerlIO *fp, int iotype, int flag)
     if (flag != TRUE)
        croak("panic: unsetting binmode"); /* Not implemented yet */
 #ifdef DOSISH
-#ifdef atarist
+#if defined(atarist) || defined(__MINT__)
     if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
        return 1;
     else
@@ -884,14 +906,17 @@ do_print(register SV *sv, PerlIO *fp)
        {
            dTHR;
            if (ckWARN(WARN_UNINITIALIZED))
-               warner(WARN_UNINITIALIZED, warn_uninit);
+               warner(WARN_UNINITIALIZED, PL_warn_uninit);
        }
        return TRUE;
     case SVt_IV:
        if (SvIOK(sv)) {
            if (SvGMAGICAL(sv))
                mg_get(sv);
-           PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
+           if (SvIsUV(sv))             /* XXXX 64-bit? */
+               PerlIO_printf(fp, "%lu", (unsigned long)SvUVX(sv));
+           else
+               PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
            return !PerlIO_error(fp);
        }
        /* FALL THROUGH */
@@ -936,6 +961,7 @@ my_stat(ARGSproto)
     else {
        SV* sv = POPs;
        char *s;
+       STRLEN n_a;
        PUTBACK;
        if (SvTYPE(sv) == SVt_PVGV) {
            tmpgv = (GV*)sv;
@@ -946,13 +972,13 @@ my_stat(ARGSproto)
            goto do_fstat;
        }
 
-       s = SvPV(sv, PL_na);
+       s = SvPV(sv, n_a);
        PL_statgv = Nullgv;
        sv_setpv(PL_statname, s);
        PL_laststype = OP_STAT;
        PL_laststatval = PerlLIO_stat(s, &PL_statcache);
        if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
-           warner(WARN_NEWLINE, warn_nl, "stat");
+           warner(WARN_NEWLINE, PL_warn_nl, "stat");
        return PL_laststatval;
     }
 }
@@ -962,6 +988,7 @@ my_lstat(ARGSproto)
 {
     djSP;
     SV *sv;
+    STRLEN n_a;
     if (PL_op->op_flags & OPf_REF) {
        EXTEND(SP,1);
        if (cGVOP->op_gv == PL_defgv) {
@@ -976,14 +1003,14 @@ my_lstat(ARGSproto)
     PL_statgv = Nullgv;
     sv = POPs;
     PUTBACK;
-    sv_setpv(PL_statname,SvPV(sv, PL_na));
+    sv_setpv(PL_statname,SvPV(sv, n_a));
 #ifdef HAS_LSTAT
-    PL_laststatval = PerlLIO_lstat(SvPV(sv, PL_na),&PL_statcache);
+    PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
 #else
-    PL_laststatval = PerlLIO_stat(SvPV(sv, PL_na),&PL_statcache);
+    PL_laststatval = PerlLIO_stat(SvPV(sv, n_a),&PL_statcache);
 #endif
-    if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, PL_na), '\n'))
-       warner(WARN_NEWLINE, warn_nl, "lstat");
+    if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
+       warner(WARN_NEWLINE, PL_warn_nl, "lstat");
     return PL_laststatval;
 }
 
@@ -992,6 +1019,7 @@ do_aexec(SV *really, register SV **mark, register SV **sp)
 {
     register char **a;
     char *tmps;
+    STRLEN n_a;
 
     if (sp > mark) {
        dTHR;
@@ -999,14 +1027,14 @@ do_aexec(SV *really, register SV **mark, register SV **sp)
        a = PL_Argv;
        while (++mark <= sp) {
            if (*mark)
-               *a++ = SvPVx(*mark, PL_na);
+               *a++ = SvPVx(*mark, n_a);
            else
                *a++ = "";
        }
        *a = Nullch;
        if (*PL_Argv[0] != '/') /* will execvp use PATH? */
            TAINT_ENV();                /* testing IFS here is overkill, probably */
-       if (really && *(tmps = SvPV(really, PL_na)))
+       if (really && *(tmps = SvPV(really, n_a)))
            PerlProc_execvp(tmps,PL_Argv);
        else
            PerlProc_execvp(PL_Argv[0],PL_Argv);
@@ -1036,6 +1064,12 @@ do_execfree(void)
 bool
 do_exec(char *cmd)
 {
+    return do_exec3(cmd,0,0);
+}
+
+bool
+do_exec3(char *cmd, int fd, int do_report)
+{
     register char **a;
     register char *s;
     char flags[10];
@@ -1116,9 +1150,15 @@ do_exec(char *cmd)
        }
        {
            dTHR;
+           int e = errno;
+
            if (ckWARN(WARN_EXEC))
                warner(WARN_EXEC, "Can't exec \"%s\": %s", 
                    PL_Argv[0], Strerror(errno));
+           if (do_report) {
+               PerlLIO_write(fd, (void*)&e, sizeof(int));
+               PerlLIO_close(fd);
+           }
        }
     }
     do_execfree();
@@ -1137,10 +1177,11 @@ apply(I32 type, register SV **mark, register SV **sp)
     char *what;
     char *s;
     SV **oldmark = mark;
+    STRLEN n_a;
 
 #define APPLY_TAINT_PROPER() \
     STMT_START {                                                       \
-       if (PL_tainting && PL_tainted) { goto taint_proper_label; }     \
+       if (PL_tainted) { TAINT_PROPER(what); }                         \
     } STMT_END
 
     /* This is a first heuristic; it doesn't catch tainting magic. */
@@ -1162,7 +1203,7 @@ apply(I32 type, register SV **mark, register SV **sp)
            APPLY_TAINT_PROPER();
            tot = sp - mark;
            while (++mark <= sp) {
-               char *name = SvPVx(*mark, PL_na);
+               char *name = SvPVx(*mark, n_a);
                APPLY_TAINT_PROPER();
                if (PerlLIO_chmod(name, val))
                    tot--;
@@ -1179,7 +1220,7 @@ apply(I32 type, register SV **mark, register SV **sp)
            APPLY_TAINT_PROPER();
            tot = sp - mark;
            while (++mark <= sp) {
-               char *name = SvPVx(*mark, PL_na);
+               char *name = SvPVx(*mark, n_a);
                APPLY_TAINT_PROPER();
                if (PerlLIO_chown(name, val, val2))
                    tot--;
@@ -1199,7 +1240,7 @@ nothing in the core.
        APPLY_TAINT_PROPER();
        if (mark == sp)
            break;
-       s = SvPVx(*++mark, PL_na);
+       s = SvPVx(*++mark, n_a);
        if (isUPPER(*s)) {
            if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
                s += 3;
@@ -1269,7 +1310,7 @@ nothing in the core.
        APPLY_TAINT_PROPER();
        tot = sp - mark;
        while (++mark <= sp) {
-           s = SvPVx(*mark, PL_na);
+           s = SvPVx(*mark, n_a);
            APPLY_TAINT_PROPER();
            if (PL_euid || PL_unsafe) {
                if (UNLINK(s))
@@ -1298,23 +1339,23 @@ nothing in the core.
            struct utimbuf utbuf;
 #else
            struct {
-               long    actime;
-               long    modtime;
+               Time_t  actime;
+               Time_t  modtime;
            } utbuf;
 #endif
 
            Zero(&utbuf, sizeof utbuf, char);
 #ifdef BIG_TIME
-           utbuf.actime = (Time_t)SvNVx(*++mark);    /* time accessed */
-           utbuf.modtime = (Time_t)SvNVx(*++mark);    /* time modified */
+           utbuf.actime = (Time_t)SvNVx(*++mark);      /* time accessed */
+           utbuf.modtime = (Time_t)SvNVx(*++mark);     /* time modified */
 #else
-           utbuf.actime = SvIVx(*++mark);    /* time accessed */
-           utbuf.modtime = SvIVx(*++mark);    /* time modified */
+           utbuf.actime = (Time_t)SvIVx(*++mark);      /* time accessed */
+           utbuf.modtime = (Time_t)SvIVx(*++mark);     /* time modified */
 #endif
            APPLY_TAINT_PROPER();
            tot = sp - mark;
            while (++mark <= sp) {
-               char *name = SvPVx(*mark, PL_na);
+               char *name = SvPVx(*mark, n_a);
                APPLY_TAINT_PROPER();
                if (PerlLIO_utime(name, &utbuf))
                    tot--;
@@ -1327,10 +1368,6 @@ nothing in the core.
     }
     return tot;
 
-  taint_proper_label:
-    TAINT_PROPER(what);
-    return 0;  /* this should never happen */
-
 #undef APPLY_TAINT_PROPER
 }
 
@@ -1439,7 +1476,7 @@ do_ipcget(I32 optype, SV **mark, SV **sp)
 #endif
 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
     default:
-       croak("%s not implemented", op_desc[optype]);
+       croak("%s not implemented", PL_op_desc[optype]);
 #endif
     }
     return -1;                 /* should never happen */
@@ -1477,6 +1514,7 @@ do_ipcctl(I32 optype, SV **mark, SV **sp)
 #endif
 #ifdef HAS_SEM
     case OP_SEMCTL:
+#ifdef Semctl
        if (cmd == IPC_STAT || cmd == IPC_SET)
            infosize = sizeof(struct semid_ds);
        else if (cmd == GETALL || cmd == SETALL)
@@ -1492,11 +1530,14 @@ do_ipcctl(I32 optype, SV **mark, SV **sp)
                /* "short" is technically wrong but much more portable
                   than guessing about u_?short(_t)? */
        }
+#else
+       croak("%s not implemented", PL_op_desc[optype]);
+#endif
        break;
 #endif
 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
     default:
-       croak("%s not implemented", op_desc[optype]);
+       croak("%s not implemented", PL_op_desc[optype]);
 #endif
     }
 
@@ -1513,7 +1554,9 @@ do_ipcctl(I32 optype, SV **mark, SV **sp)
            a = SvPV(astr, len);
            if (len != infosize)
                croak("Bad arg length for %s, is %lu, should be %ld",
-                       op_desc[optype], (unsigned long)len, (long)infosize);
+                     PL_op_desc[optype],
+                     (unsigned long)len,
+                     (long)infosize);
        }
     }
     else
@@ -1531,10 +1574,14 @@ do_ipcctl(I32 optype, SV **mark, SV **sp)
 #endif
 #ifdef HAS_SEM
     case OP_SEMCTL: {
+#ifdef Semctl
             union semun unsemds;
 
             unsemds.buf = (struct semid_ds *)a;
            ret = Semctl(id, n, cmd, unsemds);
+#else
+           croak("%s not implemented", PL_op_desc[optype]);
+#endif
         }
        break;
 #endif
@@ -1591,12 +1638,6 @@ do_msgrcv(SV **mark, SV **sp)
     msize = SvIVx(*++mark);
     mtype = (long)SvIVx(*++mark);
     flags = SvIVx(*++mark);
-    if (SvTHINKFIRST(mstr)) {
-       if (SvREADONLY(mstr))
-           croak("Can't msgrcv to readonly var");
-       if (SvROK(mstr))
-           sv_unref(mstr);
-    }
     SvPV_force(mstr, len);
     mbuf = SvGROW(mstr, sizeof(long)+msize+1);