xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/look.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate;# Usage: &look(*FILEHANDLE,$key,$dict,$fold)
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# This library is no longer being maintained, and is included for backward
4*0Sstevel@tonic-gate# compatibility with Perl 4 programs which may require it.
5*0Sstevel@tonic-gate#
6*0Sstevel@tonic-gate# In particular, this should not be used as an example of modern Perl
7*0Sstevel@tonic-gate# programming techniques.
8*0Sstevel@tonic-gate#
9*0Sstevel@tonic-gate;# Sets file position in FILEHANDLE to be first line greater than or equal
10*0Sstevel@tonic-gate;# (stringwise) to $key.  Pass flags for dictionary order and case folding.
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gatesub look {
13*0Sstevel@tonic-gate    local(*FH,$key,$dict,$fold) = @_;
14*0Sstevel@tonic-gate    local($max,$min,$mid,$_);
15*0Sstevel@tonic-gate    local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
16*0Sstevel@tonic-gate       $blksize,$blocks) = stat(FH);
17*0Sstevel@tonic-gate    $blksize = 8192 unless $blksize;
18*0Sstevel@tonic-gate    $key =~ s/[^\w\s]//g if $dict;
19*0Sstevel@tonic-gate    $key = lc $key if $fold;
20*0Sstevel@tonic-gate    $max = int($size / $blksize);
21*0Sstevel@tonic-gate    while ($max - $min > 1) {
22*0Sstevel@tonic-gate	$mid = int(($max + $min) / 2);
23*0Sstevel@tonic-gate	seek(FH,$mid * $blksize,0);
24*0Sstevel@tonic-gate	$_ = <FH> if $mid;		# probably a partial line
25*0Sstevel@tonic-gate	$_ = <FH>;
26*0Sstevel@tonic-gate	chop;
27*0Sstevel@tonic-gate	s/[^\w\s]//g if $dict;
28*0Sstevel@tonic-gate	$_ = lc $_ if $fold;
29*0Sstevel@tonic-gate	if ($_ lt $key) {
30*0Sstevel@tonic-gate	    $min = $mid;
31*0Sstevel@tonic-gate	}
32*0Sstevel@tonic-gate	else {
33*0Sstevel@tonic-gate	    $max = $mid;
34*0Sstevel@tonic-gate	}
35*0Sstevel@tonic-gate    }
36*0Sstevel@tonic-gate    $min *= $blksize;
37*0Sstevel@tonic-gate    seek(FH,$min,0);
38*0Sstevel@tonic-gate    <FH> if $min;
39*0Sstevel@tonic-gate    while (<FH>) {
40*0Sstevel@tonic-gate	chop;
41*0Sstevel@tonic-gate	s/[^\w\s]//g if $dict;
42*0Sstevel@tonic-gate	$_ = lc $_ if $fold;
43*0Sstevel@tonic-gate	last if $_ ge $key;
44*0Sstevel@tonic-gate	$min = tell(FH);
45*0Sstevel@tonic-gate    }
46*0Sstevel@tonic-gate    seek(FH,$min,0);
47*0Sstevel@tonic-gate    $min;
48*0Sstevel@tonic-gate}
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate1;
51