Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

A better (?) uudecoder.

by mephit (Scribe)
on Apr 27, 2002 at 03:16 UTC ( [id://162443]=sourcecode: print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info Mephit a.k.a. J. Kufrovich eggie@sunlink.net
Description: "Better" than the uudecode that comes with Linux, anyway. This script that will take a text file filled with uuencoded data (even multiple files, and with non-data as well, as if saved from Usenet) and uudecode each file found. Useful for me, anyway. Uses Convert::UU. Comments or questions? Leave a message at the beep.
#!/usr/bin/perl -w

# Thanks to folks on comp.lang.perl.misc for a few tips.

use strict;
use Convert::UU qw/uudecode/;

die "Filename required\n" unless $ARGV[0];
my $file = shift;
print "Parsing $file ...\n\n";
open (FILE, "<", $file) or die "Can't open file for reading:  $!\n";

my ($data, $tempfile, $tempmode);

MAIN: while (<FILE>) {
  next MAIN unless /^begin / .. /^end\s+/;
 INNER: {
    # extract the filename.  If we already have a filename, it's a pos
+sible short file in the data file, so reset everything. Could all of 
+this be done better somehow?
    if ( /^begin / ) { 
      if ($tempfile) {
    print "Warning: possible short file $tempfile found\n\n";
    $data = '';
      } # if ($tempfile)
      ($tempmode, $tempfile) = ( /\Abegin (\d+) (.*)$/m);      
      last INNER;
    } # if ( /^begin/ )
    last INNER if /^end\s+/;     # Last line of data.
    next MAIN if /[a-z]/;        # Skip non-data lines
    next MAIN if /^\s+$/;        # And again
  } #end of INNER

  $data .= $_;

  if ( /^end\s+/ ) {
    my $filename = $tempfile;
    my $mode = $tempmode;
    print "Found uuencoded file $filename\n";

    my $outstr = uudecode($data);
    if (-e $filename) {
      print "Error, $filename already exists.  Ignoring\n\n";
      $data = '';
      $tempfile = '';
      $filename = '';
      next MAIN;
    }
    open (OUTFILE, ">", $filename) 
      or die "Can't open $filename for writing:  $!\n";
    print "Writing to $filename ...\n\n";
    print OUTFILE $outstr;
    close OUTFILE;

    system ("chmod", $mode, $filename) 
      and warn "Unable to change perms of $filename:  $!\n";

    $data = '';
    $tempfile = '';
    $filename = '';
  }
}
close (FILE)

*BEEEP*

Code reposted on 4-27-02 so it doesn't appear to be double-spaced when viewed without code wrapping (I hope).

Bah! Updated again on 5-13-02, this time in Netscape, which hopefully won't insert extra ^Ms, like Opera does, apparently.

Replies are listed 'Best First'.
Re: A better (?) uudecoder.
by Matts (Deacon) on Apr 27, 2002 at 08:25 UTC
    Did you know perl can natively uudecode using pack()? No need for an extra module then.
      Yeah, I know, but why re-invent the wheel? The main reason I wrote this was to have something that could handle a single file that contains (possibly) more than one uuencoded file, and possibly News headers, which uudecode (1) doesn't handle all that well. uudecode (1) will only decode the first chunk of data that it finds in a text file, and only if it's in one piece (if there's header info in the middle, in the case of a file that was split before being posted, forget about it). This script works around that. Anyway, it's useful to me; YMMV.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://162443]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found