/*
** bgrep v1.0
**
** Binary grep
**
** Copyright (C) 28.9.1999 by Andreas Ley <ley@rz.uni-karlsruhe.de>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** 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., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** This program has been tested on a HP9000/715 with HP-UX A.09.07
** In this environment, neither lint -u nor gcc -Wall -pedantic -fsyntax-only
** produce any messages. If you encounter any errors or need to make any
** changes to port it to another platform, please contact me.
**
** Version history
**
** Version 1.0 - 28.9.1999
**	Initial version
*/

static char copyright[] = "@(#)Copyright (C) 1999 by Andreas Ley (ley@rz.uni-karlsruhe.de)";
static char sccsid[] = "@(#)bgrep v1.0 - Binary grep";


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>

#ifndef FALSE
#define	FALSE	(0)		/* This is the naked Truth */
#define	TRUE	(1)		/* and this is the Light */
#endif


char	*image;
int	debug=0,opt_x=FALSE;


static void bgrep(fd,pattern)
int	fd;
char	*pattern;
{
	char	buffer[8*1024],*ptr;
	size_t	len,offset=0;

	if (debug)
		(void)fprintf(stderr,"[%d] bgrep(%d)\n",__LINE__,fd);
	while ((len=read(fd,buffer,sizeof(buffer)))) {
		for (ptr=buffer;(ptr<buffer+len)&&(ptr=memchr(ptr,*pattern,len));ptr++)
			(void)printf(opt_x?"0x%lx\n":"%ld\n",offset+(ptr-buffer));
		offset+=len;
	}
}



static void usage()
{
	(void)fprintf(stderr,"Usage: %s [-x] pattern [filename...]\n",image);
	(void)fprintf(stderr,"-x  Output occurences in hexadecimal\n");
	exit(1);
}


int main(argc,argv)
int	argc;
char	*argv[];
{
	int		c;
	extern char	*optarg;
	extern int	optind;
	char		*pattern;
	int		fd;

	if ((image=strrchr(argv[0],'/')))
		image++;
	else
		image=argv[0];

	while ((c=getopt(argc,argv,"Dxvh?"))!=EOF)
		switch ((char)c) {
		case 'D':
			debug++;
			break;
		case 'x':
			opt_x=TRUE;
			break;
		case 'v':
			(void)fprintf(stderr,"%s\n",sccsid+4);
			(void)fprintf(stderr,"%s\n",copyright+4);
			exit(0);
		case 'h':
			(void)fprintf(stderr,"%s\n",sccsid+4);
			(void)fprintf(stderr,"%s\n",copyright+4);
		case '?':
			usage();
		}

	if (optind==argc)
		usage();
	pattern=argv[optind++];

	if (optind<argc)
		for (;optind<argc;optind++)
			if (strcmp(argv[optind],"-"))
				if ((fd=open(argv[optind],O_RDONLY))>=0) {
					bgrep(fd,pattern);
					(void)close(fd);
				}
				else {
					(void)fprintf(stderr,"%s: cannot open ",image);
					perror(argv[optind]);
				}
			else
				bgrep(0,pattern);
	else
		bgrep(0,pattern);

	return(0);
}

