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