Make major improvements to stdio

Buffering now has optimal performance, bugs have been fixed, and some
missing apis have been introduced. This implementation is also now more
production worthy since it's less brittle now in terms of system errors.
That's going to help redbean since lua i/o is all based on stdio.

See #97
This commit is contained in:
Justine Tunney 2021-03-26 22:31:41 -07:00
parent 09bcfa23d5
commit da36e7e256
69 changed files with 1595 additions and 735 deletions

View file

@ -16,22 +16,18 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
static noinline int __fgetc(FILE *f) {
if (!f->reader) return __fseteof(f);
if (f->reader(f) == -1) return -1;
return f->buf[f->beg++];
}
/**
* Reads uint8_t from stream.
* Reads byte from stream.
* @return byte in range 0..255, or -1 w/ errno
*/
int fgetc(FILE *f) {
unsigned char b;
if (f->beg < f->end) {
return f->buf[f->beg++];
return f->buf[f->beg++] & 0xff;
} else {
return __fgetc(f);
if (!fread(&b, 1, 1, f)) return -1;
return b;
}
}