xref: /original-bsd/old/berknet/netq.c (revision 2301fdfb)
1 static char sccsid[] = "@(#)netq.c	4.2	(Berkeley)	09/12/82";
2 
3 /* netq - print the netq send queue */
4 /* netq [-] [mach] */
5 /* must be setuid root */
6 
7 /* sccs id variable */
8 static char *netq_sid = "@(#)netq.c	1.5";
9 
10 # include "defs.h"
11 /* this is an internal table size -- only STSIZE entries will
12 	be sorted correctly */
13 # define STSIZE 150
14 
15 static DIR *df;
16 static char jname[16], printlong;
17 static struct table {
18 	char name[16];
19 	long filesize;
20 	} stack[STSIZE], temp;
21 static int stptr = 0;
22 static char mach,visit[MAXINX];
23 static char netcmd1[] =	NETCMD1;
24 static int hisuid,sumj,nsumj;
25 static long sumb, nsumb;
26 static struct stat statbuf;
27 
28 char _sobuf[BUFSIZ];
29 main(argc,argv)
30   char **argv; {
31 	int i;
32 	setbuf(stdout,_sobuf);
33 	hisuid = getuid();
34 	hisuid = uidmask(hisuid);
35 	if(stat(netcmd,&statbuf) >= 0)
36 		if((statbuf.st_mode & 07) == 0){
37 			printf("Network is down\n");
38 			exit(EX_UNAVAILABLE);
39 			}
40 	else if(stat(netcmd1,&statbuf) >= 0)
41 		if((statbuf.st_mode & 07) == 0){
42 			printf("Network is down\n");
43 			exit(EX_UNAVAILABLE);
44 			}
45 	while(argc > 1){
46 		switch(argv[1][0]){
47 		case '-': printlong++; break;
48 		default: mach = lookup(argv[1]);
49 			if(mach > 0 && machtype[chtoinx(mach)] == 0)mach = 0;
50 			break;
51 		}
52 		argc--, argv++;
53 		}
54 	if(mach){
55 		mach = gothru(local,mach); /* list to directly conn. machine */
56 		if(mach == 0){
57 			fprintf(stderr,"That machine not directly connected.\n");
58 			exit(EX_USAGE);
59 		}
60 		senddir[strlen(senddir)-1] = mach;
61 		if(chdir(senddir) < 0){
62 			perror(senddir);
63 			exit(EX_OSFILE);
64 		}
65 		pdir(senddir);
66 	}
67 	else for(i = 0; i < MAXINX; i++)
68 		if((mach = gothru(local,inxtoch(i))) && !visit[chtoinx(mach)]){
69 			visit[chtoinx(mach)] = 1;
70 			senddir[strlen(senddir)-1] = mach;
71 			if(chdir(senddir) < 0)continue;
72 			pdir(senddir);
73 			printf("---\n");
74 			}
75 	fflush(stdout);
76 	}
77 static pdir(str)
78   char *str; {
79 	int i;
80 	char more = 0, *cp;
81 	char listrest = 0;
82 	int (*compar)();
83 	char printhead = 0;
84 	register struct direct *dp;
85 	df = opendir(str);
86 	if(df == NULL){
87 		perror(str);
88 		exit(EX_OSFILE);
89 		}
90 	stptr = 0;
91 	while((dp = readdir(df)) != NULL){
92 		if(dp->d_name[0] != 'c'
93 		|| dp->d_name[1] != 'f'
94 		|| stat(dp->d_name,&statbuf) < 0)
95 			continue;
96 		if(mach != dp->d_name[2])continue;
97 		dp->d_name[0] = 'd';
98 		if(stat(dp->d_name,&statbuf) < 0)continue;
99 #ifdef MAXSENDQ
100 		if( stptr >= MAXSENDQ ) {
101 			listrest++;
102 			break;
103 		}
104 #endif
105 
106 		if(!insert(dp->d_name,getsize(&statbuf))){
107 			more++;
108 			break;
109 			}
110 		}
111 	if(stptr == 0){
112 		printf("Network queue to/thru %s is empty.\n",longname(mach));
113 		closedir(df);
114 		return;
115 		}
116 	cp = (char *)&(stack[0].name[0]);
117 	sort(cp,stptr,sizeof temp,compar);
118 	printf("Network queue to/thru %s:\n",longname(mach));
119 	for(i = 0; i < stptr; i++){ /* screen size */
120 		strcpy(jname,stack[i].name);
121 		jname[0] = 'd';
122 		if(stat(jname,&statbuf) < 0)
123 			continue;
124 		if(printlong || guid(statbuf.st_uid,statbuf.st_gid) == hisuid){
125 			if(!printhead){
126 				printhead = 1;
127 				printf( "From       To           Len  Code   Time          Command\n");
128 				};
129 			process();
130 			}
131 		else summarize( stack[i].filesize );
132 		}
133 # ifdef MAXSENDQ
134 	if( listrest )
135 		listem(dp);
136 # endif
137 	closedir(df);
138 	printsum();
139 	if(more)printf("   ... more ...\n");
140 	}
141 summarize( size )
142 long size;
143 	{
144 #ifndef DONTHOLDBIG
145 	if( size > MAXDAYFILE ) {
146 		nsumj++;
147 		nsumb += size;
148 	}
149 	else {
150 		sumj++;
151 		sumb += size;
152 	}
153 #else
154 	sumb += size;
155 	sumj++;
156 #endif
157 	}
158 printsum(){
159 #ifndef DONTHOLDBIG
160 	if( sumj != 0 || nsumj != 0 ){
161 		printf("day jobs: %d request%s (%ld bytes)",
162 			sumj, (sumj > 1 ? "s" : ""), sumb);
163 		if(nsumj > 0)
164 			printf("; night jobs: %d request%s (%ld bytes)",
165 				nsumj, ( nsumj > 1 ? "s" : ""), nsumb );
166 		putchar('\n');
167 		nsumb = 0l;
168 		nsumj = 0;
169 	}
170 #else
171 	if(sumj != 0){
172 		printf("%d request%s, %ld bytes\n",
173 			sumj,(sumj > 1 ? "s" : ""),sumb);
174 		}
175 #endif
176 	sumj = 0;
177 	sumb = 0l;
178 	}
179 process(){
180 	static struct header hd;
181 	static char nightheader = 0;
182 	FILE *look;
183 	char *cp;
184 	long size;
185 
186 	printsum();
187 	look = fopen(jname,"r");
188 	if(look == NULL)
189 		return;
190 	readhdfd(&hd,look);
191 	fclose(look);
192 	if(hd.hd_snfrom[0] == 0)strcat(hd.hd_addrfrom,"Internal");
193 	expandcc(hd.hd_sttyname);
194 	cp = ctime(&statbuf.st_mtime);
195 	cp[strlen(cp)-9] = 0;
196 	jname[3] = jname[2];
197 	size = getsize(&statbuf);
198 	if(size >= MAXDAYFILE && ! nightheader){
199 		printf("(> %ld bytes, will only transfer between Midnight and 6AM.)\n", MAXDAYFILE);
200 		nightheader = 1;
201 		};
202 	printf("%-10s %-10s %6ld %s %s  %-.27s\n",
203 		hd.hd_addrfrom,hd.hd_addrto,size,jname+3,cp+4,hd.hd_scmdvirt);
204 	}
205 readhdfd(phd,fd)
206 register struct header *phd;
207 FILE *fd;
208 {
209 	char sbuf[BUFSIZ], parmlist[PARMLIST];
210 	char *s, cflag;
211 	int c, i;
212 	phd->hd_code = phd->hd_mchto = phd->hd_snto[0] = phd->hd_sinfile[0] = 0;
213 	phd->hd_soutfile[0] = phd->hd_srespfile[0] = phd->hd_snfrom[0] = 0;
214 	phd->hd_scmdact[0] = 0;
215 	for(i=0;i<20;i++)phd->hd_sttyname[i] = 0;
216 
217 	phd->hd_code = ngetc(fd);
218 	if(phd->hd_code == 0)return;
219 	phd->hd_mchto = ngetc(fd);
220 	phd->hd_mchfrom = ngetc(fd);	/* from machine */
221 	phd->hd_vmajor = ngetc(fd);
222 	phd->hd_vminor = ngetc(fd);
223 	ngets(phd->hd_snto,NS,fd);
224 	ngets(phd->hd_spasswd,20,fd);			/* passwd */
225 	ngets(phd->hd_sinfile,FNS,fd);
226 	ngets(phd->hd_soutfile,FNS,fd);
227 	ngets(phd->hd_srespfile,FNS,fd);
228 	ngets(phd->hd_snfrom,NS,fd);
229 	ngets(phd->hd_sttyname,20,fd);
230 	cflag = ngetc(fd);
231 	ngets(sbuf,BUFSIZ,fd);				/* lttytime */
232 	ngets(parmlist,PARMLIST,fd);			/* jobno */
233 	parseparmlist(parmlist);
234 	ngets(sbuf,BUFSIZ,fd);				/* timesent */
235 	s = phd->hd_scmdact;
236 	while((c = getc(fd)) != EOF && c != '\n'){
237 		if(c == '\\')c = getc(fd);
238 		*s++ = c;
239 		}
240 	*s = 0;
241 	s = phd->hd_scmdvirt;
242 	while((c = getc(fd)) != EOF && c != '\n'){
243 		if(c == '\\')c = getc(fd);
244 		*s++ = c;
245 		}
246 	*s = 0;
247 	if(phd->hd_scmdvirt[0] == 0)strcpy(phd->hd_scmdvirt,phd->hd_scmdact);
248 	sprintf(phd->hd_addrfrom,"%c:%s",phd->hd_mchfrom,phd->hd_snfrom);
249 	sprintf(phd->hd_addrto  ,"%c:%s",phd->hd_mchto  ,phd->hd_snto  );
250 }
251 ngetc(fd)
252 FILE *fd;
253 {
254 	char b[3];
255 	if(feof(fd))return(0);
256 	if(fread(b,1,3,fd) != 3) return(0);
257 	return(b[0]);
258 	}
259 /* read a string s of max length maxlen out of queue file */
260 ngets(s,maxlen,fd)
261 int maxlen;
262 char *s;
263 FILE *fd;
264 {
265 	int i;
266 	if(feof(fd))return;
267 	for(;;){
268 		i = getc(fd);
269 		if(i == EOF){
270 			*s = 0;
271 			return;
272 			}
273 		*s = i;
274 		if(*s == '\\')*s = getc(fd);
275 		if(*s == ' ')break;
276 		if(maxlen-- > 0)s++;
277 		}
278 	*s = 0;
279 	getc(fd);
280 	}
281 insert(f,t)
282   char *f;
283   long t; {
284 	strcpy(stack[stptr].name,f);
285 	stack[stptr++].filesize = t;
286 	return(stptr <= STSIZE);
287 	}
288 compar(a,b)
289   register struct table *a,*b; {
290 	if(a->filesize < b->filesize)return(-1);
291 	if(a->filesize > b->filesize)return(1);
292 	return(0);
293 	}
294 sort(){		/* use this cause qsort doesn't work */
295 	register int i,j;
296 	for(i=0; i< stptr-1; i++)
297 		for(j=i+1;j<stptr;j++)
298 			if(compar(&stack[i],&stack[j]) > 0)
299 				swap(&stack[i],&stack[j]);
300 	}
301 swap(a,b)
302   register struct table *a, *b; {
303 	char str[16];
304 	long t;
305 	strcpy(str,a->name);
306 	t = a->filesize;
307 	strcpy(a->name,b->name);
308 	a->filesize = b->filesize;
309 	strcpy(b->name,str);
310 	b->filesize = t;
311 	}
312 # ifdef MAXSENDQ
313 listem(dp)
314 register struct direct *dp; {
315 
316 	do {
317 		if(dp->d_name[0] != 'c'
318 		|| dp->d_name[1] != 'f'
319 		|| stat( dp->d_name, &statbuf ) < 0 )
320 			continue;
321 		if( mach != dp->d_name[2] )
322 			continue;
323 		dp->d_name[0] = 'd';
324 		if( stat( dp->d_name, &statbuf ) < 0 )
325 			continue;
326 		if( printlong || guid( statbuf.st_uid, statbuf.st_gid) == hisuid )
327 			process();
328 		else
329 			summarize( getsize( &statbuf ) );
330 	} while((dp = readdir(df)) != NULL);
331 
332 	return;
333 }
334 # endif MAXSENDQ
335