xref: /original-bsd/usr.bin/uucp/uusnap/uusnap.c (revision 31e799e3)
1 /*
2  * Copyright (c) 1988 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Adams.
7  *
8  * originally by RJKing WECo-MG6565 May 83
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that the above copyright notice and this paragraph are
12  * duplicated in all such forms and that any documentation,
13  * advertising materials, and other materials related to such
14  * distribution and use acknowledge that the software was developed
15  * by the University of California, Berkeley.  The name of the
16  * University may not be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  */
22 
23 #ifndef lint
24 char copyright[] =
25 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
26  All rights reserved.\n";
27 #endif /* not lint */
28 
29 #ifndef lint
30 static char sccsid[] = "@(#)uusnap.c	5.11 (Berkeley) 02/02/91";
31 #endif /* not lint */
32 
33 #include "uucp.h"
34 #include <sys/stat.h>
35 #ifdef	NDIR
36 #include "ndir.h"
37 #else
38 #include <sys/dir.h>
39 #endif
40 #include <ctype.h>
41 
42 #define	NSYSTEM	300				/* max # of systems queued */
43 
44 #define	CMDSLEN	5				/* Length of trailer */
45 #define	DATALEN	5				/* Length of trailer */
46 #define	XEQTLEN	5				/* Length of trailer */
47 #define	NUMCTRS	3				/* # file types to count */
48 #define	CMDTYPE	0				/* Index into scnt.cntr */
49 #define	DATTYPE	1				/* Index into scnt.cntr */
50 #define	XEQTYPE	2				/* Index into scnt.cntr */
51 
52 struct	scnt {					/* System count structure */
53 		char	name[MAXBASENAME+1];	/* Name of system */
54 		short	cntr[NUMCTRS];		/* Count */
55 		char	stst[32];		/* STST Message */
56 		time_t	locked;			/* If LCK..sys present */
57 		int	st_type;		/* STST Type */
58 		int	st_count;		/* STST Count */
59 		time_t	st_lastime;		/* STST Last time tried */
60 		time_t	st_retry;		/* STST Secs to retry */
61 	     };
62 
63 int	sndx;					/* Number of systems */
64 struct	scnt	sys[NSYSTEM];			/* Systems queued */
65 int xqtisrunning = 0;
66 
67 main()
68 {
69 	register int i, j, nlen = 0;
70 	time_t	curtime, t;
71 
72 	dodir(CMDSDIR, "C.", CMDSLEN, '\0', CMDTYPE);
73 	dodir(DATADIR, "D.", DATALEN, '\0', DATTYPE);
74 	dodir(XEQTDIR, "X.", XEQTLEN, 'X', XEQTYPE);
75 	getstst(SPOOL);
76 	time(&curtime);
77 	for(i=0; i<sndx; ++i)
78 		if((j = strlen(sys[i].name)) > nlen)
79 			nlen = j;
80 	for(i=0; i<sndx; ++i) {
81 		t = (sys[i].st_lastime +sys[i].st_retry) - curtime;
82 
83 		/* decide if STST text is worth printing */
84 		if (-t < ONEDAY*2 && sys[i].st_type == SS_WRONGTIME) {
85 			sys[i].stst[0] = '\0';
86 			if (sys[i].cntr[0]+sys[i].cntr[1]+sys[i].cntr[2] == 0)
87 				continue;	/* ignore entire line */
88 		}
89 
90 		printf("%-*.*s ", nlen, nlen, sys[i].name);
91 		if(sys[i].cntr[CMDTYPE])
92 			printf("%3.d Cmd%s ", sys[i].cntr[CMDTYPE],
93 				sys[i].cntr[CMDTYPE]>1?"s":" ");
94 		else
95 			printf("   ---   ");
96 		if(sys[i].cntr[DATTYPE])
97 			printf("%3.d Data ", sys[i].cntr[DATTYPE]);
98 		else
99 			printf("   ---   ");
100 		if(sys[i].cntr[XEQTYPE])
101 			printf("%3.d Xqt%s ", sys[i].cntr[XEQTYPE],
102 				sys[i].cntr[XEQTYPE]>1?"s":" ");
103 		else
104 			printf("   ---   ");
105 		if(*sys[i].stst == '\0' || sys[i].locked > sys[i].st_lastime) {
106 			if(sys[i].locked)
107 				printf("LOCKED\n");
108 			else
109 				printf("\n");
110 			continue;
111 		}
112 		printf("%s  ", sys[i].stst);
113 		/* decide if STST info is worth pursuing */
114 		if (-t < ONEDAY*2 && (sys[i].st_count == 0
115 		  || sys[i].st_type == SS_WRONGTIME
116 		  || (sys[i].st_type == SS_INPROGRESS && sys[i].locked))) {
117 			printf("\n");
118 			continue;
119 		}
120 		t = (sys[i].st_lastime +sys[i].st_retry) - curtime;
121 		if (-t < ONEDAY*2 && sys[i].st_type != SS_FAIL)
122 			t = 0;
123 
124 		if (sys[i].st_count > MAXRECALLS)
125 			printf("at MAX RECALLS");
126 		else if (-t >= ONEDAY*2)
127 			printf("%ld days ago", (long)-t/ONEDAY);
128 		else if (t <= 0)
129 			printf("Retry time reached");
130 		else if (t < 60)
131 			printf("Retry time %ld sec%s", (long)(t%60),
132 					(t%60)!=1? "s": "");
133 		else
134 			printf("Retry time %ld min%s", (long)(t/60),
135 				(t/60)!=1? "s": "");
136 		if(sys[i].st_count > 1)
137 			printf(" Count: %d\n", sys[i].st_count);
138 		else
139 			printf("\n");
140 	}
141 	if (xqtisrunning)
142 		printf("\nUuxqt is running\n");
143 	exit(0);
144 }
145 
146 dodir(dnam, prfx, flen, fchr, type)
147 char *dnam, *prfx;
148 int flen;
149 char fchr;
150 int type;
151 {
152 	register struct direct *dentp;
153 	register DIR *dirp;
154 	register int i, fnamlen, plen;
155 	char	fnam[MAXNAMLEN+1];
156 
157 	plen = strlen(prfx);
158 	if(chdir(dnam) < 0) {
159 		perror(dnam);
160 		exit(1);
161 	}
162 	if ((dirp = opendir(".")) == NULL) {
163 		perror(dnam);
164 		exit(1);
165 	}
166 	while((dentp = readdir(dirp)) != NULL) {
167 		if(*dentp->d_name == '.')
168 			continue;
169 		if(strncmp(dentp->d_name, prfx, plen) != SAME) {
170 			fprintf(stderr, "strange file (%s) in %s\n",
171 				dentp->d_name, dnam);
172 			continue;
173 		}
174 		strcpy(fnam, &dentp->d_name[plen]);
175 		fnamlen = strlen(fnam);
176 		if(flen > 0) {
177 			fnamlen -= flen;
178 			fnam[fnamlen] = '\0';
179 			fnamlen = MAXBASENAME; /* yes, after = '\0'*/
180 		} else {
181 			for(; fnamlen>0; --fnamlen) {
182 				if(fnam[fnamlen] == fchr) {
183 					fnam[fnamlen] = '\0';
184 					break;
185 				}
186 			}
187 			fnamlen = MAXBASENAME;
188 		}
189 		for(i=0; i<sndx; ++i) {
190 			if(strncmp(fnam, sys[i].name, fnamlen) == SAME) {
191 				++sys[i].cntr[type];
192 				break;
193 			}
194 		}
195 		if(i == sndx) {
196 			strcpy(sys[i].name, fnam);
197 			++sys[i].cntr[type];
198 			if(++sndx >=  NSYSTEM) {
199 				sndx = NSYSTEM-1;
200 				fprintf(stderr,"Too many system names.\n");
201 			}
202 		}
203 	}
204 	closedir(dirp);
205 }
206 
207 getstst(sdir)
208 char *sdir;
209 {
210 	register int i, csys;
211 	register char *tp;
212 	char	fnam[MAXNAMLEN+1], buff[128];
213 	register struct	direct *dentp;
214 	register DIR *dirp;
215 	register FILE *st;
216 	struct stat stbuf;
217 	long atol();
218 
219 	if (chdir(sdir) < 0) {
220 		perror(sdir);
221 		exit(1);
222 	}
223 	if ((dirp = opendir(LOCKDIR)) == NULL) {
224 		perror(sdir);
225 		exit(1);
226 	}
227 	while ((dentp = readdir(dirp)) != NULL) {
228 		if (strcmp(&dentp->d_name[5], X_LOCK) == SAME) {
229 			xqtisrunning++;
230 			continue;
231 		}
232 		if(strncmp(dentp->d_name, "LCK..", 5) == SAME) {
233 			if(strncmp(&dentp->d_name[5], "tty", 3) == SAME ||
234 			   strncmp(&dentp->d_name[5], "cul", 3) == SAME)
235 				continue;
236 			strcpy(fnam, dentp->d_name);
237 			for(csys=0; csys<sndx; ++csys) {
238 				if(strncmp(&fnam[5], sys[csys].name, SYSNSIZE)
239 					== SAME)
240 					break;
241 			}
242 			strcpy(sys[csys].name, &fnam[5]);
243 			if(csys == sndx) {
244 				++sndx;
245 			}
246 			if (stat(fnam, &stbuf) < 0)
247 				sys[csys].locked = 1;
248 			else
249 				sys[csys].locked = stbuf.st_mtime;
250 			continue;
251 		}
252 	}
253 	closedir(dirp);
254 	if (chdir("STST") < 0) {
255 		perror("STST");
256 		exit(1);
257 	}
258 	if ((dirp = opendir(".")) == NULL) {
259 		perror("STST");
260 		exit(1);
261 	}
262 	while ((dentp = readdir(dirp)) != NULL) {
263 		if(*dentp->d_name == '.')
264 			continue;
265 		strcpy(fnam, dentp->d_name);
266 		for(csys=0; csys<sndx; ++csys) {
267 			if(strncmp(fnam, sys[csys].name, SYSNSIZE) == SAME)
268 				break;
269 		}
270 		strcpy(sys[csys].name, fnam);
271 		if(csys == sndx) {
272 			++sndx;
273 		}
274 		if((st = fopen(fnam, "r")) == NULL) {
275 			sys[csys].stst[0] = '\0';
276 			continue;
277 		}
278 		buff[0] = '\0';
279 		fgets(buff, sizeof(buff), st);
280 		fclose(st);
281 		if(tp = rindex(buff, ' '))
282 			*tp = '\0';		/* drop system name */
283 		else
284 			continue;
285 		for(i=0, tp=buff;  i<4;  ++i, ++tp)
286 			if((tp = index(tp, ' ')) == NULL)
287 				break;
288 		if(i != 4)
289 			continue;
290 		strncpy(sys[csys].stst, tp, sizeof(sys[csys].stst));
291 		tp = buff;
292 		sys[csys].st_type = atoi(tp);
293 		tp = index(tp+1, ' ');
294 		sys[csys].st_count = atoi(tp+1);
295 		tp = index(tp+1, ' ');
296 		sys[csys].st_lastime = atol(tp+1);
297 		tp = index(tp+1, ' ');
298 		sys[csys].st_retry = atol(tp+1);
299 	}
300 }
301 /*
302  * Return the ptr in sp at which the character c appears;
303  * NULL if not found
304  */
305 
306 char *
307 index(sp, c)
308 register char *sp, c;
309 {
310 	do {
311 		if (*sp == c)
312 			return sp;
313 	} while (*sp++);
314 	return NULL;
315 }
316 
317 /*
318  * Return the ptr in sp at which the character c last
319  * appears; NULL if not found
320 */
321 
322 char *
323 rindex(sp, c)
324 register char *sp, c;
325 {
326 	register char *r;
327 
328 	r = NULL;
329 	do {
330 		if (*sp == c)
331 			r = sp;
332 	} while (*sp++);
333 	return r;
334 }
335