Deleted Added
full compact
read.c (38452) read.c (65470)
1/* $FreeBSD: head/lib/libstand/read.c 65470 2000-09-05 09:52:50Z msmith $ */
1/* $NetBSD: read.c,v 1.8 1997/01/22 00:38:12 cgd Exp $ */
2
3/*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.

--- 54 unchanged lines hidden (view full) ---

63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
65 */
66
67#include <sys/param.h>
68#include "stand.h"
69
70ssize_t
2/* $NetBSD: read.c,v 1.8 1997/01/22 00:38:12 cgd Exp $ */
3
4/*-
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * The Mach Operating System project at Carnegie-Mellon University.

--- 54 unchanged lines hidden (view full) ---

64 * any improvements or extensions that they make and grant Carnegie the
65 * rights to redistribute these changes.
66 */
67
68#include <sys/param.h>
69#include "stand.h"
70
71ssize_t
71read(fd, dest, bcount)
72 int fd;
73 void *dest;
74 size_t bcount;
72read(int fd, void *dest, size_t bcount)
75{
73{
76 register struct open_file *f = &files[fd];
77 size_t resid;
74 struct open_file *f = &files[fd];
75 size_t resid;
78
76
79 if ((unsigned)fd >= SOPEN_MAX || !(f->f_flags & F_READ)) {
80 errno = EBADF;
81 return (-1);
77 if ((unsigned)fd >= SOPEN_MAX || !(f->f_flags & F_READ)) {
78 errno = EBADF;
79 return (-1);
80 }
81 if (f->f_flags & F_RAW) {
82 twiddle();
83 errno = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
84 btodb(f->f_offset), bcount, dest, &resid);
85 if (errno)
86 return (-1);
87 f->f_offset += resid;
88 return (resid);
89 }
90
91 /*
92 * Optimise reads from regular files using a readahead buffer.
93 * If the request can't be satisfied from the current buffer contents,
94 * check to see if it should be bypassed, or refill the buffer and complete
95 * the request.
96 */
97 resid = bcount;
98 for (;;) {
99 size_t ccount, cresid;
100 /* how much can we supply? */
101 ccount = imin(f->f_ralen, resid);
102 if (ccount > 0) {
103 bcopy(f->f_rabuf + f->f_raoffset, dest, ccount);
104 f->f_raoffset += ccount;
105 f->f_ralen -= ccount;
106 resid -= ccount;
107 if (resid == 0)
108 return(bcount);
109 dest += ccount;
82 }
110 }
83 if (f->f_flags & F_RAW) {
84 twiddle();
85 errno = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
86 btodb(f->f_offset), bcount, dest, &resid);
87 if (errno)
88 return (-1);
89 f->f_offset += resid;
90 return (resid);
111
112 /* will filling the readahead buffer again not help? */
113 if (resid >= SOPEN_RASIZE) {
114 /* bypass the rest of the request and leave the buffer empty */
115 if ((errno = (f->f_ops->fo_read)(f, dest, resid, &cresid)))
116 return(bcount - resid);
117 return(bcount - cresid);
91 }
118 }
92 resid = bcount;
93 if ((errno = (f->f_ops->fo_read)(f, dest, bcount, &resid)))
94 return (-1);
95 return (ssize_t)(bcount - resid);
119
120 /* fetch more data */
121 if ((errno = (f->f_ops->fo_read)(f, f->f_rabuf, SOPEN_RASIZE, &cresid)))
122 return(bcount - resid); /* behave like fread() */
123 f->f_raoffset = 0;
124 f->f_ralen = SOPEN_RASIZE - cresid;
125 /* no more data, return what we had */
126 if (f->f_ralen == 0)
127 return(bcount - resid);
128 }
96}
129}