cr_1578.c

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

#define BUFSIZE 512
extern int errno;

int main(int argc, char **argv)
{
static char mybuf[BUFSIZE], *p ;
char *pos;
int fp, n, error;
char *strorg=argv[2];      /* A data exist in the datafile (actually table data )  */
char chrcorrupt=*argv[3];  /* initial letter of the strorg will be dirtied */
/*    with the character chrcorrupt */
char *match;

if ((fp = open (argv[1], O_RDWR)) == -1)
{
fprintf (stderr, “Can’t open data: %s\n”, strerror (errno));
return 1;
}
while ((n = read(fp, mybuf, BUFSIZE)) > 0 )
{
match = memmem( (void *) mybuf, 512, (void *)strorg, strlen(strorg));
if ( match )
{
*match=chrcorrupt;  /* corrupt 1 byte with the given chracter chrcorrupt */
printf(“–>  occurence of  String ‘%s’ found and corrupted as ‘%c%s’\n”,
strorg,chrcorrupt,strorg+1);
lseek(fp,-n,SEEK_CUR);
write(fp, mybuf, n);
}
}
while((( error = close(fp)) == -1) && (errno == EINTR));
if(error == -1)
perror(“Failed to close the file\n”);

return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *