summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-05-26 22:54:49 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-05-26 22:54:49 -0700
commit3fc97c29d1407756bb3e7e36c40cfed966ef82f7 (patch)
tree07d93f424e09091d7de3ec88684717f25d3ba1b7
parente23478755d418e5f80384dbfa2536f2aaa3c888b (diff)
downloadtxr-3fc97c29d1407756bb3e7e36c40cfed966ef82f7.tar.gz
txr-3fc97c29d1407756bb3e7e36c40cfed966ef82f7.tar.bz2
txr-3fc97c29d1407756bb3e7e36c40cfed966ef82f7.zip
open-fileno: support "z" flag for gzip.
* stream.c (open_fileno): Use w_gzdopen_mode and make_gzio_stream to make a gzio stream if the gzip flag is present.
-rw-r--r--stream.c39
1 files changed, 27 insertions, 12 deletions
diff --git a/stream.c b/stream.c
index e336d35f..e394d6ee 100644
--- a/stream.c
+++ b/stream.c
@@ -4285,20 +4285,35 @@ val open_fileno(val fd, val mode_str)
{
val self = lit("open-fileno");
struct stdio_mode m, m_r = stdio_mode_init_r;
- FILE *f = (errno = 0, w_fdopen(c_num(fd, self),
- c_str(normalize_mode(&m, mode_str, m_r, self),
- self)));
+ val norm_mode = normalize_mode(&m, mode_str, m_r, self);
- if (!f) {
- int eno = errno;
- close(c_num(fd, self));
- uw_ethrowf(errno_to_file_error(eno), lit("error opening descriptor ~a: ~d/~s"),
- fd, num(eno), errno_to_str(eno), nao);
- }
+ if (!m.gzip) {
+ FILE *f = (errno = 0, w_fdopen(c_num(fd, self),
+ c_str(norm_mode, self)));
- return set_mode_props(m, make_stdio_stream(f, format(nil,
- lit("fd ~d"),
- fd, nao)));
+ if (!f)
+ {
+ int eno = errno;
+ close(c_num(fd, self));
+ uw_ethrowf(errno_to_file_error(eno),
+ lit("error opening descriptor ~a: ~d/~s"),
+ fd, num(eno), errno_to_str(eno), nao);
+ }
+
+ return set_mode_props(m, make_stdio_stream(f, format(nil,
+ lit("fd ~d"),
+ fd, nao)));
+ } else {
+#if HAVE_ZLIB
+ cnum fdn = c_num(fd, self);
+ gzFile f = w_gzdopen_mode(fdn, c_str(norm_mode, self), m, self);
+ return make_gzio_stream(f, fdn, format(nil, lit("fd ~d"), fd, nao),
+ m.write);
+#else
+ uw_ethrowf(file_error_s, lit("~s: not built with zlib support"),
+ self, nao);
+#endif
+ }
}
val open_tail(val path, val mode_str, val seek_end_p)