summaryrefslogtreecommitdiff
path: root/src/user/lib/file.c
diff options
context:
space:
mode:
authordzwdz2022-08-19 15:37:42 +0200
committerdzwdz2022-08-19 15:37:42 +0200
commit05f93a814a9b5fa6b0f3223fc51566c84b92d158 (patch)
treeb430bda5fd26c3cd7e273eda31171bf94100f9f1 /src/user/lib/file.c
parent0ed2f796d7723af8321f35d4ef5e6781ea41e36d (diff)
user/libc: fextflags, add nonbuffering mode for fread
useful for e.g. `hexdump -r /kdev/eth` to see packets as they come in
Diffstat (limited to 'src/user/lib/file.c')
-rw-r--r--src/user/lib/file.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/user/lib/file.c b/src/user/lib/file.c
index c8acbfb..f2b74b1 100644
--- a/src/user/lib/file.c
+++ b/src/user/lib/file.c
@@ -87,10 +87,11 @@ FILE *fdopen(int fd, const char *mode) {
FILE *f;
f = malloc(sizeof *f);
if (!f) return NULL;
+ f->fd = fd;
f->pos = mode[0] == 'a' ? -1 : 0;
f->eof = false;
- f->fd = fd;
f->error = false;
+ f->extflags = 0;
return f;
}
@@ -110,6 +111,12 @@ FILE *file_clone(const FILE *f, const char *mode) {
return f2;
}
+int fextflags(FILE *f, int extflags) {
+ int old = f->extflags;
+ f->extflags = extflags;
+ return old;
+}
+
static void fadvance(long amt, FILE *f) {
bool pos_neg = f->pos < 0;
f->pos += amt;
@@ -133,16 +140,16 @@ size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict f) {
if (res < 0) {
f->error = true;
errno = -res;
- return pos/size;
+ break;
} else if (res == 0) {
f->eof = true;
- return pos/size;
- } else {
- pos += res;
- fadvance(res, f);
+ break;
}
+ pos += res;
+ fadvance(res, f);
+ if (f->extflags & FEXT_NOFILL) break;
}
- return nitems;
+ return pos == total ? nitems : (pos/size);
}
size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict f) {