1 /* vi: set sw=4 ts=4: */
3 * Mini ar implementation for busybox
5 * Copyright (C) 2000 by Glenn McGrath
6 * Written by Glenn McGrath <bug1@optushome.com.au> 1 June 2000
8 * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 extern int ar_main(int argc, char **argv)
33 const int preserve_date = 1; /* preserve original dates */
34 const int verbose = 2; /* be verbose */
35 const int display = 4; /* display contents */
36 const int extract_to_file = 8; /* extract contents of archive */
37 const int extract_to_stdout = 16; /* extract to stdout */
39 FILE *src_file = NULL, *dst_file = NULL;
42 ar_headers_t *head, *ar_extract_list=NULL;
44 ar_extract_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
45 head = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
47 while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
50 funct |= preserve_date;
59 funct |= extract_to_stdout;
62 funct |= extract_to_file;
69 /* check the src filename was specified */
74 if ( (src_file = wfopen(argv[optind], "r")) < 0) {
75 error_msg_and_die("Cannot read %s", argv[optind]);
79 head = get_ar_headers(src_file);
80 /* find files to extract or display */
81 /* search through argv and build extract list */
82 for (;optind < argc; optind++) {
83 ar_headers_t *ar_entry;
84 ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
86 while (ar_entry->next != NULL) {
87 if (strcmp(argv[optind], ar_entry->name) == 0) {
89 tmp = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
90 *tmp = *ar_extract_list;
91 *ar_extract_list = *ar_entry;
92 ar_extract_list->next = tmp;
95 ar_entry=ar_entry->next;
99 /* if individual files not found extract all files */
100 if (ar_extract_list->next==NULL) {
101 ar_extract_list = head;
104 /* find files to extract or display */
105 while (ar_extract_list->next != NULL) {
106 if (funct & extract_to_file) {
107 dst_file = wfopen(ar_extract_list->name, "w");
109 else if (funct & extract_to_stdout) {
112 if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
113 fseek(src_file, ar_extract_list->offset, SEEK_SET);
114 copy_file_chunk(src_file, dst_file, ar_extract_list->size);
116 if (funct & verbose) {
117 printf("%s %d/%d %8d %s ", mode_string(ar_extract_list->mode),
118 ar_extract_list->uid, ar_extract_list->gid,
119 (int) ar_extract_list->size, time_string(ar_extract_list->mtime));
121 if ((funct & display) || (funct & verbose)){
122 puts(ar_extract_list->name);
124 ar_extract_list = ar_extract_list->next;