Here is the original version I wrote, optimized for Unix/Linux:
// Secure erase program
// For regular files
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main( int argc, char **argv ){
int fd = open( argv[1], O_RDWR );
// Get file length:
off_t len = lseek( fd, 0, SEEK_END );
lseek( fd, 0, SEEK_SET );
// Write zeros to the file until
// reaching the end:
for( int i = 0; i < len; i++ ){
write( fd, "\0", 1 );
}
close( fd );
// Truncate to 0 bytes:
fd = open( argv[1], O_WRONLY | O_TRUNC );
close( fd );
// Rename and delete:
rename( argv[1], tmpnam( NULL ) );
unlink( argv[1] );
return 0;
}
Here's a modified version using only the C Standard Library. It's not as efficient, but it's portable to Windows:
// Secure erase program
// For regular files
#include <stdio.h>
int main( int argc, char **argv ){
FILE *fp = fopen( argv[1], "r+" );
// Get file length:
fseek( fp, 0, SEEK_END );
long len = ftell( fp );
fseek( fp, 0, SEEK_SET );
// Write zeros to the file until
// reaching the end:
for( int i = 0; i < len; i++ ){
fputc( '\0', fp );
}
fclose( fp );
// Truncate to 0 bytes:
fp = fopen( argv[1], "w" );
fclose( fp );
// Rename and delete:
rename( argv[1], tmpnam( NULL ) );
remove( argv[1] );
return 0;
}
The program could use some improvement obviously, seeing as I haven't added any error checking and it only works on regular files (as opposed to directories, symbolic links, etc.).


Sign In
Create Account


Back to top









