This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Avoid updating a variable in a loop.
[perl5.git] / doio.c
diff --git a/doio.c b/doio.c
index 47eb516..e018964 100644 (file)
--- a/doio.c
+++ b/doio.c
@@ -81,6 +81,7 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
              int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
              I32 num_svs)
 {
+    dVAR;
     register IO *io = GvIOn(gv);
     PerlIO *saveifp = Nullfp;
     PerlIO *saveofp = Nullfp;
@@ -674,10 +675,10 @@ Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
                 sv = *av_fetch(PL_fdpid,fd,TRUE);
                 (void)SvUPGRADE(sv, SVt_IV);
                 pid = SvIVX(sv);
-                SvIVX(sv) = 0;
+                SvIV_set(sv, 0);
                 sv = *av_fetch(PL_fdpid,savefd,TRUE);
                 (void)SvUPGRADE(sv, SVt_IV);
-                SvIVX(sv) = pid;
+                SvIV_set(sv, pid);
                 UNLOCK_FDPID_MUTEX;
             }
 #endif
@@ -821,7 +822,7 @@ Perl_nextargv(pTHX_ register GV *gv)
                        sv_catpv(sv,PL_inplace);
                    }
 #ifndef FLEXFILENAMES
-                   if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
+                   if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
                         && PL_statbuf.st_dev == filedev
                         && PL_statbuf.st_ino == fileino)
 #ifdef DJGPP
@@ -839,7 +840,7 @@ Perl_nextargv(pTHX_ register GV *gv)
 #endif
 #ifdef HAS_RENAME
 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
-                   if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
+                   if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
                        if (ckWARN_d(WARN_INPLACE))     
                            Perl_warner(aTHX_ packWARN(WARN_INPLACE),
                              "Can't rename %s to %"SVf": %s, skipping file",
@@ -849,13 +850,13 @@ Perl_nextargv(pTHX_ register GV *gv)
                    }
 #else
                    do_close(gv,FALSE);
-                   (void)PerlLIO_unlink(SvPVX(sv));
-                   (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
+                   (void)PerlLIO_unlink(SvPVX_const(sv));
+                   (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
                    do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
 #endif /* DOSISH */
 #else
-                   (void)UNLINK(SvPVX(sv));
-                   if (link(PL_oldname,SvPVX(sv)) < 0) {
+                   (void)UNLINK(SvPVX_const(sv));
+                   if (link(PL_oldname,SvPVX_const(sv)) < 0) {
                        if (ckWARN_d(WARN_INPLACE))     
                            Perl_warner(aTHX_ packWARN(WARN_INPLACE),
                              "Can't rename %s to %"SVf": %s, skipping file",
@@ -925,7 +926,7 @@ Perl_nextargv(pTHX_ register GV *gv)
        }
        else {
            if (ckWARN_d(WARN_INPLACE)) {
-               int eno = errno;
+               const int eno = errno;
                if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
                    && !S_ISREG(PL_statbuf.st_mode))    
                {
@@ -1039,14 +1040,13 @@ bool
 Perl_io_close(pTHX_ IO *io, bool not_implicit)
 {
     bool retval = FALSE;
-    int status;
 
     if (IoIFP(io)) {
        if (IoTYPE(io) == IoTYPE_PIPE) {
-           status = PerlProc_pclose(IoIFP(io));
+           const int status = PerlProc_pclose(IoIFP(io));
            if (not_implicit) {
                STATUS_NATIVE_SET(status);
-               retval = (STATUS_POSIX == 0);
+               retval = (STATUS_UNIX == 0);
            }
            else {
                retval = (status != -1);
@@ -1176,7 +1176,7 @@ Perl_mode_from_discipline(pTHX_ SV *discp)
     int mode = O_BINARY;
     if (discp) {
        STRLEN len;
-       char *s = SvPV(discp,len);
+       const char *s = SvPV(discp,len);
        while (*s) {
            if (*s == ':') {
                switch (s[1]) {
@@ -1209,7 +1209,7 @@ Perl_mode_from_discipline(pTHX_ SV *discp)
                --len;
            }
            else {
-               char *end;
+               const char *end;
 fail_discipline:
                end = strchr(s+1, ':');
                if (!end)
@@ -1241,9 +1241,8 @@ Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
 }
 
 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
-I32 my_chsize(fd, length)
-I32 fd;                        /* file descriptor */
-Off_t length;          /* length to set file to */
+I32
+my_chsize(int fd, Off_t length)
 {
 #ifdef F_FREESP
        /* code courtesy of William Kucharski */
@@ -1287,12 +1286,11 @@ Off_t length;           /* length to set file to */
            return -1;
 
     }
-
     return 0;
 #else
-    dTHX;
-    DIE(aTHX_ "truncate not implemented");
+    Perl_croak_nocontext("truncate not implemented");
 #endif /* F_FREESP */
+    return -1;
 }
 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
 
@@ -1375,7 +1373,7 @@ Perl_my_stat(pTHX)
        io = GvIO(gv);
        if (io && IoIFP(io)) {
            PL_statgv = gv;
-           sv_setpv(PL_statname,"");
+           sv_setpvn(PL_statname,"", 0);
            PL_laststype = OP_STAT;
            return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
        }
@@ -1385,7 +1383,7 @@ Perl_my_stat(pTHX)
            if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
                report_evil_fh(gv, io, PL_op->op_type);
            PL_statgv = Nullgv;
-           sv_setpv(PL_statname,"");
+           sv_setpvn(PL_statname,"", 0);
            return (PL_laststatval = -1);
        }
     }
@@ -1394,7 +1392,7 @@ Perl_my_stat(pTHX)
     }
     else {
        SV* sv = POPs;
-       char *s;
+       const char *s;
        STRLEN len;
        PUTBACK;
        if (SvTYPE(sv) == SVt_PVGV) {
@@ -1409,7 +1407,7 @@ Perl_my_stat(pTHX)
        s = SvPV(sv, len);
        PL_statgv = Nullgv;
        sv_setpvn(PL_statname, s, len);
-       s = SvPVX(PL_statname);         /* s now NUL-terminated */
+       s = SvPVX_const(PL_statname);           /* s now NUL-terminated */
        PL_laststype = OP_STAT;
        PL_laststatval = PerlLIO_stat(s, &PL_statcache);
        if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
@@ -1418,7 +1416,7 @@ Perl_my_stat(pTHX)
     }
 }
 
-static char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
+static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
 
 I32
 Perl_my_lstat(pTHX)
@@ -1452,6 +1450,7 @@ Perl_my_lstat(pTHX)
                GvENAME((GV*) SvRV(sv)));
        return (PL_laststatval = -1);
     }
+    /* XXX Do really need to be calling SvPV() all these times? */
     sv_setpv(PL_statname,SvPV(sv, n_a));
     PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
     if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
@@ -1471,7 +1470,8 @@ bool
 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
               int fd, int do_report)
 {
-#ifdef MACOS_TRADITIONAL
+    dVAR;
+#if defined(MACOS_TRADITIONAL) || defined(SYMBIAN)
     Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
 #else
     register char **a;
@@ -1527,7 +1527,7 @@ Perl_do_execfree(pTHX)
     }
 }
 
-#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
+#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(SYMBIAN) && !defined(MACOS_TRADITIONAL)
 
 bool
 Perl_do_exec(pTHX_ char *cmd)
@@ -1538,6 +1538,7 @@ Perl_do_exec(pTHX_ char *cmd)
 bool
 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
 {
+    dVAR;
     register char **a;
     register char *s;
 
@@ -1699,7 +1700,7 @@ Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
            APPLY_TAINT_PROPER();
            tot = sp - mark;
            while (++mark <= sp) {
-               char *name = SvPVx(*mark, n_a);
+               const char *name = SvPVx(*mark, n_a);
                APPLY_TAINT_PROPER();
                if (PerlLIO_chmod(name, val))
                    tot--;
@@ -1717,7 +1718,7 @@ Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
            APPLY_TAINT_PROPER();
            tot = sp - mark;
            while (++mark <= sp) {
-               char *name = SvPVx(*mark, n_a);
+               const char *name = SvPVx(*mark, n_a);
                APPLY_TAINT_PROPER();
                if (PerlLIO_chown(name, val, val2))
                    tot--;
@@ -1858,12 +1859,13 @@ nothing in the core.
                 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
 #endif
             }
-            APPLY_TAINT_PROPER();
+           APPLY_TAINT_PROPER();
            tot = sp - mark;
            while (++mark <= sp) {
-               char *name = SvPVx(*mark, n_a);
+               STRLEN n_a;
+               const char *name = SvPVx(*mark, n_a);
                APPLY_TAINT_PROPER();
-               if (PerlLIO_utime(name, utbufp))
+               if (PerlLIO_utime(name, utbufp))
                    tot--;
            }
        }
@@ -1880,7 +1882,7 @@ nothing in the core.
 /* Do the permissions allow some operation?  Assumes statcache already set. */
 #ifndef VMS /* VMS' cando is in vms.c */
 bool
-Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
+Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register const Stat_t *statbufp)
 /* Note: we use `effective' both for uids and gids.
  * Here we are betting on Uid_t being equal or wider than Gid_t.  */
 {
@@ -2236,7 +2238,7 @@ Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
 {
 #ifdef HAS_SHM
     SV *mstr;
-    char *mbuf, *shm;
+    char *shm;
     I32 mpos, msize;
     STRLEN len;
     struct shmid_ds shmds;
@@ -2257,6 +2259,7 @@ Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
     if (shm == (char *)-1)     /* I hate System V IPC, I really do */
        return -1;
     if (optype == OP_SHMREAD) {
+       const char *mbuf;
        /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
        if (! SvOK(mstr))
            sv_setpvn(mstr, "", 0);
@@ -2275,7 +2278,7 @@ Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
     else {
        I32 n;
 
-       mbuf = SvPV(mstr, len);
+       const char *mbuf = SvPV(mstr, len);
        if ((n = len) > msize)
            n = msize;
        Copy(mbuf, shm + mpos, n, char);
@@ -2306,6 +2309,7 @@ Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
 PerlIO *
 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
 {
+    dVAR;
     SV *tmpcmd = NEWSV(55, 0);
     PerlIO *fp;
     ENTER;
@@ -2352,7 +2356,7 @@ Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
        }
        if ((tmpfp = PerlIO_tmpfile()) != NULL) {
            Stat_t st;
-           if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
+           if (!PerlLIO_stat(SvPVX_const(tmpglob),&st) && S_ISDIR(st.st_mode))
                ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
            else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
            if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
@@ -2439,3 +2443,13 @@ Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
     LEAVE;
     return fp;
 }
+
+/*
+ * Local variables:
+ * c-indentation-style: bsd
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * End:
+ *
+ * ex: set ts=8 sts=4 sw=4 noet:
+ */