commit 7bedf74c03f7564c4afa6e24de97cea07a4e6797
parent ab00cba80681395e8a93c63613a2adaa17587ed4
Author: qorg11 <qorg@vxempire.xyz>
Date: Sat, 25 Jul 2020 01:58:16 +0200
use stat() instead of a own function to count bytes.
also TODO: overwrite the files content with random data and ad 4096 of
random data
Diffstat:
1 file changed, 4 insertions(+), 20 deletions(-)
diff --git a/src/shred.c b/src/shred.c
@@ -5,24 +5,7 @@
#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;
-}
+#include <sys/stat.h>
int
fill_with_zeroes(const char *filename)
@@ -33,10 +16,11 @@ fill_with_zeroes(const char *filename)
printf("Error reading file: %i = %s\n",errno,strerror(errno));
return 1;
}
- int bytes_to_write = count_bytes(filename);
+ struct stat stat_struct; /* What name should i use? */
+ stat(filename,&stat_struct);
+ long int bytes_to_write = stat_struct.st_size;
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);