xref: /illumos-gate/usr/src/cmd/bnu/xio.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 #include "uucp.h"
31 
32 #ifdef X_PROTOCOL
33 
34 #define XBUFSIZ 512
35 static jmp_buf Xfailbuf;
36 extern int xrdblk();
37 extern unsigned msgtime;
38 
39 /*
40  * x.25 protocol
41  */
42 /* ARGSUSED */
43 static void
xalarm(sig)44 xalarm(sig)
45 int sig;
46 {
47 	longjmp(Xfailbuf, 1);
48 }
49 
50 static void (*xsig)();
51 
52 /*
53  * turn on protocol timer
54  */
55 int
xturnon()56 xturnon()
57 {
58 	xsig=signal(SIGALRM, xalarm);
59 	return(0);
60 }
61 
62 int
xturnoff()63 xturnoff()
64 {
65 	(void) signal(SIGALRM, xsig);
66 	return(0);
67 }
68 
69 /*
70  * write message across x.25 link
71  *	type	-> message type
72  *	str	-> message body (ascii string)
73  *	fn	-> x.25 file descriptor
74  * return
75  *	0
76  */
77 int
xwrmsg(type,str,fn)78 xwrmsg(type, str, fn)
79 char *str;
80 int fn;
81 char type;
82 {
83 	char *s;
84 	char bufr[XBUFSIZ];
85 
86 	bufr[0] = type;
87 	s = &bufr[1];
88 	while (*str)
89 		*s++ = *str++;
90 	*s = '\0';
91 	if (*(--s) == '\n')
92 		*s = '\0';
93 	(void) (*Write)(fn, bufr, strlen(bufr) + 1);
94 	return(0);
95 }
96 
97 /*
98  * read message from x.25 link
99  *	str	-> message buffer
100  *	fn	-> x.25 file descriptor
101  * return
102  *	FAIL	-> send timed out
103  *	0	-> ok message in str
104  */
105 int
xrdmsg(char * str,int fn)106 xrdmsg(char *str, int fn)
107 {
108 	int len;
109 
110 	if(setjmp(Xfailbuf))
111 		return(FAIL);
112 
113 	(void) alarm(msgtime);
114 	for (;;) {
115 		if( (len = (*Read)(fn, str, XBUFSIZ)) == 0)
116 			continue;
117 		if (len < 0) {
118 			(void) alarm(0);
119 			return(FAIL);
120 		}
121 		str += len;
122 		if (*(str - 1) == '\0')
123 			break;
124 	}
125 	(void) alarm(0);
126 	return(0);
127 }
128 
129 /*
130  * read data from file fp1 and write
131  * on x.25 link
132  *	fp1	-> file descriptor
133  *	fn	-> x.25 descriptor
134  * returns:
135  *	FAIL	->failure in x.25 link
136  *	0	-> ok
137  */
138 int
xwrdata(FILE * fp1,int fn)139 xwrdata(FILE *fp1, int fn)
140 {
141 	int fd1;
142 	int len, ret;
143 	unsigned long bytes;
144 	char bufr[XBUFSIZ];
145 
146 	bytes = 0L;
147 	fd1 = fileno( fp1 );
148 	while ((len = read( fd1, bufr, XBUFSIZ )) > 0) {
149 		bytes += len;
150 		putfilesize(bytes);
151 		ret = (*Write)(fn, bufr, len);
152 		if (ret != len) {
153 			return(FAIL);
154 		}
155 		if (len != XBUFSIZ)
156 			break;
157 	}
158 	ret = (*Write)(fn, bufr, 0);
159 	return(0);
160 }
161 
162 /*
163  * read data from x.25 link and
164  * write into file
165  *	fp2	-> file descriptor
166  *	fn	-> x.25 descriptor
167  * returns:
168  *	0	-> ok
169  *	FAIL	-> failure on x.25 link
170  */
171 int
xrddata(int fn,FILE * fp2)172 xrddata(int fn, FILE *fp2)
173 {
174 	int fd2;
175 	int len;
176 	int ret = SUCCESS;
177 	unsigned long bytes;
178 	char bufr[XBUFSIZ];
179 
180 	bytes = 0L;
181 	fd2 = fileno( fp2 );
182 	for (;;) {
183 		len = xrdblk(bufr, XBUFSIZ, fn);
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 		if (len < XBUFSIZ)
192 			break;
193 	}
194 	return(ret);
195 }
196 
197 /*
198  * read blank from x.25 link
199  * reads are timed
200  *	blk	-> address of buffer
201  *	len	-> size to read
202  *	fn	-> x.25 descriptor
203  * returns:
204  *	FAIL	-> link error timeout on link
205  *	i	-> # of bytes read
206  */
207 int
xrdblk(char * blk,int len,int fn)208 xrdblk(char *blk, int len, int fn)
209 {
210 	int i, ret;
211 
212 	if(setjmp(Xfailbuf))
213 		return(FAIL);
214 
215 	(void) alarm(msgtime);
216 	for (i = 0; i < len; i += ret) {
217 		if ((ret = (*Read)(fn, blk, len - i)) < 0) {
218 			(void) alarm(0);
219 			return(FAIL);
220 		}
221 		blk += ret;
222 		if (ret == 0)
223 			break;
224 	}
225 	(void) alarm(0);
226 	return(i);
227 }
228 #endif /* X_PROTOCOL */
229