aboutsummaryrefslogtreecommitdiff
path: root/lib/libz/zopen.c
blob: a6ac99166128a5db7132e0546b063de7554669d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Public domain stdio wrapper for libz, written by Johan Danielsson.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <stdio.h>
#include <zlib.h>

FILE *zopen(const char *fname, const char *mode);
FILE *zdopen(int fd, const char *mode);

/* convert arguments */
static int
xgzread(void *cookie, char *data, int size)
{
    return gzread(cookie, data, size);
}

static int
xgzwrite(void *cookie, const char *data, int size)
{
    return gzwrite(cookie, (void*)data, size);
}

static int
xgzclose(void *cookie)
{
    return gzclose(cookie);
}

static fpos_t
xgzseek(void *cookie,  fpos_t offset, int whence)
{
	return gzseek(cookie, (z_off_t)offset, whence);
}

FILE *
zopen(const char *fname, const char *mode)
{
    gzFile gz = gzopen(fname, mode);
    if(gz == NULL)
	return NULL;

    if(*mode == 'r')
	return (funopen(gz, xgzread, NULL, xgzseek, xgzclose));
    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));
}