Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2(pesnta/xelos-%I%) 9/18/84; site pesnta.UUCP
Path: utzoo!lsuc!per!pesnta!earlw
From: earlw@pesnta.UUCP (Earl Wallace)
Newsgroups: pe.cust.sources
Subject: hrt.c - a program to remove unreadable files
Message-ID: <2797@pesnta.UUCP>
Date: Tue, 25-Jun-85 22:23:09 EDT
Article-I.D.: pesnta.2797
Posted: Tue Jun 25 22:23:09 1985
Date-Received: Sun, 30-Jun-85 19:40:38 EDT
Distribution: pe.cust
Organization: Perkin-Elmer Data Systems Group / Customer Service
Lines: 89

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

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 */
}