5 /* Trivial example of reading a gzip'ed file or gzip'ed standard input
6 * using stdio functions fread(), getc(), etc... fseek() is not supported.
7 * Modify according to your needs. You can easily construct the symmetric
10 * Usage: zread [file[.gz]]
11 * This programs assumes that gzip is somewhere in your path.
14 main (int argc
, char **argv
)
21 if (argc
< 1 || argc
> 2) {
22 fprintf(stderr
, "usage: %s [file[.gz]]\n", argv
[0]);
25 strcpy(cmd
, "gzip -dc "); /* use "gzip -c" for zwrite */
27 strncat(cmd
, argv
[1], sizeof(cmd
)-strlen(cmd
));
29 infile
= popen(cmd
, "r"); /* use "w" for zwrite */
31 fprintf(stderr
, "%s: popen('%s', 'r') failed\n", argv
[0], cmd
);
34 /* Read one byte using getc: */
42 /* Read the rest using fread: */
44 n
= fread(buf
, 1, BUFSIZ
, infile
);
46 fwrite(buf
, 1, n
, stdout
);
48 if (pclose(infile
) != 0) {
49 fprintf(stderr
, "%s: pclose failed\n", argv
[0]);
53 return 0; /* just to make compiler happy */