xref: /titanic_44/usr/src/cmd/sendmail/libsm/fread.c (revision 9dd0f810214fdc8e1af881a9a5c4b6927629ff9e)
1 /*
2  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3  *      All rights reserved.
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Chris Torek.
9  *
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.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include <sm/gen.h>
18 SM_RCSID("@(#)$Id: fread.c,v 1.26 2001/02/28 20:54:03 ca Exp $")
19 #include <string.h>
20 #include <errno.h>
21 #include <sm/io.h>
22 #include <sm/assert.h>
23 #include "local.h"
24 
25 /*
26 **  SM_IO_READ -- read data from the file pointer
27 **
28 **	Parameters:
29 **		fp -- file pointer to read from
30 **		timeout -- time to complete the read
31 **		buf -- location to place read data
32 **		size -- size of each chunk of data
33 **
34 **	Returns:
35 **		Failure: returns 0 (zero) _and_ sets errno
36 **		Success: returns the number of whole chunks read.
37 **
38 **	A read returning 0 (zero) is only an indication of error when errno
39 **	has been set.
40 */
41 
42 size_t
43 sm_io_read(fp, timeout, buf, size)
44 	SM_FILE_T *fp;
45 	int timeout;
46 	void *buf;
47 	size_t size;
48 {
49 	register size_t resid = size;
50 	register char *p;
51 	register int r;
52 
53 	SM_REQUIRE_ISA(fp, SmFileMagic);
54 
55 	if (fp->f_read == NULL)
56 	{
57 		errno = ENODEV;
58 		return 0;
59 	}
60 
61 	/*
62 	**  The ANSI standard requires a return value of 0 for a count
63 	**  or a size of 0.  Peculiarily, it imposes no such requirements
64 	**  on fwrite; it only requires read to be broken.
65 	*/
66 
67 	if (resid == 0)
68 		return 0;
69 	if (fp->f_r < 0)
70 		fp->f_r = 0;
71 	p = buf;
72 	while ((int) resid > (r = fp->f_r))
73 	{
74 		(void) memcpy((void *) p, (void *) fp->f_p, (size_t) r);
75 		fp->f_p += r;
76 		/* fp->f_r = 0 ... done in sm_refill */
77 		p += r;
78 		resid -= r;
79 		if ((fp->f_flags & SMNOW) != 0 && r > 0)
80 		{
81 			/*
82 			**  Take whatever we have available. Spend no more time
83 			**  trying to get all that has been requested.
84 			**  This is needed on some file types (such as
85 			**  SASL) that would jam when given extra, untimely
86 			**  reads.
87 			*/
88 
89 			fp->f_r -= r;
90 			return size - resid;
91 		}
92 		if (sm_refill(fp, timeout) != 0)
93 		{
94 			/* no more input: return partial result */
95 			return size - resid;
96 		}
97 	}
98 	(void) memcpy((void *) p, (void *) fp->f_p, resid);
99 	fp->f_r -= resid;
100 	fp->f_p += resid;
101 	return size;
102 }
103