xref: /freebsd/lib/libz/zopen.c (revision 23090366f729c56cab62de74c7a51792357e98a9)
1 /*
2  * Public domain stdio wrapper for libz, written by Johan Danielsson.
3  */
4 
5 #include <sys/cdefs.h>
6 __FBSDID("$FreeBSD: head/lib/libz/zopen.c 84228 2001-09-30 22:39:00Z dillon $");
7 
8 #include <stdio.h>
9 #include <zlib.h>
10 
11 FILE *zopen(const char *fname, const char *mode);
12 
13 /* convert arguments */
14 static int
15 xgzread(void *cookie, char *data, int size)
16 {
17     return gzread(cookie, data, size);
18 }
19 
20 static int
21 xgzwrite(void *cookie, const char *data, int size)
22 {
23     return gzwrite(cookie, (void*)data, size);
24 }
25 
26 static int
27 xgzclose(void *cookie)
28 {
29     return gzclose(cookie);
30 }
31 
32 FILE *
33 zopen(const char *fname, const char *mode)
34 {
35     gzFile gz = gzopen(fname, mode);
36     if(gz == NULL)
37 	return NULL;
38 
39     if(*mode == 'r')
40 	return (funopen(gz, xgzread, NULL, NULL, xgzclose));
41     else
42 	return (funopen(gz, NULL, xgzwrite, NULL, xgzclose));
43 }
44