[net.sources] Preen.c - active file cleaner

larry@ihuxf.UUCP (Larry Marek) (10/15/83)

	Over time we have had some problems with duplicate entries sneeking
into our "active" files (and then news reader's .newsrc files).  While this
has not been disasterous, it has been a pain.  Rather than edit each of our
almost 100 active files by hand, I came up with this program.  When it
completes, the active file will be alphabetically sorted and contain only the
highest numbered entry from any duplicate lines.  TO get the active file in a
normal reading order, use "asort" which will be posted next.

preen.c:
------
/*
 *	P R E E N . C		By: Larry Marek		AT&T Bell Labs
 *
 * This program takes the netnews "active" file, makes a copy ("active.old"
 * for safe keeping), alphabetically sorts the "active" file, then proceedes
 * to preen out the duplicate entries, writing only the line with the highest
 * article number out to "active.new".  When this program completes it will
 * move this cleaned file to "active".   You should then re-order this
 * "active" file with 'asort active < active.master'. 
 *
 *	Usage: preen filename
 */

#include <stdio.h>
#include <sys/signal.h>

main(argc, argv)
int	argc;
char	*argv[];
{
	extern int	(*signal())();
	char	cmd_buf[BUFSIZ];
	char	buf[BUFSIZ];
	char	oline[BUFSIZ];
	char	num[BUFSIZ];
	char	onum[BUFSIZ];
	
	FILE	*fdi;
	FILE	*fdo;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s filename\n", argv[0]);
		exit(1);
	}
	umask(0);
/*
 * Can we read the named file?
 */
	if (access(argv[1], 04) != 0) {
		fprintf(stderr, "Can not read %s\n", argv[1]);
		exit(2);
	}
	(void) signal(SIGHUP, SIG_IGN);
	(void) signal(SIGINT, SIG_IGN);
	(void) signal(SIGQUIT, SIG_IGN);

	sprintf(cmd_buf, "cp %s %s.old; sort -o %s %s.old",
		argv[1], argv[1], argv[1], argv[1]);
	system(cmd_buf);

	fdi = fopen(argv[1], "r");
	if (fdi == NULL ) {
		fprintf(stderr, "Can not open %s\n", argv[1]);
		exit(3);
	}
	sprintf(cmd_buf, "%s.new", argv[1]);
	fdo = fopen(cmd_buf, "w");
	if (fdo == NULL) {
		fprintf(stderr, "Can not create cmd_buf.\n", cmd_buf);
		exit(4);
	}
	fscanf(fdi, "%s %s", oline, onum);
	
	while (fscanf(fdi, "%s %s", buf, num) != EOF) {
		if (strcmp(buf, oline) == 0) {
			strcpy(oline, buf);
			strcpy(onum, num);
		} else {
			fprintf(fdo, "%s %s\n", oline, onum);
			strcpy(oline, buf);
			strcpy(onum, num);
		}
	}
	fprintf(fdo, "%s %s\n", buf, num);
	fclose(fdo);
	fclose(fdi);

	unlink(argv[1]);
	link(cmd_buf, argv[1]);
	unlink(cmd_buf);
}
-- 


		Larry Marek
		 ihnp4!ihuxf!larry