xref: /freebsd/lib/libz/zopen.c (revision 09a53ad8f1318c5daae6cfb19d97f4f6459f0013)
1 /*
2  * Public domain stdio wrapper for libz, written by Johan Danielsson.
3  */
4 
5 #include <sys/cdefs.h>
6 __FBSDID("$FreeBSD$");
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 static fpos_t
33 xgzseek(void *cookie,  fpos_t offset, int whence)
34 {
35 	return gzseek(cookie, (z_off_t)offset, whence);
36 }
37 
38 FILE *
39 zopen(const char *fname, const char *mode)
40 {
41     gzFile gz = gzopen(fname, mode);
42     if(gz == NULL)
43 	return NULL;
44 
45     if(*mode == 'r')
46 	return (funopen(gz, xgzread, NULL, xgzseek, xgzclose));
47     else
48 	return (funopen(gz, NULL, xgzwrite, xgzseek, xgzclose));
49 }
50