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.
22 typedef struct ar_headers_s {
30 struct ar_headers_s *next;
33 extern ar_headers_t get_headers(int srcFd);
34 extern int tar_unzip_init(int tarFd);
35 extern int readTarFile(int tarFd, int extractFlag, int listFlag,
36 int tostdoutFlag, int verboseFlag, char** extractList,
39 extern int dpkg_deb_main(int argc, char **argv)
41 const int dpkg_deb_contents = 1;
42 const int dpkg_deb_control = 2;
43 // const int dpkg_deb_info = 4;
44 const int dpkg_deb_extract = 8;
45 const int dpkg_deb_verbose_extract = 16;
48 int extract_flag = FALSE;
49 int list_flag = FALSE;
50 int verbose_flag = FALSE;
51 int extract_to_stdout = FALSE;
55 ar_headers_t *ar_headers = NULL;
56 char **extract_list=NULL;
57 char *target_dir=NULL;
59 while ((opt = getopt(argc, argv, "cexX")) != -1) {
62 optflag |= dpkg_deb_contents;
65 optflag |= dpkg_deb_control;
68 optflag |= dpkg_deb_info;
72 optflag |= dpkg_deb_extract;
75 optflag |= dpkg_deb_verbose_extract;
78 usage(dpkg_deb_usage);
83 if (((optind + 1 ) > argc) || (optflag == 0)) {
84 usage(dpkg_deb_usage);
88 if (optflag & dpkg_deb_contents) {
91 strcpy(ar_filename, "data.tar.gz");
93 else if (optflag & dpkg_deb_control) {
95 strcpy(ar_filename, "control.tar.gz");
96 if ( (optind + 1) == argc ) {
97 target_dir = (char *) xmalloc(7);
98 strcpy(target_dir, "DEBIAN");
101 target_dir = (char *) xmalloc(strlen(argv[optind+1]));
103 strcpy(target_dir, argv[optind+1]);
106 /* else if (optflag & dpkg_deb_info) {
108 extract_to_stdout = TRUE;
109 strcpy(ar_filename, "control.tar.gz");
110 extract_list = argv+optind+1;
111 printf("list one is [%s]\n",extract_list[0]);
114 else if (optflag & dpkg_deb_extract) {
116 strcpy(ar_filename, "data.tar.gz");
117 if ( (optind + 2) > argc ) {
118 error_msg_and_die("No directory specified\n");
120 target_dir = (char *) xmalloc(strlen(argv[optind+1]));
121 strcpy(target_dir, argv[optind+1]);
123 else if (optflag & dpkg_deb_verbose_extract) {
126 strcpy(ar_filename, "data.tar.gz");
127 if ( (optind + 2) > argc ) {
128 error_msg_and_die("No directory specified\n");
130 target_dir = (char *) xmalloc(strlen(argv[optind+1]));
131 strcpy(target_dir, argv[optind+1]);
134 ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
135 srcFd = open(argv[optind], O_RDONLY);
137 *ar_headers = get_headers(srcFd);
138 if (ar_headers->next==NULL)
139 error_msg_and_die("Couldnt find %s in %s\n",ar_filename, argv[optind]);
141 while (ar_headers->next != NULL) {
142 if (strcmp(ar_headers->name, ar_filename)==0)
144 ar_headers = ar_headers->next;
147 lseek(srcFd, ar_headers->offset, SEEK_SET);
148 srcFd = tar_unzip_init(srcFd);
149 if ( target_dir != NULL) {
150 if (is_directory(target_dir, TRUE, NULL)==FALSE) {
151 mkdir(target_dir, 0755);
153 if (chdir(target_dir)==-1) {
154 error_msg_and_die("Cannot change to dir %s\n",argv[optind+1]);
157 status = readTarFile(srcFd, extract_flag, list_flag, extract_to_stdout, verbose_flag, NULL, extract_list);
158 return(EXIT_SUCCESS);