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