File: filenameconv

package info (click to toggle)
hxtools 20251011-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,468 kB
  • sloc: ansic: 4,384; perl: 3,467; sh: 1,664; cpp: 353; makefile: 90
file content (75 lines) | stat: -rwxr-xr-x 1,356 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2004-2007 Jan Engelhardt
#
#	filenameconv -
#	convert filenames on the whole filesystem between two charsets.
#	written by Jan Engelhardt, 2004-2007

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;
}