xref: /csrg-svn/usr.bin/uucp/uusnap/uusnap.c (revision 62407)
148670Sbostic /*-
2*62407Sbostic  * Copyright (c) 1988, 1993
3*62407Sbostic  *	The Regents of the University of California.  All rights reserved.
435466Sbostic  *
535466Sbostic  * This code is derived from software contributed to Berkeley by
648670Sbostic  * Rick Adams.  Originally by RJKing WECo-MG6565 May 83.
735466Sbostic  *
848670Sbostic  * %sccs.include.redist.c%
933577Srick  */
1033577Srick 
1135466Sbostic #ifndef lint
12*62407Sbostic static char copyright[] =
13*62407Sbostic "@(#) Copyright (c) 1988, 1993\n\
14*62407Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1535466Sbostic #endif /* not lint */
1613693Ssam 
1735466Sbostic #ifndef lint
18*62407Sbostic static char sccsid[] = "@(#)uusnap.c	8.1 (Berkeley) 06/06/93";
1935466Sbostic #endif /* not lint */
2035466Sbostic 
2113693Ssam #include "uucp.h"
2213693Ssam #include <sys/stat.h>
2313693Ssam #ifdef	NDIR
2413693Ssam #include "ndir.h"
2513693Ssam #else
2613702Ssam #include <sys/dir.h>
2713693Ssam #endif
2823730Sbloom #include <ctype.h>
2913693Ssam 
3033577Srick #define	NSYSTEM	300				/* max # of systems queued */
3113693Ssam 
3213693Ssam #define	CMDSLEN	5				/* Length of trailer */
3313693Ssam #define	DATALEN	5				/* Length of trailer */
3413693Ssam #define	XEQTLEN	5				/* Length of trailer */
3513693Ssam #define	NUMCTRS	3				/* # file types to count */
3613693Ssam #define	CMDTYPE	0				/* Index into scnt.cntr */
3713693Ssam #define	DATTYPE	1				/* Index into scnt.cntr */
3813693Ssam #define	XEQTYPE	2				/* Index into scnt.cntr */
3913693Ssam 
4013693Ssam struct	scnt {					/* System count structure */
4123690Sbloom 		char	name[MAXBASENAME+1];	/* Name of system */
4213693Ssam 		short	cntr[NUMCTRS];		/* Count */
4313693Ssam 		char	stst[32];		/* STST Message */
4417844Sralph 		time_t	locked;			/* If LCK..sys present */
4513693Ssam 		int	st_type;		/* STST Type */
4613693Ssam 		int	st_count;		/* STST Count */
4713693Ssam 		time_t	st_lastime;		/* STST Last time tried */
4813693Ssam 		time_t	st_retry;		/* STST Secs to retry */
4913693Ssam 	     };
5013693Ssam 
5113693Ssam int	sndx;					/* Number of systems */
5213693Ssam struct	scnt	sys[NSYSTEM];			/* Systems queued */
5317844Sralph int xqtisrunning = 0;
5413693Ssam 
main()5513693Ssam main()
5633964Srick {
5733964Srick 	register int i, j, nlen = 0;
5813693Ssam 	time_t	curtime, t;
5913693Ssam 
6046228Storek 	dodir(CMDSDIR, "C.", CMDSLEN, '\0', CMDTYPE);
6146228Storek 	dodir(DATADIR, "D.", DATALEN, '\0', DATTYPE);
6246228Storek 	dodir(XEQTDIR, "X.", XEQTLEN, 'X', XEQTYPE);
6317844Sralph 	getstst(SPOOL);
6413693Ssam 	time(&curtime);
6513693Ssam 	for(i=0; i<sndx; ++i)
6613693Ssam 		if((j = strlen(sys[i].name)) > nlen)
6713693Ssam 			nlen = j;
6817844Sralph 	for(i=0; i<sndx; ++i) {
6917844Sralph 		t = (sys[i].st_lastime +sys[i].st_retry) - curtime;
7017844Sralph 
7117844Sralph 		/* decide if STST text is worth printing */
7217844Sralph 		if (-t < ONEDAY*2 && sys[i].st_type == SS_WRONGTIME) {
7317844Sralph 			sys[i].stst[0] = '\0';
7417844Sralph 			if (sys[i].cntr[0]+sys[i].cntr[1]+sys[i].cntr[2] == 0)
7517844Sralph 				continue;	/* ignore entire line */
7617844Sralph 		}
7717844Sralph 
7817844Sralph 		printf("%-*.*s ", nlen, nlen, sys[i].name);
7913693Ssam 		if(sys[i].cntr[CMDTYPE])
8013693Ssam 			printf("%3.d Cmd%s ", sys[i].cntr[CMDTYPE],
8113693Ssam 				sys[i].cntr[CMDTYPE]>1?"s":" ");
8217844Sralph 		else
8317844Sralph 			printf("   ---   ");
8413693Ssam 		if(sys[i].cntr[DATTYPE])
8513693Ssam 			printf("%3.d Data ", sys[i].cntr[DATTYPE]);
8617844Sralph 		else
8717844Sralph 			printf("   ---   ");
8813693Ssam 		if(sys[i].cntr[XEQTYPE])
8913693Ssam 			printf("%3.d Xqt%s ", sys[i].cntr[XEQTYPE],
9013693Ssam 				sys[i].cntr[XEQTYPE]>1?"s":" ");
9117844Sralph 		else
9217844Sralph 			printf("   ---   ");
9346228Storek 		if(*sys[i].stst == '\0' || sys[i].locked > sys[i].st_lastime) {
9417844Sralph 			if(sys[i].locked)
9513693Ssam 				printf("LOCKED\n");
9617844Sralph 			else
9717844Sralph 				printf("\n");
9813693Ssam 			continue;
9913693Ssam 		}
10013693Ssam 		printf("%s  ", sys[i].stst);
10117844Sralph 		/* decide if STST info is worth pursuing */
10217844Sralph 		if (-t < ONEDAY*2 && (sys[i].st_count == 0
10317844Sralph 		  || sys[i].st_type == SS_WRONGTIME
10417844Sralph 		  || (sys[i].st_type == SS_INPROGRESS && sys[i].locked))) {
10517844Sralph 			printf("\n");
10613693Ssam 			continue;
10713693Ssam 		}
10813693Ssam 		t = (sys[i].st_lastime +sys[i].st_retry) - curtime;
10917844Sralph 		if (-t < ONEDAY*2 && sys[i].st_type != SS_FAIL)
11017844Sralph 			t = 0;
11117844Sralph 
11217844Sralph 		if (sys[i].st_count > MAXRECALLS)
11317844Sralph 			printf("at MAX RECALLS");
11417844Sralph 		else if (-t >= ONEDAY*2)
11517844Sralph 			printf("%ld days ago", (long)-t/ONEDAY);
11617844Sralph 		else if (t <= 0)
11717844Sralph 			printf("Retry time reached");
11817844Sralph 		else if (t < 60)
11917844Sralph 			printf("Retry time %ld sec%s", (long)(t%60),
12017844Sralph 					(t%60)!=1? "s": "");
12113693Ssam 		else
12217844Sralph 			printf("Retry time %ld min%s", (long)(t/60),
12317844Sralph 				(t/60)!=1? "s": "");
12413693Ssam 		if(sys[i].st_count > 1)
12517844Sralph 			printf(" Count: %d\n", sys[i].st_count);
12617844Sralph 		else
12717844Sralph 			printf("\n");
12813693Ssam 	}
12917844Sralph 	if (xqtisrunning)
13017844Sralph 		printf("\nUuxqt is running\n");
13113693Ssam 	exit(0);
13213693Ssam }
13317844Sralph 
dodir(dnam,prfx,flen,fchr,type)13446228Storek dodir(dnam, prfx, flen, fchr, type)
13546228Storek char *dnam, *prfx;
13646228Storek int flen;
13746228Storek char fchr;
13846228Storek int type;
13917844Sralph {
14013693Ssam 	register struct direct *dentp;
14113693Ssam 	register DIR *dirp;
14223690Sbloom 	register int i, fnamlen, plen;
14323690Sbloom 	char	fnam[MAXNAMLEN+1];
14413693Ssam 
14513693Ssam 	plen = strlen(prfx);
14617844Sralph 	if(chdir(dnam) < 0) {
14717844Sralph 		perror(dnam);
14813693Ssam 		exit(1);
14913693Ssam 	}
15017844Sralph 	if ((dirp = opendir(".")) == NULL) {
15117844Sralph 		perror(dnam);
15213693Ssam 		exit(1);
15313693Ssam 	}
15417844Sralph 	while((dentp = readdir(dirp)) != NULL) {
15517844Sralph 		if(*dentp->d_name == '.')
15613693Ssam 			continue;
15713693Ssam 		if(strncmp(dentp->d_name, prfx, plen) != SAME) {
15813693Ssam 			fprintf(stderr, "strange file (%s) in %s\n",
15913693Ssam 				dentp->d_name, dnam);
16013693Ssam 			continue;
16113693Ssam 		}
16213693Ssam 		strcpy(fnam, &dentp->d_name[plen]);
16323690Sbloom 		fnamlen = strlen(fnam);
16423690Sbloom 		if(flen > 0) {
16523690Sbloom 			fnamlen -= flen;
16646228Storek 			fnam[fnamlen] = '\0';
16746228Storek 			fnamlen = MAXBASENAME; /* yes, after = '\0'*/
16823690Sbloom 		} else {
16923690Sbloom 			for(; fnamlen>0; --fnamlen) {
17023690Sbloom 				if(fnam[fnamlen] == fchr) {
17146228Storek 					fnam[fnamlen] = '\0';
17217844Sralph 					break;
17317844Sralph 				}
17413693Ssam 			}
17523690Sbloom 			fnamlen = MAXBASENAME;
17623690Sbloom 		}
17717844Sralph 		for(i=0; i<sndx; ++i) {
17823690Sbloom 			if(strncmp(fnam, sys[i].name, fnamlen) == SAME) {
17917844Sralph 				++sys[i].cntr[type];
18013693Ssam 				break;
18113693Ssam 			}
18213693Ssam 		}
18317844Sralph 		if(i == sndx) {
18417844Sralph 			strcpy(sys[i].name, fnam);
18513693Ssam 			++sys[i].cntr[type];
18633577Srick 			if(++sndx >=  NSYSTEM) {
18733577Srick 				sndx = NSYSTEM-1;
18833577Srick 				fprintf(stderr,"Too many system names.\n");
18933577Srick 			}
19013693Ssam 		}
19113693Ssam 	}
19213693Ssam 	closedir(dirp);
19313693Ssam }
19417844Sralph 
getstst(sdir)19517844Sralph getstst(sdir)
19613693Ssam char *sdir;
19717844Sralph {
19817844Sralph 	register int i, csys;
19913693Ssam 	register char *tp;
20013693Ssam 	char	fnam[MAXNAMLEN+1], buff[128];
20113693Ssam 	register struct	direct *dentp;
20213693Ssam 	register DIR *dirp;
20313693Ssam 	register FILE *st;
20417844Sralph 	struct stat stbuf;
20523730Sbloom 	long atol();
20613693Ssam 
20717844Sralph 	if (chdir(sdir) < 0) {
20817844Sralph 		perror(sdir);
20913693Ssam 		exit(1);
21013693Ssam 	}
21118626Sralph 	if ((dirp = opendir(LOCKDIR)) == NULL) {
21217844Sralph 		perror(sdir);
21313693Ssam 		exit(1);
21413693Ssam 	}
21517844Sralph 	while ((dentp = readdir(dirp)) != NULL) {
21625145Sbloom 		if (strcmp(&dentp->d_name[5], X_LOCK) == SAME) {
21717844Sralph 			xqtisrunning++;
21813693Ssam 			continue;
21917844Sralph 		}
22017844Sralph 		if(strncmp(dentp->d_name, "LCK..", 5) == SAME) {
22117844Sralph 			if(strncmp(&dentp->d_name[5], "tty", 3) == SAME ||
22213693Ssam 			   strncmp(&dentp->d_name[5], "cul", 3) == SAME)
22313693Ssam 				continue;
22413693Ssam 			strcpy(fnam, dentp->d_name);
22517844Sralph 			for(csys=0; csys<sndx; ++csys) {
22623690Sbloom 				if(strncmp(&fnam[5], sys[csys].name, SYSNSIZE)
22723690Sbloom 					== SAME)
22813693Ssam 					break;
22913693Ssam 			}
23023690Sbloom 			strcpy(sys[csys].name, &fnam[5]);
23117844Sralph 			if(csys == sndx) {
23213693Ssam 				++sndx;
23313693Ssam 			}
23417844Sralph 			if (stat(fnam, &stbuf) < 0)
23517844Sralph 				sys[csys].locked = 1;
23617844Sralph 			else
23717844Sralph 				sys[csys].locked = stbuf.st_mtime;
23813693Ssam 			continue;
23913693Ssam 		}
24017844Sralph 	}
24117844Sralph 	closedir(dirp);
24217844Sralph 	if (chdir("STST") < 0) {
24317844Sralph 		perror("STST");
24417844Sralph 		exit(1);
24517844Sralph 	}
24617844Sralph 	if ((dirp = opendir(".")) == NULL) {
24717844Sralph 		perror("STST");
24817844Sralph 		exit(1);
24917844Sralph 	}
25017844Sralph 	while ((dentp = readdir(dirp)) != NULL) {
25117844Sralph 		if(*dentp->d_name == '.')
25217844Sralph 			continue;
25317844Sralph 		strcpy(fnam, dentp->d_name);
25417844Sralph 		for(csys=0; csys<sndx; ++csys) {
25523690Sbloom 			if(strncmp(fnam, sys[csys].name, SYSNSIZE) == SAME)
25617844Sralph 				break;
25713693Ssam 		}
25823690Sbloom 		strcpy(sys[csys].name, fnam);
25917844Sralph 		if(csys == sndx) {
26017844Sralph 			++sndx;
26117844Sralph 		}
26217844Sralph 		if((st = fopen(fnam, "r")) == NULL) {
26323690Sbloom 			sys[csys].stst[0] = '\0';
26417844Sralph 			continue;
26517844Sralph 		}
26623690Sbloom 		buff[0] = '\0';
26717844Sralph 		fgets(buff, sizeof(buff), st);
26817844Sralph 		fclose(st);
26917844Sralph 		if(tp = rindex(buff, ' '))
27046228Storek 			*tp = '\0';		/* drop system name */
27117844Sralph 		else
27217844Sralph 			continue;
27317844Sralph 		for(i=0, tp=buff;  i<4;  ++i, ++tp)
27417844Sralph 			if((tp = index(tp, ' ')) == NULL)
27517844Sralph 				break;
27617844Sralph 		if(i != 4)
27717844Sralph 			continue;
27817844Sralph 		strncpy(sys[csys].stst, tp, sizeof(sys[csys].stst));
27917844Sralph 		tp = buff;
28017844Sralph 		sys[csys].st_type = atoi(tp);
28117844Sralph 		tp = index(tp+1, ' ');
28217844Sralph 		sys[csys].st_count = atoi(tp+1);
28317844Sralph 		tp = index(tp+1, ' ');
28423730Sbloom 		sys[csys].st_lastime = atol(tp+1);
28517844Sralph 		tp = index(tp+1, ' ');
28623730Sbloom 		sys[csys].st_retry = atol(tp+1);
28713693Ssam 	}
28813693Ssam }
28913693Ssam /*
29013693Ssam  * Return the ptr in sp at which the character c appears;
29113693Ssam  * NULL if not found
29213693Ssam  */
29313693Ssam 
29413693Ssam char *
index(sp,c)29513693Ssam index(sp, c)
29613693Ssam register char *sp, c;
29713693Ssam {
29813693Ssam 	do {
29913693Ssam 		if (*sp == c)
30017844Sralph 			return sp;
30113693Ssam 	} while (*sp++);
30217844Sralph 	return NULL;
30313693Ssam }
30413693Ssam 
30513693Ssam /*
30613693Ssam  * Return the ptr in sp at which the character c last
30713693Ssam  * appears; NULL if not found
30813693Ssam */
30913693Ssam 
31013693Ssam char *
rindex(sp,c)31113693Ssam rindex(sp, c)
31213693Ssam register char *sp, c;
31313693Ssam {
31413693Ssam 	register char *r;
31513693Ssam 
31613693Ssam 	r = NULL;
31713693Ssam 	do {
31813693Ssam 		if (*sp == c)
31913693Ssam 			r = sp;
32013693Ssam 	} while (*sp++);
32117844Sralph 	return r;
32213693Ssam }
323