aboutsummaryrefslogtreecommitdiff
path: root/coreutils/shred.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/shred.c')
-rw-r--r--coreutils/shred.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/coreutils/shred.c b/coreutils/shred.c
index 0b11b9491..794d7b815 100644
--- a/coreutils/shred.c
+++ b/coreutils/shred.c
@@ -15,14 +15,15 @@
15//kbuild:lib-$(CONFIG_SHRED) += shred.o 15//kbuild:lib-$(CONFIG_SHRED) += shred.o
16 16
17//usage:#define shred_trivial_usage 17//usage:#define shred_trivial_usage
18//usage: "FILE..." 18//usage: "[-fuz] [-n N] [-s SIZE] FILE..."
19//usage:#define shred_full_usage "\n\n" 19//usage:#define shred_full_usage "\n\n"
20//usage: "Overwrite/delete FILEs\n" 20//usage: "Overwrite/delete FILEs\n"
21//usage: "\n -f Chmod to ensure writability" 21//usage: "\n -f Chmod to ensure writability"
22//usage: "\n -s SIZE Size to write"
22//usage: "\n -n N Overwrite N times (default 3)" 23//usage: "\n -n N Overwrite N times (default 3)"
23//usage: "\n -z Final overwrite with zeros" 24//usage: "\n -z Final overwrite with zeros"
24//usage: "\n -u Remove file" 25//usage: "\n -u Remove file"
25//-x and -v are accepted but have no effect 26//-x (exact: don't round up to 4k) and -v (verbose) are accepted but have no effect
26 27
27/* shred (GNU coreutils) 8.25: 28/* shred (GNU coreutils) 8.25:
28-f, --force change permissions to allow writing if necessary 29-f, --force change permissions to allow writing if necessary
@@ -41,6 +42,7 @@
41int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 42int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42int shred_main(int argc UNUSED_PARAM, char **argv) 43int shred_main(int argc UNUSED_PARAM, char **argv)
43{ 44{
45 char *opt_s;
44 int rand_fd = rand_fd; /* for compiler */ 46 int rand_fd = rand_fd; /* for compiler */
45 int zero_fd; 47 int zero_fd;
46 unsigned num_iter = 3; 48 unsigned num_iter = 3;
@@ -52,18 +54,16 @@ int shred_main(int argc UNUSED_PARAM, char **argv)
52 OPT_n = (1 << 3), 54 OPT_n = (1 << 3),
53 OPT_v = (1 << 4), 55 OPT_v = (1 << 4),
54 OPT_x = (1 << 5), 56 OPT_x = (1 << 5),
57 OPT_s = (1 << 6),
55 }; 58 };
56 59
57 opt = getopt32(argv, "fuzn:+vx", &num_iter); 60 opt = getopt32(argv, "^" "fuzn:+vxs:" "\0" "-1"/*min 1 arg*/, &num_iter, &opt_s);
58 argv += optind; 61 argv += optind;
59 62
60 zero_fd = MINGW_SPECIAL(xopen)("/dev/zero", O_RDONLY); 63 zero_fd = MINGW_SPECIAL(xopen)("/dev/zero", O_RDONLY);
61 if (num_iter != 0) 64 if (num_iter != 0)
62 rand_fd = MINGW_SPECIAL(xopen)("/dev/urandom", O_RDONLY); 65 rand_fd = MINGW_SPECIAL(xopen)("/dev/urandom", O_RDONLY);
63 66
64 if (!*argv)
65 bb_show_usage();
66
67 for (;;) { 67 for (;;) {
68 struct stat sb; 68 struct stat sb;
69 const char *fname; 69 const char *fname;
@@ -85,6 +85,11 @@ int shred_main(int argc UNUSED_PARAM, char **argv)
85 if (fstat(fd, &sb) == 0 && sb.st_size > 0) { 85 if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
86 off_t size = sb.st_size; 86 off_t size = sb.st_size;
87 87
88 if (opt & OPT_s) {
89 size = BB_STRTOOFF(opt_s, NULL, 0); /* accepts oct/hex */
90 if (errno || size < 0) bb_show_usage();
91 }
92
88 for (i = 0; i < num_iter; i++) { 93 for (i = 0; i < num_iter; i++) {
89 bb_copyfd_size(rand_fd, fd, size); 94 bb_copyfd_size(rand_fd, fd, size);
90 fdatasync(fd); 95 fdatasync(fd);
@@ -94,18 +99,18 @@ int shred_main(int argc UNUSED_PARAM, char **argv)
94 bb_copyfd_size(zero_fd, fd, size); 99 bb_copyfd_size(zero_fd, fd, size);
95 fdatasync(fd); 100 fdatasync(fd);
96 } 101 }
97 if (opt & OPT_u) { 102 }
98 ftruncate(fd, 0); 103 if (opt & OPT_u) {
104 ftruncate(fd, 0);
99#if ENABLE_PLATFORM_MINGW32 105#if ENABLE_PLATFORM_MINGW32
100 xclose(fd); 106 xclose(fd);
101#endif 107#endif
102 xunlink(fname); 108 xunlink(fname);
103 } 109 }
104#if ENABLE_PLATFORM_MINGW32 110#if ENABLE_PLATFORM_MINGW32
105 else 111 else
106#endif 112#endif
107 xclose(fd); 113 xclose(fd);
108 }
109 } 114 }
110 115
111 return EXIT_SUCCESS; 116 return EXIT_SUCCESS;