OSDN Git Service

Make xopen() skip stdin/stdout/stderr, add xopen_stdio() if you want stdout,
[android-x86/external-toybox.git] / toys / other / shred.c
index 07d62c3..30b5e7d 100644 (file)
@@ -8,7 +8,7 @@ USE_SHRED(NEWTOY(shred, "<1zxus#<1n#<1o#<0f", TOYFLAG_USR|TOYFLAG_BIN))
 
 config SHRED
   bool "shred"
-  default n
+  default y
   help
     usage: shred [-fuz] [-n COUNT] [-s SIZE] FILE...
 
@@ -42,7 +42,7 @@ void shred_main(void)
   char **try;
 
   if (!(toys.optflags & FLAG_n)) TT.iterations++;
-  TT.ufd = xopen("/dev/urandom", O_RDONLY);
+  TT.ufd = xopenro("/dev/urandom");
 
   // We don't use loopfiles() here because "-" isn't stdin, and want to
   // respond to files we can't open via chmod.
@@ -57,7 +57,7 @@ void shred_main(void)
       fd = open(*try, O_RDWR);
     }
     if (fd == -1) {
-      perror_msg("%s", *try);
+      perror_msg_raw(*try);
       continue;
     }
 
@@ -84,7 +84,7 @@ void shred_main(void)
 
       if (pos < TT.offset) {
         if (TT.offset != lseek(fd, TT.offset, SEEK_SET)) {
-          perror_msg("%s", *try);
+          perror_msg_raw(*try);
           break;
         }
         pos = TT.offset;
@@ -97,8 +97,10 @@ void shred_main(void)
         if (len-pos < throw) throw = len-pos;
 
       if (iter != TT.iterations) xread(TT.ufd, toybuf, throw);
-      if (throw != writeall(fd, toybuf, throw)) perror_msg("%s");
+      if (throw != writeall(fd, toybuf, throw)) perror_msg_raw(*try);
       pos += throw;
     }
+    if (toys.optflags & FLAG_u)
+      if (unlink(*try)) perror_msg("unlink '%s'", *try);
   }
 }