2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Merge this applet into dpkg when dpkg becomes more stable
31 extern int gz_open(FILE *compressed_file, int *pid);
32 extern void gz_close(int gunzip_pid);
34 typedef struct ar_headers_s {
42 struct ar_headers_s *next;
45 extern ar_headers_t get_ar_headers(int srcFd);
46 extern int tar_unzip_init(int tarFd);
47 extern int readTarFile(int tarFd, int extractFlag, int listFlag,
48 int tostdoutFlag, int verboseFlag, char** extractList, char** excludeList);
50 static const int dpkg_deb_contents = 1;
51 static const int dpkg_deb_control = 2;
52 // const int dpkg_deb_info = 4;
53 static const int dpkg_deb_extract = 8;
54 static const int dpkg_deb_verbose_extract = 16;
55 static const int dpkg_deb_list = 32;
57 extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename)
59 char **extract_list = NULL;
60 ar_headers_t *ar_headers = NULL;
62 int extract_flag = FALSE;
63 int list_flag = FALSE;
64 int verbose_flag = FALSE;
65 int extract_to_stdout = FALSE;
69 FILE *comp_file = NULL;
71 if (dpkg_deb_contents == (dpkg_deb_contents & optflags)) {
72 strcpy(ar_filename, "data.tar.gz");
76 if (dpkg_deb_list == (dpkg_deb_list & optflags)) {
77 strcpy(ar_filename, "data.tar.gz");
80 if (dpkg_deb_control == (dpkg_deb_control & optflags)) {
81 strcpy(ar_filename, "control.tar.gz");
84 if (dpkg_deb_extract == (dpkg_deb_extract & optflags)) {
85 strcpy(ar_filename, "data.tar.gz");
88 if (dpkg_deb_verbose_extract == (dpkg_deb_verbose_extract & optflags)) {
89 strcpy(ar_filename, "data.tar.gz");
94 ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
95 srcFd = open(deb_filename, O_RDONLY);
97 *ar_headers = get_ar_headers(srcFd);
98 if (ar_headers->next == NULL) {
99 error_msg_and_die("Couldnt find %s in %s", ar_filename, deb_filename);
102 while (ar_headers->next != NULL) {
103 if (strcmp(ar_headers->name, ar_filename) == 0) {
106 ar_headers = ar_headers->next;
108 lseek(srcFd, ar_headers->offset, SEEK_SET);
109 /* Uncompress the file */
110 comp_file = fdopen(srcFd, "r");
111 if ((srcFd = gz_open(comp_file, &pid)) == EXIT_FAILURE) {
112 error_msg_and_die("Couldnt unzip file");
114 if ( dir_name != NULL) {
115 if (is_directory(dir_name, TRUE, NULL)==FALSE) {
116 mkdir(dir_name, 0755);
118 if (chdir(dir_name)==-1) {
119 error_msg_and_die("Cannot change to dir %s", dir_name);
122 status = readTarFile(srcFd, extract_flag, list_flag,
123 extract_to_stdout, verbose_flag, NULL, extract_list);
125 /* we are deliberately terminating the child so we can safely ignore this */
126 signal(SIGTERM, SIG_IGN);
134 extern int dpkg_deb_main(int argc, char **argv)
136 char *target_dir = NULL;
140 while ((opt = getopt(argc, argv, "cexXl")) != -1) {
143 optflag |= dpkg_deb_contents;
146 optflag |= dpkg_deb_control;
149 optflag |= dpkg_deb_verbose_extract;
152 optflag |= dpkg_deb_extract;
155 optflag |= dpkg_deb_list;
158 optflag |= dpkg_deb_info;
166 if (((optind + 1 ) > argc) || (optflag == 0)) {
169 if ((optflag & dpkg_deb_control) || (optflag & dpkg_deb_extract) || (optflag & dpkg_deb_verbose_extract)) {
170 if ( (optind + 1) == argc ) {
171 target_dir = (char *) xmalloc(7);
172 strcpy(target_dir, "DEBIAN");
174 target_dir = (char *) xmalloc(strlen(argv[optind + 1]));
175 strcpy(target_dir, argv[optind + 1]);
178 deb_extract(optflag, target_dir, argv[optind]);
179 /* else if (optflag & dpkg_deb_info) {
181 extract_to_stdout = TRUE;
182 strcpy(ar_filename, "control.tar.gz");
183 extract_list = argv+optind+1;
184 printf("list one is [%s]\n",extract_list[0]);
187 return(EXIT_SUCCESS);