#!/usr/local/bin/perl # # unchc - UN Cern Httpd Cache. # Skip header of CERN httpd cache file, and write data to stdout. # # Author: Tetsuya INOUE # # # Algorithm: ファイルの先頭から1行づつ、空行まで読み込む。 # それより後をデータとみなして、1 byte ごとに読み込んでから # stdout に書き込む。 # 空行は、UNIX(LF, 0x0A) と MSDOS(CR+LF, 0x0D0A) を識別する。 # もし、MAC(CR, 0x0D)の形式が存在するなら御連絡下さい。 # # # このプログラムは非常にいいかげんなものです。おかしい、気に入らない、 # 使えないなどがありましたら、意を汲んで修正して下さい。 # sub pr_help { print "unchc : UN Cern Httpd Cache.¥n"; print " Skip header of CERN httpd cache file, ", "and write data to stdout.¥n"; print "usage : unchc ¥n"; } $skiped = 0; if ($#ARGV == -1 && ! -t){ # コマンドライン引数が無く、stdin が tty でなければ、stdin から読み込む # cat foo | unchc : read stdin(foo) # unchc < foo : read stdin(foo) $_ = ; if (! /^HTTP/) { print STDERR "Probably, this is not CERN httpd cache file.¥n"; exit 1; } while ($skiped == 0) { $_ = ; if (($_ eq "¥n") || ($_ eq "¥r¥n")) { $skiped = 1; } } while(read(STDIN, $buf, 1)){ print STDOUT $buf; } exit 0; }elsif($#ARGV == 0){ # コマンドライン引数が1つの場合 # unchc -h : print help message and exit # unchc -help : print help message and exit # unchc foo : read foo # unchc - : read stdin if((@ARGV[0] eq "-h") || (@ARGV[0] eq "-help")){ &pr_help(); exit 1; }elsif ((@ARGV[0] eq "-") && ! -t){ $_ = ; if (! /^HTTP/) { print STDERR "Probably, this is not CERN httpd cache file.¥n"; exit 1; } while ($skiped == 0) { $_ = ; if (($_ eq "¥n") || ($_ eq "¥r¥n")) { $skiped = 1; } } while(read(STDIN, $buf, 1)){ print STDOUT $buf; } }elsif (-e @ARGV[0]){ $fname = shift(@ARGV); open(FILE, $fname) || die "Can't open $fname: $!¥n"; $_ = ; if (! /^HTTP/) { print STDERR "Probably, this is not CERN httpd cache file.¥n"; exit 1; } while ($skiped == 0) { $_ = ; if (($_ eq "¥n") || ($_ eq "¥r¥n")) { $skiped = 1; } } while(read(FILE, $buf, 1)){ print STDOUT $buf; } }else{ &pr_help(); exit 1; } exit 0; }else{ # コマンドライン引数が2以上の場合 エラー &pr_help(); exit 1; } # not reached &pr_help(); exit 1; # # end of script. #