1N/A/*
1N/A * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A * Copyright (c) 1990, 1993
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * This code is derived from software contributed to Berkeley by
1N/A * Chris Torek.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include <sm/gen.h>
1N/ASM_RCSID("@(#)$Id: fread.c,v 1.26 2001/02/28 20:54:03 ca Exp $")
1N/A#include <string.h>
1N/A#include <errno.h>
1N/A#include <sm/io.h>
1N/A#include <sm/assert.h>
1N/A#include "local.h"
1N/A
1N/A/*
1N/A** SM_IO_READ -- read data from the file pointer
1N/A**
1N/A** Parameters:
1N/A** fp -- file pointer to read from
1N/A** timeout -- time to complete the read
1N/A** buf -- location to place read data
1N/A** size -- size of each chunk of data
1N/A**
1N/A** Returns:
1N/A** Failure: returns 0 (zero) _and_ sets errno
1N/A** Success: returns the number of whole chunks read.
1N/A**
1N/A** A read returning 0 (zero) is only an indication of error when errno
1N/A** has been set.
1N/A*/
1N/A
1N/Asize_t
1N/Asm_io_read(fp, timeout, buf, size)
1N/A SM_FILE_T *fp;
1N/A int timeout;
1N/A void *buf;
1N/A size_t size;
1N/A{
1N/A register size_t resid = size;
1N/A register char *p;
1N/A register int r;
1N/A
1N/A SM_REQUIRE_ISA(fp, SmFileMagic);
1N/A
1N/A if (fp->f_read == NULL)
1N/A {
1N/A errno = ENODEV;
1N/A return 0;
1N/A }
1N/A
1N/A /*
1N/A ** The ANSI standard requires a return value of 0 for a count
1N/A ** or a size of 0. Peculiarily, it imposes no such requirements
1N/A ** on fwrite; it only requires read to be broken.
1N/A */
1N/A
1N/A if (resid == 0)
1N/A return 0;
1N/A if (fp->f_r < 0)
1N/A fp->f_r = 0;
1N/A p = buf;
1N/A while ((int) resid > (r = fp->f_r))
1N/A {
1N/A (void) memcpy((void *) p, (void *) fp->f_p, (size_t) r);
1N/A fp->f_p += r;
1N/A /* fp->f_r = 0 ... done in sm_refill */
1N/A p += r;
1N/A resid -= r;
1N/A if ((fp->f_flags & SMNOW) != 0 && r > 0)
1N/A {
1N/A /*
1N/A ** Take whatever we have available. Spend no more time
1N/A ** trying to get all that has been requested.
1N/A ** This is needed on some file types (such as
1N/A ** SASL) that would jam when given extra, untimely
1N/A ** reads.
1N/A */
1N/A
1N/A fp->f_r -= r;
1N/A return size - resid;
1N/A }
1N/A if (sm_refill(fp, timeout) != 0)
1N/A {
1N/A /* no more input: return partial result */
1N/A return size - resid;
1N/A }
1N/A }
1N/A (void) memcpy((void *) p, (void *) fp->f_p, resid);
1N/A fp->f_r -= resid;
1N/A fp->f_p += resid;
1N/A return size;
1N/A}