pread.c revision 1.4
11.4Sdsl/* $NetBSD: pread.c,v 1.4 2003/02/01 14:48:18 dsl 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 * 3. All advertising materials mentioning features or use of this software 161.1Sperry * must display the following acknowledgement: 171.1Sperry * This product includes software developed for the NetBSD Project 181.1Sperry * by Matthias Drochner. 191.1Sperry * 4. The name of the author may not be used to endorse or promote products 201.1Sperry * derived from this software without specific prior written permission. 211.1Sperry * 221.1Sperry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 231.1Sperry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 241.1Sperry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 251.1Sperry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 261.1Sperry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 271.1Sperry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 281.1Sperry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 291.1Sperry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 301.1Sperry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 311.1Sperry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 321.1Sperry * 331.1Sperry */ 341.1Sperry 351.1Sperry/* read into destination in flat addr space */ 361.1Sperry 371.1Sperry#include <lib/libsa/stand.h> 381.1Sperry 391.1Sperry#include "libi386.h" 401.1Sperry 411.1Sperry#ifdef SAVE_MEMORY 421.1Sperry#define BUFSIZE (1*1024) 431.1Sperry#else 441.1Sperry#define BUFSIZE (4*1024) 451.1Sperry#endif 461.1Sperry 471.4Sdslstatic char *buf; 481.1Sperry 491.3Schristosssize_t 501.2Sthorpejpread(fd, dest, size) 511.3Schristos int fd; 521.3Schristos void *dest; 531.3Schristos size_t size; 541.1Sperry{ 551.2Sthorpej int rsize; 561.4Sdsl 571.4Sdsl if (!buf) 581.4Sdsl buf = alloc(BUFSIZE); 591.1Sperry 601.2Sthorpej rsize = size; 611.2Sthorpej while (rsize > 0) { 621.2Sthorpej int count, got; 631.2Sthorpej 641.2Sthorpej count = (rsize < BUFSIZE ? rsize : BUFSIZE); 651.2Sthorpej 661.2Sthorpej got = read(fd, buf, count); 671.2Sthorpej if (got < 0) 681.2Sthorpej return (-1); 691.2Sthorpej 701.2Sthorpej /* put to physical space */ 711.2Sthorpej vpbcopy(buf, dest, got); 721.2Sthorpej 731.2Sthorpej dest += got; 741.2Sthorpej rsize -= got; 751.2Sthorpej if (got < count) 761.2Sthorpej break; /* EOF */ 771.2Sthorpej } 781.2Sthorpej return (size - rsize); 791.1Sperry} 80