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