Change PerlIO_(get|set)pos to take SV *
[p5sagit/p5-mst-13.2.git] / ext / IO / IO.xs
index e5ce83d..13b198c 100644 (file)
@@ -59,29 +59,34 @@ io_blocking(InputStream f, int block)
     if (RETVAL >= 0) {
        int mode = RETVAL;
 #ifdef O_NONBLOCK
-       /* POSIX style */ 
+       /* POSIX style */
 #if defined(O_NDELAY) && O_NDELAY != O_NONBLOCK
-       /* Ooops has O_NDELAY too - make sure we don't 
-        * get SysV behaviour by mistake
-        */
-       RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
-
-       if ((mode & O_NDELAY) || ((block == 0) && !(mode & O_NONBLOCK))) {
-           int ret;
-           mode = (mode & ~O_NDELAY) | O_NONBLOCK;
-           ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
-           if(ret < 0)
-               RETVAL = ret;
-       }
-       else if ((mode & O_NDELAY) || ((block > 0) && (mode & O_NONBLOCK))) {
-           int ret;
-           mode &= ~(O_NONBLOCK | O_NDELAY);
-           ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
-           if(ret < 0)
-               RETVAL = ret;
+       /* Ooops has O_NDELAY too - make sure we don't
+        * get SysV behaviour by mistake. */
+
+       /* E.g. In UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
+        * after a successful F_SETFL of an O_NONBLOCK. */
+       RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
+
+       if (block >= 0) {
+           if ((mode & O_NDELAY) || ((block == 0) && !(mode & O_NONBLOCK))) {
+               int ret;
+               mode = (mode & ~O_NDELAY) | O_NONBLOCK;
+               ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
+               if(ret < 0)
+                   RETVAL = ret;
+           }
+           else
+              if ((mode & O_NDELAY) || ((block > 0) && (mode & O_NONBLOCK))) {
+               int ret;
+               mode &= ~(O_NONBLOCK | O_NDELAY);
+               ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
+               if(ret < 0)
+                   RETVAL = ret;
+              }
        }
 #else
-       /* Standard POSIX */ 
+       /* Standard POSIX */
        RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
 
        if ((block == 0) && !(mode & O_NONBLOCK)) {
@@ -98,11 +103,11 @@ io_blocking(InputStream f, int block)
            if(ret < 0)
                RETVAL = ret;
         }
-#endif 
+#endif
 #else
        /* Not POSIX - better have O_NDELAY or we can't cope.
         * for BSD-ish machines this is an acceptable alternative
-        * for SysV we can't tell "would block" from EOF but that is 
+        * for SysV we can't tell "would block" from EOF but that is
         * the way SysV is...
         */
        RETVAL = RETVAL & O_NDELAY ? 0 : 1;
@@ -136,13 +141,18 @@ fgetpos(handle)
        InputStream     handle
     CODE:
        if (handle) {
-           Fpos_t pos;
 #ifdef PerlIO
-           PerlIO_getpos(handle, &pos);
+           ST(0) = sv_2mortal(newSV(0));
+           if (PerlIO_getpos(handle, ST(0)) != 0) {
+               ST(0) = &PL_sv_undef;
+           }
 #else
-           fgetpos(handle, &pos);
+           if (fgetpos(handle, &pos)) {
+               ST(0) = &PL_sv_undef;
+           } else {
+               ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
+           }
 #endif
-           ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
        }
        else {
            ST(0) = &PL_sv_undef;
@@ -154,14 +164,21 @@ fsetpos(handle, pos)
        InputStream     handle
        SV *            pos
     CODE:
-        char *p;
-       STRLEN len;
-       if (handle && (p = SvPV(pos,len)) && len == sizeof(Fpos_t))
+       if (handle) {
 #ifdef PerlIO
-           RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
+           RETVAL = PerlIO_setpos(handle, pos);
 #else
-           RETVAL = fsetpos(handle, (Fpos_t*)p);
+           char *p;
+           STRLEN len;
+           if ((p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
+               RETVAL = fsetpos(handle, (Fpos_t*)p);
+           }
+           else {
+               RETVAL = -1;
+               errno = EINVAL;
+           }
 #endif
+       }
        else {
            RETVAL = -1;
            errno = EINVAL;
@@ -197,7 +214,7 @@ new_tmpfile(packname = "IO::File")
 
 MODULE = IO    PACKAGE = IO::Poll
 
-void   
+void
 _poll(timeout,...)
        int timeout;
 PPCODE:
@@ -453,11 +470,4 @@ BOOT:
 #ifdef SEEK_END
         newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));
 #endif
-    /*
-     * constant subs for IO
-     */
-    stash = gv_stashpvn("IO", 2, TRUE);
-#ifdef EINPROGRESS
-        newCONSTSUB(stash,"EINPROGRESS", newSViv(EINPROGRESS));
-#endif
 }