Read a FILE* till an empty line or eof and return it as a char buffer.
[oweals/busybox.git] / libbb / deb_extract.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright (C) tons of folks.  Tracking down who wrote what
4  * isn't something I'm going to worry about...  If you wrote something
5  * here, please feel free to acknowledge your work.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Based in part on code from sash, Copyright (c) 1999 by David I. Bell 
22  * Permission has been granted to redistribute this code under the GPL.
23  *
24  */
25
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <signal.h>
31 #include "libbb.h"
32
33 /*
34  * The contents of argument depend on the value of function.
35  * It is either a dir name or a control file or field name(see dpkg_deb.c)
36  */
37 extern int deb_extract(const char *package_filename, int function, char *argument)
38 {
39
40         FILE *deb_file, *uncompressed_file;
41         ar_headers_t *headers = NULL;
42         char *ared_file = NULL;
43         int gunzip_pid;
44
45         switch (function) {
46                 case (extract_info):
47                 case (extract_control):
48                 case (extract_field):
49                         ared_file = xstrdup("control.tar.gz");
50                         break;
51                 default:
52                         ared_file = xstrdup("data.tar.gz");
53                         break;
54         }
55
56         /* open the debian package to be worked on */
57         deb_file = wfopen(package_filename, "r");
58
59         headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));       
60         
61         /* get a linked list of all ar entries */
62         if ((headers = get_ar_headers(deb_file)) == NULL) {
63                 error_msg("Couldnt get ar headers\n");
64                 return(EXIT_FAILURE);
65         }
66
67         /* seek to the start of the .tar.gz file within the ar file*/
68         if (seek_ared_file(deb_file, headers, ared_file) == EXIT_FAILURE) {
69                 error_msg("Couldnt seek to ar file");
70         }
71
72         /* open a stream of decompressed data */
73         uncompressed_file = fdopen(gz_open(deb_file, &gunzip_pid), "r");
74
75         if (function & extract_fsys_tarfile) {
76                 copy_file_chunk(uncompressed_file, stdout, -1);
77         } else {
78                 untar(uncompressed_file, function, argument);
79         }
80         /* we are deliberately terminating the child so we can safely ignore this */
81         gz_close(gunzip_pid);
82
83         fclose(deb_file);
84         fclose(uncompressed_file);
85         free(ared_file);
86
87         return(EXIT_SUCCESS);
88 }