]> Pileus Git - ~andy/rsl/blob - src/gzip.c
Build fixes for win32
[~andy/rsl] / src / 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 <signal.h>
30 #include "win32compat.h"
31
32 /* Prototype definitions within this file. */
33 int no_command (char *cmd);
34 FILE *uncompress_pipe (FILE *fp);
35 FILE *compress_pipe (FILE *fp);
36
37
38 /* Avoids the 'Broken pipe' message by reading the rest of the stream. */
39 void rsl_readflush(FILE *fp)
40 {
41   if (fork() == 0) { /* Child */
42         char buf[1024];
43         while(fread(buf, sizeof(char), sizeof(buf), fp)) continue;
44         exit(0);
45   }
46 }
47         
48 int rsl_pclose(FILE *fp)
49 {
50   int rc;
51   if ((rc=pclose(fp)) == EOF) {
52         perror ("pclose");  /* This or fclose do the job. */
53         if ((rc=fclose(fp)) == EOF)
54           perror ("fclose");  /* This or fclose do the job. */
55   }
56   return rc;
57 }
58
59 int no_command (char *cmd)
60 {
61   int rc;
62   /* Return 0 if there is the command 'cmd' on the system. */
63   /* Return !0 otherwise. */
64   rc = system(cmd);
65   if (rc == 0) return rc;
66   else return !0;
67 }
68
69 FILE *uncompress_pipe (FILE *fp)
70 {
71   /* Pass the file pointed to by 'fp' through the gzip pipe. */
72
73   FILE *fpipe;
74   int save_fd;
75
76   if (no_command("gzip --version > /dev/null 2>&1")) return fp;
77   save_fd = dup(0);
78   close(0); /* Redirect stdin for gzip. */
79   if (dup(fileno(fp)) < 0) {
80     perror("decompress_pipe");
81     return NULL;
82   }
83
84   fpipe = popen("gzip -q -d -f --stdout", "r");
85   if (fpipe == NULL) perror("uncompress_pipe");
86   close(0);
87   if (dup(save_fd) < 0) {
88     perror("decompress_pipe");
89     return NULL;
90   }
91   close(save_fd);
92   return fpipe;
93 }
94
95 FILE *compress_pipe (FILE *fp)
96 {
97   /* Pass the file pointed to by 'fp' through the gzip pipe. */
98
99   FILE *fpipe;
100   int save_fd;
101
102   if (no_command("gzip --version > /dev/null 2>&1")) return fp;
103   fflush(NULL); /* Flush all buffered output before opening this pipe. */
104   save_fd = dup(1);
105   close(1); /* Redirect stdout for gzip. */
106   if (dup(fileno(fp)) < 0) {
107     perror("compress_pipe");
108     return NULL;
109   }
110
111   fpipe = popen("gzip -q -1 -c", "w");
112   if (fpipe == NULL) perror("compress_pipe");
113   close(1);
114   if (dup(save_fd) < 0) {
115     perror("compress_pipe");
116     return NULL;
117   }
118   return fpipe;
119 }
120
121