2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)rmt.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
50 #include <sys/socket.h>
62 static int maxrecsize = -1;
65 static char device[SSIZE];
66 static char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
68 static char resp[BUFSIZ];
71 #define DEBUG(f) if (debug) fprintf(debug, f)
72 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
73 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
75 static char *checkbuf(char *, int);
76 static void error(int);
77 static void getstring(char *);
80 main(int argc, char **argv)
88 debug = fopen(*argv, "w");
90 DEBUG1("rmtd: error to open %s\n", *argv);
93 (void)setbuf(debug, (char *)0);
98 if (read(STDIN_FILENO, &c, 1) != 1)
107 DEBUG2("rmtd: O %s %s\n", device, mode);
109 * XXX the rmt protocol does not provide a means to
110 * specify the permission bits; allow rw for everyone,
111 * as modified by the users umask
113 tape = open(device, atoi(mode), 0666);
120 getstring(device); /* discard */
129 DEBUG2("rmtd: L %s %s\n", count, pos);
130 rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos));
138 DEBUG1("rmtd: W %s\n", count);
139 record = checkbuf(record, n);
140 for (i = 0; i < n; i += cc) {
141 cc = read(STDIN_FILENO, &record[i], n - i);
143 DEBUG("rmtd: premature eof\n");
147 rval = write(tape, record, n);
154 DEBUG1("rmtd: R %s\n", count);
156 record = checkbuf(record, n);
157 rval = read(tape, record, n);
160 (void)sprintf(resp, "A%d\n", rval);
161 (void)write(STDOUT_FILENO, resp, strlen(resp));
162 (void)write(STDOUT_FILENO, record, rval);
168 DEBUG2("rmtd: I %s %s\n", op, count);
170 mtop.mt_op = atoi(op);
171 mtop.mt_count = atoi(count);
172 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
174 rval = mtop.mt_count;
178 case 'S': /* status */
180 { struct mtget mtget;
181 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
183 rval = sizeof (mtget);
184 if (rval > 24) /* original mtget structure size */
186 (void)sprintf(resp, "A%d\n", rval);
187 (void)write(STDOUT_FILENO, resp, strlen(resp));
188 (void)write(STDOUT_FILENO, (char *)&mtget, rval);
192 case 'V': /* version */
194 DEBUG1("rmtd: V %s\n", op);
199 DEBUG1("rmtd: garbage command %c\n", c);
203 DEBUG1("rmtd: A %d\n", rval);
204 (void)sprintf(resp, "A%d\n", rval);
205 (void)write(STDOUT_FILENO, resp, strlen(resp));
218 for (i = 0; i < SSIZE; i++) {
219 if (read(STDIN_FILENO, cp+i, 1) != 1)
228 checkbuf(char *rec, int size)
231 if (size <= maxrecsize)
237 DEBUG("rmtd: cannot allocate buffer space\n");
241 while (size > 1024 &&
242 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
251 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
252 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
253 (void)write(STDOUT_FILENO, resp, strlen(resp));