SCR/SCR3.1/TP2/eraser.c

53 lines
1.1 KiB
C
Raw Normal View History

2023-09-15 16:17:36 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
2023-10-05 07:53:10 +02:00
#include <errno.h>
#include <time.h>
#define BLOCK_SIZE 1
2023-09-15 16:17:36 +02:00
int main(int argc, char **argv){
2023-10-05 07:53:10 +02:00
int fd, fd1;
ssize_t read_bytes, wrote_bytes;
char buf[BLOCK_SIZE];
char *name;
2023-09-15 16:17:36 +02:00
if (argc != 2){
printf("Usage : %s <path_to_the_file_to_erase>", argv[0]);
exit(EXIT_FAILURE);
}
2023-10-05 07:53:10 +02:00
struct stat sb;
2023-09-15 16:17:36 +02:00
if (stat(argv[1], &sb) == -1){
perror("stat");
exit(EXIT_FAILURE);
}
2023-10-05 07:53:10 +02:00
if (sb.st_nlink > (nlink_t) 1) {
unlink(argv[1]);
2023-09-15 16:17:36 +02:00
exit(EXIT_FAILURE);
}
else {
fd = open(argv[1], O_SYNC|O_RDONLY);
if (fd == -1){
perror("fd");
exit(EXIT_SUCCESS);
}
2023-10-05 07:53:10 +02:00
while (read_bytes = read(fd,buf,BLOCK_SIZE) > 0) {
wrote_bytes = write(fd,"0xff",BLOCK_SIZE);
}
2023-09-15 16:17:36 +02:00
2023-10-05 07:53:10 +02:00
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);
2023-09-15 16:17:36 +02:00
return EXIT_SUCCESS;
}