4 BEGIN { pop @INC if $INC[-1] eq '.' }
10 # Allow historic support for dashless bundled options
12 # is valid (GNU) tar style
13 @ARGV && $ARGV[0] =~ m/^[DdcvzthxIC]+[fT]?$/ and
14 unshift @ARGV, map { "-$_" } split m// => shift @ARGV;
16 getopts('Ddcvzthxf:ICT:', $opts) or die usage();
18 ### show the help message ###
19 die usage() if $opts->{h};
21 ### enable debugging (undocumented feature)
22 local $Archive::Tar::DEBUG = 1 if $opts->{d};
24 ### enable insecure extracting.
25 local $Archive::Tar::INSECURE_EXTRACT_MODE = 1 if $opts->{I};
28 unless ( 1 == grep { defined $opts->{$_} } qw[x t c] ) {
29 die "You need exactly one of 'x', 't' or 'c' options: " . usage();
32 my $compress = $opts->{z} ? 1 : 0;
33 my $verbose = $opts->{v} ? 1 : 0;
34 my $file = $opts->{f} ? $opts->{f} : 'default.tar';
35 my $tar = Archive::Tar->new();
41 if( $opts->{T} eq "-" ) {
42 chomp( @src = <STDIN> );
43 } elsif( open my $fh, "<", $opts->{T} ) {
44 chomp( @src = <$fh> );
46 die "$0: $opts->{T}: $!\n";
50 find( sub { push @files, $File::Find::name;
51 print $File::Find::name.$/ if $verbose }, @src );
55 $file = IO::Handle->new();
56 $file->fdopen(fileno(STDOUT),"w");
59 my $tar = Archive::Tar->new;
60 $tar->add_files(@files);
62 for my $f ($tar->get_files) {
63 $f->mode($f->mode & ~022); # chmod go-w
66 $tar->write($file, $compress);
70 $file = IO::Handle->new();
71 $file->fdopen(fileno(STDIN),"r");
74 ### print the files we're finding?
75 my $print = $verbose || $opts->{'t'} || 0;
77 my $iter = Archive::Tar->iter( $file );
79 while( my $f = $iter->() ) {
80 print $f->full_path . $/ if $print;
82 ### data dumper output
83 print Dumper( $f ) if $opts->{'D'};
86 $f->extract if $opts->{'x'};
90 ### pod & usage in one
92 my $usage .= << '=cut';
97 ptar - a tar-like program written in perl
101 ptar is a small, tar look-alike program that uses the perl module
102 Archive::Tar to extract, create and list tar archives.
106 ptar -c [-v] [-z] [-C] [-f ARCHIVE_FILE | -] FILE FILE ...
107 ptar -c [-v] [-z] [-C] [-T index | -] [-f ARCHIVE_FILE | -]
108 ptar -x [-v] [-z] [-f ARCHIVE_FILE | -]
109 ptar -t [-z] [-f ARCHIVE_FILE | -]
114 c Create ARCHIVE_FILE or STDOUT (-) from FILE
115 x Extract from ARCHIVE_FILE or STDIN (-)
116 t List the contents of ARCHIVE_FILE or STDIN (-)
117 f Name of the ARCHIVE_FILE to use. Default is './default.tar'
118 z Read/Write zlib compressed ARCHIVE_FILE (not always available)
119 v Print filenames as they are added or extracted from ARCHIVE_FILE
120 h Prints this help message
121 C CPAN mode - drop 022 from permissions
122 T get names to create from file
126 tar(1), L<Archive::Tar>.
130 ### strip the pod directives
131 $usage =~ s/=pod\n//g;
132 $usage =~ s/=head1 //g;
134 ### add some newlines