xref: /illumos-gate/usr/src/boot/i386/libi386/pread.c (revision 55fea89d)
1199767f8SToomas Soome /*
2199767f8SToomas Soome  * $NetBSD: pread.c,v 1.2 1997/03/22 01:48:38 thorpej Exp $
3199767f8SToomas Soome  */
4199767f8SToomas Soome 
5199767f8SToomas Soome /*-
6199767f8SToomas Soome  * Copyright (c) 1996
7199767f8SToomas Soome  *	Matthias Drochner.  All rights reserved.
8199767f8SToomas Soome  *
9199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
10199767f8SToomas Soome  * modification, are permitted provided that the following conditions
11199767f8SToomas Soome  * are met:
12199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
13199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
14199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
15199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
16199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
17199767f8SToomas Soome  * 3. All advertising materials mentioning features or use of this software
18199767f8SToomas Soome  *    must display the following acknowledgement:
19199767f8SToomas Soome  *	This product includes software developed for the NetBSD Project
20199767f8SToomas Soome  *	by Matthias Drochner.
21199767f8SToomas Soome  * 4. The name of the author may not be used to endorse or promote products
22199767f8SToomas Soome  *    derived from this software without specific prior written permission.
23199767f8SToomas Soome  *
24199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25199767f8SToomas Soome  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26199767f8SToomas Soome  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27199767f8SToomas Soome  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28199767f8SToomas Soome  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29199767f8SToomas Soome  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30199767f8SToomas Soome  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31199767f8SToomas Soome  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32199767f8SToomas Soome  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33199767f8SToomas Soome  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34199767f8SToomas Soome  */
35199767f8SToomas Soome 
36199767f8SToomas Soome #include <sys/cdefs.h>
37199767f8SToomas Soome __FBSDID("$FreeBSD$");
38199767f8SToomas Soome 
39199767f8SToomas Soome /* read into destination in flat addr space */
40199767f8SToomas Soome 
41199767f8SToomas Soome #include <stand.h>
42199767f8SToomas Soome 
43199767f8SToomas Soome #include "libi386.h"
44199767f8SToomas Soome 
45199767f8SToomas Soome #ifdef SAVE_MEMORY
46199767f8SToomas Soome #define BUFSIZE (1*1024)
47199767f8SToomas Soome #else
48199767f8SToomas Soome #define BUFSIZE (4*1024)
49199767f8SToomas Soome #endif
50199767f8SToomas Soome 
51199767f8SToomas Soome static char     buf[BUFSIZE];
52199767f8SToomas Soome 
53*55fea89dSDan Cross int
pread(fd,dest,size)54199767f8SToomas Soome pread(fd, dest, size)
55199767f8SToomas Soome 	int             fd;
56199767f8SToomas Soome 	vm_offset_t         dest;
57199767f8SToomas Soome 	int             size;
58199767f8SToomas Soome {
59199767f8SToomas Soome 	int             rsize;
60199767f8SToomas Soome 
61199767f8SToomas Soome 	rsize = size;
62199767f8SToomas Soome 	while (rsize > 0) {
63199767f8SToomas Soome 		int             count, got;
64199767f8SToomas Soome 
65199767f8SToomas Soome 		count = (rsize < BUFSIZE ? rsize : BUFSIZE);
66199767f8SToomas Soome 
67199767f8SToomas Soome 		got = read(fd, buf, count);
68199767f8SToomas Soome 		if (got < 0)
69199767f8SToomas Soome 			return (-1);
70199767f8SToomas Soome 
71199767f8SToomas Soome 		/* put to physical space */
72199767f8SToomas Soome 		vpbcopy(buf, dest, got);
73199767f8SToomas Soome 
74199767f8SToomas Soome 		dest += got;
75199767f8SToomas Soome 		rsize -= got;
76199767f8SToomas Soome 		if (got < count)
77199767f8SToomas Soome 			break;	/* EOF */
78199767f8SToomas Soome 	}
79199767f8SToomas Soome 	return (size - rsize);
80199767f8SToomas Soome }
81