File: atool

package info (click to toggle)
atool 0.11.0-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 108 kB
  • ctags: 31
  • sloc: perl: 643; makefile: 95
file content (777 lines) | stat: -rwxr-xr-x 25,831 bytes parent folder | download
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#! /usr/bin/perl -w
#
# atool - A script for managing file archives of various types.
#
# Copyright (C) 2001  Oskar Liljeblad
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#   MA  02111-1307  USA
#
# See the atool(1) manual page for usage details.
#

use File::Basename;
use File::Spec;
use Getopt::Long;
use strict;

# Subroutine prototypes (needed for perl 5.6)
sub runcmds($$$;@);
sub getmode();
sub multiarchivecmd($$$$$@);
sub singlearchivecmd($$$$$@);
sub maketarcmd($$$$);
sub cmdexec($@);
sub parsefmt($$);
sub makeoutdir();
sub explain($);
sub extract(@);
sub shquotemeta($);
sub tailslash($);
sub de($);
sub makespec(@);
sub backticks(@);
sub readconfig($$);
sub formatext($);
sub stripext($);
sub findformat($$);

# Configuration options and their built-in defaults
$::cfg_use_tar_j_option   = 1;              # does tar support -j?
$::cfg_use_tar_z_option   = 1;              # does tar support -z?
$::cfg_use_gzip_for_z     = 1;              # use gzip to decompress .Z files?
$::cfg_use_jar            = 0;              # use jar or zip for .jar archives?
$::cfg_use_file           = 1;              # use file(1) for unknown extensions?
$::cfg_path_pager         = 'pager';        # pager program
$::cfg_path_jar           = 'jar';          # jar program
$::cfg_path_tar           = 'tar';          # tar program
$::cfg_path_zip           = 'zip';          # zip program
$::cfg_path_unzip         = 'unzip';        # unzip program
$::cfg_path_gzip          = 'gzip';         # gzip program
$::cfg_path_bzip          = 'bzip';         # bzip program
$::cfg_path_bzip2         = 'bzip2';        # bzip2 program
$::cfg_path_compress      = 'ncompress';    # compress program
$::cfg_path_rar           = 'rar';          # rar program
$::cfg_path_file          = 'file';         # file program
$::cfg_tmpdir_name        = 'Unpack-%04d';  # extraction directory name
$::cfg_default_verbosity  = 1;              # default verbosity level
$::cfg_path_syscfg        = '/etc/atool.conf';	# system-wide configuration file
$::cfg_path_usercfg       = '.atoolrc';					# user configuration file

# Global variables
$::version = '0.11.0';
$::basename = File::Basename::basename($0);
@::rmdirs = ();

# Parse arguments
Getopt::Long::config('bundling');
Getopt::Long::GetOptions(
	'l|list'         => \$::opt_cmd_list,
	'x|extract'      => \$::opt_cmd_extract,
	'X|extract-to=s' => \$::opt_cmd_extract_to,
	'a|add'          => \$::opt_cmd_add,
	'c|cat'          => \$::opt_cmd_cat,
	'q|quiet'        => sub { $::opt_verbosity--; },
	'v|verbose'      => sub { $::opt_verbosity++; },
	'V|verbosity=i'  => \$::opt_verbosity,
	'config=s'       => \$::opt_config,
	'help'           => \$::opt_cmd_help,
	'version'        => \$::opt_cmd_version,
	'F|format=s'     => \$::opt_format,
	'f|force'        => \$::opt_force,
	'p|page'         => \$::opt_use_pager,
	'e|each'         => \$::opt_each,
	'E|explain'      => \$::opt_explain,
	'S|simulate'     => \$::opt_simulate,
) or exit 1;

# Display --version
if ($::opt_cmd_version) {
	print
"atool $::version\
Written by Oskar Liljeblad <osk\@hem.passagen.se>.\
\
Copyright (C) 2001 Oskar Liljeblad.\
This is free software; see the source for copying conditions.  There is NO\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
	exit;
}

# Display --help
if ($::opt_cmd_help) {
	print
"Usage: atool [OPTION]... ARCHIVE [FILE]...\
       atool -e [OPTION]... [ARCHIVE]...
Managing file archives of various types.\
\
Commands:\
  -l, --list              list files in archive (als)\
  -x, --extract           extract files from archive (aunpack)\
  -X, --extract-to=PATH   extract archive to current directory\
  -a, --add               create archive (apack)\
  -c, --cat               extract file to standard out (acat)\
      --help              display this help and exit\
      --version           output version information and exit\
\
Options:\
  -e, --each              execute command above for each file specified
  -F, --format=EXT        override archive format (see below)\
  -f, --force             allow overwriting of local files\
  -q, --quiet             decrease verbosity level by one\
  -v, --verbose           increase verbosity level by one\
  -V, --verbosity=LEVEL   specify verbosity (0, 1 or 2)\
  -p, --page              send output through pager\
  -E, --explain           explain what is being done by atool\
  -S, --simulate          simulation mode - no filesystem changes are made\
      --config=FILE       load configuration defaults from file\
\
Archive format (for --format) may be specified either as a\
file extension (\"tar.gz\") or as \"tar+gzip\".\
\
Report bugs to Oskar Liljeblad <osk\@hem.passagen.se>.\
";
	exit;
}

# Read configuration files
if (defined $::opt_config) {
	readconfig($::opt_config, 0);
} else {
	readconfig($::cfg_path_syscfg, 1);
	if ($::cfg_path_usercfg !~ /^\//) {
		readconfig(File::Spec->catfile($ENV{HOME}, $::cfg_path_usercfg), 1);
	} else {
		readconfig($::cfg_path_usercfg, 1);
	}
}

# Verify option integrity
$::opt_verbosity += $::cfg_default_verbosity;
if ($::opt_explain && $::opt_simulate) {
	die "$::basename: --explain and --simulate options are mutually exclusive\n";	#OK
}

my $mode = getmode();
if ($::opt_each) {
	if ($mode eq 'cat') {
		die "$::basename: --each can not be used with cat or add command\n";	#OK
	}
	if ($mode eq 'add') {
		if (!defined $::opt_format) {
			die "$::basename: specify a format with -F when using --each in add mode\n";
		}
		my $format = findformat($::opt_format, 1);
		for (my $c = 0; $c < @ARGV; $c++) {
			my $archive = File::Spec->canonpath($ARGV[$c]) . formatext($format);
			warn "$archive:\n" if $::opt_verbosity > 1;
			runcmds($mode, $archive, $c == $#ARGV, $ARGV[$c]);
		}
	} else {
		for (my $c = 0; $c < @ARGV; $c++) {
			warn "$ARGV[$c]:\n" if $::opt_verbosity > 1;
			runcmds($mode, $ARGV[$c], $c == $#ARGV);
		}
	}
} else {
	die "$::basename: missing archive argument\n" if ($#ARGV == -1);	#OK
	runcmds($mode, shift @ARGV, 1, @ARGV);
}

# runcmds:
# Execute an atool command.
# This is where it all happens.
sub runcmds($$$;@) {
	my ($mode, $archive, $exec, @args) = @_;

	my $format;
	if (defined $::opt_format) {
		$format = findformat($::opt_format, 1);
	} else {
		$format = findformat($archive, 0);
	}
	return if !defined $format;

	my @cmd;
	my $outdir;
	if ($format eq 'tar+bzip2') {
		if ($mode eq 'extract') {
			$outdir = makeoutdir();
			return if (!defined $outdir);
		}
		if ($::cfg_use_tar_j_option) {
			push @cmd, maketarcmd($archive, $outdir, $mode, 'jf'), @args;
		} else {
			push @cmd, $::cfg_path_bzip2, '-cd', $archive, ['|'] if $mode ne 'add';
			push @cmd, maketarcmd(undef, $outdir, $mode, ''), @args;
			push @cmd, ['|'], $::cfg_path_bzip2, ['>'], $archive if $mode eq 'add';
		}
		multiarchivecmd $archive, $outdir, $mode, 1, $exec, \@args, @cmd;
	}
	elsif ($format eq 'tar+gzip') {
		if ($mode eq 'extract') {
			$outdir = makeoutdir();
			return if (!defined $outdir);
		}
		if ($::cfg_use_tar_z_option) {
			push @cmd, maketarcmd($archive, $outdir, $mode, 'zf'), @args;
		} else {
			push @cmd, $::cfg_path_gzip, '-cd', $archive, ['|'] if $mode ne 'add';
			push @cmd, maketarcmd(undef, $outdir, $mode, ''), @args;
			push @cmd, ['|'], $::cfg_path_gzip, ['>'], $archive if $mode eq 'add';
		}
		multiarchivecmd $archive, $outdir, $mode, 1, $exec, \@args, @cmd;
	}
	elsif ($format eq 'tar+bzip') {
		if ($mode eq 'extract') {
			$outdir = makeoutdir();
			return if (!defined $outdir);
		}
		push @cmd, $::cfg_path_bzip, '-cd', $archive, ['|']	if $mode ne 'add';
		push @cmd, maketarcmd(undef, $outdir, $mode, ''), @args;
		push @cmd, ['|'], $::cfg_path_bzip, ['>'], $archive if $mode eq 'add';
		multiarchivecmd $archive, $outdir, $mode, 1, $exec, \@args, @cmd;
	}
	elsif ($format eq 'tar+compress') {
		if ($mode eq 'extract') {
			$outdir = makeoutdir();
			return if (!defined $outdir);
		}
		if ($::cfg_use_gzip_for_z) {
			push @cmd, $::cfg_path_gzip, '-cd', $archive, ['|']	if $mode ne 'add';
		} else {
			push @cmd, $::cfg_path_compress, '-cd', $archive, ['|']	if $mode ne 'add';
		}
		push @cmd, maketarcmd(undef, $outdir, $mode, ''), @args;
		push @cmd, ['|'], $::cfg_path_compress, ['>'], $archive if $mode eq 'add';
		multiarchivecmd $archive, $outdir, $mode, 1, $exec, \@args, @cmd;
	}
	elsif ($format eq 'tar') {
		if ($mode eq 'extract') {
			$outdir = makeoutdir();
			return if (!defined $outdir);
		}
		push @cmd, maketarcmd($archive, $outdir, $mode, 'f'), @args;
		multiarchivecmd $archive, $outdir, $mode, 1, $exec, \@args, @cmd;
	}
	elsif ($format eq 'jar' && $::cfg_use_jar) {
		if ($mode eq 'extract') {
			$outdir = makeoutdir();
			return if (!defined $outdir);
		}
		my $opts = '';
		if ($mode eq 'add') {
			warn "$::basename: $archive: $mode command not supported for $format archives\n";
			return;
		}
		$opts .= 'v' if $::opt_verbosity >= 1;
		push @cmd, $::cfg_path_jar;
		push @cmd, "x$opts", '-C', $outdir if $mode eq 'extract';
		push @cmd, "x$opts", '-C', $::opt_cmd_extract_to if $mode eq 'extract-to';
		push @cmd, "t$opts" if $mode eq 'list';
		push @cmd, "c$opts" if $mode eq 'add';
		push @cmd, $archive, @args;
		multiarchivecmd $archive, $outdir, $mode, 1, $exec, \@args, @cmd;
	}
	elsif ($format eq 'jar' || $format eq 'zip') {
		if ($mode eq 'extract') {
			$outdir = makeoutdir();
			return if (!defined $outdir);
		}
		if ($mode eq 'add') {
			push @cmd, $::cfg_path_zip, '-r';
		} else {
			push @cmd, $::cfg_path_unzip;
			push @cmd, '-p' if $mode eq 'cat';
			push @cmd, '-l' if $mode eq 'list';
			push @cmd, '-d', $outdir if $mode eq 'extract';
			push @cmd, '-d', $::opt_cmd_extract_to if $mode eq 'extract-to';
		}
		push @cmd, '-v' if $::opt_verbosity > 1;
		push @cmd, '-qq' if $::opt_verbosity < 0;
		push @cmd, '-q' if $::opt_verbosity == 0;
		push @cmd, $archive, @args;
		multiarchivecmd $archive, $outdir, $mode, 0, $exec, \@args, @cmd;
	}
	elsif ($format eq 'rar') {
		if ($mode eq 'extract') {
			$outdir = makeoutdir();
			return if (!defined $outdir);
		}
		push @cmd, $::cfg_path_rar;
		push @cmd, 'a' if $mode eq 'add';
		push @cmd, 'vt' if $mode eq 'list' && $::opt_verbosity >= 3;
		push @cmd, 'v' if $mode eq 'list' && $::opt_verbosity == 2;
		push @cmd, 'l' if $mode eq 'list' && $::opt_verbosity <= 1;
		push @cmd, 'x' if ($mode eq 'extract' || $mode eq 'extract-to');
		push @cmd, '-ierr', 'p' if $mode eq 'cat';
		push @cmd, $archive, @args;
		push @cmd, tailslash($outdir) if $mode eq 'extract';
		push @cmd, tailslash($::opt_cmd_extract_to) if $mode eq 'extract-to';
		multiarchivecmd $archive, $outdir, $mode, 0, $exec, \@args, @cmd;
	}
	elsif ($format eq 'bzip2') {
		singlearchivecmd $archive, $::cfg_path_bzip2, $format, $mode, $exec, @args;
	}
	elsif ($format eq 'bzip') {
		singlearchivecmd $archive, $::cfg_path_bzip, $format, $mode, $exec, @args;
	}
	elsif ($format eq 'gzip') {
		singlearchivecmd $archive, $::cfg_path_gzip, $format, $mode, $exec, @args;
	}
	elsif ($format eq 'compress') {
		if ($::cfg_use_gzip_for_z && $mode ne 'add') {
			singlearchivecmd $archive, $::cfg_path_gzip, $format, $mode, $exec, @args;
		} else {
			singlearchivecmd $archive, $::cfg_path_compress, $format, $mode, $exec, @args;
		}
	}
}

# de(value):
# Return 1 if value defined and is non-zero, 0 otherwise.
sub de($) {
	my ($value) = @_;
	return defined $value && $value ? 1 : 0;
}

# getmode()
# Identify the execution mode, and return it.
# Possible modes are 'cat', 'extract', 'list', 'add' or 'extract-to'.
sub getmode() {
	my $mode;
	if (de($::opt_cmd_list) + de($::opt_cmd_cat)
			+ de($::opt_cmd_extract) + de($::opt_cmd_add) 
			+ de($::opt_cmd_extract_to) > 1) {
		die "$::basename: only one command may be specified\n"; #OK
	}
	$mode = 'cat'           if ($::basename eq 'acat');
	$mode = 'extract'       if ($::basename eq 'aunpack');
	$mode = 'list'          if ($::basename eq 'als');
	$mode = 'add'           if ($::basename eq 'apack');
	$mode = 'add'		        if ($::opt_cmd_add);
	$mode = 'cat'		        if ($::opt_cmd_cat);
	$mode = 'list'			    if ($::opt_cmd_list);
	$mode = 'extract'	      if ($::opt_cmd_extract);
	$mode = 'extract-to'    if ($::opt_cmd_extract_to);
	if (!defined $mode) {
		die "$::basename: don't know what to do - no command specified\n"; #OK
	}
	return $mode;
}

# singlearchivecmd(archive, command, format, mode, exec, args)
# Execute a command for single-file archives.
# The command parameter specifies what command to execute.
sub singlearchivecmd($$$$$@) {
	my ($archive, $cmd, $format, $mode, $exec, @args) = @_;

	if ($mode eq 'add' && !$::opt_force && (-e $archive || -l $archive)) {
		warn "$::basename: $archive: refuse to overwrite existing file\n";
		return;
	}

	my $outdir = $::opt_cmd_extract_to;
	if ($mode eq 'extract-to' && -d $outdir) {
		my $base = File::Basename::basename($archive);
		$outdir = File::Spec->catfile($outdir, stripext($base));
	}

	if ($mode eq 'list') {
		warn "$::basename: $archive: $mode command not supported for $format archives\n";
		return;
	}
	my @cmd;
	push @cmd, $cmd;
	push @cmd, '-v'                       if $::opt_verbosity > 1;
	push @cmd, '-c'                       if $mode eq 'cat';
	push @cmd, '-c'                       if $mode eq 'extract-to';
	if ($mode eq 'add') {
 	 	push @cmd, '-c', @args, ['>'], $archive;
	} else {
	  push @cmd, '-d', $archive, @args;
	}
	push @cmd, ['>'], $outdir             if $mode eq 'extract-to';
	push @cmd, ['|'], $::cfg_path_pager   if $::opt_use_pager;
	cmdexec $exec, @cmd;
}

# maketarcmd(opts):
# Create (partial) command line arguments for a tar command.
# The parameter opts specifies additional arguments to add.
sub maketarcmd($$$$) {
	my ($archive, $outdir, $mode, $opts) = @_;
	$opts .= 'v' if $::opt_verbosity >= 1;
	my @cmd = ($::cfg_path_tar);
	push @cmd, "xO$opts" if $mode eq 'cat';
	push @cmd, "x$opts" if ($mode eq 'extract' || $mode eq 'extract-to');
	push @cmd, "t$opts" if $mode eq 'list';
	push @cmd, "c$opts" if $mode eq 'add';
	push @cmd, $archive if defined $archive;
	push @cmd, '-C', $outdir if $mode eq 'extract';
	push @cmd, '-C', $::opt_cmd_extract_to if $mode eq 'extract-to';
	return @cmd;
}

# cmdexec(exec, cmdspec)
# Execute a command specification.
# The cmdspec parameter is a list of string arguments building
# the command line. If there's a list reference instead of a
# string, it is a shell meta character/string which shouldn't
# be quoted.
sub cmdexec($@) {
	my ($exec, @cmd) = @_;
	my $spec = join(' ', map { ref $_ ? @{$_} : shquotemeta $_ } @cmd);
	my $rc;

	explain "$spec\n";
	if ($::opt_simulate) {
		return 1 if !$exec;
		exit;
	}

	if (grep(ref, @_)) {
		eval { local $^W = 0; exec $spec } if $exec;
		$rc = system $spec if !$exec;
	} else {
		eval { local $^W = 0; exec @cmd } if $exec;
		$rc = system @cmd if !$exec;
	}

	my $cmds = makespec(@cmd);
	if ($exec || $rc == -1) {
		warn "$::basename: cannot execute `$cmds': $!\n";
		return 0;
	}
	if ($rc & 0xFF != 0) {
		warn "$::basename: $cmds: abnormal exit\n";
		return 0;
	}
	if ($rc >> 8 != 0) {
		warn "$::basename: $cmds: non-zero return-code\n";
		return 0;
	}
	return 1;
}

# makespec(@)
# Make a command specification when printing errors.
sub makespec(@) {
	my (@cmd) = @_;
	my $spec = $cmd[0].' ..';
	my $lastref = 0;
	foreach (@cmd, '') {
		if ($lastref) {
			$spec .= " | $_ ..";
			$lastref = 0;
		}
		$lastref = 1 if (ref);
	}
	return $spec;
}

# makeoutdir()
# Make a temporary (unique) output directory for extraction command.
sub makeoutdir() {
	my $dir = '';
	do {
		$dir = sprintf $::cfg_tmpdir_name, int rand 10000;
	} while (-e $dir);

	if (!$::opt_simulate) {
		if (!mkdir($dir, 0700)) {
			warn "$::basename: cannot create directory `$dir': $!\n";
			return undef;
		}
	}
	push @::rmdirs, $dir;
	return $dir;
}

# explain($)
# Print on screen if $::opt_explain is true.
sub explain($) {
	my ($msg) = @_;
	print STDERR $msg if ($::opt_explain || $::opt_simulate);
}

# tailslash($)
# If specified file does not end with a slash,
# add one and return the new file.
sub tailslash($) {
	my ($file) = @_;
	return ($file =~ /\/$/ ? $file : "$file/");
}

# shquotemeta($)
# A more sophisticated quotemeta for bourne shells.
sub shquotemeta($) {
	my ($str) = @_;
	$str =~ s/([^A-Za-z0-9_.+,\/:=@%^-])/\\$1/g;
	return $str;
}

# multiarchivecmd(archive, outdir, mode, create, exec, argref, cmdspec)
# Execute a command for multi-file archives.
# The `create' argument controls whether the archive
# will be created (1) or just added to (0) if mode is "add".
sub multiarchivecmd($$$$$@) {
	my ($archive, $outdir, $mode, $create, $exec, $argref, @cmd) = @_;
	my @args = @{$argref};

	if (($mode eq 'cat' || $mode eq 'add') && $#args == -1) {
		die "$::basename: missing file argument\n";	#OK
	}
	if ($mode eq 'add' && $create && !$::opt_force && (-e $archive || -l $archive)) {
		warn "$::basename: $archive: refusing to overwrite existing file\n";
		return;
	}

	push @cmd, ['|'], $::cfg_path_pager if $::opt_use_pager;
	if ($mode ne 'extract') {
		cmdexec $exec, @cmd;
		return;
	}

 	return if (!cmdexec 0, @cmd);
	return if $::opt_simulate;

	if (!opendir DIR, $outdir) {
		warn "$::basename: $outdir: cannot list: $!\n";
		return;
	}
	my @files = grep !/^\.\.?$/, readdir DIR;
	closedir DIR;

	my $archivebase = File::Basename::basename($archive);
	my $reason;
	if ($#files == -1) {
		warn "$archivebase: archive is empty\n";
		rmdir $outdir;
		return;
	} elsif ($#files == 0) {
		my $fromfile = File::Spec->catfile($outdir, $files[0]);
		if ($::opt_force || (!-l $files[0] && !-e $files[0])) {
			if (!rename $fromfile, $files[0]) {
				warn "$fromfile: cannot rename: $!\n";
				return;
			}
			rmdir $outdir;
			return;
		}
		$reason = 'local file exists';
	} else {
		$reason = 'multiple root files';
	}

	my $localoutdir = stripext($archivebase);
	if (!-e $localoutdir) {
		if (!rename $outdir, $localoutdir) {
			warn "$outdir: cannot rename: $!\n";
			return;
		}
		$outdir = $localoutdir;
	}

	warn "$archivebase: extracted to `$outdir' ($reason)\n";
}

# stripext(file)
# Strip extension from the specified file.
sub stripext($) {
	my ($file) = @_;
	return $file if ($file =~ s/(\.tar\.bz2|\.tbz2)$//);
	return $file if ($file =~ s/(\.tar\.bz|\.tbz)$//);
	return $file if ($file =~ s/(\.tar\.gz|\.tgz)$//);
	return $file if ($file =~ s/(\.tar\.Z|\.tZ)$//);
	return $file if ($file =~ s/\.tar$//);
	return $file if ($file =~ s/\.bz2$//);
	return $file if ($file =~ s/\.bz$//);
	return $file if ($file =~ s/\.gz$//);
	return $file if ($file =~ s/\.zip$//);
	return $file if ($file =~ s/\.jar$//);
	return $file if ($file =~ s/\.war$//);
	return $file if ($file =~ s/\.Z$//);
	return $file if ($file =~ s/\.rar$//);
	return $file;
}

# formatext(format)
# Return the usual extension for the specified file format
sub formatext($) {
	my ($format) = @_;
	return '.tar.bz2'  if $format eq 'tar+bzip2';
	return '.tar.bz'   if $format eq 'tar+bzip';
	return '.tar.gz'   if $format eq 'tar+gzip';
	return '.tar.Z'    if $format eq 'tar+compress';
	return '.tar'      if $format eq 'tar';
	return '.bz2'      if $format eq 'bzip2';
	return '.bz'       if $format eq 'bzip';
	return '.gz'       if $format eq 'gzip';
	return '.zip'      if $format eq 'zip';
	return '.jar'      if $format eq 'jar';
	return '.Z'        if $format eq 'compress';
	return '.rar'      if $format eq 'rar';
	die "$::basename: $format: don't know file extension for format\n";
}

# findformat(spec, manual)
# Figure out format from specified file/string.
# If manual is 0, spec is a filename, otherwise
# it is a format description string.
sub findformat($$) {
	my ($file, $manual) = @_;
	my $spec = lc $file;
	if ($manual) {
		$spec =~ tr/+/./;
		$spec =~ s/^\.*/\./;
		$spec =~ s/bzip2/bz2/;
		$spec =~ s/bzip/bz/;
		$spec =~ s/gzip/gz/;
		$spec =~ s/compress/Z/;
	}
	return 'tar+bzip2'    if ($spec =~ /(\.tar\.bz2|\.tbz2)$/);
	return 'tar+bzip'     if ($spec =~ /(\.tar\.bz|\.tbz)$/);
	return 'tar+gzip'     if ($spec =~ /(\.tar\.gz|\.tgz)$/);
	return 'tar+compress' if ($spec =~ /(\.tar\.[zZ]|\.t[zZ])$/);
	return 'tar'          if ($spec =~ /\.tar$/);
	return 'bzip2'        if ($spec =~ /\.bz2$/);
	return 'bzip'         if ($spec =~ /\.bz$/);
	return 'gzip'         if ($spec =~ /\.gz$/);
	return 'zip'          if ($spec =~ /\.zip$/);
	return 'jar'          if ($spec =~ /\.(jar|war)$/);
	return 'compress'     if ($spec =~ /\.[zZ]$/);
	return 'rar'          if ($spec =~ /\.rar/);
	if (!$manual && $::cfg_use_file) {
		if (!open(TMP, $file)) {
			warn "$::basename: $file: $!\n";
			return;
		}
		close TMP;
		if (!-f $file) {
			warn "$::basename: $file: not a regular file\n";
			return;
		}
		if ($::opt_verbosity >= 1) {
			warn "$::basename: $file: format not known, identifying using file\n";
		}
		my @cmd = ($::cfg_path_file, '-b', '-L', '-z', '--', $file);
		$spec = backticks(@cmd);
		if (!defined $spec) {
			warn "$::basename: $::cfg_path_file: $!\n";
			return;
		}
		if ($? & 0xFF != 0) {
			warn "$::basename: $::cfg_path_file: abnormal exit\n";
			return;
		}
		if ($? >> 8 != 0) {
			warn "$::basename: $file: unknown file format\n";
			return;
		}
		chomp $spec;
		my $f;
		$f = 'tar+bzip2'    if (!$f && $spec =~ /^(GNU|POSIX) tar archive \(bzip2 compressed data(\W|$)/);
		$f = 'tar+gzip'     if (!$f && $spec =~ /^(GNU|POSIX) tar archive \(gzip compressed data(\W|$)/);
		$f = 'tar+bzip'     if (!$f && $spec =~ /^(GNU|POSIX) tar archive \(bzip compressed data(\W|$)/);
		$f = 'tar+compress' if (!$f && $spec =~ /^(GNU|POSIX) tar archive \(compress'd data(\W|$)/);
		$f = 'tar'        	if (!$f && $spec =~ /^(GNU|POSIX) tar archive(\W|$)/);
		$f = 'zip'        	if (!$f && $spec =~ /^Zip archive data(\W|$)/);
		$f = 'rar'        	if (!$f && $spec eq 'RAR archive data');
#		$f = 'bzip2'      	if (!$f && $spec =~ /^bzip2 compressed data(\W|$)/);
#		$f = 'bzip'       	if (!$f && $spec =~ /^bzip compressed data(\W|$)/);
#		$f = 'gzip'       	if (!$f && $spec =~ /^gzip compressed data(\W|$)/);
#		$f = 'compress'   	if (!$f && $spec =~ /^compress'd data(\W|$)/);
		$f = 'bzip2'      	if (!$f && $spec =~ / \(bzip2 compressed data(\W|$)/);
		$f = 'bzip'       	if (!$f && $spec =~ / \(bzip compressed data(\W|$)/);
		$f = 'gzip'					if (!$f && $spec =~ / \(gzip compressed data(\W|$)/);
		$f = 'compress'   	if (!$f && $spec =~ / \(compress'd data(\W|$)/);
		if ($f) {
			warn "$::basename: $file: format is `$f'\n" if $::opt_verbosity >= 1;
			return $f;
		}
		warn "$::basename: $file: unsupported file format `$spec'\n";
		return;
	}
	warn "$::basename: $file: unrecognized file format\n";
	return;
}

# backticks(cmdargs, ..)
# An implementation of the backtick (qx//) operator.
# The difference is that command STDERR output will still
# be printed on STDERR, and the shell isn't used to parse
# the command line.
sub backticks(@) {
  pipe(IN,OUT) || return;
  my $child = fork;
  return if !defined $child;
  if ($child == 0) {
    close IN || exit 1;
    close STDOUT || exit 1;
    open(STDOUT, '>&OUT') || exit 1;
    $SIG{__WARN__} = sub {};
    exec(@_) || exit 1;
  }
  close OUT;
  my $text = join('', <IN>);
  close IN;
  waitpid($child,0) == $child || return;
  return $text;
}

# readconfig(file)
# Read and parse the specified configuration file.
# If the file does not exist, just return.
# If there is an error in the configuration file,
# the program will be terminated. This could be a
# problem when there are errors in the system-wide
# configuration file.
sub readconfig($$) {
	my ($file, $failok) = @_;
	return if ($failok && !-e $file);
	open(FILE, $file) || die "$::basename: $file: $!\n";	#OK
	while (<FILE>) {
		chomp;
		next if /^\s*(#(.*))?$/;
		my ($var,$val) = /^(.*?)\s+([^\s].*)$/;
		my $varref = undef;
		($var eq 'use_tar_j_option')   && ($varref = \$::cfg_use_tar_j_option);
		($var eq 'use_tar_z_option')   && ($varref = \$::cfg_use_tar_z_option);
		($var eq 'cfg_use_gzip_for_z') && ($varref = \$::cfg_use_gzip_for_z);
		($var eq 'use_jar')            && ($varref = \$::cfg_use_jar);
		($var eq 'use_file')           && ($varref = \$::cfg_use_file);
		($var eq 'path_pager')         && ($varref = \$::cfg_path_pager);
		($var eq 'path_jar')           && ($varref = \$::cfg_path_jar);
		($var eq 'path_tar')           && ($varref = \$::cfg_path_tar);
		($var eq 'path_zip')           && ($varref = \$::cfg_path_zip);
		($var eq 'path_unzip')         && ($varref = \$::cfg_path_unzip);
		($var eq 'path_gzip')          && ($varref = \$::cfg_path_gzip);
		($var eq 'path_bzip')          && ($varref = \$::cfg_path_bzip);
		($var eq 'path_bzip2')         && ($varref = \$::cfg_path_bzip2);
		($var eq 'path_compress')      && ($varref = \$::cfg_path_compress);
		($var eq 'path_rar')           && ($varref = \$::cfg_path_rar);
		($var eq 'path_file')          && ($varref = \$::cfg_path_file);
		($var eq 'path_usercfg')       && ($varref = \$::cfg_path_usercfg);
		($var eq 'default_verbosity')  && ($varref = \$::cfg_default_verbosity);
		($var eq 'tmpdir_name')      && ($varref = \$::cfg_tmpdir_name);
		die "$::basename: $file: error in line $.\n" if (!defined $varref);	#OK
		${$varref} = $val;
	}
	close(FILE);
}

sub END {
	map (rmdir, @::rmdirs);	# Errors are ignored
}