From 1cc53d2b1d318f8df7a53025153b3918096897f4 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Thu, 13 Jul 2023 12:48:17 +0800 Subject: [PATCH 1/3] base64: fail early for directory argument * small pod update too --- bin/base64 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bin/base64 b/bin/base64 index 3a789d74..bdec6c71 100644 --- a/bin/base64 +++ b/bin/base64 @@ -1,11 +1,13 @@ #!/usr/bin/perl +=encoding utf8 + =begin metadata Name: base64 Description: encode and decode base64 data Author: Michael Mikonos -License: perl +License: artistic2 =end metadata @@ -38,6 +40,7 @@ if (defined $opt{'o'}) { $out = *STDOUT; } if (defined $ARGV[0] && $ARGV[0] ne '-') { + die "$0: '$ARGV[0]' is a directory\n" if (-d $ARGV[0]); unless (open $in, '<', $ARGV[0]) { die "$0: $ARGV[0]: $!\n"; } @@ -140,6 +143,4 @@ Written by Michael Mikonos. Copyright (c) 2023 Michael Mikonos. -This program is free software; you can redistribute it and/or modify it under -the same terms as Perl itself. - +This code is licensed under the Artistic License 2. From 44520991a53f292d310f53feac406706947f7b6e Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Thu, 13 Jul 2023 12:49:14 +0800 Subject: [PATCH 2/3] kill =pod --- bin/base64 | 2 -- 1 file changed, 2 deletions(-) diff --git a/bin/base64 b/bin/base64 index bdec6c71..d2cd9117 100644 --- a/bin/base64 +++ b/bin/base64 @@ -82,8 +82,6 @@ sub usage { __END__ -=pod - =head1 NAME base64 - encode and decode base64 data From 27cf8e27e86450040ed6d71ff18b7862517081a9 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Thu, 13 Jul 2023 20:03:15 +0800 Subject: [PATCH 3/3] style: die < warn+exit --- bin/base64 | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/bin/base64 b/bin/base64 index d2cd9117..9d4133e0 100644 --- a/bin/base64 +++ b/bin/base64 @@ -16,33 +16,45 @@ License: artistic2 use strict; use warnings; +use constant EX_SUCCESS => 0; +use constant EX_FAILURE => 1; + +use File::Basename qw(basename); use Getopt::Std qw(getopts); use MIME::Base64 qw(decode_base64 encode_base64); my $VERSION = '1.0'; +my $Program = basename($0); + my (%opt, $bufsz, $in, $out); getopts('do:v', \%opt) or usage(); if (scalar(@ARGV) > 1) { - warn "$0 too many arguments\n"; + warn "$Program: too many arguments\n"; usage(); } if ($opt{'v'}) { - die "$0 version $VERSION\n"; + print "$Program version $VERSION\n"; + exit EX_SUCCESS; } $bufsz = $opt{'d'} ? 76 : 57; $bufsz *= 20; if (defined $opt{'o'}) { unless (open $out, '>', $opt{'o'}) { - die "$0: $opt{o}: $!\n"; + warn "$Program: cannot open '$opt{o}': $!\n"; + exit EX_FAILURE; } } else { $out = *STDOUT; } if (defined $ARGV[0] && $ARGV[0] ne '-') { - die "$0: '$ARGV[0]' is a directory\n" if (-d $ARGV[0]); + if (-d $ARGV[0]) { + warn "$Program: '$ARGV[0]' is a directory\n"; + exit EX_FAILURE; + } unless (open $in, '<', $ARGV[0]) { - die "$0: $ARGV[0]: $!\n"; + warn "$Program: cannot open '$ARGV[0]': $!\n"; + exit EX_FAILURE; } } else { $in = *STDIN; @@ -51,12 +63,16 @@ if (defined $ARGV[0] && $ARGV[0] ne '-') { $opt{'d'} ? decode() : encode(); close $in; close $out; +exit EX_SUCCESS; sub decode { my $buf = ''; while (readline $in) { s/\s//g; - die("$0: bad input\n") if m/[^A-Za-z0-9\+\/\=]/; + if (m/[^A-Za-z0-9\+\/\=]/) { + warn "$Program: bad input\n"; + exit EX_FAILURE; + } $buf .= $_; if (length($buf) >= $bufsz) { my $chunk = substr($buf, 0, $bufsz); @@ -77,11 +93,14 @@ sub encode { } sub usage { - die "usage: $0 [-dv] [-o FILE] [FILE]\n"; + warn "usage: $Program [-dv] [-o FILE] [FILE]\n"; + exit EX_FAILURE; } __END__ +=pod + =head1 NAME base64 - encode and decode base64 data