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