mimedecode.pl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #! /usr/bin/perl
  2. # Copyright (C) 2007 Alex Schroeder <alex@emacswiki.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. =head1 mimedecode.pl
  17. This script opens all files given as parameters on the command line
  18. and destructively converts them to binary files.
  19. Something similar could be achieved using command line tools.
  20. Unfortunately, they are not always available. If Perl's MIME library
  21. is available instead, you can use this script.
  22. This particular version preserves the file's mtime in order to play
  23. nice with raw.pl which relies on mtime.
  24. =cut
  25. use MIME::Base64;
  26. local $/;
  27. while (<>) {
  28. close ARGV;
  29. if (substr($_,0,6) eq '#FILE ') {
  30. print "decoding $ARGV\n";
  31. my $ts = (stat($ARGV))[9];
  32. s/^.*\n//;
  33. my $bytes = decode_base64($_);
  34. open(F, "> $ARGV");
  35. print F $bytes;
  36. close F;
  37. # restore mtime to collaborate with raw.pl
  38. utime $ts, $ts, $ARGV;
  39. }
  40. }