2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1983, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)rmt.c 8.1 (Berkeley) 6/6/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
47 #include <sys/types.h>
48 #include <sys/socket.h>
60 static int maxrecsize = -1;
63 static char device[SSIZE];
64 static char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
66 static char resp[BUFSIZ];
69 #define DEBUG(f) if (debug) fprintf(debug, f)
70 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
71 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
73 static char *checkbuf(char *, int);
74 static void error(int);
75 static void getstring(char *);
78 main(int argc, char **argv)
86 debug = fopen(*argv, "w");
88 DEBUG1("rmtd: error to open %s\n", *argv);
91 (void)setbuf(debug, (char *)0);
96 if (read(STDIN_FILENO, &c, 1) != 1)
105 DEBUG2("rmtd: O %s %s\n", device, mode);
107 * XXX the rmt protocol does not provide a means to
108 * specify the permission bits; allow rw for everyone,
109 * as modified by the users umask
111 tape = open(device, atoi(mode), 0666);
118 getstring(device); /* discard */
127 DEBUG2("rmtd: L %s %s\n", count, pos);
128 rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos));
136 DEBUG1("rmtd: W %s\n", count);
137 record = checkbuf(record, n);
138 for (i = 0; i < n; i += cc) {
139 cc = read(STDIN_FILENO, &record[i], n - i);
141 DEBUG("rmtd: premature eof\n");
145 rval = write(tape, record, n);
152 DEBUG1("rmtd: R %s\n", count);
154 record = checkbuf(record, n);
155 rval = read(tape, record, n);
158 (void)sprintf(resp, "A%d\n", rval);
159 (void)write(STDOUT_FILENO, resp, strlen(resp));
160 (void)write(STDOUT_FILENO, record, rval);
166 DEBUG2("rmtd: I %s %s\n", op, count);
168 mtop.mt_op = atoi(op);
169 mtop.mt_count = atoi(count);
170 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
172 rval = mtop.mt_count;
176 case 'S': /* status */
178 { struct mtget mtget;
179 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
181 rval = sizeof (mtget);
182 if (rval > 24) /* original mtget structure size */
184 (void)sprintf(resp, "A%d\n", rval);
185 (void)write(STDOUT_FILENO, resp, strlen(resp));
186 (void)write(STDOUT_FILENO, (char *)&mtget, rval);
190 case 'V': /* version */
192 DEBUG1("rmtd: V %s\n", op);
197 DEBUG1("rmtd: garbage command %c\n", c);
201 DEBUG1("rmtd: A %d\n", rval);
202 (void)sprintf(resp, "A%d\n", rval);
203 (void)write(STDOUT_FILENO, resp, strlen(resp));
216 for (i = 0; i < SSIZE; i++) {
217 if (read(STDIN_FILENO, cp+i, 1) != 1)
226 checkbuf(char *rec, int size)
229 if (size <= maxrecsize)
235 DEBUG("rmtd: cannot allocate buffer space\n");
239 while (size > 1024 &&
240 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
249 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
250 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
251 (void)write(STDOUT_FILENO, resp, strlen(resp));