pike.git / src / modules / _Stdio / buffer.cmod

version» Context lines:

pike.git/src/modules/_Stdio/buffer.cmod:907:    }       PIKEFUN void __fd_set_output( int(0..0) f )    {    IOBuffer *io = THIS;    if( io->output ) free_object(io->output);    io->output = 0;    io->output_triggered = 0;    }    -  /*! @decl int output_to( Stdio.Stream f, int|void nbytes ) +  /*! @decl int(-1..) output_to( Stdio.Stream f, int(0..)|void nbytes )    *!    *! Write data from the buffer to the indicated file.    *! -  *! Will return the number of bytes that were successfully written. -  *! +  *! @param nbytes    *! If @[nbytes] is not specified the whole buffer will be written    *! if possible. Otherwise at most @[nbytes] will be written. -  +  *! +  *! @returns +  *! Will return the number of bytes that were successfully written. +  *! +  *! If no bytes were successfully written and @expr{f->write()@} failed +  *! with an error @expr{-1@} will be returned.    */ -  PIKEFUN int(0..) output_to( object f, int|void _nbytes ) +  PIKEFUN int(-1..) output_to( object f, int|void _nbytes )    {    IOBuffer *io = THIS; -  size_t written = 0, nbytes = (size_t)-1; +  ssize_t written = 0, nbytes = (ssize_t)(((size_t)~0)>>1);    struct my_file *fd;    INT_TYPE *wr = &Pike_sp->u.integer;    -  size_t sz = io_len( io ); +  ssize_t sz = io_len( io );       if( !sz )    {    io_range_error(io, sz);    sz = io_len(io);    }    if( _nbytes )    nbytes = _nbytes->u.integer;       if( (fd = get_storage( f, file_program )) )    {    /* lock this object. */    while( sz > written && nbytes )    { -  size_t rd = MINIMUM(MINIMUM(sz-written,4096),nbytes); +  ssize_t rd = MINIMUM(MINIMUM(sz-written,4096),nbytes);    unsigned char *ptr = io_read_pointer( io );    ssize_t res;    res = fd_write( fd->box.fd, ptr, rd );    if( res == -1 && errno == EINTR )    continue; -  if( res <= 0 ) +  if( res <= 0 ) { +  if (!written) written = -1;    break; -  +  }    io_consume( io, res );    written += res;    nbytes-=res;    }    fd->box.revents &= ~(PIKE_BIT_FD_WRITE|PIKE_BIT_FD_WRITE_OOB);    if(!SAFE_IS_ZERO(&fd->event_cbs[PIKE_FD_WRITE])    && fd->box.backend)    set_fd_callback_events(&fd->box, fd->box.events|PIKE_BIT_FD_WRITE, 0);    }    else
pike.git/src/modules/_Stdio/buffer.cmod:969:    {    size_t rd = MINIMUM(MINIMUM(sz-written,4096),nbytes);    struct pike_string *s = io_read_string( io, rd );    if( !s )    break;    push_string(s);    /* when this throws we need to rewind the buffer correctly. */    safe_apply(f, "write", 1);    if( *wr <= 0 ){    io_rewind(io, rd ); +  if (!written) written = -1;    break;    }    written += *wr;    if( *wr < 4096 ) {    io_rewind(io, rd-*wr );    break;    }    pop_stack();    }    }