Read a file and search for a pattern.

This is a demo program, to show how we can search through a file using mmap or regular reads. The mmap version is actually so inefficient that it works slower than regular reading. Efficiently wasn't the point here, though, so fixing it is left for later.

Output is a list of offsets into the file, where you can find the searched for text. You can make sure the search gave you the right offset with dd. E.g.
dd if=somefile.txt skip={offset} ibs=1 count=9
(Note: 9 is length of "some text"; {offset} is the number generated by this program.) dd will print out the text from the file. It should be same as the text you searched for.

Note: if you search in a device file, such as /dev/hda1, you will be asked for size of the file, as the file doesn't have a "size."

Usage: ./finder somefile.txt 'some text'

 Copyright 2005, Troy Korjuslommi, aka TJK.
 Latest copy available from

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License,
 version 2, as published by the Free Software Foundation.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,

 Any express or implied warranties, including, but not limited to, 
 the implied warranties of merchantability and fitness for a 
 particular purpose are disclaimed.  In no event shall TJK,
 agents of TJK, or other contributors be liable for any direct, 
 indirect, incidental, special, exemplary, or consequential damages 
 (including, but not limited to, procurement of substitute goods or 
 services; loss of use, data, or profits; or business interruption) 
 however caused and on any theory of liability, whether in 
 contract, strict liability, or tort (including negligence or 
 otherwise) arising in any way out of the use of this software, 
 even if advised of the possibility of such damage.                    

Download finder.c.

Copyright 2005 Tksoft Inc. All Rights Reserved.