diff --git a/lib/libz/Makefile b/lib/libz/Makefile index 0ea87bb191ab..96c46c67265b 100644 --- a/lib/libz/Makefile +++ b/lib/libz/Makefile @@ -7,6 +7,7 @@ LIB= z SHLIBDIR?= /lib SHLIB_MAJOR= 6 MAN= zlib.3 zopen.3 +MLINKS+= zopen.3 zdopen.3 ZLIBSRC= ${SRCTOP}/contrib/zlib diff --git a/lib/libz/Symbol.map b/lib/libz/Symbol.map index 92b02b56d0eb..0a91537272c1 100644 --- a/lib/libz/Symbol.map +++ b/lib/libz/Symbol.map @@ -103,6 +103,10 @@ FBSD_1.2 { zopen; }; +FBSD_1.6 { + zdopen; +}; + ZLIBprivate_1.0 { _tr_align; _tr_flush_block; diff --git a/lib/libz/Versions.def b/lib/libz/Versions.def index d5f1816b2be8..9a67602185b8 100644 --- a/lib/libz/Versions.def +++ b/lib/libz/Versions.def @@ -15,6 +15,9 @@ ZLIB_1.2.9 { FBSD_1.2 { } ZLIB_1.2.4.0; +FBSD_1.6 { +} FBSD_1.2; + ZLIBprivate_1.0 { } ZLIB_1.2.4.0; diff --git a/lib/libz/zopen.3 b/lib/libz/zopen.3 index b56fb447a345..7677f300a8ae 100644 --- a/lib/libz/zopen.3 +++ b/lib/libz/zopen.3 @@ -23,7 +23,7 @@ .\" .\" $FreeBSD$ .\" -.Dd March 5, 2014 +.Dd December 6, 2018 .Dt ZOPEN 3 .Os .Sh NAME @@ -34,33 +34,44 @@ .Sh SYNOPSIS .Ft FILE * .Fn zopen "const char *path" "const char *mode" +.Ft FILE * +.Fn zdopen "int fd" "const char *mode" .Sh DESCRIPTION The .Fn zopen -opens a gzip file whose name is the string pointed to by +function opens a gzip file whose name is the string pointed to by .Fa path -and associates a stream with it. -It is a wrapper around +and returns a stream which can be used to access the uncompressed contents +of the file. +The +.Fn zdopen +variant takes a gzip file referenced by the file descriptor +.Fa fd , +analogous to +.Xr fdopen 3 . +They are wrappers around .Xr zlib 3 -and standard stream I/O APIs. +and the standard stream I/O APIs. .Pp The argument .Fa mode -have the same meaning as it does in +has the same meaning as it does in .Xr fopen 3 . .Pp The -.Nm -function will associate read, write, seek and close +.Fn zopen +and +.Fn zdopen +functions will associate the read, write, seek and close functions of .Xr zlib 3 -after successfully opened a file with -.Xr funopen 3 -so that they will be used to read or write the new stream. +with the returned stream. .Sh RETURN VALUES Upon successful completion -.Nm -returns a +.Fn zopen +and +.Fn zdopen +return a .Tn FILE pointer. Otherwise, @@ -70,26 +81,28 @@ is returned and the global variable is set to indicate the error. .Sh ERRORS In addition to the errors documented for -.Xr fopen 3 , -the -.Nm -function may also fail for: +.Xr fopen 3 +and +.Xr fdopen 3 , +the functions may also fail for: .Bl -tag -width Er .It Bq Er ENOMEM Insufficient memory is available. .El .Sh COMPATIBILITY -This implementation of -.Nm +The implementation of +.Fn zopen function first appeared in .Nx 1.6 and .Fx 4.5 . -The -.Nm -function may not be portable to systems other than +.Fn zdopen +first appeared in +.Fx 13.0 . +These functions may not be portable to systems other than .Fx . .Sh SEE ALSO +.Xr fdopen 3 , .Xr fopen 3 , .Xr funopen 3 , .Xr zlib 3 diff --git a/lib/libz/zopen.c b/lib/libz/zopen.c index cde022dcf0e6..a6ac99166128 100644 --- a/lib/libz/zopen.c +++ b/lib/libz/zopen.c @@ -9,6 +9,7 @@ __FBSDID("$FreeBSD$"); #include FILE *zopen(const char *fname, const char *mode); +FILE *zdopen(int fd, const char *mode); /* convert arguments */ static int @@ -47,3 +48,18 @@ zopen(const char *fname, const char *mode) else return (funopen(gz, NULL, xgzwrite, xgzseek, xgzclose)); } + +FILE * +zdopen(int fd, const char *mode) +{ + gzFile gz; + + gz = gzdopen(fd, mode); + if (gz == NULL) + return (NULL); + + if (*mode == 'r') + return (funopen(gz, xgzread, NULL, xgzseek, xgzclose)); + else + return (funopen(gz, NULL, xgzwrite, xgzseek, xgzclose)); +}