xref: /netbsd-src/sys/arch/i386/stand/lib/pread.c (revision 5e073c03257bbe307b85366c6116f0fcbf90f8a6)
1*5e073c03Smanu /*	$NetBSD: pread.c,v 1.8 2019/09/13 02:19:46 manu Exp $	 */
2816bb961Sperry 
3816bb961Sperry /*
4816bb961Sperry  * Copyright (c) 1996
5816bb961Sperry  *	Matthias Drochner.  All rights reserved.
6816bb961Sperry  *
7816bb961Sperry  * Redistribution and use in source and binary forms, with or without
8816bb961Sperry  * modification, are permitted provided that the following conditions
9816bb961Sperry  * are met:
10816bb961Sperry  * 1. Redistributions of source code must retain the above copyright
11816bb961Sperry  *    notice, this list of conditions and the following disclaimer.
12816bb961Sperry  * 2. Redistributions in binary form must reproduce the above copyright
13816bb961Sperry  *    notice, this list of conditions and the following disclaimer in the
14816bb961Sperry  *    documentation and/or other materials provided with the distribution.
15816bb961Sperry  *
16816bb961Sperry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17816bb961Sperry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18816bb961Sperry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19816bb961Sperry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20816bb961Sperry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21816bb961Sperry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22816bb961Sperry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23816bb961Sperry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24816bb961Sperry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25816bb961Sperry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26816bb961Sperry  *
27816bb961Sperry  */
28816bb961Sperry 
29816bb961Sperry /* read into destination in flat addr space */
30816bb961Sperry 
31816bb961Sperry #include <lib/libsa/stand.h>
32816bb961Sperry 
33816bb961Sperry #include "libi386.h"
34816bb961Sperry 
35816bb961Sperry #ifdef SAVE_MEMORY
36816bb961Sperry #define BUFSIZE (1*1024)
37816bb961Sperry #else
38816bb961Sperry #define BUFSIZE (4*1024)
39816bb961Sperry #endif
40816bb961Sperry 
4181acacd7Sdsl static char     *buf;
42816bb961Sperry 
439fa73628Schristos ssize_t
pread(int fd,void * dest,size_t size)44334f5e8fSchristos pread(int fd, void *dest, size_t size)
45816bb961Sperry {
46*5e073c03Smanu 	size_t             rsize;
47816bb961Sperry 
4881acacd7Sdsl 	if (!buf)
4981acacd7Sdsl 		buf = alloc(BUFSIZE);
5081acacd7Sdsl 
51816bb961Sperry 	rsize = size;
52816bb961Sperry 	while (rsize > 0) {
53*5e073c03Smanu 		size_t count;
54*5e073c03Smanu 		ssize_t got;
55816bb961Sperry 
56816bb961Sperry 		count = (rsize < BUFSIZE ? rsize : BUFSIZE);
57816bb961Sperry 
58816bb961Sperry 		got = read(fd, buf, count);
59f07be036Sthorpej 		if (got < 0)
60334f5e8fSchristos 			return -1;
61816bb961Sperry 
62816bb961Sperry 		/* put to physical space */
63816bb961Sperry 		vpbcopy(buf, dest, got);
64816bb961Sperry 
65816bb961Sperry 		dest += got;
66816bb961Sperry 		rsize -= got;
67f07be036Sthorpej 		if (got < count)
68f07be036Sthorpej 			break;	/* EOF */
69816bb961Sperry 	}
70334f5e8fSchristos 	return size - rsize;
71816bb961Sperry }
72