xref: /netbsd-src/bin/mt/mt.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* $NetBSD: mt.c,v 1.47 2011/08/29 14:46:01 joerg Exp $ */
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)mt.c	8.2 (Berkeley) 6/6/93";
41 #else
42 __RCSID("$NetBSD: mt.c,v 1.47 2011/08/29 14:46:01 joerg Exp $");
43 #endif
44 #endif /* not lint */
45 
46 /*
47  * mt --
48  *   magnetic tape manipulation program
49  */
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/ioctl.h>
53 #include <sys/mtio.h>
54 #include <sys/stat.h>
55 
56 #include <ctype.h>
57 #include <err.h>
58 #include <fcntl.h>
59 #include <paths.h>
60 #include <rmt.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 
66 /* pseudo ioctl constants */
67 #define MTASF	100
68 
69 struct commands {
70 	const char *c_name;		/* command */
71 	size_t c_namelen;		/* command len */
72 	u_long c_spcl;			/* ioctl request */
73 	int c_code;			/* ioctl code for MTIOCTOP command */
74 	int c_ronly;			/* open tape read-only */
75 	int c_mincount;			/* min allowed count value */
76 };
77 
78 #define CMD(a)	a, sizeof(a) - 1
79 static const struct commands com[] = {
80 	{ CMD("asf"),		MTIOCTOP,     MTASF,      1,  0 },
81 	{ CMD("blocksize"),	MTIOCTOP,     MTSETBSIZ,  1,  0 },
82 	{ CMD("bsf"),		MTIOCTOP,     MTBSF,      1,  1 },
83 	{ CMD("bsr"),		MTIOCTOP,     MTBSR,      1,  1 },
84 	{ CMD("compress"),	MTIOCTOP,     MTCMPRESS,  1,  0 },
85 	{ CMD("density"),	MTIOCTOP,     MTSETDNSTY, 1,  0 },
86 	{ CMD("eof"),		MTIOCTOP,     MTWEOF,     0,  1 },
87 	{ CMD("eom"),		MTIOCTOP,     MTEOM,      1,  0 },
88 	{ CMD("erase"),		MTIOCTOP,     MTERASE,    0,  0 },
89 	{ CMD("fsf"),		MTIOCTOP,     MTFSF,      1,  1 },
90 	{ CMD("fsr"),		MTIOCTOP,     MTFSR,      1,  1 },
91 	{ CMD("offline"),	MTIOCTOP,     MTOFFL,     1,  0 },
92 	{ CMD("rdhpos"),	MTIOCRDHPOS,  0,          1,  0 },
93 	{ CMD("rdspos"),	MTIOCRDSPOS,  0,          1,  0 },
94 	{ CMD("retension"),	MTIOCTOP,     MTRETEN,    1,  0 },
95 	{ CMD("rewind"),	MTIOCTOP,     MTREW,      1,  0 },
96 	{ CMD("rewoffl"),	MTIOCTOP,     MTOFFL,     1,  0 },
97 	{ CMD("setblk"),	MTIOCTOP,     MTSETBSIZ,  1,  0 },
98 	{ CMD("setdensity"),	MTIOCTOP,     MTSETDNSTY, 1,  0 },
99 	{ CMD("sethpos"),	MTIOCHLOCATE, 0,          1,  0 },
100 	{ CMD("setspos"),	MTIOCSLOCATE, 0,          1,  0 },
101 	{ CMD("status"),	MTIOCGET,     MTNOP,      1,  0 },
102 	{ CMD("weof"),		MTIOCTOP,     MTWEOF,     0,  1 },
103 	{ CMD("eew"),		MTIOCTOP,     MTEWARN,    1,  0 },
104 	{ .c_name = NULL }
105 };
106 
107 static void printreg(const char *, u_int, const char *);
108 static void status(struct mtget *);
109 __dead static void usage(void);
110 
111 int
112 main(int argc, char *argv[])
113 {
114 	const struct commands *cp, *comp;
115 	struct mtget mt_status;
116 	struct mtop mt_com;
117 	int ch, mtfd, flags;
118 	char *p;
119 	const char *tape;
120 	int count;
121 	size_t len;
122 
123 	setprogname(argv[0]);
124 	if ((tape = getenv("TAPE")) == NULL)
125 		tape = _PATH_DEFTAPE;
126 
127 	while ((ch = getopt(argc, argv, "f:t:")) != -1)
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 = NULL, cp = com; cp->c_name != NULL; cp++) {
145 		size_t clen = MIN(len, cp->c_namelen);
146 		if (strncmp(p, cp->c_name, clen) == 0) {
147 			if (comp != NULL)
148 				errx(1, "%s: Ambiguous command `%s' or `%s'?",
149 				    p, cp->c_name, comp->c_name);
150 			else
151 				comp = cp;
152 		}
153 	}
154 	if (comp == NULL)
155 		errx(1, "%s: unknown command", p);
156 
157 	if (*argv) {
158 		count = strtol(*argv, &p, 10);
159 		if (count < comp->c_mincount || *p)
160 			errx(2, "%s: illegal count", *argv);
161 	} else
162 		count = 1;
163 
164 	flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
165 
166 	if ((mtfd = open(tape, flags)) < 0)
167 		err(2, "%s", tape);
168 
169 	switch (comp->c_spcl) {
170 	case MTIOCTOP:
171 		if (comp->c_code == MTASF) {
172 
173 			/* If mtget.mt_fileno was implemented, We could
174 			   compute the minimal seek needed to position
175 			   the tape.  Until then, rewind and seek from
176 			   begining-of-tape */
177 
178 			mt_com.mt_op = MTREW;
179 			mt_com.mt_count = 1;
180 			if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
181 				err(2, "%s", tape);
182 
183 			if (count > 0) {
184 				mt_com.mt_op = MTFSF;
185 				    mt_com.mt_count = count;
186 				if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
187 				    err(2, "%s", tape);
188 			}
189 
190 		} else {
191 			mt_com.mt_op = comp->c_code;
192 			mt_com.mt_count = count;
193 
194 			if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
195 				err(2, "%s: %s", tape, comp->c_name);
196 
197 		}
198 		break;
199 
200 	case MTIOCGET:
201 		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
202 			err(2, "%s: %s", tape, comp->c_name);
203 		status(&mt_status);
204 		break;
205 
206 	case MTIOCRDSPOS:
207 	case MTIOCRDHPOS:
208 		if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
209 			err(2, "%s", tape);
210 		printf("%s: block location %u\n", tape, (unsigned int) count);
211 		break;
212 
213 	case MTIOCSLOCATE:
214 	case MTIOCHLOCATE:
215 		if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
216 			err(2, "%s", tape);
217 		break;
218 
219 	default:
220 		errx(1, "internal error: unknown request %ld", comp->c_spcl);
221 	}
222 
223 	exit(0);
224 	/* NOTREACHED */
225 }
226 
227 #if defined(sun) && !defined(__SVR4)
228 #include <sundev/tmreg.h>
229 #include <sundev/arreg.h>
230 #endif
231 
232 #ifdef tahoe
233 #include <tahoe/vba/cyreg.h>
234 #endif
235 
236 static const struct tape_desc {
237 	short	t_type;		/* type of magtape device */
238 	const	char *t_name;	/* printing name */
239 	const	char *t_dsbits;	/* "drive status" register */
240 	const	char *t_erbits;	/* "error" register */
241 } tapes[] = {
242 #if defined(sun) && !defined(__SVR4)
243 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
244 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
245 #endif
246 #ifdef tahoe
247 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
248 #endif
249 #define SCSI_DS_BITS	"\20\5WriteProtect\2Mounted"
250 	{ 0x7,		"SCSI",		SCSI_DS_BITS,	"76543210" },
251 	{ .t_type = 0 }
252 };
253 
254 
255 /*
256  * Interpret the status buffer returned
257  */
258 static void
259 status(struct mtget *bp)
260 {
261 	const struct tape_desc *mt;
262 
263 	for (mt = tapes;; mt++) {
264 		if (mt->t_type == 0) {
265 			(void)printf("%d: unknown tape drive type\n",
266 			    bp->mt_type);
267 			return;
268 		}
269 		if (mt->t_type == bp->mt_type)
270 			break;
271 	}
272 	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
273 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
274 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
275 	(void)putchar('\n');
276 	(void)printf("blocksize: %d (%d, %d, %d, %d)\n",
277 		bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
278 		bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
279 	(void)printf("density: %d (%d, %d, %d, %d)\n",
280 		bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
281 		bp->mt_mdensity[2], bp->mt_mdensity[3]);
282 	(void)printf("current file number: %d\n", bp->mt_fileno);
283 	(void)printf("current block number: %d\n", bp->mt_blkno);
284 }
285 
286 /*
287  * Print a register a la the %b format of the kernel's printf.
288  */
289 static void
290 printreg(const char *s, u_int v, const char *bits)
291 {
292 	int any, i;
293 	char c;
294 
295 	any = 0;
296 	if (bits && *bits == 8)
297 		printf("%s=%o", s, v);
298 	else
299 		printf("%s=%x", s, v);
300 	if (v && bits && *++bits) {
301 		putchar('<');
302 		while ((i = *bits++)) {
303 			if (v & (1 << (i-1))) {
304 				if (any)
305 					putchar(',');
306 				any = 1;
307 				for (; (c = *bits) > 32; bits++)
308 					putchar(c);
309 			} else
310 				for (; *bits > 32; bits++)
311 					;
312 		}
313 		putchar('>');
314 	}
315 }
316 
317 static void
318 usage(void)
319 {
320 	(void)fprintf(stderr, "usage: %s [-f device] command [count]\n",
321 	    getprogname());
322 	exit(1);
323 	/* NOTREACHED */
324 }
325