addgarbage.c
1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* 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;
}