2 * Copyright (c) 2011, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
15 #include "sat_xfopen.h"
17 static ssize_t cookie_gzread(void *cookie, char *buf, size_t nbytes)
19 return gzread((gzFile *)cookie, buf, nbytes);
23 cookie_gzclose(void *cookie)
25 return gzclose((gzFile *)cookie);
28 static FILE *mygzfopen(gzFile* gzf)
32 gzf, (int (*)(void *, char *, int))cookie_gzread,
33 (int (*)(void *, const char *, int))NULL, /* writefn */
34 (fpos_t (*)(void *, fpos_t, int))NULL, /* seekfn */
37 #elif defined(HAVE_FOPENCOOKIE)
38 cookie_io_functions_t cio;
39 memset(&cio, 0, sizeof(cio));
40 cio.read = cookie_gzread;
41 cio.close = cookie_gzclose;
42 return fopencookie(gzf, "r", cio);
44 # error Need to implement custom I/O
49 sat_xfopen(const char *fn, const char *mode)
58 suf = strrchr(fn, '.');
59 if (!suf || strcmp(suf, ".gz") != 0)
60 return fopen(fn, mode);
61 gzf = gzopen(fn, mode);
64 return mygzfopen(gzf);
68 sat_xfopen_fd(const char *fn, int fd, const char *mode)
73 suf = fn ? strrchr(fn, '.') : 0;
76 int fl = fcntl(fd, F_GETFL, 0);
79 fl &= O_RDONLY|O_WRONLY|O_RDWR;
82 else if (fl == O_RDWR)
87 if (!suf || strcmp(suf, ".gz") != 0)
88 return fdopen(fd, mode);
89 gzf = gzdopen(fd, mode);
92 return mygzfopen(gzf);