From: utzoo!decvax!harpo!seismo!uwvax!solomon
Newsgroups: net.unix-wizards
Title: dsw
Article-I.D.: crystal.164
Posted: Sun Feb 20 12:26:30 1983
Received: Mon Feb 21 19:07:24 1983

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 
#include 
#include 
#include 
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;
	}
}