xref: /titanic_50/usr/src/lib/libc/port/stdio/fread.c (revision 79d2f1e1d69dd8621f962e48584f1e2434b3c837)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include "lint.h"
33 #include "file64.h"
34 #include "mtlib.h"
35 #include <stdio.h>
36 #include <stddef.h>
37 #include <values.h>
38 #include <memory.h>
39 #include <thread.h>
40 #include <synch.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include "stdiom.h"
44 #include "mse.h"
45 
46 size_t
47 fread(void *ptr, size_t size, size_t count, FILE *iop)
48 {
49 	ssize_t s;
50 	int c;
51 	char *dptr = (char *)ptr;
52 	rmutex_t *lk;
53 
54 	FLOCKFILE(lk, iop);
55 
56 	_SET_ORIENTATION_BYTE(iop);
57 
58 	/* is it a readable stream */
59 	if (!(iop->_flag & (_IOREAD | _IORW))) {
60 		iop->_flag |= _IOERR;
61 		errno = EBADF;
62 		FUNLOCKFILE(lk);
63 		return (0);
64 	}
65 
66 	if (iop->_flag & _IOEOF) {
67 		FUNLOCKFILE(lk);
68 		return (0);
69 	}
70 
71 	/* These checks are here to avoid the multiply */
72 	if (count == 1)
73 		s = size;
74 	else if (size == 1)
75 		s = count;
76 	else
77 		s = size * count;
78 
79 	while (s > 0) {
80 		if (iop->_cnt < s) {
81 			if (iop->_cnt > 0) {
82 				(void) memcpy((void*)dptr, iop->_ptr,
83 				    iop->_cnt);
84 				dptr += iop->_cnt;
85 				s -= iop->_cnt;
86 			}
87 			/*
88 			 * filbuf clobbers _cnt & _ptr,
89 			 * so don't waste time setting them.
90 			 */
91 			if ((c = __filbuf(iop)) == EOF)
92 				break;
93 			*dptr++ = (char)c;
94 			s--;
95 		}
96 		if (iop->_cnt >= s) {
97 			char *tmp = (char *)iop->_ptr;
98 			switch (s) {
99 			case 8:
100 				*dptr++ = *tmp++;
101 				/*FALLTHRU*/
102 			case 7:
103 				*dptr++ = *tmp++;
104 				/*FALLTHRU*/
105 			case 6:
106 				*dptr++ = *tmp++;
107 				/*FALLTHRU*/
108 			case 5:
109 				*dptr++ = *tmp++;
110 				/*FALLTHRU*/
111 			case 4:
112 				*dptr++ = *tmp++;
113 				/*FALLTHRU*/
114 			case 3:
115 				*dptr++ = *tmp++;
116 				/*FALLTHRU*/
117 			case 2:
118 				*dptr++ = *tmp++;
119 				/*FALLTHRU*/
120 			case 1:
121 				*dptr++ = *tmp++;
122 				break;
123 			default:
124 				(void) memcpy((void*)dptr, iop->_ptr,
125 				    (size_t)s);
126 			}
127 			iop->_ptr += s;
128 			iop->_cnt -= s;
129 			FUNLOCKFILE(lk);
130 			return (count);
131 		}
132 	}
133 	FUNLOCKFILE(lk);
134 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
135 }
136