This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix core dump on IO::Seekable::setpos($fh, undef)
[perl5.git] / ext / IO / IO.xs
index 82dce85..2eb16f4 100644 (file)
 #  include <fcntl.h>
 #endif
 
+#ifdef PerlIO
 typedef int SysRet;
 typedef PerlIO * InputStream;
 typedef PerlIO * OutputStream;
+#else
+#define PERLIO_IS_STDIO 1
+typedef int SysRet;
+typedef FILE * InputStream;
+typedef FILE * OutputStream;
+#endif
 
 static int
 not_here(s)
@@ -82,7 +89,11 @@ fgetpos(handle)
     CODE:
        if (handle) {
            Fpos_t pos;
+#ifdef PerlIO
            PerlIO_getpos(handle, &pos);
+#else
+           fgetpos(handle, &pos);
+#endif
            ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
        }
        else {
@@ -95,8 +106,13 @@ fsetpos(handle, pos)
        InputStream     handle
        SV *            pos
     CODE:
-       if (handle)
-           RETVAL = PerlIO_setpos(handle, (Fpos_t*)SvPVX(pos));
+       char *p;
+       if (handle && (p = SvPVx(pos, na)) && na == sizeof(Fpos_t))
+#ifdef PerlIO
+           RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
+#else
+           RETVAL = fsetpos(handle, (Fpos_t*)p);
+#endif
        else {
            RETVAL = -1;
            errno = EINVAL;
@@ -106,13 +122,29 @@ fsetpos(handle, pos)
 
 MODULE = IO    PACKAGE = IO::File      PREFIX = f
 
-OutputStream
+SV *
 new_tmpfile(packname = "IO::File")
     char *             packname
+    PREINIT:
+       OutputStream fp;
+       GV *gv;
     CODE:
-       RETVAL = PerlIO_tmpfile();
-    OUTPUT:
-       RETVAL
+#ifdef PerlIO
+       fp = PerlIO_tmpfile();
+#else
+       fp = tmpfile();
+#endif
+       gv = (GV*)SvREFCNT_inc(newGVgen(packname));
+       hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
+       if (do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
+           ST(0) = sv_2mortal(newRV((SV*)gv));
+           sv_bless(ST(0), gv_stashpv(packname, TRUE));
+           SvREFCNT_dec(gv);   /* undo increment in newRV() */
+       }
+       else {
+           ST(0) = &sv_undef;
+           SvREFCNT_dec(gv);
+       }
 
 MODULE = IO    PACKAGE = IO::Handle    PREFIX = f
 
@@ -132,7 +164,11 @@ ungetc(handle, c)
        int             c
     CODE:
        if (handle)
+#ifdef PerlIO
            RETVAL = PerlIO_ungetc(handle, c);
+#else
+           RETVAL = ungetc(c, handle);
+#endif
        else {
            RETVAL = -1;
            errno = EINVAL;
@@ -145,7 +181,30 @@ ferror(handle)
        InputStream     handle
     CODE:
        if (handle)
+#ifdef PerlIO
            RETVAL = PerlIO_error(handle);
+#else
+           RETVAL = ferror(handle);
+#endif
+       else {
+           RETVAL = -1;
+           errno = EINVAL;
+       }
+    OUTPUT:
+       RETVAL
+
+int
+clearerr(handle)
+       InputStream     handle
+    CODE:
+       if (handle) {
+#ifdef PerlIO
+           PerlIO_clearerr(handle);
+#else
+           clearerr(handle);
+#endif
+           RETVAL = 0;
+       }
        else {
            RETVAL = -1;
            errno = EINVAL;
@@ -153,12 +212,37 @@ ferror(handle)
     OUTPUT:
        RETVAL
 
+int
+untaint(handle)
+       SV *    handle
+    CODE:
+#ifdef IOf_UNTAINT
+       IO * io;
+       io = sv_2io(handle);
+       if (io) {
+           IoFLAGS(io) |= IOf_UNTAINT;
+           RETVAL = 0;
+       }
+        else {
+#endif
+           RETVAL = -1;
+           errno = EINVAL;
+#ifdef IOf_UNTAINT
+       }
+#endif
+    OUTPUT:
+       RETVAL
+
 SysRet
 fflush(handle)
        OutputStream    handle
     CODE:
        if (handle)
+#ifdef PerlIO
            RETVAL = PerlIO_flush(handle);
+#else
+           RETVAL = Fflush(handle);
+#endif
        else {
            RETVAL = -1;
            errno = EINVAL;
@@ -185,8 +269,8 @@ setvbuf(handle, buf, type, size)
        int             type
        int             size
     CODE:
-#ifdef PERLIO_IS_STDIO
-#ifdef _IOFBF   /* Should be HAS_SETVBUF once Configure tests for that */
+/* Should check HAS_SETVBUF once Configure tests for that */
+#if defined(PERLIO_IS_STDIO) && defined(_IOFBF)
        if (handle)
            RETVAL = setvbuf(handle, buf, type, size);
        else {
@@ -194,10 +278,7 @@ setvbuf(handle, buf, type, size)
            errno = EINVAL;
        }
 #else
-           RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
-#endif /* _IOFBF */
-#else
-           not_here("IO::Handle::setvbuf");
+       RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
 #endif
     OUTPUT:
        RETVAL