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