ERASE UTILITY

Kenny McDonald c60244 at ccfiris.aedc
Sat Mar 23 07:08:56 AEST 1991


The following is a copy of the utility I wrote:

______________________________________________________________________________
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main(argc,argv)
int argc;
char *argv[];
{
   int i,fd,j,size, confirm=0, ch;
   char word1=0, word2=0xff, word3=0x44, ans[4];
   struct stat buf;
   extern int optind;

/* parse the command line for the argument -i to interactively ask the   */
/* user if he wants to remove the file after being overwritten.          */

   while ((ch = getopt (argc,argv,"i")) != EOF) {
      switch (ch) {
      case 'i':
        confirm++;
        break;
      case '?':
        printf ("usage: %s [-i] file [file ...]\n",argv[0]);
        exit(1);
        break;
      }
   }

/* check to make sure the user entered a file name */

   if ((argc-optind)<1) {
      printf ("usage: %s [-i] file [file ...]\n",argv[0]);
      exit(1);
   }

/* for each file on the command line do the overwrite */

   j=optind;
   while (j<argc) {
      if ((chmod(argv[j],0600)) != 0)
         printf ("error: %s could not find %s\n",argv[0],argv[j]);
      else if ((fd=open(argv[j],O_WRONLY)) == -1)
         printf ("error: %s could not open %s\n",argv[0],argv[j]);

/* do fstat to get size of file */

      else if ((fstat (fd, &buf)) != 0)
         printf ("error: %s could not get stats on %s\n",argv[0],argv[j]);
      else {
         printf ("size of %s = %d\n",argv[j],size=buf.st_size);
         printf ("inode of %s = %d\n",argv[j],buf.st_ino);
         printf ("Wrinting 0\n");
         lseek (fd, 0, SEEK_SET);
         for (i=0;i<size;i++)
            write(fd,&word1,sizeof(char));
         printf ("Wrinting 0xff\n");
         lseek (fd, 0, SEEK_SET);
         for (i=0;i<size;i++)
            write(fd,&word2,sizeof(char));
         printf ("Wrinting 0x44\n");
         lseek (fd, 0, SEEK_SET);
         for (i=0;i<size;i++)
            write(fd,&word3,sizeof(char));
         lseek (fd, 0, SEEK_SET);
         if ((fstat (fd, &buf)) != 0)
            printf ("error: %s could not get stats on file\n",argv[0]);
         else if (confirm) {
            printf ("Would you like to remove %s? ",argv[j]);
            scanf ("%s",ans);
            if ((ans[0] == 'y') || (ans[0] == 'Y'))
               remove (argv[j]);
         }
         else
            remove(argv[j]);
      }
      close(fd);
      j++;
   }
}

______________________________________________________________________________

My questions about this code are the following:

1. Does this actually overwrite the file on disk 3 times, or does it just do
   it in memory & at the end overwrite the disk?

2. What about how Irix deals with files (EFS)?  Will this overwrite the exact
   same space that the original file occupied, or could Irix write the file to
   different blocks?

Any comments/suggestions about this are welcomed.



  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  | Kenneth M. McDonald * OAO Corp * Arnold Engineering Development Center |
  |          MS 120 * Arnold AFS, TN 37389-9998 * (615) 454-3413           |
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  INTERNET:                                               OO   AA   OO
  mcdonald at aedc-vax.af.mil                               O  O A  A O  O
                                                         O  O AAAA O  O
  LOCAL:                                                 O  O A  A O  O
  c60244 at ccfiris                                          OO  A  A  OO
                                                        ________________
                                                                       /



More information about the Comp.sys.sgi mailing list