This commit is contained in:
Your Name 2023-10-15 11:29:56 +03:00
parent 4ad86cbbd4
commit 4cf3658034
2 changed files with 109 additions and 2 deletions

2
TODO
View File

@ -4,8 +4,6 @@ printf
shuf
id
ln
cp
shred
du
df
dd

109
coreutils/shred.c Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define RAND_SOURCE "/dev/urandom"
#define ZERO_SOURCE "/dev/zero"
unsigned int f_flag;
unsigned int u_flag;
unsigned int z_flag;
unsigned int n_loops;
int shred(int rand_fd, int zero_fd, int fd, unsigned int iter) {
off_t size = lseek(fd, 0, SEEK_END);
if (size < 0)
return 1;
lseek(fd, 0, SEEK_SET);
char buf[1];
while (size) {
if (iter == n_loops && z_flag)
read(zero_fd, buf, sizeof(buf));
else
read(rand_fd, buf, sizeof(buf));
write(fd, buf, sizeof(buf));
size--;
}
fsync(fd);
return 0;
}
int main(const int argc, const char **argv) {
n_loops = 3;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "-f"))
f_flag = 1;
else if (!strcmp(argv[i], "-u"))
u_flag = 1;
else if (!strcmp(argv[i], "-z"))
z_flag = 1;
else if (!strncmp(argv[i], "-n=", 3)) {
char *val = strchr(argv[i], '=');
if (!val)
break;
val[0] = '\0';
n_loops = atoi(val + 1);
}
else if (!strcmp(argv[i], "-h")) {
printf("shred [-n=N (Overwrite N times (default 3))] [-z (Final overwrite with zeros)] [-u (Remove file)] [-f (Chmod to ensure writability)] [FILE...]\n");
return 1;
}
}
int rand_fd = open(RAND_SOURCE, O_RDONLY);
if (rand_fd < 0) {
fprintf(stderr, "shred: %s is %s\n", RAND_SOURCE, strerror(errno));
return 1;
}
int zero_fd = open(ZERO_SOURCE, O_RDONLY);
if (zero_fd < 0) {
fprintf(stderr, "shred: %s is %s\n", ZERO_SOURCE, strerror(errno));
return 1;
}
for (; i < argc; i++) {
int fd = open(argv[i], O_WRONLY);
if (fd < 0) {
fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno));
continue;
}
for (unsigned int n = 0; n <= n_loops; n++)
shred(rand_fd, zero_fd, fd, n);
close(fd);
if (u_flag)
if (unlink(argv[i]) < 0)
fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno));
if (f_flag)
if (chmod(argv[i], 0) < 0)
fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno));
}
close(rand_fd);
close(zero_fd);
return 0;
}