xref: /freebsd/usr.bin/mt/mt.c (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)mt.c	8.1 (Berkeley) 6/6/93";
42 #endif /* not lint */
43 
44 /*
45  * mt --
46  *   magnetic tape manipulation program
47  */
48 #include <sys/types.h>
49 #include <sys/ioctl.h>
50 #include <sys/mtio.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <ctype.h>
56 #include <string.h>
57 
58 /* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
59 #if defined(__FreeBSD__)
60 /* c_flags */
61 #define NEED_2ARGS	0x01
62 #define ZERO_ALLOWED	0x02
63 #define IS_DENSITY	0x04
64 #endif /* defined(__FreeBSD__) */
65 
66 struct commands {
67 	char *c_name;
68 	int c_code;
69 	int c_ronly;
70 #if defined(__FreeBSD__)
71 	int c_flags;
72 #endif /* defined(__FreeBSD__) */
73 } com[] = {
74 	{ "bsf",	MTBSF,	1 },
75 	{ "bsr",	MTBSR,	1 },
76 	{ "eof",	MTWEOF,	0 },
77 	{ "fsf",	MTFSF,	1 },
78 	{ "fsr",	MTFSR,	1 },
79 	{ "offline",	MTOFFL,	1 },
80 	{ "rewind",	MTREW,	1 },
81 	{ "rewoffl",	MTOFFL,	1 },
82 	{ "status",	MTNOP,	1 },
83 	{ "weof",	MTWEOF,	0 },
84 #if defined(__FreeBSD__)
85 	{ "erase",	MTERASE, 0 },
86 	{ "blocksize",	MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
87 	{ "density",	MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
88 	{ "eom",	MTEOD, 1 },
89 	{ "comp",	MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED },
90 #endif /* defined(__FreeBSD__) */
91 	{ NULL }
92 };
93 
94 void err __P((const char *, ...));
95 void printreg __P((char *, u_int, char *));
96 void status __P((struct mtget *));
97 void usage __P((void));
98 #if defined (__FreeBSD__)
99 void st_status (struct mtget *);
100 int stringtodens (const char *s);
101 const char *denstostring (int d);
102 #endif /* defined (__FreeBSD__) */
103 
104 int
105 main(argc, argv)
106 	int argc;
107 	char *argv[];
108 {
109 	register struct commands *comp;
110 	struct mtget mt_status;
111 	struct mtop mt_com;
112 	int ch, len, mtfd;
113 	char *p, *tape;
114 
115 	if ((tape = getenv("TAPE")) == NULL)
116 		tape = DEFTAPE;
117 
118 	while ((ch = getopt(argc, argv, "f:t:")) != EOF)
119 		switch(ch) {
120 		case 'f':
121 		case 't':
122 			tape = optarg;
123 			break;
124 		case '?':
125 		default:
126 			usage();
127 		}
128 	argc -= optind;
129 	argv += optind;
130 
131 	if (argc < 1 || argc > 2)
132 		usage();
133 
134 	len = strlen(p = *argv++);
135 	for (comp = com;; comp++) {
136 		if (comp->c_name == NULL)
137 			err("%s: unknown command", p);
138 		if (strncmp(p, comp->c_name, len) == 0)
139 			break;
140 	}
141 #if defined(__FreeBSD__)
142 	if((comp->c_flags & NEED_2ARGS) && argc != 2)
143 		usage();
144 #endif /* defined(__FreeBSD__) */
145 	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
146 		err("%s: %s", tape, strerror(errno));
147 	if (comp->c_code != MTNOP) {
148 		mt_com.mt_op = comp->c_code;
149 		if (*argv) {
150 #if defined (__FreeBSD__)
151 			if (!isdigit(**argv) &&
152 			    comp->c_flags & IS_DENSITY) {
153 				const char *dcanon;
154 				mt_com.mt_count = stringtodens(*argv);
155 				if (mt_com.mt_count == 0)
156 					err("%s: unknown density", *argv);
157 				dcanon = denstostring(mt_com.mt_count);
158 				if (strcmp(dcanon, *argv) != 0)
159 					printf(
160 					"Using \"%s\" as an alias for %s\n",
161 					       *argv, dcanon);
162 				p = "";
163 			} else
164 				/* allow for hex numbers; useful for density */
165 				mt_com.mt_count = strtol(*argv, &p, 0);
166 #else
167 			mt_com.mt_count = strtol(*argv, &p, 10);
168 #endif /* defined(__FreeBSD__) */
169 			if (mt_com.mt_count <=
170 #if defined (__FreeBSD__)
171 			    ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
172 #else
173 			    0
174 #endif /* defined (__FreeBSD__) */
175 			    || *p)
176 				err("%s: illegal count", *argv);
177 		}
178 		else
179 			mt_com.mt_count = 1;
180 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
181 			err("%s: %s: %s", tape, comp->c_name, strerror(errno));
182 	} else {
183 		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
184 			err("%s", strerror(errno));
185 		status(&mt_status);
186 	}
187 	exit (0);
188 	/* NOTREACHED */
189 }
190 
191 #ifdef vax
192 #include <vax/mba/mtreg.h>
193 #include <vax/mba/htreg.h>
194 
195 #include <vax/uba/utreg.h>
196 #include <vax/uba/tmreg.h>
197 #undef b_repcnt		/* argh */
198 #include <vax/uba/tsreg.h>
199 #endif
200 
201 #ifdef sun
202 #include <sundev/tmreg.h>
203 #include <sundev/arreg.h>
204 #endif
205 
206 #ifdef tahoe
207 #include <tahoe/vba/cyreg.h>
208 #endif
209 
210 struct tape_desc {
211 	short	t_type;		/* type of magtape device */
212 	char	*t_name;	/* printing name */
213 	char	*t_dsbits;	/* "drive status" register */
214 	char	*t_erbits;	/* "error" register */
215 } tapes[] = {
216 #ifdef vax
217 	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
218 	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
219 	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
220 	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
221 	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
222 #endif
223 #ifdef sun
224 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
225 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
226 #endif
227 #ifdef tahoe
228 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
229 #endif
230 #if defined (__FreeBSD__)
231 	/*
232 	 * XXX This is terrific.  The st driver reports the tape drive
233 	 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
234 	 * either reports MT_ISVIPER1 for an Archive tape, or 0x11
235 	 * (MT_ISMFOUR) for other tapes.
236 	 * XXX for the wt driver, rely on it behaving like a "standard"
237 	 * magtape driver.
238 	 */
239 	{ MT_ISAR,	"SCSI tape drive", 0,		0 },
240 	{ MT_ISVIPER1,	"Archive Viper", 0,		0 },
241 	{ MT_ISMFOUR,	"Wangtek",	0,		0 },
242 #endif /* defined (__FreeBSD__) */
243 	{ 0 }
244 };
245 
246 /*
247  * Interpret the status buffer returned
248  */
249 void
250 status(bp)
251 	register struct mtget *bp;
252 {
253 	register struct tape_desc *mt;
254 
255 	for (mt = tapes;; mt++) {
256 		if (mt->t_type == 0) {
257 			(void)printf("%d: unknown tape drive type\n",
258 			    bp->mt_type);
259 			return;
260 		}
261 		if (mt->t_type == bp->mt_type)
262 			break;
263 	}
264 #if defined (__FreeBSD__)
265 	if(mt->t_type == MT_ISAR)
266 		st_status(bp);
267 	else {
268 #endif /* defined (__FreeBSD__) */
269 	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
270 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
271 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
272 	(void)putchar('\n');
273 #if defined (__FreeBSD__)
274 	}
275 #endif /* defined (__FreeBSD__) */
276 }
277 
278 /*
279  * Print a register a la the %b format of the kernel's printf.
280  */
281 void
282 printreg(s, v, bits)
283 	char *s;
284 	register u_int v;
285 	register char *bits;
286 {
287 	register int i, any = 0;
288 	register char c;
289 
290 	if (bits && *bits == 8)
291 		printf("%s=%o", s, v);
292 	else
293 		printf("%s=%x", s, v);
294 	bits++;
295 	if (v && bits) {
296 		putchar('<');
297 		while (i = *bits++) {
298 			if (v & (1 << (i-1))) {
299 				if (any)
300 					putchar(',');
301 				any = 1;
302 				for (; (c = *bits) > 32; bits++)
303 					putchar(c);
304 			} else
305 				for (; *bits > 32; bits++)
306 					;
307 		}
308 		putchar('>');
309 	}
310 }
311 
312 void
313 usage()
314 {
315 	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
316 	exit(1);
317 }
318 
319 #if __STDC__
320 #include <stdarg.h>
321 #else
322 #include <varargs.h>
323 #endif
324 
325 void
326 #if __STDC__
327 err(const char *fmt, ...)
328 #else
329 err(fmt, va_alist)
330 	char *fmt;
331         va_dcl
332 #endif
333 {
334 	va_list ap;
335 #if __STDC__
336 	va_start(ap, fmt);
337 #else
338 	va_start(ap);
339 #endif
340 	(void)fprintf(stderr, "mt: ");
341 	(void)vfprintf(stderr, fmt, ap);
342 	va_end(ap);
343 	(void)fprintf(stderr, "\n");
344 	exit(1);
345 	/* NOTREACHED */
346 }
347 
348 #if defined (__FreeBSD__)
349 
350 struct densities {
351 	int dens;
352 	const char *name;
353 } dens [] = {
354 	{ 0x1,  "X3.22-1983" },
355 	{ 0x2,  "X3.39-1986" },
356 	{ 0x3,  "X3.54-1986" },
357 	{ 0x5,  "X3.136-1986" },
358 	{ 0x6,  "X3.157-1987" },
359 	{ 0x7,  "X3.116-1986" },
360 	{ 0x8,  "X3.158-1986" },
361 	{ 0x9,  "X3B5/87-099" },
362 	{ 0xA,  "X3B5/86-199" },
363 	{ 0xB,  "X3.56-1986" },
364 	{ 0xC,  "HI-TC1" },
365 	{ 0xD,  "HI-TC2" },
366 	{ 0xF,  "QIC-120" },
367 	{ 0x10, "QIC-150" },
368 	{ 0x11, "QIC-320" },
369 	{ 0x12, "QIC-1350" },
370 	{ 0x13, "X3B5/88-185A" },
371 	{ 0x14, "X3.202-1991" },
372 	{ 0x15, "ECMA TC17" },
373 	{ 0x16, "X3.193-1990" },
374 	{ 0x17, "X3B5/91-174" },
375 	{ 0, 0 }
376 };
377 
378 const char *
379 denstostring(int d)
380 {
381 	static char buf[20];
382 	struct densities *sd;
383 
384 	for (sd = dens; sd->dens; sd++)
385 		if (sd->dens == d)
386 			break;
387 	if (sd->dens == 0) {
388 		sprintf(buf, "0x%02x", d);
389 		return buf;
390 	} else
391 		return sd->name;
392 }
393 
394 int
395 stringtodens(const char *s)
396 {
397 	struct densities *sd;
398 	size_t l = strlen(s);
399 
400 	for (sd = dens; sd->dens; sd++)
401 		if (strncasecmp(sd->name, s, l) == 0)
402 			break;
403 	return sd->dens;
404 }
405 
406 
407 const char *
408 getblksiz(int bs)
409 {
410 	static char buf[25];
411 	if (bs == 0)
412 		return "variable";
413 	else {
414 		sprintf(buf, "= %d bytes", bs);
415 		return buf;
416 	}
417 }
418 
419 
420 void
421 st_status(struct mtget *bp)
422 {
423 	printf("Present Mode:   Density = %-12s Blocksize %s\n",
424 	       denstostring(bp->mt_density), getblksiz(bp->mt_blksiz));
425 	printf("---------available modes---------\n");
426 	printf("Mode 0:         Density = %-12s Blocksize %s\n",
427 	       denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0));
428 	printf("Mode 1:         Density = %-12s Blocksize %s\n",
429 	       denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1));
430 	printf("Mode 2:         Density = %-12s Blocksize %s\n",
431 	       denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2));
432 	printf("Mode 3:         Density = %-12s Blocksize %s\n",
433 	       denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3));
434 }
435 
436 #endif /* defined (__FreeBSD__) */
437