xref: /freebsd/stand/i386/libi386/pread.c (revision 3e15b01d6914c927e37d1699645783acf286655c)
1*ca987d46SWarner Losh /*
2*ca987d46SWarner Losh  * $NetBSD: pread.c,v 1.2 1997/03/22 01:48:38 thorpej Exp $
3*ca987d46SWarner Losh  */
4*ca987d46SWarner Losh 
5*ca987d46SWarner Losh /*-
6*ca987d46SWarner Losh  * Copyright (c) 1996
7*ca987d46SWarner Losh  *	Matthias Drochner.  All rights reserved.
8*ca987d46SWarner Losh  *
9*ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
10*ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
11*ca987d46SWarner Losh  * are met:
12*ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
13*ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
14*ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
15*ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
16*ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
17*ca987d46SWarner Losh  * 3. All advertising materials mentioning features or use of this software
18*ca987d46SWarner Losh  *    must display the following acknowledgement:
19*ca987d46SWarner Losh  *	This product includes software developed for the NetBSD Project
20*ca987d46SWarner Losh  *	by Matthias Drochner.
21*ca987d46SWarner Losh  * 4. The name of the author may not be used to endorse or promote products
22*ca987d46SWarner Losh  *    derived from this software without specific prior written permission.
23*ca987d46SWarner Losh  *
24*ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25*ca987d46SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26*ca987d46SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27*ca987d46SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28*ca987d46SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29*ca987d46SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30*ca987d46SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31*ca987d46SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*ca987d46SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33*ca987d46SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*ca987d46SWarner Losh  */
35*ca987d46SWarner Losh 
36*ca987d46SWarner Losh /* read into destination in flat addr space */
37*ca987d46SWarner Losh 
38*ca987d46SWarner Losh #include <stand.h>
39*ca987d46SWarner Losh 
40*ca987d46SWarner Losh #include "libi386.h"
41*ca987d46SWarner Losh 
42*ca987d46SWarner Losh #ifdef SAVE_MEMORY
43*ca987d46SWarner Losh #define BUFSIZE (1*1024)
44*ca987d46SWarner Losh #else
45*ca987d46SWarner Losh #define BUFSIZE (4*1024)
46*ca987d46SWarner Losh #endif
47*ca987d46SWarner Losh 
48*ca987d46SWarner Losh static char     buf[BUFSIZE];
49*ca987d46SWarner Losh 
50*ca987d46SWarner Losh int
pread(fd,dest,size)51*ca987d46SWarner Losh pread(fd, dest, size)
52*ca987d46SWarner Losh 	int             fd;
53*ca987d46SWarner Losh 	vm_offset_t         dest;
54*ca987d46SWarner Losh 	int             size;
55*ca987d46SWarner Losh {
56*ca987d46SWarner Losh 	int             rsize;
57*ca987d46SWarner Losh 
58*ca987d46SWarner Losh 	rsize = size;
59*ca987d46SWarner Losh 	while (rsize > 0) {
60*ca987d46SWarner Losh 		int             count, got;
61*ca987d46SWarner Losh 
62*ca987d46SWarner Losh 		count = (rsize < BUFSIZE ? rsize : BUFSIZE);
63*ca987d46SWarner Losh 
64*ca987d46SWarner Losh 		got = read(fd, buf, count);
65*ca987d46SWarner Losh 		if (got < 0)
66*ca987d46SWarner Losh 			return (-1);
67*ca987d46SWarner Losh 
68*ca987d46SWarner Losh 		/* put to physical space */
69*ca987d46SWarner Losh 		vpbcopy(buf, dest, got);
70*ca987d46SWarner Losh 
71*ca987d46SWarner Losh 		dest += got;
72*ca987d46SWarner Losh 		rsize -= got;
73*ca987d46SWarner Losh 		if (got < count)
74*ca987d46SWarner Losh 			break;	/* EOF */
75*ca987d46SWarner Losh 	}
76*ca987d46SWarner Losh 	return (size - rsize);
77*ca987d46SWarner Losh }
78