mt.c revision 1.22 1 1.22 mjacob /* $NetBSD: mt.c,v 1.22 1997/09/29 19:35:31 mjacob Exp $ */
2 1.5 cgd
3 1.1 cgd /*
4 1.5 cgd * Copyright (c) 1980, 1993
5 1.5 cgd * 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.21 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.21 christos __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 1.21 christos 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.5 cgd #if 0
44 1.8 tls static char sccsid[] = "@(#)mt.c 8.2 (Berkeley) 6/6/93";
45 1.5 cgd #else
46 1.22 mjacob __RCSID("$NetBSD: mt.c,v 1.22 1997/09/29 19:35:31 mjacob Exp $");
47 1.5 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd * mt --
52 1.1 cgd * magnetic tape manipulation program
53 1.1 cgd */
54 1.17 jtc #include <rmt.h>
55 1.1 cgd #include <sys/types.h>
56 1.1 cgd #include <sys/ioctl.h>
57 1.1 cgd #include <sys/mtio.h>
58 1.12 scottr #include <sys/stat.h>
59 1.12 scottr
60 1.12 scottr #include <ctype.h>
61 1.12 scottr #include <err.h>
62 1.1 cgd #include <fcntl.h>
63 1.19 lukem #include <paths.h>
64 1.12 scottr #include <stdio.h>
65 1.5 cgd #include <stdlib.h>
66 1.3 jtc #include <string.h>
67 1.11 scottr #include <unistd.h>
68 1.1 cgd
69 1.16 jtc /* pseudo ioctl constants */
70 1.16 jtc #define MTASF 100
71 1.16 jtc
72 1.1 cgd struct commands {
73 1.20 hannken const char *c_name; /* command */
74 1.20 hannken int c_code; /* ioctl code for this command */
75 1.20 hannken int c_ronly; /* open tape read-only */
76 1.20 hannken int c_mincount; /* min allowed count value */
77 1.16 jtc };
78 1.16 jtc
79 1.16 jtc const struct commands com[] = {
80 1.20 hannken { "asf", MTASF, 1, 1 },
81 1.20 hannken { "blocksize", MTSETBSIZ, 1, 0 },
82 1.20 hannken { "bsf", MTBSF, 1, 1 },
83 1.20 hannken { "bsr", MTBSR, 1, 1 },
84 1.20 hannken { "density", MTSETDNSTY, 1, 0 },
85 1.20 hannken { "eof", MTWEOF, 0, 1 },
86 1.20 hannken { "eom", MTEOM, 1, 0 },
87 1.20 hannken { "erase", MTERASE, 0, 0 },
88 1.20 hannken { "fsf", MTFSF, 1, 1 },
89 1.20 hannken { "fsr", MTFSR, 1, 1 },
90 1.20 hannken { "offline", MTOFFL, 1, 0 },
91 1.20 hannken { "rewind", MTREW, 1, 0 },
92 1.20 hannken { "rewoffl", MTOFFL, 1, 0 },
93 1.20 hannken { "status", MTNOP, 1, 0 },
94 1.20 hannken { "retension", MTRETEN, 1, 0 },
95 1.20 hannken { "weof", MTWEOF, 0, 1 },
96 1.22 mjacob { "compress", MTCMPRESS, 1, 0 },
97 1.5 cgd { NULL }
98 1.1 cgd };
99 1.1 cgd
100 1.5 cgd void printreg __P((char *, u_int, char *));
101 1.5 cgd void status __P((struct mtget *));
102 1.5 cgd void usage __P((void));
103 1.21 christos int main __P((int, char *[]));
104 1.1 cgd
105 1.3 jtc int
106 1.1 cgd main(argc, argv)
107 1.3 jtc int argc;
108 1.5 cgd char *argv[];
109 1.1 cgd {
110 1.22 mjacob const struct commands *comp = (const struct commands *) NULL;
111 1.5 cgd struct mtget mt_status;
112 1.5 cgd struct mtop mt_com;
113 1.12 scottr int ch, len, mtfd, flags;
114 1.5 cgd char *p, *tape;
115 1.22 mjacob int count, spcl;
116 1.1 cgd
117 1.5 cgd if ((tape = getenv("TAPE")) == NULL)
118 1.19 lukem tape = _PATH_DEFTAPE;
119 1.5 cgd
120 1.5 cgd while ((ch = getopt(argc, argv, "f:t:")) != -1)
121 1.5 cgd switch (ch) {
122 1.3 jtc case 'f':
123 1.3 jtc case 't':
124 1.3 jtc tape = optarg;
125 1.3 jtc break;
126 1.5 cgd case '?':
127 1.3 jtc default:
128 1.3 jtc usage();
129 1.3 jtc }
130 1.3 jtc argc -= optind;
131 1.3 jtc argv += optind;
132 1.3 jtc
133 1.5 cgd if (argc < 1 || argc > 2)
134 1.3 jtc usage();
135 1.3 jtc
136 1.22 mjacob if (strcmp(*argv, "rdspos") == 0) {
137 1.22 mjacob count = 0;
138 1.22 mjacob spcl = MTIOCRDSPOS;
139 1.22 mjacob flags = O_RDONLY;
140 1.22 mjacob } else if (strcmp(*argv, "rdhpos") == 0) {
141 1.22 mjacob count = 0;
142 1.22 mjacob spcl = MTIOCRDHPOS;
143 1.22 mjacob flags = O_RDONLY;
144 1.22 mjacob } else if (strcmp(*argv, "setspos") == 0) {
145 1.22 mjacob spcl = MTIOCSLOCATE;
146 1.22 mjacob flags = O_RDONLY;
147 1.22 mjacob
148 1.22 mjacob ++argv;
149 1.22 mjacob if ((p = *argv) != NULL) {
150 1.22 mjacob count = strtol(*argv, &p, 0);
151 1.22 mjacob if (p == *argv) {
152 1.22 mjacob fprintf(stderr, "%s: illegal block address\n", p);
153 1.22 mjacob exit(1);
154 1.22 mjacob }
155 1.22 mjacob } else {
156 1.22 mjacob fprintf(stderr, "MTIOSLOCATE: missing block address\n");
157 1.22 mjacob exit(1);
158 1.22 mjacob }
159 1.22 mjacob } else if (strcmp(*argv, "sethpos") == 0) {
160 1.22 mjacob spcl = MTIOCHLOCATE;
161 1.22 mjacob flags = O_RDONLY;
162 1.22 mjacob
163 1.22 mjacob ++argv;
164 1.22 mjacob if ((p = *argv) != NULL) {
165 1.22 mjacob count = strtol(*argv, &p, 0);
166 1.22 mjacob if (p == *argv) {
167 1.22 mjacob fprintf(stderr, "%s: illegal block address\n", p);
168 1.22 mjacob exit(1);
169 1.22 mjacob }
170 1.22 mjacob } else {
171 1.22 mjacob fprintf(stderr, "MTIOHLOCATE: missing block address\n");
172 1.22 mjacob exit(1);
173 1.22 mjacob }
174 1.22 mjacob } else {
175 1.22 mjacob
176 1.22 mjacob len = strlen(p = *argv++);
177 1.22 mjacob for (comp = com;; comp++) {
178 1.22 mjacob if (comp->c_name == NULL)
179 1.22 mjacob errx(1, "%s: unknown command", p);
180 1.22 mjacob if (strncmp(p, comp->c_name, len) == 0)
181 1.22 mjacob break;
182 1.22 mjacob }
183 1.12 scottr
184 1.22 mjacob if (*argv) {
185 1.22 mjacob count = strtol(*argv, &p, 10);
186 1.22 mjacob if (count < comp->c_mincount || *p)
187 1.22 mjacob errx(2, "%s: illegal count", *argv);
188 1.22 mjacob } else
189 1.22 mjacob count = 1;
190 1.22 mjacob flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
191 1.22 mjacob spcl = 0;
192 1.20 hannken }
193 1.20 hannken
194 1.17 jtc if ((mtfd = open(tape, flags)) < 0)
195 1.7 pk err(2, "%s", tape);
196 1.16 jtc
197 1.22 mjacob if (spcl) {
198 1.22 mjacob int doset = (count != 0);
199 1.22 mjacob if (ioctl(mtfd, spcl, (caddr_t) &count) < 0) {
200 1.22 mjacob err(2, "%s", tape);
201 1.22 mjacob } else if (doset) {
202 1.22 mjacob printf("%s: block location set to %u\n", tape,
203 1.22 mjacob (unsigned int) count);
204 1.22 mjacob } else {
205 1.22 mjacob printf("%s: block location %u\n", tape,
206 1.22 mjacob (unsigned int) count);
207 1.22 mjacob }
208 1.22 mjacob } else if (comp->c_code == MTASF) {
209 1.16 jtc /* If mtget.mt_fileno was implemented, We could
210 1.16 jtc compute the minimal seek needed to position
211 1.16 jtc the tape. Until then, rewind and seek from
212 1.16 jtc begining-of-tape */
213 1.16 jtc
214 1.16 jtc mt_com.mt_op = MTREW;
215 1.16 jtc mt_com.mt_count = 1;
216 1.17 jtc if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
217 1.16 jtc err(2, "%s", tape);
218 1.16 jtc
219 1.16 jtc mt_com.mt_op = MTFSF;
220 1.16 jtc mt_com.mt_count = count;
221 1.17 jtc if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
222 1.16 jtc err(2, "%s", tape);
223 1.16 jtc
224 1.16 jtc } else if (comp->c_code != MTNOP) {
225 1.16 jtc mt_com.mt_op = comp->c_code;
226 1.16 jtc mt_com.mt_count = count;
227 1.16 jtc
228 1.17 jtc if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
229 1.7 pk err(2, "%s: %s", tape, comp->c_name);
230 1.16 jtc
231 1.1 cgd } else {
232 1.17 jtc if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
233 1.17 jtc err(2, "%s: %s", tape, comp->c_name);
234 1.17 jtc status(&mt_status);
235 1.1 cgd }
236 1.9 scottr
237 1.17 jtc exit(0);
238 1.5 cgd /* NOTREACHED */
239 1.1 cgd }
240 1.1 cgd
241 1.1 cgd #ifdef sun
242 1.1 cgd #include <sundev/tmreg.h>
243 1.1 cgd #include <sundev/arreg.h>
244 1.1 cgd #endif
245 1.1 cgd
246 1.1 cgd #ifdef tahoe
247 1.1 cgd #include <tahoe/vba/cyreg.h>
248 1.1 cgd #endif
249 1.1 cgd
250 1.1 cgd struct tape_desc {
251 1.1 cgd short t_type; /* type of magtape device */
252 1.1 cgd char *t_name; /* printing name */
253 1.1 cgd char *t_dsbits; /* "drive status" register */
254 1.1 cgd char *t_erbits; /* "error" register */
255 1.1 cgd } tapes[] = {
256 1.1 cgd #ifdef sun
257 1.1 cgd { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
258 1.1 cgd { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
259 1.1 cgd #endif
260 1.1 cgd #ifdef tahoe
261 1.1 cgd { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS },
262 1.1 cgd #endif
263 1.15 mrg { 0x7, "SCSI", "76543210", "76543210" },
264 1.1 cgd { 0 }
265 1.1 cgd };
266 1.1 cgd
267 1.1 cgd /*
268 1.1 cgd * Interpret the status buffer returned
269 1.1 cgd */
270 1.5 cgd void
271 1.1 cgd status(bp)
272 1.18 tls struct mtget *bp;
273 1.1 cgd {
274 1.18 tls struct tape_desc *mt;
275 1.1 cgd
276 1.5 cgd for (mt = tapes;; mt++) {
277 1.5 cgd if (mt->t_type == 0) {
278 1.5 cgd (void)printf("%d: unknown tape drive type\n",
279 1.5 cgd bp->mt_type);
280 1.5 cgd return;
281 1.5 cgd }
282 1.1 cgd if (mt->t_type == bp->mt_type)
283 1.1 cgd break;
284 1.1 cgd }
285 1.5 cgd (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
286 1.1 cgd printreg("ds", bp->mt_dsreg, mt->t_dsbits);
287 1.1 cgd printreg("\ner", bp->mt_erreg, mt->t_erbits);
288 1.5 cgd (void)putchar('\n');
289 1.21 christos (void)printf("blocksize: %d (%d, %d, %d, %d)\n",
290 1.14 mrg bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
291 1.14 mrg bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
292 1.21 christos (void)printf("density: %d (%d, %d, %d, %d)\n",
293 1.14 mrg bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
294 1.14 mrg bp->mt_mdensity[2], bp->mt_mdensity[3]);
295 1.1 cgd }
296 1.1 cgd
297 1.1 cgd /*
298 1.5 cgd * Print a register a la the %b format of the kernel's printf.
299 1.1 cgd */
300 1.5 cgd void
301 1.1 cgd printreg(s, v, bits)
302 1.1 cgd char *s;
303 1.18 tls u_int v;
304 1.18 tls char *bits;
305 1.1 cgd {
306 1.18 tls int i, any = 0;
307 1.18 tls char c;
308 1.1 cgd
309 1.1 cgd if (bits && *bits == 8)
310 1.1 cgd printf("%s=%o", s, v);
311 1.1 cgd else
312 1.1 cgd printf("%s=%x", s, v);
313 1.1 cgd bits++;
314 1.1 cgd if (v && bits) {
315 1.1 cgd putchar('<');
316 1.11 scottr while ((i = *bits++)) {
317 1.1 cgd if (v & (1 << (i-1))) {
318 1.1 cgd if (any)
319 1.1 cgd putchar(',');
320 1.1 cgd any = 1;
321 1.1 cgd for (; (c = *bits) > 32; bits++)
322 1.1 cgd putchar(c);
323 1.1 cgd } else
324 1.1 cgd for (; *bits > 32; bits++)
325 1.1 cgd ;
326 1.1 cgd }
327 1.1 cgd putchar('>');
328 1.1 cgd }
329 1.3 jtc }
330 1.3 jtc
331 1.5 cgd void
332 1.3 jtc usage()
333 1.3 jtc {
334 1.5 cgd (void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
335 1.17 jtc exit(1);
336 1.1 cgd }
337