xref: /original-bsd/usr.bin/uucp/uusnap/uusnap.c (revision e78e7ec3)
1 #ifndef lint
2 static char sccsid[] = "@(#)uusnap.c	5.4 (Berkeley) 04/10/85";
3 #endif
4 
5 /*
6  *	Uusnap - displays a snapshot of the uucp system.
7  *					RJKing WECo-MG6565 May 83
8  */
9 
10 #include "uucp.h"
11 #include <sys/stat.h>
12 #ifdef	NDIR
13 #include "ndir.h"
14 #else
15 #include <sys/dir.h>
16 #endif
17 
18 #ifndef	SYSBUF
19 char SYSBUF[BUFSIZ];
20 #endif
21 
22 #define	NSYSTEM	100				/* max # of systems queued */
23 
24 #define	CMDSLEN	5				/* Length of trailer */
25 #define	DATALEN	5				/* Length of trailer */
26 #define	XEQTLEN	5				/* Length of trailer */
27 #define	NUMCTRS	3				/* # file types to count */
28 #define	CMDTYPE	0				/* Index into scnt.cntr */
29 #define	DATTYPE	1				/* Index into scnt.cntr */
30 #define	XEQTYPE	2				/* Index into scnt.cntr */
31 
32 extern	char *index(), *rindex(), *strcpy(), *strncpy();;
33 extern	long atol();
34 extern	time_t	time();
35 
36 struct	scnt {					/* System count structure */
37 		char	name[16];		/* Name of system */
38 		short	cntr[NUMCTRS];		/* Count */
39 		char	stst[32];		/* STST Message */
40 		time_t	locked;			/* If LCK..sys present */
41 		int	st_type;		/* STST Type */
42 		int	st_count;		/* STST Count */
43 		time_t	st_lastime;		/* STST Last time tried */
44 		time_t	st_retry;		/* STST Secs to retry */
45 	     };
46 
47 int	sndx;					/* Number of systems */
48 struct	scnt	sys[NSYSTEM];			/* Systems queued */
49 int xqtisrunning = 0;
50 
51 main()
52 {	register int i, j, nlen = 0;
53 	time_t	curtime, t;
54 
55 	setbuf(stdout, SYSBUF);
56 	scandir(CMDSDIR, "C.", CMDSLEN, NULL, CMDTYPE);
57 	scandir(DATADIR, "D.", DATALEN, NULL, DATTYPE);
58 	scandir(XEQTDIR, "X.", XEQTLEN, 'X', XEQTYPE);
59 	getstst(SPOOL);
60 	time(&curtime);
61 	for(i=0; i<sndx; ++i)
62 		if((j = strlen(sys[i].name)) > nlen)
63 			nlen = j;
64 	for(i=0; i<sndx; ++i) {
65 		t = (sys[i].st_lastime +sys[i].st_retry) - curtime;
66 
67 		/* decide if STST text is worth printing */
68 		if (-t < ONEDAY*2 && sys[i].st_type == SS_WRONGTIME) {
69 			sys[i].stst[0] = '\0';
70 			if (sys[i].cntr[0]+sys[i].cntr[1]+sys[i].cntr[2] == 0)
71 				continue;	/* ignore entire line */
72 		}
73 
74 		printf("%-*.*s ", nlen, nlen, sys[i].name);
75 		if(sys[i].cntr[CMDTYPE])
76 			printf("%3.d Cmd%s ", sys[i].cntr[CMDTYPE],
77 				sys[i].cntr[CMDTYPE]>1?"s":" ");
78 		else
79 			printf("   ---   ");
80 		if(sys[i].cntr[DATTYPE])
81 			printf("%3.d Data ", sys[i].cntr[DATTYPE]);
82 		else
83 			printf("   ---   ");
84 		if(sys[i].cntr[XEQTYPE])
85 			printf("%3.d Xqt%s ", sys[i].cntr[XEQTYPE],
86 				sys[i].cntr[XEQTYPE]>1?"s":" ");
87 		else
88 			printf("   ---   ");
89 		if(*sys[i].stst == NULL || sys[i].locked > sys[i].st_lastime) {
90 			if(sys[i].locked)
91 				printf("LOCKED\n");
92 			else
93 				printf("\n");
94 			continue;
95 		}
96 		printf("%s  ", sys[i].stst);
97 		/* decide if STST info is worth pursuing */
98 		if (-t < ONEDAY*2 && (sys[i].st_count == 0
99 		  || sys[i].st_type == SS_WRONGTIME
100 		  || (sys[i].st_type == SS_INPROGRESS && sys[i].locked))) {
101 			printf("\n");
102 			continue;
103 		}
104 		t = (sys[i].st_lastime +sys[i].st_retry) - curtime;
105 		if (-t < ONEDAY*2 && sys[i].st_type != SS_FAIL)
106 			t = 0;
107 
108 		if (sys[i].st_count > MAXRECALLS)
109 			printf("at MAX RECALLS");
110 		else if (-t >= ONEDAY*2)
111 			printf("%ld days ago", (long)-t/ONEDAY);
112 		else if (t <= 0)
113 			printf("Retry time reached");
114 		else if (t < 60)
115 			printf("Retry time %ld sec%s", (long)(t%60),
116 					(t%60)!=1? "s": "");
117 		else
118 			printf("Retry time %ld min%s", (long)(t/60),
119 				(t/60)!=1? "s": "");
120 		if(sys[i].st_count > 1)
121 			printf(" Count: %d\n", sys[i].st_count);
122 		else
123 			printf("\n");
124 	}
125 	if (xqtisrunning)
126 		printf("\nUuxqt is running\n");
127 	exit(0);
128 }
129 
130 scandir(dnam, prfx, flen, fchr, type)
131 char *dnam, *prfx, fchr;
132 {
133 	register int i, plen;
134 	char	fnam[MAXNAMLEN+1];
135 	register struct direct *dentp;
136 	register DIR *dirp;
137 
138 	plen = strlen(prfx);
139 	if(chdir(dnam) < 0) {
140 		perror(dnam);
141 		exit(1);
142 	}
143 	if ((dirp = opendir(".")) == NULL) {
144 		perror(dnam);
145 		exit(1);
146 	}
147 	while((dentp = readdir(dirp)) != NULL) {
148 		if(*dentp->d_name == '.')
149 			continue;
150 		if(strncmp(dentp->d_name, prfx, plen) != SAME) {
151 			fprintf(stderr, "strange file (%s) in %s\n",
152 				dentp->d_name, dnam);
153 			continue;
154 		}
155 		strcpy(fnam, &dentp->d_name[plen]);
156 		i = strlen(fnam);
157 		if(flen > 0)
158 			fnam[i -flen] = NULL;
159 		else
160 			for(; i>0; --i) {
161 				if(fnam[i] == fchr) {
162 					fnam[i] = NULL;
163 					break;
164 				}
165 			}
166 		for(i=0; i<sndx; ++i) {
167 			if(strcmp(fnam, sys[i].name) == SAME) {
168 				++sys[i].cntr[type];
169 				break;
170 			}
171 		}
172 		if(i == sndx) {
173 			strcpy(sys[i].name, fnam);
174 			++sys[i].cntr[type];
175 			++sndx;
176 		}
177 	}
178 	closedir(dirp);
179 }
180 
181 getstst(sdir)
182 char *sdir;
183 {
184 	register int i, csys;
185 	register char *tp;
186 	char	fnam[MAXNAMLEN+1], buff[128];
187 	register struct	direct *dentp;
188 	register DIR *dirp;
189 	register FILE *st;
190 	struct stat stbuf;
191 
192 	if (chdir(sdir) < 0) {
193 		perror(sdir);
194 		exit(1);
195 	}
196 	if ((dirp = opendir(LOCKDIR)) == NULL) {
197 		perror(sdir);
198 		exit(1);
199 	}
200 	while ((dentp = readdir(dirp)) != NULL) {
201 		if (strcmp(dentp->d_name, X_LOCK) == SAME) {
202 			xqtisrunning++;
203 			continue;
204 		}
205 		if(strncmp(dentp->d_name, "LCK..", 5) == SAME) {
206 			if(strncmp(&dentp->d_name[5], "tty", 3) == SAME ||
207 			   strncmp(&dentp->d_name[5], "cul", 3) == SAME)
208 				continue;
209 			strcpy(fnam, dentp->d_name);
210 			for(csys=0; csys<sndx; ++csys) {
211 				if(strcmp(&fnam[5], sys[csys].name) == SAME)
212 					break;
213 			}
214 			if(csys == sndx) {
215 				strcpy(sys[csys].name, &fnam[5]);
216 				++sndx;
217 			}
218 			if (stat(fnam, &stbuf) < 0)
219 				sys[csys].locked = 1;
220 			else
221 				sys[csys].locked = stbuf.st_mtime;
222 			continue;
223 		}
224 	}
225 	closedir(dirp);
226 	if (chdir("STST") < 0) {
227 		perror("STST");
228 		exit(1);
229 	}
230 	if ((dirp = opendir(".")) == NULL) {
231 		perror("STST");
232 		exit(1);
233 	}
234 	while ((dentp = readdir(dirp)) != NULL) {
235 		if(*dentp->d_name == '.')
236 			continue;
237 		strcpy(fnam, dentp->d_name);
238 		for(csys=0; csys<sndx; ++csys) {
239 			if(strcmp(fnam, sys[csys].name) == SAME)
240 				break;
241 		}
242 		if(csys == sndx) {
243 			strcpy(sys[csys].name, fnam);
244 			++sndx;
245 		}
246 		if((st = fopen(fnam, "r")) == NULL) {
247 			strncpy(sys[csys].stst, "", sizeof(sys[csys].stst));
248 			continue;
249 		}
250 		strncpy(buff, "", sizeof(buff));
251 		fgets(buff, sizeof(buff), st);
252 		fclose(st);
253 		if(tp = rindex(buff, ' '))
254 			*tp = NULL;		/* drop system name */
255 		else
256 			continue;
257 		for(i=0, tp=buff;  i<4;  ++i, ++tp)
258 			if((tp = index(tp, ' ')) == NULL)
259 				break;
260 		if(i != 4)
261 			continue;
262 		strncpy(sys[csys].stst, tp, sizeof(sys[csys].stst));
263 		tp = buff;
264 		sys[csys].st_type = atoi(tp);
265 		tp = index(tp+1, ' ');
266 		sys[csys].st_count = atoi(tp+1);
267 		tp = index(tp+1, ' ');
268 		sys[csys].st_lastime = (time_t)atol(tp+1);
269 		tp = index(tp+1, ' ');
270 		sys[csys].st_retry = (time_t)atol(tp+1);
271 	}
272 }
273 /*
274  * Return the ptr in sp at which the character c appears;
275  * NULL if not found
276  */
277 
278 char *
279 index(sp, c)
280 register char *sp, c;
281 {
282 	do {
283 		if (*sp == c)
284 			return sp;
285 	} while (*sp++);
286 	return NULL;
287 }
288 
289 /*
290  * Return the ptr in sp at which the character c last
291  * appears; NULL if not found
292 */
293 
294 char *
295 rindex(sp, c)
296 register char *sp, c;
297 {
298 	register char *r;
299 
300 	r = NULL;
301 	do {
302 		if (*sp == c)
303 			r = sp;
304 	} while (*sp++);
305 	return r;
306 }
307