summaryrefslogtreecommitdiff
path: root/usr/src/cmd/bnu/dio.c
blob: 5ff4a34b6f916b8ff38a0986a7765f95c02ea947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "uucp.h"

#ifdef D_PROTOCOL
#include <dk.h>

#define XBUFSIZ 1024
time_t time();
static jmp_buf Dfailbuf;
extern int drdblk();

/*
 * Datakit protocol
 */
/* ARGSUSED */
static void
dalarm(sig)
int sig;
{
	longjmp(Dfailbuf,1);
}

static void (*dsig)();
#ifndef V8
static short dkrmode[3] = { DKR_BLOCK, 0, 0 };
static short dkeof[3] = { 106, 0, 0 };	/* End of File signal */
#endif

/*
 * turn on protocol
 */
int
dturnon()
{
#ifdef V8
	extern int dkp_ld;
#endif

	dsig=signal(SIGALRM, dalarm);
#ifdef V8
	if (dkproto(Ofn, dkp_ld) < 0)
	   {
		DEBUG(3, "%s\n", "No dkp_ld");
		return(-1);
	   }
#else
	if((*Ioctl)(Ofn, DIOCRMODE, dkrmode) < 0) {
	    int ret;
	    ret=(*Ioctl)(Ofn, DIOCRMODE, dkrmode);
	    DEBUG(4, "dturnon: ret=%d, ", ret);
	    DEBUG(4, "Ofn=%d, ", Ofn);
	    DEBUG(4, "errno=%d\n", errno);
	    return(-1);
	}
#endif /* V8 */
	return(0);
}

int
dturnoff()
{
	(void) signal(SIGALRM, dsig);
	return(0);
}

/*
 * write message across Datakit link
 *	type	-> message type
 *	str	-> message body (ascii string)
 *	fn	-> Datakit file descriptor
 * return
 *	SUCCESS	-> message sent
 *	FAIL	-> write failed
 */
int
dwrmsg(type, str, fn)
register char *str;
int fn;
char type;
{
	register char *s;
	char bufr[XBUFSIZ];

	bufr[0] = type;
	s = &bufr[1];
	while (*str)
		*s++ = *str++;
	*s = '\0';
	if (*(--s) == '\n')
		*s = '\0';
	return((*Write)(fn, bufr, (unsigned) strlen(bufr) + 1) < 0 ? FAIL : SUCCESS);
}

/*
 * read message from Datakit link
 *	str	-> message buffer
 *	fn	-> Datakit file descriptor
 * return
 *	FAIL	-> send timed out
 *	SUCCESS	-> ok message in str
 */
int
drdmsg(str, fn)
register char *str;
{

	register int len;

	if(setjmp(Dfailbuf))
		return(FAIL);

	(void) alarm(60);
	for (;;) {
		if( (len = (*Read)(fn, str, XBUFSIZ)) <= 0) {
			(void) alarm(0);
			return(FAIL);
		}
		str += len;
		if (*(str - 1) == '\0')
			break;
	}
	(void) alarm(0);
	return(SUCCESS);
}

/*
 * read data from file fp1 and write
 * on Datakit link
 *	fp1	-> file descriptor
 *	fn	-> Datakit descriptor
 * returns:
 *	FAIL	->failure in Datakit link
 *	SUCCESS	-> ok
 */
int
dwrdata(fp1, fn)
FILE *fp1;
{
	register int fd1;
	register int len, ret;
	unsigned long bytes;
	char bufr[XBUFSIZ];

	bytes = 0L;
	fd1 = fileno( fp1 );
	while ((len = read( fd1, bufr, XBUFSIZ )) > 0) {
		bytes += len;
		putfilesize(bytes);
		ret = (*Write)(fn, bufr, (unsigned) len);
		if (ret != len) {
			return(FAIL);
		}
		if (len != XBUFSIZ)
			break;
	}
	ASSERT(len >= 0, "DISK READ ERROR", strerror(errno), len);
#ifndef V8
	(*Ioctl)(fn, DIOCXCTL, dkeof);
#endif
	ret = (*Write)(fn, bufr, (unsigned) 0);
	return(SUCCESS);
}

/*
 * read data from Datakit link and
 * write into file
 *	fp2	-> file descriptor
 *	fn	-> Datakit descriptor
 * returns:
 *	SUCCESS	-> ok
 *	FAIL	-> failure on Datakit link
 */
int
drddata(fn, fp2)
FILE *fp2;
{
	register int fd2;
	register int len;
	register int ret = SUCCESS;
	unsigned long bytes;
	char bufr[XBUFSIZ];

	bytes = 0L;
	fd2 = fileno( fp2 );
	for (;;) {
		len = drdblk(bufr, XBUFSIZ, fn);
		if (len < 0) {
			return(FAIL);
		}
		bytes += len;
		putfilesize(bytes);
		if( ret == SUCCESS && write( fd2, bufr, len ) != len )
			ret = errno;
		if (len < XBUFSIZ)
			break;
	}
	return(ret);
}

/*
 * read block from Datakit link
 * reads are timed
 *	blk	-> address of buffer
 *	len	-> size to read
 *	fn	-> Datakit descriptor
 * returns:
 *	FAIL	-> link error timeout on link
 *	i	-> # of bytes read
 */
int
drdblk(blk, len,  fn)
register char *blk;
{
	register int i, ret;
	struct dkqqabo	why;

	if(setjmp(Dfailbuf))
		return(FAIL);

	for (i = 0; i < len; i += ret) {
		(void) alarm(60);
		if ((ret = (*Read)(fn, blk, (unsigned) len - i)) < 0) {
			(void) alarm(0);
			return(FAIL);
		}
		blk += ret;
		if (ret == 0) {	/* zero length block contains only EOF signal */
			ioctl(fn, DIOCQQABO, &why);
			if (why.rcv_ctlchar != dkeof[0])
				i = FAIL;
			break;
		}
	}
	(void) alarm(0);
	return(i);
}
#endif /* D_PROTOCOL */