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