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 * 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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)rmt.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
51 #include <sys/types.h>
52 #include <sys/socket.h>
69 char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
74 #define DEBUG(f) if (debug) fprintf(debug, f)
75 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
76 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
78 char *checkbuf __P((char *, int));
79 void error __P((int));
80 void getstring __P((char *));
93 debug = fopen(*argv, "w");
96 (void)setbuf(debug, (char *)0);
101 if (read(STDIN_FILENO, &c, 1) != 1)
110 DEBUG2("rmtd: O %s %s\n", device, mode);
112 * XXX the rmt protocol does not provide a means to
113 * specify the permission bits; allow rw for everyone,
114 * as modified by the users umask
116 tape = open(device, atoi(mode), 0666);
123 getstring(device); /* discard */
132 DEBUG2("rmtd: L %s %s\n", count, pos);
133 rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos));
141 DEBUG1("rmtd: W %s\n", count);
142 record = checkbuf(record, n);
143 for (i = 0; i < n; i += cc) {
144 cc = read(STDIN_FILENO, &record[i], n - i);
146 DEBUG("rmtd: premature eof\n");
150 rval = write(tape, record, n);
157 DEBUG1("rmtd: R %s\n", count);
159 record = checkbuf(record, n);
160 rval = read(tape, record, n);
163 (void)sprintf(resp, "A%d\n", rval);
164 (void)write(STDOUT_FILENO, resp, strlen(resp));
165 (void)write(STDOUT_FILENO, record, rval);
171 DEBUG2("rmtd: I %s %s\n", op, count);
173 mtop.mt_op = atoi(op);
174 mtop.mt_count = atoi(count);
175 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
177 rval = mtop.mt_count;
181 case 'S': /* status */
183 { struct mtget mtget;
184 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
186 rval = sizeof (mtget);
187 if (rval > 24) /* original mtget structure size */
189 (void)sprintf(resp, "A%d\n", rval);
190 (void)write(STDOUT_FILENO, resp, strlen(resp));
191 (void)write(STDOUT_FILENO, (char *)&mtget, rval);
195 case 'V': /* version */
197 DEBUG1("rmtd: V %s\n", op);
202 DEBUG1("rmtd: garbage command %c\n", c);
206 DEBUG1("rmtd: A %d\n", rval);
207 (void)sprintf(resp, "A%d\n", rval);
208 (void)write(STDOUT_FILENO, resp, strlen(resp));
222 for (i = 0; i < SSIZE; i++) {
223 if (read(STDIN_FILENO, cp+i, 1) != 1)
232 checkbuf(record, size)
237 if (size <= maxrecsize)
241 record = malloc(size);
243 DEBUG("rmtd: cannot allocate buffer space\n");
247 while (size > 1024 &&
248 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
258 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
259 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
260 (void)write(STDOUT_FILENO, resp, strlen(resp));