addgarbage.c 1.46 KB
/*
 * read in a file, write random garbage starting from a byte offset,
 * upto rest of file
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>


  
     
int main(int argc, char **argv){
   
  int numread;
  FILE *infileptr;
  FILE *outfileptr;
  unsigned char tempbuf[256];
  unsigned long bytestart;
  int started = 0;
  int blocknum = 0;
  int i;

  if(argc != 4){
    fprintf(stderr,"usage: %s  inputfile outputfile bytestart\n", argv[0]);
    exit(1);
  }
  
    
  /*open files */
  infileptr = fopen(argv[1], "r");
  outfileptr = fopen(argv[2], "w");
  if((infileptr == NULL) ||(outfileptr == NULL)){
    fprintf(stderr,"couldnt open files\n");
    exit(1);
  }
  bytestart = atoi(argv[3]);
  do{
    numread = fread(tempbuf, 1, sizeof(tempbuf), infileptr);
    if(started > 0){
      for (i=0; i < sizeof(tempbuf); i++){
	tempbuf[i] = (unsigned char) rand() & 0x000000ff;
      }
    }
    else{
      if ((bytestart > (blocknum * sizeof(tempbuf)))&&(bytestart < ((blocknum+1) * sizeof(tempbuf)))){
	printf("started in block num = %d\n", blocknum);
	started++; /* next time fill completely */
	for(i = bytestart - (blocknum*sizeof(tempbuf)); i < sizeof(tempbuf); i++){
	  tempbuf[i] = (unsigned char ) rand() & 0x000000ff;
	}
      }
    }
    fwrite(tempbuf, 1, numread, outfileptr);
    blocknum++;
  } while(numread == sizeof(tempbuf));

  fclose(infileptr);
  fclose(outfileptr);

  return 0;
}