xref: /csrg-svn/usr.bin/uucp/uucico/gio.c (revision 62391)
148664Sbostic /*-
2*62391Sbostic  * Copyright (c) 1985, 1993
3*62391Sbostic  *	The Regents of the University of California.  All rights reserved.
448664Sbostic  *
548664Sbostic  * %sccs.include.proprietary.c%
648664Sbostic  */
748664Sbostic 
813652Ssam #ifndef lint
9*62391Sbostic static char sccsid[] = "@(#)gio.c	8.1 (Berkeley) 06/06/93";
1048664Sbostic #endif /* not lint */
1113652Ssam 
1218620Sralph #include "uucp.h"
1313652Ssam #include "pk.h"
1413652Ssam #include <setjmp.h>
1513652Ssam 
1613652Ssam jmp_buf Failbuf;
1713652Ssam 
1823606Sbloom int Retries = 0;
1913652Ssam struct pack *Pk;
2013652Ssam 
2133564Srick extern long Bytes_Sent, Bytes_Received;
2233564Srick 
pkfail()2313652Ssam pkfail()
2413652Ssam {
2513652Ssam 	longjmp(Failbuf, 1);
2613652Ssam }
2713652Ssam 
gturnon()2813652Ssam gturnon()
2913652Ssam {
3013652Ssam 	struct pack *pkopen();
3117835Sralph 
3213652Ssam 	if (setjmp(Failbuf))
3317835Sralph 		return FAIL;
3417835Sralph 	Pk = pkopen(Ifn, Ofn);
3517835Sralph 	if (Pk == NULL)
3617835Sralph 		return FAIL;
3717835Sralph 	return SUCCESS;
3813652Ssam }
3913652Ssam 
gturnoff()4013652Ssam gturnoff()
4113652Ssam {
4213652Ssam 	if(setjmp(Failbuf))
4313652Ssam 		return(FAIL);
4417835Sralph 	pkclose(Pk);
4517835Sralph 	return SUCCESS;
4613652Ssam }
4713652Ssam 
4813652Ssam 
gwrmsg(type,str,fn)4913652Ssam gwrmsg(type, str, fn)
5013652Ssam char type;
5113652Ssam register char *str;
5213652Ssam {
5313652Ssam 	char bufr[BUFSIZ];
5413652Ssam 	register char *s;
5513652Ssam 	int len, i;
5613652Ssam 
5713652Ssam 	if(setjmp(Failbuf))
5813652Ssam 		return(FAIL);
5913652Ssam 	bufr[0] = type;
6013652Ssam 	s = &bufr[1];
6113652Ssam 	while (*str)
6213652Ssam 		*s++ = *str++;
6313652Ssam 	*s = '\0';
6413652Ssam 	if (*(--s) == '\n')
6513652Ssam 		*s = '\0';
6613652Ssam 	len = strlen(bufr) + 1;
6713652Ssam 	if ((i = len % PACKSIZE)) {
6813652Ssam 		len = len + PACKSIZE - i;
6913652Ssam 		bufr[len - 1] = '\0';
7013652Ssam 	}
7113652Ssam 	gwrblk(bufr, len, fn);
7217835Sralph 	return SUCCESS;
7313652Ssam }
7413652Ssam 
7517835Sralph /*ARGSUSED*/
grdmsg(str,fn)7613652Ssam grdmsg(str, fn)
7713652Ssam register char *str;
7813652Ssam {
7913652Ssam 	unsigned len;
8013652Ssam 
8113652Ssam 	if(setjmp(Failbuf))
8217835Sralph 		return FAIL;
8313652Ssam 	for (;;) {
8417835Sralph 		len = pkread(Pk, str, PACKSIZE);
8513652Ssam 		if (len == 0)
8613652Ssam 			continue;
8713652Ssam 		str += len;
8813652Ssam 		if (*(str - 1) == '\0')
8913652Ssam 			break;
9013652Ssam 	}
9117835Sralph 	return SUCCESS;
9213652Ssam }
9313652Ssam 
9413652Ssam 
gwrdata(fp1,fn)9513652Ssam gwrdata(fp1, fn)
9613652Ssam FILE *fp1;
9713652Ssam {
9813652Ssam 	char bufr[BUFSIZ];
9913652Ssam 	register int len;
10017835Sralph 	int ret, mil;
10117835Sralph 	struct timeb t1, t2;
10213652Ssam 	long bytes;
10313652Ssam 	char text[BUFSIZ];
10433564Srick 	float ft;
10513652Ssam 
10613652Ssam 	if(setjmp(Failbuf))
10717835Sralph 		return FAIL;
10813652Ssam 	bytes = 0L;
10923606Sbloom 	Retries = 0;
11018620Sralph #ifdef USG
11118620Sralph 	time(&t1.time);
11218620Sralph 	t1.millitm = 0;
11318620Sralph #else !USG
11417835Sralph 	ftime(&t1);
11518620Sralph #endif !USG
11617835Sralph 	while ((len = read(fileno(fp1), bufr, BUFSIZ)) > 0) {
11713652Ssam 		bytes += len;
11813652Ssam 		ret = gwrblk(bufr, len, fn);
11913652Ssam 		if (ret != len) {
12017835Sralph 			return FAIL;
12113652Ssam 		}
12213652Ssam 		if (len != BUFSIZ)
12313652Ssam 			break;
12413652Ssam 	}
12513652Ssam 	ret = gwrblk(bufr, 0, fn);
12618620Sralph #ifdef USG
12718620Sralph 	time(&t2.time);
12818620Sralph 	t2.millitm = 0;
12918620Sralph #else !USG
13017835Sralph 	ftime(&t2);
13118620Sralph #endif !USG
13218620Sralph 	Now = t2;
13317835Sralph 	t2.time -= t1.time;
13417835Sralph 	mil = t2.millitm - t1.millitm;
13517835Sralph 	if (mil < 0) {
13617835Sralph 		--t2.time;
13717835Sralph 		mil += 1000;
13817835Sralph 	}
13933564Srick 	ft = (float)t2.time + (float)mil/1000.;
14033564Srick 	sprintf(text, "sent data %ld bytes %.2f secs %ld bps",
14133564Srick 		bytes, ft, (long)((float)bytes*8./ft));
14225150Sbloom 	sysacct(bytes, t2.time);
14333564Srick 	Bytes_Sent += bytes;
14423606Sbloom 	if (Retries > 0)
14523606Sbloom 		sprintf((char *)text+strlen(text)," %d retries", Retries);
14613652Ssam 	DEBUG(1, "%s\n", text);
14733950Srick 	log_xferstats(text);
14817835Sralph 	return SUCCESS;
14913652Ssam }
15013652Ssam 
grddata(fn,fp2)15113652Ssam grddata(fn, fp2)
15213652Ssam FILE *fp2;
15313652Ssam {
15413652Ssam 	register int len;
15513652Ssam 	char bufr[BUFSIZ];
15617835Sralph 	struct timeb t1, t2;
15717835Sralph 	int mil;
15813652Ssam 	long bytes;
15913652Ssam 	char text[BUFSIZ];
16033564Srick 	float ft;
16113652Ssam 
16213652Ssam 	if(setjmp(Failbuf))
16317835Sralph 		return FAIL;
16413652Ssam 	bytes = 0L;
16523606Sbloom 	Retries = 0;
16618620Sralph #ifdef USG
16718620Sralph 	time(&t1.time);
16818620Sralph 	t1.millitm = 0;
16918620Sralph #else !USG
17017835Sralph 	ftime(&t1);
17118620Sralph #endif !USG
17213652Ssam 	for (;;) {
17313652Ssam 		len = grdblk(bufr, BUFSIZ, fn);
17413652Ssam 		if (len < 0) {
17517835Sralph 			return FAIL;
17613652Ssam 		}
17713652Ssam 		bytes += len;
17817835Sralph 		if (write(fileno(fp2), bufr, len) != len)
17917835Sralph 			return FAIL;
18013652Ssam 		if (len < BUFSIZ)
18113652Ssam 			break;
18213652Ssam 	}
18318620Sralph #ifdef USG
18418620Sralph 	time(&t2.time);
18518620Sralph 	t2.millitm = 0;
18618620Sralph #else !USG
18717835Sralph 	ftime(&t2);
18818620Sralph #endif !USG
18918620Sralph 	Now = t2;
19017835Sralph 	t2.time -= t1.time;
19117835Sralph 	mil = t2.millitm - t1.millitm;
19217835Sralph 	if (mil < 0) {
19317835Sralph 		--t2.time;
19417835Sralph 		mil += 1000;
19517835Sralph 	}
19633564Srick 	ft = (float)t2.time + (float)mil/1000.;
19733564Srick 	sprintf(text, "received data %ld bytes %.2f secs %ld bps",
19833564Srick 		bytes, ft, (long)((float)bytes*8./ft));
19925150Sbloom 	sysacct(bytes, t2.time);
20033564Srick 	Bytes_Received += bytes;
20123606Sbloom 	if (Retries > 0)
20223606Sbloom 		sprintf((char *)text+strlen(text)," %d retries", Retries);
20313652Ssam 	DEBUG(1, "%s\n", text);
20433950Srick 	log_xferstats(text);
20517835Sralph 	return SUCCESS;
20613652Ssam }
20713652Ssam 
20825133Sbloom #if !defined(BSD4_2) && !defined(USG)
20925133Sbloom /* call ultouch every TC calls to either grdblk or gwrblk */
21013652Ssam #define	TC	20
21113652Ssam static	int tc = TC;
21225133Sbloom #endif !BSD4_2 && !USG
21313652Ssam 
21417835Sralph /*ARGSUSED*/
grdblk(blk,len,fn)21513652Ssam grdblk(blk, len,  fn)
21613652Ssam register int len;
21713652Ssam char *blk;
21813652Ssam {
21913652Ssam 	register int i, ret;
22013652Ssam 
22125133Sbloom #if !defined(BSD4_2) && !defined(USG)
22217835Sralph 	/* call ultouch occasionally */
22313652Ssam 	if (--tc < 0) {
22413652Ssam 		tc = TC;
22513652Ssam 		ultouch();
22613652Ssam 	}
22725133Sbloom #endif !BSD4_2 && !USG
22813652Ssam 	for (i = 0; i < len; i += ret) {
22917835Sralph 		ret = pkread(Pk, blk, len - i);
23013652Ssam 		if (ret < 0)
23117835Sralph 			return FAIL;
23213652Ssam 		blk += ret;
23313652Ssam 		if (ret == 0)
23417835Sralph 			return i;
23513652Ssam 	}
23617835Sralph 	return i;
23713652Ssam }
23813652Ssam 
23917835Sralph /*ARGSUSED*/
gwrblk(blk,len,fn)24013652Ssam gwrblk(blk, len, fn)
24113652Ssam register char *blk;
24213652Ssam {
24325133Sbloom #if !defined(BSD4_2) && !defined(USG)
24425133Sbloom 	/* call ultouch occasionally */
24513652Ssam 	if (--tc < 0) {
24613652Ssam 		tc = TC;
24713652Ssam 		ultouch();
24813652Ssam 	}
24925133Sbloom #endif !BSD4_2 && !USG
25025133Sbloom 	return  pkwrite(Pk, blk, len);
25113652Ssam }
252