-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreloc_perl
executable file
·48 lines (39 loc) · 1.07 KB
/
reloc_perl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd qw(abs_path);
use File::Basename qw(dirname);
my $dir = dirname abs_path $0;
my %SKIP = map { $_ => 1 } qw(. .. a2p extract_vba chartex);
# extract_vba and chartex are not UTF8
opendir my $dh, $dir or die "Could not open '$dir' $!";
while (my $entry = readdir $dh) {
next if $SKIP{$entry};
# TODO heck if the file is a binary file in a better way!
next if grep { /ELF/ } qx{file $dir/$entry};
eval {
my $content = read_file("$dir/$entry");
qx{chmod u+wx $dir/$entry};
write_file("$dir/$entry", "#!$dir/perl\n\n$content");
1;
} or do {
my $err = $@ // "Unknown error with '$entry'\n";
print $err;
};
#print "done $entry\n";
#<STDIN>;
}
sub read_file {
my ($filename) = @_;
open my $fh, '<:encoding(UTF-8)', $filename or die "Could not open '$filename' to read $!";
local $/ = undef;
my $cont = <$fh>;
close $fh;
return $cont;
}
sub write_file {
my ($filename, $content) = @_;
open my $fh, '>:encoding(UTF-8)', $filename or die "Could not open '$filename' to write $!";
print $fh $content;
close $fh;
}