mt.c revision 1590
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
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
58struct commands {
59	char *c_name;
60	int c_code;
61	int c_ronly;
62} com[] = {
63	{ "bsf",	MTBSF,	1 },
64	{ "bsr",	MTBSR,	1 },
65	{ "eof",	MTWEOF,	0 },
66	{ "fsf",	MTFSF,	1 },
67	{ "fsr",	MTFSR,	1 },
68	{ "offline",	MTOFFL,	1 },
69	{ "rewind",	MTREW,	1 },
70	{ "rewoffl",	MTOFFL,	1 },
71	{ "status",	MTNOP,	1 },
72	{ "weof",	MTWEOF,	0 },
73	{ NULL }
74};
75
76void err __P((const char *, ...));
77void printreg __P((char *, u_int, char *));
78void status __P((struct mtget *));
79void usage __P((void));
80
81int
82main(argc, argv)
83	int argc;
84	char *argv[];
85{
86	register struct commands *comp;
87	struct mtget mt_status;
88	struct mtop mt_com;
89	int ch, len, mtfd;
90	char *p, *tape;
91
92	if ((tape = getenv("TAPE")) == NULL)
93		tape = DEFTAPE;
94
95	while ((ch = getopt(argc, argv, "f:t:")) != EOF)
96		switch(ch) {
97		case 'f':
98		case 't':
99			tape = optarg;
100			break;
101		case '?':
102		default:
103			usage();
104		}
105	argc -= optind;
106	argv += optind;
107
108	if (argc < 1 || argc > 2)
109		usage();
110
111	len = strlen(p = *argv++);
112	for (comp = com;; comp++) {
113		if (comp->c_name == NULL)
114			err("%s: unknown command", p);
115		if (strncmp(p, comp->c_name, len) == 0)
116			break;
117	}
118	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
119		err("%s: %s", tape, strerror(errno));
120	if (comp->c_code != MTNOP) {
121		mt_com.mt_op = comp->c_code;
122		if (*argv) {
123			mt_com.mt_count = strtol(*argv, &p, 10);
124			if (mt_com.mt_count <= 0 || *p)
125				err("%s: illegal count", *argv);
126		}
127		else
128			mt_com.mt_count = 1;
129		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
130			err("%s: %s: %s", tape, comp->c_name, strerror(errno));
131	} else {
132		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
133			err("%s", strerror(errno));
134		status(&mt_status);
135	}
136	exit (0);
137	/* NOTREACHED */
138}
139
140#ifdef vax
141#include <vax/mba/mtreg.h>
142#include <vax/mba/htreg.h>
143
144#include <vax/uba/utreg.h>
145#include <vax/uba/tmreg.h>
146#undef b_repcnt		/* argh */
147#include <vax/uba/tsreg.h>
148#endif
149
150#ifdef sun
151#include <sundev/tmreg.h>
152#include <sundev/arreg.h>
153#endif
154
155#ifdef tahoe
156#include <tahoe/vba/cyreg.h>
157#endif
158
159struct tape_desc {
160	short	t_type;		/* type of magtape device */
161	char	*t_name;	/* printing name */
162	char	*t_dsbits;	/* "drive status" register */
163	char	*t_erbits;	/* "error" register */
164} tapes[] = {
165#ifdef vax
166	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
167	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
168	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
169	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
170	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
171#endif
172#ifdef sun
173	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
174	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
175#endif
176#ifdef tahoe
177	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
178#endif
179	{ 0 }
180};
181
182/*
183 * Interpret the status buffer returned
184 */
185void
186status(bp)
187	register struct mtget *bp;
188{
189	register struct tape_desc *mt;
190
191	for (mt = tapes;; mt++) {
192		if (mt->t_type == 0) {
193			(void)printf("%d: unknown tape drive type\n",
194			    bp->mt_type);
195			return;
196		}
197		if (mt->t_type == bp->mt_type)
198			break;
199	}
200	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
201	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
202	printreg("\ner", bp->mt_erreg, mt->t_erbits);
203	(void)putchar('\n');
204}
205
206/*
207 * Print a register a la the %b format of the kernel's printf.
208 */
209void
210printreg(s, v, bits)
211	char *s;
212	register u_int v;
213	register char *bits;
214{
215	register int i, any = 0;
216	register char c;
217
218	if (bits && *bits == 8)
219		printf("%s=%o", s, v);
220	else
221		printf("%s=%x", s, v);
222	bits++;
223	if (v && bits) {
224		putchar('<');
225		while (i = *bits++) {
226			if (v & (1 << (i-1))) {
227				if (any)
228					putchar(',');
229				any = 1;
230				for (; (c = *bits) > 32; bits++)
231					putchar(c);
232			} else
233				for (; *bits > 32; bits++)
234					;
235		}
236		putchar('>');
237	}
238}
239
240void
241usage()
242{
243	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
244	exit(1);
245}
246
247#if __STDC__
248#include <stdarg.h>
249#else
250#include <varargs.h>
251#endif
252
253void
254#if __STDC__
255err(const char *fmt, ...)
256#else
257err(fmt, va_alist)
258	char *fmt;
259        va_dcl
260#endif
261{
262	va_list ap;
263#if __STDC__
264	va_start(ap, fmt);
265#else
266	va_start(ap);
267#endif
268	(void)fprintf(stderr, "mt: ");
269	(void)vfprintf(stderr, fmt, ap);
270	va_end(ap);
271	(void)fprintf(stderr, "\n");
272	exit(1);
273	/* NOTREACHED */
274}
275