2 * Copyright (c) 2011, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
15 #include "solv_xfopen.h"
17 static ssize_t cookie_gzread(void *cookie, char *buf, size_t nbytes)
19 return gzread((gzFile *)cookie, buf, nbytes);
22 static ssize_t cookie_gzwrite(void *cookie, const char *buf, size_t nbytes)
24 return gzwrite((gzFile *)cookie, buf, nbytes);
28 cookie_gzclose(void *cookie)
30 return gzclose((gzFile *)cookie);
33 static FILE *mygzfopen(gzFile* gzf, const char *mode)
37 gzf, (int (*)(void *, char *, int))(*mode == 'r' ? cookie_gzread : NULL), /* readfn */
38 (int (*)(void *, const char *, int))(*mode == 'w' ? cookie_gzwrite : NULL), /* writefn */
39 (fpos_t (*)(void *, fpos_t, int))NULL, /* seekfn */
42 #elif defined(HAVE_FOPENCOOKIE)
43 cookie_io_functions_t cio;
44 memset(&cio, 0, sizeof(cio));
46 cio.read = cookie_gzread;
47 else if (*mode == 'w')
48 cio.write = cookie_gzwrite;
49 cio.close = cookie_gzclose;
50 return fopencookie(gzf, *mode == 'w' ? "w" : "r", cio);
52 # error Need to implement custom I/O
57 solv_xfopen(const char *fn, const char *mode)
66 suf = strrchr(fn, '.');
67 if (!suf || strcmp(suf, ".gz") != 0)
68 return fopen(fn, mode);
69 gzf = gzopen(fn, mode);
72 return mygzfopen(gzf, mode);
76 solv_xfopen_fd(const char *fn, int fd, const char *mode)
81 suf = fn ? strrchr(fn, '.') : 0;
84 int fl = fcntl(fd, F_GETFL, 0);
87 fl &= O_RDONLY|O_WRONLY|O_RDWR;
90 else if (fl == O_RDWR)
92 if (!suf || strcmp(suf, ".gz") != 0)
100 if (!suf || strcmp(suf, ".gz") != 0)
101 return fdopen(fd, mode);
102 gzf = gzdopen(fd, mode);
105 return mygzfopen(gzf, mode);