add sccsid
[unix-history] / usr / src / usr.bin / uucp / uucico / tio.c
CommitLineData
0b77c4fc 1#ifndef lint
9ba32cf8 2static char sccsid[] = "@(#)tio.c 4.9 (Berkeley) %G%";
0b77c4fc
RC
3#endif
4
a075b7ef 5#include <signal.h>
0b77c4fc 6#include "uucp.h"
1a85e9d2 7#include <setjmp.h>
0b77c4fc 8#include <sys/stat.h>
9ba32cf8 9#include <machine/machparam.h>
0b77c4fc
RC
10
11extern int pkfail();
0b77c4fc
RC
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
29struct tbuf {
30 long t_nbytes;
31 char t_data[TBUFSIZE];
32};
33
a075b7ef 34extern jmp_buf Failbuf;
0b77c4fc 35
51337009
RA
36extern long Bytes_Sent, Bytes_Received;
37
0b77c4fc
RC
38twrmsg(type, str, fn)
39char type;
40register 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);
51337009 49 alarm(MAXMSGTIME*5);
0b77c4fc
RC
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
67trdmsg(str, fn)
68register char *str;
69{
70 int len, cnt = 0;
71
72 if(setjmp(Failbuf))
73 return FAIL;
74 signal(SIGALRM, pkfail);
51337009 75 alarm(MAXMSGTIME*5);
0b77c4fc
RC
76 for (;;) {
77 len = read(fn, str, TPACKSIZE);
b30327a3 78 if (len <= 0) {
0b77c4fc
RC
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
91twrdata(fp1, fn)
92FILE *fp1;
93{
94 struct tbuf bufr;
95 register int len;
96 int ret, mil;
0b77c4fc 97 struct timeb t1, t2;
0b77c4fc
RC
98 long bytes;
99 char text[TBUFSIZE];
51337009 100 float ft;
0b77c4fc
RC
101
102 if(setjmp(Failbuf))
103 return FAIL;
104 signal(SIGALRM, pkfail);
105 bytes = 0L;
1a85e9d2
RC
106#ifdef USG
107 time(&t1.time);
108 t1.millitm = 0;
109#else !USG
0b77c4fc 110 ftime(&t1);
1a85e9d2 111#endif !USG
0b77c4fc
RC
112 while ((len = read(fileno(fp1), bufr.t_data, TBUFSIZE)) > 0) {
113 bytes += len;
9313cec6 114#if defined(vax) || defined(pdp11) || defined(ns32000)
0b77c4fc 115 bufr.t_nbytes = htonl((long)len);
9313cec6 116#else !vax and !pdp11 and !ns32000
0b77c4fc 117 bufr.t_nbytes = len;
9313cec6 118#endif !vax and !pdp11 and !ns32000
0b77c4fc
RC
119 DEBUG(8,"twrdata sending %d bytes\n",len);
120 len += sizeof(long);
51337009 121 alarm(MAXMSGTIME*5);
a075b7ef 122 ret = twrblk((char *)&bufr, len, fn);
0b77c4fc
RC
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);
51337009 131 alarm(MAXMSGTIME*5);
a075b7ef 132 ret = twrblk((char *)&bufr, len, fn);
0b77c4fc
RC
133 alarm(0);
134 if (ret != len)
135 return FAIL;
1a85e9d2
RC
136#ifdef USG
137 time(&t2.time);
138 t2.millitm = 0;
139#else !USG
0b77c4fc 140 ftime(&t2);
1a85e9d2
RC
141#endif !USG
142 Now = t2;
0b77c4fc
RC
143 t2.time -= t1.time;
144 mil = t2.millitm - t1.millitm;
145 if (mil < 0) {
146 --t2.time;
147 mil += 1000;
148 }
51337009
RA
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));
28ed17d4 152 sysacct(bytes, t2.time);
51337009 153 Bytes_Sent += bytes;
0b77c4fc 154 DEBUG(1, "%s\n", text);
b30327a3 155 log_xferstats(text);
0b77c4fc
RC
156 return SUCCESS;
157}
158
0b77c4fc
RC
159trddata(fn, fp2)
160FILE *fp2;
161{
162 register int len, nread;
163 char bufr[TBUFSIZE];
0b77c4fc
RC
164 struct timeb t1, t2;
165 int mil;
0b77c4fc 166 long bytes, Nbytes;
51337009 167 float ft;
0b77c4fc
RC
168
169 if(setjmp(Failbuf))
170 return FAIL;
171 signal(SIGALRM, pkfail);
1a85e9d2
RC
172#ifdef USG
173 time(&t1.time);
174 t1.millitm = 0;
175#else !USG
0b77c4fc 176 ftime(&t1);
1a85e9d2 177#endif !USG
0b77c4fc
RC
178 bytes = 0L;
179 for (;;) {
51337009 180 alarm(MAXMSGTIME*5);
a075b7ef 181 len = trdblk((char *)&Nbytes,sizeof Nbytes,fn);
0b77c4fc
RC
182 alarm(0);
183 if (len != sizeof Nbytes)
184 return FAIL;
9313cec6 185#if defined(vax) || defined(pdp11) || defined(ns32000)
0b77c4fc 186 Nbytes = ntohl(Nbytes);
9313cec6 187#endif vax or pdp11 or ns32000
0b77c4fc
RC
188 DEBUG(8,"trddata expecting %ld bytes\n",Nbytes);
189 nread = Nbytes;
190 if (nread == 0)
191 break;
51337009 192 alarm(MAXMSGTIME*5);
0b77c4fc
RC
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 }
1a85e9d2
RC
205#ifdef USG
206 time(&t2.time);
207 t2.millitm = 0;
208#else !USG
0b77c4fc 209 ftime(&t2);
1a85e9d2
RC
210#endif !USG
211 Now = t2;
0b77c4fc
RC
212 t2.time -= t1.time;
213 mil = t2.millitm - t1.millitm;
214 if (mil < 0) {
215 --t2.time;
216 mil += 1000;
217 }
51337009
RA
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;
0b77c4fc 223 DEBUG(1, "%s\n", bufr);
b30327a3 224 log_xferstats(bufr);
0b77c4fc
RC
225 return SUCCESS;
226}
227
0f432fcf 228#if !defined(BSD4_2) && !defined(USG)
0b77c4fc
RC
229#define TC 1024
230static int tc = TC;
0f432fcf 231#endif !BSD4_2 && !USG
0b77c4fc
RC
232
233trdblk(blk, len, fn)
234register int len;
235char *blk;
236{
237 register int i, ret;
238
0f432fcf 239#if !defined(BSD4_2) && !defined(USG)
0b77c4fc
RC
240 /* call ultouch occasionally */
241 if (--tc < 0) {
242 tc = TC;
243 ultouch();
244 }
0f432fcf 245#endif !BSD4_2 && !USG
0b77c4fc
RC
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
258twrblk(blk, len, fn)
259register char *blk;
260{
0f432fcf 261#if !defined(BSD4_2) && !defined(USG)
0b77c4fc
RC
262 /* call ultouch occasionally */
263 if (--tc < 0) {
264 tc = TC;
265 ultouch();
266 }
0f432fcf
JB
267#endif !BSD4_2 && !USG
268 return write(fn, blk, len);
0b77c4fc 269}