xref: /titanic_44/usr/src/cmd/bnu/gio.c (revision bde3d612a7c090234c60e6e4578821237a5db135)
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 
37 struct pack *Pk;
38 extern int pkread(), pkwrite();
39 extern void pkclose();
40 
41 static int grdblk(char *, int);
42 static int gwrblk(char *, int);
43 
44 extern int packsize, xpacksize;
45 
46 jmp_buf Getjbuf, Gfailbuf;
47 
48 static void (*gsig)();
49 
50 /* ARGSUSED */
51 static void
52 galarm(sig)
53 int sig;
54 {
55 	signal(SIGALRM, galarm);
56 	longjmp(Getjbuf, 1);
57 }
58 
59 void
60 pkfail()
61 {
62 	longjmp(Gfailbuf, 1);
63 }
64 
65 int
66 gturnon()
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 
80 int
81 gturnoff()
82 {
83 	if(setjmp(Gfailbuf))
84 		return(FAIL);
85 	pkclose();
86 	(void) signal(SIGALRM, gsig);
87 	return(0);
88 }
89 
90 /*ARGSUSED*/
91 int
92 gwrmsg(type, str, fn)
93 char 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*/
118 int
119 grdmsg(str, fn)
120 char *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*/
139 int
140 gwrdata(fp1, fn)
141 FILE *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*/
168 int
169 grddata(fn, fp2)
170 FILE *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 
200 static
201 int
202 grdblk(blk, len)
203 char *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 
219 static int
220 gwrblk(blk, len)
221 char *blk;
222 {
223 	return(pkwrite(blk, len));
224 }
225