cosmopolitan/libc/stdio/fread_unlocked.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

166 lines
5 KiB
C
Raw Normal View History

/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=8 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
2024-05-25 02:28:23 +00:00
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/iovec.h"
#include "libc/errno.h"
#include "libc/macros.h"
#include "libc/stdckdint.h"
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
2024-05-25 02:28:23 +00:00
#include "libc/sysv/errfuns.h"
static ssize_t readvall(FILE *f, struct iovec *iov, int iovlen, size_t need) {
2024-05-25 02:28:23 +00:00
ssize_t rc;
size_t got, toto;
for (toto = 0;;) {
// perform i/o
if ((rc = readv(f->fd, iov, iovlen)) == -1) {
f->state = errno;
if (toto)
return toto;
2024-05-25 02:28:23 +00:00
return -1;
}
got = rc;
toto += got;
if (!got) {
f->state = EOF;
return toto;
}
// roll forward iov
// skip over empty elements
2024-05-25 02:28:23 +00:00
for (;;) {
if (!iov->iov_len) {
--iovlen;
++iov;
} else if (got >= iov->iov_len) {
got -= iov->iov_len;
--iovlen;
++iov;
} else {
iov->iov_base += got;
iov->iov_len -= got;
break;
}
if (!iovlen)
return toto;
2024-05-25 02:28:23 +00:00
}
// don't trigger eof condition if we're rolling greed to fill buffer
if (toto >= need)
return toto;
}
2024-05-25 02:28:23 +00:00
}
/**
* Reads data from stream.
*
* @param stride specifies the size of individual items
* @param count is the number of strides to fetch
* @return count on success, [0,count) on eof, or 0 on error or count==0
*/
size_t fread_unlocked(void *buf, size_t stride, size_t count, FILE *f) {
char *p;
ssize_t rc;
struct iovec iov[2];
2024-05-25 02:28:23 +00:00
size_t n, m, got, need;
// check state and parameters
if ((f->iomode & O_ACCMODE) == O_WRONLY) {
f->state = errno = EBADF;
return 0;
}
if (f->beg > f->end) {
f->state = errno = EINVAL;
return 0;
}
if (ckd_mul(&n, stride, count)) {
f->state = errno = EOVERFLOW;
return 0;
}
2024-05-25 02:28:23 +00:00
if (!n)
return 0;
// try to fulfill request from buffer if possible
p = buf;
m = f->end - f->beg;
2024-05-25 02:28:23 +00:00
if (n <= m) {
memcpy(p, f->buf + f->beg, n);
if ((f->beg += n) == f->end) {
f->beg = 0;
f->end = 0;
}
return count;
}
2024-05-25 02:28:23 +00:00
// handle end-of-file condition in fileless mode
if (f->fd == -1) {
2024-05-25 02:28:23 +00:00
m /= stride;
m *= stride;
if (m)
memcpy(p, f->buf + f->beg, m);
if ((f->beg += m) == f->end) {
f->state = EOF;
f->beg = 0;
f->end = 0;
}
return m / stride;
}
2024-05-25 02:28:23 +00:00
// `n` is number of bytes requested by caller
// `m` is how much of `n` came from existing buffer
// `iov[0]` reads remainder of the caller request
// `iov[1]` reads ahead extra content into buffer
if (m)
memcpy(p, f->buf + f->beg, m);
iov[0].iov_base = p + m;
2024-05-25 02:28:23 +00:00
iov[0].iov_len = need = n - m;
if (f->bufmode != _IONBF && n < f->size) {
iov[1].iov_base = f->buf;
2024-05-25 02:28:23 +00:00
if (f->size > PUSHBACK)
iov[1].iov_len = f->size - PUSHBACK;
2024-05-25 02:28:23 +00:00
else
iov[1].iov_len = f->size;
} else {
iov[1].iov_base = NULL;
iov[1].iov_len = 0;
}
rc = readvall(f, iov, 2, need);
if (rc == -1)
return 0;
2024-05-25 02:28:23 +00:00
got = rc;
// handle partial fulfillment
if (got < need) {
got += m;
f->beg = 0;
f->end = 0;
return got / stride;
}
2024-05-25 02:28:23 +00:00
// handle overfulfillment
f->beg = 0;
f->end = got - need;
return count;
}