1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
#!/bin/sh
#
# $NetBSD: extract,v 1.12 2006/01/21 18:01:26 jlam Exp $
#
# Copyright (c) 2006 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Johnny C. Lam.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by the NetBSD
# Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
######################################################################
#
# NAME
# extract -- extract distfile, regardless of format
#
# SYNOPSIS
# extract [options] distfile [file ...]
#
# DESCRIPTION
# extract will unpack the contents of the named distfile into the
# current working directory. If any optional files are specified then
# only they will be extracted from the distfile, provided that the
# underlying tool supports this ability. If the distfile's file
# extension doesn't match any known archive format's, then the
# distfile is simply copied into the current working directory.
#
# OPTIONS
# -d dir Extract the files into the specified dir instead
# of the current working directory. If the directory
# doesn't exist, then it is created along with any
# intermediate directories using the current umask.
#
# -f format Force interpretation of the distfile's archive
# format to be the specified format.
#
# -t tarprog This specifies the tool to use to extract tar/ustar
# archives, and may be either "tar" or "pax", or the
# full path to the program.
#
# -X excludefile excludefile is a list of file patterns to exclude
# from extraction. If the -X option is specified
# then any optional files listed on the command line
# are ignored.
#
# -x This causes the optional files listed on the
# command line to be excluded from extraction,
# provided the underlying tool supports this
# ability.
#
# ENVIRONMENT
# EXTRACT_OPTS_BIN
# EXTRACT_OPTS_LHA
# EXTRACT_OPTS_PAX
# EXTRACT_OPTS_RAR
# EXTRACT_OPTS_TAR
# EXTRACT_OPTS_ZIP
# EXTRACT_OPTS_ZOO
# These variables set additional arguments passed to the
# underlying extraction tool to unpack their respective
# archive formats.
#
######################################################################
: ${BZCAT:=bzcat}
: ${CAT:=cat}
: ${CP:=cp}
: ${ECHO:=echo}
: ${GZCAT:=gzcat}
: ${LHA:=lha}
: ${MKDIR:=mkdir}
: ${PAX:=pax}
: ${RM:=rm}
: ${SH:=sh}
: ${TAR:=tar}
: ${TEST:=test}
: ${TMPDIR:=/tmp}
: ${UNRAR:=unrar}
: ${UNZIP:=unzip}
: ${UNZOO:=unzoo}
self="${0##*/}"
usage() {
${ECHO} 1>&2 "usage: $self [-d dir] [-f format] [-t tarprog] [-X excludefile | -x] distfile [file ...]"
}
decompress_cat="${CAT}"
exclude=no
exclude_file=
exclude_flag=
extract_dir=.
extract_using=tar
format=
# Process optional arguments
while ${TEST} $# -gt 0; do
case "$1" in
-d) extract_dir="$2"; shift 2 ;;
-f) format="$2"; shift 2 ;;
-t) extract_using="$2"; shift 2 ;;
-X) exclude_file="$2"; shift 2 ;;
-x) exclude=yes; shift ;;
--) shift; break ;;
-*) ${ECHO} 1>&2 "$self: unknown option -- ${1#-}"
usage
exit 1
;;
*) break ;;
esac
done
case "$extract_using" in
/*tar|/*pax) tarprog="$extract_using" ;;
*tar) tarprog="${TAR}" ;;
*pax) tarprog="${PAX}" ;;
esac
if ${TEST} -n "$exclude_file" -a ! -f "$exclude_file"; then
${ECHO} 1>&2 "$self: exclude file missing: $exclude_file"
exit 1
fi
# Process required arguments
${TEST} $# -gt 0 || { usage; exit 1; }
distfile="$1"; shift
# Set the command to decompress the file and write the contents to stdout.
case "$distfile" in
*.gz|*.tgz|*.z) decompress_cat="${GZCAT}" ;;
*.bz2|*.tbz|*.tbz2) decompress_cat="${BZCAT}" ;;
*.Z) decompress_cat="${GZCAT}" ;;
esac
# Derive the format of the archive based on the file extension.
case "$distfile" in
*.tar.gz|*.tgz|*_tar.gz|*.tar.bz2|*.tbz|*.tbz2|*.tar.Z|*.tar)
_format=tar ;;
*.shar.gz|*.shar.bz2|*.shar.Z|*.shar|*.shr.gz|*.shr.bz2|*.shr.Z|*.shr)
_format=shar ;;
*.zip) _format=zip ;;
*.lha|*.lzh) _format=lha ;;
*.Z|*.bz2|*.gz|*.z)
_format=compressed ;;
*.zoo) _format=zoo ;;
*.rar) _format=rar ;;
*.bin) _format=jre-bin ;;
*) _format=none ;;
esac
${TEST} -n "$format" || format="$_format"
${TEST} -d "$extract_dir" || ${MKDIR} -p "$extract_dir"
cd "$extract_dir"
# Use the correct tool and extraction procedure to perform the extraction
# based on the archive format.
#
case "$format" in
tar)
case "$extract_using" in
*pax)
case "$extract_using" in
/*) paxprog="$extract_using" ;;
*) paxprog="${PAX}" ;;
esac
if ${TEST} -n "$exclude_file"; then
exclude=yes
set -- dummy `${CAT} "$exclude_file"`; shift
fi
${TEST} "$exclude" = no || exclude_flag="-c"
$decompress_cat "$distfile" |
$paxprog ${EXTRACT_OPTS_PAX} $exclude_flag -O -r "$@"
;;
*tar)
case "$extract_using" in
/*) tarprog="$extract_using" ;;
*) tarprog="${TAR}" ;;
esac
tmpfile=
if ${TEST} "$exclude" = "yes"; then
tmpfile="${TMPDIR}/$self.$$"
${RM} -f "$tmpfile"
for i in "$@"; do
${ECHO} "$i" >> "$tmpfile"
done
exclude_file="$tmpfile"
fi
if ${TEST} -n "$exclude_file"; then
exclude_flag="-X $exclude_file"
set -- dummy; shift
fi
$decompress_cat "$distfile" |
$tarprog ${EXTRACT_OPTS_TAR} $exclude_flag -xf - "$@" \
&& ${RM} -f $tmpfile
;;
*)
${ECHO} 1>&2 "$self: unknown tar program: $extract_using"
exit 1
esac
;;
shar)
$decompress_cat "$distfile" | ${SH}
;;
zip)
: ${EXTRACT_OPTS_ZIP=-Laqo}
${TEST} "$exclude" = "no" || exclude_flag="-x"
if ${TEST} -n "$exclude_file"; then
set -- dummy `${CAT} "$exclude_file"`; shift
fi
${UNZIP} ${EXTRACT_OPTS_ZIP} "$distfile" $exclude_flag "$@"
;;
lha)
: ${EXTRACT_OPTS_LHA=q}
${LHA} x$extract_options "$distfile" "$@"
;;
compressed)
target="${distfile##*/}"; target="${target%.*}"
$decompress_cat "$distfile" > "$target"
;;
zoo)
${UNZOO} -x ${EXTRACT_OPTS_ZOO} "$distfile" "$@"
;;
rar)
: ${EXTRACT_OPTS_RAR=-inul}
${UNRAR} -x ${EXTRACT_OPTS_RAR} "$distfile" "$@"
;;
jre-bin)
${ECHO} yes | "$distfile" ${EXTRACT_OPTS_BIN} >/dev/null
;;
none)
# By default, copy the distfile over to the current working directory.
${CP} "$distfile" .
;;
*)
${ECHO} 1>&2 "$self: archive format not recognized -- $format"
exit 1
;;
esac
|