#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>

#define MAXBUF 250

FILE *fptr;
unsigned char nbuf[MAXBUF],cbuf[MAXBUF],cb[2],srch[MAXBUF],ans[20];
int slen;

int edit(posn)
int posn;
{
    register int t;

    cbuf[MAXBUF-1] = '\0';   /* Make sure string is null terminated */
           
    printf("Found <%s> at offset %d.\n",cbuf,posn-1);
    printf("Do you wish to modify the string (yes/no/continue)?");
    gets(ans);   /* Get reply */

    if (tolower(ans[0]) == 'c') {   /* Continue search ? */
        fseek(fptr,posn+1,0);   /* Yes, seek to next char. in file */
        return(posn);   /* and return to search loop */
    }

    if (tolower(ans[0]) == 'y') {
        fseek(fptr,posn-1,0);   /* Seek to start of string */
        printf("Enter new text; terminate ^Z; abort ^C (be careful!!!)\n");

        for(t = 0; (nbuf[t] = getchar()) != '\032'; t++) {
            if (nbuf[t] == '\003')   /* Abort ? */
                return(-2);   /* If aborted, start over */
        }

        nbuf[t] = '\0';   /* Terminate the string */
        while (getchar() != '\n')   /* Throw away up to next newline */
            ;

         fwrite(nbuf,1,++t,fptr);   /* Zap out the string */
         printf("ZAP !!!!!\n");
    } else
        printf("OK, I left it alone.\n");

    printf("Continue, restart, or quit (c/r/q) ? --->");
    gets(ans);   /* Get reply */
    if (tolower(ans[0]) == 'q') {   /* Quit ? */
        fclose(fptr);   /* Yes, close the file */
        exit(0);   /* and exit */
    }
    if (tolower(ans[0]) == 'c') {   /* Continue ? */
        fseek(fptr,posn+1,0);   /* Yes, seek to current postion */
        return(posn);   /* and return with current position */
    }
    return(-2);   /* Else, restart */
}

int compare(posn)
int posn;
{
    fseek(fptr,posn-1,0);   /* Seek to start of string */
    if (fread(cbuf,1,MAXBUF-1,fptr) < slen) {   /* Try to get string */
        fseek(fptr,posn+1,0);   /* Not enough file left, seek back */
        return(-1);   /* and take failure return */
    }

    if (strncmp(cbuf,srch,slen) == 0)   /* Found it ? */
        return(0);   /* Yes, take success return */
    fseek(fptr,posn+1,0);   /* Else, seek back */
    return(-1);   /* and take error return */
}

main(argc,argv)
int argc;
char **argv;
{
    register int cc1,cc2,i;

    if (argc != 2) {
        fprintf(stderr,"usage: zap file_to_zap\n");
        exit(1);
    }

    if ((fptr = fopen(*++argv,"r+")) < 0) {
        fprintf(stderr,"zap: Couldn't open %s for read/write\n",*argv);
        exit(1);
    }

    for ( ; ; ) {
        fseek(fptr,0,0);   /* Go to beginning of file */
        printf("Enter string to search for (or C/R to quit) --->");
        gets(srch);  /* Get search string */
        slen = strlen(srch);   /* Find its length */

        if (slen == 0) {   /* Done ? */
            fclose(fptr);   /* Yes, close the file */
            exit(0);   /* and quit */
        }

        cc1 = srch[0];   /* Get 1st char. of string */
        cc2 = srch[1];   /* Get 2nd char. of string */
        fread(cb,1,1,fptr);   /* Get 1st char. in file */

        for (i = 1; (i >= 0) && (fread(&cb[1],1,1,fptr) == 1); i++) {
            if (i != 1) {   /* Not the start of file ? */
                if ((cc1 == cb[0]) && (cc2 == cb[1])) {   /* Match 1 & 2 ? */
                    if (compare(i) == 0) {   /* and all the rest ? */
                        i = edit(i);   /* Yes, go see if edit desired */
                    }
                }
            }
            cb[0] = cb[1];
        }

        if (i < 0)   /* Search for new string ? */
            continue;   /* Yes, re-loop */

        printf("Didn't find <%s> in file %s\n",srch,*argv);
    }
}
