mt.c revision 14176
1284345Ssjg/*
2284345Ssjg * Copyright (c) 1980, 1993
3284345Ssjg *	The Regents of the University of California.  All rights reserved.
4284345Ssjg *
5284345Ssjg * Redistribution and use in source and binary forms, with or without
6284345Ssjg * modification, are permitted provided that the following conditions
7284345Ssjg * are met:
8284345Ssjg * 1. Redistributions of source code must retain the above copyright
9284345Ssjg *    notice, this list of conditions and the following disclaimer.
10284345Ssjg * 2. Redistributions in binary form must reproduce the above copyright
11284345Ssjg *    notice, this list of conditions and the following disclaimer in the
12284345Ssjg *    documentation and/or other materials provided with the distribution.
13284345Ssjg * 3. All advertising materials mentioning features or use of this software
14284345Ssjg *    must display the following acknowledgement:
15284345Ssjg *	This product includes software developed by the University of
16284345Ssjg *	California, Berkeley and its contributors.
17284345Ssjg * 4. Neither the name of the University nor the names of its contributors
18284345Ssjg *    may be used to endorse or promote products derived from this software
19284345Ssjg *    without specific prior written permission.
20284345Ssjg *
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
35static 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
41static 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#define DISABLE_THIS	0x08
65#endif /* defined(__FreeBSD__) */
66
67struct commands {
68	char *c_name;
69	int c_code;
70	int c_ronly;
71#if defined(__FreeBSD__)
72	int c_flags;
73#endif /* defined(__FreeBSD__) */
74} com[] = {
75	{ "bsf",	MTBSF,	1 },
76	{ "bsr",	MTBSR,	1 },
77#if defined(__FreeBSD__)
78	/* XXX FreeBSD considered "eof" dangerous, since it's being
79	   confused with "eom" (and is an alias for "weof" anyway) */
80	{ "eof",	MTWEOF, 0, DISABLE_THIS },
81#else
82	{ "eof",	MTWEOF, 0 },
83#endif
84	{ "fsf",	MTFSF,	1 },
85	{ "fsr",	MTFSR,	1 },
86	{ "offline",	MTOFFL,	1 },
87	{ "rewind",	MTREW,	1 },
88	{ "rewoffl",	MTOFFL,	1 },
89	{ "status",	MTNOP,	1 },
90	{ "weof",	MTWEOF,	0 },
91#if defined(__FreeBSD__)
92	{ "erase",	MTERASE, 0 },
93	{ "blocksize",	MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
94	{ "density",	MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
95	{ "eom",	MTEOD, 1 },
96	{ "comp",	MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED },
97	{ "retension",	MTRETENS, 1 },
98#endif /* defined(__FreeBSD__) */
99	{ NULL }
100};
101
102void err __P((const char *, ...));
103void printreg __P((char *, u_int, char *));
104void status __P((struct mtget *));
105void usage __P((void));
106#if defined (__FreeBSD__)
107void st_status (struct mtget *);
108int stringtodens (const char *s);
109const char *denstostring (int d);
110void warn_eof __P((void));
111#endif /* defined (__FreeBSD__) */
112
113int
114main(argc, argv)
115	int argc;
116	char *argv[];
117{
118	register struct commands *comp;
119	struct mtget mt_status;
120	struct mtop mt_com;
121	int ch, len, mtfd;
122	char *p, *tape;
123
124	if ((tape = getenv("TAPE")) == NULL)
125		tape = DEFTAPE;
126
127	while ((ch = getopt(argc, argv, "f:t:")) != EOF)
128		switch(ch) {
129		case 'f':
130		case 't':
131			tape = optarg;
132			break;
133		case '?':
134		default:
135			usage();
136		}
137	argc -= optind;
138	argv += optind;
139
140	if (argc < 1 || argc > 2)
141		usage();
142
143	len = strlen(p = *argv++);
144	for (comp = com;; comp++) {
145		if (comp->c_name == NULL)
146			err("%s: unknown command", p);
147		if (strncmp(p, comp->c_name, len) == 0)
148			break;
149	}
150#if defined(__FreeBSD__)
151	if((comp->c_flags & NEED_2ARGS) && argc != 2)
152		usage();
153	if(comp->c_flags & DISABLE_THIS) {
154		warn_eof();
155	}
156#endif /* defined(__FreeBSD__) */
157	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
158		err("%s: %s", tape, strerror(errno));
159	if (comp->c_code != MTNOP) {
160		mt_com.mt_op = comp->c_code;
161		if (*argv) {
162#if defined (__FreeBSD__)
163			if (!isdigit(**argv) &&
164			    comp->c_flags & IS_DENSITY) {
165				const char *dcanon;
166				mt_com.mt_count = stringtodens(*argv);
167				if (mt_com.mt_count == 0)
168					err("%s: unknown density", *argv);
169				dcanon = denstostring(mt_com.mt_count);
170				if (strcmp(dcanon, *argv) != 0)
171					printf(
172					"Using \"%s\" as an alias for %s\n",
173					       *argv, dcanon);
174				p = "";
175			} else
176				/* allow for hex numbers; useful for density */
177				mt_com.mt_count = strtol(*argv, &p, 0);
178#else
179			mt_com.mt_count = strtol(*argv, &p, 10);
180#endif /* defined(__FreeBSD__) */
181			if (mt_com.mt_count <=
182#if defined (__FreeBSD__)
183			    ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
184#else
185			    0
186#endif /* defined (__FreeBSD__) */
187			    || *p)
188				err("%s: illegal count", *argv);
189		}
190		else
191			mt_com.mt_count = 1;
192		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
193			err("%s: %s: %s", tape, comp->c_name, strerror(errno));
194	} else {
195		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
196			err("%s", strerror(errno));
197		status(&mt_status);
198	}
199	exit (0);
200	/* NOTREACHED */
201}
202
203#ifdef vax
204#include <vax/mba/mtreg.h>
205#include <vax/mba/htreg.h>
206
207#include <vax/uba/utreg.h>
208#include <vax/uba/tmreg.h>
209#undef b_repcnt		/* argh */
210#include <vax/uba/tsreg.h>
211#endif
212
213#ifdef sun
214#include <sundev/tmreg.h>
215#include <sundev/arreg.h>
216#endif
217
218#ifdef tahoe
219#include <tahoe/vba/cyreg.h>
220#endif
221
222#ifdef __FreeBSD__
223#include <machine/wtio.h>
224#endif
225
226struct tape_desc {
227	short	t_type;		/* type of magtape device */
228	char	*t_name;	/* printing name */
229	char	*t_dsbits;	/* "drive status" register */
230	char	*t_erbits;	/* "error" register */
231} tapes[] = {
232#ifdef vax
233	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
234	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
235	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
236	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
237	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
238#endif
239#ifdef sun
240	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
241	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
242#endif
243#ifdef tahoe
244	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
245#endif
246#if defined (__FreeBSD__)
247	/*
248	 * XXX This is weird.  The st driver reports the tape drive
249	 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
250	 * either reports MT_ISVIPER1 for an Archive tape, or 0x11
251	 * (MT_ISMFOUR) for other tapes.
252	 * XXX for the wt driver, rely on it behaving like a "standard"
253	 * magtape driver.
254	 */
255	{ MT_ISAR,	"SCSI tape drive", 0,		0 },
256	{ MT_ISVIPER1,	"Archive Viper", WTDS_BITS, WTER_BITS },
257	{ MT_ISMFOUR,	"Wangtek",	 WTDS_BITS, WTER_BITS },
258#endif /* defined (__FreeBSD__) */
259	{ 0 }
260};
261
262/*
263 * Interpret the status buffer returned
264 */
265void
266status(bp)
267	register struct mtget *bp;
268{
269	register struct tape_desc *mt;
270
271	for (mt = tapes;; mt++) {
272		if (mt->t_type == 0) {
273			(void)printf("%d: unknown tape drive type\n",
274			    bp->mt_type);
275			return;
276		}
277		if (mt->t_type == bp->mt_type)
278			break;
279	}
280#if defined (__FreeBSD__)
281	if(mt->t_type == MT_ISAR)
282		st_status(bp);
283	else {
284#endif /* defined (__FreeBSD__) */
285	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
286	printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
287	printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
288	(void)putchar('\n');
289#if defined (__FreeBSD__)
290	}
291#endif /* defined (__FreeBSD__) */
292}
293
294/*
295 * Print a register a la the %b format of the kernel's printf.
296 */
297void
298printreg(s, v, bits)
299	char *s;
300	register u_int v;
301	register char *bits;
302{
303	register int i, any = 0;
304	register char c;
305
306	if (bits && *bits == 8)
307		printf("%s=%o", s, v);
308	else
309		printf("%s=%x", s, v);
310	if (!bits)
311		return;
312	bits++;
313	if (v && bits) {
314		putchar('<');
315		while (i = *bits++) {
316			if (v & (1 << (i-1))) {
317				if (any)
318					putchar(',');
319				any = 1;
320				for (; (c = *bits) > 32; bits++)
321					putchar(c);
322			} else
323				for (; *bits > 32; bits++)
324					;
325		}
326		putchar('>');
327	}
328}
329
330void
331usage()
332{
333	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
334	exit(1);
335}
336
337#if __STDC__
338#include <stdarg.h>
339#else
340#include <varargs.h>
341#endif
342
343void
344#if __STDC__
345err(const char *fmt, ...)
346#else
347err(fmt, va_alist)
348	char *fmt;
349        va_dcl
350#endif
351{
352	va_list ap;
353#if __STDC__
354	va_start(ap, fmt);
355#else
356	va_start(ap);
357#endif
358	(void)fprintf(stderr, "mt: ");
359	(void)vfprintf(stderr, fmt, ap);
360	va_end(ap);
361	(void)fprintf(stderr, "\n");
362	exit(1);
363	/* NOTREACHED */
364}
365
366#if defined (__FreeBSD__)
367
368struct densities {
369	int dens;
370	const char *name;
371} dens [] = {
372	{ 0x1,  "X3.22-1983" },
373	{ 0x2,  "X3.39-1986" },
374	{ 0x3,  "X3.54-1986" },
375	{ 0x5,  "X3.136-1986" },
376	{ 0x6,  "X3.157-1987" },
377	{ 0x7,  "X3.116-1986" },
378	{ 0x8,  "X3.158-1986" },
379	{ 0x9,  "X3B5/87-099" },
380	{ 0xA,  "X3B5/86-199" },
381	{ 0xB,  "X3.56-1986" },
382	{ 0xC,  "HI-TC1" },
383	{ 0xD,  "HI-TC2" },
384	{ 0xF,  "QIC-120" },
385	{ 0x10, "QIC-150" },
386	{ 0x11, "QIC-320" },
387	{ 0x12, "QIC-1350" },
388	{ 0x13, "X3B5/88-185A" },
389	{ 0x14, "X3.202-1991" },
390	{ 0x15, "ECMA TC17" },
391	{ 0x16, "X3.193-1990" },
392	{ 0x17, "X3B5/91-174" },
393	{ 0, 0 }
394};
395
396const char *
397denstostring(int d)
398{
399	static char buf[20];
400	struct densities *sd;
401
402	for (sd = dens; sd->dens; sd++)
403		if (sd->dens == d)
404			break;
405	if (sd->dens == 0) {
406		sprintf(buf, "0x%02x", d);
407		return buf;
408	} else
409		return sd->name;
410}
411
412int
413stringtodens(const char *s)
414{
415	struct densities *sd;
416	size_t l = strlen(s);
417
418	for (sd = dens; sd->dens; sd++)
419		if (strncasecmp(sd->name, s, l) == 0)
420			break;
421	return sd->dens;
422}
423
424
425const char *
426getblksiz(int bs)
427{
428	static char buf[25];
429	if (bs == 0)
430		return "variable";
431	else {
432		sprintf(buf, "= %d bytes", bs);
433		return buf;
434	}
435}
436
437
438void
439st_status(struct mtget *bp)
440{
441	printf("Present Mode:   Density = %-12s Blocksize %s\n",
442	       denstostring(bp->mt_density), getblksiz(bp->mt_blksiz));
443	printf("---------available modes---------\n");
444	printf("Mode 0:         Density = %-12s Blocksize %s\n",
445	       denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0));
446	printf("Mode 1:         Density = %-12s Blocksize %s\n",
447	       denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1));
448	printf("Mode 2:         Density = %-12s Blocksize %s\n",
449	       denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2));
450	printf("Mode 3:         Density = %-12s Blocksize %s\n",
451	       denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3));
452}
453
454void
455warn_eof(void)
456{
457	fprintf(stderr,
458		"The \"eof\" command has been disabled.\n"
459		"Use \"weof\" if you really want to write end-of-file marks,\n"
460		"or \"eom\" if you rather want to skip to the end of "
461		"recorded medium.\n");
462	exit(1);
463}
464
465#endif /* defined (__FreeBSD__) */
466