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