diff options
Diffstat (limited to 'mbbsd/io.c')
-rw-r--r-- | mbbsd/io.c | 49 |
1 files changed, 45 insertions, 4 deletions
@@ -15,14 +15,54 @@ static int obufsize = 0, ibufsize = 0; static int icurrchar = 0; /* ----------------------------------------------------- */ -/* output routines */ +/* convert routines */ /* ----------------------------------------------------- */ +typedef int (* read_write_type)(int, void *, size_t); +static read_write_type write_type = (read_write_type)write; +static read_write_type read_type = read; + +int converting_read(int fd, void *buf, size_t count) +{ + int len = read(fd, buf, count); + if (len >= 0) + gb2big(buf, len); + return len; +} +int converting_write(int fd, void *buf, size_t count) +{ + big2gb(buf, count); + return write(fd, buf, count); +} + +void set_converting_type(int which) +{ + if (which == 0) { + read_type = read; + write_type = (read_write_type)write; + } + else if (which == 1) { + read_type = converting_read; + write_type = converting_write; + } +} + +inline static int read_wrapper(int fd, void *buf, size_t count) { + return (*read_type)(fd, buf, count); +} + +inline static int write_wrapper(int fd, void *buf, size_t count) { + return (*write_type)(fd, buf, count); +} + +/* ----------------------------------------------------- */ +/* output routines */ +/* ----------------------------------------------------- */ void oflush() { if (obufsize) { - write(1, outbuf, obufsize); + write_wrapper(1, outbuf, obufsize); obufsize = 0; } } @@ -40,7 +80,7 @@ output(char *s, int len) assert(len<OBUFSIZE); if (obufsize + len > OBUFSIZE) { - write(1, outbuf, obufsize); + write_wrapper(1, outbuf, obufsize); obufsize = 0; } memcpy(outbuf + obufsize, s, len); @@ -51,6 +91,7 @@ int ochar(int c) { if (obufsize > OBUFSIZE - 1) { + /* suppose one byte data doesn't need to be converted. */ write(1, outbuf, obufsize); obufsize = 0; } @@ -134,7 +175,7 @@ dogetch() #ifdef SKIP_TELNET_CONTROL_SIGNAL do{ #endif - while ((len = read(0, inbuf, IBUFSIZE)) <= 0) { + while ((len = read_wrapper(0, inbuf, IBUFSIZE)) <= 0) { if (len == 0 || errno != EINTR) abort_bbs(0); /* raise(SIGHUP); */ |