xref: /netbsd-src/usr.sbin/rmt/rmt.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)rmt.c	5.6 (Berkeley) 6/1/90";*/
42 static char rcsid[] = "$Id: rmt.c,v 1.6 1995/04/13 02:07:23 mycroft Exp $";
43 #endif /* not lint */
44 
45 /*
46  * rmt
47  */
48 #include <stdio.h>
49 #include <sgtty.h>
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <sys/mtio.h>
54 #include <errno.h>
55 #include <string.h>
56 
57 int	tape = -1;
58 
59 char	*record;
60 int	maxrecsize = -1;
61 char	*checkbuf();
62 
63 #define	SSIZE	64
64 char	device[SSIZE];
65 char	count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
66 
67 char	resp[BUFSIZ];
68 
69 FILE	*debug;
70 #define	DEBUG(f)	if (debug) fprintf(debug, f)
71 #define	DEBUG1(f,a)	if (debug) fprintf(debug, f, a)
72 #define	DEBUG2(f,a1,a2)	if (debug) fprintf(debug, f, a1, a2)
73 
74 main(argc, argv)
75 	int argc;
76 	char **argv;
77 {
78 	int rval;
79 	char c;
80 	int n, i, cc;
81 
82 	argc--, argv++;
83 	if (argc > 0) {
84 		debug = fopen(*argv, "w");
85 		if (debug == 0)
86 			exit(1);
87 		(void) setbuf(debug, (char *)0);
88 	}
89 top:
90 	errno = 0;
91 	rval = 0;
92 	if (read(0, &c, 1) != 1)
93 		exit(0);
94 	switch (c) {
95 
96 	case 'O':
97 		if (tape >= 0)
98 			(void) close(tape);
99 		getstring(device); getstring(mode);
100 		DEBUG2("rmtd: O %s %s\n", device, mode);
101 		tape = open(device, atoi(mode),
102 		    S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
103 		if (tape < 0)
104 			goto ioerror;
105 		goto respond;
106 
107 	case 'C':
108 		DEBUG("rmtd: C\n");
109 		getstring(device);		/* discard */
110 		if (close(tape) < 0)
111 			goto ioerror;
112 		tape = -1;
113 		goto respond;
114 
115 	case 'L':
116 		getstring(count); getstring(pos);
117 		DEBUG2("rmtd: L %s %s\n", count, pos);
118 		rval = lseek(tape, atoi(count), atoi(pos));
119 		if (rval < 0)
120 			goto ioerror;
121 		goto respond;
122 
123 	case 'W':
124 		getstring(count);
125 		n = atoi(count);
126 		DEBUG1("rmtd: W %s\n", count);
127 		record = checkbuf(record, n);
128 		for (i = 0; i < n; i += cc) {
129 			cc = read(0, &record[i], n - i);
130 			if (cc <= 0) {
131 				DEBUG("rmtd: premature eof\n");
132 				exit(2);
133 			}
134 		}
135 		rval = write(tape, record, n);
136 		if (rval < 0)
137 			goto ioerror;
138 		goto respond;
139 
140 	case 'R':
141 		getstring(count);
142 		DEBUG1("rmtd: R %s\n", count);
143 		n = atoi(count);
144 		record = checkbuf(record, n);
145 		rval = read(tape, record, n);
146 		if (rval < 0)
147 			goto ioerror;
148 		(void) sprintf(resp, "A%d\n", rval);
149 		(void) write(1, resp, strlen(resp));
150 		(void) write(1, record, rval);
151 		goto top;
152 
153 	case 'I':
154 		getstring(op); getstring(count);
155 		DEBUG2("rmtd: I %s %s\n", op, count);
156 		{ struct mtop mtop;
157 		  mtop.mt_op = atoi(op);
158 		  mtop.mt_count = atoi(count);
159 		  if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
160 			goto ioerror;
161 		  rval = mtop.mt_count;
162 		}
163 		goto respond;
164 
165 	case 'S':		/* status */
166 		DEBUG("rmtd: S\n");
167 		{ struct mtget mtget;
168 		  if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
169 			goto ioerror;
170 		  rval = sizeof (mtget);
171 		  (void) sprintf(resp, "A%d\n", rval);
172 		  (void) write(1, resp, strlen(resp));
173 		  (void) write(1, (char *)&mtget, sizeof (mtget));
174 		  goto top;
175 		}
176 
177 	default:
178 		DEBUG1("rmtd: garbage command %c\n", c);
179 		exit(3);
180 	}
181 respond:
182 	DEBUG1("rmtd: A %d\n", rval);
183 	(void) sprintf(resp, "A%d\n", rval);
184 	(void) write(1, resp, strlen(resp));
185 	goto top;
186 ioerror:
187 	error(errno);
188 	goto top;
189 }
190 
191 getstring(bp)
192 	char *bp;
193 {
194 	int i;
195 	char *cp = bp;
196 
197 	for (i = 0; i < SSIZE; i++) {
198 		if (read(0, cp+i, 1) != 1)
199 			exit(0);
200 		if (cp[i] == '\n')
201 			break;
202 	}
203 	cp[i] = '\0';
204 }
205 
206 char *
207 checkbuf(record, size)
208 	char *record;
209 	int size;
210 {
211 	extern char *malloc();
212 
213 	if (size <= maxrecsize)
214 		return (record);
215 	if (record != 0)
216 		free(record);
217 	record = malloc(size);
218 	if (record == 0) {
219 		DEBUG("rmtd: cannot allocate buffer space\n");
220 		exit(4);
221 	}
222 	maxrecsize = size;
223 	while (size > 1024 &&
224 	       setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
225 		size -= 1024;
226 	return (record);
227 }
228 
229 error(num)
230 	int num;
231 {
232 
233 	DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
234 	(void) sprintf(resp, "E%d\n%s\n", num, strerror(num));
235 	(void) write(1, resp, strlen(resp));
236 }
237