[pe.cust.sources] hrt.c - a program to remove unreadable files

earlw@pesnta.UUCP (Earl Wallace) (06/26/85)

This program is used to determine how readable a file is and remove the
file if it doesn't meet a specified readable percentage.  I use it to 
blow away binaries of Edition VII on XELOS; since the binary won't load
and execute, why keep it around?  Line feeds, carriage returns, formfeeds,
etc. are taken into account.

-earlw

static char *sccsid="@(#)hrt.c	2.1	11/7/84 19:32:51 [pesnta]";
/************************************************************************
 * CopyWrong 1984 Earl Wallace, Perkin-Elmer Corp., Santa Clara, Calif. *
 * All Rights Reserved for Table 6 at Swanks.				*
 * No part of this file may be used in a product for bucks but may be   *
 * given away if the person has a last name that begins with a letter   *
 ************************************************************************/

/*
 * hrt (Human Readable Text) - examines a file to see if it is readable.
 * The return code is the percentage readable.  If the -rn is specified,
 * the file is removed if it is less than n% readable and no message is
 * displayed.  Zero-length files will always be removed with the -rn.
 */

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>

main(argc, argv)
int	argc;
char	*argv[];
{
	FILE	 *fp;
	char	 **cp;
	register int  c, cnt, ok;
	int	 percent, r;
	struct stat buf;

	if (argc < 2) {
	   fprintf(stderr,"Usage: [-rn] hrt file\n");
	   exit(0);
	}
	if (argv[1][0] == '-' && argv[1][1] == 'r') {
		if (argv[1][2] == 0) {
			fprintf(stderr,"The -rn option needs 'n' to be a ");
			fprintf(stderr,"number in the range 10-100\n");
			exit(100);
		}
		r = atoi(&argv[1][2]);
		if (r < 10 || r > 100) {
			fprintf(stderr,"out of range (10-100) for -rn\n");
			exit(100);
		}
		cp = &argv[1];
	} else
		cp = &argv[0];
	while(*++cp) {
		cnt = ok = 0;
		if(fp=fopen(*cp, "r")) {
	      		while( (c=getc(fp)) != EOF) {
	         		cnt++;
	         		if(isprint(c) || isspace(c) || c == 010) ok++;
	      		}
	      		if(cnt == 0) {
		 		if (r) {
					if (fstat(fileno(fp), &buf) == 0 &&
					    (buf.st_mode&S_IFMT) == S_IFREG) {
					    unlink(*cp);
					}
		 		} else
	         			printf("%s is empty.\n", *cp);
	      		} else {
	         		percent = ok*100 / cnt;	
		 		if (r) {
					if (percent < r &&
					    fstat(fileno(fp), &buf) == 0 && 
					    (buf.st_mode&S_IFMT) == S_IFREG) { 
						unlink(*cp);
					}
		 		} else
	         			printf("%s is %d%% readable.\n",
						*cp, percent);
			}
			fclose(fp);
	 	} else
	      		fprintf(stderr,"can't open %s\n", *cp);
	}
	exit(percent);	/* last guy */
}