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