*** empty log message ***
[coreutils.git] / src / shred.c
blobfb77dcade57dbd7fc747f10dc5335ea394a99615
1 /* shred.c - overwrite files and devices to make it harder to recover data
3 Copyright (C) 1999-2002 Free Software Foundation, Inc.
4 Copyright (C) 1997, 1998, 1999 Colin Plumb.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 Written by Colin Plumb. */
22 /* TODO:
23 - use consistent non-capitalization in error messages
24 - add standard GNU copyleft comment
26 - Add -r/-R/--recursive
27 - Add -i/--interactive
28 - Reserve -d
29 - Add -L
30 - Deal with the amazing variety of gettimeofday() implementation bugs.
31 (Some systems use a one-arg form; still others insist that the timezone
32 either be NULL or be non-NULL. Whee.)
33 - Add an unlink-all option to emulate rm.
37 * Do a more secure overwrite of given files or devices, to make it harder
38 * for even very expensive hardware probing to recover the data.
40 * Although this process is also known as "wiping", I prefer the longer
41 * name both because I think it is more evocative of what is happening and
42 * because a longer name conveys a more appropriate sense of deliberateness.
44 * For the theory behind this, see "Secure Deletion of Data from Magnetic
45 * and Solid-State Memory", on line at
46 * http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
48 * Just for the record, reversing one or two passes of disk overwrite
49 * is not terribly difficult with hardware help. Hook up a good-quality
50 * digitizing oscilloscope to the output of the head preamplifier and copy
51 * the high-res digitized data to a computer for some off-line analysis.
52 * Read the "current" data and average all the pulses together to get an
53 * "average" pulse on the disk. Subtract this average pulse from all of
54 * the actual pulses and you can clearly see the "echo" of the previous
55 * data on the disk.
57 * Real hard drives have to balance the cost of the media, the head,
58 * and the read circuitry. They use better-quality media than absolutely
59 * necessary to limit the cost of the read circuitry. By throwing that
60 * assumption out, and the assumption that you want the data processed
61 * as fast as the hard drive can spin, you can do better.
63 * If asked to wipe a file, this also unlinks it, renaming it to in a
64 * clever way to try to leave no trace of the original filename.
66 * The ISAAC code still bears some resemblance to the code written
67 * by Bob Jenkins, but he permits pretty unlimited use.
69 * This was inspired by a desire to improve on some code titled:
70 * Wipe V1.0-- Overwrite and delete files. S. 2/3/96
71 * but I've rewritten everything here so completely that no trace of
72 * the original remains.
74 * Thanks to:
75 * Bob Jenkins, for his good RNG work and patience with the FSF copyright
76 * paperwork.
77 * Jim Meyering, for his work merging this into the GNU fileutils while
78 * still letting me feel a sense of ownership and pride. Getting me to
79 * tolerate the GNU brace style was quite a feat of diplomacy.
80 * Paul Eggert, for lots of useful discussion and code. I disagree with
81 * an awful lot of his suggestions, but they're disagreements worth having.
83 * Things to think about:
84 * - Security: Is there any risk to the race
85 * between overwriting and unlinking a file? Will it do anything
86 * drastically bad if told to attack a named pipe or socket?
89 /* The official name of this program (e.g., no `g' prefix). */
90 #define PROGRAM_NAME "shred"
92 #define AUTHORS "Colin Plumb"
94 #if HAVE_CONFIG_H
95 # include <config.h>
96 #endif
98 #include <getopt.h>
99 #include <stdio.h>
100 #include <assert.h>
101 #include <setjmp.h>
102 #include <signal.h>
103 #include <sys/types.h>
105 #include "system.h"
106 #include "xstrtol.h"
107 #include "closeout.h"
108 #include "error.h"
109 #include "human.h"
110 #include "quotearg.h" /* For quotearg_colon */
111 #include "quote.h" /* For quotearg_colon */
112 char *xstrdup PARAMS ((char const *));
114 #ifndef O_NOCTTY
115 # define O_NOCTTY 0 /* This is a very optional frill */
116 #endif
118 /* Some systems don't support some file types. */
119 #ifndef S_ISFIFO
120 # define S_ISFIFO(mode) 0
121 #endif
122 #ifndef S_ISLNK
123 # define S_ISLNK(mode) 0
124 #endif
125 #ifndef S_ISSOCK
126 # define S_ISSOCK(mode) 0
127 #endif
129 #define DEFAULT_PASSES 25 /* Default */
131 /* How many seconds to wait before checking whether to output another
132 verbose output line. */
133 #define VERBOSE_UPDATE 5
135 /* If positive, the units to use when printing sizes;
136 if negative, the human-readable base. */
137 #define OUTPUT_BLOCK_SIZE (-1024)
139 struct Options
141 int force; /* -f flag: chmod files if necessary */
142 size_t n_iterations; /* -n flag: Number of iterations */
143 off_t size; /* -s flag: size of file */
144 int remove_file; /* -u flag: remove file after shredding */
145 int verbose; /* -v flag: Print progress */
146 int exact; /* -x flag: Do not round up file size */
147 int zero_fill; /* -z flag: Add a final zero pass */
150 static struct option const long_opts[] =
152 {"exact", no_argument, NULL, 'x'},
153 {"force", no_argument, NULL, 'f'},
154 {"iterations", required_argument, NULL, 'n'},
155 {"size", required_argument, NULL, 's'},
156 {"remove", no_argument, NULL, 'u'},
157 {"verbose", no_argument, NULL, 'v'},
158 {"zero", required_argument, NULL, 'z'},
159 {GETOPT_HELP_OPTION_DECL},
160 {GETOPT_VERSION_OPTION_DECL},
161 {NULL, 0, NULL, 0}
164 /* Global variable for error printing purposes */
165 char const *program_name; /* Initialized before any possible use */
167 void
168 usage (int status)
170 if (status != 0)
171 fprintf (stderr, _("Try `%s --help' for more information.\n"),
172 program_name);
173 else
175 printf (_("Usage: %s [OPTIONS] FILE [...]\n"), program_name);
176 fputs (_("\
177 Overwrite the specified FILE(s) repeatedly, in order to make it harder\n\
178 for even very expensive hardware probing to recover the data.\n\
180 "), stdout);
181 fputs (_("\
182 Mandatory arguments to long options are mandatory for short options too.\n\
183 "), stdout);
184 printf (_("\
185 -f, --force change permissions to allow writing if necessary\n\
186 -n, --iterations=N Overwrite N times instead of the default (%d)\n\
187 -s, --size=N shred this many bytes (suffixes like K, M, G accepted)\n\
188 "), DEFAULT_PASSES);
189 fputs (_("\
190 -u, --remove truncate and remove file after overwriting\n\
191 -v, --verbose show progress\n\
192 -x, --exact do not round file sizes up to the next full block\n\
193 -z, --zero add a final overwrite with zeros to hide shredding\n\
194 - shred standard output\n\
195 "), stdout);
196 fputs (HELP_OPTION_DESCRIPTION, stdout);
197 fputs (VERSION_OPTION_DESCRIPTION, stdout);
198 fputs (_("\
200 Delete FILE(s) if --remove (-u) is specified. The default is not to remove\n\
201 the files because it is common to operate on device files like /dev/hda,\n\
202 and those files usually should not be removed. When operating on regular\n\
203 files, most people use the --remove option.\n\
205 "), stdout);
206 fputs (_("\
207 CAUTION: Note that shred relies on a very important assumption:\n\
208 that the filesystem overwrites data in place. This is the traditional\n\
209 way to do things, but many modern filesystem designs do not satisfy this\n\
210 assumption. The following are examples of filesystems on which shred is\n\
211 not effective:\n\
213 "), stdout);
214 fputs (_("\
215 * log-structured or journaled filesystems, such as those supplied with\n\
216 AIX and Solaris (and JFS, ReiserFS, XFS, Ext3, etc.)\n\
218 * filesystems that write redundant data and carry on even if some writes\n\
219 fail, such as RAID-based filesystems\n\
221 * filesystems that make snapshots, such as Network Appliance's NFS server\n\
223 "), stdout);
224 fputs (_("\
225 * filesystems that cache in temporary locations, such as NFS\n\
226 version 3 clients\n\
228 * compressed filesystems\n\
230 In addition, file system backups and remote mirrors may contain copies\n\
231 of the file that cannot be removed, and that will allow a shredded file\n\
232 to be recovered later.\n\
233 "), stdout);
234 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
236 exit (status);
239 #if ! HAVE_FDATASYNC
240 # define fdatasync(fd) -1
241 #endif
244 * --------------------------------------------------------------------
245 * Bob Jenkins' cryptographic random number generator, ISAAC.
246 * Hacked by Colin Plumb.
248 * We need a source of random numbers for some of the overwrite data.
249 * Cryptographically secure is desirable, but it's not life-or-death
250 * so I can be a little bit experimental in the choice of RNGs here.
252 * This generator is based somewhat on RC4, but has analysis
253 * (http://ourworld.compuserve.com/homepages/bob_jenkins/randomnu.htm)
254 * pointing to it actually being better. I like it because it's nice
255 * and fast, and because the author did good work analyzing it.
256 * --------------------------------------------------------------------
259 #if defined __STDC__ && __STDC__
260 # define UINT_MAX_32_BITS 4294967295U
261 #else
262 # define UINT_MAX_32_BITS 0xFFFFFFFF
263 #endif
265 #if ULONG_MAX == UINT_MAX_32_BITS
266 typedef unsigned long word32;
267 #else
268 # if UINT_MAX == UINT_MAX_32_BITS
269 typedef unsigned word32;
270 # else
271 # if USHRT_MAX == UINT_MAX_32_BITS
272 typedef unsigned short word32;
273 # else
274 # if UCHAR_MAX == UINT_MAX_32_BITS
275 typedef unsigned char word32;
276 # else
277 "No 32-bit type available!"
278 # endif
279 # endif
280 # endif
281 #endif
283 /* Size of the state tables to use. (You may change ISAAC_LOG) */
284 #define ISAAC_LOG 8
285 #define ISAAC_WORDS (1 << ISAAC_LOG)
286 #define ISAAC_BYTES (ISAAC_WORDS * sizeof (word32))
288 /* RNG state variables */
289 struct isaac_state
291 word32 mm[ISAAC_WORDS]; /* Main state array */
292 word32 iv[8]; /* Seeding initial vector */
293 word32 a, b, c; /* Extra index variables */
296 /* This index operation is more efficient on many processors */
297 #define ind(mm, x) \
298 (* (word32 *) ((char *) (mm) + ((x) & (ISAAC_WORDS - 1) * sizeof (word32))))
301 * The central step. This uses two temporaries, x and y. mm is the
302 * whole state array, while m is a pointer to the current word. off is
303 * the offset from m to the word ISAAC_WORDS/2 words away in the mm array,
304 * i.e. +/- ISAAC_WORDS/2.
306 #define isaac_step(mix, a, b, mm, m, off, r) \
308 a = ((a) ^ (mix)) + (m)[off], \
309 x = *(m), \
310 *(m) = y = ind (mm, x) + (a) + (b), \
311 *(r) = b = ind (mm, (y) >> ISAAC_LOG) + x \
315 * Refill the entire R array, and update S.
317 static void
318 isaac_refill (struct isaac_state *s, word32 r[/* ISAAC_WORDS */])
320 register word32 a, b; /* Caches of a and b */
321 register word32 x, y; /* Temps needed by isaac_step macro */
322 register word32 *m = s->mm; /* Pointer into state array */
324 a = s->a;
325 b = s->b + (++s->c);
329 isaac_step (a << 13, a, b, s->mm, m, ISAAC_WORDS / 2, r);
330 isaac_step (a >> 6, a, b, s->mm, m + 1, ISAAC_WORDS / 2, r + 1);
331 isaac_step (a << 2, a, b, s->mm, m + 2, ISAAC_WORDS / 2, r + 2);
332 isaac_step (a >> 16, a, b, s->mm, m + 3, ISAAC_WORDS / 2, r + 3);
333 r += 4;
335 while ((m += 4) < s->mm + ISAAC_WORDS / 2);
338 isaac_step (a << 13, a, b, s->mm, m, -ISAAC_WORDS / 2, r);
339 isaac_step (a >> 6, a, b, s->mm, m + 1, -ISAAC_WORDS / 2, r + 1);
340 isaac_step (a << 2, a, b, s->mm, m + 2, -ISAAC_WORDS / 2, r + 2);
341 isaac_step (a >> 16, a, b, s->mm, m + 3, -ISAAC_WORDS / 2, r + 3);
342 r += 4;
344 while ((m += 4) < s->mm + ISAAC_WORDS);
345 s->a = a;
346 s->b = b;
350 * The basic seed-scrambling step for initialization, based on Bob
351 * Jenkins' 256-bit hash.
353 #define mix(a,b,c,d,e,f,g,h) \
354 ( a ^= b << 11, d += a, \
355 b += c, b ^= c >> 2, e += b, \
356 c += d, c ^= d << 8, f += c, \
357 d += e, d ^= e >> 16, g += d, \
358 e += f, e ^= f << 10, h += e, \
359 f += g, f ^= g >> 4, a += f, \
360 g += h, g ^= h << 8, b += g, \
361 h += a, h ^= a >> 9, c += h, \
362 a += b )
364 /* The basic ISAAC initialization pass. */
365 static void
366 isaac_mix (struct isaac_state *s, word32 const seed[/* ISAAC_WORDS */])
368 int i;
369 word32 a = s->iv[0];
370 word32 b = s->iv[1];
371 word32 c = s->iv[2];
372 word32 d = s->iv[3];
373 word32 e = s->iv[4];
374 word32 f = s->iv[5];
375 word32 g = s->iv[6];
376 word32 h = s->iv[7];
378 for (i = 0; i < ISAAC_WORDS; i += 8)
380 a += seed[i];
381 b += seed[i + 1];
382 c += seed[i + 2];
383 d += seed[i + 3];
384 e += seed[i + 4];
385 f += seed[i + 5];
386 g += seed[i + 6];
387 h += seed[i + 7];
389 mix (a, b, c, d, e, f, g, h);
391 s->mm[i] = a;
392 s->mm[i + 1] = b;
393 s->mm[i + 2] = c;
394 s->mm[i + 3] = d;
395 s->mm[i + 4] = e;
396 s->mm[i + 5] = f;
397 s->mm[i + 6] = g;
398 s->mm[i + 7] = h;
401 s->iv[0] = a;
402 s->iv[1] = b;
403 s->iv[2] = c;
404 s->iv[3] = d;
405 s->iv[4] = e;
406 s->iv[5] = f;
407 s->iv[6] = g;
408 s->iv[7] = h;
411 #if 0 /* Provided for reference only; not used in this code */
413 * Initialize the ISAAC RNG with the given seed material.
414 * Its size MUST be a multiple of ISAAC_BYTES, and may be
415 * stored in the s->mm array.
417 * This is a generalization of the original ISAAC initialization code
418 * to support larger seed sizes. For seed sizes of 0 and ISAAC_BYTES,
419 * it is identical.
421 static void
422 isaac_init (struct isaac_state *s, word32 const *seed, size_t seedsize)
424 static word32 const iv[8] =
426 0x1367df5a, 0x95d90059, 0xc3163e4b, 0x0f421ad8,
427 0xd92a4a78, 0xa51a3c49, 0xc4efea1b, 0x30609119};
428 int i;
430 # if 0
431 /* The initialization of iv is a precomputed form of: */
432 for (i = 0; i < 7; i++)
433 iv[i] = 0x9e3779b9; /* the golden ratio */
434 for (i = 0; i < 4; ++i) /* scramble it */
435 mix (iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
436 # endif
437 s->a = s->b = s->c = 0;
439 for (i = 0; i < 8; i++)
440 s->iv[i] = iv[i];
442 if (seedsize)
444 /* First pass (as in reference ISAAC code) */
445 isaac_mix (s, seed);
446 /* Second and subsequent passes (extension to ISAAC) */
447 while (seedsize -= ISAAC_BYTES)
449 seed += ISAAC_WORDS;
450 for (i = 0; i < ISAAC_WORDS; i++)
451 s->mm[i] += seed[i];
452 isaac_mix (s, s->mm);
455 else
457 /* The no seed case (as in reference ISAAC code) */
458 for (i = 0; i < ISAAC_WORDS; i++)
459 s->mm[i] = 0;
462 /* Final pass */
463 isaac_mix (s, s->mm);
465 #endif
467 /* Start seeding an ISAAC structire */
468 static void
469 isaac_seed_start (struct isaac_state *s)
471 static word32 const iv[8] =
473 0x1367df5a, 0x95d90059, 0xc3163e4b, 0x0f421ad8,
474 0xd92a4a78, 0xa51a3c49, 0xc4efea1b, 0x30609119
476 int i;
478 #if 0
479 /* The initialization of iv is a precomputed form of: */
480 for (i = 0; i < 7; i++)
481 iv[i] = 0x9e3779b9; /* the golden ratio */
482 for (i = 0; i < 4; ++i) /* scramble it */
483 mix (iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
484 #endif
485 for (i = 0; i < 8; i++)
486 s->iv[i] = iv[i];
487 /* We could initialize s->mm to zero, but why bother? */
489 /* s->c gets used for a data pointer during the seeding phase */
490 s->a = s->b = s->c = 0;
493 /* Add a buffer of seed material */
494 static void
495 isaac_seed_data (struct isaac_state *s, void const *buf, size_t size)
497 unsigned char *p;
498 size_t avail;
499 size_t i;
501 avail = sizeof s->mm - (size_t) s->c; /* s->c is used as a write pointer */
503 /* Do any full buffers that are necessary */
504 while (size > avail)
506 p = (unsigned char *) s->mm + s->c;
507 for (i = 0; i < avail; i++)
508 p[i] ^= ((unsigned char const *) buf)[i];
509 buf = (char const *) buf + avail;
510 size -= avail;
511 isaac_mix (s, s->mm);
512 s->c = 0;
513 avail = sizeof s->mm;
516 /* And the final partial block */
517 p = (unsigned char *) s->mm + s->c;
518 for (i = 0; i < size; i++)
519 p[i] ^= ((unsigned char const *) buf)[i];
520 s->c = (word32) size;
524 /* End of seeding phase; get everything ready to produce output. */
525 static void
526 isaac_seed_finish (struct isaac_state *s)
528 isaac_mix (s, s->mm);
529 isaac_mix (s, s->mm);
530 /* Now reinitialize c to start things off right */
531 s->c = 0;
533 #define ISAAC_SEED(s,x) isaac_seed_data (s, &(x), sizeof (x))
536 #if __GNUC__ >= 2 && (__i386__ || __alpha__)
538 * Many processors have very-high-resolution timer registers,
539 * The timer registers can be made inaccessible, so we have to deal with the
540 * possibility of SIGILL while we're working.
542 static jmp_buf env;
543 static RETSIGTYPE
544 sigill_handler (int signum)
546 (void) signum;
547 longjmp (env, 1); /* Trivial, just return an indication that it happened */
550 /* FIXME: find a better way.
551 This signal-handling code may well end up being ripped out eventually.
552 An example of how fragile it is, on an i586-sco-sysv5uw7.0.1 system, with
553 gcc-2.95.3pl1, the "rdtsc" instruction causes a segmentation violation.
554 So now, the code catches SIGSEGV. It'd probably be better to remove all
555 of that mess and find a better source of random data. Patches welcome. */
557 static void
558 isaac_seed_machdep (struct isaac_state *s)
560 RETSIGTYPE (*old_handler[2]) (int);
562 /* This is how one does try/except in C */
563 old_handler[0] = signal (SIGILL, sigill_handler);
564 old_handler[1] = signal (SIGSEGV, sigill_handler);
565 if (setjmp (env)) /* ANSI: Must be entire controlling expression */
567 signal (SIGILL, old_handler[0]);
568 signal (SIGSEGV, old_handler[1]);
570 else
572 # if __i386__
573 word32 t[2];
574 __asm__ __volatile__ ("rdtsc" : "=a" (t[0]), "=d" (t[1]));
575 # endif
576 # if __alpha__
577 unsigned long t;
578 __asm__ __volatile__ ("rpcc %0" : "=r" (t));
579 # endif
580 # if _ARCH_PPC
581 /* Code not used because this instruction is available only on first-
582 generation PPCs and evokes a SIGBUS on some Linux 2.4 kernels. */
583 word32 t;
584 __asm__ __volatile__ ("mfspr %0,22" : "=r" (t));
585 # endif
586 # if __mips
587 /* Code not used because this is not accessible from userland */
588 word32 t;
589 __asm__ __volatile__ ("mfc0\t%0,$9" : "=r" (t));
590 # endif
591 # if __sparc__
592 /* This doesn't compile on all platforms yet. How to fix? */
593 unsigned long t;
594 __asm__ __volatile__ ("rd %%tick, %0" : "=r" (t));
595 # endif
596 signal (SIGILL, old_handler[0]);
597 signal (SIGSEGV, old_handler[1]);
598 isaac_seed_data (s, &t, sizeof t);
602 #else /* !(__i386__ || __alpha__) */
604 /* Do-nothing stub */
605 # define isaac_seed_machdep(s) (void) 0
607 #endif /* !(__i386__ || __alpha__) */
611 * Get seed material. 16 bytes (128 bits) is plenty, but if we have
612 * /dev/urandom, we get 32 bytes = 256 bits for complete overkill.
614 static void
615 isaac_seed (struct isaac_state *s)
617 isaac_seed_start (s);
619 { pid_t t = getpid (); ISAAC_SEED (s, t); }
620 { pid_t t = getppid (); ISAAC_SEED (s, t); }
621 { uid_t t = getuid (); ISAAC_SEED (s, t); }
622 { gid_t t = getgid (); ISAAC_SEED (s, t); }
625 #if HAVE_GETHRTIME
626 hrtime_t t = gethrtime ();
627 ISAAC_SEED (s, t);
628 #else
629 # if HAVE_CLOCK_GETTIME /* POSIX ns-resolution */
630 struct timespec t;
631 clock_gettime (CLOCK_REALTIME, &t);
632 # else
633 # if HAVE_GETTIMEOFDAY
634 struct timeval t;
635 gettimeofday (&t, (struct timezone *) 0);
636 # else
637 time_t t;
638 t = time (NULL);
639 # endif
640 # endif
641 #endif
642 ISAAC_SEED (s, t);
645 isaac_seed_machdep (s);
648 char buf[32];
649 int fd = open ("/dev/urandom", O_RDONLY | O_NOCTTY);
650 if (fd >= 0)
652 read (fd, buf, 32);
653 close (fd);
654 isaac_seed_data (s, buf, 32);
656 else
658 fd = open ("/dev/random", O_RDONLY | O_NONBLOCK | O_NOCTTY);
659 if (fd >= 0)
661 /* /dev/random is more precious, so use less */
662 read (fd, buf, 16);
663 close (fd);
664 isaac_seed_data (s, buf, 16);
669 isaac_seed_finish (s);
672 /* Single-word RNG built on top of ISAAC */
673 struct irand_state
675 word32 r[ISAAC_WORDS];
676 unsigned numleft;
677 struct isaac_state *s;
680 static void
681 irand_init (struct irand_state *r, struct isaac_state *s)
683 r->numleft = 0;
684 r->s = s;
688 * We take from the end of the block deliberately, so if we need
689 * only a small number of values, we choose the final ones which are
690 * marginally better mixed than the initial ones.
692 static word32
693 irand32 (struct irand_state *r)
695 if (!r->numleft)
697 isaac_refill (r->s, r->r);
698 r->numleft = ISAAC_WORDS;
700 return r->r[--r->numleft];
704 * Return a uniformly distributed random number between 0 and n,
705 * inclusive. Thus, the result is modulo n+1.
707 * Theory of operation: as x steps through every possible 32-bit number,
708 * x % n takes each value at least 2^32 / n times (rounded down), but
709 * the values less than 2^32 % n are taken one additional time. Thus,
710 * x % n is not perfectly uniform. To fix this, the values of x less
711 * than 2^32 % n are disallowed, and if the RNG produces one, we ask
712 * for a new value.
714 static word32
715 irand_mod (struct irand_state *r, word32 n)
717 word32 x;
718 word32 lim;
720 if (!++n)
721 return irand32 (r);
723 lim = -n % n; /* == (2**32-n) % n == 2**32 % n */
726 x = irand32 (r);
728 while (x < lim);
729 return x % n;
733 * Fill a buffer with a fixed pattern.
735 * The buffer must be at least 3 bytes long, even if
736 * size is less. Larger sizes are filled exactly.
738 static void
739 fillpattern (int type, unsigned char *r, size_t size)
741 size_t i;
742 unsigned bits = type & 0xfff;
744 bits |= bits << 12;
745 ((unsigned char *) r)[0] = (bits >> 4) & 255;
746 ((unsigned char *) r)[1] = (bits >> 8) & 255;
747 ((unsigned char *) r)[2] = bits & 255;
748 for (i = 3; i < size / 2; i *= 2)
749 memcpy ((char *) r + i, (char *) r, i);
750 if (i < size)
751 memcpy ((char *) r + i, (char *) r, size - i);
753 /* Invert the first bit of every 512-byte sector. */
754 if (type & 0x1000)
755 for (i = 0; i < size; i += 512)
756 r[i] ^= 0x80;
760 * Fill a buffer, R (of size SIZE_MAX), with random data.
761 * SIZE is rounded UP to a multiple of ISAAC_BYTES.
763 static void
764 fillrand (struct isaac_state *s, word32 *r, size_t size_max, size_t size)
766 size = (size + ISAAC_BYTES - 1) / ISAAC_BYTES;
767 assert (size <= size_max);
769 while (size--)
771 isaac_refill (s, r);
772 r += ISAAC_WORDS;
777 * Generate a 6-character (+ nul) pass name string
778 * FIXME: allow translation of "random".
780 #define PASS_NAME_SIZE 7
781 static void
782 passname (unsigned char const *data, char name[PASS_NAME_SIZE])
784 if (data)
785 sprintf (name, "%02x%02x%02x", data[0], data[1], data[2]);
786 else
787 memcpy (name, "random", PASS_NAME_SIZE);
791 * Do pass number k of n, writing "size" bytes of the given pattern "type"
792 * to the file descriptor fd. Qname, k and n are passed in only for verbose
793 * progress message purposes. If n == 0, no progress messages are printed.
795 * If *sizep == -1, the size is unknown, and it will be filled in as soon
796 * as writing fails.
798 static int
799 dopass (int fd, char const *qname, off_t *sizep, int type,
800 struct isaac_state *s, unsigned long k, unsigned long n)
802 off_t size = *sizep;
803 off_t offset; /* Current file posiiton */
804 time_t thresh IF_LINT (= 0); /* Time to maybe print next status update */
805 time_t now = 0; /* Current time */
806 size_t lim; /* Amount of data to try writing */
807 size_t soff; /* Offset into buffer for next write */
808 ssize_t ssize; /* Return value from write */
809 #if ISAAC_WORDS > 1024
810 word32 r[ISAAC_WORDS * 3]; /* Multiple of 4K and of pattern size */
811 #else
812 word32 r[1024 * 3]; /* Multiple of 4K and of pattern size */
813 #endif
814 char pass_string[PASS_NAME_SIZE]; /* Name of current pass */
816 /* Printable previous offset into the file */
817 char previous_offset_buf[LONGEST_HUMAN_READABLE + 1];
818 char const *previous_human_offset IF_LINT (= 0);
820 if (lseek (fd, (off_t) 0, SEEK_SET) == -1)
822 error (0, errno, _("%s: cannot rewind"), qname);
823 return -1;
826 /* Constant fill patterns need only be set up once. */
827 if (type >= 0)
829 lim = sizeof r;
830 if ((off_t) lim > size && size != -1)
832 lim = (size_t) size;
834 fillpattern (type, (unsigned char *) r, lim);
835 passname ((unsigned char *) r, pass_string);
837 else
839 passname (0, pass_string);
842 /* Set position if first status update */
843 if (n)
845 error (0, 0, _("%s: pass %lu/%lu (%s)..."), qname, k, n, pass_string);
846 thresh = time (NULL) + VERBOSE_UPDATE;
847 previous_human_offset = "";
850 offset = 0;
851 for (;;)
853 /* How much to write this time? */
854 lim = sizeof r;
855 if ((off_t) lim > size - offset && size != -1)
857 if (size < offset)
858 break;
859 lim = (size_t) (size - offset);
860 if (!lim)
861 break;
863 if (type < 0)
864 fillrand (s, r, sizeof r, lim);
865 /* Loop to retry partial writes. */
866 for (soff = 0; soff < lim; soff += ssize)
868 ssize = write (fd, (char *) r + soff, lim - soff);
869 if (ssize <= 0)
871 if ((ssize == 0 || errno == ENOSPC)
872 && size == -1)
874 /* Ah, we have found the end of the file */
875 *sizep = size = offset + soff;
876 break;
878 else
880 int errnum = errno;
881 char buf[LONGEST_HUMAN_READABLE + 1];
882 error (0, errnum, _("%s: error writing at offset %s"),
883 qname,
884 human_readable ((uintmax_t) (offset + soff),
885 buf, 1, 1));
887 * I sometimes use shred on bad media, before throwing it
888 * out. Thus, I don't want it to give up on bad blocks.
889 * This code assumes 512-byte blocks and tries to skip
890 * over them. It works because lim is always a multiple
891 * of 512, except at the end.
893 if (errnum == EIO && soff % 512 == 0 && lim >= soff + 512
894 && size != -1)
896 if (lseek (fd, (off_t) (offset + soff + 512), SEEK_SET)
897 != -1)
899 soff += 512;
900 continue;
902 error (0, errno, "%s: lseek", qname);
904 return -1;
909 /* Okay, we have written "soff" bytes. */
911 if (offset + soff < offset)
913 error (0, 0, _("%s: file too large"), qname);
914 return -1;
917 offset += soff;
919 /* Time to print progress? */
920 if (n
921 && ((offset == size && *previous_human_offset)
922 || thresh <= (now = time (NULL))))
924 char offset_buf[LONGEST_HUMAN_READABLE + 1];
925 char size_buf[LONGEST_HUMAN_READABLE + 1];
926 char const *human_offset
927 = human_readable ((uintmax_t) offset, offset_buf, 1,
928 OUTPUT_BLOCK_SIZE);
930 if (offset == size
931 || !STREQ (previous_human_offset, human_offset))
933 if (size == -1)
934 error (0, 0, _("%s: pass %lu/%lu (%s)...%s"),
935 qname, k, n, pass_string, human_offset);
936 else
938 int percent = (size == 0
939 ? 100
940 : (offset <= TYPE_MAXIMUM (uintmax_t) / 100
941 ? offset * (uintmax_t) 100 / size
942 : offset / (size / 100)));
943 error (0, 0, _("%s: pass %lu/%lu (%s)...%s/%s %d%%"),
944 qname, k, n, pass_string, human_offset,
945 human_readable ((uintmax_t) size, size_buf, 1,
946 OUTPUT_BLOCK_SIZE),
947 percent);
950 strcpy (previous_offset_buf, human_offset);
951 previous_human_offset = previous_offset_buf;
952 thresh = now + VERBOSE_UPDATE;
955 * Force periodic syncs to keep displayed progress accurate
956 * FIXME: Should these be present even if -v is not enabled,
957 * to keep the buffer cache from filling with dirty pages?
958 * It's a common problem with programs that do lots of writes,
959 * like mkfs.
961 if (fdatasync (fd) < 0 && fsync (fd) < 0)
963 error (0, errno, "%s: fsync", qname);
964 return -1;
970 /* Force what we just wrote to hit the media. */
971 if (fdatasync (fd) < 0 && fsync (fd) < 0)
973 error (0, errno, "%s: fsync", qname);
974 return -1;
976 return 0;
980 * The passes start and end with a random pass, and the passes in between
981 * are done in random order. The idea is to deprive someone trying to
982 * reverse the process of knowledge of the overwrite patterns, so they
983 * have the additional step of figuring out what was done to the disk
984 * before they can try to reverse or cancel it.
986 * First, all possible 1-bit patterns. There are two of them.
987 * Then, all possible 2-bit patterns. There are four, but the two
988 * which are also 1-bit patterns can be omitted.
989 * Then, all possible 3-bit patterns. Likewise, 8-2 = 6.
990 * Then, all possible 4-bit patterns. 16-4 = 12.
992 * The basic passes are:
993 * 1-bit: 0x000, 0xFFF
994 * 2-bit: 0x555, 0xAAA
995 * 3-bit: 0x249, 0x492, 0x924, 0x6DB, 0xB6D, 0xDB6 (+ 1-bit)
996 * 100100100100 110110110110
997 * 9 2 4 D B 6
998 * 4-bit: 0x111, 0x222, 0x333, 0x444, 0x666, 0x777,
999 * 0x888, 0x999, 0xBBB, 0xCCC, 0xDDD, 0xEEE (+ 1-bit, 2-bit)
1000 * Adding three random passes at the beginning, middle and end
1001 * produces the default 25-pass structure.
1003 * The next extension would be to 5-bit and 6-bit patterns.
1004 * There are 30 uncovered 5-bit patterns and 64-8-2 = 46 uncovered
1005 * 6-bit patterns, so they would increase the time required
1006 * significantly. 4-bit patterns are enough for most purposes.
1008 * The main gotcha is that this would require a trickier encoding,
1009 * since lcm(2,3,4) = 12 bits is easy to fit into an int, but
1010 * lcm(2,3,4,5) = 60 bits is not.
1012 * One extension that is included is to complement the first bit in each
1013 * 512-byte block, to alter the phase of the encoded data in the more
1014 * complex encodings. This doesn't apply to MFM, so the 1-bit patterns
1015 * are considered part of the 3-bit ones and the 2-bit patterns are
1016 * considered part of the 4-bit patterns.
1019 * How does the generalization to variable numbers of passes work?
1021 * Here's how...
1022 * Have an ordered list of groups of passes. Each group is a set.
1023 * Take as many groups as will fit, plus a random subset of the
1024 * last partial group, and place them into the passes list.
1025 * Then shuffle the passes list into random order and use that.
1027 * One extra detail: if we can't include a large enough fraction of the
1028 * last group to be interesting, then just substitute random passes.
1030 * If you want more passes than the entire list of groups can
1031 * provide, just start repeating from the beginning of the list.
1033 static int const
1034 patterns[] =
1036 -2, /* 2 random passes */
1037 2, 0x000, 0xFFF, /* 1-bit */
1038 2, 0x555, 0xAAA, /* 2-bit */
1039 -1, /* 1 random pass */
1040 6, 0x249, 0x492, 0x6DB, 0x924, 0xB6D, 0xDB6, /* 3-bit */
1041 12, 0x111, 0x222, 0x333, 0x444, 0x666, 0x777,
1042 0x888, 0x999, 0xBBB, 0xCCC, 0xDDD, 0xEEE, /* 4-bit */
1043 -1, /* 1 random pass */
1044 /* The following patterns have the frst bit per block flipped */
1045 8, 0x1000, 0x1249, 0x1492, 0x16DB, 0x1924, 0x1B6D, 0x1DB6, 0x1FFF,
1046 14, 0x1111, 0x1222, 0x1333, 0x1444, 0x1555, 0x1666, 0x1777,
1047 0x1888, 0x1999, 0x1AAA, 0x1BBB, 0x1CCC, 0x1DDD, 0x1EEE,
1048 -1, /* 1 random pass */
1049 0 /* End */
1053 * Generate a random wiping pass pattern with num passes.
1054 * This is a two-stage process. First, the passes to include
1055 * are chosen, and then they are shuffled into the desired
1056 * order.
1058 static void
1059 genpattern (int *dest, size_t num, struct isaac_state *s)
1061 struct irand_state r;
1062 size_t randpasses;
1063 int const *p;
1064 int *d;
1065 size_t n;
1066 size_t accum, top, swap;
1067 int k;
1069 if (!num)
1070 return;
1072 irand_init (&r, s);
1074 /* Stage 1: choose the passes to use */
1075 p = patterns;
1076 randpasses = 0;
1077 d = dest; /* Destination for generated pass list */
1078 n = num; /* Passes remaining to fill */
1080 for (;;)
1082 k = *p++; /* Block descriptor word */
1083 if (!k)
1084 { /* Loop back to the beginning */
1085 p = patterns;
1087 else if (k < 0)
1088 { /* -k random passes */
1089 k = -k;
1090 if ((size_t) k >= n)
1092 randpasses += n;
1093 n = 0;
1094 break;
1096 randpasses += k;
1097 n -= k;
1099 else if ((size_t) k <= n)
1100 { /* Full block of patterns */
1101 memcpy (d, p, k * sizeof (int));
1102 p += k;
1103 d += k;
1104 n -= k;
1106 else if (n < 2 || 3 * n < (size_t) k)
1107 { /* Finish with random */
1108 randpasses += n;
1109 break;
1111 else
1112 { /* Pad out with k of the n available */
1115 if (n == (size_t) k-- || irand_mod (&r, k) < n)
1117 *d++ = *p;
1118 n--;
1120 p++;
1122 while (n);
1123 break;
1126 top = num - randpasses; /* Top of initialized data */
1127 /* assert (d == dest+top); */
1130 * We now have fixed patterns in the dest buffer up to
1131 * "top", and we need to scramble them, with "randpasses"
1132 * random passes evenly spaced among them.
1134 * We want one at the beginning, one at the end, and
1135 * evenly spaced in between. To do this, we basically
1136 * use Bresenham's line draw (a.k.a DDA) algorithm
1137 * to draw a line with slope (randpasses-1)/(num-1).
1138 * (We use a positive accumulator and count down to
1139 * do this.)
1141 * So for each desired output value, we do the following:
1142 * - If it should be a random pass, copy the pass type
1143 * to top++, out of the way of the other passes, and
1144 * set the current pass to -1 (random).
1145 * - If it should be a normal pattern pass, choose an
1146 * entry at random between here and top-1 (inclusive)
1147 * and swap the current entry with that one.
1149 randpasses--; /* To speed up later math */
1150 accum = randpasses; /* Bresenham DDA accumulator */
1151 for (n = 0; n < num; n++)
1153 if (accum <= randpasses)
1155 accum += num - 1;
1156 dest[top++] = dest[n];
1157 dest[n] = -1;
1159 else
1161 swap = n + irand_mod (&r, top - n - 1);
1162 k = dest[n];
1163 dest[n] = dest[swap];
1164 dest[swap] = k;
1166 accum -= randpasses;
1168 /* assert (top == num); */
1170 memset (&r, 0, sizeof r); /* Wipe this on general principles */
1174 * The core routine to actually do the work. This overwrites the first
1175 * size bytes of the given fd. Returns -1 on error, 0 on success.
1177 static int
1178 do_wipefd (int fd, char const *qname, struct isaac_state *s,
1179 struct Options const *flags)
1181 size_t i;
1182 struct stat st;
1183 off_t size; /* Size to write, size to read */
1184 unsigned long n; /* Number of passes for printing purposes */
1185 int *passarray;
1187 n = 0; /* dopass takes n -- 0 to mean "don't print progress" */
1188 if (flags->verbose)
1189 n = flags->n_iterations + ((flags->zero_fill) != 0);
1191 if (fstat (fd, &st))
1193 error (0, errno, "%s: fstat", qname);
1194 return -1;
1197 /* If we know that we can't possibly shred the file, give up now.
1198 Otherwise, we may go into a infinite loop writing data before we
1199 find that we can't rewind the device. */
1200 if ((S_ISCHR (st.st_mode) && isatty (fd))
1201 || S_ISFIFO (st.st_mode)
1202 || S_ISSOCK (st.st_mode))
1204 error (0, 0, _("%s: invalid file type"), qname);
1205 return -1;
1208 /* Allocate pass array */
1209 passarray = xmalloc (flags->n_iterations * sizeof (int));
1211 size = flags->size;
1212 if (size == -1)
1214 /* Accept a length of zero only if it's a regular file.
1215 For any other type of file, try to get the size another way. */
1216 if (S_ISREG (st.st_mode))
1218 size = st.st_size;
1219 if (size < 0)
1221 error (0, 0, _("%s: file has negative size"), qname);
1222 return -1;
1225 else
1227 size = lseek (fd, (off_t) 0, SEEK_END);
1228 if (size <= 0)
1230 /* We are unable to determine the length, up front.
1231 Let dopass do that as part of its first iteration. */
1232 size = -1;
1236 if (0 <= size && !(flags->exact))
1238 size += ST_BLKSIZE (st) - 1 - (size - 1) % ST_BLKSIZE (st);
1240 /* If in rounding up, we've just overflowed, use the maximum. */
1241 if (size < 0)
1242 size = TYPE_MAXIMUM (off_t);
1246 /* Schedule the passes in random order. */
1247 genpattern (passarray, flags->n_iterations, s);
1249 /* Do the work */
1250 for (i = 0; i < flags->n_iterations; i++)
1252 if (dopass (fd, qname, &size, passarray[i], s, i + 1, n) < 0)
1254 memset (passarray, 0, flags->n_iterations * sizeof (int));
1255 free (passarray);
1256 return -1;
1260 memset (passarray, 0, flags->n_iterations * sizeof (int));
1261 free (passarray);
1263 if (flags->zero_fill)
1264 if (dopass (fd, qname, &size, 0, s, flags->n_iterations + 1, n) < 0)
1265 return -1;
1267 /* Okay, now deallocate the data. The effect of ftruncate on
1268 non-regular files is unspecified, so don't worry about any
1269 errors reported for them. */
1270 if (flags->remove_file && ftruncate (fd, (off_t) 0) != 0
1271 && S_ISREG (st.st_mode))
1273 error (0, errno, _("%s: error truncating"), qname);
1274 return -1;
1277 return 0;
1280 /* A wrapper with a little more checking for fds on the command line */
1281 static int
1282 wipefd (int fd, char const *qname, struct isaac_state *s,
1283 struct Options const *flags)
1285 int fd_flags = fcntl (fd, F_GETFL);
1287 if (fd_flags < 0)
1289 error (0, errno, "%s: fcntl", qname);
1290 return -1;
1292 if (fd_flags & O_APPEND)
1294 error (0, 0, _("%s: cannot shred append-only file descriptor"), qname);
1295 return -1;
1297 return do_wipefd (fd, qname, s, flags);
1300 /* --- Name-wiping code --- */
1302 /* Characters allowed in a file name - a safe universal set. */
1303 static char const nameset[] =
1304 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_+=%@#.";
1307 * This increments the name, considering it as a big-endian base-N number
1308 * with the digits taken from nameset. Characters not in the nameset
1309 * are considered to come before nameset[0].
1311 * It's not obvious, but this will explode if name[0..len-1] contains
1312 * any 0 bytes.
1314 * This returns the carry (1 on overflow).
1316 static int
1317 incname (char *name, unsigned len)
1319 char const *p;
1321 if (!len)
1322 return 1;
1324 p = strchr (nameset, name[--len]);
1325 /* If the character is not found, replace it with a 0 digit */
1326 if (!p)
1328 name[len] = nameset[0];
1329 return 0;
1331 /* If this character has a successor, use it */
1332 if (p[1])
1334 name[len] = p[1];
1335 return 0;
1337 /* Otherwise, set this digit to 0 and increment the prefix */
1338 name[len] = nameset[0];
1339 return incname (name, len);
1343 * Repeatedly rename a file with shorter and shorter names,
1344 * to obliterate all traces of the file name on any system that
1345 * adds a trailing delimiter to on-disk file names and reuses
1346 * the same directory slot. Finally, unlink it.
1347 * The passed-in filename is modified in place to the new filename.
1348 * (Which is unlinked if this function succeeds, but is still present if
1349 * it fails for some reason.)
1351 * The main loop is written carefully to not get stuck if all possible
1352 * names of a given length are occupied. It counts down the length from
1353 * the original to 0. While the length is non-zero, it tries to find an
1354 * unused file name of the given length. It continues until either the
1355 * name is available and the rename succeeds, or it runs out of names
1356 * to try (incname wraps and returns 1). Finally, it unlinks the file.
1358 * The unlink is Unix-specific, as ANSI-standard remove has more
1359 * portability problems with C libraries making it "safe". rename
1360 * is ANSI-standard.
1362 * To force the directory data out, we try to open the directory and
1363 * invoke fdatasync on it. This is rather non-standard, so we don't
1364 * insist that it works, just fall back to a global sync in that case.
1365 * This is fairly significantly Unix-specific. Of course, on any
1366 * filesystem with synchronous metadata updates, this is unnecessary.
1368 static int
1369 wipename (char *oldname, char const *qoldname, struct Options const *flags)
1371 char *newname, *base; /* Base points to filename part of newname */
1372 unsigned len;
1373 int first = 1;
1374 int err;
1375 int dir_fd; /* Try to open directory to sync *it* */
1377 newname = xstrdup (oldname);
1378 if (flags->verbose)
1379 error (0, 0, _("%s: removing"), qoldname);
1381 /* Find the file name portion */
1382 base = strrchr (newname, '/');
1383 /* Temporary hackery to get a directory fd */
1384 if (base)
1386 *base = '\0';
1387 dir_fd = open (newname, O_RDONLY | O_NOCTTY);
1388 *base = '/';
1390 else
1392 dir_fd = open (".", O_RDONLY | O_NOCTTY);
1394 base = base ? base + 1 : newname;
1395 len = strlen (base);
1397 while (len)
1399 memset (base, nameset[0], len);
1400 base[len] = 0;
1403 struct stat st;
1404 if (lstat (newname, &st) < 0)
1406 if (rename (oldname, newname) == 0)
1408 if (dir_fd < 0
1409 || (fdatasync (dir_fd) < 0 && fsync (dir_fd) < 0))
1410 sync (); /* Force directory out */
1411 if (flags->verbose)
1414 * People seem to understand this better than talking
1415 * about renaming oldname. newname doesn't need
1416 * quoting because we picked it. oldname needs to
1417 * be quoted only the first time.
1419 char const *old = (first ? qoldname : oldname);
1420 error (0, 0, _("%s: renamed to %s"), old, newname);
1421 first = 0;
1423 memcpy (oldname + (base - newname), base, len + 1);
1424 break;
1426 else
1428 /* The rename failed: give up on this length. */
1429 break;
1432 else
1434 /* newname exists, so increment BASE so we use another */
1437 while (!incname (base, len));
1438 len--;
1440 free (newname);
1441 err = unlink (oldname);
1442 if (dir_fd < 0 || (fdatasync (dir_fd) < 0 && fsync (dir_fd) < 0))
1443 sync ();
1444 close (dir_fd);
1445 if (!err && flags->verbose)
1446 error (0, 0, _("%s: removed"), qoldname);
1447 return err;
1451 * Finally, the function that actually takes a filename and grinds
1452 * it into hamburger.
1454 * FIXME
1455 * Detail to note: since we do not restore errno to EACCES after
1456 * a failed chmod, we end up printing the error code from the chmod.
1457 * This is actually the error that stopped us from proceeding, so
1458 * it's arguably the right one, and in practice it'll be either EACCES
1459 * again or EPERM, which both give similar error messages.
1460 * Does anyone disagree?
1462 static int
1463 wipefile (char *name, char const *qname,
1464 struct isaac_state *s, struct Options const *flags)
1466 int err, fd;
1468 fd = open (name, O_WRONLY | O_NOCTTY);
1469 if (fd < 0)
1471 if (errno == EACCES && flags->force)
1473 if (chmod (name, S_IWUSR) >= 0) /* 0200, user-write-only */
1474 fd = open (name, O_WRONLY | O_NOCTTY);
1476 else if ((errno == ENOENT || errno == ENOTDIR)
1477 && strncmp (name, "/dev/fd/", 8) == 0)
1479 /* We accept /dev/fd/# even if the OS doesn't support it */
1480 int errnum = errno;
1481 unsigned long num;
1482 char *p;
1483 errno = 0;
1484 num = strtoul (name + 8, &p, 10);
1485 /* If it's completely decimal with no leading zeros... */
1486 if (errno == 0 && !*p && num <= INT_MAX &&
1487 (('1' <= name[8] && name[8] <= '9')
1488 || (name[8] == '0' && !name[9])))
1490 return wipefd ((int) num, qname, s, flags);
1492 errno = errnum;
1495 if (fd < 0)
1497 error (0, errno, "%s", qname);
1498 return -1;
1501 err = do_wipefd (fd, qname, s, flags);
1502 if (close (fd) != 0)
1504 error (0, 0, "%s: close", qname);
1505 err = -1;
1507 if (err == 0 && flags->remove_file)
1509 err = wipename (name, qname, flags);
1510 if (err < 0)
1511 error (0, 0, _("%s: cannot remove"), qname);
1513 return err;
1517 main (int argc, char **argv)
1519 struct isaac_state s;
1520 int err = 0;
1521 struct Options flags;
1522 char **file;
1523 int n_files;
1524 int c;
1525 int i;
1527 program_name = argv[0];
1528 setlocale (LC_ALL, "");
1529 bindtextdomain (PACKAGE, LOCALEDIR);
1530 textdomain (PACKAGE);
1532 atexit (close_stdout);
1534 isaac_seed (&s);
1536 memset (&flags, 0, sizeof flags);
1538 flags.n_iterations = DEFAULT_PASSES;
1539 flags.size = -1;
1541 while ((c = getopt_long (argc, argv, "fn:s:uvxz", long_opts, NULL)) != -1)
1543 switch (c)
1545 case 0:
1546 break;
1548 case 'f':
1549 flags.force = 1;
1550 break;
1552 case 'n':
1554 uintmax_t tmp;
1555 if (xstrtoumax (optarg, NULL, 10, &tmp, NULL) != LONGINT_OK
1556 || (word32) tmp != tmp
1557 || ((size_t) (tmp * sizeof (int)) / sizeof (int) != tmp))
1559 error (1, 0, _("%s: invalid number of passes"),
1560 quotearg_colon (optarg));
1562 flags.n_iterations = (size_t) tmp;
1564 break;
1566 case 'u':
1567 flags.remove_file = 1;
1568 break;
1570 case 's':
1572 uintmax_t tmp;
1573 if (xstrtoumax (optarg, NULL, 0, &tmp, "cbBkKMGTPEZY0")
1574 != LONGINT_OK)
1576 error (1, 0, _("%s: invalid file size"),
1577 quotearg_colon (optarg));
1579 flags.size = tmp;
1581 break;
1583 case 'v':
1584 flags.verbose = 1;
1585 break;
1587 case 'x':
1588 flags.exact = 1;
1589 break;
1591 case 'z':
1592 flags.zero_fill = 1;
1593 break;
1595 case_GETOPT_HELP_CHAR;
1597 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1599 default:
1600 usage (1);
1604 file = argv + optind;
1605 n_files = argc - optind;
1607 if (n_files == 0)
1609 error (0, 0, _("missing file argument"));
1610 usage (1);
1613 for (i = 0; i < n_files; i++)
1615 char *qname = xstrdup (quotearg_colon (file[i]));
1616 if (STREQ (file[i], "-"))
1618 if (wipefd (STDOUT_FILENO, qname, &s, &flags) < 0)
1619 err = 1;
1621 else
1623 /* Plain filename - Note that this overwrites *argv! */
1624 if (wipefile (file[i], qname, &s, &flags) < 0)
1625 err = 1;
1627 free (qname);
1630 /* Just on general principles, wipe s. */
1631 memset (&s, 0, sizeof s);
1633 exit (err);
1636 * vim:sw=2:sts=2: