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

version» Context lines:

pike.git/src/modules/_Stdio/buffer.cmod:968:    *! @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 have been written successfully.    *!    *! If no bytes have been written successfully and @expr{f->write()@} failed    *! with an error, @expr{-1@} will be returned.    */ -  PIKEFUN int(-1..) output_to( object f, int|void _nbytes ) +  PIKEFUN int(-1..) output_to( object f, int|void nbytes )    {    Buffer *io = THIS; -  ptrdiff_t written = 0, nbytes = (ptrdiff_t)(((size_t)~0)>>1); +  ptrdiff_t written = 0;    struct my_file *fd;    ptrdiff_t sz = io_len( io );    int write_fun_num;    struct inherit *inh;       if( !sz )    {    io_range_error(io, sz);    sz = io_len(io);    } -  if( _nbytes ) -  nbytes = _nbytes->u.integer; +  if( nbytes ) +  sz = MINIMUM(nbytes->u.integer, sz);       if ((write_fun_num = find_identifier("write", f->prog)) == -1) {    Pike_error("Cannot call unknown function \"write\".\n");    }       inh = INHERIT_FROM_INT(f->prog, write_fun_num);       if( inh->prog == file_program )    {    fd = get_inherit_storage( f, inh - f->prog->inherits );    -  while( sz > written && nbytes ) +  while( sz > written )    { -  ptrdiff_t rd = MINIMUM(MINIMUM(sz-written,4096),nbytes); +  ptrdiff_t rd = MINIMUM(sz-written,4096);    unsigned char *ptr = io_read_pointer( io );    ptrdiff_t res;    res = fd_write( fd->box.fd, ptr, rd );    if( res == -1 && errno == EINTR )    continue;    if( res <= 0 ) {    fd->my_errno = errno;    if (!written) written = -1;    break;    }    io_consume( io, res );    written += res; -  nbytes-=res; +     io_set_events( io, fd, PIKE_BIT_FD_WRITE_OOB, PIKE_FD_WRITE);    }    }    else    {    /* some other object. Just call write */ -  while( sz > written && nbytes ) +  while( sz > written )    { -  size_t rd = MINIMUM(MINIMUM(sz-written,4096),nbytes); +  size_t rd = MINIMUM(sz-written,4096);    ptrdiff_t wr = io_call_write( io, f, rd );    if( wr <= 0 )    {    if (!written) written = -1;    break;    }    written += wr;    if( wr < 4096 ) {    break;    }