[net.unix-wizards] dsw

solomon (02/20/83)

I got so sick of seeing all that nonsense about removing files with garbage
names, I wrote my own dsw.  It took about 10 minutes.  Here it is.

/* "interesting etymology" program to remove files */
/* NB:  will not work with the new 4.2 BSD file system */
#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
main() {
	int dot,n;
	struct stat statb;
	struct direct dir;
	char name[DIRSIZ+1],*strncpy();

	dot = open(".",0);
	if (dot < 0) {
		perror(".");
		exit(1);
	}
	while ((n=read(dot,(char *)(&dir),sizeof dir))==sizeof dir) {
		if (dir.d_ino == 0) continue;
		(void) strncpy(name,dir.d_name,DIRSIZ);
		name[DIRSIZ] = '\0';
		if(stat(name,&statb)<0) {
			perror(name);
			exit(1);
		}
		if ((statb.st_mode&S_IFMT)!=S_IFREG) continue;
		fprintf(stderr,"remove %s: ",name); /* really should display a readable
			version of the name */
		if (yes()) {
			if (unlink(name)<0) {
				perror(name);
				continue;
			}
		}
	}
	if (n<0)
		perror(".");
}

yes() {
	int c;

	for(;;) {
		c = getchar();
		if (c=='y' || c=='Y') {
			while ((c = getchar())!='\n' && c!=EOF);
			return 1;
		}
		if (c=='n' || c=='N') {
			while ((c = getchar())!='\n' && c!=EOF);
			return 0;
		}
		while (c!='\n' && c!=EOF) c = getchar();
		if (c == EOF) return 0;
	}
}

cyrus (02/21/83)

Here's a problem I've run across removing bogus file names:
	What if the first char of the funny file is a null?
That's right, a valid file, valid inode, first char in file name is a zero.
No program can remove the file by name since the kernel will take the null char
to be the end of string marker.

The only thing that I have been able to do is clri the inode and rebuild the
file system.  Anyone know a better way?

	-Cyrus Azar (AMPEX)

rogers (02/23/83)

	I have written a little command called 'srm' which
will allow the user to remove files by inode number rather than
file name.  If there is enough interest (via mail or to this
newsgroup) I will be happy to post the source into net.sources.

		-Roger

greep@Su-Dsn (03/28/83)

Regarding your file with a the first character null:

You could always just change the file name (change the null
to some legal character) by writing over the directory manually.
Of course Unix doesn't let you write directories, so first
you have to munge the directory's inode to make it look like
an ordinary file, and then unmunge it again after you rewrite
the directory (use "ls -f" to make sure you did it right).
I wrote a program to do inode munging once, but that was on
version 6 so it wouldn't be of much use now.

		- greep