[asperl] various changes to get asperl working under Borland
[p5sagit/p5-mst-13.2.git] / pp_sys.c
index f0ac319..8159a5c 100644 (file)
--- a/pp_sys.c
+++ b/pp_sys.c
 # include <unistd.h>
 #endif
 
+#ifdef HAS_SYSCALL   
+#ifdef __cplusplus              
+extern "C" int syscall(unsigned long,...);
+#endif
+#endif
+
 #ifdef I_SYS_WAIT
 # include <sys/wait.h>
 #endif
 # include <sys/resource.h>
 #endif
 
-/* Put this after #includes because fork and vfork prototypes may
-   conflict.
-*/
-#ifndef HAS_VFORK
-#   define vfork fork
-#endif
-
 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
 # include <sys/socket.h>
-# include <netdb.h>
+# ifdef I_NETDB
+#  include <netdb.h>
+# endif
 # ifndef ENOTSOCK
 #  ifdef I_NET_ERRNO
 #   include <net/errno.h>
@@ -78,7 +79,11 @@ extern int h_errno;
 #endif
 
 #ifdef I_UTIME
-#include <utime.h>
+#  if defined(_MSC_VER) || defined(__MINGW32__)
+#    include <sys/utime.h>
+#  else
+#    include <utime.h>
+#  endif
 #endif
 #ifdef I_FCNTL
 #include <fcntl.h>
@@ -87,6 +92,20 @@ extern int h_errno;
 #include <sys/file.h>
 #endif
 
+/* Put this after #includes because fork and vfork prototypes may conflict. */
+#ifndef HAS_VFORK
+#   define vfork fork
+#endif
+
+/* Put this after #includes because <unistd.h> defines _XOPEN_*. */
+#ifndef Sock_size_t
+#  if _XOPEN_VERSION >= 5 || defined(_XOPEN_SOURCE_EXTENDED) || defined(__GLIBC__)
+#    define Sock_size_t Size_t
+#  else
+#    define Sock_size_t int
+#  endif
+#endif
+
 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
 static int dooneliner _((char *cmd, char *filename));
 #endif
@@ -95,7 +114,7 @@ static int dooneliner _((char *cmd, char *filename));
 # ifdef my_chsize  /* Probably #defined to Perl_my_chsize in embed.h */
 #   undef my_chsize
 # endif
-# define my_chsize chsize
+# define my_chsize PerlLIO_chsize
 #endif
 
 #ifdef HAS_FLOCK
@@ -143,18 +162,36 @@ static int dooneliner _((char *cmd, char *filename));
 
 #endif /* no flock() */
 
+#ifndef MAXPATHLEN
+#  ifdef PATH_MAX
+#    define MAXPATHLEN PATH_MAX
+#  else
+#    define MAXPATHLEN 1024
+#  endif
+#endif
+
+#define ZBTLEN 10
+static char zero_but_true[ZBTLEN + 1] = "0 but true";
 
 /* Pushy I/O. */
 
 PP(pp_backtick)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     PerlIO *fp;
     char *tmps = POPp;
+    I32 gimme = GIMME_V;
+
     TAINT_PROPER("``");
-    fp = my_popen(tmps, "r");
+    fp = PerlProc_popen(tmps, "r");
     if (fp) {
-       if (GIMME == G_SCALAR) {
+       if (gimme == G_VOID) {
+           char tmpbuf[256];
+           while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
+               /*SUPPRESS 530*/
+               ;
+       }
+       else if (gimme == G_SCALAR) {
            sv_setpv(TARG, ""); /* note that this preserves previous buffer */
            while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
                /*SUPPRESS 530*/
@@ -179,12 +216,12 @@ PP(pp_backtick)
                SvTAINTED_on(sv);
            }
        }
-       STATUS_NATIVE_SET(my_pclose(fp));
+       STATUS_NATIVE_SET(PerlProc_pclose(fp));
        TAINT;          /* "I believe that this is not gratuitous!" */
     }
     else {
        STATUS_NATIVE_SET(-1);
-       if (GIMME == G_SCALAR)
+       if (gimme == G_SCALAR)
            RETPUSHUNDEF;
     }
 
@@ -196,6 +233,17 @@ PP(pp_glob)
     OP *result;
     ENTER;
 
+#ifndef VMS
+    if (tainting) {
+       /*
+        * The external globbing program may use things we can't control,
+        * so for security reasons we must assume the worst.
+        */
+       TAINT;
+       taint_proper(no_security, "glob");
+    }
+#endif /* !VMS */
+
     SAVESPTR(last_in_gv);      /* We don't want this to be permanent. */
     last_in_gv = (GV*)*stack_sp--;
 
@@ -226,7 +274,7 @@ PP(pp_rcatline)
 
 PP(pp_warn)
 {
-    dSP; dMARK;
+    djSP; dMARK;
     char *tmps;
     if (SP - MARK != 1) {
        dTARGET;
@@ -238,11 +286,10 @@ PP(pp_warn)
        tmps = SvPV(TOPs, na);
     }
     if (!tmps || !*tmps) {
-       SV *error = GvSV(errgv);
-       (void)SvUPGRADE(error, SVt_PV);
-       if (SvPOK(error) && SvCUR(error))
-           sv_catpv(error, "\t...caught");
-       tmps = SvPV(error, na);
+       (void)SvUPGRADE(ERRSV, SVt_PV);
+       if (SvPOK(ERRSV) && SvCUR(ERRSV))
+           sv_catpv(ERRSV, "\t...caught");
+       tmps = SvPV(ERRSV, na);
     }
     if (!tmps || !*tmps)
        tmps = "Warning: something's wrong";
@@ -252,7 +299,7 @@ PP(pp_warn)
 
 PP(pp_die)
 {
-    dSP; dMARK;
+    djSP; dMARK;
     char *tmps;
     if (SP - MARK != 1) {
        dTARGET;
@@ -264,11 +311,10 @@ PP(pp_die)
        tmps = SvPV(TOPs, na);
     }
     if (!tmps || !*tmps) {
-       SV *error = GvSV(errgv);
-       (void)SvUPGRADE(error, SVt_PV);
-       if (SvPOK(error) && SvCUR(error))
-           sv_catpv(error, "\t...propagated");
-       tmps = SvPV(error, na);
+       (void)SvUPGRADE(ERRSV, SVt_PV);
+       if (SvPOK(ERRSV) && SvCUR(ERRSV))
+           sv_catpv(ERRSV, "\t...propagated");
+       tmps = SvPV(ERRSV, na);
     }
     if (!tmps || !*tmps)
        tmps = "Died";
@@ -279,7 +325,7 @@ PP(pp_die)
 
 PP(pp_open)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     GV *gv;
     SV *sv;
     char *tmps;
@@ -308,21 +354,33 @@ PP(pp_open)
 
 PP(pp_close)
 {
-    dSP;
+    djSP;
     GV *gv;
+    MAGIC *mg;
 
     if (MAXARG == 0)
        gv = defoutgv;
     else
        gv = (GV*)POPs;
+
+    if (SvRMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
+       PUSHMARK(SP);
+       XPUSHs(mg->mg_obj);
+       PUTBACK;
+       ENTER;
+       perl_call_method("CLOSE", G_SCALAR);
+       LEAVE;
+       SPAGAIN;
+       RETURN;
+    }
     EXTEND(SP, 1);
-    PUSHs( do_close(gv, TRUE) ? &sv_yes : &sv_no );
+    PUSHs(boolSV(do_close(gv, TRUE)));
     RETURN;
 }
 
 PP(pp_pipe_op)
 {
-    dSP;
+    djSP;
 #ifdef HAS_PIPE
     GV *rgv;
     GV *wgv;
@@ -346,7 +404,7 @@ PP(pp_pipe_op)
     if (IoIFP(wstio))
        do_close(wgv, FALSE);
 
-    if (pipe(fd) < 0)
+    if (PerlProc_pipe(fd) < 0)
        goto badexit;
 
     IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
@@ -357,9 +415,9 @@ PP(pp_pipe_op)
 
     if (!IoIFP(rstio) || !IoOFP(wstio)) {
        if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
-       else close(fd[0]);
+       else PerlLIO_close(fd[0]);
        if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
-       else close(fd[1]);
+       else PerlLIO_close(fd[1]);
        goto badexit;
     }
 
@@ -374,7 +432,7 @@ badexit:
 
 PP(pp_fileno)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     GV *gv;
     IO *io;
     PerlIO *fp;
@@ -389,16 +447,16 @@ PP(pp_fileno)
 
 PP(pp_umask)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     int anum;
 
 #ifdef HAS_UMASK
     if (MAXARG < 1) {
-       anum = umask(0);
-       (void)umask(anum);
+       anum = PerlLIO_umask(0);
+       (void)PerlLIO_umask(anum);
     }
     else
-       anum = umask(POPi);
+       anum = PerlLIO_umask(POPi);
     TAINT_PROPER("umask");
     XPUSHi(anum);
 #else
@@ -409,7 +467,7 @@ PP(pp_umask)
 
 PP(pp_binmode)
 {
-    dSP;
+    djSP;
     GV *gv;
     IO *io;
     PerlIO *fp;
@@ -430,8 +488,19 @@ PP(pp_binmode)
     else
        RETPUSHUNDEF;
 #else
-    if (setmode(PerlIO_fileno(fp), OP_BINARY) != -1)
+    if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
+#if defined(WIN32) && defined(__BORLANDC__)
+       /* The translation mode of the stream is maintained independent
+        * of the translation mode of the fd in the Borland RTL (heavy
+        * digging through their runtime sources reveal).  User has to
+        * set the mode explicitly for the stream (though they don't
+        * document this anywhere). GSAR 97-5-24
+        */
+       PerlIO_seek(fp,0L,0);
+       ((FILE*)fp)->flags |= _F_BIN;
+#endif
        RETPUSHYES;
+    }
     else
        RETPUSHUNDEF;
 #endif
@@ -448,64 +517,59 @@ PP(pp_binmode)
 
 }
 
+
 PP(pp_tie)
 {
-    dSP;
+    djSP;
     SV *varsv;
     HV* stash;
     GV *gv;
-    BINOP myop;
     SV *sv;
     SV **mark = stack_base + ++*markstack_ptr; /* reuse in entersub */
     I32 markoff = mark - stack_base - 1;
     char *methname;
-    bool oldmustcatch = mustcatch;
-
-    varsv = mark[0];
-    if (SvTYPE(varsv) == SVt_PVHV)
-       methname = "TIEHASH";
-    else if (SvTYPE(varsv) == SVt_PVAV)
-       methname = "TIEARRAY";
-    else if (SvTYPE(varsv) == SVt_PVGV)
-       methname = "TIEHANDLE";
-    else
-       methname = "TIESCALAR";
+    int how = 'P';
 
-    stash = gv_stashsv(mark[1], FALSE);
-    if (!stash || !(gv = gv_fetchmethod(stash, methname)))
-       DIE("Can't locate object method \"%s\" via package \"%s\"",
-               methname, SvPV(mark[1],na));
-
-    Zero(&myop, 1, BINOP);
-    myop.op_last = (OP *) &myop;
-    myop.op_next = Nullop;
-    myop.op_flags = OPf_KNOW|OPf_STACKED;
-    mustcatch = TRUE;
-
-    ENTER;
-    SAVESPTR(op);
-    op = (OP *) &myop;
-    if (perldb && curstash != debstash)
-       op->op_private |= OPpENTERSUB_DB;
-
-    XPUSHs((SV*)GvCV(gv));
-    PUTBACK;
+    varsv = mark[0];  
+    switch(SvTYPE(varsv)) {
+       case SVt_PVHV:
+           methname = "TIEHASH";
+           break;
+       case SVt_PVAV:
+           methname = "TIEARRAY";
+           break;
+       case SVt_PVGV:
+           methname = "TIEHANDLE";
+           how = 'q';
+           break;
+       default:
+           methname = "TIESCALAR";
+           how = 'q';
+           break;
+    }
 
-    if (op = pp_entersub())
-        runops();
+    if (sv_isobject(mark[1])) {
+       ENTER;
+       perl_call_method(methname, G_SCALAR);
+    } 
+    else {
+       /* Not clear why we don't call perl_call_method here too.
+        * perhaps to get different error message ?
+        */
+       stash = gv_stashsv(mark[1], FALSE);
+       if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
+           DIE("Can't locate object method \"%s\" via package \"%s\"",
+                methname, SvPV(mark[1],na));                   
+       }
+       ENTER;
+       perl_call_sv((SV*)GvCV(gv), G_SCALAR);
+    }
     SPAGAIN;
 
-    mustcatch = oldmustcatch;
     sv = TOPs;
     if (sv_isobject(sv)) {
-       if (SvTYPE(varsv) == SVt_PVHV || SvTYPE(varsv) == SVt_PVAV) {
-           sv_unmagic(varsv, 'P');
-           sv_magic(varsv, sv, 'P', Nullch, 0);
-       }
-       else {
-           sv_unmagic(varsv, 'q');
-           sv_magic(varsv, sv, 'q', Nullch, 0);
-       }
+       sv_unmagic(varsv, how);            
+       sv_magic(varsv, sv, how, Nullch, 0);
     }
     LEAVE;
     SP = stack_base + markoff;
@@ -515,10 +579,9 @@ PP(pp_tie)
 
 PP(pp_untie)
 {
-    dSP;
+    djSP;
     SV * sv ;
-
-    sv = POPs;
+    sv = POPs;          
 
     if (dowarn) {
         MAGIC * mg ;
@@ -543,7 +606,7 @@ PP(pp_untie)
 
 PP(pp_tied)
 {
-    dSP;
+    djSP;
     SV * sv ;
     MAGIC * mg ;
 
@@ -559,20 +622,17 @@ PP(pp_tied)
             RETURN ;
        }
     }
-
     RETPUSHUNDEF;
 }
 
 PP(pp_dbmopen)
 {
-    dSP;
+    djSP;
     HV *hv;
     dPOPPOPssrl;
     HV* stash;
     GV *gv;
-    BINOP myop;
     SV *sv;
-    bool oldmustcatch = mustcatch;
 
     hv = (HV*)POPs;
 
@@ -587,21 +647,10 @@ PP(pp_dbmopen)
            DIE("No dbm on this machine");
     }
 
-    Zero(&myop, 1, BINOP);
-    myop.op_last = (OP *) &myop;
-    myop.op_next = Nullop;
-    myop.op_flags = OPf_KNOW|OPf_STACKED;
-    mustcatch = TRUE;
-
     ENTER;
-    SAVESPTR(op);
-    op = (OP *) &myop;
-    if (perldb && curstash != debstash)
-       op->op_private |= OPpENTERSUB_DB;
-    PUTBACK;
-    pp_pushmark();
+    PUSHMARK(SP);
 
-    EXTEND(sp, 5);
+    EXTEND(SP, 5);
     PUSHs(sv);
     PUSHs(left);
     if (SvIV(right))
@@ -609,34 +658,26 @@ PP(pp_dbmopen)
     else
        PUSHs(sv_2mortal(newSViv(O_RDWR)));
     PUSHs(right);
-    PUSHs((SV*)GvCV(gv));
     PUTBACK;
-
-    if (op = pp_entersub())
-        runops();
+    perl_call_sv((SV*)GvCV(gv), G_SCALAR);
     SPAGAIN;
 
     if (!sv_isobject(TOPs)) {
-       sp--;
-       op = (OP *) &myop;
-       PUTBACK;
-       pp_pushmark();
-
+       SP--;
+       PUSHMARK(SP);
        PUSHs(sv);
        PUSHs(left);
        PUSHs(sv_2mortal(newSViv(O_RDONLY)));
        PUSHs(right);
-       PUSHs((SV*)GvCV(gv));
        PUTBACK;
-
-       if (op = pp_entersub())
-           runops();
+       perl_call_sv((SV*)GvCV(gv), G_SCALAR);
        SPAGAIN;
     }
 
-    mustcatch = oldmustcatch;
-    if (sv_isobject(TOPs))
+    if (sv_isobject(TOPs)) {
+       sv_unmagic((SV *) hv, 'P');            
        sv_magic((SV*)hv, TOPs, 'P', Nullch, 0);
+    }
     LEAVE;
     RETURN;
 }
@@ -648,7 +689,7 @@ PP(pp_dbmclose)
 
 PP(pp_sselect)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
 #ifdef HAS_SELECT
     register I32 i;
     register I32 j;
@@ -746,7 +787,7 @@ PP(pp_sselect)
 #endif
     }
 
-    nfound = select(
+    nfound = PerlSock_select(
        maxlen * 8,
        (Select_fd_set_t) fd_sets[1],
        (Select_fd_set_t) fd_sets[2],
@@ -781,9 +822,9 @@ PP(pp_sselect)
 }
 
 void
-setdefout(gv)
-GV *gv;
+setdefout(GV *gv)
 {
+    dTHR;
     if (gv)
        (void)SvREFCNT_inc(gv);
     if (defoutgv)
@@ -793,11 +834,11 @@ GV *gv;
 
 PP(pp_select)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     GV *newdefout, *egv;
     HV *hv;
 
-    newdefout = (op->op_private > 0) ? ((GV *) POPs) : NULL;
+    newdefout = (op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
 
     egv = GvEGV(defoutgv);
     if (!egv)
@@ -827,7 +868,7 @@ PP(pp_select)
 
 PP(pp_getc)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     GV *gv;
     MAGIC *mg;
 
@@ -838,16 +879,17 @@ PP(pp_getc)
     if (!gv)
        gv = argvgv;
 
-    if (SvMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
+    if (SvRMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
+       I32 gimme = GIMME_V;
        PUSHMARK(SP);
        XPUSHs(mg->mg_obj);
        PUTBACK;
        ENTER;
-       perl_call_method("GETC", GIMME);
+       perl_call_method("GETC", gimme);
        LEAVE;
        SPAGAIN;
-       if (GIMME == G_SCALAR)
-           SvSetSV_nosteal(TARG, TOPs);
+       if (gimme == G_SCALAR)
+           SvSetMagicSV_nosteal(TARG, TOPs);
        RETURN;
     }
     if (!gv || do_eof(gv)) /* make sure we have fp with something */
@@ -864,14 +906,12 @@ PP(pp_read)
     return pp_sysread(ARGS);
 }
 
-static OP *
-doform(cv,gv,retop)
-CV *cv;
-GV *gv;
-OP *retop;
+STATIC OP *
+doform(CV *cv, GV *gv, OP *retop)
 {
-    register CONTEXT *cx;
-    I32 gimme = GIMME;
+    dTHR;
+    register PERL_CONTEXT *cx;
+    I32 gimme = GIMME_V;
     AV* padlist = CvPADLIST(cv);
     SV** svp = AvARRAY(padlist);
 
@@ -890,7 +930,7 @@ OP *retop;
 
 PP(pp_enterwrite)
 {
-    dSP;
+    djSP;
     register GV *gv;
     register IO *io;
     GV *fgv;
@@ -931,14 +971,14 @@ PP(pp_enterwrite)
 
 PP(pp_leavewrite)
 {
-    dSP;
+    djSP;
     GV *gv = cxstack[cxstack_ix].blk_sub.gv;
     register IO *io = GvIOp(gv);
     PerlIO *ofp = IoOFP(io);
     PerlIO *fp;
     SV **newsp;
     I32 gimme;
-    register CONTEXT *cx;
+    register PERL_CONTEXT *cx;
 
     DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
          (long)IoLINES_LEFT(io), (long)FmLINES(formtarget)));
@@ -949,16 +989,16 @@ PP(pp_leavewrite)
        CV *cv;
        if (!IoTOP_GV(io)) {
            GV *topgv;
-           char tmpbuf[256];
+           SV *topname;
 
            if (!IoTOP_NAME(io)) {
                if (!IoFMT_NAME(io))
                    IoFMT_NAME(io) = savepv(GvNAME(gv));
-               sprintf(tmpbuf, "%s_TOP", IoFMT_NAME(io));
-               topgv = gv_fetchpv(tmpbuf,FALSE, SVt_PVFM);
+               topname = sv_2mortal(newSVpvf("%s_TOP", IoFMT_NAME(io)));
+               topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
                if ((topgv && GvFORM(topgv)) ||
                  !gv_fetchpv("top",FALSE,SVt_PVFM))
-                   IoTOP_NAME(io) = savepv(tmpbuf);
+                   IoTOP_NAME(io) = savepv(SvPVX(topname));
                else
                    IoTOP_NAME(io) = savepv("top");
            }
@@ -1045,16 +1085,39 @@ PP(pp_leavewrite)
 
 PP(pp_prtf)
 {
-    dSP; dMARK; dORIGMARK;
+    djSP; dMARK; dORIGMARK;
     GV *gv;
     IO *io;
     PerlIO *fp;
-    SV *sv = NEWSV(0,0);
+    SV *sv;
+    MAGIC *mg;
 
     if (op->op_flags & OPf_STACKED)
        gv = (GV*)*++MARK;
     else
        gv = defoutgv;
+
+    if (SvRMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
+       if (MARK == ORIGMARK) {
+           MEXTEND(SP, 1);
+           ++MARK;
+           Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
+           ++SP;
+       }
+       PUSHMARK(MARK - 1);
+       *MARK = mg->mg_obj;
+       PUTBACK;
+       ENTER;
+       perl_call_method("PRINTF", G_SCALAR);
+       LEAVE;
+       SPAGAIN;
+       MARK = ORIGMARK + 1;
+       *MARK = *SP;
+       SP = MARK;
+       RETURN;
+    }
+
+    sv = NEWSV(0,0);
     if (!(io = GvIO(gv))) {
        if (dowarn) {
            gv_fullname3(sv, gv, Nullch);
@@ -1103,7 +1166,7 @@ PP(pp_prtf)
 
 PP(pp_sysopen)
 {
-    dSP;
+    djSP;
     GV *gv;
     SV *sv;
     char *tmps;
@@ -1131,19 +1194,21 @@ PP(pp_sysopen)
 
 PP(pp_sysread)
 {
-    dSP; dMARK; dORIGMARK; dTARGET;
+    djSP; dMARK; dORIGMARK; dTARGET;
     int offset;
     GV *gv;
     IO *io;
     char *buffer;
-    int length;
+    SSize_t length;
     Sock_size_t bufsize;
     SV *bufsv;
     STRLEN blen;
     MAGIC *mg;
 
     gv = (GV*)*++MARK;
-    if (SvMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
+    if ((op->op_type == OP_READ || op->op_type == OP_SYSREAD) &&
+       SvRMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q')))
+    {
        SV *sv;
        
        PUSHMARK(MARK-1);
@@ -1177,11 +1242,16 @@ PP(pp_sysread)
        goto say_undef;
 #ifdef HAS_SOCKET
     if (op->op_type == OP_RECV) {
-       bufsize = sizeof buf;
+       char namebuf[MAXPATHLEN];
+#if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
+       bufsize = sizeof (struct sockaddr_in);
+#else
+       bufsize = sizeof namebuf;
+#endif
        buffer = SvGROW(bufsv, length+1);
        /* 'offset' means 'flags' here */
-       length = recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
-           (struct sockaddr *)buf, &bufsize);
+       length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
+                         (struct sockaddr *)namebuf, &bufsize);
        if (length < 0)
            RETPUSHUNDEF;
        SvCUR_set(bufsv, length);
@@ -1192,7 +1262,7 @@ PP(pp_sysread)
        if (!(IoFLAGS(io) & IOf_UNTAINT))
            SvTAINTED_on(bufsv);
        SP = ORIGMARK;
-       sv_setpvn(TARG, buf, bufsize);
+       sv_setpvn(TARG, namebuf, bufsize);
        PUSHs(TARG);
        RETURN;
     }
@@ -1211,14 +1281,19 @@ PP(pp_sysread)
        Zero(buffer+bufsize, offset-bufsize, char);
     }
     if (op->op_type == OP_SYSREAD) {
-       length = read(PerlIO_fileno(IoIFP(io)), buffer+offset, length);
+       length = PerlLIO_read(PerlIO_fileno(IoIFP(io)), buffer+offset, length);
     }
     else
 #ifdef HAS_SOCKET__bad_code_maybe
     if (IoTYPE(io) == 's') {
-       bufsize = sizeof buf;
-       length = recvfrom(PerlIO_fileno(IoIFP(io)), buffer+offset, length, 0,
-           (struct sockaddr *)buf, &bufsize);
+       char namebuf[MAXPATHLEN];
+#if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
+       bufsize = sizeof (struct sockaddr_in);
+#else
+       bufsize = sizeof namebuf;
+#endif
+       length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer+offset, length, 0,
+                         (struct sockaddr *)namebuf, &bufsize);
     }
     else
 #endif
@@ -1248,7 +1323,7 @@ PP(pp_syswrite)
 
 PP(pp_send)
 {
-    dSP; dMARK; dORIGMARK; dTARGET;
+    djSP; dMARK; dORIGMARK; dTARGET;
     GV *gv;
     IO *io;
     int offset;
@@ -1256,8 +1331,25 @@ PP(pp_send)
     char *buffer;
     int length;
     STRLEN blen;
+    MAGIC *mg;
 
     gv = (GV*)*++MARK;
+    if (op->op_type == OP_SYSWRITE &&
+       SvRMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q')))
+    {
+       SV *sv;
+       
+       PUSHMARK(MARK-1);
+       *MARK = mg->mg_obj;
+       ENTER;
+       perl_call_method("WRITE", G_SCALAR);
+       LEAVE;
+       SPAGAIN;
+       sv = POPs;
+       SP = ORIGMARK;
+       PUSHs(sv);
+       RETURN;
+    }
     if (!gv)
        goto say_undef;
     bufsv = *++MARK;
@@ -1283,24 +1375,25 @@ PP(pp_send)
                if (-offset > blen)
                    DIE("Offset outside string");
                offset += blen;
-           } else if (offset >= blen)
+           } else if (offset >= blen && blen > 0)
                DIE("Offset outside string");
        } else
            offset = 0;
        if (length > blen - offset)
            length = blen - offset;
-       length = write(PerlIO_fileno(IoIFP(io)), buffer+offset, length);
+       length = PerlLIO_write(PerlIO_fileno(IoIFP(io)), buffer+offset, length);
     }
 #ifdef HAS_SOCKET
     else if (SP > MARK) {
        char *sockbuf;
        STRLEN mlen;
        sockbuf = SvPVx(*++MARK, mlen);
-       length = sendto(PerlIO_fileno(IoIFP(io)), buffer, blen, length,
+       length = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen, length,
                                (struct sockaddr *)sockbuf, mlen);
     }
     else
-       length = send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
+       length = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
+
 #else
     else
        DIE(no_sock_func, "send");
@@ -1323,20 +1416,20 @@ PP(pp_recv)
 
 PP(pp_eof)
 {
-    dSP;
+    djSP;
     GV *gv;
 
     if (MAXARG <= 0)
        gv = last_in_gv;
     else
        gv = last_in_gv = (GV*)POPs;
-    PUSHs(!gv || do_eof(gv) ? &sv_yes : &sv_no);
+    PUSHs(boolSV(!gv || do_eof(gv)));
     RETURN;
 }
 
 PP(pp_tell)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     GV *gv;
 
     if (MAXARG <= 0)
@@ -1349,19 +1442,31 @@ PP(pp_tell)
 
 PP(pp_seek)
 {
-    dSP;
+    return pp_sysseek(ARGS);
+}
+
+PP(pp_sysseek)
+{
+    djSP;
     GV *gv;
     int whence = POPi;
     long offset = POPl;
 
     gv = last_in_gv = (GV*)POPs;
-    PUSHs( do_seek(gv, offset, whence) ? &sv_yes : &sv_no );
+    if (op->op_type == OP_SEEK)
+       PUSHs(boolSV(do_seek(gv, offset, whence)));
+    else {
+       long n = do_sysseek(gv, offset, whence);
+       PUSHs((n < 0) ? &sv_undef
+             : sv_2mortal(n ? newSViv((IV)n)
+                          : newSVpv(zero_but_true, ZBTLEN)));
+    }
     RETURN;
 }
 
 PP(pp_truncate)
 {
-    dSP;
+    djSP;
     Off_t len = (Off_t)POPn;
     int result = 1;
     GV *tmpgv;
@@ -1401,12 +1506,12 @@ PP(pp_truncate)
 #else
        {
            int tmpfd;
-           if ((tmpfd = open(name, O_RDWR)) < 0)
+           if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
                result = 0;
            else {
                if (my_chsize(tmpfd, len) < 0)
                    result = 0;
-               close(tmpfd);
+               PerlLIO_close(tmpfd);
            }
        }
 #endif
@@ -1429,12 +1534,12 @@ PP(pp_fcntl)
 
 PP(pp_ioctl)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     SV *argsv = POPs;
     unsigned int func = U_I(POPn);
     int optype = op->op_type;
     char *s;
-    int retval;
+    IV retval;
     GV *gv = (GV*)POPs;
     IO *io = GvIOn(gv);
 
@@ -1445,29 +1550,26 @@ PP(pp_ioctl)
 
     if (SvPOK(argsv) || !SvNIOK(argsv)) {
        STRLEN len;
+       STRLEN need;
        s = SvPV_force(argsv, len);
-       retval = IOCPARM_LEN(func);
-       if (len < retval) {
-           s = Sv_Grow(argsv, retval+1);
-           SvCUR_set(argsv, retval);
+       need = IOCPARM_LEN(func);
+       if (len < need) {
+           s = Sv_Grow(argsv, need + 1);
+           SvCUR_set(argsv, need);
        }
 
        s[SvCUR(argsv)] = 17;   /* a little sanity check here */
     }
     else {
        retval = SvIV(argsv);
-#ifdef DOSISH
-       s = (char*)(long)retval;        /* ouch */
-#else
        s = (char*)retval;              /* ouch */
-#endif
     }
 
     TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
 
     if (optype == OP_IOCTL)
 #ifdef HAS_IOCTL
-       retval = ioctl(PerlIO_fileno(IoIFP(io)), func, s);
+       retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
 #else
        DIE("ioctl is not implemented");
 #endif
@@ -1496,14 +1598,14 @@ PP(pp_ioctl)
        PUSHi(retval);
     }
     else {
-       PUSHp("0 but true", 10);
+       PUSHp(zero_but_true, ZBTLEN);
     }
     RETURN;
 }
 
 PP(pp_flock)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     I32 value;
     int argtype;
     GV *gv;
@@ -1521,7 +1623,7 @@ PP(pp_flock)
        fp = Nullfp;
     if (fp) {
        (void)PerlIO_flush(fp);
-       value = (I32)(FLOCK(PerlIO_fileno(fp), argtype) >= 0);
+       value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
     }
     else
        value = 0;
@@ -1536,7 +1638,7 @@ PP(pp_flock)
 
 PP(pp_socket)
 {
-    dSP;
+    djSP;
 #ifdef HAS_SOCKET
     GV *gv;
     register IO *io;
@@ -1557,7 +1659,7 @@ PP(pp_socket)
        do_close(gv, FALSE);
 
     TAINT_PROPER("socket");
-    fd = socket(domain, type, protocol);
+    fd = PerlSock_socket(domain, type, protocol);
     if (fd < 0)
        RETPUSHUNDEF;
     IoIFP(io) = PerlIO_fdopen(fd, "r");        /* stdio gets confused about sockets */
@@ -1566,7 +1668,7 @@ PP(pp_socket)
     if (!IoIFP(io) || !IoOFP(io)) {
        if (IoIFP(io)) PerlIO_close(IoIFP(io));
        if (IoOFP(io)) PerlIO_close(IoOFP(io));
-       if (!IoIFP(io) && !IoOFP(io)) close(fd);
+       if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
        RETPUSHUNDEF;
     }
 
@@ -1578,7 +1680,7 @@ PP(pp_socket)
 
 PP(pp_sockpair)
 {
-    dSP;
+    djSP;
 #ifdef HAS_SOCKETPAIR
     GV *gv1;
     GV *gv2;
@@ -1602,7 +1704,7 @@ PP(pp_sockpair)
        do_close(gv2, FALSE);
 
     TAINT_PROPER("socketpair");
-    if (socketpair(domain, type, protocol, fd) < 0)
+    if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
        RETPUSHUNDEF;
     IoIFP(io1) = PerlIO_fdopen(fd[0], "r");
     IoOFP(io1) = PerlIO_fdopen(fd[0], "w");
@@ -1613,10 +1715,10 @@ PP(pp_sockpair)
     if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
        if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
        if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
-       if (!IoIFP(io1) && !IoOFP(io1)) close(fd[0]);
+       if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
        if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
        if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
-       if (!IoIFP(io2) && !IoOFP(io2)) close(fd[1]);
+       if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
        RETPUSHUNDEF;
     }
 
@@ -1628,7 +1730,7 @@ PP(pp_sockpair)
 
 PP(pp_bind)
 {
-    dSP;
+    djSP;
 #ifdef HAS_SOCKET
     SV *addrsv = POPs;
     char *addr;
@@ -1641,7 +1743,7 @@ PP(pp_bind)
 
     addr = SvPV(addrsv, len);
     TAINT_PROPER("bind");
-    if (bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
+    if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
        RETPUSHYES;
     else
        RETPUSHUNDEF;
@@ -1658,7 +1760,7 @@ nuts:
 
 PP(pp_connect)
 {
-    dSP;
+    djSP;
 #ifdef HAS_SOCKET
     SV *addrsv = POPs;
     char *addr;
@@ -1671,7 +1773,7 @@ PP(pp_connect)
 
     addr = SvPV(addrsv, len);
     TAINT_PROPER("connect");
-    if (connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
+    if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
        RETPUSHYES;
     else
        RETPUSHUNDEF;
@@ -1688,7 +1790,7 @@ nuts:
 
 PP(pp_listen)
 {
-    dSP;
+    djSP;
 #ifdef HAS_SOCKET
     int backlog = POPi;
     GV *gv = (GV*)POPs;
@@ -1697,7 +1799,7 @@ PP(pp_listen)
     if (!io || !IoIFP(io))
        goto nuts;
 
-    if (listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
+    if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
        RETPUSHYES;
     else
        RETPUSHUNDEF;
@@ -1714,7 +1816,7 @@ nuts:
 
 PP(pp_accept)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
 #ifdef HAS_SOCKET
     GV *ngv;
     GV *ggv;
@@ -1740,7 +1842,7 @@ PP(pp_accept)
     if (IoIFP(nstio))
        do_close(ngv, FALSE);
 
-    fd = accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
+    fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
     if (fd < 0)
        goto badexit;
     IoIFP(nstio) = PerlIO_fdopen(fd, "r");
@@ -1749,7 +1851,7 @@ PP(pp_accept)
     if (!IoIFP(nstio) || !IoOFP(nstio)) {
        if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
        if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
-       if (!IoIFP(nstio) && !IoOFP(nstio)) close(fd);
+       if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
        goto badexit;
     }
 
@@ -1771,7 +1873,7 @@ badexit:
 
 PP(pp_shutdown)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
 #ifdef HAS_SOCKET
     int how = POPi;
     GV *gv = (GV*)POPs;
@@ -1780,7 +1882,7 @@ PP(pp_shutdown)
     if (!io || !IoIFP(io))
        goto nuts;
 
-    PUSHi( shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
+    PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
     RETURN;
 
 nuts:
@@ -1804,7 +1906,7 @@ PP(pp_gsockopt)
 
 PP(pp_ssockopt)
 {
-    dSP;
+    djSP;
 #ifdef HAS_SOCKET
     int optype = op->op_type;
     SV *sv;
@@ -1835,7 +1937,7 @@ PP(pp_ssockopt)
        SvCUR_set(sv,256);
        *SvEND(sv) ='\0';
        len = SvCUR(sv);
-       if (getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
+       if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
            goto nuts2;
        SvCUR_set(sv, len);
        *SvEND(sv) ='\0';
@@ -1853,7 +1955,7 @@ PP(pp_ssockopt)
                buf = (char*)&aint;
                len = sizeof(int);
            }
-           if (setsockopt(fd, lvl, optname, buf, len) < 0)
+           if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
                goto nuts2;
            PUSHs(&sv_yes);
        }
@@ -1884,7 +1986,7 @@ PP(pp_getsockname)
 
 PP(pp_getpeername)
 {
-    dSP;
+    djSP;
 #ifdef HAS_SOCKET
     int optype = op->op_type;
     SV *sv;
@@ -1904,12 +2006,23 @@ PP(pp_getpeername)
     fd = PerlIO_fileno(IoIFP(io));
     switch (optype) {
     case OP_GETSOCKNAME:
-       if (getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
+       if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
            goto nuts2;
        break;
     case OP_GETPEERNAME:
-       if (getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
+       if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
            goto nuts2;
+#if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
+       {
+           static const char nowhere[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
+           /* If the call succeeded, make sure we don't have a zeroed port/addr */
+           if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
+               !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
+                       sizeof(u_short) + sizeof(struct in_addr))) {
+               goto nuts2;         
+           }
+       }
+#endif
        break;
     }
 #ifdef BOGUS_GETNAME_RETURN
@@ -1944,8 +2057,9 @@ PP(pp_lstat)
 
 PP(pp_stat)
 {
-    dSP;
+    djSP;
     GV *tmpgv;
+    I32 gimme;
     I32 max = 13;
 
     if (op->op_flags & OPf_REF) {
@@ -1956,7 +2070,7 @@ PP(pp_stat)
            statgv = tmpgv;
            sv_setpv(statname, "");
            laststatval = (GvIO(tmpgv) && IoIFP(GvIOp(tmpgv))
-               ? Fstat(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), &statcache) : -1);
+               ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), &statcache) : -1);
        }
        if (laststatval < 0)
            max = 0;
@@ -1976,10 +2090,10 @@ PP(pp_stat)
 #ifdef HAS_LSTAT
        laststype = op->op_type;
        if (op->op_type == OP_LSTAT)
-           laststatval = lstat(SvPV(statname, na), &statcache);
+           laststatval = PerlLIO_lstat(SvPV(statname, na), &statcache);
        else
 #endif
-           laststatval = Stat(SvPV(statname, na), &statcache);
+           laststatval = PerlLIO_stat(SvPV(statname, na), &statcache);
        if (laststatval < 0) {
            if (dowarn && strchr(SvPV(statname, na), '\n'))
                warn(warn_nl, "stat");
@@ -1987,17 +2101,15 @@ PP(pp_stat)
        }
     }
 
-    if (GIMME != G_ARRAY) {
-       EXTEND(SP, 1);
-       if (max)
-           RETPUSHYES;
-       else
-           RETPUSHUNDEF;
+    gimme = GIMME_V;
+    if (gimme != G_ARRAY) {
+       if (gimme != G_VOID)
+           XPUSHs(boolSV(max));
+       RETURN;
     }
     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)));
@@ -2033,7 +2145,7 @@ PP(pp_stat)
 PP(pp_ftrread)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (cando(S_IRUSR, 0, &statcache))
@@ -2044,7 +2156,7 @@ PP(pp_ftrread)
 PP(pp_ftrwrite)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (cando(S_IWUSR, 0, &statcache))
@@ -2055,7 +2167,7 @@ PP(pp_ftrwrite)
 PP(pp_ftrexec)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (cando(S_IXUSR, 0, &statcache))
@@ -2066,7 +2178,7 @@ PP(pp_ftrexec)
 PP(pp_fteread)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (cando(S_IRUSR, 1, &statcache))
@@ -2077,7 +2189,7 @@ PP(pp_fteread)
 PP(pp_ftewrite)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (cando(S_IWUSR, 1, &statcache))
@@ -2088,7 +2200,7 @@ PP(pp_ftewrite)
 PP(pp_fteexec)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (cando(S_IXUSR, 1, &statcache))
@@ -2099,7 +2211,7 @@ PP(pp_fteexec)
 PP(pp_ftis)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     RETPUSHYES;
@@ -2113,7 +2225,7 @@ PP(pp_fteowned)
 PP(pp_ftrowned)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (statcache.st_uid == (op->op_type == OP_FTEOWNED ? euid : uid) )
@@ -2124,7 +2236,7 @@ PP(pp_ftrowned)
 PP(pp_ftzero)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (!statcache.st_size)
@@ -2135,7 +2247,7 @@ PP(pp_ftzero)
 PP(pp_ftsize)
 {
     I32 result = my_stat(ARGS);
-    dSP; dTARGET;
+    djSP; dTARGET;
     if (result < 0)
        RETPUSHUNDEF;
     PUSHi(statcache.st_size);
@@ -2145,7 +2257,7 @@ PP(pp_ftsize)
 PP(pp_ftmtime)
 {
     I32 result = my_stat(ARGS);
-    dSP; dTARGET;
+    djSP; dTARGET;
     if (result < 0)
        RETPUSHUNDEF;
     PUSHn( ((I32)basetime - (I32)statcache.st_mtime) / 86400.0 );
@@ -2155,7 +2267,7 @@ PP(pp_ftmtime)
 PP(pp_ftatime)
 {
     I32 result = my_stat(ARGS);
-    dSP; dTARGET;
+    djSP; dTARGET;
     if (result < 0)
        RETPUSHUNDEF;
     PUSHn( ((I32)basetime - (I32)statcache.st_atime) / 86400.0 );
@@ -2165,7 +2277,7 @@ PP(pp_ftatime)
 PP(pp_ftctime)
 {
     I32 result = my_stat(ARGS);
-    dSP; dTARGET;
+    djSP; dTARGET;
     if (result < 0)
        RETPUSHUNDEF;
     PUSHn( ((I32)basetime - (I32)statcache.st_ctime) / 86400.0 );
@@ -2175,7 +2287,7 @@ PP(pp_ftctime)
 PP(pp_ftsock)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (S_ISSOCK(statcache.st_mode))
@@ -2186,7 +2298,7 @@ PP(pp_ftsock)
 PP(pp_ftchr)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (S_ISCHR(statcache.st_mode))
@@ -2197,7 +2309,7 @@ PP(pp_ftchr)
 PP(pp_ftblk)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (S_ISBLK(statcache.st_mode))
@@ -2208,7 +2320,7 @@ PP(pp_ftblk)
 PP(pp_ftfile)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (S_ISREG(statcache.st_mode))
@@ -2219,7 +2331,7 @@ PP(pp_ftfile)
 PP(pp_ftdir)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (S_ISDIR(statcache.st_mode))
@@ -2230,7 +2342,7 @@ PP(pp_ftdir)
 PP(pp_ftpipe)
 {
     I32 result = my_stat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (S_ISFIFO(statcache.st_mode))
@@ -2241,7 +2353,7 @@ PP(pp_ftpipe)
 PP(pp_ftlink)
 {
     I32 result = my_lstat(ARGS);
-    dSP;
+    djSP;
     if (result < 0)
        RETPUSHUNDEF;
     if (S_ISLNK(statcache.st_mode))
@@ -2251,7 +2363,7 @@ PP(pp_ftlink)
 
 PP(pp_ftsuid)
 {
-    dSP;
+    djSP;
 #ifdef S_ISUID
     I32 result = my_stat(ARGS);
     SPAGAIN;
@@ -2265,7 +2377,7 @@ PP(pp_ftsuid)
 
 PP(pp_ftsgid)
 {
-    dSP;
+    djSP;
 #ifdef S_ISGID
     I32 result = my_stat(ARGS);
     SPAGAIN;
@@ -2279,7 +2391,7 @@ PP(pp_ftsgid)
 
 PP(pp_ftsvtx)
 {
-    dSP;
+    djSP;
 #ifdef S_ISVTX
     I32 result = my_stat(ARGS);
     SPAGAIN;
@@ -2293,23 +2405,27 @@ PP(pp_ftsvtx)
 
 PP(pp_fttty)
 {
-    dSP;
+    djSP;
     int fd;
     GV *gv;
-    char *tmps;
-    if (op->op_flags & OPf_REF) {
+    char *tmps = Nullch;
+
+    if (op->op_flags & OPf_REF)
        gv = cGVOP->op_gv;
-       tmps = "";
-    }
+    else if (isGV(TOPs))
+       gv = (GV*)POPs;
+    else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
+       gv = (GV*)SvRV(POPs);
     else
        gv = gv_fetchpv(tmps = POPp, FALSE, SVt_PVIO);
+
     if (GvIO(gv) && IoIFP(GvIOp(gv)))
        fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
-    else if (isDIGIT(*tmps))
+    else if (tmps && isDIGIT(*tmps))
        fd = atoi(tmps);
     else
        RETPUSHUNDEF;
-    if (isatty(fd))
+    if (PerlLIO_isatty(fd))
        RETPUSHYES;
     RETPUSHNO;
 }
@@ -2324,7 +2440,7 @@ PP(pp_fttty)
 
 PP(pp_fttext)
 {
-    dSP;
+    djSP;
     I32 i;
     I32 len;
     I32 odd = 0;
@@ -2362,7 +2478,7 @@ PP(pp_fttext)
        if (io && IoIFP(io)) {
            if (! PerlIO_has_base(IoIFP(io)))
                DIE("-T and -B not implemented on filehandles");
-           laststatval = Fstat(PerlIO_fileno(IoIFP(io)), &statcache);
+           laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &statcache);
            if (laststatval < 0)
                RETPUSHUNDEF;
            if (S_ISDIR(statcache.st_mode))     /* handle NFS glitch */
@@ -2398,20 +2514,20 @@ PP(pp_fttext)
        laststatval = -1;
        sv_setpv(statname, SvPV(sv, na));
 #ifdef HAS_OPEN3
-       i = open(SvPV(sv, na), O_RDONLY, 0);
+       i = PerlLIO_open3(SvPV(sv, na), O_RDONLY, 0);
 #else
-       i = open(SvPV(sv, na), 0);
+       i = PerlLIO_open(SvPV(sv, na), 0);
 #endif
        if (i < 0) {
            if (dowarn && strchr(SvPV(sv, na), '\n'))
                warn(warn_nl, "open");
            RETPUSHUNDEF;
        }
-       laststatval = Fstat(i, &statcache);
+       laststatval = PerlLIO_fstat(i, &statcache);
        if (laststatval < 0)
            RETPUSHUNDEF;
-       len = read(i, tbuf, 512);
-       (void)close(i);
+       len = PerlLIO_read(i, tbuf, 512);
+       (void)PerlLIO_close(i);
        if (len <= 0) {
            if (S_ISDIR(statcache.st_mode) && op->op_type == OP_FTTEXT)
                RETPUSHNO;              /* special case NFS directories */
@@ -2451,7 +2567,7 @@ PP(pp_ftbinary)
 
 PP(pp_chdir)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     char *tmps;
     SV **svp;
 
@@ -2470,7 +2586,7 @@ PP(pp_chdir)
            tmps = SvPV(*svp, na);
     }
     TAINT_PROPER("chdir");
-    PUSHi( chdir(tmps) >= 0 );
+    PUSHi( PerlDir_chdir(tmps) >= 0 );
 #ifdef VMS
     /* Clear the DEFAULT element of ENV so we'll get the new value
      * in the future. */
@@ -2481,7 +2597,7 @@ PP(pp_chdir)
 
 PP(pp_chown)
 {
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     I32 value;
 #ifdef HAS_CHOWN
     value = (I32)apply(op->op_type, MARK, SP);
@@ -2495,7 +2611,7 @@ PP(pp_chown)
 
 PP(pp_chroot)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     char *tmps;
 #ifdef HAS_CHROOT
     tmps = POPp;
@@ -2509,7 +2625,7 @@ PP(pp_chroot)
 
 PP(pp_unlink)
 {
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     I32 value;
     value = (I32)apply(op->op_type, MARK, SP);
     SP = MARK;
@@ -2519,7 +2635,7 @@ PP(pp_unlink)
 
 PP(pp_chmod)
 {
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     I32 value;
     value = (I32)apply(op->op_type, MARK, SP);
     SP = MARK;
@@ -2529,7 +2645,7 @@ PP(pp_chmod)
 
 PP(pp_utime)
 {
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     I32 value;
     value = (I32)apply(op->op_type, MARK, SP);
     SP = MARK;
@@ -2539,7 +2655,7 @@ PP(pp_utime)
 
 PP(pp_rename)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     int anum;
 
     char *tmps2 = POPp;
@@ -2565,7 +2681,7 @@ PP(pp_rename)
 
 PP(pp_link)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
 #ifdef HAS_LINK
     char *tmps2 = POPp;
     char *tmps = SvPV(TOPs, na);
@@ -2579,7 +2695,7 @@ PP(pp_link)
 
 PP(pp_symlink)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
 #ifdef HAS_SYMLINK
     char *tmps2 = POPp;
     char *tmps = SvPV(TOPs, na);
@@ -2593,10 +2709,15 @@ PP(pp_symlink)
 
 PP(pp_readlink)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
 #ifdef HAS_SYMLINK
     char *tmps;
+    char buf[MAXPATHLEN];
     int len;
+
+#ifndef INCOMPLETE_TAINTS
+    TAINT;
+#endif
     tmps = POPp;
     len = readlink(tmps, buf, sizeof buf);
     EXTEND(SP, 1);
@@ -2630,14 +2751,14 @@ char *filename;
        *s++ = *filename++;
     }
     strcpy(s, " 2>&1");
-    myfp = my_popen(cmdline, "r");
+    myfp = PerlProc_popen(cmdline, "r");
     Safefree(cmdline);
 
     if (myfp) {
        SV *tmpsv = sv_newmortal();
        /* Need to save/restore 'rs' ?? */
        s = sv_gets(tmpsv, myfp, 0);
-       (void)my_pclose(myfp);
+       (void)PerlProc_pclose(myfp);
        if (s != Nullch) {
            int e;
            for (e = 1;
@@ -2701,7 +2822,7 @@ char *filename;
 
 PP(pp_mkdir)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     int mode = POPi;
 #ifndef HAS_MKDIR
     int oldumask;
@@ -2710,25 +2831,25 @@ PP(pp_mkdir)
 
     TAINT_PROPER("mkdir");
 #ifdef HAS_MKDIR
-    SETi( mkdir(tmps, mode) >= 0 );
+    SETi( PerlDir_mkdir(tmps, mode) >= 0 );
 #else
     SETi( dooneliner("mkdir", tmps) );
-    oldumask = umask(0);
-    umask(oldumask);
-    chmod(tmps, (mode & ~oldumask) & 0777);
+    oldumask = PerlLIO_umask(0);
+    PerlLIO_umask(oldumask);
+    PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
 #endif
     RETURN;
 }
 
 PP(pp_rmdir)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     char *tmps;
 
     tmps = POPp;
     TAINT_PROPER("rmdir");
 #ifdef HAS_RMDIR
-    XPUSHi( rmdir(tmps) >= 0 );
+    XPUSHi( PerlDir_rmdir(tmps) >= 0 );
 #else
     XPUSHi( dooneliner("rmdir", tmps) );
 #endif
@@ -2739,7 +2860,7 @@ PP(pp_rmdir)
 
 PP(pp_open_dir)
 {
-    dSP;
+    djSP;
 #if defined(Direntry_t) && defined(HAS_READDIR)
     char *dirname = POPp;
     GV *gv = (GV*)POPs;
@@ -2749,8 +2870,8 @@ PP(pp_open_dir)
        goto nope;
 
     if (IoDIRP(io))
-       closedir(IoDIRP(io));
-    if (!(IoDIRP(io) = opendir(dirname)))
+       PerlDir_close(IoDIRP(io));
+    if (!(IoDIRP(io) = PerlDir_open(dirname)))
        goto nope;
 
     RETPUSHYES;
@@ -2765,7 +2886,7 @@ nope:
 
 PP(pp_readdir)
 {
-    dSP;
+    djSP;
 #if defined(Direntry_t) && defined(HAS_READDIR)
 #ifndef I_DIRENT
     Direntry_t *readdir _((DIR *));
@@ -2773,28 +2894,37 @@ PP(pp_readdir)
     register Direntry_t *dp;
     GV *gv = (GV*)POPs;
     register IO *io = GvIOn(gv);
+    SV *sv;
 
     if (!io || !IoDIRP(io))
        goto nope;
 
     if (GIMME == G_ARRAY) {
        /*SUPPRESS 560*/
-       while (dp = (Direntry_t *)readdir(IoDIRP(io))) {
+       while (dp = (Direntry_t *)PerlDir_read(IoDIRP(io))) {
 #ifdef DIRNAMLEN
-           XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
+           sv = newSVpv(dp->d_name, dp->d_namlen);
 #else
-           XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
+           sv = newSVpv(dp->d_name, 0);
+#endif
+#ifndef INCOMPLETE_TAINTS
+           SvTAINTED_on(sv);
 #endif
+           XPUSHs(sv_2mortal(sv));
        }
     }
     else {
-       if (!(dp = (Direntry_t *)readdir(IoDIRP(io))))
+       if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
            goto nope;
 #ifdef DIRNAMLEN
-       XPUSHs(sv_2mortal(newSVpv(dp->d_name, dp->d_namlen)));
+       sv = newSVpv(dp->d_name, dp->d_namlen);
 #else
-       XPUSHs(sv_2mortal(newSVpv(dp->d_name, 0)));
+       sv = newSVpv(dp->d_name, 0);
 #endif
+#ifndef INCOMPLETE_TAINTS
+       SvTAINTED_on(sv);
+#endif
+       XPUSHs(sv_2mortal(sv));
     }
     RETURN;
 
@@ -2812,18 +2942,18 @@ nope:
 
 PP(pp_telldir)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
 #if defined(HAS_TELLDIR) || defined(telldir)
-#if !defined(telldir) && !defined(HAS_TELLDIR_PROTOTYPE)
+# ifdef NEED_TELLDIR_PROTO /* XXX does _anyone_ need this? --AD 2/20/1998 */
     long telldir _((DIR *));
-#endif
+# endif
     GV *gv = (GV*)POPs;
     register IO *io = GvIOn(gv);
 
     if (!io || !IoDIRP(io))
        goto nope;
 
-    PUSHi( telldir(IoDIRP(io)) );
+    PUSHi( PerlDir_tell(IoDIRP(io)) );
     RETURN;
 nope:
     if (!errno)
@@ -2836,7 +2966,7 @@ nope:
 
 PP(pp_seekdir)
 {
-    dSP;
+    djSP;
 #if defined(HAS_SEEKDIR) || defined(seekdir)
     long along = POPl;
     GV *gv = (GV*)POPs;
@@ -2845,7 +2975,7 @@ PP(pp_seekdir)
     if (!io || !IoDIRP(io))
        goto nope;
 
-    (void)seekdir(IoDIRP(io), along);
+    (void)PerlDir_seek(IoDIRP(io), along);
 
     RETPUSHYES;
 nope:
@@ -2859,7 +2989,7 @@ nope:
 
 PP(pp_rewinddir)
 {
-    dSP;
+    djSP;
 #if defined(HAS_REWINDDIR) || defined(rewinddir)
     GV *gv = (GV*)POPs;
     register IO *io = GvIOn(gv);
@@ -2867,7 +2997,7 @@ PP(pp_rewinddir)
     if (!io || !IoDIRP(io))
        goto nope;
 
-    (void)rewinddir(IoDIRP(io));
+    (void)PerlDir_rewind(IoDIRP(io));
     RETPUSHYES;
 nope:
     if (!errno)
@@ -2880,7 +3010,7 @@ nope:
 
 PP(pp_closedir)
 {
-    dSP;
+    djSP;
 #if defined(Direntry_t) && defined(HAS_READDIR)
     GV *gv = (GV*)POPs;
     register IO *io = GvIOn(gv);
@@ -2889,9 +3019,9 @@ PP(pp_closedir)
        goto nope;
 
 #ifdef VOID_CLOSEDIR
-    closedir(IoDIRP(io));
+    PerlDir_close(IoDIRP(io));
 #else
-    if (closedir(IoDIRP(io)) < 0) {
+    if (PerlDir_close(IoDIRP(io)) < 0) {
        IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
        goto nope;
     }
@@ -2913,7 +3043,7 @@ nope:
 PP(pp_fork)
 {
 #ifdef HAS_FORK
-    dSP; dTARGET;
+    djSP; dTARGET;
     int childpid;
     GV *tmpgv;
 
@@ -2936,8 +3066,8 @@ PP(pp_fork)
 
 PP(pp_wait)
 {
-#if !defined(DOSISH) || defined(OS2)
-    dSP; dTARGET;
+#if !defined(DOSISH) || defined(OS2) || defined(WIN32)
+    djSP; dTARGET;
     int childpid;
     int argflags;
 
@@ -2952,8 +3082,8 @@ PP(pp_wait)
 
 PP(pp_waitpid)
 {
-#if !defined(DOSISH) || defined(OS2)
-    dSP; dTARGET;
+#if !defined(DOSISH) || defined(OS2) || defined(WIN32)
+    djSP; dTARGET;
     int childpid;
     int optype;
     int argflags;
@@ -2965,13 +3095,13 @@ PP(pp_waitpid)
     SETi(childpid);
     RETURN;
 #else
-    DIE(no_func, "Unsupported function wait");
+    DIE(no_func, "Unsupported function waitpid");
 #endif
 }
 
 PP(pp_system)
 {
-    dSP; dMARK; dORIGMARK; dTARGET;
+    djSP; dMARK; dORIGMARK; dTARGET;
     I32 value;
     int childpid;
     int result;
@@ -3018,14 +3148,14 @@ PP(pp_system)
     else {
        value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), na));
     }
-    _exit(-1);
+    PerlProc__exit(-1);
 #else /* ! FORK or VMS or OS/2 */
     if (op->op_flags & OPf_STACKED) {
        SV *really = *++MARK;
-       value = (I32)do_aspawn(really, MARK, SP);
+       value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
     }
     else if (SP - MARK != 1)
-       value = (I32)do_aspawn(Nullsv, MARK, SP);
+       value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
     else {
        value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), na));
     }
@@ -3039,7 +3169,7 @@ PP(pp_system)
 
 PP(pp_exec)
 {
-    dSP; dMARK; dORIGMARK; dTARGET;
+    djSP; dMARK; dORIGMARK; dTARGET;
     I32 value;
 
     if (op->op_flags & OPf_STACKED) {
@@ -3071,7 +3201,7 @@ PP(pp_exec)
 
 PP(pp_kill)
 {
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     I32 value;
 #ifdef HAS_KILL
     value = (I32)apply(op->op_type, MARK, SP);
@@ -3086,7 +3216,7 @@ PP(pp_kill)
 PP(pp_getppid)
 {
 #ifdef HAS_GETPPID
-    dSP; dTARGET;
+    djSP; dTARGET;
     XPUSHi( getppid() );
     RETURN;
 #else
@@ -3097,7 +3227,7 @@ PP(pp_getppid)
 PP(pp_getpgrp)
 {
 #ifdef HAS_GETPGRP
-    dSP; dTARGET;
+    djSP; dTARGET;
     int pid;
     I32 value;
 
@@ -3122,7 +3252,7 @@ PP(pp_getpgrp)
 PP(pp_setpgrp)
 {
 #ifdef HAS_SETPGRP
-    dSP; dTARGET;
+    djSP; dTARGET;
     int pgrp;
     int pid;
     if (MAXARG < 2) {
@@ -3138,7 +3268,7 @@ PP(pp_setpgrp)
 #ifdef BSD_SETPGRP
     SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
 #else
-    if ((pgrp != 0 && pgrp != getpid())) || (pid != 0 && pid != getpid()))
+    if ((pgrp != 0 && pgrp != getpid()) || (pid != 0 && pid != getpid()))
        DIE("POSIX setpgrp can't take an argument");
     SETi( setpgrp() >= 0 );
 #endif /* USE_BSDPGRP */
@@ -3150,7 +3280,7 @@ PP(pp_setpgrp)
 
 PP(pp_getpriority)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     int which;
     int who;
 #ifdef HAS_GETPRIORITY
@@ -3165,7 +3295,7 @@ PP(pp_getpriority)
 
 PP(pp_setpriority)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     int which;
     int who;
     int niceval;
@@ -3185,7 +3315,7 @@ PP(pp_setpriority)
 
 PP(pp_time)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
 #ifdef BIG_TIME
     XPUSHn( time(Null(Time_t*)) );
 #else
@@ -3212,7 +3342,7 @@ PP(pp_time)
 
 PP(pp_tms)
 {
-    dSP;
+    djSP;
 
 #ifndef HAS_TIMES
     DIE("times not implemented");
@@ -3220,11 +3350,11 @@ PP(pp_tms)
     EXTEND(SP, 4);
 
 #ifndef VMS
-    (void)times(&timesbuf);
+    (void)PerlProc_times(&timesbuf);
 #else
-    (void)times((tbuffer_t *)&timesbuf);  /* time.h uses different name for */
-                                          /* struct tms, though same data   */
-                                          /* is returned.                   */
+    (void)PerlProc_times((tbuffer_t *)&timesbuf);  /* time.h uses different name for */
+                                                   /* struct tms, though same data   */
+                                                   /* is returned.                   */
 #endif
 
     PUSHs(sv_2mortal(newSVnv(((double)timesbuf.tms_utime)/HZ)));
@@ -3244,7 +3374,7 @@ PP(pp_localtime)
 
 PP(pp_gmtime)
 {
-    dSP;
+    djSP;
     Time_t when;
     struct tm *tmbuf;
     static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
@@ -3269,18 +3399,18 @@ PP(pp_gmtime)
     EXTEND_MORTAL(9);
     if (GIMME != G_ARRAY) {
        dTARGET;
-       char mybuf[30];
+       SV *tsv;
        if (!tmbuf)
            RETPUSHUNDEF;
-       sprintf(mybuf, "%s %s %2d %02d:%02d:%02d %d",
-           dayname[tmbuf->tm_wday],
-           monname[tmbuf->tm_mon],
-           tmbuf->tm_mday,
-           tmbuf->tm_hour,
-           tmbuf->tm_min,
-           tmbuf->tm_sec,
-           tmbuf->tm_year + 1900);
-       PUSHp(mybuf, strlen(mybuf));
+       tsv = newSVpvf("%s %s %2d %02d:%02d:%02d %d",
+                      dayname[tmbuf->tm_wday],
+                      monname[tmbuf->tm_mon],
+                      tmbuf->tm_mday,
+                      tmbuf->tm_hour,
+                      tmbuf->tm_min,
+                      tmbuf->tm_sec,
+                      tmbuf->tm_year + 1900);
+       PUSHs(sv_2mortal(tsv));
     }
     else if (tmbuf) {
        PUSHs(sv_2mortal(newSViv((I32)tmbuf->tm_sec)));
@@ -3298,7 +3428,7 @@ PP(pp_gmtime)
 
 PP(pp_alarm)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     int anum;
 #ifdef HAS_ALARM
     anum = POPi;
@@ -3315,17 +3445,17 @@ PP(pp_alarm)
 
 PP(pp_sleep)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
     I32 duration;
     Time_t lasttime;
     Time_t when;
 
     (void)time(&lasttime);
     if (MAXARG < 1)
-       Pause();
+       PerlProc_pause();
     else {
        duration = POPi;
-       sleep((unsigned int)duration);
+       PerlProc_sleep((unsigned int)duration);
     }
     (void)time(&when);
     XPUSHi(when - lasttime);
@@ -3352,7 +3482,7 @@ PP(pp_shmread)
 PP(pp_shmwrite)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     I32 value = (I32)(do_shmio(op->op_type, MARK, SP) >= 0);
     SP = MARK;
     PUSHi(value);
@@ -3377,7 +3507,7 @@ PP(pp_msgctl)
 PP(pp_msgsnd)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
     SP = MARK;
     PUSHi(value);
@@ -3390,7 +3520,7 @@ PP(pp_msgsnd)
 PP(pp_msgrcv)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
     SP = MARK;
     PUSHi(value);
@@ -3405,7 +3535,7 @@ PP(pp_msgrcv)
 PP(pp_semget)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     int anum = do_ipcget(op->op_type, MARK, SP);
     SP = MARK;
     if (anum == -1)
@@ -3420,7 +3550,7 @@ PP(pp_semget)
 PP(pp_semctl)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     int anum = do_ipcctl(op->op_type, MARK, SP);
     SP = MARK;
     if (anum == -1)
@@ -3429,7 +3559,7 @@ PP(pp_semctl)
        PUSHi(anum);
     }
     else {
-       PUSHp("0 but true",10);
+       PUSHp(zero_but_true, ZBTLEN);
     }
     RETURN;
 #else
@@ -3440,7 +3570,7 @@ PP(pp_semctl)
 PP(pp_semop)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
+    djSP; dMARK; dTARGET;
     I32 value = (I32)(do_semop(MARK, SP) >= 0);
     SP = MARK;
     PUSHi(value);
@@ -3454,7 +3584,7 @@ PP(pp_semop)
 
 PP(pp_ghbyname)
 {
-#ifdef HAS_SOCKET
+#ifdef HAS_GETHOSTBYNAME
     return pp_ghostent(ARGS);
 #else
     DIE(no_sock_func, "gethostbyname");
@@ -3463,7 +3593,7 @@ PP(pp_ghbyname)
 
 PP(pp_ghbyaddr)
 {
-#ifdef HAS_SOCKET
+#ifdef HAS_GETHOSTBYADDR
     return pp_ghostent(ARGS);
 #else
     DIE(no_sock_func, "gethostbyaddr");
@@ -3472,34 +3602,36 @@ PP(pp_ghbyaddr)
 
 PP(pp_ghostent)
 {
-    dSP;
-#ifdef HAS_SOCKET
+    djSP;
+#if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
     I32 which = op->op_type;
     register char **elem;
     register SV *sv;
-    struct hostent *gethostbyname();
-    struct hostent *gethostbyaddr();
-#ifdef HAS_GETHOSTENT
-    struct hostent *gethostent();
+#if defined(HAS_GETHOSTENT) && !defined(DONT_DECLARE_STD)
+    struct hostent *PerlSock_gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
+    struct hostent *PerlSock_gethostbyname(Netdb_name_t);
+#ifndef PerlSock_gethostent
+    struct hostent *PerlSock_gethostent(void);
+#endif
 #endif
     struct hostent *hent;
     unsigned long len;
 
     EXTEND(SP, 10);
     if (which == OP_GHBYNAME) {
-       hent = gethostbyname(POPp);
+       hent = PerlSock_gethostbyname(POPp);
     }
     else if (which == OP_GHBYADDR) {
        int addrtype = POPi;
        SV *addrsv = POPs;
        STRLEN addrlen;
-       char *addr = SvPV(addrsv, addrlen);
+       Netdb_host_t addr = (Netdb_host_t) SvPV(addrsv, addrlen);
 
-       hent = gethostbyaddr(addr, addrlen, addrtype);
+       hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
     }
     else
 #ifdef HAS_GETHOSTENT
-       hent = gethostent();
+       hent = PerlSock_gethostent();
 #else
        DIE("gethostent not implemented");
 #endif
@@ -3555,7 +3687,7 @@ PP(pp_ghostent)
 
 PP(pp_gnbyname)
 {
-#ifdef HAS_SOCKET
+#ifdef HAS_GETNETBYNAME
     return pp_gnetent(ARGS);
 #else
     DIE(no_sock_func, "getnetbyname");
@@ -3564,7 +3696,7 @@ PP(pp_gnbyname)
 
 PP(pp_gnbyaddr)
 {
-#ifdef HAS_SOCKET
+#ifdef HAS_GETNETBYADDR
     return pp_gnetent(ARGS);
 #else
     DIE(no_sock_func, "getnetbyaddr");
@@ -3573,25 +3705,27 @@ PP(pp_gnbyaddr)
 
 PP(pp_gnetent)
 {
-    dSP;
-#ifdef HAS_SOCKET
+    djSP;
+#if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
     I32 which = op->op_type;
     register char **elem;
     register SV *sv;
-    struct netent *getnetbyname();
-    struct netent *getnetbyaddr();
-    struct netent *getnetent();
+#ifdef NETDB_H_OMITS_GETNET
+    struct netent *getnetbyaddr(Netdb_net_t, int);
+    struct netent *getnetbyname(Netdb_name_t);
+    struct netent *getnetent(void);
+#endif
     struct netent *nent;
 
     if (which == OP_GNBYNAME)
-       nent = getnetbyname(POPp);
+       nent = PerlSock_getnetbyname(POPp);
     else if (which == OP_GNBYADDR) {
        int addrtype = POPi;
-       unsigned long addr = U_L(POPn);
-       nent = getnetbyaddr((long)addr, addrtype);
+       Netdb_net_t addr = (Netdb_net_t) U_L(POPn);
+       nent = PerlSock_getnetbyaddr(addr, addrtype);
     }
     else
-       nent = getnetent();
+       nent = PerlSock_getnetent();
 
     EXTEND(SP, 4);
     if (GIMME != G_ARRAY) {
@@ -3609,7 +3743,7 @@ PP(pp_gnetent)
        PUSHs(sv = sv_mortalcopy(&sv_no));
        sv_setpv(sv, nent->n_name);
        PUSHs(sv = sv_mortalcopy(&sv_no));
-       for (elem = nent->n_aliases; *elem; elem++) {
+       for (elem = nent->n_aliases; elem && *elem; elem++) {
            sv_catpv(sv, *elem);
            if (elem[1])
                sv_catpvn(sv, " ", 1);
@@ -3628,7 +3762,7 @@ PP(pp_gnetent)
 
 PP(pp_gpbyname)
 {
-#ifdef HAS_SOCKET
+#ifdef HAS_GETPROTOBYNAME
     return pp_gprotoent(ARGS);
 #else
     DIE(no_sock_func, "getprotobyname");
@@ -3637,7 +3771,7 @@ PP(pp_gpbyname)
 
 PP(pp_gpbynumber)
 {
-#ifdef HAS_SOCKET
+#ifdef HAS_GETPROTOBYNUMBER
     return pp_gprotoent(ARGS);
 #else
     DIE(no_sock_func, "getprotobynumber");
@@ -3646,22 +3780,26 @@ PP(pp_gpbynumber)
 
 PP(pp_gprotoent)
 {
-    dSP;
-#ifdef HAS_SOCKET
+    djSP;
+#if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
     I32 which = op->op_type;
     register char **elem;
-    register SV *sv;
-    struct protoent *getprotobyname();
-    struct protoent *getprotobynumber();
-    struct protoent *getprotoent();
+    register SV *sv;  
+#ifndef DONT_DECLARE_STD
+    struct protoent *PerlSock_getprotobyname(Netdb_name_t);
+    struct protoent *PerlSock_getprotobynumber(int);
+#ifndef PerlSock_getprotoent
+    struct protoent *PerlSock_getprotoent(void);
+#endif
+#endif
     struct protoent *pent;
 
     if (which == OP_GPBYNAME)
-       pent = getprotobyname(POPp);
+       pent = PerlSock_getprotobyname(POPp);
     else if (which == OP_GPBYNUMBER)
-       pent = getprotobynumber(POPi);
+       pent = PerlSock_getprotobynumber(POPi);
     else
-       pent = getprotoent();
+       pent = PerlSock_getprotoent();
 
     EXTEND(SP, 3);
     if (GIMME != G_ARRAY) {
@@ -3679,7 +3817,7 @@ PP(pp_gprotoent)
        PUSHs(sv = sv_mortalcopy(&sv_no));
        sv_setpv(sv, pent->p_name);
        PUSHs(sv = sv_mortalcopy(&sv_no));
-       for (elem = pent->p_aliases; *elem; elem++) {
+       for (elem = pent->p_aliases; elem && *elem; elem++) {
            sv_catpv(sv, *elem);
            if (elem[1])
                sv_catpvn(sv, " ", 1);
@@ -3696,7 +3834,7 @@ PP(pp_gprotoent)
 
 PP(pp_gsbyname)
 {
-#ifdef HAS_SOCKET
+#ifdef HAS_GETSERVICEBYNAME
     return pp_gservent(ARGS);
 #else
     DIE(no_sock_func, "getservbyname");
@@ -3705,7 +3843,7 @@ PP(pp_gsbyname)
 
 PP(pp_gsbyport)
 {
-#ifdef HAS_SOCKET
+#ifdef HAS_GETSERVICEBYPORT
     return pp_gservent(ARGS);
 #else
     DIE(no_sock_func, "getservbyport");
@@ -3714,14 +3852,18 @@ PP(pp_gsbyport)
 
 PP(pp_gservent)
 {
-    dSP;
-#ifdef HAS_SOCKET
+    djSP;
+#if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
     I32 which = op->op_type;
     register char **elem;
     register SV *sv;
-    struct servent *getservbyname();
-    struct servent *getservbynumber();
-    struct servent *getservent();
+#ifndef DONT_DECLARE_STD
+    struct servent *PerlSock_getservbyname(Netdb_name_t, Netdb_name_t);
+    struct servent *PerlSock_getservbyport(int, Netdb_name_t);
+#ifndef PerlSock_getservent
+    struct servent *PerlSock_getservent(void);
+#endif
+#endif
     struct servent *sent;
 
     if (which == OP_GSBYNAME) {
@@ -3731,19 +3873,19 @@ PP(pp_gservent)
        if (proto && !*proto)
            proto = Nullch;
 
-       sent = getservbyname(name, proto);
+       sent = PerlSock_getservbyname(name, proto);
     }
     else if (which == OP_GSBYPORT) {
        char *proto = POPp;
        unsigned short port = POPu;
 
 #ifdef HAS_HTONS
-       port = htons(port);
+       port = PerlSock_htons(port);
 #endif
-       sent = getservbyport(port, proto);
+       sent = PerlSock_getservbyport(port, proto);
     }
     else
-       sent = getservent();
+       sent = PerlSock_getservent();
 
     EXTEND(SP, 4);
     if (GIMME != G_ARRAY) {
@@ -3751,7 +3893,7 @@ PP(pp_gservent)
        if (sent) {
            if (which == OP_GSBYNAME) {
 #ifdef HAS_NTOHS
-               sv_setiv(sv, (IV)ntohs(sent->s_port));
+               sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
 #else
                sv_setiv(sv, (IV)(sent->s_port));
 #endif
@@ -3766,14 +3908,14 @@ PP(pp_gservent)
        PUSHs(sv = sv_mortalcopy(&sv_no));
        sv_setpv(sv, sent->s_name);
        PUSHs(sv = sv_mortalcopy(&sv_no));
-       for (elem = sent->s_aliases; *elem; elem++) {
+       for (elem = sent->s_aliases; elem && *elem; elem++) {
            sv_catpv(sv, *elem);
            if (elem[1])
                sv_catpvn(sv, " ", 1);
        }
        PUSHs(sv = sv_mortalcopy(&sv_no));
 #ifdef HAS_NTOHS
-       sv_setiv(sv, (IV)ntohs(sent->s_port));
+       sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
 #else
        sv_setiv(sv, (IV)(sent->s_port));
 #endif
@@ -3789,9 +3931,9 @@ PP(pp_gservent)
 
 PP(pp_shostent)
 {
-    dSP;
-#ifdef HAS_SOCKET
-    sethostent(TOPi);
+    djSP;
+#ifdef HAS_SETHOSTENT
+    PerlSock_sethostent(TOPi);
     RETSETYES;
 #else
     DIE(no_sock_func, "sethostent");
@@ -3800,9 +3942,9 @@ PP(pp_shostent)
 
 PP(pp_snetent)
 {
-    dSP;
-#ifdef HAS_SOCKET
-    setnetent(TOPi);
+    djSP;
+#ifdef HAS_SETNETENT
+    PerlSock_setnetent(TOPi);
     RETSETYES;
 #else
     DIE(no_sock_func, "setnetent");
@@ -3811,9 +3953,9 @@ PP(pp_snetent)
 
 PP(pp_sprotoent)
 {
-    dSP;
-#ifdef HAS_SOCKET
-    setprotoent(TOPi);
+    djSP;
+#ifdef HAS_SETPROTOENT
+    PerlSock_setprotoent(TOPi);
     RETSETYES;
 #else
     DIE(no_sock_func, "setprotoent");
@@ -3822,9 +3964,9 @@ PP(pp_sprotoent)
 
 PP(pp_sservent)
 {
-    dSP;
-#ifdef HAS_SOCKET
-    setservent(TOPi);
+    djSP;
+#ifdef HAS_SETSERVENT
+    PerlSock_setservent(TOPi);
     RETSETYES;
 #else
     DIE(no_sock_func, "setservent");
@@ -3833,10 +3975,10 @@ PP(pp_sservent)
 
 PP(pp_ehostent)
 {
-    dSP;
-#ifdef HAS_SOCKET
-    endhostent();
-    EXTEND(sp,1);
+    djSP;
+#ifdef HAS_ENDHOSTENT
+    PerlSock_endhostent();
+    EXTEND(SP,1);
     RETPUSHYES;
 #else
     DIE(no_sock_func, "endhostent");
@@ -3845,10 +3987,10 @@ PP(pp_ehostent)
 
 PP(pp_enetent)
 {
-    dSP;
-#ifdef HAS_SOCKET
-    endnetent();
-    EXTEND(sp,1);
+    djSP;
+#ifdef HAS_ENDNETENT
+    PerlSock_endnetent();
+    EXTEND(SP,1);
     RETPUSHYES;
 #else
     DIE(no_sock_func, "endnetent");
@@ -3857,10 +3999,10 @@ PP(pp_enetent)
 
 PP(pp_eprotoent)
 {
-    dSP;
-#ifdef HAS_SOCKET
-    endprotoent();
-    EXTEND(sp,1);
+    djSP;
+#ifdef HAS_ENDPROTOENT
+    PerlSock_endprotoent();
+    EXTEND(SP,1);
     RETPUSHYES;
 #else
     DIE(no_sock_func, "endprotoent");
@@ -3869,10 +4011,10 @@ PP(pp_eprotoent)
 
 PP(pp_eservent)
 {
-    dSP;
-#ifdef HAS_SOCKET
-    endservent();
-    EXTEND(sp,1);
+    djSP;
+#ifdef HAS_ENDSERVENT
+    PerlSock_endservent();
+    EXTEND(SP,1);
     RETPUSHYES;
 #else
     DIE(no_sock_func, "endservent");
@@ -3899,7 +4041,7 @@ PP(pp_gpwuid)
 
 PP(pp_gpwent)
 {
-    dSP;
+    djSP;
 #ifdef HAS_PASSWD
     I32 which = op->op_type;
     register SV *sv;
@@ -3955,6 +4097,9 @@ PP(pp_gpwent)
 #endif
        PUSHs(sv = sv_mortalcopy(&sv_no));
        sv_setpv(sv, pwent->pw_gecos);
+#ifndef INCOMPLETE_TAINTS
+       SvTAINTED_on(sv);
+#endif
        PUSHs(sv = sv_mortalcopy(&sv_no));
        sv_setpv(sv, pwent->pw_dir);
        PUSHs(sv = sv_mortalcopy(&sv_no));
@@ -3972,9 +4117,8 @@ PP(pp_gpwent)
 
 PP(pp_spwent)
 {
-    dSP;
- /* CYGWIN32 is missing setpwent(), (but has the others!) */
-#if 0
+    djSP;
+#if defined(HAS_PASSWD) && !defined(CYGWIN32)
     setpwent();
     RETPUSHYES;
 #else
@@ -3984,7 +4128,7 @@ PP(pp_spwent)
 
 PP(pp_epwent)
 {
-    dSP;
+    djSP;
 #ifdef HAS_PASSWD
     endpwent();
     RETPUSHYES;
@@ -4013,7 +4157,7 @@ PP(pp_ggrgid)
 
 PP(pp_ggrent)
 {
-    dSP;
+    djSP;
 #ifdef HAS_GROUP
     I32 which = op->op_type;
     register char **elem;
@@ -4047,7 +4191,7 @@ PP(pp_ggrent)
        PUSHs(sv = sv_mortalcopy(&sv_no));
        sv_setiv(sv, (IV)grent->gr_gid);
        PUSHs(sv = sv_mortalcopy(&sv_no));
-       for (elem = grent->gr_mem; *elem; elem++) {
+       for (elem = grent->gr_mem; elem && *elem; elem++) {
            sv_catpv(sv, *elem);
            if (elem[1])
                sv_catpvn(sv, " ", 1);
@@ -4062,7 +4206,7 @@ PP(pp_ggrent)
 
 PP(pp_sgrent)
 {
-    dSP;
+    djSP;
 #ifdef HAS_GROUP
     setgrent();
     RETPUSHYES;
@@ -4073,7 +4217,7 @@ PP(pp_sgrent)
 
 PP(pp_egrent)
 {
-    dSP;
+    djSP;
 #ifdef HAS_GROUP
     endgrent();
     RETPUSHYES;
@@ -4084,11 +4228,11 @@ PP(pp_egrent)
 
 PP(pp_getlogin)
 {
-    dSP; dTARGET;
+    djSP; dTARGET;
 #ifdef HAS_GETLOGIN
     char *tmps;
     EXTEND(SP, 1);
-    if (!(tmps = getlogin()))
+    if (!(tmps = PerlProc_getlogin()))
        RETPUSHUNDEF;
     PUSHp(tmps, strlen(tmps));
     RETURN;
@@ -4101,8 +4245,8 @@ PP(pp_getlogin)
 
 PP(pp_syscall)
 {
-#ifdef HAS_SYSCALL
-    dSP; dMARK; dORIGMARK; dTARGET;
+#ifdef HAS_SYSCALL   
+    djSP; dMARK; dORIGMARK; dTARGET;
     register I32 items = SP - MARK;
     unsigned long a[20];
     register I32 i = 0;
@@ -4203,9 +4347,7 @@ PP(pp_syscall)
 */
 
 static int
-fcntl_emulate_flock(fd, operation)
-int fd;
-int operation;
+fcntl_emulate_flock(int fd, int operation)
 {
     struct flock flock;
  
@@ -4267,6 +4409,17 @@ int fd;
 int operation;
 {
     int i;
+    int save_errno;
+    Off_t pos;
+
+    /* flock locks entire file so for lockf we need to do the same     */
+    save_errno = errno;
+    pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR);    /* get pos to restore later */
+    if (pos > 0)       /* is seekable and needs to be repositioned     */
+       if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
+           pos = -1;   /* seek failed, so don't seek back afterwards   */
+    errno = save_errno;
+
     switch (operation) {
 
        /* LOCK_SH - get a shared lock */
@@ -4298,7 +4451,12 @@ int operation;
            errno = EINVAL;
            break;
     }
+
+    if (pos > 0)      /* need to restore position of the handle        */
+       PerlLIO_lseek(fd, pos, SEEK_SET);       /* ignore error here    */
+
     return (i);
 }
 
 #endif /* LOCKF_EMULATE_FLOCK */
+