mt.c revision 39260
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1980, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3527752Scharnierstatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1980, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4127752Scharnier#if 0
4223693Speterstatic char sccsid[] = "@(#)mt.c	8.2 (Berkeley) 5/4/95";
4327752Scharnier#endif
4427752Scharnierstatic const char rcsid[] =
4535305Sphk	"$Id: mt.c,v 1.13 1997/08/21 05:49:29 joerg Exp $";
461590Srgrimes#endif /* not lint */
471590Srgrimes
481590Srgrimes/*
491590Srgrimes * mt --
501590Srgrimes *   magnetic tape manipulation program
511590Srgrimes */
521590Srgrimes#include <sys/types.h>
531590Srgrimes#include <sys/ioctl.h>
541590Srgrimes#include <sys/mtio.h>
5523693Speter
5623693Speter#include <ctype.h>
5727752Scharnier#include <err.h>
581590Srgrimes#include <fcntl.h>
5923693Speter#include <stdio.h>
601590Srgrimes#include <stdlib.h>
611590Srgrimes#include <string.h>
6223693Speter#include <unistd.h>
631590Srgrimes
647913Sjoerg/* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
657913Sjoerg#if defined(__FreeBSD__)
667913Sjoerg/* c_flags */
677913Sjoerg#define NEED_2ARGS	0x01
687929Sjoerg#define ZERO_ALLOWED	0x02
697929Sjoerg#define IS_DENSITY	0x04
709541Sjoerg#define DISABLE_THIS	0x08
7139260Sgibbs#define IS_COMP		0x10
727913Sjoerg#endif /* defined(__FreeBSD__) */
737913Sjoerg
7439260Sgibbs#ifndef TRUE
7539260Sgibbs#define TRUE 1
7639260Sgibbs#endif
7739260Sgibbs#ifndef FALSE
7839260Sgibbs#define FALSE 0
7939260Sgibbs#endif
8039260Sgibbs
811590Srgrimesstruct commands {
821590Srgrimes	char *c_name;
831590Srgrimes	int c_code;
841590Srgrimes	int c_ronly;
857913Sjoerg#if defined(__FreeBSD__)
867913Sjoerg	int c_flags;
877913Sjoerg#endif /* defined(__FreeBSD__) */
881590Srgrimes} com[] = {
891590Srgrimes	{ "bsf",	MTBSF,	1 },
901590Srgrimes	{ "bsr",	MTBSR,	1 },
919541Sjoerg#if defined(__FreeBSD__)
929541Sjoerg	/* XXX FreeBSD considered "eof" dangerous, since it's being
939541Sjoerg	   confused with "eom" (and is an alias for "weof" anyway) */
949541Sjoerg	{ "eof",	MTWEOF, 0, DISABLE_THIS },
959541Sjoerg#else
969541Sjoerg	{ "eof",	MTWEOF, 0 },
979541Sjoerg#endif
981590Srgrimes	{ "fsf",	MTFSF,	1 },
991590Srgrimes	{ "fsr",	MTFSR,	1 },
1001590Srgrimes	{ "offline",	MTOFFL,	1 },
1011590Srgrimes	{ "rewind",	MTREW,	1 },
1021590Srgrimes	{ "rewoffl",	MTOFFL,	1 },
1031590Srgrimes	{ "status",	MTNOP,	1 },
1041590Srgrimes	{ "weof",	MTWEOF,	0 },
1057913Sjoerg#if defined(__FreeBSD__)
10639260Sgibbs	{ "erase",	MTERASE, 0, ZERO_ALLOWED},
1077913Sjoerg	{ "blocksize",	MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
1087929Sjoerg	{ "density",	MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
1097913Sjoerg	{ "eom",	MTEOD, 1 },
11028492Sjoerg	{ "eod",	MTEOD, 1 },
11139260Sgibbs	{ "comp",	MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED|IS_COMP },
11213401Sjoerg	{ "retension",	MTRETENS, 1 },
1137913Sjoerg#endif /* defined(__FreeBSD__) */
1141590Srgrimes	{ NULL }
1151590Srgrimes};
1161590Srgrimes
1171590Srgrimesvoid printreg __P((char *, u_int, char *));
1181590Srgrimesvoid status __P((struct mtget *));
1191590Srgrimesvoid usage __P((void));
1207913Sjoerg#if defined (__FreeBSD__)
1217913Sjoergvoid st_status (struct mtget *);
1227929Sjoergint stringtodens (const char *s);
1237929Sjoergconst char *denstostring (int d);
12439260Sgibbsint denstobp(int d, int bpi);
12539260Sgibbsu_int32_t stringtocomp(const char *s);
12639260Sgibbsconst char * comptostring(u_int32_t comp);
1279541Sjoergvoid warn_eof __P((void));
1287913Sjoerg#endif /* defined (__FreeBSD__) */
1291590Srgrimes
1301590Srgrimesint
1311590Srgrimesmain(argc, argv)
1321590Srgrimes	int argc;
1331590Srgrimes	char *argv[];
1341590Srgrimes{
1351590Srgrimes	register struct commands *comp;
1361590Srgrimes	struct mtget mt_status;
1371590Srgrimes	struct mtop mt_com;
1381590Srgrimes	int ch, len, mtfd;
1391590Srgrimes	char *p, *tape;
1401590Srgrimes
1411590Srgrimes	if ((tape = getenv("TAPE")) == NULL)
1421590Srgrimes		tape = DEFTAPE;
1431590Srgrimes
14424360Simp	while ((ch = getopt(argc, argv, "f:t:")) != -1)
1451590Srgrimes		switch(ch) {
1461590Srgrimes		case 'f':
1471590Srgrimes		case 't':
1481590Srgrimes			tape = optarg;
1491590Srgrimes			break;
1501590Srgrimes		case '?':
1511590Srgrimes		default:
1521590Srgrimes			usage();
1531590Srgrimes		}
1541590Srgrimes	argc -= optind;
1551590Srgrimes	argv += optind;
1561590Srgrimes
1571590Srgrimes	if (argc < 1 || argc > 2)
1581590Srgrimes		usage();
1591590Srgrimes
1601590Srgrimes	len = strlen(p = *argv++);
1611590Srgrimes	for (comp = com;; comp++) {
1621590Srgrimes		if (comp->c_name == NULL)
16327752Scharnier			errx(1, "%s: unknown command", p);
1641590Srgrimes		if (strncmp(p, comp->c_name, len) == 0)
1651590Srgrimes			break;
1661590Srgrimes	}
1677913Sjoerg#if defined(__FreeBSD__)
1687913Sjoerg	if((comp->c_flags & NEED_2ARGS) && argc != 2)
1697913Sjoerg		usage();
1709541Sjoerg	if(comp->c_flags & DISABLE_THIS) {
1719541Sjoerg		warn_eof();
1729541Sjoerg	}
1737913Sjoerg#endif /* defined(__FreeBSD__) */
1741590Srgrimes	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
17527752Scharnier		err(1, "%s", tape);
1761590Srgrimes	if (comp->c_code != MTNOP) {
1771590Srgrimes		mt_com.mt_op = comp->c_code;
1781590Srgrimes		if (*argv) {
1797913Sjoerg#if defined (__FreeBSD__)
1807929Sjoerg			if (!isdigit(**argv) &&
1817929Sjoerg			    comp->c_flags & IS_DENSITY) {
1827929Sjoerg				const char *dcanon;
1837929Sjoerg				mt_com.mt_count = stringtodens(*argv);
1847929Sjoerg				if (mt_com.mt_count == 0)
18527752Scharnier					errx(1, "%s: unknown density", *argv);
1867929Sjoerg				dcanon = denstostring(mt_com.mt_count);
1877929Sjoerg				if (strcmp(dcanon, *argv) != 0)
1887929Sjoerg					printf(
1897929Sjoerg					"Using \"%s\" as an alias for %s\n",
1907929Sjoerg					       *argv, dcanon);
1917929Sjoerg				p = "";
19239260Sgibbs			} else if (!isdigit(**argv) &&
19339260Sgibbs				   comp->c_flags & IS_COMP) {
19439260Sgibbs
19539260Sgibbs				mt_com.mt_count = stringtocomp(*argv);
19639260Sgibbs				if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
19739260Sgibbs					errx(1, "%s: unknown compression",
19839260Sgibbs					     *argv);
19939260Sgibbs				p = "";
2007929Sjoerg			} else
2017929Sjoerg				/* allow for hex numbers; useful for density */
2027929Sjoerg				mt_com.mt_count = strtol(*argv, &p, 0);
2037913Sjoerg#else
2041590Srgrimes			mt_com.mt_count = strtol(*argv, &p, 10);
2057913Sjoerg#endif /* defined(__FreeBSD__) */
2067913Sjoerg			if (mt_com.mt_count <=
2077913Sjoerg#if defined (__FreeBSD__)
2087913Sjoerg			    ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
20939260Sgibbs			    && ((comp->c_flags & IS_COMP) == 0)
2107913Sjoerg#else
2117913Sjoerg			    0
2127913Sjoerg#endif /* defined (__FreeBSD__) */
2137913Sjoerg			    || *p)
21427752Scharnier				errx(1, "%s: illegal count", *argv);
2151590Srgrimes		}
2161590Srgrimes		else
2171590Srgrimes			mt_com.mt_count = 1;
2181590Srgrimes		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
21927752Scharnier			err(1, "%s: %s", tape, comp->c_name);
2201590Srgrimes	} else {
2211590Srgrimes		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
22227752Scharnier			err(1, NULL);
2231590Srgrimes		status(&mt_status);
2241590Srgrimes	}
2251590Srgrimes	exit (0);
2261590Srgrimes	/* NOTREACHED */
2271590Srgrimes}
2281590Srgrimes
2291590Srgrimes#ifdef vax
2301590Srgrimes#include <vax/mba/mtreg.h>
2311590Srgrimes#include <vax/mba/htreg.h>
2321590Srgrimes
2331590Srgrimes#include <vax/uba/utreg.h>
2341590Srgrimes#include <vax/uba/tmreg.h>
2351590Srgrimes#undef b_repcnt		/* argh */
2361590Srgrimes#include <vax/uba/tsreg.h>
2371590Srgrimes#endif
2381590Srgrimes
2391590Srgrimes#ifdef sun
2401590Srgrimes#include <sundev/tmreg.h>
2411590Srgrimes#include <sundev/arreg.h>
2421590Srgrimes#endif
2431590Srgrimes
2441590Srgrimes#ifdef tahoe
2451590Srgrimes#include <tahoe/vba/cyreg.h>
2461590Srgrimes#endif
2471590Srgrimes
24814176Sjoerg#ifdef __FreeBSD__
24914176Sjoerg#include <machine/wtio.h>
25014176Sjoerg#endif
25114176Sjoerg
2521590Srgrimesstruct tape_desc {
2531590Srgrimes	short	t_type;		/* type of magtape device */
2541590Srgrimes	char	*t_name;	/* printing name */
2551590Srgrimes	char	*t_dsbits;	/* "drive status" register */
2561590Srgrimes	char	*t_erbits;	/* "error" register */
2571590Srgrimes} tapes[] = {
2581590Srgrimes#ifdef vax
2591590Srgrimes	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
2601590Srgrimes	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
2611590Srgrimes	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
2621590Srgrimes	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
2631590Srgrimes	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
2641590Srgrimes#endif
2651590Srgrimes#ifdef sun
2661590Srgrimes	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
2671590Srgrimes	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
2681590Srgrimes#endif
2691590Srgrimes#ifdef tahoe
2701590Srgrimes	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
2711590Srgrimes#endif
2727913Sjoerg#if defined (__FreeBSD__)
2737913Sjoerg	/*
27414176Sjoerg	 * XXX This is weird.  The st driver reports the tape drive
2757913Sjoerg	 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
2767913Sjoerg	 * either reports MT_ISVIPER1 for an Archive tape, or 0x11
2777913Sjoerg	 * (MT_ISMFOUR) for other tapes.
2787913Sjoerg	 * XXX for the wt driver, rely on it behaving like a "standard"
2797913Sjoerg	 * magtape driver.
2807913Sjoerg	 */
2817913Sjoerg	{ MT_ISAR,	"SCSI tape drive", 0,		0 },
28214176Sjoerg	{ MT_ISVIPER1,	"Archive Viper", WTDS_BITS, WTER_BITS },
28314176Sjoerg	{ MT_ISMFOUR,	"Wangtek",	 WTDS_BITS, WTER_BITS },
2847913Sjoerg#endif /* defined (__FreeBSD__) */
2851590Srgrimes	{ 0 }
2861590Srgrimes};
2871590Srgrimes
2881590Srgrimes/*
2891590Srgrimes * Interpret the status buffer returned
2901590Srgrimes */
2911590Srgrimesvoid
2921590Srgrimesstatus(bp)
2931590Srgrimes	register struct mtget *bp;
2941590Srgrimes{
2951590Srgrimes	register struct tape_desc *mt;
2961590Srgrimes
2971590Srgrimes	for (mt = tapes;; mt++) {
2981590Srgrimes		if (mt->t_type == 0) {
2991590Srgrimes			(void)printf("%d: unknown tape drive type\n",
3001590Srgrimes			    bp->mt_type);
3011590Srgrimes			return;
3021590Srgrimes		}
3031590Srgrimes		if (mt->t_type == bp->mt_type)
3041590Srgrimes			break;
3051590Srgrimes	}
3067913Sjoerg#if defined (__FreeBSD__)
3077913Sjoerg	if(mt->t_type == MT_ISAR)
3087913Sjoerg		st_status(bp);
3097913Sjoerg	else {
3107913Sjoerg#endif /* defined (__FreeBSD__) */
3111590Srgrimes	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
31214176Sjoerg	printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
31314176Sjoerg	printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
3141590Srgrimes	(void)putchar('\n');
3157913Sjoerg#if defined (__FreeBSD__)
3167913Sjoerg	}
3177913Sjoerg#endif /* defined (__FreeBSD__) */
3181590Srgrimes}
3191590Srgrimes
3201590Srgrimes/*
3211590Srgrimes * Print a register a la the %b format of the kernel's printf.
3221590Srgrimes */
3231590Srgrimesvoid
3241590Srgrimesprintreg(s, v, bits)
3251590Srgrimes	char *s;
3261590Srgrimes	register u_int v;
3271590Srgrimes	register char *bits;
3281590Srgrimes{
3291590Srgrimes	register int i, any = 0;
3301590Srgrimes	register char c;
3311590Srgrimes
3321590Srgrimes	if (bits && *bits == 8)
3331590Srgrimes		printf("%s=%o", s, v);
3341590Srgrimes	else
3351590Srgrimes		printf("%s=%x", s, v);
33611608Sbde	if (!bits)
33711608Sbde		return;
3381590Srgrimes	bits++;
3391590Srgrimes	if (v && bits) {
3401590Srgrimes		putchar('<');
34127752Scharnier		while ((i = *bits++)) {
3421590Srgrimes			if (v & (1 << (i-1))) {
3431590Srgrimes				if (any)
3441590Srgrimes					putchar(',');
3451590Srgrimes				any = 1;
3461590Srgrimes				for (; (c = *bits) > 32; bits++)
3471590Srgrimes					putchar(c);
3481590Srgrimes			} else
3491590Srgrimes				for (; *bits > 32; bits++)
3501590Srgrimes					;
3511590Srgrimes		}
3521590Srgrimes		putchar('>');
3531590Srgrimes	}
3541590Srgrimes}
3551590Srgrimes
3561590Srgrimesvoid
3571590Srgrimesusage()
3581590Srgrimes{
3591590Srgrimes	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
3601590Srgrimes	exit(1);
3611590Srgrimes}
3621590Srgrimes
3637913Sjoerg#if defined (__FreeBSD__)
3647913Sjoerg
3657913Sjoergstruct densities {
3667913Sjoerg	int dens;
36739260Sgibbs	int bpmm;
36839260Sgibbs	int bpi;
3697913Sjoerg	const char *name;
37039260Sgibbs} dens[] = {
37139260Sgibbs	/*
37239260Sgibbs	 * Taken from T10 Project 997D
37339260Sgibbs	 * SCSI-3 Stream Device Commands (SSC)
37439260Sgibbs	 * Revision 11, 4-Nov-97
37539260Sgibbs	 */
37639260Sgibbs	/*Num.  bpmm    bpi     Reference     */
37739260Sgibbs	{ 0x1,	32,	800,	"X3.22-1983" },
37839260Sgibbs	{ 0x2,	63,	1600,	"X3.39-1986" },
37939260Sgibbs	{ 0x3,	246,	6250,	"X3.54-1986" },
38039260Sgibbs	{ 0x5,	315,	8000,	"X3.136-1986" },
38139260Sgibbs	{ 0x6,	126,	3200,	"X3.157-1987" },
38239260Sgibbs	{ 0x7,	252,	6400,	"X3.116-1986" },
38339260Sgibbs	{ 0x8,	315,	8000,	"X3.158-1987" },
38439260Sgibbs	{ 0x9,	491,	37871,	"X3.180" },
38539260Sgibbs	{ 0xA,	262,	6667,	"X3B5/86-199" },
38639260Sgibbs	{ 0xB,	63,	1600,	"X3.56-1986" },
38739260Sgibbs	{ 0xC,	500,	12690,	"HI-TC1" },
38839260Sgibbs	{ 0xD,	999,	25380,	"HI-TC2" },
38939260Sgibbs	{ 0xF,	394,	10000,	"QIC-120" },
39039260Sgibbs	{ 0x10,	394,	10000,	"QIC-150" },
39139260Sgibbs	{ 0x11,	630,	16000,	"QIC-320" },
39239260Sgibbs	{ 0x12,	2034,	51667,	"QIC-1350" },
39339260Sgibbs	{ 0x13,	2400,	61000,	"X3B5/88-185A" },
39439260Sgibbs	{ 0x14,	1703,	43245,	"X3.202-1991" },
39539260Sgibbs	{ 0x15,	1789,	45434,	"ECMA TC17" },
39639260Sgibbs	{ 0x16,	394,	10000,	"X3.193-1990" },
39739260Sgibbs	{ 0x17,	1673,	42500,	"X3B5/91-174" },
39839260Sgibbs	{ 0x18,	1673,	42500,	"X3B5/92-50" },
39939260Sgibbs	{ 0x1C, 1654,	42000,	"QIC-385M" },
40039260Sgibbs	{ 0x1D,	1512,	38400,	"QIC-410M" },
40139260Sgibbs	{ 0x1E, 1385,	36000,	"QIC-1000C" },
40239260Sgibbs	{ 0x1F,	2666,	67733,	"QIC-2100C" },
40339260Sgibbs	{ 0x20, 2666,	67733,	"QIC-6GB(M)" },
40439260Sgibbs	{ 0x21,	2666,	67733,	"QIC-20GB(C)" },
40539260Sgibbs	{ 0x22,	1600,	40640,	"QIC-2GB(C)" },
40639260Sgibbs	{ 0x23, 2666,	67733,	"QIC-875M" },
40739260Sgibbs	{ 0x24,	2400,	61000,	"DDS-2" },
40839260Sgibbs	{ 0x25,	3816,	97000,	"DDS-3" },
40939260Sgibbs	{ 0x26,	3816,	97000,	"DDS-4" },
41039260Sgibbs	{ 0x27,	3056,	77611,	"Mammoth" },
41139260Sgibbs	{ 0x28,	1491,	37871,	"X3.224" },
41239260Sgibbs	{ 0, 0, 0, NULL }
4137913Sjoerg};
4147913Sjoerg
41539260Sgibbsstruct compression_types {
41639260Sgibbs	u_int32_t	comp_number;
41739260Sgibbs	const char 	*name;
41839260Sgibbs} comp_types[] = {
41939260Sgibbs	{ 0x00, "none" },
42039260Sgibbs	{ 0x00, "off" },
42139260Sgibbs	{ 0x10, "IDRC" },
42239260Sgibbs	{ 0x20, "DCLZ" },
42339260Sgibbs	{ 0xffffffff, "enable" },
42439260Sgibbs	{ 0xffffffff, "on" },
42539260Sgibbs	{ 0xf0f0f0f0, NULL}
42639260Sgibbs};
42739260Sgibbs
4287913Sjoergconst char *
4297929Sjoergdenstostring(int d)
4307913Sjoerg{
4317913Sjoerg	static char buf[20];
4327913Sjoerg	struct densities *sd;
4337913Sjoerg
4347913Sjoerg	for (sd = dens; sd->dens; sd++)
4357913Sjoerg		if (sd->dens == d)
4367913Sjoerg			break;
4377913Sjoerg	if (sd->dens == 0) {
4387929Sjoerg		sprintf(buf, "0x%02x", d);
4397913Sjoerg		return buf;
4407929Sjoerg	} else
4417913Sjoerg		return sd->name;
4427913Sjoerg}
4437913Sjoerg
44439260Sgibbs/*
44539260Sgibbs * Given a specific density number, return either the bits per inch or bits
44639260Sgibbs * per millimeter for the given density.
44739260Sgibbs */
4487929Sjoergint
44939260Sgibbsdenstobp(int d, int bpi)
45039260Sgibbs{
45139260Sgibbs	struct densities *sd;
45239260Sgibbs
45339260Sgibbs	for (sd = dens; sd->dens; sd++)
45439260Sgibbs		if (sd->dens == d)
45539260Sgibbs			break;
45639260Sgibbs	if (sd->dens == 0)
45739260Sgibbs		return(0);
45839260Sgibbs	else {
45939260Sgibbs		if (bpi)
46039260Sgibbs			return(sd->bpi);
46139260Sgibbs		else
46239260Sgibbs			return(sd->bpmm);
46339260Sgibbs	}
46439260Sgibbs}
46539260Sgibbs
46639260Sgibbsint
4677929Sjoergstringtodens(const char *s)
4687929Sjoerg{
4697929Sjoerg	struct densities *sd;
4707929Sjoerg	size_t l = strlen(s);
4717929Sjoerg
4727929Sjoerg	for (sd = dens; sd->dens; sd++)
4737929Sjoerg		if (strncasecmp(sd->name, s, l) == 0)
4747929Sjoerg			break;
4757929Sjoerg	return sd->dens;
4767929Sjoerg}
4777929Sjoerg
4787929Sjoerg
4797913Sjoergconst char *
4807913Sjoerggetblksiz(int bs)
4817913Sjoerg{
4827913Sjoerg	static char buf[25];
4837913Sjoerg	if (bs == 0)
4847913Sjoerg		return "variable";
4857913Sjoerg	else {
48639260Sgibbs		sprintf(buf, "%d bytes", bs);
4877913Sjoerg		return buf;
4887913Sjoerg	}
4897913Sjoerg}
4907913Sjoerg
49139260Sgibbsconst char *
49239260Sgibbscomptostring(u_int32_t comp)
49339260Sgibbs{
49439260Sgibbs	static char buf[20];
49539260Sgibbs	struct compression_types *ct;
4967913Sjoerg
49739260Sgibbs	if (comp == MT_COMP_DISABLED)
49839260Sgibbs		return "disabled";
49939260Sgibbs	else if (comp == MT_COMP_UNSUPP)
50039260Sgibbs		return "unsupported";
50139260Sgibbs
50239260Sgibbs	for (ct = comp_types; ct->name; ct++)
50339260Sgibbs		if (ct->comp_number == comp)
50439260Sgibbs			break;
50539260Sgibbs
50639260Sgibbs	if (ct->comp_number == 0xf0f0f0f0) {
50739260Sgibbs		sprintf(buf, "0x%2x", comp);
50839260Sgibbs		return(buf);
50939260Sgibbs	} else
51039260Sgibbs		return(ct->name);
51139260Sgibbs}
51239260Sgibbs
51339260Sgibbsu_int32_t
51439260Sgibbsstringtocomp(const char *s)
51539260Sgibbs{
51639260Sgibbs	struct compression_types *ct;
51739260Sgibbs	size_t l = strlen(s);
51839260Sgibbs
51939260Sgibbs	for (ct = comp_types; ct->name; ct++)
52039260Sgibbs		if (strncasecmp(ct->name, s, l) == 0)
52139260Sgibbs			break;
52239260Sgibbs
52339260Sgibbs	return(ct->comp_number);
52439260Sgibbs}
52539260Sgibbs
5267913Sjoergvoid
5277913Sjoergst_status(struct mtget *bp)
5287913Sjoerg{
52939260Sgibbs	printf("Mode      Density         Blocksize      bpi      "
53039260Sgibbs	       "Compression\n"
53139260Sgibbs	       "Current:  %-12s    %-12s   %-7d  %s\n"
53239260Sgibbs	       "---------available modes---------\n"
53339260Sgibbs	       "0:        %-12s    %-12s   %-7d  %s\n"
53439260Sgibbs	       "1:        %-12s    %-12s   %-7d  %s\n"
53539260Sgibbs	       "2:        %-12s    %-12s   %-7d  %s\n"
53639260Sgibbs	       "3:        %-12s    %-12s   %-7d  %s\n",
53739260Sgibbs	       denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
53839260Sgibbs	       denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
53939260Sgibbs	       denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
54039260Sgibbs	       denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
54139260Sgibbs	       denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
54239260Sgibbs	       denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
54339260Sgibbs	       denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
54439260Sgibbs	       denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
54539260Sgibbs	       denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
54639260Sgibbs	       denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
5477913Sjoerg}
5487913Sjoerg
5499541Sjoergvoid
5509541Sjoergwarn_eof(void)
5519541Sjoerg{
5529541Sjoerg	fprintf(stderr,
5539541Sjoerg		"The \"eof\" command has been disabled.\n"
5549541Sjoerg		"Use \"weof\" if you really want to write end-of-file marks,\n"
5559541Sjoerg		"or \"eom\" if you rather want to skip to the end of "
5569541Sjoerg		"recorded medium.\n");
5579541Sjoerg	exit(1);
5589541Sjoerg}
5599541Sjoerg
5607913Sjoerg#endif /* defined (__FreeBSD__) */
561