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 */
42 ar_headers_t head, *extract_list=NULL;
44 extract_list = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
46 while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
49 funct |= preserve_date;
58 funct |= extract_to_stdout;
61 funct |= extract_to_file;
68 /* check the src filename was specified */
72 if ( (srcFd = open(argv[optind], O_RDONLY)) < 0)
73 error_msg_and_die("Cannot read %s", argv[optind]);
76 head = get_ar_headers(srcFd);
78 /* find files to extract or display */
79 /* search through argv and build extract list */
80 for (;optind<argc; optind++) {
81 ar_headers_t *ar_entry;
82 ar_entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
84 while (ar_entry->next != NULL) {
85 if (strcmp(argv[optind], ar_entry->name) == 0) {
87 tmp = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
89 *extract_list = *ar_entry;
90 extract_list->next = tmp;
93 ar_entry=ar_entry->next;
97 /* if individual files not found extract all files */
98 if (extract_list->next==NULL) {
102 /* find files to extract or display */
103 while (extract_list->next != NULL) {
104 if (funct & extract_to_file) {
105 dstFd = open(extract_list->name, O_WRONLY | O_CREAT, extract_list->mode);
107 else if (funct & extract_to_stdout) {
108 dstFd = fileno(stdout);
110 if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
111 lseek(srcFd, extract_list->offset, SEEK_SET);
112 copy_file_chunk(srcFd, dstFd, (off_t) extract_list->size);
114 if (funct & verbose) {
115 printf("%s %d/%d %8d %s ", mode_string(extract_list->mode),
116 extract_list->uid, extract_list->gid,
117 (int) extract_list->size, time_string(extract_list->mtime));
119 if ((funct & display) || (funct & verbose)){
120 printf("%s\n", extract_list->name);
122 extract_list=extract_list->next;