 /*
  * Khoros: $Id: block_read.c,v 1.3 1992/03/20 23:40:08 dkhoros Exp $
  */

#if !defined(lint) && !defined(SABER)
static char rcsid[] = "Khoros: $Id: block_read.c,v 1.3 1992/03/20 23:40:08 dkhoros Exp $";
#endif

 /*
  * $Log: block_read.c,v $
 * Revision 1.3  1992/03/20  23:40:08  dkhoros
 * VirtualPatch5
 *
  */ 

/*
 *----------------------------------------------------------------------
 *
 * Copyright 1990, University of New Mexico.  All rights reserved.

 * Permission to copy and modify this software and its documen-
 * tation only for internal use in your organization is hereby
 * granted, provided that this notice is retained thereon and
 * on all copies.  UNM makes no representations as too the sui-
 * tability and operability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 * 
 * UNM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
 * NESS.  IN NO EVENT SHALL UNM BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY OTHER DAMAGES WHAT-
 * SOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PER-
 * FORMANCE OF THIS SOFTWARE.
 * 
 * No other rights, including for example, the right to redis-
 * tribute this software and its documentation or the right to
 * prepare derivative works, are granted unless specifically
 * provided in a separate license agreement.
 *---------------------------------------------------------------------
 */

#include "unmcopyright.h"	 /* Copyright 1990 by UNM */
#include "vinclude.h"
#include "readbuffer.h"

/************************************************************
*
*  MODULE NAME: block_read
*
*      PURPOSE: block_read is a stupid little routine that performs
*		a block read on the number of desired blocks.  This
*		useful when the input is coming from standard in, since
*		read can return without reading all the desired blocks.
*
*        INPUT: 1.  fid   --  the file descriptor in which to read from
*		2.  input --  a character pointer where the bytes will be 
*			      read and stored to
*		3.  size --   an integer specifying the number of bytes to 
*			      to be read.
*
*       OUTPUT: Returns the number of bytes read.
*
*    CALLED BY: any routine that wishes to block read
*
*   WRITTEN BY: Mark Young
*
*************************************************************/


int block_read(fid, input, size)

int	fid;
char	*input;
int	size;
  {
    int	numread = 0, nbytes = 1, num = 0;

    if (fid == 0 && num_buffer > 0)
      {
	while (num_buffer > 0 && num < size)
          {
	    *(input + num) = read_buffer[num];
	    num_buffer--; num++; numread++;
	  }
      }

        while ((numread < size) && (nbytes >0))
          {
            nbytes = kread(fid, (char *) (input + numread), size - numread);
	    numread += nbytes;
          }
    return(numread);
  }
