xref: /titanic_41/usr/src/cmd/bnu/dio.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* from SVR4 bnu:dio.c 2.11 */
27 
28 #include "uucp.h"
29 
30 #ifdef D_PROTOCOL
31 #include <dk.h>
32 
33 #define XBUFSIZ 1024
34 time_t time();
35 static jmp_buf Dfailbuf;
36 extern int drdblk();
37 
38 /*
39  * Datakit protocol
40  */
41 /* ARGSUSED */
42 static void
43 dalarm(sig)
44 int sig;
45 {
46 	longjmp(Dfailbuf,1);
47 }
48 
49 static void (*dsig)();
50 #ifndef V8
51 static short dkrmode[3] = { DKR_BLOCK, 0, 0 };
52 static short dkeof[3] = { 106, 0, 0 };	/* End of File signal */
53 #endif
54 
55 /*
56  * turn on protocol
57  */
58 int
59 dturnon()
60 {
61 #ifdef V8
62 	extern int dkp_ld;
63 #endif
64 
65 	dsig=signal(SIGALRM, dalarm);
66 #ifdef V8
67 	if (dkproto(Ofn, dkp_ld) < 0)
68 	   {
69 		DEBUG(3, "%s\n", "No dkp_ld");
70 		return(-1);
71 	   }
72 #else
73 	if((*Ioctl)(Ofn, DIOCRMODE, dkrmode) < 0) {
74 	    int ret;
75 	    ret=(*Ioctl)(Ofn, DIOCRMODE, dkrmode);
76 	    DEBUG(4, "dturnon: ret=%d, ", ret);
77 	    DEBUG(4, "Ofn=%d, ", Ofn);
78 	    DEBUG(4, "errno=%d\n", errno);
79 	    return(-1);
80 	}
81 #endif /* V8 */
82 	return(0);
83 }
84 
85 int
86 dturnoff()
87 {
88 	(void) signal(SIGALRM, dsig);
89 	return(0);
90 }
91 
92 /*
93  * write message across Datakit link
94  *	type	-> message type
95  *	str	-> message body (ascii string)
96  *	fn	-> Datakit file descriptor
97  * return
98  *	SUCCESS	-> message sent
99  *	FAIL	-> write failed
100  */
101 int
102 dwrmsg(type, str, fn)
103 register char *str;
104 int fn;
105 char type;
106 {
107 	register char *s;
108 	char bufr[XBUFSIZ];
109 
110 	bufr[0] = type;
111 	s = &bufr[1];
112 	while (*str)
113 		*s++ = *str++;
114 	*s = '\0';
115 	if (*(--s) == '\n')
116 		*s = '\0';
117 	return((*Write)(fn, bufr, (unsigned) strlen(bufr) + 1) < 0 ? FAIL : SUCCESS);
118 }
119 
120 /*
121  * read message from Datakit link
122  *	str	-> message buffer
123  *	fn	-> Datakit file descriptor
124  * return
125  *	FAIL	-> send timed out
126  *	SUCCESS	-> ok message in str
127  */
128 int
129 drdmsg(str, fn)
130 register char *str;
131 {
132 
133 	register int len;
134 
135 	if(setjmp(Dfailbuf))
136 		return(FAIL);
137 
138 	(void) alarm(60);
139 	for (;;) {
140 		if( (len = (*Read)(fn, str, XBUFSIZ)) <= 0) {
141 			(void) alarm(0);
142 			return(FAIL);
143 		}
144 		str += len;
145 		if (*(str - 1) == '\0')
146 			break;
147 	}
148 	(void) alarm(0);
149 	return(SUCCESS);
150 }
151 
152 /*
153  * read data from file fp1 and write
154  * on Datakit link
155  *	fp1	-> file descriptor
156  *	fn	-> Datakit descriptor
157  * returns:
158  *	FAIL	->failure in Datakit link
159  *	SUCCESS	-> ok
160  */
161 int
162 dwrdata(fp1, fn)
163 FILE *fp1;
164 {
165 	register int fd1;
166 	register int len, ret;
167 	unsigned long bytes;
168 	char bufr[XBUFSIZ];
169 
170 	bytes = 0L;
171 	fd1 = fileno( fp1 );
172 	while ((len = read( fd1, bufr, XBUFSIZ )) > 0) {
173 		bytes += len;
174 		putfilesize(bytes);
175 		ret = (*Write)(fn, bufr, (unsigned) len);
176 		if (ret != len) {
177 			return(FAIL);
178 		}
179 		if (len != XBUFSIZ)
180 			break;
181 	}
182 	ASSERT(len >= 0, "DISK READ ERROR", sys_errlist[errno], len);
183 #ifndef V8
184 	(*Ioctl)(fn, DIOCXCTL, dkeof);
185 #endif
186 	ret = (*Write)(fn, bufr, (unsigned) 0);
187 	return(SUCCESS);
188 }
189 
190 /*
191  * read data from Datakit link and
192  * write into file
193  *	fp2	-> file descriptor
194  *	fn	-> Datakit descriptor
195  * returns:
196  *	SUCCESS	-> ok
197  *	FAIL	-> failure on Datakit link
198  */
199 int
200 drddata(fn, fp2)
201 FILE *fp2;
202 {
203 	register int fd2;
204 	register int len;
205 	register int ret = SUCCESS;
206 	unsigned long bytes;
207 	char bufr[XBUFSIZ];
208 
209 	bytes = 0L;
210 	fd2 = fileno( fp2 );
211 	for (;;) {
212 		len = drdblk(bufr, XBUFSIZ, fn);
213 		if (len < 0) {
214 			return(FAIL);
215 		}
216 		bytes += len;
217 		putfilesize(bytes);
218 		if( ret == SUCCESS && write( fd2, bufr, len ) != len )
219 			ret = errno;
220 		if (len < XBUFSIZ)
221 			break;
222 	}
223 	return(ret);
224 }
225 
226 /*
227  * read block from Datakit link
228  * reads are timed
229  *	blk	-> address of buffer
230  *	len	-> size to read
231  *	fn	-> Datakit descriptor
232  * returns:
233  *	FAIL	-> link error timeout on link
234  *	i	-> # of bytes read
235  */
236 int
237 drdblk(blk, len,  fn)
238 register char *blk;
239 {
240 	register int i, ret;
241 	struct dkqqabo	why;
242 
243 	if(setjmp(Dfailbuf))
244 		return(FAIL);
245 
246 	for (i = 0; i < len; i += ret) {
247 		(void) alarm(60);
248 		if ((ret = (*Read)(fn, blk, (unsigned) len - i)) < 0) {
249 			(void) alarm(0);
250 			return(FAIL);
251 		}
252 		blk += ret;
253 		if (ret == 0) {	/* zero length block contains only EOF signal */
254 			ioctl(fn, DIOCQQABO, &why);
255 			if (why.rcv_ctlchar != dkeof[0])
256 				i = FAIL;
257 			break;
258 		}
259 	}
260 	(void) alarm(0);
261 	return(i);
262 }
263 #endif /* D_PROTOCOL */
264