SCR/SCR3.1/TP2/eraser.c
2023-10-05 07:53:10 +02:00

53 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#define BLOCK_SIZE 1
int main(int argc, char **argv){
int fd, fd1;
ssize_t read_bytes, wrote_bytes;
char buf[BLOCK_SIZE];
char *name;
if (argc != 2){
printf("Usage : %s <path_to_the_file_to_erase>", argv[0]);
exit(EXIT_FAILURE);
}
struct stat sb;
if (stat(argv[1], &sb) == -1){
perror("stat");
exit(EXIT_FAILURE);
}
if (sb.st_nlink > (nlink_t) 1) {
unlink(argv[1]);
exit(EXIT_FAILURE);
}
else {
fd = open(argv[1], O_SYNC|O_RDONLY);
if (fd == -1){
perror("fd");
exit(EXIT_SUCCESS);
}
while (read_bytes = read(fd,buf,BLOCK_SIZE) > 0) {
wrote_bytes = write(fd,"0xff",BLOCK_SIZE);
}
fd1 = open("/dev/urandom", O_RDONLY);
while (read(fd, buf, BLOCK_SIZE) > 0) {
read_bytes = read(fd1, buf, BLOCK_SIZE);
wrote_bytes = write(fd, buf, BLOCK_SIZE);
}
srand(time(NULL));
name = "file" + (char) rand() + ".crypt";
rename(argv[1], name);
unlink(name);
}
close(fd);
close(fd1);
return EXIT_SUCCESS;
}