xref: /titanic_44/usr/src/cmd/bnu/gio.c (revision b7f45089ccbe01bab3d7c7377b49d80d2ae18a69)
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 1988 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 
37 struct pack *Pk;
38 extern int gwrblk(), grdblk(), pkread(), pkwrite();
39 extern void pkclose();
40 
41 extern int packsize, xpacksize;
42 
43 jmp_buf Getjbuf, Gfailbuf;
44 
45 static void (*gsig)();
46 
47 /* ARGSUSED */
48 static void
49 galarm(sig)
50 int sig;
51 {
52 	signal(SIGALRM, galarm);
53 	longjmp(Getjbuf, 1);
54 }
55 
56 void
57 pkfail()
58 {
59 	longjmp(Gfailbuf, 1);
60 }
61 
62 int
63 gturnon()
64 {
65 	struct pack *pkopen();
66 	if (setjmp(Gfailbuf))
67 		return(FAIL);
68 	gsig=signal(SIGALRM, galarm);
69 	if (Debug > 4)
70 		pkdebug = 1;
71 	Pk = pkopen(Ifn, Ofn);
72 	if ((int) Pk == NULL)
73 		return(FAIL);
74 	return(0);
75 }
76 
77 int
78 gturnoff()
79 {
80 	if(setjmp(Gfailbuf))
81 		return(FAIL);
82 	pkclose();
83 	(void) signal(SIGALRM, gsig);
84 	return(0);
85 }
86 
87 /*ARGSUSED*/
88 int
89 gwrmsg(type, str, fn)
90 char type, *str;
91 {
92 	char bufr[BUFSIZ], *s;
93 	int len, i;
94 
95 	if(setjmp(Gfailbuf))
96 		return(FAIL);
97 	bufr[0] = type;
98 	s = &bufr[1];
99 	while (*str)
100 		*s++ = *str++;
101 	*s = '\0';
102 	if (*(--s) == '\n')
103 		*s = '\0';
104 	len = strlen(bufr) + 1;
105 	if ((i = len % xpacksize) != 0) {
106 		len = len + xpacksize - i;
107 		bufr[len - 1] = '\0';
108 	}
109 	gwrblk(bufr, len);
110 	return(0);
111 }
112 
113 
114 /*ARGSUSED*/
115 int
116 grdmsg(str, fn)
117 char *str;
118 {
119 	unsigned len;
120 
121 	if(setjmp(Gfailbuf))
122 		return(FAIL);
123 	for (;;) {
124 		len = pkread(str, packsize);
125 		if (len == 0)
126 			continue;
127 		str += len;
128 		if (*(str - 1) == '\0')
129 			break;
130 	}
131 	return(0);
132 }
133 
134 
135 /*ARGSUSED*/
136 int
137 gwrdata(fp1, fn)
138 FILE *fp1;
139 {
140 	char bufr[BUFSIZ];
141 	int fd1;
142 	int len;
143 	int ret;
144 	unsigned long bytes;
145 
146 	if(setjmp(Gfailbuf))
147 		return(FAIL);
148 	bytes = 0L;
149 	fd1 = fileno( fp1 );
150 	while ((len = read( fd1, bufr, BUFSIZ )) > 0) {
151 		bytes += len;
152 		putfilesize(bytes);
153 		ret = gwrblk(bufr, len);
154 		if (ret != len) {
155 			return(FAIL);
156 		}
157 		if (len != BUFSIZ)
158 			break;
159 	}
160 	ret = gwrblk(bufr, 0);
161 	return(0);
162 }
163 
164 /*ARGSUSED*/
165 int
166 grddata(fn, fp2)
167 FILE *fp2;
168 {
169 	register int ret = SUCCESS;
170 	int fd2;
171 	int len;
172 	char bufr[BUFSIZ];
173 	unsigned long bytes;
174 
175 	if(setjmp(Gfailbuf))
176 		return(FAIL);
177 	bytes = 0L;
178 	fd2 = fileno( fp2 );
179 	for (;;) {
180 		len = grdblk(bufr, BUFSIZ);
181 		if (len < 0) {
182 			return(FAIL);
183 		}
184 		bytes += len;
185 		putfilesize(bytes);
186 		if ( ret == SUCCESS && write( fd2, bufr, len ) != len) {
187 			ret = errno;
188 			DEBUG(7, "grddata: write to file failed, errno %d\n", errno);
189 		}
190 		if (len < BUFSIZ)
191 			break;
192 	}
193 	return(ret);
194 }
195 
196 
197 static
198 int
199 grdblk(blk, len)
200 char *blk;
201 {
202 	int i, ret;
203 
204 	for (i = 0; i < len; i += ret) {
205 		ret = pkread(blk, len - i);
206 		if (ret < 0)
207 			return(FAIL);
208 		blk += ret;
209 		if (ret == 0)
210 			return(i);
211 	}
212 	return(i);
213 }
214 
215 
216 static int
217 gwrblk(blk, len)
218 char *blk;
219 {
220 	return(pkwrite(blk, len));
221 }
222