pread.c revision 1.8
11.8Smanu/*	$NetBSD: pread.c,v 1.8 2019/09/13 02:19:46 manu Exp $	 */
21.1Sperry
31.1Sperry/*
41.1Sperry * Copyright (c) 1996
51.1Sperry *	Matthias Drochner.  All rights reserved.
61.1Sperry *
71.1Sperry * Redistribution and use in source and binary forms, with or without
81.1Sperry * modification, are permitted provided that the following conditions
91.1Sperry * are met:
101.1Sperry * 1. Redistributions of source code must retain the above copyright
111.1Sperry *    notice, this list of conditions and the following disclaimer.
121.1Sperry * 2. Redistributions in binary form must reproduce the above copyright
131.1Sperry *    notice, this list of conditions and the following disclaimer in the
141.1Sperry *    documentation and/or other materials provided with the distribution.
151.1Sperry *
161.1Sperry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sperry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sperry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sperry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sperry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211.1Sperry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221.1Sperry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231.1Sperry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241.1Sperry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251.1Sperry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261.1Sperry *
271.1Sperry */
281.1Sperry
291.1Sperry/* read into destination in flat addr space */
301.1Sperry
311.1Sperry#include <lib/libsa/stand.h>
321.1Sperry
331.1Sperry#include "libi386.h"
341.1Sperry
351.1Sperry#ifdef SAVE_MEMORY
361.1Sperry#define BUFSIZE (1*1024)
371.1Sperry#else
381.1Sperry#define BUFSIZE (4*1024)
391.1Sperry#endif
401.1Sperry
411.4Sdslstatic char     *buf;
421.1Sperry
431.7Schristosssize_t
441.7Schristospread(int fd, void *dest, size_t size)
451.1Sperry{
461.8Smanu	size_t             rsize;
471.4Sdsl
481.4Sdsl	if (!buf)
491.4Sdsl		buf = alloc(BUFSIZE);
501.1Sperry
511.2Sthorpej	rsize = size;
521.2Sthorpej	while (rsize > 0) {
531.8Smanu		size_t count;
541.8Smanu		ssize_t got;
551.2Sthorpej
561.2Sthorpej		count = (rsize < BUFSIZE ? rsize : BUFSIZE);
571.2Sthorpej
581.2Sthorpej		got = read(fd, buf, count);
591.2Sthorpej		if (got < 0)
601.7Schristos			return -1;
611.2Sthorpej
621.2Sthorpej		/* put to physical space */
631.2Sthorpej		vpbcopy(buf, dest, got);
641.2Sthorpej
651.2Sthorpej		dest += got;
661.2Sthorpej		rsize -= got;
671.2Sthorpej		if (got < count)
681.2Sthorpej			break;	/* EOF */
691.2Sthorpej	}
701.7Schristos	return size - rsize;
711.1Sperry}
72