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