Make minor improvements

This commit is contained in:
Justine Tunney
2020-12-23 23:42:56 -08:00
parent 04caf6f9ad
commit 95b142e4e5
95 changed files with 3818 additions and 2760 deletions

View File

@@ -21,14 +21,8 @@
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
/**
* Writes byte to stream.
*
* @return c (as unsigned char) if written or -1 w/ errno
*/
noinstrument int fputc(int c, FILE *f) {
static noinline int __fputcg(int c, FILE *f) {
if (f->beg < f->size) {
c &= 0xff;
f->buf[f->beg++] = c;
if (f->beg == f->size || f->bufmode == _IONBF ||
(f->bufmode == _IOLBF && c == '\n')) {
@@ -38,8 +32,22 @@ noinstrument int fputc(int c, FILE *f) {
f->beg = 0;
}
}
return c;
return c & 0xff;
} else {
return __fseteof(f);
}
}
/**
* Writes byte to stream.
* @return c (as unsigned char) if written or -1 w/ errno
* @see putc() if called within loop
*/
noinstrument int fputc(int c, FILE *f) {
if (f->beg + 1 < f->size && f->bufmode == _IOFBF) {
f->buf[f->beg++] = c;
return c & 0xff;
} else {
return __fputcg(c, f);
}
}

53
libc/stdio/fputcfb.c Normal file
View File

@@ -0,0 +1,53 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ This program is free software; you can redistribute it and/or modify │
│ it under the terms of the GNU General Public License as published by │
│ the Free Software Foundation; version 2 of the License. │
│ │
│ This program is distributed in the hope that it will be useful, but │
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
│ General Public License for more details. │
│ │
│ You should have received a copy of the GNU General Public License │
│ along with this program; if not, write to the Free Software │
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
│ 02110-1301 USA │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
static noinline int slowpath(int c, FILE *f) {
if (f->beg < f->size) {
c &= 0xff;
f->buf[f->beg++] = c;
if (f->beg == f->size) {
if (f->writer) {
if (f->writer(f) == -1) return -1;
} else if (f->beg == f->size) {
f->beg = 0;
}
}
return c;
} else {
return __fseteof(f);
}
}
/**
* Writes byte to stream.
*
* @return c (as unsigned char) if written or -1 w/ errno
*/
noinstrument int fputcfb(int c, FILE *f) {
if (f->beg + 1 < f->size) {
c &= 0xff;
f->buf[f->beg++] = c;
return c;
} else {
return slowpath(c, f);
}
}

View File

@@ -38,7 +38,7 @@ int fputs(const char *s, FILE *f) {
int i, n, m;
n = strlen(s);
for (i = 0; i < n; ++i) {
if (fputc(s[i], f) == -1) {
if (putc(s[i], f) == -1) {
if (ferror(f) == EINTR) continue;
if (feof(f)) errno = f->state = EPIPE;
return -1;

View File

@@ -25,7 +25,7 @@
#include "libc/str/internal.h"
/**
* Reads data to stream.
* Reads data from stream.
*
* @param stride specifies the size of individual items
* @param count is the number of strides to fetch
@@ -36,7 +36,7 @@ size_t fread(void *buf, size_t stride, size_t count, FILE *f) {
size_t i, n;
unsigned char *p;
for (n = stride * count, p = buf, i = 0; i < n; ++i) {
if ((c = fgetc(f)) != -1) {
if ((c = getc(f)) != -1) {
p[i] = c & 0xff;
} else if (!(i % stride)) {
return i / stride;

View File

@@ -19,5 +19,5 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/stdio/internal.h"
alignas(PAGESIZE) unsigned char g_stdoutbuf[BUFSIZ];
alignas(PAGESIZE) unsigned char g_stderrbuf[BUFSIZ];
_Alignas(PAGESIZE) unsigned char g_stdoutbuf[BUFSIZ];
_Alignas(PAGESIZE) unsigned char g_stderrbuf[BUFSIZ];

View File

@@ -100,8 +100,8 @@ int vfscanf(FILE *, const char *, va_list);
│ cosmopolitan § standard i/o » optimizations ─╬─│┼
╚────────────────────────────────────────────────────────────────────────────│*/
#define putc(c, f) fputc(c, f)
#define getc(f) (f->beg < f->end ? f->buf[f->beg++] : fgetc(f))
#define putc(c, f) fputc(c, f)
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define printf(FMT, ...) (printf)(PFLINK(FMT), ##__VA_ARGS__)