home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / gitwipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  3.2 KB  |  156 lines

  1. /* gitwipe.c -- An utility that deletes the contents of a file in order to
  2.    make it impossible for someone to recover it.  */
  3.  
  4. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  5.  
  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.
  10.  
  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.
  15.  
  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
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Written by Tudor Hulubei and Andrei Pitis.  */
  21.  
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <stdio.h>
  28.  
  29. #ifdef HAVE_STDLIB_H
  30. #include <stdlib.h>
  31. #else /* !HAVE_STDLIB_H */
  32. #include "ansi_stdlib.h"
  33. #endif /* !HAVE_STDLIB_H */
  34.  
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif /* HAVE_UNISTD_H */
  38.  
  39. #include "xtime.h"
  40.  
  41. #include <sys/types.h>
  42. #include "file.h"
  43. #include <fcntl.h>
  44. #include <limits.h>
  45.  
  46.  
  47. #define WIPE_BUFFER_SIZE        64*1024
  48. #undef min
  49. #define min(a, b) ((a) <= (b) ? (a) : (b))
  50.  
  51.  
  52. char *program;
  53.  
  54.  
  55. int
  56. usage()
  57. {
  58.     fprintf(stderr, "%s file1 [file2 [...]]\n", program);
  59.     exit(1);
  60. }
  61.  
  62.  
  63. int
  64. file_length(handle)
  65.     int handle;
  66. {
  67.     int tmp, length;
  68.  
  69.     tmp    = lseek(handle, 0, SEEK_CUR);
  70.     length = lseek(handle, 0, SEEK_END);
  71.     lseek(handle, tmp, SEEK_SET);
  72.     return length;
  73. }
  74.  
  75.  
  76. int
  77. wipe(file)
  78.     char *file;
  79. {
  80.     int handle, i, j;
  81.     unsigned char *buf;
  82.     size_t len, bytes_to_write;
  83.  
  84.     handle = open(file, O_RDWR);
  85.  
  86.     if (handle == -1)
  87.     {
  88.     fprintf(stderr, "%s: can't open file '%s'.\n", program, file);
  89.     return 1;
  90.     }
  91.  
  92.     len = file_length(handle);
  93.  
  94.     if (len == 0)
  95.     return 0;
  96.  
  97.     buf = (unsigned char *)malloc(WIPE_BUFFER_SIZE);
  98.  
  99.     if (buf == NULL)
  100.     {
  101.     fprintf(stderr, "%s: virtual memory exhausted.\n", program);
  102.     return 1;
  103.     }
  104.  
  105.     for (i = 0; i < len; i += WIPE_BUFFER_SIZE)
  106.     {
  107.     bytes_to_write = min(len - i, WIPE_BUFFER_SIZE);
  108.  
  109.     for (j = 0; j < bytes_to_write; j++)
  110.         buf[j] = rand() % 0xFF;
  111.  
  112.     if (write(handle, buf, bytes_to_write) != bytes_to_write)
  113.     {
  114.         fprintf(stderr, "%s: can't write to file %s.\n", program, file);
  115.         return 1;
  116.     }
  117.     }
  118.  
  119.     close(handle);
  120.     sync();
  121.  
  122.     /* Don't delete the file! The file system might notice that the blocks
  123.        in this file are no longer used and never write them back to disk.
  124.        And there is more: sync() may return before the actual writing is
  125.        done.  See the Linux sync(2) & sync(8) manual pages for more detail.
  126.        If you want to be sure, REBOOT NOW ! :-))))))))))))))  */
  127.  
  128.     return 0;
  129. }
  130.  
  131.  
  132. /*
  133.  * argv[1...]   = files
  134.  * return value = no of errors (unprocessed files)
  135.  */
  136.  
  137. int
  138. main(argc, argv)
  139.     int argc;
  140.     char *argv[];
  141. {
  142.     int i, errors = 0;
  143.  
  144.     program = argv[0];
  145.  
  146.     if (argc < 2)
  147.     usage();
  148.  
  149.     srand(time(NULL));
  150.  
  151.     for (i = 1; i < argc; i++)
  152.     errors += wipe(argv[i]);
  153.  
  154.     return errors;
  155. }
  156.