xref: /netbsd-src/bin/mt/mt.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: mt.c,v 1.46 2008/07/20 00:52:40 lukem 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.46 2008/07/20 00:52:40 lukem 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 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 void printreg(const char *, u_int, const char *);
108 void status(struct mtget *);
109 void usage(void);
110 int main(int, char *[]);
111 
112 int
113 main(int argc, char *argv[])
114 {
115 	const struct commands *cp, *comp;
116 	struct mtget mt_status;
117 	struct mtop mt_com;
118 	int ch, mtfd, flags;
119 	char *p;
120 	const char *tape;
121 	int count;
122 	size_t len;
123 
124 	setprogname(argv[0]);
125 	if ((tape = getenv("TAPE")) == NULL)
126 		tape = _PATH_DEFTAPE;
127 
128 	while ((ch = getopt(argc, argv, "f:t:")) != -1)
129 		switch (ch) {
130 		case 'f':
131 		case 't':
132 			tape = optarg;
133 			break;
134 		case '?':
135 		default:
136 			usage();
137 		}
138 	argc -= optind;
139 	argv += optind;
140 
141 	if (argc < 1 || argc > 2)
142 		usage();
143 
144 	len = strlen(p = *argv++);
145 	for (comp = NULL, cp = com; cp->c_name != NULL; cp++) {
146 		size_t clen = MIN(len, cp->c_namelen);
147 		if (strncmp(p, cp->c_name, clen) == 0) {
148 			if (comp != NULL)
149 				errx(1, "%s: Ambiguous command `%s' or `%s'?",
150 				    p, cp->c_name, comp->c_name);
151 			else
152 				comp = cp;
153 		}
154 	}
155 	if (comp == NULL)
156 		errx(1, "%s: unknown command", p);
157 
158 	if (*argv) {
159 		count = strtol(*argv, &p, 10);
160 		if (count < comp->c_mincount || *p)
161 			errx(2, "%s: illegal count", *argv);
162 	} else
163 		count = 1;
164 
165 	flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
166 
167 	if ((mtfd = open(tape, flags)) < 0)
168 		err(2, "%s", tape);
169 
170 	switch (comp->c_spcl) {
171 	case MTIOCTOP:
172 		if (comp->c_code == MTASF) {
173 
174 			/* If mtget.mt_fileno was implemented, We could
175 			   compute the minimal seek needed to position
176 			   the tape.  Until then, rewind and seek from
177 			   begining-of-tape */
178 
179 			mt_com.mt_op = MTREW;
180 			mt_com.mt_count = 1;
181 			if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
182 				err(2, "%s", tape);
183 
184 			if (count > 0) {
185 				mt_com.mt_op = MTFSF;
186 				    mt_com.mt_count = count;
187 				if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
188 				    err(2, "%s", tape);
189 			}
190 
191 		} else {
192 			mt_com.mt_op = comp->c_code;
193 			mt_com.mt_count = count;
194 
195 			if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
196 				err(2, "%s: %s", tape, comp->c_name);
197 
198 		}
199 		break;
200 
201 	case MTIOCGET:
202 		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
203 			err(2, "%s: %s", tape, comp->c_name);
204 		status(&mt_status);
205 		break;
206 
207 	case MTIOCRDSPOS:
208 	case MTIOCRDHPOS:
209 		if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
210 			err(2, "%s", tape);
211 		printf("%s: block location %u\n", tape, (unsigned int) count);
212 		break;
213 
214 	case MTIOCSLOCATE:
215 	case MTIOCHLOCATE:
216 		if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
217 			err(2, "%s", tape);
218 		break;
219 
220 	default:
221 		errx(1, "internal error: unknown request %ld", comp->c_spcl);
222 	}
223 
224 	exit(0);
225 	/* NOTREACHED */
226 }
227 
228 #if defined(sun) && !defined(__SVR4)
229 #include <sundev/tmreg.h>
230 #include <sundev/arreg.h>
231 #endif
232 
233 #ifdef tahoe
234 #include <tahoe/vba/cyreg.h>
235 #endif
236 
237 const struct tape_desc {
238 	short	t_type;		/* type of magtape device */
239 	const	char *t_name;	/* printing name */
240 	const	char *t_dsbits;	/* "drive status" register */
241 	const	char *t_erbits;	/* "error" register */
242 } tapes[] = {
243 #if defined(sun) && !defined(__SVR4)
244 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
245 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
246 #endif
247 #ifdef tahoe
248 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
249 #endif
250 #define SCSI_DS_BITS	"\20\5WriteProtect\2Mounted"
251 	{ 0x7,		"SCSI",		SCSI_DS_BITS,	"76543210" },
252 	{ .t_type = 0 }
253 };
254 
255 
256 /*
257  * Interpret the status buffer returned
258  */
259 void
260 status(struct mtget *bp)
261 {
262 	const struct tape_desc *mt;
263 
264 	for (mt = tapes;; mt++) {
265 		if (mt->t_type == 0) {
266 			(void)printf("%d: unknown tape drive type\n",
267 			    bp->mt_type);
268 			return;
269 		}
270 		if (mt->t_type == bp->mt_type)
271 			break;
272 	}
273 	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
274 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
275 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
276 	(void)putchar('\n');
277 	(void)printf("blocksize: %d (%d, %d, %d, %d)\n",
278 		bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
279 		bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
280 	(void)printf("density: %d (%d, %d, %d, %d)\n",
281 		bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
282 		bp->mt_mdensity[2], bp->mt_mdensity[3]);
283 	(void)printf("current file number: %d\n", bp->mt_fileno);
284 	(void)printf("current block number: %d\n", bp->mt_blkno);
285 }
286 
287 /*
288  * Print a register a la the %b format of the kernel's printf.
289  */
290 void
291 printreg(const char *s, u_int v, const char *bits)
292 {
293 	int any, i;
294 	char c;
295 
296 	any = 0;
297 	if (bits && *bits == 8)
298 		printf("%s=%o", s, v);
299 	else
300 		printf("%s=%x", s, v);
301 	if (v && bits && *++bits) {
302 		putchar('<');
303 		while ((i = *bits++)) {
304 			if (v & (1 << (i-1))) {
305 				if (any)
306 					putchar(',');
307 				any = 1;
308 				for (; (c = *bits) > 32; bits++)
309 					putchar(c);
310 			} else
311 				for (; *bits > 32; bits++)
312 					;
313 		}
314 		putchar('>');
315 	}
316 }
317 
318 void
319 usage(void)
320 {
321 	(void)fprintf(stderr, "usage: %s [-f device] command [count]\n",
322 	    getprogname());
323 	exit(1);
324 	/* NOTREACHED */
325 }
326