* find file or directory that name contains non sjis character
[lab.git] / misc / findnonsjis.pl
1 #! /usr/bin/perl -w
2
3 use strict;
4 use warnings;
5 use utf8;
6 use encoding 'utf-8';
7
8 use Unicode::Japanese qw(unijp);
9
10 my $top = shift @ARGV || exit;
11 if (! -d $top) { exit; }
12
13 checkdir($top);
14
15 sub match {
16     my $str = shift @_;
17     
18     my $sjis = unijp($str)->sjis;
19     if ($sjis =~ /&#\d{4,};/) {
20         return 1;
21     }
22     
23     return '';
24 }
25
26 sub checkdir {
27     my $target = shift @_;
28     
29     print STDERR "checking '$target'\n";
30     opendir(my $dir, $target) || return $target;
31     my @entries = sort readdir($dir);
32     closedir($dir);
33     
34     my @dirs;
35     while (my $entry = shift @entries) {
36         next if ($entry =~ /^\.+$/);
37         if (match($entry)) {
38             print "'$target/$entry' contains non Shift_JIS character\n";
39             next;
40         }
41         if (-d "$target/$entry") {
42             push @dirs, $entry;
43             next;
44         }
45     }
46     
47     while (my $entry = pop @dirs) {
48         checkdir("$target/$entry");
49     }
50 }