Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

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