[comp.sources.misc] v16i081: newsclean - tool to cleanup .newsrc, Part01/01

kluge@informatik.tu-muenchen.dbp.de (Oliver Kluge) (01/18/91)

Submitted-by: kluge@informatik.tu-muenchen.dbp.de (Oliver Kluge)
Posting-number: Volume 16, Issue 81
Archive-name: newsclean/part01

This program compacts the .newsrc file of the rrn news reader. It
does this by eliminating the read article counter of unsubscribed
newsgroups. Additionally, the counters of subscribed-to newsgroups
get compacted by removing the unavailable article skip marks. It
also sorts the file so that all subscribed-to newsgroups appear at
the beginning of the file. This also speeds up rrn because it needs
less time to seek your subscribed-to newsgroups. The unsubscribed
ones get alphabetically sorted so whenever you want to rearrange
your .newsrc, they will be neatly ordered (for convenience, case is
ignored). The order in which the subscribed-to newsgroups appear is
left unchanged, so you can order them in your favorite reading
order using vi.

Oliver
--
#!/bin/sh
# shar:	Shell Archiver  (v1.23)
#
#	Run the following text with /bin/sh to create:
#	  README
#	  Makefile
#	  newsclean.c
#
sed 's/^X//' << 'SHAR_EOF' > README &&
XVersion 1.20 of 17.1.1991
X
XAnother small bug fixed: If the .newsrc's last subscribed-to
Xnewsgroup had no read articles, it got merged with the first
Xunsubscribed newsgroup. Fixed.
XAdditional feature: Now the program can handle .newsrc files that
Xhave invalid data structure. It is possible to remove the separating
Xspace between a newsgroup name and the read article counter without
Xhaving rrn complaining immediately. Now newsclean can handle such
X.newsrc and will correct the data structure.
X
XVersion 1.10 of 16.1.1991
X
XThis is a small bugfix for version 1.00.
XThe bug occurred only when there was a subscribed-to newsgroup with
Xno read articles followed at least one additional subscribed-to
Xnewsgroup with read articles. The bug is fixed.
XAn additional bug was fixed. The program did not optimally compact
Xread article counters that had combinations of skip marks and
Xranges. Not really a bug, but now the result is even more compact.
X
XVersion 1.00 of 15.1.1991
X
XThis program compacts the .newsrc file of the rrn news reader. It
Xdoes this by eliminating the read article counter of unsubscribed
Xnewsgroups. Additionally, the counters of subscribed-to newsgroups
Xget compacted by removing the unavailable article skip marks. It
Xalso sorts the file so that all subscribed-to newsgroups appear at
Xthe beginning of the file. This also speeds up rrn because it needs
Xless time to seek your subscribed-to newsgroups. The unsubscribed
Xones get alphabetically sorted so whenever you want to rearrange
Xyour .newsrc, they will be neatly ordered (for convenience, case is
Xignored). The order in which the subscribed-to newsgroups appear is
Xleft unchanged, so you can order them in your favorite reading
Xorder using vi.
X
XTo compile, have a look at Makefile and adjust as necessary.
XNewsclean was originally written for a DEC MicroVAX II under
XUltrix. Currently it is also tested on Sun's SunOS.
XAfter checking, simply type make.
X
XOliver Kluge
X
X                                         / relay.cs.net (CS-NET, ARPA)
Xkluge%lan.informatik.tu-muenchen.dbp.de@ - unido.uucp   (UUCP)
X                                         \ unido.bitnet (BITNET)
XTTTTTTUU  MUMMMMMMMM            Munich Institute of Technology
XTTTTTTUU  UMMMMMMMMM  Department of Mathematics and Computer Sciences SAB
X  TT  UU  MU  MM  MM           Laboratory for Parallel Computing
X  TT  UU  UM  MM  MM                    Arcisstrasse 21
X  TT  UU  MU  MM  MM                     8000-Munich 2
X  TT  UUUUUM  MM  MM              Federal Republic of Germany
X  TT  UUUUMU  MM  MM       Voice +49 89 2105-3251, Fax +49 89 2800529
X"Why stop now just when I'm hating it?" Marvin, the paranoid android
SHAR_EOF
chmod 0644 README || echo "restore of README fails"
sed 's/^X//' << 'SHAR_EOF' > Makefile &&
XCFLAGS=-O
X
Xnewsclean: newsclean.c
X	$(CC) $(CFLAGS) -o newsclean newsclean.c
X	strip newsclean
X
Xlint:
X	lint newsclean.c > lint.out
X
Xclean:
X	rm -f newsclean.o lint.out core
SHAR_EOF
chmod 0644 Makefile || echo "restore of Makefile fails"
sed 's/^X//' << 'SHAR_EOF' > newsclean.c &&
X/* ===============================================
X   |                                             |
X   |   N E W S C L E A N                         |
X   |                                             |
X   |   Utility to clean up rrn's .newsrc.        |
X   |   This program removes read article count   |
X   |   of unsubscribed newsgroups, eliminates    |
X   |   unavailable article skips in count,       |
X   |   rearranges .newsrc so subscribed          |
X   |   newsgroup appear first and unsubscribed   |
X   |   ones get alphabetically sorted.           |
X   |   By doing this, newsclean significantly    |
X   |   decreases .newsrc's size and increases    |
X   |   rrn's startup speed.                      |
X   |   Version 1.20 (c) 17.1.1991 Oliver Kluge   |
X   |                                             |
X   |   Please send corrections or suggestions    |
X   |   for improvement to:                       |
X   |   kluge%lan.informatik.tu-muenchen.dbp.de   |
X   |   ... @relay.cs.net (CS-NET, ARPA)          |
X   |   ... @unido.uucp   (UUCP)                  |
X   |   No warranty, expressed or implied, is     |
X   |   made that this program fits a specific    |
X   |   purpose or that it does no damage.        |
X   |   This program is free to be used as long   |
X   |   as the copyright is not removed.          |
X   |                                             |
X   |=============================================| */
X
X#include <stdio.h>
X#include <ctype.h>
X
X#define FALSE 0
X#define TRUE !FALSE
X
Xcleanup ()
X{
X	/* This routine checks the .newsrc if invalid newsgroups
X	entries are present. These have no separating spaces between
X	the newsgroup name and the read article counter */
X	FILE *fopen(), *Newsrc, *Cleaned;
X	int i, Mark;
X	int EndOfInput;
X	char Entry[1024];
X
X	Newsrc = fopen(".newsrc", "r");
X	Cleaned = fopen("rec.newsrc", "w");
X
X	do {
X		EndOfInput = fscanf(Newsrc, "%s", Entry);
X		if (EndOfInput!=EOF) {
X			if (!isdigit(Entry[0])) {
X			/* Newsgroup name */
X				/* Start search after subscription
X				   mark! */
X				for (i=1;(i<strlen(Entry))
X					&& (Entry[i]!=':')
X					&& (Entry[i]!='!'); i++)
X					;
X				Mark = i;
X				for (i=Mark;(i<strlen(Entry)) &&
X					(!isdigit(Entry[i]));i++)
X					;
X				if (i<strlen(Entry)) {
X				/* Illegal article counter without
X				   preceding space found */
X					Mark = i;
X					Entry[strlen(Entry)+1] = '\0';
X					for (i=strlen(Entry);i>Mark;
X						i--)
X						Entry[i] = Entry[i-1];
X					Entry[Mark] = ' ';
X					fprintf(Cleaned, "\n%s", Entry);
X				}
X				else
X				/* No illegal counter */
X					fprintf(Cleaned, "\n%s", Entry);
X			}
X			else
X			/* Read article counter */
X				fprintf(Cleaned, " %s", Entry);
X		}
X	}
X	while (EndOfInput!=EOF);
X	fclose (Cleaned);
X	fclose (Newsrc);
X}
X
Xcompact (Count)
Xchar Count[1024];
X{
X	/* This routine eliminates all skips for unavailable
X	   articles in a newsgroup's read article count entry */
X	int i, Mark1, Mark2;
X
X	for (i=1; (Count[i]!='-') && (Count[i]!=',')
X		&& (i<strlen(Count)); i++)
X		;
X	if (i<strlen(Count)) {
X		Mark1 = i;
X		for (i=strlen(Count); (Count[i]!='-') && (Count[i]!=',')
X			&& (i>1); i--)
X			;
X		if (i>1) {
X			Mark2 = i;
X			for (i=Mark2+1; i!=strlen(Count); i++)
X				Count[Mark1+i-Mark2] = Count[i];
X			Count[Mark1+strlen(Count)-Mark2] = '\0';
X			Count[Mark1]='-';
X		}
X	}
X}
X
Xmain() {
X	FILE *fopen(), *Newsrc, *Cleaned, *Temp;
X	char Entry[1024];
X	int Subscribed, EndOfInput, NoArticle;
X
X	printf ("NewsClean - Version 1.20 (c) 17.1.1991 Oliver Kluge\n");
X	Subscribed = FALSE;
X
X	/* Clean up .newsrc of entries with invalid structure */
X	cleanup();
X
X	/* Distribute the contents of .newsrc in two new files, one
X	   to hold all subscribed newsgroups and one to hold the
X	   unsubscribed. The latter gets stripped of the read article
X	   counter entries. */
X	Newsrc = fopen("rec.newsrc", "r");
X	Cleaned = fopen("new.newsrc", "w");
X	Temp = fopen("tmp.newsrc", "w");
X
X	do {
X		EndOfInput = fscanf(Newsrc, "%s", Entry);
X		if (EndOfInput!=EOF) {
X			if (!isdigit(Entry[0])) {
X			/* Got a newsgroup name! */
X				if (Entry[strlen(Entry)-1]==':') {
X				/* It is subscribed-to */
X					Subscribed = TRUE;
X					fprintf(Cleaned, "\n%s", Entry);
X				}
X				else {
X				/* It is unsubscribed */
X					Subscribed = FALSE;
X					fprintf(Temp, "\n%s", Entry);
X				}
X			}
X			else {
X			/* Got a newsgroup read article counter! */
X				if (Subscribed==TRUE) {
X				/* It is subscribed-to */
X					compact(Entry);
X					fprintf(Cleaned, " %s", Entry);
X				}
X			}
X		}
X	}
X	while (EndOfInput!=EOF);
X	fprintf(Cleaned, "\n");
X	fprintf(Temp, "\n");
X	fclose(Newsrc);
X	fclose(Cleaned);
X	fclose(Temp);
X
X	/* Now let UNIX's sort do the alphabetical sorting of the
X	   unsubscribed newsgroups. */
X	system("sort -d -f -u tmp.newsrc -o tmp.newsrc");
X
X	/* And reunite the subscribed and the unsubscribed to form
X	   the new .newsrc */
X	Newsrc = fopen("new.newsrc", "r");
X	Temp = fopen("tmp.newsrc", "r");
X	Cleaned = fopen(".newsrc", "w");
X	/* Subscribed */
X	NoArticle = FALSE;
X	do {
X		/* Newsgroup names first */
X		EndOfInput = fscanf(Newsrc, "%s", Entry);
X		if (EndOfInput!=EOF) {
X			if (!isdigit(Entry[0])) {
X			/* Newsgroup name */
X				if (NoArticle==TRUE) fprintf(Cleaned, "\n");
X				fprintf(Cleaned, "%s", Entry);
X				NoArticle = TRUE;
X			}
X			else {
X			/* Read article counter */
X				fprintf(Cleaned, " %s\n", Entry);
X				NoArticle = FALSE;
X			}
X		}
X	}
X	while (EndOfInput!=EOF);
X	if (NoArticle==TRUE) fprintf(Cleaned, "\n");
X
X	/* Unsubscribed */
X	do {
X		/* Unsubscribed have no counter anymore */
X		EndOfInput = fscanf(Temp, "%s", Entry);
X		if (EndOfInput!=EOF) fprintf(Cleaned, "%s\n", Entry);
X	}
X	while (EndOfInput!=EOF);
X	fclose(Newsrc);
X	fclose(Temp);
X	fclose(Cleaned);
X
X	/* Now trash the temporary files */
X	unlink("new.newsrc");
X	unlink("tmp.newsrc");
X}
SHAR_EOF
chmod 0644 newsclean.c || echo "restore of newsclean.c fails"
exit 0

exit 0 # Just in case...
-- 
Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
Sterling Software, IMD           UUCP:     uunet!sparky!kent
Phone:    (402) 291-8300         FAX:      (402) 291-4362
Please send comp.sources.misc-related mail to kent@uunet.uu.net.