ef30ff8945b500149490e1be187674908dd74e79
[platform/upstream/busybox.git] / libbb / gz_open.c
1 #include <sys/types.h>
2 #include <sys/wait.h>
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include "libbb.h"
8
9 extern FILE *gz_open(FILE *compressed_file, int *pid)
10 {
11         int unzip_pipe[2];
12
13         if (pipe(unzip_pipe)!=0) {
14                 error_msg("pipe error");
15                 return(NULL);
16         }
17         if ((*pid = fork()) == -1) {
18                 error_msg("fork failed");
19                 return(NULL);
20         }
21         if (*pid==0) {
22                 /* child process */
23                 close(unzip_pipe[0]);
24                 unzip(compressed_file, fdopen(unzip_pipe[1], "w"));
25                 fflush(NULL);
26                 fclose(compressed_file);
27                 close(unzip_pipe[1]);
28                 exit(EXIT_SUCCESS);
29         }
30         close(unzip_pipe[1]);
31         if (unzip_pipe[0] == -1) {
32                 error_msg("gzip stream init failed");
33         }
34         return(fdopen(unzip_pipe[0], "r"));
35 }