Re: [PATCH 5.6.1] OS2 syslog
[p5sagit/p5-mst-13.2.git] / perlio.c
index 590abf6..3fac35d 100644 (file)
--- a/perlio.c
+++ b/perlio.c
@@ -754,7 +754,7 @@ PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av,IV n,PerlIO_funcs *def)
 {
  if (n >= 0 && n < av->cur)
   {
-   PerlIO_debug("Layer %ld is %s\n",n,av->array[n].funcs->name);
+   PerlIO_debug("Layer %"IVdf" is %s\n",n,av->array[n].funcs->name);
    return av->array[n].funcs;
   }
  if (!def)
@@ -1776,11 +1776,12 @@ SSize_t
 PerlIOBase_unread(PerlIO *f, const void *vbuf, Size_t count)
 {
  dTHX;
+ /* Save the position as current head considers it */
  Off_t old = PerlIO_tell(f);
  SSize_t done;
  PerlIO_push(aTHX_ f,&PerlIO_pending,"r",Nullsv);
+ PerlIOSelf(f,PerlIOBuf)->posn = old;
  done = PerlIOBuf_unread(f,vbuf,count);
- PerlIOSelf(f,PerlIOBuf)->posn = old - done;
  return done;
 }
 
@@ -2799,22 +2800,31 @@ PerlIOBuf_unread(PerlIO *f, const void *vbuf, Size_t count)
   {
    if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
     {
+     /* Buffer is already a read buffer, we can overwrite any chars
+        which have been read back to buffer start
+      */
      avail = (b->ptr - b->buf);
     }
    else
     {
-     avail = b->bufsiz;
+     /* Buffer is idle, set it up so whole buffer is available for unread */
+     avail  = b->bufsiz;
      b->end = b->buf + avail;
      b->ptr = b->end;
      PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
+     /* Buffer extends _back_ from where we are now */
      b->posn -= b->bufsiz;
     }
    if (avail > (SSize_t) count)
-    avail = count;
+    {
+     /* If we have space for more than count, just move count */
+     avail = count;
+    }
    if (avail > 0)
     {
      b->ptr -= avail;
      buf    -= avail;
+     /* In simple stdio-like ungetc() case chars will be already there */
      if (buf != b->ptr)
       {
        Copy(buf,b->ptr,avail,STDCHAR);
@@ -2899,9 +2909,13 @@ Off_t
 PerlIOBuf_tell(PerlIO *f)
 {
  PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
+ /* b->posn is file position where b->buf was read, or will be written */
  Off_t posn = b->posn;
  if (b->buf)
-  posn += (b->ptr - b->buf);
+  {
+   /* If buffer is valid adjust position by amount in buffer */
+   posn += (b->ptr - b->buf);
+  }
  return posn;
 }