1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include "libbb.h"
41
42int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
43int shred_main(int argc UNUSED_PARAM, char **argv)
44{
45 char *opt_s;
46 int rand_fd = rand_fd;
47 int zero_fd;
48 unsigned num_iter = 3;
49 unsigned opt;
50 enum {
51 OPT_f = (1 << 0),
52 OPT_u = (1 << 1),
53 OPT_z = (1 << 2),
54 OPT_n = (1 << 3),
55 OPT_v = (1 << 4),
56 OPT_x = (1 << 5),
57 OPT_s = (1 << 6),
58 };
59
60 opt = getopt32(argv, "^" "fuzn:+vxs:" "\0" "-1", &num_iter, &opt_s);
61 argv += optind;
62
63 zero_fd = xopen("/dev/zero", O_RDONLY);
64 if (num_iter != 0)
65 rand_fd = xopen("/dev/urandom", O_RDONLY);
66
67 for (;;) {
68 struct stat sb;
69 const char *fname;
70 unsigned i;
71 int fd;
72
73 fname = *argv++;
74 if (!fname)
75 break;
76 fd = -1;
77 if (opt & OPT_f) {
78 fd = open(fname, O_WRONLY);
79 if (fd < 0)
80 chmod(fname, 0666);
81 }
82 if (fd < 0)
83 fd = xopen(fname, O_WRONLY);
84
85 if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
86 off_t size = sb.st_size;
87
88 if (opt & OPT_s) {
89 size = BB_STRTOOFF(opt_s, NULL, 0);
90 if (errno || size < 0) bb_show_usage();
91 }
92
93 for (i = 0; i < num_iter; i++) {
94 bb_copyfd_size(rand_fd, fd, size);
95 fdatasync(fd);
96 xlseek(fd, 0, SEEK_SET);
97 }
98 if (opt & OPT_z) {
99 bb_copyfd_size(zero_fd, fd, size);
100 fdatasync(fd);
101 }
102 }
103 if (opt & OPT_u) {
104 ftruncate(fd, 0);
105 xunlink(fname);
106 }
107 xclose(fd);
108 }
109
110 return EXIT_SUCCESS;
111}
112