-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
52 lines (45 loc) · 1.48 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
uint64_t count_bytes(FILE* file) {
uint64_t counter = 0;
while (fgetc(file) != EOF) counter++;
fseek(file, 0, SEEK_SET);
return counter;
}
void overwrite(FILE* file, const char* fname, uint64_t iterations) {
// mem: overwrite is responsible for the buffer it creates, that's it
uint64_t bytes = count_bytes(file);
printf("file: %s, %lu bytes found, overwirting it\n", fname, bytes);
uint8_t* buffer = (uint8_t*) malloc(bytes);
for (uint64_t overwrite_count=0; overwrite_count < iterations; overwrite_count++) {
// write n times to the file
for (uint64_t i=0; i < bytes; i++) {
// fill in buffer with randoms bytes
uint8_t num = (uint8_t)(rand() % 254);
buffer[i] = num;
}
fwrite(&buffer, 1, bytes, file);
fseek(file, 0, SEEK_SET);
}
free(buffer);
}
int main(const int argc, const char**argv) {
srand(time(NULL));
if (argc == 1) {
printf("No files to shred\n");
return EXIT_FAILURE;
}
for (uint64_t i = 1; i < argc; i++) {
// iterate over given files
printf("reading file : %s", argv[i]);
FILE* write_file = fopen(argv[i], "rb+");
if (write_file == NULL) {
printf("Could not open file %s in write mode, continuing...", argv[i]);
}
overwrite(write_file, argv[i], 100);
fclose(write_file);
};
return 0;
}