Something that comes up from time to time is the question of whether
to import bzip2 into base or not. Turns out the question is moot
because already have imported it. There's a copy in perl. (I didn't
know this until I happened to be watching a build closer than usual.)

Since we already have the code built, why not let people use it? This
is a small perl script that implements the 90% subset of functionality
people expect from the regular bzip2 and bunzip2 utilities. It's not
really complete or perfect, but I haven't spent all that much time on
it.

--- /dev/null   Wed Jun  5 20:54:56 2013
+++ bzip2/Makefile      Wed Jun  5 20:51:51 2013
@@ -0,0 +1,15 @@
+# $OpenBSD$
+
+.include <bsd.own.mk>
+
+MAN=
+
+SCRIPT=bzip2.pl
+LINKS=${BINDIR}/bzip2 ${BINDIR}/bunzip2
+
+realinstall:
+       ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
+           ${.CURDIR}/${SCRIPT} ${DESTDIR}${BINDIR}/bzip2
+
+
+.include <bsd.prog.mk>
--- /dev/null   Wed Jun  5 20:55:02 2013
+++ bzip2/bzip2.pl      Wed Jun  5 20:53:23 2013
@@ -0,0 +1,113 @@
+#!/usr/bin/perl -w
+# $OpenBSD$
+# Copyright (c) Ted Unangst <t...@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+use strict;
+use Getopt::Long qw(:config posix_default bundling no_ignore_case);
+use IO::Compress::Bzip2 qw(bzip2);
+
+
+my $usestdout;
+my $level = 9;
+my $reqlevel; # requested level, who cares?
+GetOptions(
+       "c|stdout" => \$usestdout,
+       "1|2|3|4|5|6|7|8|9|fast|best" => \$reqlevel
+);
+
+sub bzipfile {
+       my $fname = shift;
+       my $ofname;
+       my $in;
+       if ($fname ne "-") {
+               if ($usestdout) {
+                       $ofname = "-";
+               } else {
+                       $ofname = $fname . ".bz2";
+                       if (-e $ofname) {
+                               warn "Output file $ofname already exists.\n";
+                               return;
+                       }
+               }
+               $in = new IO::File "<$fname";
+               if (!$in) {
+                       warn "Can't open input file $fname.\n";
+                       return;
+               }
+       } else {
+               $in = "-";
+               $ofname = "-";
+       }
+       if ($ofname eq "-" && -t STDOUT) {
+               warn "I won't write compressed data to a terminal.\n";
+               return;
+       }
+       if (not bzip2 $in => $ofname, BlockSize100K => $level) {
+               warn "failz";
+               return;
+       }
+       if ($ofname ne "-") {
+               unlink($fname);
+       }
+}
+
+sub bunzipfile {
+print "bunzipping\n";
+       my $fname = shift;
+       my $ofname;
+       my $in;
+       if ($fname ne "-") {
+               if ($usestdout) {
+                       $ofname = "-";
+               } else {
+                       $ofname = $fname;
+                       $ofname =~ s/\.bz2$//;
+                       if (-e $ofname) {
+                               warn "Output file $ofname already exists.\n";
+                               return;
+                       }
+               }
+               $in = new IO::File "<$fname";
+               if (!$in) {
+                       warn "Can't open input file $fname.\n";
+                       return;
+               }
+       } else {
+               $in = "-";
+               $ofname = "-";
+       }
+       if (not bunzip2 $in => $ofname) {
+               warn "failz";
+               return;
+       }
+       if ($ofname ne "-") {
+               unlink($fname);
+       }
+}
+
+my $main;
+if ($0 =~ /bunzip2$/) {
+       $main = \&bunzipfile;
+} else {
+       $main = \&bzipfile;
+}
+
+if (@ARGV == 0) {
+       &$main("-");
+} else {
+       foreach my $f (@ARGV) {
+               &$main($f);
+       }
+}

Reply via email to