summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2000-04-29 20:40:05 +0000
committerMarc Espie <espie@cvs.openbsd.org>2000-04-29 20:40:05 +0000
commitc6945d5a4cf3042ef1fa1a134187cf38ad000d93 (patch)
tree9fa6113b5227eb323d9bbc9c0e80d571690cefbc
parentb5980d1d773b738066002225274d148d6e9534bd (diff)
Use perl 5.6.0 features:
- don't use IO::File, autovivify handles instead, - use open 3 args mode for security, - pipe return code is now useful, so use it. Don't install the whatis.db file directly, instead put it in a tmp location, and copy it only if it changed: this avoids spurious errors on ro /usr.
-rw-r--r--libexec/makewhatis/makewhatis.pl42
1 files changed, 30 insertions, 12 deletions
diff --git a/libexec/makewhatis/makewhatis.pl b/libexec/makewhatis/makewhatis.pl
index 78bb8c1a452..4b52bed4990 100644
--- a/libexec/makewhatis/makewhatis.pl
+++ b/libexec/makewhatis/makewhatis.pl
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
# ex:ts=8 sw=4:
-# $OpenBSD: makewhatis.pl,v 1.8 2000/04/26 15:44:18 espie Exp $
+# $OpenBSD: makewhatis.pl,v 1.9 2000/04/29 20:40:04 espie Exp $
#
# Copyright (c) 2000 Marc Espie.
#
@@ -26,9 +26,12 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+require 5.6.0;
+
use strict;
use File::Find;
-use IO::File;
+use File::Temp qw/tempfile/;
+use File::Compare;
use Getopt::Std;
my ($picky, $testmode);
@@ -37,25 +40,38 @@ my ($picky, $testmode);
#
# write $list to file named $file, removing duplicate entries.
# Change $file mode/owners to expected values
+# Write to temporary file first, and do the copy only if changes happened.
#
sub write_uniques
{
my $list = shift;
my $f = shift;
- my ($out, $last);
local $_;
- $out = new IO::File $f, "w" or die "$0: Can't open $f";
+ my ($out, $tempname);
+ ($out, $tempname) = tempfile() or die "$0: Can't open temporary file";
my @sorted = sort @$list;
+ my $last;
while ($_ = shift @sorted) {
print $out $_, "\n" unless defined $last and $_ eq $last;
$last = $_;
}
close $out;
- chmod 0444, $f;
- chown 0, (getgrnam 'bin')[2], $f;
+ if (compare($tempname, $f) == 0) {
+ unlink($tempname);
+ } else {
+ use File::Copy;
+
+ if (move($tempname, $f)) {
+ chmod 0444, $f;
+ chown 0, (getgrnam 'bin')[2], $f;
+ } else {
+ print STDERR "$0: Can't create $f ($!), temporary result is in $tempname\n";
+ exit 1;
+ }
+ }
}
sub found
@@ -388,10 +404,13 @@ sub scan_manpages
for (@$list) {
my ($file, $subjects);
if (m/\.(?:Z|gz)$/) {
- $file = new IO::File "gzip -fdc $_|";
+ unless (open $file, '-|', "gzip -fdc $_") {
+ warn "$0: Can't decompress $_\n";
+ next;
+ }
$_ = $`;
} else {
- unless ($file = new IO::File $_) {
+ unless (open $file, '<', $_) {
warn "$0: Can't read $_\n";
next;
}
@@ -450,22 +469,21 @@ if (defined $opts{'d'}) {
chdir $mandir;
my $whatis = "$mandir/whatis.db";
- my $old = new IO::File $whatis or
+ open(my $old, '<', $whatis) or
die "$0 $whatis to merge with";
my $subjects = scan_manpages(\@ARGV);
while (<$old>) {
chomp;
push(@$subjects, $_);
}
- close $old;
+ close($old);
write_uniques($subjects, $whatis);
exit 0;
}
if ($#ARGV == -1) {
local $_;
@ARGV=();
- my $conf;
- $conf = new IO::File '/etc/man.conf' or
+ open(my $conf, '<', '/etc/man.conf') or
die "$0: Can't open /etc/man.conf";
while (<$conf>) {
chomp;