#!/usr/bin/perl # perllocate - a perl replacement for GNU locate. This allows perl # regular expressions instead of shell globs. # Written by Ken Estes, Mail.com. use Getopt::Long; sub usage { my $usage =<; $/ = $old_irs; close(DBFILE)|| die("$0: Could not close: $filename. $!\n"); $FILE =~ m/^$DB_FILE_MAGIC/ || die("$0: file: $filename is not an GNU locatedb file. ". "No magic number found.\n"); } return ; } sub parse_database { my ($pattern) = @_; my $file_size = length($FILE); my $position = length($DB_FILE_MAGIC); my ( $new_prefix_size, $new_filename, $old_prefix_size, $old_filename, ) = (); while ($position < $file_size) { my ($offset, $suffix) = (); # read offset ($offset) = unpack("c", substr($FILE, $position, 1)); $position++; if ($offest == 0x80) { # offset is too large to store in one byte, the data we want is # in the next two bytes. ($offset) = unpack("n", substr($FILE, $position, 2)); $position += 2; } # read suffix { my $null_position = index ($FILE, "\0", $position); my $length = $null_position - $position; $suffix = substr($FILE, $position, $length); $position += $length + 1; } # new values depend on old values and the contents of the database. $new_prefix_size = $offset + $old_prefix_size; $new_filename = substr($old_filename, 0, $new_prefix_size) .$suffix; if ( $new_filename =~ m/$pattern/ ) { print "$new_filename\n"; } $old_prefix_size = $new_prefix_size; $old_filename = $new_filename; } return ; } # -------------- main -------------- { set_static_vars(); parse_args(); foreach $file ( split(/:/, $DB_PATH) ) { read_database($file); my $pattern = '('.join(')|(', @ARGV).')'; parse_database($pattern); } exit 0; }