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