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