diff --git a/include/picrin/port.h b/include/picrin/port.h index 0b89e373..857d2deb 100644 --- a/include/picrin/port.h +++ b/include/picrin/port.h @@ -16,6 +16,9 @@ enum pic_port_status { }; typedef struct { + char *buf; + int mode; + int bufsiz; struct { void *cookie; int (*read)(void *, char *, int); @@ -41,6 +44,7 @@ struct pic_port *pic_stdin(pic_state *); struct pic_port *pic_stdout(pic_state *); struct pic_port *pic_stderr(pic_state *); +int pic_setvbuf(pic_file *, char *, int, size_t); int pic_fflush(pic_file *); pic_file *pic_funopen(void *cookie, int (*read)(void *, char *, int), int (*write)(void *, const char *, int), fpos_t (*seek)(void *, fpos_t, int), int (*close)(void *)); diff --git a/src/port.c b/src/port.c index 11e835e7..2c7fd2c7 100644 --- a/src/port.c +++ b/src/port.c @@ -37,6 +37,32 @@ pic_stdout(pic_state *pic) return pic_port_ptr(pic_apply(pic, proc, pic_nil_value())); } +int +pic_setvbuf(pic_file *file, char *buf, int mode, size_t bufsiz) +{ + /* FIXME: free old buf */ + + file->mode = mode; + if (buf) { + file->buf = buf; + file->bufsiz = bufsiz; + } + else { + if (mode == _IONBF) { + file->buf = NULL; + file->bufsiz = 0; + } + else { + assert(bufsiz == 0); + file->buf = (char *)malloc(BUFSIZ); + file->bufsiz = BUFSIZ; + } + } + file->s = file->c = file->buf; + file->e = file->buf + file->bufsiz; + return 0; +} + pic_file * pic_funopen(void *cookie, int (*read)(void *, char *, int),