diff options
author | Nicholas <nbnoll@eml.cc> | 2021-11-20 17:07:23 -0800 |
---|---|---|
committer | Nicholas <nbnoll@eml.cc> | 2021-11-20 17:07:23 -0800 |
commit | c9a32c1a43d2bdded07eaa45732c3a6e195a5442 (patch) | |
tree | cb7e9cb8c34cbe6551b801eff4201afcf71dd0fd /include/base/io.h | |
parent | e97c8c469db0aa27985dab2879dc1f14905c7387 (diff) |
Chore: cleaned up the exit code to cleanly interface with libc
We use weak linking to ensure we clean up at exit time correctly. If
libc is linked, then we call our cleanup function by registering an
atexit callback with the library. If libc is not linked, we have a weak
symbol that results in a noop. Similarly, if we call rt·exit while
linked with libc, this immediately calls libc's exit (which will call
our cleanup as we registered it). If we are not linked to libc, exit()
is given as a weak link to a noop function.
Diffstat (limited to 'include/base/io.h')
-rw-r--r-- | include/base/io.h | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/include/base/io.h b/include/base/io.h index 5e0f1e0..5d31282 100644 --- a/include/base/io.h +++ b/include/base/io.h @@ -1,12 +1,5 @@ #pragma once -enum SeekPos -{ - seek·cur = SEEK_CUR, - seek·set = SEEK_SET, - seek·end = SEEK_END -}; - typedef struct io·Reader { int (*read)(void*, int sz, int n, void *buf); @@ -22,7 +15,7 @@ extern io·Peeker sys·Peeker; typedef struct io·Seeker { - int (*seek)(void *skr, long off, enum SeekPos whence); + int (*seek)(void *skr, long off, int whence); long (*tell)(void *skr); } io·Seeker; extern io·Seeker sys·Seeker; @@ -93,10 +86,11 @@ int io·putbyte(io·Stream *s, byte c); int io·putstring(io·Stream *s, string str); int io·write(io·Stream *s, int sz, int n, void *buf); int io·flush(io·Stream *s); -int io·seek(io·Stream *s, long off, enum SeekPos whence); +int io·seek(io·Stream *s, long off, int whence); long io·tell(io·Stream *s); /* buffered i/o */ +typedef struct io·Header io·Header; typedef struct io·Buffer io·Buffer; #define iota(x) (1 << (x)) @@ -116,21 +110,26 @@ enum }; #undef iota -struct io·Buffer +struct io·Header { - int state, id, flag; + int state, fd, flag; struct{ - int in; /* negative number of bytes at end */ - int out; /* number of bytes at start */ - int line; /* number of bytes after last readline */ + int in; /* negative number of bytes at end */ + int out; /* number of bytes at start */ + int ln; /* number of bytes after last readline */ } off; intptr pos, cap; /* position in file, capacity of buffer */ uchar *b,*g,*e; /* start, good bytes, end of byte pointers */ - uchar bytes[]; +}; + +struct io·Buffer +{ + io·Header; + uchar bytes[io·BufLen+io·BufUngets]; }; int bio·init(io·Buffer *io, int fd, int mode); -int bio·initcap(io·Buffer *io, int fd, int mode, int cap); +int bio·initcap(io·Header *io, int fd, int mode, int cap, uchar *buf); /* basic os helpers */ /* XXX: find a better location for this */ |