Loading...
stdio/FreeBSD/fread.c Libc-1044.1.2 Libc-825.26
--- Libc/Libc-1044.1.2/stdio/FreeBSD/fread.c
+++ Libc/Libc-825.26/stdio/FreeBSD/fread.c
@@ -58,22 +58,23 @@
 	return (ret);
 }
 
-/*
- * The maximum amount to read to avoid integer overflow.  INT_MAX is odd,
- * so it make sense to make it even.  We subtract (BUFSIZ - 1) to get a
- * whole number of BUFSIZ chunks.
- */
-#define MAXREAD	(INT_MAX - (BUFSIZ - 1))
-
-/* __fread0: int sized, with size = 1 */
-static inline int
-__fread0(void * __restrict buf, int count, FILE * __restrict fp)
+size_t
+__fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp)
 {
-	int resid;
+	size_t resid;
 	char *p;
 	int r, ret;
+	size_t total;
 
-	resid = count;
+	/*
+	 * ANSI and SUSv2 require a return value of 0 if size or count are 0.
+	 */
+	if ((resid = count * size) == 0)
+		return (0);
+	ORIENT(fp, -1);
+	if (fp->_r < 0)
+		fp->_r = 0;
+	total = resid;
 	p = buf;
 	/* first deal with anything left in buffer, plus any ungetc buffers */
 	while (resid > (r = fp->_r)) {
@@ -86,7 +87,7 @@
 			break;
 		else if (ret) {
 			/* no more input: return partial result */
-			return (count - resid);
+			return ((total - resid) / size);
 		}
 	}
 	/*
@@ -100,7 +101,7 @@
 		size_t n;
 
 		save = fp->_bf;
-		fp->_bf._base = (unsigned char *)p;
+		fp->_bf._base = p;
 		fp->_bf._size = resid;
 		while (fp->_bf._size > 0) {
 			if ((ret = __srefill1(fp)) != 0) {
@@ -109,7 +110,7 @@
 				fp->_bf = save;
 				fp->_p = fp->_bf._base;
 				/* fp->_r = 0;  already set in __srefill1 */
-				return (count - resid);
+				return ((total - resid) / size);
 			}
 			fp->_bf._base += fp->_r;
 			fp->_bf._size -= fp->_r;
@@ -129,7 +130,7 @@
 			resid -= r;
 			if (__srefill1(fp)) {
 				/* no more input: return partial result */
-				return (count - resid);
+				return ((total - resid) / size);
 			}
 		}
 		(void)memcpy((void *)p, (void *)fp->_p, resid);
@@ -138,29 +139,3 @@
 	}
 	return (count);
 }
-
-size_t
-__fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp)
-{
-	size_t resid;
-	int r, ret;
-	size_t total;
-
-	/*
-	 * ANSI and SUSv2 require a return value of 0 if size or count are 0.
-	 */
-	if ((resid = count * size) == 0)
-		return (0);
-	ORIENT(fp, -1);
-	if (fp->_r < 0)
-		fp->_r = 0;
-
-	for (total = resid; resid > 0; buf += r, resid -= r) {
-		r = resid > INT_MAX ? MAXREAD : (int)resid;
-		if ((ret = __fread0(buf, r, fp)) != r) {
-			count = (total - resid + ret) / size;
-			break;
-		}
-	}
-	return (count);
-}