]> Pileus Git - ~andy/rsl/blob - gzip.c
RSL v1.42
[~andy/rsl] / gzip.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1996, 1997
5             John H. Merritt
6             Space Applications Corporation
7             Vienna, Virginia
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU Library General Public
11     License as published by the Free Software Foundation; either
12     version 2 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17     Library General Public License for more details.
18
19     You should have received a copy of the GNU Library General Public
20     License along with this library; if not, write to the Free
21     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #define _USE_BSD
28 #include <sys/types.h>
29 #include <sys/resource.h>
30 #include <sys/wait.h>
31 #include <signal.h>
32
33 /* Prototype definitions within this file. */
34 int no_command (char *cmd);
35 FILE *uncompress_pipe (FILE *fp);
36 FILE *compress_pipe (FILE *fp);
37
38
39 /* Avoids the 'Broken pipe' message by reading the rest of the stream. */
40 void rsl_readflush(FILE *fp)
41 {
42   if (fork() == 0) { /* Child */
43         char buf[1024];
44         while(fread(buf, sizeof(char), sizeof(buf), fp)) continue;
45         exit(0);
46   }
47 }
48         
49 int rsl_pclose(FILE *fp)
50 {
51   int rc;
52   if ((rc=pclose(fp)) == EOF) {
53         perror ("pclose");  /* This or fclose do the job. */
54         if ((rc=fclose(fp)) == EOF)
55           perror ("fclose");  /* This or fclose do the job. */
56   }
57   return rc;
58 }
59
60 int no_command (char *cmd)
61 {
62   int rc;
63   /* Return 0 if there is the command 'cmd' on the system. */
64   /* Return !0 otherwise. */
65   rc = system(cmd);
66   if (rc == 0) return rc;
67   else return !0;
68 }
69
70 FILE *uncompress_pipe (FILE *fp)
71 {
72   /* Pass the file pointed to by 'fp' through the gzip pipe. */
73
74   FILE *fpipe;
75   int save_fd;
76
77   if (no_command("gzip --version > /dev/null 2>&1")) return fp;
78   save_fd = dup(0);
79   close(0); /* Redirect stdin for gzip. */
80   dup(fileno(fp));
81
82   fpipe = popen("gzip -q -d -f --stdout", "r");
83   if (fpipe == NULL) perror("uncompress_pipe");
84   close(0);
85   dup(save_fd);
86   close(save_fd);
87   return fpipe;
88 }
89
90 FILE *compress_pipe (FILE *fp)
91 {
92   /* Pass the file pointed to by 'fp' through the gzip pipe. */
93
94   FILE *fpipe;
95   int save_fd;
96
97   if (no_command("gzip --version > /dev/null 2>&1")) return fp;
98   fflush(NULL); /* Flush all buffered output before opening this pipe. */
99   save_fd = dup(1);
100   close(1); /* Redirect stdout for gzip. */
101   dup(fileno(fp));
102
103   fpipe = popen("gzip -q -1 -c", "w");
104   if (fpipe == NULL) perror("compress_pipe");
105   close(1);
106   dup(save_fd);
107   return fpipe;
108 }
109
110