source: trunk/essentials/app-arch/gzip/sample/zread.c

Last change on this file was 3325, checked in by bird, 18 years ago

gzip 1.3.11

File size: 1.3 KB
Line 
1#include <stdio.h>
2
3/* Trivial example of reading a gzip'ed file or gzip'ed standard input
4 * using stdio functions fread(), getc(), etc... fseek() is not supported.
5 * Modify according to your needs. You can easily construct the symmetric
6 * zwrite program.
7 *
8 * Usage: zread [file[.gz]]
9 * This programs assumes that gzip is somewhere in your path.
10 */
11int main(argc, argv)
12 int argc;
13 char **argv;
14{
15 FILE *infile;
16 char cmd[256];
17 char buf[BUFSIZ];
18 int n;
19
20 if (argc < 1 || argc > 2) {
21 fprintf(stderr, "usage: %s [file[.gz]]\n", argv[0]);
22 exit(1);
23 }
24 strcpy(cmd, "gzip -dc "); /* use "gzip -c" for zwrite */
25 if (argc == 2) {
26 strncat(cmd, argv[1], sizeof(cmd)-strlen(cmd));
27 }
28 infile = popen(cmd, "r"); /* use "w" for zwrite */
29 if (infile == NULL) {
30 fprintf(stderr, "%s: popen('%s', 'r') failed\n", argv[0], cmd);
31 exit(1);
32 }
33 /* Read one byte using getc: */
34 n = getc(infile);
35 if (n == EOF) {
36 pclose(infile);
37 exit(0);
38 }
39 putchar(n);
40
41 /* Read the rest using fread: */
42 for (;;) {
43 n = fread(buf, 1, BUFSIZ, infile);
44 if (n <= 0) break;
45 fwrite(buf, 1, n, stdout);
46 }
47 if (pclose(infile) != 0) {
48 fprintf(stderr, "%s: pclose failed\n", argv[0]);
49 exit(1);
50 }
51 exit(0);
52 return 0; /* just to make compiler happy */
53}
Note: See TracBrowser for help on using the repository browser.