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>
64 char count
[SSIZE
], mode
[SSIZE
], pos
[SSIZE
], op
[SSIZE
];
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 char *checkbuf(char *, int);
75 void getstring(char *);
78 main(int argc
, char **argv
)
86 debug
= fopen(*argv
, "w");
89 (void)setbuf(debug
, (char *)0);
94 if (read(STDIN_FILENO
, &c
, 1) != 1)
103 DEBUG2("rmtd: O %s %s\n", device
, mode
);
105 * XXX the rmt protocol does not provide a means to
106 * specify the permission bits; allow rw for everyone,
107 * as modified by the users umask
109 tape
= open(device
, atoi(mode
), 0666);
116 getstring(device
); /* discard */
125 DEBUG2("rmtd: L %s %s\n", count
, pos
);
126 rval
= lseek(tape
, (off_t
)strtoll(count
, NULL
, 10), atoi(pos
));
134 DEBUG1("rmtd: W %s\n", count
);
135 record
= checkbuf(record
, n
);
136 for (i
= 0; i
< n
; i
+= cc
) {
137 cc
= read(STDIN_FILENO
, &record
[i
], n
- i
);
139 DEBUG("rmtd: premature eof\n");
143 rval
= write(tape
, record
, n
);
150 DEBUG1("rmtd: R %s\n", count
);
152 record
= checkbuf(record
, n
);
153 rval
= read(tape
, record
, n
);
156 (void)sprintf(resp
, "A%d\n", rval
);
157 (void)write(STDOUT_FILENO
, resp
, strlen(resp
));
158 (void)write(STDOUT_FILENO
, record
, rval
);
164 DEBUG2("rmtd: I %s %s\n", op
, count
);
166 mtop
.mt_op
= atoi(op
);
167 mtop
.mt_count
= atoi(count
);
168 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0)
170 rval
= mtop
.mt_count
;
174 case 'S': /* status */
176 { struct mtget mtget
;
177 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0)
179 rval
= sizeof (mtget
);
180 if (rval
> 24) /* original mtget structure size */
182 (void)sprintf(resp
, "A%d\n", rval
);
183 (void)write(STDOUT_FILENO
, resp
, strlen(resp
));
184 (void)write(STDOUT_FILENO
, (char *)&mtget
, rval
);
188 case 'V': /* version */
190 DEBUG1("rmtd: V %s\n", op
);
195 DEBUG1("rmtd: garbage command %c\n", c
);
199 DEBUG1("rmtd: A %d\n", rval
);
200 (void)sprintf(resp
, "A%d\n", rval
);
201 (void)write(STDOUT_FILENO
, resp
, strlen(resp
));
215 for (i
= 0; i
< SSIZE
; i
++) {
216 if (read(STDIN_FILENO
, cp
+i
, 1) != 1)
230 if (size
<= maxrecsize
)
236 DEBUG("rmtd: cannot allocate buffer space\n");
240 while (size
> 1024 &&
241 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
));