xref: /illumos-gate/usr/src/cmd/bnu/xio.c (revision a89c0811c892ec231725fe10817ef95dda813c06)
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
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
56 xturnon()
57 {
58 	xsig=signal(SIGALRM, xalarm);
59 	return(0);
60 }
61 
62 int
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
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
106 xrdmsg(str, fn)
107 char *str;
108 {
109 	int len;
110 
111 	if(setjmp(Xfailbuf))
112 		return(FAIL);
113 
114 	(void) alarm(msgtime);
115 	for (;;) {
116 		if( (len = (*Read)(fn, str, XBUFSIZ)) == 0)
117 			continue;
118 		if (len < 0) {
119 			(void) alarm(0);
120 			return(FAIL);
121 		}
122 		str += len;
123 		if (*(str - 1) == '\0')
124 			break;
125 	}
126 	(void) alarm(0);
127 	return(0);
128 }
129 
130 /*
131  * read data from file fp1 and write
132  * on x.25 link
133  *	fp1	-> file descriptor
134  *	fn	-> x.25 descriptor
135  * returns:
136  *	FAIL	->failure in x.25 link
137  *	0	-> ok
138  */
139 int
140 xwrdata(fp1, fn)
141 FILE *fp1;
142 {
143 	int fd1;
144 	int len, ret;
145 	unsigned long bytes;
146 	char bufr[XBUFSIZ];
147 
148 	bytes = 0L;
149 	fd1 = fileno( fp1 );
150 	while ((len = read( fd1, bufr, XBUFSIZ )) > 0) {
151 		bytes += len;
152 		putfilesize(bytes);
153 		ret = (*Write)(fn, bufr, len);
154 		if (ret != len) {
155 			return(FAIL);
156 		}
157 		if (len != XBUFSIZ)
158 			break;
159 	}
160 	ret = (*Write)(fn, bufr, 0);
161 	return(0);
162 }
163 
164 /*
165  * read data from x.25 link and
166  * write into file
167  *	fp2	-> file descriptor
168  *	fn	-> x.25 descriptor
169  * returns:
170  *	0	-> ok
171  *	FAIL	-> failure on x.25 link
172  */
173 int
174 xrddata(fn, fp2)
175 FILE *fp2;
176 {
177 	int fd2;
178 	int len;
179 	int ret = SUCCESS;
180 	unsigned long bytes;
181 	char bufr[XBUFSIZ];
182 
183 	bytes = 0L;
184 	fd2 = fileno( fp2 );
185 	for (;;) {
186 		len = xrdblk(bufr, XBUFSIZ, fn);
187 		if (len < 0) {
188 			return(FAIL);
189 		}
190 		bytes += len;
191 		putfilesize(bytes);
192 		if( ret == SUCCESS && write( fd2, bufr, len ) != len )
193 			ret = errno;
194 		if (len < XBUFSIZ)
195 			break;
196 	}
197 	return(ret);
198 }
199 
200 /*
201  * read blank from x.25 link
202  * reads are timed
203  *	blk	-> address of buffer
204  *	len	-> size to read
205  *	fn	-> x.25 descriptor
206  * returns:
207  *	FAIL	-> link error timeout on link
208  *	i	-> # of bytes read
209  */
210 int
211 xrdblk(blk, len,  fn)
212 char *blk;
213 {
214 	int i, ret;
215 
216 	if(setjmp(Xfailbuf))
217 		return(FAIL);
218 
219 	(void) alarm(msgtime);
220 	for (i = 0; i < len; i += ret) {
221 		if ((ret = (*Read)(fn, blk, len - i)) < 0) {
222 			(void) alarm(0);
223 			return(FAIL);
224 		}
225 		blk += ret;
226 		if (ret == 0)
227 			break;
228 	}
229 	(void) alarm(0);
230 	return(i);
231 }
232 #endif /* X_PROTOCOL */
233