8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sendmail / libsm / fread.c
blob40cd23cfcda68b6a3fccfc18bdeb9832c89442b5
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.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
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.
15 #pragma ident "%Z%%M% %I% %E% SMI"
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"
26 ** SM_IO_READ -- read data from the file pointer
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
34 ** Returns:
35 ** Failure: returns 0 (zero) _and_ sets errno
36 ** Success: returns the number of whole chunks read.
38 ** A read returning 0 (zero) is only an indication of error when errno
39 ** has been set.
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;
49 register size_t resid = size;
50 register char *p;
51 register int r;
53 SM_REQUIRE_ISA(fp, SmFileMagic);
55 if (fp->f_read == NULL)
57 errno = ENODEV;
58 return 0;
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.
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))
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)
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.
89 fp->f_r -= r;
90 return size - resid;
92 if (sm_refill(fp, timeout) != 0)
94 /* no more input: return partial result */
95 return size - resid;
98 (void) memcpy((void *) p, (void *) fp->f_p, resid);
99 fp->f_r -= resid;
100 fp->f_p += resid;
101 return size;