Fix issues with stdio needed for Lua

See #61
This commit is contained in:
Justine Tunney
2021-03-06 16:06:15 -08:00
parent c3ed8d6c7f
commit d769df3482
17 changed files with 102 additions and 155 deletions

View File

@@ -17,6 +17,7 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/stdio/stdio.h"
/**
@@ -25,6 +26,23 @@
* @param stream is a non-null stream handle
* @returns current byte offset from beginning of file, or -1
*/
long ftell(FILE *stream) {
return fseek(stream, 0, SEEK_CUR);
long ftell(FILE *f) {
int64_t pos;
if (f->fd != -1) {
if (f->beg && !f->end) {
f->writer(f);
}
if ((pos = lseek(f->fd, 0, SEEK_CUR)) != -1) {
f->state = 0;
f->beg = 0;
f->end = 0;
return pos;
} else {
f->state = errno == ESPIPE ? EBADF : errno;
return -1;
}
} else {
errno = f->state;
return -1;
}
}