xref: /illumos-gate/usr/src/cmd/bnu/gio.c (revision 3b296559)
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
galarm(sig)50 galarm(sig)
51 int sig;
52 {
53 	signal(SIGALRM, galarm);
54 	longjmp(Getjbuf, 1);
55 }
56 
57 void
pkfail()58 pkfail()
59 {
60 	longjmp(Gfailbuf, 1);
61 }
62 
63 int
gturnon()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
gturnoff()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
gwrmsg(char type,char * str,int fn)90 gwrmsg(char type, char *str, int fn)
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
grdmsg(char * str,int fn)116 grdmsg(char *str, int fn)
117 {
118 	unsigned len;
119 
120 	if(setjmp(Gfailbuf))
121 		return(FAIL);
122 	for (;;) {
123 		len = pkread(str, packsize);
124 		if (len == 0)
125 			continue;
126 		str += len;
127 		if (*(str - 1) == '\0')
128 			break;
129 	}
130 	return(0);
131 }
132 
133 
134 /*ARGSUSED*/
135 int
gwrdata(FILE * fp1,int fn)136 gwrdata(FILE *fp1, int fn)
137 {
138 	char bufr[BUFSIZ];
139 	int fd1;
140 	int len;
141 	int ret;
142 	unsigned long bytes;
143 
144 	if(setjmp(Gfailbuf))
145 		return(FAIL);
146 	bytes = 0L;
147 	fd1 = fileno( fp1 );
148 	while ((len = read( fd1, bufr, BUFSIZ )) > 0) {
149 		bytes += len;
150 		putfilesize(bytes);
151 		ret = gwrblk(bufr, len);
152 		if (ret != len) {
153 			return(FAIL);
154 		}
155 		if (len != BUFSIZ)
156 			break;
157 	}
158 	ret = gwrblk(bufr, 0);
159 	return(0);
160 }
161 
162 /*ARGSUSED*/
163 int
grddata(int fn,FILE * fp2)164 grddata(int fn, FILE *fp2)
165 {
166 	int ret = SUCCESS;
167 	int fd2;
168 	int len;
169 	char bufr[BUFSIZ];
170 	unsigned long bytes;
171 
172 	if(setjmp(Gfailbuf))
173 		return(FAIL);
174 	bytes = 0L;
175 	fd2 = fileno( fp2 );
176 	for (;;) {
177 		len = grdblk(bufr, BUFSIZ);
178 		if (len < 0) {
179 			return(FAIL);
180 		}
181 		bytes += len;
182 		putfilesize(bytes);
183 		if ( ret == SUCCESS && write( fd2, bufr, len ) != len) {
184 			ret = errno;
185 			DEBUG(7, "grddata: write to file failed, errno %d\n", errno);
186 		}
187 		if (len < BUFSIZ)
188 			break;
189 	}
190 	return(ret);
191 }
192 
193 
194 static
195 int
grdblk(char * blk,int len)196 grdblk(char *blk, int len)
197 {
198 	int i, ret;
199 
200 	for (i = 0; i < len; i += ret) {
201 		ret = pkread(blk, len - i);
202 		if (ret < 0)
203 			return(FAIL);
204 		blk += ret;
205 		if (ret == 0)
206 			return(i);
207 	}
208 	return(i);
209 }
210 
211 
212 static int
gwrblk(char * blk,int len)213 gwrblk(char *blk, int len)
214 {
215 	return(pkwrite(blk, len));
216 }
217