Debian Codegraph @

fixin

raw
1 #!/usr/bin/perl
2
3 # Usage: fixin [-s] [files]
4
5 # Configuration constants.
6
7 $does_shbang = 1; # Does kernel recognize #! hack?
8 $verbose = 1; # Default to verbose
9
10 # Construct list of directories to search.
11
12 @absdirs = reverse grep(m!^/!, split(/:/, $ENV{'PATH'}, 999));
13
14 # Process command line arguments.
15
16 if ($ARGV[0] eq '-s') {
17 shift;
18 $verbose = 0;
19 }
20
21 die "Usage: fixin [-s] [files]\n" unless @ARGV || !-t;
22
23 @ARGV = '-' unless @ARGV;
24
25 # Now do each file.
26
27 FILE: foreach $filename (@ARGV) {
28 open(IN, $filename) ||
29 ((warn "Can't process $filename: $!\n"), next);
30 $_ = <IN>;
31 next FILE unless /^#!/; # Not a shbang file.
32
33 # Now figure out the interpreter name.
34
35 chop($cmd = $_);
36 $cmd =~ s/^#! *//;
37 ($cmd,$arg) = split(' ', $cmd, 2);
38 $cmd =~ s!^.*/!!;
39
40 # Now look (in reverse) for interpreter in absolute PATH.
41
42 $found = '';
43 foreach $dir (@absdirs) {
44 if (-x "$dir/$cmd") {
45 warn " Ignoring $found\n" if $verbose && $found;
46 $found = "$dir/$cmd";
47 }
48 }
49
50 # Figure out how to invoke interpreter on this machine.
51
52 if ($found) {
53 warn " Changing $filename to $found\n" if $verbose;
54 if ($does_shbang) {
55 $_ = "#!$found";
56 $_ .= ' ' . $arg if $arg ne '';
57 $_ .= "\n";
58 }
59 else {
60 $_ = <<EOF;
61 :
62 eval 'exec $found $arg -S \$0 \${1+"\$@"}'
63 if \$running_under_some_shell;
64 EOF
65 }
66 }
67 else {
68 warn "Can't find $cmd in PATH, $filename unchanged\n"
69 if $verbose;
70 next FILE;
71 }
72
73 # Make new file if necessary.
74
75 if ($filename eq '-') {
76 select(STDOUT);
77 }
78 else {
79 rename($filename, "$filename.bak")
80 || ((warn "Can't modify $filename"), next FILE);
81 open(OUT,">$filename")
82 || die "Can't create new $filename: $!\n";
83 ($dev,$ino,$mode) = stat IN;
84 $mode = 0755 unless $dev;
85 chmod $mode, $filename;
86 select(OUT);
87 }
88
89 # Print out the new #! line (or equivalent).
90
91 print;
92
93 # Copy the rest of the file.
94
95 while (<IN>) {
96 print;
97 }
98 close IN;
99 close OUT;
100 }