mt.c revision 11608
117683Spst/*
239291Sfenner * Copyright (c) 1980, 1993
317683Spst *	The Regents of the University of California.  All rights reserved.
417683Spst *
517683Spst * Redistribution and use in source and binary forms, with or without
617683Spst * modification, are permitted provided that the following conditions
717683Spst * are met:
817683Spst * 1. Redistributions of source code must retain the above copyright
917683Spst *    notice, this list of conditions and the following disclaimer.
1017683Spst * 2. Redistributions in binary form must reproduce the above copyright
1117683Spst *    notice, this list of conditions and the following disclaimer in the
1217683Spst *    documentation and/or other materials provided with the distribution.
1317683Spst * 3. All advertising materials mentioning features or use of this software
1417683Spst *    must display the following acknowledgement:
1517683Spst *	This product includes software developed by the University of
1617683Spst *	California, Berkeley and its contributors.
1717683Spst * 4. Neither the name of the University nor the names of its contributors
1817683Spst *    may be used to endorse or promote products derived from this software
1917683Spst *    without specific prior written permission.
20162020Ssam *
21162020Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2217683Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2317683Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24127664Sbms * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25172680Smlaier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2617683Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2717683Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2875107Sfenner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2975107Sfenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3075107Sfenner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3175107Sfenner * SUCH DAMAGE.
3217683Spst */
33183102Scsjp
3417683Spst#ifndef lint
3517683Spststatic char copyright[] =
3617683Spst"@(#) Copyright (c) 1980, 1993\n\
3717683Spst	The Regents of the University of California.  All rights reserved.\n";
3817683Spst#endif /* not lint */
39127664Sbms
4017683Spst#ifndef lint
4117683Spststatic char sccsid[] = "@(#)mt.c	8.1 (Berkeley) 6/6/93";
42127664Sbms#endif /* not lint */
4398530Sfenner
44127664Sbms/*
4598530Sfenner * mt --
46127664Sbms *   magnetic tape manipulation program
47127664Sbms */
4898530Sfenner#include <sys/types.h>
49127664Sbms#include <sys/ioctl.h>
50127664Sbms#include <sys/mtio.h>
51127664Sbms#include <fcntl.h>
52127664Sbms#include <errno.h>
53127664Sbms#include <stdlib.h>
54127664Sbms#include <stdio.h>
55127664Sbms#include <ctype.h>
56127664Sbms#include <string.h>
57127664Sbms
58127664Sbms/* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
59127664Sbms#if defined(__FreeBSD__)
60127664Sbms/* c_flags */
61127664Sbms#define NEED_2ARGS	0x01
6298530Sfenner#define ZERO_ALLOWED	0x02
63127664Sbms#define IS_DENSITY	0x04
64127664Sbms#define DISABLE_THIS	0x08
65147894Ssam#endif /* defined(__FreeBSD__) */
66127664Sbms
6717683Spststruct commands {
68127664Sbms	char *c_name;
69127664Sbms	int c_code;
70127664Sbms	int c_ronly;
71127664Sbms#if defined(__FreeBSD__)
72127664Sbms	int c_flags;
73127664Sbms#endif /* defined(__FreeBSD__) */
74127664Sbms} com[] = {
75127664Sbms	{ "bsf",	MTBSF,	1 },
76127664Sbms	{ "bsr",	MTBSR,	1 },
77127664Sbms#if defined(__FreeBSD__)
78127664Sbms	/* XXX FreeBSD considered "eof" dangerous, since it's being
79127664Sbms	   confused with "eom" (and is an alias for "weof" anyway) */
80127664Sbms	{ "eof",	MTWEOF, 0, DISABLE_THIS },
81127664Sbms#else
82127664Sbms	{ "eof",	MTWEOF, 0 },
83127664Sbms#endif
84127664Sbms	{ "fsf",	MTFSF,	1 },
85127664Sbms	{ "fsr",	MTFSR,	1 },
86127664Sbms	{ "offline",	MTOFFL,	1 },
87127664Sbms	{ "rewind",	MTREW,	1 },
88127664Sbms	{ "rewoffl",	MTOFFL,	1 },
89127664Sbms	{ "status",	MTNOP,	1 },
90183102Scsjp	{ "weof",	MTWEOF,	0 },
91183102Scsjp#if defined(__FreeBSD__)
92183102Scsjp	{ "erase",	MTERASE, 0 },
93183102Scsjp	{ "blocksize",	MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
9417683Spst	{ "density",	MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
9517683Spst	{ "eom",	MTEOD, 1 },
9617683Spst	{ "comp",	MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED },
9717683Spst#endif /* defined(__FreeBSD__) */
9817683Spst	{ NULL }
9917683Spst};
10017683Spst
10117683Spstvoid err __P((const char *, ...));
10217683Spstvoid printreg __P((char *, u_int, char *));
10317683Spstvoid status __P((struct mtget *));
104127664Sbmsvoid usage __P((void));
105127664Sbms#if defined (__FreeBSD__)
106127664Sbmsvoid st_status (struct mtget *);
107127664Sbmsint stringtodens (const char *s);
10817683Spstconst char *denstostring (int d);
10917683Spstvoid warn_eof __P((void));
11017683Spst#endif /* defined (__FreeBSD__) */
11117683Spst
112127664Sbmsint
11356889Sfennermain(argc, argv)
114127664Sbms	int argc;
115162012Ssam	char *argv[];
116127664Sbms{
117127664Sbms	register struct commands *comp;
118127664Sbms	struct mtget mt_status;
119127664Sbms	struct mtop mt_com;
12017683Spst	int ch, len, mtfd;
12117683Spst	char *p, *tape;
12217683Spst
12398530Sfenner	if ((tape = getenv("TAPE")) == NULL)
12498530Sfenner		tape = DEFTAPE;
12598530Sfenner
12698530Sfenner	while ((ch = getopt(argc, argv, "f:t:")) != EOF)
12798530Sfenner		switch(ch) {
12898530Sfenner		case 'f':
12998530Sfenner		case 't':
13098530Sfenner			tape = optarg;
13198530Sfenner			break;
13298530Sfenner		case '?':
13398530Sfenner		default:
13498530Sfenner			usage();
13598530Sfenner		}
13617683Spst	argc -= optind;
13775107Sfenner	argv += optind;
13875107Sfenner
13917683Spst	if (argc < 1 || argc > 2)
14017683Spst		usage();
14117683Spst
14217683Spst	len = strlen(p = *argv++);
14317683Spst	for (comp = com;; comp++) {
14417683Spst		if (comp->c_name == NULL)
14517683Spst			err("%s: unknown command", p);
14617683Spst		if (strncmp(p, comp->c_name, len) == 0)
147183102Scsjp			break;
148183102Scsjp	}
149183102Scsjp#if defined(__FreeBSD__)
150183102Scsjp	if((comp->c_flags & NEED_2ARGS) && argc != 2)
151183102Scsjp		usage();
152183102Scsjp	if(comp->c_flags & DISABLE_THIS) {
153183102Scsjp		warn_eof();
154183102Scsjp	}
155183102Scsjp#endif /* defined(__FreeBSD__) */
156183102Scsjp	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
157127664Sbms		err("%s: %s", tape, strerror(errno));
158183102Scsjp	if (comp->c_code != MTNOP) {
159183102Scsjp		mt_com.mt_op = comp->c_code;
160183102Scsjp		if (*argv) {
161183102Scsjp#if defined (__FreeBSD__)
162183102Scsjp			if (!isdigit(**argv) &&
163183102Scsjp			    comp->c_flags & IS_DENSITY) {
164183102Scsjp				const char *dcanon;
165183102Scsjp				mt_com.mt_count = stringtodens(*argv);
166183102Scsjp				if (mt_com.mt_count == 0)
167183102Scsjp					err("%s: unknown density", *argv);
168183102Scsjp				dcanon = denstostring(mt_com.mt_count);
169183102Scsjp				if (strcmp(dcanon, *argv) != 0)
170183102Scsjp					printf(
171183102Scsjp					"Using \"%s\" as an alias for %s\n",
172183102Scsjp					       *argv, dcanon);
173183102Scsjp				p = "";
174183102Scsjp			} else
175183102Scsjp				/* allow for hex numbers; useful for density */
176183102Scsjp				mt_com.mt_count = strtol(*argv, &p, 0);
177183102Scsjp#else
178183102Scsjp			mt_com.mt_count = strtol(*argv, &p, 10);
179183102Scsjp#endif /* defined(__FreeBSD__) */
180183102Scsjp			if (mt_com.mt_count <=
181183102Scsjp#if defined (__FreeBSD__)
182183102Scsjp			    ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
183183102Scsjp#else
184183102Scsjp			    0
185183102Scsjp#endif /* defined (__FreeBSD__) */
186183102Scsjp			    || *p)
187183102Scsjp				err("%s: illegal count", *argv);
188183102Scsjp		}
189183102Scsjp		else
190183102Scsjp			mt_com.mt_count = 1;
191183102Scsjp		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
192183102Scsjp			err("%s: %s: %s", tape, comp->c_name, strerror(errno));
193183102Scsjp	} else {
194183102Scsjp		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
195183102Scsjp			err("%s", strerror(errno));
196183102Scsjp		status(&mt_status);
197183102Scsjp	}
198183102Scsjp	exit (0);
199183102Scsjp	/* NOTREACHED */
200183102Scsjp}
201183102Scsjp
202183102Scsjp#ifdef vax
203183102Scsjp#include <vax/mba/mtreg.h>
204183102Scsjp#include <vax/mba/htreg.h>
205183102Scsjp
206183102Scsjp#include <vax/uba/utreg.h>
207183102Scsjp#include <vax/uba/tmreg.h>
208183102Scsjp#undef b_repcnt		/* argh */
209183102Scsjp#include <vax/uba/tsreg.h>
210183102Scsjp#endif
211183102Scsjp
212183102Scsjp#ifdef sun
213183102Scsjp#include <sundev/tmreg.h>
214183102Scsjp#include <sundev/arreg.h>
215183102Scsjp#endif
216183102Scsjp
217183102Scsjp#ifdef tahoe
218183102Scsjp#include <tahoe/vba/cyreg.h>
219183102Scsjp#endif
220183102Scsjp
221183102Scsjpstruct tape_desc {
222183102Scsjp	short	t_type;		/* type of magtape device */
223183102Scsjp	char	*t_name;	/* printing name */
224183102Scsjp	char	*t_dsbits;	/* "drive status" register */
225183102Scsjp	char	*t_erbits;	/* "error" register */
226183102Scsjp} tapes[] = {
227183102Scsjp#ifdef vax
228183102Scsjp	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
229183102Scsjp	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
230183102Scsjp	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
231183102Scsjp	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
232183102Scsjp	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
233183102Scsjp#endif
234183102Scsjp#ifdef sun
235183102Scsjp	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
236183102Scsjp	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
237183102Scsjp#endif
238183102Scsjp#ifdef tahoe
239183102Scsjp	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
240183102Scsjp#endif
241183102Scsjp#if defined (__FreeBSD__)
242183102Scsjp	/*
243183102Scsjp	 * XXX This is terrific.  The st driver reports the tape drive
244183102Scsjp	 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
245183102Scsjp	 * either reports MT_ISVIPER1 for an Archive tape, or 0x11
246183102Scsjp	 * (MT_ISMFOUR) for other tapes.
247183102Scsjp	 * XXX for the wt driver, rely on it behaving like a "standard"
248183102Scsjp	 * magtape driver.
249183102Scsjp	 */
250183102Scsjp	{ MT_ISAR,	"SCSI tape drive", 0,		0 },
251183102Scsjp	{ MT_ISVIPER1,	"Archive Viper", 0,		0 },
252183102Scsjp	{ MT_ISMFOUR,	"Wangtek",	0,		0 },
253183102Scsjp#endif /* defined (__FreeBSD__) */
254183102Scsjp	{ 0 }
255183102Scsjp};
256183102Scsjp
257183102Scsjp/*
258183102Scsjp * Interpret the status buffer returned
259183102Scsjp */
260183102Scsjpvoid
261183102Scsjpstatus(bp)
262183102Scsjp	register struct mtget *bp;
263183102Scsjp{
264183102Scsjp	register struct tape_desc *mt;
265183102Scsjp
266183102Scsjp	for (mt = tapes;; mt++) {
267183102Scsjp		if (mt->t_type == 0) {
268183102Scsjp			(void)printf("%d: unknown tape drive type\n",
269183102Scsjp			    bp->mt_type);
270183102Scsjp			return;
271183102Scsjp		}
272183102Scsjp		if (mt->t_type == bp->mt_type)
273183102Scsjp			break;
274183102Scsjp	}
275183102Scsjp#if defined (__FreeBSD__)
276183102Scsjp	if(mt->t_type == MT_ISAR)
277183102Scsjp		st_status(bp);
278183102Scsjp	else {
279183102Scsjp#endif /* defined (__FreeBSD__) */
280183102Scsjp	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
281183102Scsjp	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
282183102Scsjp	printreg("\ner", bp->mt_erreg, mt->t_erbits);
283183102Scsjp	(void)putchar('\n');
284183102Scsjp#if defined (__FreeBSD__)
285183102Scsjp	}
286183102Scsjp#endif /* defined (__FreeBSD__) */
287183102Scsjp}
288183102Scsjp
289183102Scsjp/*
290183102Scsjp * Print a register a la the %b format of the kernel's printf.
291183102Scsjp */
292183102Scsjpvoid
293183102Scsjpprintreg(s, v, bits)
294183102Scsjp	char *s;
295183102Scsjp	register u_int v;
296183102Scsjp	register char *bits;
297183102Scsjp{
298183102Scsjp	register int i, any = 0;
299183102Scsjp	register char c;
300183102Scsjp
301127664Sbms	if (bits && *bits == 8)
30217683Spst		printf("%s=%o", s, v);
30317683Spst	else
30417683Spst		printf("%s=%x", s, v);
30517683Spst	if (!bits)
306146768Ssam		return;
307127664Sbms	bits++;
308183102Scsjp	if (v && bits) {
309183102Scsjp		putchar('<');
310183102Scsjp		while (i = *bits++) {
311146768Ssam			if (v & (1 << (i-1))) {
312146768Ssam				if (any)
313146768Ssam					putchar(',');
31417683Spst				any = 1;
315127664Sbms				for (; (c = *bits) > 32; bits++)
31617683Spst					putchar(c);
317127664Sbms			} else
318127664Sbms				for (; *bits > 32; bits++)
319127664Sbms					;
320127664Sbms		}
321127664Sbms		putchar('>');
322127664Sbms	}
323127664Sbms}
324127664Sbms
325127664Sbmsvoid
326127664Sbmsusage()
327127664Sbms{
328127664Sbms	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
32917683Spst	exit(1);
33017683Spst}
331183102Scsjp
332183102Scsjp#if __STDC__
333183102Scsjp#include <stdarg.h>
334183102Scsjp#else
335183102Scsjp#include <varargs.h>
336183102Scsjp#endif
337183102Scsjp
338183102Scsjpvoid
339183102Scsjp#if __STDC__
340183102Scsjperr(const char *fmt, ...)
341183102Scsjp#else
342183102Scsjperr(fmt, va_alist)
343183102Scsjp	char *fmt;
344183102Scsjp        va_dcl
345183102Scsjp#endif
346183102Scsjp{
347183102Scsjp	va_list ap;
348183102Scsjp#if __STDC__
349183102Scsjp	va_start(ap, fmt);
350183102Scsjp#else
351183102Scsjp	va_start(ap);
35217683Spst#endif
35317683Spst	(void)fprintf(stderr, "mt: ");
35417683Spst	(void)vfprintf(stderr, fmt, ap);
35517683Spst	va_end(ap);
35617683Spst	(void)fprintf(stderr, "\n");
35717683Spst	exit(1);
35817683Spst	/* NOTREACHED */
359127664Sbms}
360127664Sbms
361127664Sbms#if defined (__FreeBSD__)
362127664Sbms
363127664Sbmsstruct densities {
364127664Sbms	int dens;
365127664Sbms	const char *name;
366127664Sbms} dens [] = {
367127664Sbms	{ 0x1,  "X3.22-1983" },
368127664Sbms	{ 0x2,  "X3.39-1986" },
369127664Sbms	{ 0x3,  "X3.54-1986" },
370127664Sbms	{ 0x5,  "X3.136-1986" },
371127664Sbms	{ 0x6,  "X3.157-1987" },
372127664Sbms	{ 0x7,  "X3.116-1986" },
373127664Sbms	{ 0x8,  "X3.158-1986" },
374127664Sbms	{ 0x9,  "X3B5/87-099" },
375127664Sbms	{ 0xA,  "X3B5/86-199" },
376127664Sbms	{ 0xB,  "X3.56-1986" },
377127664Sbms	{ 0xC,  "HI-TC1" },
378127664Sbms	{ 0xD,  "HI-TC2" },
379127664Sbms	{ 0xF,  "QIC-120" },
380127664Sbms	{ 0x10, "QIC-150" },
381127664Sbms	{ 0x11, "QIC-320" },
382127664Sbms	{ 0x12, "QIC-1350" },
383127664Sbms	{ 0x13, "X3B5/88-185A" },
384127664Sbms	{ 0x14, "X3.202-1991" },
38517683Spst	{ 0x15, "ECMA TC17" },
38617683Spst	{ 0x16, "X3.193-1990" },
38717683Spst	{ 0x17, "X3B5/91-174" },
38817683Spst	{ 0, 0 }
38917683Spst};
39017683Spst
39117683Spstconst char *
39217683Spstdenstostring(int d)
39317683Spst{
39417683Spst	static char buf[20];
39517683Spst	struct densities *sd;
39617683Spst
39717683Spst	for (sd = dens; sd->dens; sd++)
39817683Spst		if (sd->dens == d)
39917683Spst			break;
40017683Spst	if (sd->dens == 0) {
40117683Spst		sprintf(buf, "0x%02x", d);
40275107Sfenner		return buf;
40375107Sfenner	} else
40417683Spst		return sd->name;
40517683Spst}
40617683Spst
40717683Spstint
40817683Spststringtodens(const char *s)
40917683Spst{
41017683Spst	struct densities *sd;
41117683Spst	size_t l = strlen(s);
41217683Spst
41317683Spst	for (sd = dens; sd->dens; sd++)
41417683Spst		if (strncasecmp(sd->name, s, l) == 0)
415146768Ssam			break;
416146768Ssam	return sd->dens;
417146768Ssam}
41817683Spst
41917683Spst
420127664Sbmsconst char *
421127664Sbmsgetblksiz(int bs)
422127664Sbms{
423127664Sbms	static char buf[25];
424127664Sbms	if (bs == 0)
425127664Sbms		return "variable";
426127664Sbms	else {
427127664Sbms		sprintf(buf, "= %d bytes", bs);
428127664Sbms		return buf;
429127664Sbms	}
430127664Sbms}
431127664Sbms
432127664Sbms
433127664Sbmsvoid
434127664Sbmsst_status(struct mtget *bp)
435127664Sbms{
436127664Sbms	printf("Present Mode:   Density = %-12s Blocksize %s\n",
437127664Sbms	       denstostring(bp->mt_density), getblksiz(bp->mt_blksiz));
438127664Sbms	printf("---------available modes---------\n");
439127664Sbms	printf("Mode 0:         Density = %-12s Blocksize %s\n",
440127664Sbms	       denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0));
44117683Spst	printf("Mode 1:         Density = %-12s Blocksize %s\n",
44217683Spst	       denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1));
443146768Ssam	printf("Mode 2:         Density = %-12s Blocksize %s\n",
44417683Spst	       denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2));
445127664Sbms	printf("Mode 3:         Density = %-12s Blocksize %s\n",
446127664Sbms	       denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3));
447146768Ssam}
448146768Ssam
449146768Ssamvoid
450146768Ssamwarn_eof(void)
451146768Ssam{
452146768Ssam	fprintf(stderr,
453146768Ssam		"The \"eof\" command has been disabled.\n"
454146768Ssam		"Use \"weof\" if you really want to write end-of-file marks,\n"
45517683Spst		"or \"eom\" if you rather want to skip to the end of "
456127664Sbms		"recorded medium.\n");
457146768Ssam	exit(1);
458146768Ssam}
459146768Ssam
460146768Ssam#endif /* defined (__FreeBSD__) */
46198530Sfenner