diff options
-rw-r--r-- | gzio.c | 35 | ||||
-rw-r--r-- | gzio.h | 2 | ||||
-rw-r--r-- | stream.c | 14 | ||||
-rw-r--r-- | stream.h | 1 |
4 files changed, 49 insertions, 3 deletions
@@ -479,6 +479,13 @@ gzFile w_gzopen_mode(const wchar_t *wname, const wchar_t *wmode, uw_throwf(file_error_s, lit("~a: gzip stream cannot both read and write"), self, nao); } + +#if HAVE_FCNTL + { + int fd = w_open_mode(wname, m); + return (fd < 0) ? NULL : w_gzdopen_mode(fd, wmode, m, self); + } +#else { char *name = utf8_dup_to(wname); char *mode = utf8_dup_to(wmode); @@ -487,6 +494,34 @@ gzFile w_gzopen_mode(const wchar_t *wname, const wchar_t *wmode, free(mode); return f; } +#endif +} + +gzFile w_gzdopen_mode(int fd, const wchar_t *wmode, + const struct stdio_mode m, val self) +{ + if (m.buforder >= 0 || m.nonblock || m.notrunc || m.unbuf || + m.linebuf || m.interactive) + { + goto badmode; + } + + if (m.read && m.write) { + uw_throwf(file_error_s, + lit("~a: gzip stream cannot both read and write"), self, nao); + } + + { + char *mode = utf8_dup_to(wmode); + gzFile f = gzdopen(fd, mode); + free(mode); + if (f) + return f; + } + +badmode: + uw_throwf(file_error_s, + lit("~a: invalid modes for gzip stream"), self, nao); } val make_gzio_stream(gzFile f, int fd, val descr, int is_output) @@ -30,4 +30,6 @@ val gzio_stream_s; void gzio_init(void); gzFile w_gzopen_mode(const wchar_t *wname, const wchar_t *wmode, const struct stdio_mode m, val self); +gzFile w_gzdopen_mode(int fd, const wchar_t *wmode, + const struct stdio_mode m, val self); val make_gzio_stream(gzFile f, int fd, val descr, int is_output); @@ -1127,10 +1127,9 @@ static struct strm_ops stdio_ops = static struct strm_ops stdio_sock_ops; #endif -static FILE *w_fopen_mode(const wchar_t *wname, const wchar_t *mode, - const struct stdio_mode m) -{ #if HAVE_FCNTL +int w_open_mode(const wchar_t *wname, const struct stdio_mode m) +{ char *name = utf8_dup_to(wname); size_t nsiz = strlen(name) + 1; int flags = (if3(m.read && m.write, O_RDWR, 0) | @@ -1150,6 +1149,15 @@ static FILE *w_fopen_mode(const wchar_t *wname, const wchar_t *mode, fd = open(stkname, flags, 0666); sig_restore_enable; + return fd; +} +#endif + +static FILE *w_fopen_mode(const wchar_t *wname, const wchar_t *mode, + const struct stdio_mode m) +{ +#if HAVE_FCNTL + int fd = w_open_mode(wname, m); return (fd < 0) ? NULL : w_fdopen(fd, mode); #else /* TODO: detect if fopen supports "x" in mode */ @@ -167,6 +167,7 @@ void fill_stream_ops(struct strm_ops *ops); void stream_print_op(val stream, val out, val pretty, struct strm_ctx *); void stream_mark_op(val stream); void stream_destroy_op(val stream); +int w_open_mode(const wchar_t *wname, const struct stdio_mode m); struct stdio_mode parse_mode(val mode_str, struct stdio_mode m_dfl, val self); val normalize_mode(struct stdio_mode *m, val mode_str, struct stdio_mode m_dfl, val self); |