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