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