Debian Codegraph @

suser/pesubst

raw
1 #!/usr/bin/perl
2 # SPDX-License-Identifier: MIT
3 #
4 # pesubst - replace strings in files
5 # written by Jan Engelhardt, 2004-2007
6
7 use Getopt::Long;
8 use strict;
9
10 my($p_dest, $p_src, $p_mod, $o_fit);
11 &Getopt::Long::Configure(qw(bundling pass_through));
12 &GetOptions(
13 "d=s" => \$p_dest,
14 "f" => \$o_fit,
15 "m=s" => \$p_mod,
16 "s=s" => \$p_src,
17 );
18
19 foreach my $file (@ARGV) {
20 if (!open(IN, "< $file")) {
21 warn "Could not open $file: $!\n";
22 next;
23 }
24
25 my $data = join("", <IN>);
26 close IN;
27
28 if ($o_fit) {
29 eval "\$data =~ s{$p_src}{".&fit_in($p_dest, length($p_src))."}g$p_mod";
30 } else {
31 eval "\$data =~ s{$p_src}{$p_dest}g$p_mod";
32 }
33
34 if (!open(OUT, "> $file")) {
35 warn "Could not reopen $file: $!\n";
36 next;
37 }
38
39 print OUT $data;
40 close OUT;
41 }
42
43 sub fit_in ()
44 {
45 my($str, $len) = @_;
46 $str = substr($str, 0, $len);
47 $str .= "\\x00" x ($len - length($str));
48 return $str;
49 }