recovering blown-away .nindex files

utzoo!decvax!ittvax!swatt utzoo!decvax!ittvax!swatt
Sun Aug 30 18:17:36 AEST 1981


A while back, Mark at ucbvax mentioned that they had lost their
.nindex file.  Well we just did too -- boy what a pain!.  If
any of you have run into that problem you might already have a
program that does this, but for the rest, here is some insurance.


	- Alan S. Watt (decvax!ittvax!swatt)

::::::::::::::::::
newsrecover.c:
::::::::::::::::::
/* Yuck, recover ".nindex" files that got clobbered by
 * a news malfunction.
 *
 * Usage:
 *	newsrecover [-submit]
 *
 *	-submit argument causes date to be taken from the submission
 *	date of article, file modification time otherwise.
 */
#include <stdio.h>
#include <sys/types.h>
#include <dir.h>
#include <stat.h>

#ifndef NEWSDIR
# define NEWSDIR	"/usr/spool/news"
#endif

#ifdef lint
# define IGNORE(X)	__void__=(int)(X)
  int __void__;
#else
# define IGNORE(X)	X
#endif
#define EOS	'\0'
#define NO	0
#define YES	1

char	newsdir[]	= NEWSDIR;
struct {
	char	fmtid[80];
	char	ngrp[80];
	char	contrb[80];
	char	date[32];
} H;
char *index(), *rindex();
int submit = NO;

main(argc, argv)
char **argv;
{
	long timeret, timenow;
	register FILE *filef, *dirf;
	struct dir dirbuf;
	struct stat sb;

	if (argc > 1 && strcmp (argv[1], "-submit") == 0)
		submit = YES;
	IGNORE (time (&timenow));
	if (chdir (newsdir))
		fperror ("cannot change directory to %s", newsdir);
	if ((dirf = fopen (".", "r")) < 0)
		fperror ("cannot open %s", newsdir);
	while (fread ((char *)&dirbuf, sizeof dirbuf, 1, dirf) == 1) {
		char *p;
		char temp[DIRSIZ+1];
		char junk[DIRSIZ];
		int ijunk, sjunk;

		if (dirbuf.d_ino == 0)
			continue;
		strncpy (temp, dirbuf.d_name, sizeof dirbuf.d_name);
		/* skip non system name files */
		if ((sjunk = sscanf (temp, "%[^.].%d", junk, &ijunk)) != 2)
			continue;
		if ((filef = fopen (temp, "r")) == NULL) {
			fpremark ("Cannot open %s", temp);
			continue;
		}
		fstat (fileno(filef), &sb);
		if ((fgets ((char *)H.fmtid, sizeof H.fmtid, filef) == NULL)
		    || (fgets ((char *)H.ngrp, sizeof H.ngrp, filef) == NULL)
		    || (fgets ((char *)H.contrb,sizeof H.contrb, filef) == NULL)
		    || (fgets ((char *)H.date, sizeof H.date, filef) == NULL)) {
			IGNORE (fclose (filef));
			continue;
		}
		IGNORE (fclose (filef));
		rmnl (H.ngrp);
		rmnl (H.date);
		/* terrible hack:  the format of date strings simply will
		 * not pass through the getdate routine.  Chop off the first
		 * field (the day) and the last field (the year),
		 * and everything should be OK.
		 */
		if (submit == YES) {
			p = index (H.date, ' ');
			p++;
			strcpy (H.date, p);
			p = rindex (H.date, ' ');
			*p = 0;
			timeret = getdate (H.date, &timenow);
		}
		else
			timeret = sb.st_mtime;
		printf ("%s:%D:%s\n", temp, timeret, H.ngrp);
	}
}

extern int errno;
extern char *sys_errlist[];
fperror (gripe, args)
char *gripe;
{
	int sverrno = errno;

	_doprnt (gripe, &args, stderr);
	fprintf (stderr, ": %s\n", sys_errlist[sverrno]);
	exit (-1);
}

fpremark (gripe, args)
char *gripe;
{
	int sverrno = errno;

	_doprnt (gripe, &args, stderr);
	fprintf (stderr, ": %s\n", sys_errlist[sverrno]);
}

rmnl (str)
char *str;
{
	char *p;
	char *index();

	if (p = index (str, '\n'))
		*p = EOS;
}



More information about the Comp.unix.wizards mailing list