2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * By using this file, you agree to the terms and conditions set
11 * forth in the LICENSE file which can be found at the top level of
12 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: fread.c,v 1.28 2001/09/11 04:04:48 gshapiro Exp $")
20 #include <sm/assert.h>
24 ** SM_IO_READ -- read data from the file pointer
27 ** fp -- file pointer to read from
28 ** timeout -- time to complete the read
29 ** buf -- location to place read data
30 ** size -- size of each chunk of data
33 ** Failure: returns 0 (zero) _and_ sets errno
34 ** Success: returns the number of whole chunks read.
36 ** A read returning 0 (zero) is only an indication of error when errno
41 sm_io_read(fp, timeout, buf, size)
47 register size_t resid = size;
51 SM_REQUIRE_ISA(fp, SmFileMagic);
53 if (fp->f_read == NULL)
60 ** The ANSI standard requires a return value of 0 for a count
61 ** or a size of 0. Peculiarily, it imposes no such requirements
62 ** on fwrite; it only requires read to be broken.
70 while ((int) resid > (r = fp->f_r))
72 (void) memcpy((void *) p, (void *) fp->f_p, (size_t) r);
74 /* fp->f_r = 0 ... done in sm_refill */
77 if ((fp->f_flags & SMNOW) != 0 && r > 0)
80 ** Take whatever we have available. Spend no more time
81 ** trying to get all that has been requested.
82 ** This is needed on some file types (such as
83 ** SASL) that would jam when given extra, untimely
90 if (sm_refill(fp, timeout) != 0)
92 /* no more input: return partial result */
96 (void) memcpy((void *) p, (void *) fp->f_p, resid);