[net.sources] rm-damnit.1

karsh@geowhiz.UUCP (Bruce Karsh) (04/06/85)

[]

  I don't know if this is a generic UN*X problem, or if it just
occurs on our Masscomp system, but sometimes improperly written
programs will create files which rm can not remove.  (Typically
because they contain imbedded garbage characters.)

  This program, rm-damnit(1), allows users to remove these files.

  I have checked this program out pretty carefully, but since it
does delete files, be warned that it comes with *NO* guarantees.
(We use it at geowhiz and haven't had problems.)

# -------------------- Cut Here ---------------------------------
# This is a shell archive.  Remove anything before this line, then
# unpack it by saving it in a file and typing "sh file".  (Files
# unpacked will be owned by you and have default permissions.)
#
# This archive contains:
# rm-damnit.1 rm-damnit.c

echo x - rm-damnit.1
cat > "rm-damnit.1" << '//E*O*F rm-damnit.1//'
.TH RM-DAMNIT 1 LOCAL
.SH NAME
rm-damnit  \- remove stubborn files
.SH SYNOPSIS
.B rm-damnit
.PP
.SH DESCRIPTION
.I Rm-damnit\^
removes the entries for one or more
files
from a directory.
If an entry was the last link to the file, the file
is destroyed.
Removal of a file requires write permission in its directory,
but neither read nor write permission on the file itself.
.PP
.I Rm-damnit\^
is intended to be used to remove files which rm(2)
can not remove because they contain improper names.
(Rm(2) will not remove files with improper names such as those 
with imbedded spaces, characters with the high bit set or imbedded
zeros.)
.PP
Rm-damnit prints each file name in the current directory in turn.
(Invalid characters are printed as question marks.) 
After each name, it prompts with a question mark.
You may type y to remove the file, or q to quit the program.
Any other character will cause the program to go on to the next file.
(Be careful.  If you remove a file, the file is irretrievable.)
.SH SEE ALSO
rm(2).
.SH DIAGNOSTICS
Rm-damnit will prompt for all entries in a directory, regardless of
whether or not it has permission to remove them.  If the prompt is
responded to with a 'y', it will try to unlink the file even if it
does not have permission.  If the unlink fails, the message:
.RS
"Remove Failed"
.RE
will be output.
//E*O*F rm-damnit.1//

echo x - rm-damnit.c
cat > "rm-damnit.c" << '//E*O*F rm-damnit.c//'
/*********************************************************
*							 *
* rm-damnit - remove stubborn files.			 *
*							 *
*    Usage: cd directory				 *
*           rm-damnit					 *
*							 *
* Permits the user to remove files with improper names.  *
* Interactively prompts for each file in a directory.    *
*							 *
* ORIGINAL April, 1985					 *
*   Bruce Karsh, Univ. Wisconsin, Madison, Dept. of      *
*   Geology and Geophysics.                              *
*   UUCP: uwvax\!geowhiz\!karsh                          *
*********************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/dir.h>
main()
{
int fd,status,i,lastchr;
struct direct direct;
char string[255],nm[DIRSIZ];
fd=open(".",O_RDONLY);
while(read(fd,&direct,sizeof(direct))==sizeof(direct))
  {
  if(direct.d_ino != 0)
    {
/*
** Search for the last non-zero char.
*/
    for(lastchr=DIRSIZ-1;lastchr>=0;lastchr--)
      if(direct.d_name[lastchr] != 0)break;
/*
** If all chars are zero, all chars should be changed to
** question marks.
*/
    if(lastchr<0)lastchr=DIRSIZ-1;
/*
** Copy name into temp buffer to be made ready to print.
*/
    for(i=0;i<DIRSIZ;i++)
      nm[i]=direct.d_name[i];
/*
** Convert bad chars to question marks.
*/
    for(i=0;i<lastchr+1;i++)
      {
      if(nm[i]<=040 || nm[i]>=0177)nm[i]='?';
      }
/*
** Ask if it's ok to delete.
*/
    printf("%.14s ?",nm);
    fgets(string,255,stdin);
/*
** Process user's response.
*/
    if(strcmp(string,"y\n") == 0)
      {
      printf("Removing %.14s\n",nm);
      status=unlink(direct.d_name);
      if(status != 0)printf("Remove failed\n");
      }
    if(strcmp(string,"q\n") == 0)exit(0);
    }
  }
printf("All done\n");
exit(0);
}
//E*O*F rm-damnit.c//

echo Possible errors detected by \'wc\' [hopefully none]:
temp=/tmp/shar$$
trap "rm -f $temp; exit" 0 1 2 3 15
cat > $temp <<\!!!
     40    238   1342 rm-damnit.1
     71    194   1756 rm-damnit.c
    111    432   3098 total
!!!
wc  rm-damnit.1 rm-damnit.c | sed 's=[^ ]*/==' | diff -b $temp -
exit 0
-- 
Bruce Karsh                           |
U. Wisc. Dept. Geology and Geophysics |
1215 W Dayton, Madison, WI 53706      | This space for rent.
(608) 262-1697                        |
{ihnp4,seismo}!uwvax!geowhiz!karsh    |

edward@ukma.UUCP (Edward C. Bennett) (04/09/85)

	Uh. I hate to burst your bubble. But doesn't

		rm -i

	do the same thing?


-- 
edward

		 {ucbvax,unmvax,boulder,research}!anlams! -|
			{mcvax!qtlon,vax135,mddc}!qusavx! -|-->	ukma!edward
     {decvax,ihnp4,mhuxt,clyde,osu-eddie,ulysses}!cbosgd! -|

dennisw@tekigm.UUCP (Dennis Ward) (04/09/85)

Removing funky files?  An easily solved problem.  No program necessary.

METHOD 1: 'ls | see' - look at the control characters, now 'rm' of
		the file is much easier!  (use '?' or '*' for
		untypable chars)

METHOD 2: 'rm -i *' - only respond with 'y' to the right one!

bill@haddock.UUCP (04/25/85)

  And then there is always

	 find . <some criteria> -exec rm {} \;

  You can use crude but effective tools like "od -bch ." to see what the
true name of a file is.  I think the problem with funny names is getting
them past the shell; rm doesn't care.