Home | History | Annotate | Line # | Download | only in savecore
zopen.c revision 1.2.46.1
      1 /*	$NetBSD: zopen.c,v 1.2.46.1 2013/01/23 00:05:33 yamt Exp $	*/
      2 
      3 /*
      4  * Public domain stdio wrapper for libz, written by Johan Danielsson.
      5  */
      6 
      7 #include <sys/cdefs.h>
      8 #ifndef lint
      9 __RCSID("$NetBSD: zopen.c,v 1.2.46.1 2013/01/23 00:05:33 yamt Exp $");
     10 #endif
     11 
     12 #include <stdio.h>
     13 #include <zlib.h>
     14 
     15 FILE *zopen(const char *fname, const char *mode);
     16 
     17 /* convert arguments */
     18 static int
     19 xgzread(void *cookie, char *data, int size)
     20 {
     21     return gzread(cookie, data, size);
     22 }
     23 
     24 static int
     25 xgzwrite(void *cookie, const char *data, int size)
     26 {
     27     return gzwrite(cookie, __UNCONST(data), size);
     28 }
     29 
     30 FILE *
     31 zopen(const char *fname, const char *mode)
     32 {
     33     gzFile gz = gzopen(fname, mode);
     34     if(gz == NULL)
     35 	return NULL;
     36 
     37     if(*mode == 'r')
     38 	return funopen(gz, xgzread, NULL, NULL, gzclose);
     39     else
     40 	return funopen(gz, NULL, xgzwrite, NULL, gzclose);
     41 }
     42