xref: /original-bsd/usr.bin/uucp/uucico/gio.c (revision ad93c43e)
1 #ifndef lint
2 static char sccsid[] = "@(#)gio.c	5.8 (Berkeley) 02/24/88";
3 #endif
4 
5 #include "uucp.h"
6 #include "pk.h"
7 #include <setjmp.h>
8 
9 jmp_buf Failbuf;
10 
11 int Retries = 0;
12 struct pack *Pk;
13 
14 extern long Bytes_Sent, Bytes_Received;
15 
16 pkfail()
17 {
18 	longjmp(Failbuf, 1);
19 }
20 
21 gturnon()
22 {
23 	struct pack *pkopen();
24 
25 	if (setjmp(Failbuf))
26 		return FAIL;
27 	Pk = pkopen(Ifn, Ofn);
28 	if (Pk == NULL)
29 		return FAIL;
30 	return SUCCESS;
31 }
32 
33 gturnoff()
34 {
35 	if(setjmp(Failbuf))
36 		return(FAIL);
37 	pkclose(Pk);
38 	return SUCCESS;
39 }
40 
41 
42 gwrmsg(type, str, fn)
43 char type;
44 register char *str;
45 {
46 	char bufr[BUFSIZ];
47 	register char *s;
48 	int len, i;
49 
50 	if(setjmp(Failbuf))
51 		return(FAIL);
52 	bufr[0] = type;
53 	s = &bufr[1];
54 	while (*str)
55 		*s++ = *str++;
56 	*s = '\0';
57 	if (*(--s) == '\n')
58 		*s = '\0';
59 	len = strlen(bufr) + 1;
60 	if ((i = len % PACKSIZE)) {
61 		len = len + PACKSIZE - i;
62 		bufr[len - 1] = '\0';
63 	}
64 	gwrblk(bufr, len, fn);
65 	return SUCCESS;
66 }
67 
68 /*ARGSUSED*/
69 grdmsg(str, fn)
70 register char *str;
71 {
72 	unsigned len;
73 
74 	if(setjmp(Failbuf))
75 		return FAIL;
76 	for (;;) {
77 		len = pkread(Pk, str, PACKSIZE);
78 		if (len == 0)
79 			continue;
80 		str += len;
81 		if (*(str - 1) == '\0')
82 			break;
83 	}
84 	return SUCCESS;
85 }
86 
87 
88 gwrdata(fp1, fn)
89 FILE *fp1;
90 {
91 	char bufr[BUFSIZ];
92 	register int len;
93 	int ret, mil;
94 	struct timeb t1, t2;
95 	long bytes;
96 	char text[BUFSIZ];
97 	float ft;
98 
99 	if(setjmp(Failbuf))
100 		return FAIL;
101 	bytes = 0L;
102 	Retries = 0;
103 #ifdef USG
104 	time(&t1.time);
105 	t1.millitm = 0;
106 #else !USG
107 	ftime(&t1);
108 #endif !USG
109 	while ((len = read(fileno(fp1), bufr, BUFSIZ)) > 0) {
110 		bytes += len;
111 		ret = gwrblk(bufr, len, fn);
112 		if (ret != len) {
113 			return FAIL;
114 		}
115 		if (len != BUFSIZ)
116 			break;
117 	}
118 	ret = gwrblk(bufr, 0, fn);
119 #ifdef USG
120 	time(&t2.time);
121 	t2.millitm = 0;
122 #else !USG
123 	ftime(&t2);
124 #endif !USG
125 	Now = t2;
126 	t2.time -= t1.time;
127 	mil = t2.millitm - t1.millitm;
128 	if (mil < 0) {
129 		--t2.time;
130 		mil += 1000;
131 	}
132 	ft = (float)t2.time + (float)mil/1000.;
133 	sprintf(text, "sent data %ld bytes %.2f secs %ld bps",
134 		bytes, ft, (long)((float)bytes*8./ft));
135 	sysacct(bytes, t2.time);
136 	Bytes_Sent += bytes;
137 	if (Retries > 0)
138 		sprintf((char *)text+strlen(text)," %d retries", Retries);
139 	DEBUG(1, "%s\n", text);
140 	syslog(text);
141 	return SUCCESS;
142 }
143 
144 grddata(fn, fp2)
145 FILE *fp2;
146 {
147 	register int len;
148 	char bufr[BUFSIZ];
149 	struct timeb t1, t2;
150 	int mil;
151 	long bytes;
152 	char text[BUFSIZ];
153 	float ft;
154 
155 	if(setjmp(Failbuf))
156 		return FAIL;
157 	bytes = 0L;
158 	Retries = 0;
159 #ifdef USG
160 	time(&t1.time);
161 	t1.millitm = 0;
162 #else !USG
163 	ftime(&t1);
164 #endif !USG
165 	for (;;) {
166 		len = grdblk(bufr, BUFSIZ, fn);
167 		if (len < 0) {
168 			return FAIL;
169 		}
170 		bytes += len;
171 		if (write(fileno(fp2), bufr, len) != len)
172 			return FAIL;
173 		if (len < BUFSIZ)
174 			break;
175 	}
176 #ifdef USG
177 	time(&t2.time);
178 	t2.millitm = 0;
179 #else !USG
180 	ftime(&t2);
181 #endif !USG
182 	Now = t2;
183 	t2.time -= t1.time;
184 	mil = t2.millitm - t1.millitm;
185 	if (mil < 0) {
186 		--t2.time;
187 		mil += 1000;
188 	}
189 	ft = (float)t2.time + (float)mil/1000.;
190 	sprintf(text, "received data %ld bytes %.2f secs %ld bps",
191 		bytes, ft, (long)((float)bytes*8./ft));
192 	sysacct(bytes, t2.time);
193 	Bytes_Received += bytes;
194 	if (Retries > 0)
195 		sprintf((char *)text+strlen(text)," %d retries", Retries);
196 	DEBUG(1, "%s\n", text);
197 	syslog(text);
198 	return SUCCESS;
199 }
200 
201 #if !defined(BSD4_2) && !defined(USG)
202 /* call ultouch every TC calls to either grdblk or gwrblk */
203 #define	TC	20
204 static	int tc = TC;
205 #endif !BSD4_2 && !USG
206 
207 /*ARGSUSED*/
208 grdblk(blk, len,  fn)
209 register int len;
210 char *blk;
211 {
212 	register int i, ret;
213 
214 #if !defined(BSD4_2) && !defined(USG)
215 	/* call ultouch occasionally */
216 	if (--tc < 0) {
217 		tc = TC;
218 		ultouch();
219 	}
220 #endif !BSD4_2 && !USG
221 	for (i = 0; i < len; i += ret) {
222 		ret = pkread(Pk, blk, len - i);
223 		if (ret < 0)
224 			return FAIL;
225 		blk += ret;
226 		if (ret == 0)
227 			return i;
228 	}
229 	return i;
230 }
231 
232 /*ARGSUSED*/
233 gwrblk(blk, len, fn)
234 register char *blk;
235 {
236 #if !defined(BSD4_2) && !defined(USG)
237 	/* call ultouch occasionally */
238 	if (--tc < 0) {
239 		tc = TC;
240 		ultouch();
241 	}
242 #endif !BSD4_2 && !USG
243 	return  pkwrite(Pk, blk, len);
244 }
245