[inseparable changes from patch from perl5.003_12 to perl5.003_13]
[p5sagit/p5-mst-13.2.git] / pp_sys.c
index d580fba..5e096fe 100644 (file)
--- a/pp_sys.c
+++ b/pp_sys.c
@@ -98,10 +98,50 @@ static int dooneliner _((char *cmd, char *filename));
 # define my_chsize chsize
 #endif
 
-#if !defined(HAS_FLOCK) && defined(HAS_LOCKF)
-  static int lockf_emulate_flock _((int fd, int operation));
-# define flock lockf_emulate_flock
-#endif
+#ifdef HAS_FLOCK
+#  define FLOCK flock
+#else /* no flock() */
+
+   /* fcntl.h might not have been included, even if it exists, because
+      the current Configure only sets I_FCNTL if it's needed to pick up
+      the *_OK constants.  Make sure it has been included before testing
+      the fcntl() locking constants. */
+#  if defined(HAS_FCNTL) && !defined(I_FCNTL)
+#    include <fcntl.h>
+#  endif
+
+#  if defined(HAS_FCNTL) && defined(F_SETLK) && defined (F_SETLKW)
+#    define FLOCK fcntl_emulate_flock
+#    define FCNTL_EMULATE_FLOCK
+#  else /* no flock() or fcntl(F_SETLK,...) */
+#    ifdef HAS_LOCKF
+#      define FLOCK lockf_emulate_flock
+#      define LOCKF_EMULATE_FLOCK
+#    endif /* lockf */
+#  endif /* no flock() or fcntl(F_SETLK,...) */
+
+#  ifdef FLOCK
+     static int FLOCK(int, int);
+
+    /*
+     * These are the flock() constants.  Since this sytems doesn't have
+     * flock(), the values of the constants are probably not available.
+     */
+#    ifndef LOCK_SH
+#      define LOCK_SH 1
+#    endif
+#    ifndef LOCK_EX
+#      define LOCK_EX 2
+#    endif
+#    ifndef LOCK_NB
+#      define LOCK_NB 4
+#    endif
+#    ifndef LOCK_UN
+#      define LOCK_UN 8
+#    endif
+#  endif /* emulating flock() */
+
+#endif /* no flock() */
 
 
 /* Pushy I/O. */
@@ -244,18 +284,18 @@ PP(pp_open)
 
     if (MAXARG > 1)
        sv = POPs;
-    else if (SvTYPE(TOPs) == SVt_PVGV)
-       sv = GvSV(TOPs);
-    else
+    if (!isGV(TOPs))
        DIE(no_usym, "filehandle");
+    if (MAXARG <= 1)
+       sv = GvSV(TOPs);
     gv = (GV*)POPs;
-    if (IoFLAGS(GvIOn(gv)) & IOf_UNTAINT) /* This GV has UNTAINT previously set */
-       IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT; /* Clear it. We don't carry that over */
+    if (!isGV(gv))
+       DIE(no_usym, "filehandle");
+    if (GvIOp(gv))
+       IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
     tmps = SvPV(sv, len);
-    if (do_open(gv, tmps, len, FALSE, 0, 0, Nullfp)) {
-       IoLINES(GvIOp(gv)) = 0;
+    if (do_open(gv, tmps, len, FALSE, 0, 0, Nullfp))
        PUSHi( (I32)forkprocess );
-    }
     else if (forkprocess == 0)         /* we are a new child */
        PUSHi(0);
     else
@@ -1007,12 +1047,12 @@ PP(pp_prtf)
        goto just_say_no;
     }
     else {
-#ifdef LC_NUMERIC
+#ifdef USE_LOCALE_NUMERIC
        if (op->op_private & OPpLOCALE)
-           NUMERIC_LOCAL();
+           SET_NUMERIC_LOCAL();
        else
-           NUMERIC_STANDARD();
-#endif /* LC_NUMERIC */
+           SET_NUMERIC_STANDARD();
+#endif
        do_sprintf(sv, SP - MARK, MARK + 1);
        if (!do_print(sv, fp))
            goto just_say_no;
@@ -1077,6 +1117,8 @@ PP(pp_sysread)
     if (!gv)
        goto say_undef;
     bufsv = *++MARK;
+    if (! SvOK(bufsv))
+       sv_setpvn(bufsv, "", 0);
     buffer = SvPV_force(bufsv, blen);
     length = SvIVx(*++MARK);
     if (length < 0)
@@ -1418,7 +1460,7 @@ PP(pp_flock)
     GV *gv;
     PerlIO *fp;
 
-#if defined(HAS_FLOCK) || defined(flock)
+#ifdef FLOCK
     argtype = POPi;
     if (MAXARG <= 0)
        gv = last_in_gv;
@@ -1429,7 +1471,7 @@ PP(pp_flock)
     else
        fp = Nullfp;
     if (fp) {
-       value = (I32)(flock(PerlIO_fileno(fp), argtype) >= 0);
+       value = (I32)(FLOCK(PerlIO_fileno(fp), argtype) >= 0);
     }
     else
        value = 0;
@@ -1855,13 +1897,10 @@ PP(pp_stat)
            laststype = OP_STAT;
            statgv = tmpgv;
            sv_setpv(statname, "");
-           if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)) ||
-             Fstat(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), &statcache) < 0) {
-               max = 0;
-               laststatval = -1;
-           }
+           laststatval = (GvIO(tmpgv) && IoIFP(GvIOp(tmpgv))
+               ? Fstat(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), &statcache) : -1);
        }
-       else if (laststatval < 0)
+       if (laststatval < 0)
            max = 0;
     }
     else {
@@ -1890,15 +1929,17 @@ PP(pp_stat)
        }
     }
 
-    EXTEND(SP, 13);
-    EXTEND_MORTAL(13);
     if (GIMME != G_ARRAY) {
+       EXTEND(SP, 1);
        if (max)
            RETPUSHYES;
        else
            RETPUSHUNDEF;
     }
     if (max) {
+       EXTEND(SP, max);
+       EXTEND_MORTAL(max);
+
        PUSHs(sv_2mortal(newSViv((I32)statcache.st_dev)));
        PUSHs(sv_2mortal(newSViv((I32)statcache.st_ino)));
        PUSHs(sv_2mortal(newSViv((I32)statcache.st_mode)));
@@ -2232,11 +2273,21 @@ PP(pp_fttext)
     STDCHAR tbuf[512];
     register STDCHAR *s;
     register IO *io;
-    SV *sv;
+    register SV *sv;
+    GV *gv;
 
-    if (op->op_flags & OPf_REF) {
+    if (op->op_flags & OPf_REF)
+       gv = cGVOP->op_gv;
+    else if (isGV(TOPs))
+       gv = (GV*)POPs;
+    else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
+       gv = (GV*)SvRV(POPs);
+    else
+       gv = Nullgv;
+
+    if (gv) {
        EXTEND(SP, 1);
-       if (cGVOP->op_gv == defgv) {
+       if (gv == defgv) {
            if (statgv)
                io = GvIO(statgv);
            else {
@@ -2245,13 +2296,17 @@ PP(pp_fttext)
            }
        }
        else {
-           statgv = cGVOP->op_gv;
+           statgv = gv;
+           laststatval = -1;
            sv_setpv(statname, "");
            io = GvIO(statgv);
        }
        if (io && IoIFP(io)) {
-          if (PerlIO_has_base(IoIFP(io))) {
-           Fstat(PerlIO_fileno(IoIFP(io)), &statcache);
+           if (! PerlIO_has_base(IoIFP(io)))
+               DIE("-T and -B not implemented on filehandles");
+           laststatval = Fstat(PerlIO_fileno(IoIFP(io)), &statcache);
+           if (laststatval < 0)
+               RETPUSHUNDEF;
            if (S_ISDIR(statcache.st_mode))     /* handle NFS glitch */
                if (op->op_type == OP_FTTEXT)
                    RETPUSHNO;
@@ -2269,10 +2324,6 @@ PP(pp_fttext)
            /* sfio can have large buffers - limit to 512 */
            if (len > 512)
                len = 512;
-         }
-          else {
-           DIE("-T and -B not implemented on filehandles");
-         }
        }
        else {
            if (dowarn)
@@ -2284,9 +2335,10 @@ PP(pp_fttext)
     }
     else {
        sv = POPs;
+      really_filename:
        statgv = Nullgv;
+       laststatval = -1;
        sv_setpv(statname, SvPV(sv, na));
-      really_filename:
 #ifdef HAS_OPEN3
        i = open(SvPV(sv, na), O_RDONLY, 0);
 #else
@@ -2297,7 +2349,9 @@ PP(pp_fttext)
                warn(warn_nl, "open");
            RETPUSHUNDEF;
        }
-       Fstat(i, &statcache);
+       laststatval = Fstat(i, &statcache);
+       if (laststatval < 0)
+           RETPUSHUNDEF;
        len = read(i, tbuf, 512);
        (void)close(i);
        if (len <= 0) {
@@ -2856,8 +2910,7 @@ PP(pp_system)
     int childpid;
     int result;
     int status;
-    Signal_t (*ihand)();     /* place to save signal during system() */
-    Signal_t (*qhand)();     /* place to save signal during system() */
+    Sigsave_t ihand,qhand;     /* place to save signals during system() */
 
 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2)
     if (SP - MARK == 1) {
@@ -2877,13 +2930,13 @@ PP(pp_system)
        sleep(5);
     }
     if (childpid > 0) {
-       ihand = signal(SIGINT, SIG_IGN);
-       qhand = signal(SIGQUIT, SIG_IGN);
+       rsignal_save(SIGINT, SIG_IGN, &ihand);
+       rsignal_save(SIGQUIT, SIG_IGN, &qhand);
        do {
            result = wait4pid(childpid, &status, 0);
        } while (result == -1 && errno == EINTR);
-       (void)signal(SIGINT, ihand);
-       (void)signal(SIGQUIT, qhand);
+       (void)rsignal_restore(SIGINT, &ihand);
+       (void)rsignal_restore(SIGQUIT, &qhand);
        statusvalue = FIXSTATUS(status);
        if (result < 0)
            value = -1;
@@ -3622,8 +3675,11 @@ PP(pp_gservent)
     }
     else if (which == OP_GSBYPORT) {
        char *proto = POPp;
-       int port = POPi;
+       unsigned short port = POPu;
 
+#ifdef HAS_HTONS
+       port = htons(port);
+#endif
        sent = getservbyport(port, proto);
     }
     else
@@ -4079,7 +4135,42 @@ PP(pp_syscall)
 #endif
 }
 
-#if !defined(HAS_FLOCK) && defined(HAS_LOCKF)
+#ifdef FCNTL_EMULATE_FLOCK
+/*  XXX Emulate flock() with fcntl().
+    What's really needed is a good file locking module.
+*/
+
+static int
+fcntl_emulate_flock(fd, operation)
+int fd;
+int operation;
+{
+    struct flock flock;
+    switch (operation & ~LOCK_NB) {
+    case LOCK_SH:
+       flock.l_type = F_RDLCK;
+       break;
+    case LOCK_EX:
+       flock.l_type = F_WRLCK;
+       break;
+    case LOCK_UN:
+       flock.l_type = F_UNLCK;
+       break;
+    default:
+       errno = EINVAL;
+       return -1;
+    }
+    flock.l_whence = SEEK_SET;
+    flock.l_start = flock.l_len = 0L;
+    return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
+}
+
+#endif /* FCNTL_EMULATE_FLOCK */
+
+#ifdef LOCKF_EMULATE_FLOCK
 
 /*  XXX Emulate flock() with lockf().  This is just to increase
     portability of scripts.  The calls are not completely
@@ -4109,22 +4200,6 @@ PP(pp_syscall)
 #  define F_TEST       3       /* Test a region for other processes locks */
 # endif
 
-/* These are the flock() constants.  Since this sytems doesn't have
-   flock(), the values of the constants are probably not available.
-*/
-# ifndef LOCK_SH
-#  define LOCK_SH 1
-# endif
-# ifndef LOCK_EX
-#  define LOCK_EX 2
-# endif
-# ifndef LOCK_NB
-#  define LOCK_NB 4
-# endif
-# ifndef LOCK_UN
-#  define LOCK_UN 8
-# endif
-
 static int
 lockf_emulate_flock (fd, operation)
 int fd;
@@ -4150,8 +4225,9 @@ int operation;
                    errno = EWOULDBLOCK;
            break;
 
-       /* LOCK_UN - unlock */
+       /* LOCK_UN - unlock (non-blocking is a no-op) */
        case LOCK_UN:
+       case LOCK_UN|LOCK_NB:
            i = lockf (fd, F_ULOCK, 0);
            break;
 
@@ -4163,4 +4239,5 @@ int operation;
     }
     return (i);
 }
-#endif
+
+#endif /* LOCKF_EMULATE_FLOCK */