File: filenameconv

package info (click to toggle)
hxtools 20201116-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,696 kB
  • sloc: ansic: 5,074; perl: 3,589; cpp: 2,152; sh: 1,610; asm: 173; makefile: 153
file content (78 lines) | stat: -rwxr-xr-x 1,447 bytes parent folder | download | duplicates (2)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl
#
#	filenameconv -
#	convert filenames on the whole filesystem between two charsets.
#	written by Jan Engelhardt, 2004-2007
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the WTF Public License version 2 or
#	(at your option) any later version.
#

use Cwd;
use Getopt::Long;
use IPC::Open2;
use strict;

my($FROM, $TO, $YES, $Depth);
&Getopt::Long::Configure(qw(bundling pass_through));
&GetOptions("f=s" => \$FROM, "t=s" => \$TO, "Y" => \$YES);

if ($FROM eq "" || $TO eq "") {
	die "You need to specify both -f and -t\n";
}

$SIG{CHLD} = \&REAPER;
&convert(@ARGV);

sub REAPER ()
{
	wait();
	$SIG{CHLD} = \&REAPER;
	return;
}

sub convert ()
{
	local(*RD, *WD);

	foreach my $src (@_) {
		my $pid = open2(\*RD, \*WD, "iconv", "-f", $FROM, "-t", $TO);
		if (!defined($pid)) {
			print STDERR "Error starting conversion program!\n";
			exit 1;
		}

		print WD $src;
		close WD;

		my $dest = join("", <RD>);
		if (!-l $src && -d _) {
			&dive($src);
		}
		if ($src eq $dest) {
			next;
		}
		if ($YES) {
			rename $src, $dest;
		}
		print "\tProcessing \"$src\" => \"$dest\"\n";
	}
	return;
}

sub dive ()
{
	my $dir = shift @_;
	my $prev = getcwd();

	++$Depth;
	print "[$Depth] Entering \"$prev/$dir\"\n";
	chdir($dir) || return undef;
	&convert(glob("*"));
	chdir("..") || die "Got a problem chdir'ing back\n";

	print "[$Depth] Leaving \"$prev/$dir\"\n";
	--$Depth;
	return;
}