pread.c revision 1.7
11.7Schristos/*	$NetBSD: pread.c,v 1.7 2008/12/14 17:03:43 christos 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.2Sthorpej	int             rsize;
471.4Sdsl
481.4Sdsl	if (!buf)
491.4Sdsl		buf = alloc(BUFSIZE);
501.1Sperry
511.2Sthorpej	rsize = size;
521.2Sthorpej	while (rsize > 0) {
531.2Sthorpej		int             count, got;
541.2Sthorpej
551.2Sthorpej		count = (rsize < BUFSIZE ? rsize : BUFSIZE);
561.2Sthorpej
571.2Sthorpej		got = read(fd, buf, count);
581.2Sthorpej		if (got < 0)
591.7Schristos			return -1;
601.2Sthorpej
611.2Sthorpej		/* put to physical space */
621.2Sthorpej		vpbcopy(buf, dest, got);
631.2Sthorpej
641.2Sthorpej		dest += got;
651.2Sthorpej		rsize -= got;
661.2Sthorpej		if (got < count)
671.2Sthorpej			break;	/* EOF */
681.2Sthorpej	}
691.7Schristos	return size - rsize;
701.1Sperry}
71