package Tar;

use Cwd;

my $cwd = getcwd();

sub untar {
    my( $file, $dir ) = @_;

    my $pid = fork();
    if( $pid ) {
	waitpid( $pid, 0 );
	return( ! $? );
    }
    elsif( defined $pid ) {
	if( $dir ) {
	    unless( $file =~ /^(\/|\\|[a-z]:)/i ) {
		$file = (getcwd())."/$file";
	    }
	    chdir( $dir ) || exit( 1 );
	}
	eval {
	    require Archive::Tar;
	    $tar = new Archive::Tar( $file );
	    # this is an Archive::Tar bug: extract() without arguments does not work correctly
	    $tar->extract( $tar->list_files() );
	    exit(0);
	};
	exec( "tar", "xf", $file );
	exit 0;
    }
    else {
	return 0;
    }
}

1;

