k9core

Unnamed repository
Log | Files | Refs | LICENSE

commit ab00cba80681395e8a93c63613a2adaa17587ed4
parent 801cf9d5ae75a36105bb88468219eee9b7f04466
Author: qorg11 <qorg@vxempire.xyz>
Date:   Fri, 24 Jul 2020 02:35:23 +0200

Added shred

Diffstat:
Asrc/shred.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+), 0 deletions(-)

diff --git a/src/shred.c b/src/shred.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <getopt.h> +#include <errno.h> + +int +count_bytes(const char *filename) +{ + int fd = open(filename,O_RDWR); + if(fd == -1) + { + printf("Error reading file: %i = %s\n",errno,strerror(errno)); + return 1; + } + int bytes_read = 0; + char *buf = NULL; + int bytes = 0; + buf = malloc(1); + while ((bytes_read = read(fd,buf,1)) > 0) + bytes++; + return bytes; +} + +int +fill_with_zeroes(const char *filename) +{ + int fd = open(filename,O_RDWR); + if(fd == -1) + { + printf("Error reading file: %i = %s\n",errno,strerror(errno)); + return 1; + } + int bytes_to_write = count_bytes(filename); + int bytes_read = 0; + char *buf = NULL; + int bytes = 0; + buf = malloc(bytes_to_write); + while((bytes_read = read(fd,buf,bytes_to_write)) > 0) + write(fd,"\0",bytes_to_write + 4096); + free(buf); + return 0; +} + +int +main(int argc, char *argv[]) +{ + int c = getopt(argc, argv, "u"); /* TODO: add -f */ + + fill_with_zeroes(argv[1]); + if(c == 'u') + remove(argv[1]); +}