xref: /csrg-svn/usr.bin/uucp/uucico/tio.c (revision 17827)
1*17827Sralph #ifndef lint
2*17827Sralph static char sccsid[] = "@(#)tio.c	4.1 (Berkeley) 01/22/85";
3*17827Sralph #endif
4*17827Sralph 
5*17827Sralph #include <sys/types.h>
6*17827Sralph #include <setjmp.h>
7*17827Sralph #include "uucp.h"
8*17827Sralph #include <signal.h>
9*17827Sralph #include <sys/stat.h>
10*17827Sralph #ifdef USG
11*17827Sralph #define ftime time
12*17827Sralph #else V7
13*17827Sralph #include <sys/timeb.h>
14*17827Sralph #endif V7
15*17827Sralph 
16*17827Sralph extern int pkfail();
17*17827Sralph extern	time_t	time();
18*17827Sralph #define TPACKSIZE	512
19*17827Sralph #define TBUFSIZE	1024
20*17827Sralph #define min(a,b)	(((a)<(b))?(a):(b))
21*17827Sralph 
22*17827Sralph /*
23*17827Sralph  *	htonl is a function that converts a long from host
24*17827Sralph  *		order to network order
25*17827Sralph  *	ntohl is a function that converts a long from network
26*17827Sralph  *		order to host order
27*17827Sralph  *
28*17827Sralph  *	network order is 		0 1 2 3 (bytes in a long)
29*17827Sralph  *	host order on a vax is		3 2 1 0
30*17827Sralph  *	host order on a pdp11 is	1 0 3 2
31*17827Sralph  *	host order on a 68000 is	0 1 2 3
32*17827Sralph  *	most other machines are		0 1 2 3
33*17827Sralph  */
34*17827Sralph 
35*17827Sralph struct tbuf {
36*17827Sralph 	long t_nbytes;
37*17827Sralph 	char t_data[TBUFSIZE];
38*17827Sralph };
39*17827Sralph 
40*17827Sralph jmp_buf Failbuf;
41*17827Sralph 
42*17827Sralph tnullf()
43*17827Sralph {
44*17827Sralph 	return SUCCESS;
45*17827Sralph }
46*17827Sralph 
47*17827Sralph twrmsg(type, str, fn)
48*17827Sralph char type;
49*17827Sralph register char *str;
50*17827Sralph {
51*17827Sralph 	char bufr[TBUFSIZE];
52*17827Sralph 	register char *s;
53*17827Sralph 	int len, i;
54*17827Sralph 
55*17827Sralph 	if(setjmp(Failbuf))
56*17827Sralph 		return FAIL;
57*17827Sralph 	signal(SIGALRM, pkfail);
58*17827Sralph 	alarm(MAXMSGTIME);
59*17827Sralph 	bufr[0] = type;
60*17827Sralph 	s = &bufr[1];
61*17827Sralph 	while (*str)
62*17827Sralph 		*s++ = *str++;
63*17827Sralph 	*s = '\0';
64*17827Sralph 	if (*(--s) == '\n')
65*17827Sralph 		*s = '\0';
66*17827Sralph 	len = strlen(bufr) + 1;
67*17827Sralph 	if ((i = len % TPACKSIZE)) {
68*17827Sralph 		len = len + TPACKSIZE - i;
69*17827Sralph 		bufr[len - 1] = '\0';
70*17827Sralph 	}
71*17827Sralph 	twrblk(bufr, len, fn);
72*17827Sralph 	alarm(0);
73*17827Sralph 	return SUCCESS;
74*17827Sralph }
75*17827Sralph 
76*17827Sralph trdmsg(str, fn)
77*17827Sralph register char *str;
78*17827Sralph {
79*17827Sralph 	int len, cnt = 0;
80*17827Sralph 
81*17827Sralph 	if(setjmp(Failbuf))
82*17827Sralph 		return FAIL;
83*17827Sralph 	signal(SIGALRM, pkfail);
84*17827Sralph 	alarm(MAXMSGTIME);
85*17827Sralph 	for (;;) {
86*17827Sralph 		len = read(fn, str, TPACKSIZE);
87*17827Sralph 		if (len == 0)
88*17827Sralph 			continue;
89*17827Sralph 		if (len < 0) {
90*17827Sralph 			alarm(0);
91*17827Sralph 			return FAIL;
92*17827Sralph 		}
93*17827Sralph 		str += len;
94*17827Sralph 		cnt += len;
95*17827Sralph 		if (*(str - 1) == '\0' && (cnt % TPACKSIZE) == 0)
96*17827Sralph 			break;
97*17827Sralph 	}
98*17827Sralph 	alarm(0);
99*17827Sralph 	return SUCCESS;
100*17827Sralph }
101*17827Sralph 
102*17827Sralph twrdata(fp1, fn)
103*17827Sralph FILE *fp1;
104*17827Sralph {
105*17827Sralph 	struct tbuf bufr;
106*17827Sralph 	register int len;
107*17827Sralph 	int ret, mil;
108*17827Sralph #ifdef USG
109*17827Sralph 	time_t t1, t2;
110*17827Sralph #else v7
111*17827Sralph 	struct timeb t1, t2;
112*17827Sralph #endif v7
113*17827Sralph 	long bytes;
114*17827Sralph 	char text[TBUFSIZE];
115*17827Sralph 
116*17827Sralph 	if(setjmp(Failbuf))
117*17827Sralph 		return FAIL;
118*17827Sralph 	signal(SIGALRM, pkfail);
119*17827Sralph 	bytes = 0L;
120*17827Sralph 	ftime(&t1);
121*17827Sralph 	while ((len = read(fileno(fp1), bufr.t_data, TBUFSIZE)) > 0) {
122*17827Sralph 		bytes += len;
123*17827Sralph #if defined(vax) || defined(pdp11)
124*17827Sralph 		bufr.t_nbytes = htonl((long)len);
125*17827Sralph #else !vax and !pdp11
126*17827Sralph 		bufr.t_nbytes = len;
127*17827Sralph #endif !vax and !pdp11
128*17827Sralph 		DEBUG(8,"twrdata sending %d bytes\n",len);
129*17827Sralph 		len += sizeof(long);
130*17827Sralph 		alarm(MAXMSGTIME);
131*17827Sralph 		ret = twrblk(&bufr, len, fn);
132*17827Sralph 		alarm(0);
133*17827Sralph 		if (ret != len)
134*17827Sralph 			return FAIL;
135*17827Sralph 		if (len != TBUFSIZE+sizeof(long))
136*17827Sralph 			break;
137*17827Sralph 	}
138*17827Sralph 	bufr.t_nbytes = 0;
139*17827Sralph 	len = sizeof(long);
140*17827Sralph 	alarm(MAXMSGTIME);
141*17827Sralph 	ret = twrblk(&bufr, len, fn);
142*17827Sralph 	alarm(0);
143*17827Sralph 	if (ret != len)
144*17827Sralph 		return FAIL;
145*17827Sralph 	ftime(&t2);
146*17827Sralph #ifndef USG
147*17827Sralph 	t2.time -= t1.time;
148*17827Sralph 	mil = t2.millitm - t1.millitm;
149*17827Sralph 	if (mil < 0) {
150*17827Sralph 		--t2.time;
151*17827Sralph 		mil += 1000;
152*17827Sralph 	}
153*17827Sralph 	sprintf(text, "sent data %ld bytes %ld.%03d secs",
154*17827Sralph 				bytes, (long)t2.time, mil);
155*17827Sralph 	sysacct(bytes, t2.time - t1.time);
156*17827Sralph #else USG
157*17827Sralph 	sprintf(text, "sent data %ld bytes %ld secs", bytes, t2 - t1);
158*17827Sralph 	sysacct(bytes, t2 - t1);
159*17827Sralph #endif USG
160*17827Sralph 	DEBUG(1, "%s\n", text);
161*17827Sralph 	syslog(text);
162*17827Sralph 	return SUCCESS;
163*17827Sralph }
164*17827Sralph 
165*17827Sralph 
166*17827Sralph trddata(fn, fp2)
167*17827Sralph FILE *fp2;
168*17827Sralph {
169*17827Sralph 	register int len, nread;
170*17827Sralph 	char bufr[TBUFSIZE];
171*17827Sralph #ifdef USG
172*17827Sralph 	time_t t1, t2;
173*17827Sralph #else V7
174*17827Sralph 	struct timeb t1, t2;
175*17827Sralph 	int mil;
176*17827Sralph #endif V7
177*17827Sralph 	long bytes, Nbytes;
178*17827Sralph 
179*17827Sralph 	if(setjmp(Failbuf))
180*17827Sralph 		return FAIL;
181*17827Sralph 	signal(SIGALRM, pkfail);
182*17827Sralph 	ftime(&t1);
183*17827Sralph 	bytes = 0L;
184*17827Sralph 	for (;;) {
185*17827Sralph 		alarm(MAXMSGTIME);
186*17827Sralph 		len = trdblk(&Nbytes,sizeof Nbytes,fn);
187*17827Sralph 		alarm(0);
188*17827Sralph 		if (len != sizeof Nbytes)
189*17827Sralph 			return FAIL;
190*17827Sralph #if defined(vax) || defined(pdp11)
191*17827Sralph 		Nbytes = ntohl(Nbytes);
192*17827Sralph #endif vax or pdp11
193*17827Sralph 		DEBUG(8,"trddata expecting %ld bytes\n",Nbytes);
194*17827Sralph 		nread = Nbytes;
195*17827Sralph 		if (nread == 0)
196*17827Sralph 			break;
197*17827Sralph 		alarm(MAXMSGTIME);
198*17827Sralph 		len = trdblk(bufr, nread, fn);
199*17827Sralph 		alarm(0);
200*17827Sralph 		if (len < 0) {
201*17827Sralph 			return FAIL;
202*17827Sralph 		}
203*17827Sralph 		bytes += len;
204*17827Sralph 		DEBUG(11,"trddata got %ld\n",bytes);
205*17827Sralph 		if (write(fileno(fp2), bufr, len) != len) {
206*17827Sralph 			alarm(0);
207*17827Sralph 			return FAIL;
208*17827Sralph 		}
209*17827Sralph 	}
210*17827Sralph 	ftime(&t2);
211*17827Sralph #ifndef USG
212*17827Sralph 	t2.time -= t1.time;
213*17827Sralph 	mil = t2.millitm - t1.millitm;
214*17827Sralph 	if (mil < 0) {
215*17827Sralph 		--t2.time;
216*17827Sralph 		mil += 1000;
217*17827Sralph 	}
218*17827Sralph 	sprintf(bufr, "received data %ld bytes %ld.%03d secs",
219*17827Sralph 				bytes, (long)t2.time, mil);
220*17827Sralph 	sysacct(bytes, t2.time - t1.time);
221*17827Sralph #else USG
222*17827Sralph 	sprintf(bufr, "received data %ld bytes %ld secs", bytes, t2 - t1);
223*17827Sralph 	sysacct(bytes, t2 - t1);
224*17827Sralph #endif USG
225*17827Sralph 	DEBUG(1, "%s\n", bufr);
226*17827Sralph 	syslog(bufr);
227*17827Sralph 	return SUCCESS;
228*17827Sralph }
229*17827Sralph 
230*17827Sralph 
231*17827Sralph #define	TC	1024
232*17827Sralph static	int tc = TC;
233*17827Sralph 
234*17827Sralph trdblk(blk, len,  fn)
235*17827Sralph register int len;
236*17827Sralph char *blk;
237*17827Sralph {
238*17827Sralph 	register int i, ret;
239*17827Sralph 
240*17827Sralph 	/* call ultouch occasionally */
241*17827Sralph 	if (--tc < 0) {
242*17827Sralph 		tc = TC;
243*17827Sralph 		ultouch();
244*17827Sralph 	}
245*17827Sralph 	for (i = 0; i < len; i += ret) {
246*17827Sralph 		ret = read(fn, blk, len - i);
247*17827Sralph 		if (ret < 0)
248*17827Sralph 			return FAIL;
249*17827Sralph 		blk += ret;
250*17827Sralph 		if (ret == 0)
251*17827Sralph 			return i;
252*17827Sralph 	}
253*17827Sralph 	return i;
254*17827Sralph }
255*17827Sralph 
256*17827Sralph 
257*17827Sralph twrblk(blk, len, fn)
258*17827Sralph register char *blk;
259*17827Sralph {
260*17827Sralph 	register int ret;
261*17827Sralph 	/* call ultouch occasionally */
262*17827Sralph 	if (--tc < 0) {
263*17827Sralph 		tc = TC;
264*17827Sralph 		ultouch();
265*17827Sralph 	}
266*17827Sralph 	ret = write(fn, blk, len);
267*17827Sralph 	return ret;
268*17827Sralph }
269