#ifdef	ournix
#include "ournix.h"
#endif
#ifdef MSDOS	/* { */
#include	"grep_msd.h"
#endif		/* } */
char sccsID[] =
	"@(#) replace.c V1.6 Copyright Julian H. Stacey 27 July '89.\n" ;

/* See manual for documentation */
/*	Can rip copyright notices out of binaries.
	Useful for instance when we have a first compile with one
	company's binaries, but intend to replace the libraries
	later with something else, & don't want first company
	getting free publicity meantime.
 */

/* needs to be linked with  ../lib/replace.c */

#include	<stdio.h>
#include <stdlib.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#ifdef ns32000	/* { */
#define BSD
#endif		/* } */
#ifdef	BSD
#include	<sys/file.h>
#include	<sys/signal.h>
#else
#include	<signal.h>
#endif
#ifndef	BSD	/* { */
#include	<fcntl.h>
#endif		/* } */

#undef	MICROSHIT	/* { Define if compiling under Microshit,
				& we want to see hom much of the free
				advertising we can obfuscate.
			 */
#ifdef	MICROSHIT	/* { */
#include	"ms_exe.h"
#endif		/* } */

typedef	char		FLAG ;
#ifdef	MSDOS	/* { */
	typedef int	CKSUM ;
#endif		/* } */
#ifdef	ns32000	/* { */
	typedef	short	CKSUM ;
#endif		/* } */

extern int replace() ;
char	**ARGV ;

main(argc, argv)
	int	argc ;
	char	**argv ;
	{
	char	*remove_p	= (char *)0 ;
	char	*replace_p	= (char *)0 ;
	char	fill_c		= ' ' ;
	int	occurences_i	= -1 ;	/* allow up to no of occurences to do :
						-1 == dont care - none or many
						0 == none
						1 == 1
						2 == 2
						*/
	FLAG	exact_f		= 0 ;	/* error if dont acheive exactly
						occurences_i matches */
	FLAG	binary_f	= 0 ;
	FLAG	ms_cksum_f	= 0 ;	/* 0 = dont change msdos checksum */
	FLAG	verbose_f	= 0 ;	/* herald file names before doing */
	unsigned extra_u		= 0 ;	/* extra length beyond remove_p
						to also be removed */

	ARGV = argv ;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */

	while(argc--)
		{
		if (**++argv != '-') break ;
		switch( *++*argv )
			{
			case 'o':	/* out from file	*/
				remove_p = ++*argv ;
				break ;
			case 'i':	/* in to file	*/
				replace_p = ++*argv ;
				break ;
			case 'f':	/* fill character */
				fill_c = *++*argv ;
				break ;
			case 'e':	/* swallow an extra few bytes after
						end of -o string, -i string
						can not use this space */
				extra_u = atoi(++*argv) ;
				break ;
			case 'v':	/* verbose */
				verbose_f = 1 ;
				break ;
			case 't':	/* text files */
				binary_f = 0 ;
				break ;
			case 'b':	/* binary file:
					allow no size variation on file,
					enforce padding or truncation
					as appropriate */
				binary_f = 1 ;
				break ;
			case 'c':	/* correct checksum at end */
				ms_cksum_f = 1 ;
				binary_f = 1 ;
				break ;
			case 'N':
				exact_f	= 1 ;
				/* deliberately ommitted break ; */
			case 'n':	/* only first number of occurences */
				occurences_i = atoi(++*argv) ;
				break ;
#ifdef	MICROSHIT	/* { */
			case 's':	/* swop standard vsl defaults - option
					only available for VSL internal use */
				binary_f = 1 ;
				ms_cksum_f = 1 ;
				occurences_i = 1 ;
				replace_p = MS_IN ;
				remove_p = MS_OUT ;
				*remove_p = 'C' ;
				extra_u = 0 ;
				break ;
				/* could later allow for variants such as
				"C Library - (C)Copyright Microsoft Corp 1986"
					and
				"C Library - (C)Copyright Microsoft Corp 1985"
				 there appear to be 2 check digits after the
				MSC logo, first is 0x1f, then 0x00,
				although you can rearrange individual letters
				of the logo, and swap spaces to underscore,
				you cant decrement one letter, and
				increment another, (whether byte adjacent or
				word adjacent) so until this is better
				understood, I merely scramble their logo,
				when it is better understood,
					- extra_u can become 5,
					- string " 198" can be removed from end
						of MS_OUT,
					- replace_p can be :
			"C Library - To be replaced later ___________" */
#endif		/* } */
			default: syntax() ;			break ;
			}
		}
	if ( argc == 0 ) /* no args ; copy standard input	*/
		{
		fprintf( stderr,
			"%s: Must specify file(s), pipe usage not allowed.\n",
				*ARGV ) ;
		/* if (ms_cksum_f) pipe usage can never be allowed with
			checksum correction */
		syntax() ;
		}
	while (argc-- )
		if (replace(*argv++, remove_p, replace_p, extra_u,
				fill_c, occurences_i, exact_f, binary_f,

				ms_cksum_f, verbose_f )

/* JJlater
				ms_cksum_f, verbose_f, (FLAG)0, (char *)0) 
*/
			)

			{
			fprintf(stderr,
			"%s Warning: replace failed on %s, continuing.\n",
				*ARGV,*(argv - 1));
			}
	exit(0) ;
	}

syntax()
	{
	fprintf( stderr,
		"Syntax error with program version:\n%sCorrect Syntax:\n",
		sccsID);
	fprintf(stderr,
 "%s -oOut_string [-iIn_string] [-fFill_ch] [-eExtra_length] [-v] [-b/t] [-c] [-N/nOccurences]%s file[s]\n",
		*ARGV,
#ifdef	VSL	/* { VSL for export versions */
		""
#else		/* } { */
		" [-s]"
#endif		/* } */
		) ;
	exit(1) ;
	}
