xref: /onnv-gate/usr/src/lib/libc/port/stdio/fread.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include "file64.h"
340Sstevel@tonic-gate #include "mtlib.h"
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <stddef.h>
370Sstevel@tonic-gate #include <values.h>
380Sstevel@tonic-gate #include <memory.h>
390Sstevel@tonic-gate #include <thread.h>
400Sstevel@tonic-gate #include <synch.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include "stdiom.h"
440Sstevel@tonic-gate #include "mse.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate size_t
fread(void * ptr,size_t size,size_t count,FILE * iop)470Sstevel@tonic-gate fread(void *ptr, size_t size, size_t count, FILE *iop)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	ssize_t s;
500Sstevel@tonic-gate 	int c;
510Sstevel@tonic-gate 	char *dptr = (char *)ptr;
520Sstevel@tonic-gate 	rmutex_t *lk;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	FLOCKFILE(lk, iop);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	_SET_ORIENTATION_BYTE(iop);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	/* is it a readable stream */
590Sstevel@tonic-gate 	if (!(iop->_flag & (_IOREAD | _IORW))) {
600Sstevel@tonic-gate 		iop->_flag |= _IOERR;
610Sstevel@tonic-gate 		errno = EBADF;
620Sstevel@tonic-gate 		FUNLOCKFILE(lk);
630Sstevel@tonic-gate 		return (0);
640Sstevel@tonic-gate 	}
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	if (iop->_flag & _IOEOF) {
670Sstevel@tonic-gate 		FUNLOCKFILE(lk);
680Sstevel@tonic-gate 		return (0);
690Sstevel@tonic-gate 	}
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	/* These checks are here to avoid the multiply */
720Sstevel@tonic-gate 	if (count == 1)
730Sstevel@tonic-gate 		s = size;
740Sstevel@tonic-gate 	else if (size == 1)
750Sstevel@tonic-gate 		s = count;
760Sstevel@tonic-gate 	else
770Sstevel@tonic-gate 		s = size * count;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	while (s > 0) {
800Sstevel@tonic-gate 		if (iop->_cnt < s) {
810Sstevel@tonic-gate 			if (iop->_cnt > 0) {
820Sstevel@tonic-gate 				(void) memcpy((void*)dptr, iop->_ptr,
830Sstevel@tonic-gate 				    iop->_cnt);
840Sstevel@tonic-gate 				dptr += iop->_cnt;
850Sstevel@tonic-gate 				s -= iop->_cnt;
860Sstevel@tonic-gate 			}
870Sstevel@tonic-gate 			/*
880Sstevel@tonic-gate 			 * filbuf clobbers _cnt & _ptr,
890Sstevel@tonic-gate 			 * so don't waste time setting them.
900Sstevel@tonic-gate 			 */
910Sstevel@tonic-gate 			if ((c = __filbuf(iop)) == EOF)
920Sstevel@tonic-gate 				break;
930Sstevel@tonic-gate 			*dptr++ = (char)c;
940Sstevel@tonic-gate 			s--;
950Sstevel@tonic-gate 		}
960Sstevel@tonic-gate 		if (iop->_cnt >= s) {
970Sstevel@tonic-gate 			char *tmp = (char *)iop->_ptr;
980Sstevel@tonic-gate 			switch (s) {
990Sstevel@tonic-gate 			case 8:
1000Sstevel@tonic-gate 				*dptr++ = *tmp++;
1010Sstevel@tonic-gate 				/*FALLTHRU*/
1020Sstevel@tonic-gate 			case 7:
1030Sstevel@tonic-gate 				*dptr++ = *tmp++;
1040Sstevel@tonic-gate 				/*FALLTHRU*/
1050Sstevel@tonic-gate 			case 6:
1060Sstevel@tonic-gate 				*dptr++ = *tmp++;
1070Sstevel@tonic-gate 				/*FALLTHRU*/
1080Sstevel@tonic-gate 			case 5:
1090Sstevel@tonic-gate 				*dptr++ = *tmp++;
1100Sstevel@tonic-gate 				/*FALLTHRU*/
1110Sstevel@tonic-gate 			case 4:
1120Sstevel@tonic-gate 				*dptr++ = *tmp++;
1130Sstevel@tonic-gate 				/*FALLTHRU*/
1140Sstevel@tonic-gate 			case 3:
1150Sstevel@tonic-gate 				*dptr++ = *tmp++;
1160Sstevel@tonic-gate 				/*FALLTHRU*/
1170Sstevel@tonic-gate 			case 2:
1180Sstevel@tonic-gate 				*dptr++ = *tmp++;
1190Sstevel@tonic-gate 				/*FALLTHRU*/
1200Sstevel@tonic-gate 			case 1:
1210Sstevel@tonic-gate 				*dptr++ = *tmp++;
1220Sstevel@tonic-gate 				break;
1230Sstevel@tonic-gate 			default:
1240Sstevel@tonic-gate 				(void) memcpy((void*)dptr, iop->_ptr,
1250Sstevel@tonic-gate 				    (size_t)s);
1260Sstevel@tonic-gate 			}
1270Sstevel@tonic-gate 			iop->_ptr += s;
1280Sstevel@tonic-gate 			iop->_cnt -= s;
1290Sstevel@tonic-gate 			FUNLOCKFILE(lk);
1300Sstevel@tonic-gate 			return (count);
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	FUNLOCKFILE(lk);
1340Sstevel@tonic-gate 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
1350Sstevel@tonic-gate }
136