xref: /titanic_41/usr/src/cmd/bnu/gio.c (revision 462be471126495414a94f9fa35e16c02dc462c04)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*462be471Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "uucp.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "pk.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate struct pack *Pk;
38*462be471Sceastha extern int pkread(), pkwrite();
397c478bd9Sstevel@tonic-gate extern void pkclose();
407c478bd9Sstevel@tonic-gate 
41*462be471Sceastha static int grdblk(char *, int);
42*462be471Sceastha static int gwrblk(char *, int);
43*462be471Sceastha 
447c478bd9Sstevel@tonic-gate extern int packsize, xpacksize;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate jmp_buf Getjbuf, Gfailbuf;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static void (*gsig)();
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* ARGSUSED */
517c478bd9Sstevel@tonic-gate static void
galarm(sig)527c478bd9Sstevel@tonic-gate galarm(sig)
537c478bd9Sstevel@tonic-gate int sig;
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	signal(SIGALRM, galarm);
567c478bd9Sstevel@tonic-gate 	longjmp(Getjbuf, 1);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate void
pkfail()607c478bd9Sstevel@tonic-gate pkfail()
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	longjmp(Gfailbuf, 1);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate int
gturnon()667c478bd9Sstevel@tonic-gate gturnon()
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	struct pack *pkopen();
697c478bd9Sstevel@tonic-gate 	if (setjmp(Gfailbuf))
707c478bd9Sstevel@tonic-gate 		return(FAIL);
717c478bd9Sstevel@tonic-gate 	gsig=signal(SIGALRM, galarm);
727c478bd9Sstevel@tonic-gate 	if (Debug > 4)
737c478bd9Sstevel@tonic-gate 		pkdebug = 1;
747c478bd9Sstevel@tonic-gate 	Pk = pkopen(Ifn, Ofn);
757c478bd9Sstevel@tonic-gate 	if ((int) Pk == NULL)
767c478bd9Sstevel@tonic-gate 		return(FAIL);
777c478bd9Sstevel@tonic-gate 	return(0);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate int
gturnoff()817c478bd9Sstevel@tonic-gate gturnoff()
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	if(setjmp(Gfailbuf))
847c478bd9Sstevel@tonic-gate 		return(FAIL);
857c478bd9Sstevel@tonic-gate 	pkclose();
867c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, gsig);
877c478bd9Sstevel@tonic-gate 	return(0);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
917c478bd9Sstevel@tonic-gate int
gwrmsg(type,str,fn)927c478bd9Sstevel@tonic-gate gwrmsg(type, str, fn)
937c478bd9Sstevel@tonic-gate char type, *str;
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	char bufr[BUFSIZ], *s;
967c478bd9Sstevel@tonic-gate 	int len, i;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if(setjmp(Gfailbuf))
997c478bd9Sstevel@tonic-gate 		return(FAIL);
1007c478bd9Sstevel@tonic-gate 	bufr[0] = type;
1017c478bd9Sstevel@tonic-gate 	s = &bufr[1];
1027c478bd9Sstevel@tonic-gate 	while (*str)
1037c478bd9Sstevel@tonic-gate 		*s++ = *str++;
1047c478bd9Sstevel@tonic-gate 	*s = '\0';
1057c478bd9Sstevel@tonic-gate 	if (*(--s) == '\n')
1067c478bd9Sstevel@tonic-gate 		*s = '\0';
1077c478bd9Sstevel@tonic-gate 	len = strlen(bufr) + 1;
1087c478bd9Sstevel@tonic-gate 	if ((i = len % xpacksize) != 0) {
1097c478bd9Sstevel@tonic-gate 		len = len + xpacksize - i;
1107c478bd9Sstevel@tonic-gate 		bufr[len - 1] = '\0';
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	gwrblk(bufr, len);
1137c478bd9Sstevel@tonic-gate 	return(0);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1187c478bd9Sstevel@tonic-gate int
grdmsg(str,fn)1197c478bd9Sstevel@tonic-gate grdmsg(str, fn)
1207c478bd9Sstevel@tonic-gate char *str;
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	unsigned len;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if(setjmp(Gfailbuf))
1257c478bd9Sstevel@tonic-gate 		return(FAIL);
1267c478bd9Sstevel@tonic-gate 	for (;;) {
1277c478bd9Sstevel@tonic-gate 		len = pkread(str, packsize);
1287c478bd9Sstevel@tonic-gate 		if (len == 0)
1297c478bd9Sstevel@tonic-gate 			continue;
1307c478bd9Sstevel@tonic-gate 		str += len;
1317c478bd9Sstevel@tonic-gate 		if (*(str - 1) == '\0')
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	return(0);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1397c478bd9Sstevel@tonic-gate int
gwrdata(fp1,fn)1407c478bd9Sstevel@tonic-gate gwrdata(fp1, fn)
1417c478bd9Sstevel@tonic-gate FILE *fp1;
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	char bufr[BUFSIZ];
1447c478bd9Sstevel@tonic-gate 	int fd1;
1457c478bd9Sstevel@tonic-gate 	int len;
1467c478bd9Sstevel@tonic-gate 	int ret;
1477c478bd9Sstevel@tonic-gate 	unsigned long bytes;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if(setjmp(Gfailbuf))
1507c478bd9Sstevel@tonic-gate 		return(FAIL);
1517c478bd9Sstevel@tonic-gate 	bytes = 0L;
1527c478bd9Sstevel@tonic-gate 	fd1 = fileno( fp1 );
1537c478bd9Sstevel@tonic-gate 	while ((len = read( fd1, bufr, BUFSIZ )) > 0) {
1547c478bd9Sstevel@tonic-gate 		bytes += len;
1557c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
1567c478bd9Sstevel@tonic-gate 		ret = gwrblk(bufr, len);
1577c478bd9Sstevel@tonic-gate 		if (ret != len) {
1587c478bd9Sstevel@tonic-gate 			return(FAIL);
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		if (len != BUFSIZ)
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	ret = gwrblk(bufr, 0);
1647c478bd9Sstevel@tonic-gate 	return(0);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1687c478bd9Sstevel@tonic-gate int
grddata(fn,fp2)1697c478bd9Sstevel@tonic-gate grddata(fn, fp2)
1707c478bd9Sstevel@tonic-gate FILE *fp2;
1717c478bd9Sstevel@tonic-gate {
172*462be471Sceastha 	int ret = SUCCESS;
1737c478bd9Sstevel@tonic-gate 	int fd2;
1747c478bd9Sstevel@tonic-gate 	int len;
1757c478bd9Sstevel@tonic-gate 	char bufr[BUFSIZ];
1767c478bd9Sstevel@tonic-gate 	unsigned long bytes;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if(setjmp(Gfailbuf))
1797c478bd9Sstevel@tonic-gate 		return(FAIL);
1807c478bd9Sstevel@tonic-gate 	bytes = 0L;
1817c478bd9Sstevel@tonic-gate 	fd2 = fileno( fp2 );
1827c478bd9Sstevel@tonic-gate 	for (;;) {
1837c478bd9Sstevel@tonic-gate 		len = grdblk(bufr, BUFSIZ);
1847c478bd9Sstevel@tonic-gate 		if (len < 0) {
1857c478bd9Sstevel@tonic-gate 			return(FAIL);
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 		bytes += len;
1887c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
1897c478bd9Sstevel@tonic-gate 		if ( ret == SUCCESS && write( fd2, bufr, len ) != len) {
1907c478bd9Sstevel@tonic-gate 			ret = errno;
1917c478bd9Sstevel@tonic-gate 			DEBUG(7, "grddata: write to file failed, errno %d\n", errno);
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 		if (len < BUFSIZ)
1947c478bd9Sstevel@tonic-gate 			break;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	return(ret);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static
2017c478bd9Sstevel@tonic-gate int
grdblk(blk,len)2027c478bd9Sstevel@tonic-gate grdblk(blk, len)
2037c478bd9Sstevel@tonic-gate char *blk;
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	int i, ret;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i += ret) {
2087c478bd9Sstevel@tonic-gate 		ret = pkread(blk, len - i);
2097c478bd9Sstevel@tonic-gate 		if (ret < 0)
2107c478bd9Sstevel@tonic-gate 			return(FAIL);
2117c478bd9Sstevel@tonic-gate 		blk += ret;
2127c478bd9Sstevel@tonic-gate 		if (ret == 0)
2137c478bd9Sstevel@tonic-gate 			return(i);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 	return(i);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static int
gwrblk(blk,len)2207c478bd9Sstevel@tonic-gate gwrblk(blk, len)
2217c478bd9Sstevel@tonic-gate char *blk;
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	return(pkwrite(blk, len));
2247c478bd9Sstevel@tonic-gate }
225