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.
22 if (argc
< 1 || argc
> 2) {
23 fprintf(stderr
, "usage: %s [file[.gz]]\n", argv
[0]);
26 strcpy(cmd
, "gzip -dc "); /* use "gzip -c" for zwrite */
28 strncat(cmd
, argv
[1], sizeof(cmd
)-strlen(cmd
));
30 infile
= popen(cmd
, "r"); /* use "w" for zwrite */
32 fprintf(stderr
, "%s: popen('%s', 'r') failed\n", argv
[0], cmd
);
35 /* Read one byte using getc: */
43 /* Read the rest using fread: */
45 n
= fread(buf
, 1, BUFSIZ
, infile
);
47 fwrite(buf
, 1, n
, stdout
);
49 if (pclose(infile
) != 0) {
50 fprintf(stderr
, "%s: pclose failed\n", argv
[0]);
54 return 0; /* just to make compiler happy */