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