1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28/*	  All Rights Reserved  	*/
29
30
31#pragma ident	"%Z%%M%	%I%	%E% SMI"
32
33#include "uucp.h"
34
35#include "pk.h"
36
37struct pack *Pk;
38extern int pkread(), pkwrite();
39extern void pkclose();
40
41static int grdblk(char *, int);
42static int gwrblk(char *, int);
43
44extern int packsize, xpacksize;
45
46jmp_buf Getjbuf, Gfailbuf;
47
48static void (*gsig)();
49
50/* ARGSUSED */
51static void
52galarm(sig)
53int sig;
54{
55	signal(SIGALRM, galarm);
56	longjmp(Getjbuf, 1);
57}
58
59void
60pkfail()
61{
62	longjmp(Gfailbuf, 1);
63}
64
65int
66gturnon()
67{
68	struct pack *pkopen();
69	if (setjmp(Gfailbuf))
70		return(FAIL);
71	gsig=signal(SIGALRM, galarm);
72	if (Debug > 4)
73		pkdebug = 1;
74	Pk = pkopen(Ifn, Ofn);
75	if ((int) Pk == NULL)
76		return(FAIL);
77	return(0);
78}
79
80int
81gturnoff()
82{
83	if(setjmp(Gfailbuf))
84		return(FAIL);
85	pkclose();
86	(void) signal(SIGALRM, gsig);
87	return(0);
88}
89
90/*ARGSUSED*/
91int
92gwrmsg(type, str, fn)
93char type, *str;
94{
95	char bufr[BUFSIZ], *s;
96	int len, i;
97
98	if(setjmp(Gfailbuf))
99		return(FAIL);
100	bufr[0] = type;
101	s = &bufr[1];
102	while (*str)
103		*s++ = *str++;
104	*s = '\0';
105	if (*(--s) == '\n')
106		*s = '\0';
107	len = strlen(bufr) + 1;
108	if ((i = len % xpacksize) != 0) {
109		len = len + xpacksize - i;
110		bufr[len - 1] = '\0';
111	}
112	gwrblk(bufr, len);
113	return(0);
114}
115
116
117/*ARGSUSED*/
118int
119grdmsg(str, fn)
120char *str;
121{
122	unsigned len;
123
124	if(setjmp(Gfailbuf))
125		return(FAIL);
126	for (;;) {
127		len = pkread(str, packsize);
128		if (len == 0)
129			continue;
130		str += len;
131		if (*(str - 1) == '\0')
132			break;
133	}
134	return(0);
135}
136
137
138/*ARGSUSED*/
139int
140gwrdata(fp1, fn)
141FILE *fp1;
142{
143	char bufr[BUFSIZ];
144	int fd1;
145	int len;
146	int ret;
147	unsigned long bytes;
148
149	if(setjmp(Gfailbuf))
150		return(FAIL);
151	bytes = 0L;
152	fd1 = fileno( fp1 );
153	while ((len = read( fd1, bufr, BUFSIZ )) > 0) {
154		bytes += len;
155		putfilesize(bytes);
156		ret = gwrblk(bufr, len);
157		if (ret != len) {
158			return(FAIL);
159		}
160		if (len != BUFSIZ)
161			break;
162	}
163	ret = gwrblk(bufr, 0);
164	return(0);
165}
166
167/*ARGSUSED*/
168int
169grddata(fn, fp2)
170FILE *fp2;
171{
172	int ret = SUCCESS;
173	int fd2;
174	int len;
175	char bufr[BUFSIZ];
176	unsigned long bytes;
177
178	if(setjmp(Gfailbuf))
179		return(FAIL);
180	bytes = 0L;
181	fd2 = fileno( fp2 );
182	for (;;) {
183		len = grdblk(bufr, BUFSIZ);
184		if (len < 0) {
185			return(FAIL);
186		}
187		bytes += len;
188		putfilesize(bytes);
189		if ( ret == SUCCESS && write( fd2, bufr, len ) != len) {
190			ret = errno;
191			DEBUG(7, "grddata: write to file failed, errno %d\n", errno);
192		}
193		if (len < BUFSIZ)
194			break;
195	}
196	return(ret);
197}
198
199
200static
201int
202grdblk(blk, len)
203char *blk;
204{
205	int i, ret;
206
207	for (i = 0; i < len; i += ret) {
208		ret = pkread(blk, len - i);
209		if (ret < 0)
210			return(FAIL);
211		blk += ret;
212		if (ret == 0)
213			return(i);
214	}
215	return(i);
216}
217
218
219static int
220gwrblk(blk, len)
221char *blk;
222{
223	return(pkwrite(blk, len));
224}
225