xref: /netbsd-src/bin/mt/mt.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*
2  * Copyright (c) 1980 The Regents of the University of California.
3  * 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 char copyright[] =
36 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)mt.c	5.6 (Berkeley) 6/6/91";*/
42 static char rcsid[] = "$Id: mt.c,v 1.4 1994/04/05 21:13:55 mycroft Exp $";
43 #endif /* not lint */
44 
45 /*
46  * mt --
47  *   magnetic tape manipulation program
48  */
49 #include <sys/types.h>
50 #include <sys/ioctl.h>
51 #include <sys/mtio.h>
52 #include <fcntl.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <ctype.h>
57 #include <err.h>
58 
59 static void printreg();
60 static void status();
61 static void usage();
62 
63 struct commands {
64 	char *c_name;
65 	int c_code;
66 	int c_ronly;
67 } com[] = {
68 	{ "weof",	MTWEOF,	 0 },
69 	{ "eof",	MTWEOF,	 0 },
70 	{ "fsf",	MTFSF,	 1 },
71 	{ "bsf",	MTBSF,	 1 },
72 	{ "fsr",	MTFSR,	 1 },
73 	{ "bsr",	MTBSR,	 1 },
74 	{ "rewind",	MTREW,	 1 },
75 	{ "offline",	MTOFFL,	 1 },
76 	{ "rewoffl",	MTOFFL,	 1 },
77 	{ "status",	MTNOP,	 1 },
78 	{ "retension",	MTRETEN, 1 },
79 	{ "erase",	MTERASE, 0 },
80 	{ "eom",	MTEOM,	 1 },
81 	{ "nbsf",	MTNBSF,	 1 },
82 	{ 0 }
83 };
84 
85 int mtfd;
86 struct mtop mt_com;
87 struct mtget mt_status;
88 char *tape = NULL;
89 
90 int
91 main(argc, argv)
92 	int argc;
93 	char **argv;
94 {
95 	register char *cp;
96 	register struct commands *comp;
97 	int c;
98 
99 	while ((c = getopt(argc, argv, "f:t:")) != -1) {
100 		switch (c) {
101 		case 'f':
102 		case 't':
103 			tape = optarg;
104 			break;
105 		default:
106 			usage();
107 			/* NOTREACHED */
108 		}
109 	}
110 	argc -= optind;
111 	argv += optind;
112 
113 	if (argc < 1 || argc > 2) {
114 		usage();
115 		/* NOTREACHED */
116 	}
117 
118 	cp = argv[0];
119 	for (comp = com; comp->c_name != NULL; comp++)
120 		if (strncmp(cp, comp->c_name, strlen(cp)) == 0)
121 			break;
122 	if (comp->c_name == NULL) {
123 		errx(1, "don't grok \"%s\"", cp);
124 		/* NOTREACHED */
125 	}
126 
127 	if (tape == NULL) {
128 		if ((tape = getenv("TAPE")) == NULL)
129 			tape = DEFTAPE;
130 	}
131 
132 	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0) {
133 		err(1, "%s", tape);
134 		/* NOTREACHED */
135 	}
136 	if (comp->c_code != MTNOP) {
137 		mt_com.mt_op = comp->c_code;
138 		mt_com.mt_count = (argc == 2 ? atoi(argv[1]) : 1);
139 		if (mt_com.mt_count < 0) {
140 			errx(1, "negative repeat count");
141 			/* NOTREACHED */
142 		}
143 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
144 			err(2, "%s %s %d failed",
145 				tape, comp->c_name, mt_com.mt_count);
146 			/* NOTREACHED */
147 		}
148 	} else {
149 		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
150 			err(2, NULL);
151 			/* NOTREACHED */
152 		}
153 		status(&mt_status);
154 	}
155 
156 	exit(0);
157 }
158 
159 #ifdef vax
160 #include <vaxmba/mtreg.h>
161 #include <vaxmba/htreg.h>
162 
163 #include <vaxuba/utreg.h>
164 #include <vaxuba/tmreg.h>
165 #undef b_repcnt		/* argh */
166 #include <vaxuba/tsreg.h>
167 #endif
168 
169 #ifdef sun
170 #include <sundev/tmreg.h>
171 #include <sundev/arreg.h>
172 #endif
173 
174 #ifdef tahoe
175 #include <tahoe/vba/cyreg.h>
176 #endif
177 
178 struct tape_desc {
179 	short	t_type;		/* type of magtape device */
180 	char	*t_name;	/* printing name */
181 	char	*t_dsbits;	/* "drive status" register */
182 	char	*t_erbits;	/* "error" register */
183 } tapes[] = {
184 #ifdef vax
185 	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
186 	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
187 	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
188 	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
189 	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
190 #endif
191 #ifdef sun
192 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
193 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
194 #endif
195 #ifdef tahoe
196 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
197 #endif
198 	{ 0 }
199 };
200 
201 /*
202  * Interpret the status buffer returned
203  */
204 static void
205 status(bp)
206 	register struct mtget *bp;
207 {
208 	register struct tape_desc *mt;
209 
210 	for (mt = tapes; mt->t_type; mt++)
211 		if (mt->t_type == bp->mt_type)
212 			break;
213 	if (mt->t_type == 0) {
214 		printf("unknown tape drive type (%d)\n", bp->mt_type);
215 		return;
216 	}
217 	printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
218 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
219 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
220 	putchar('\n');
221 }
222 
223 /*
224  * Print a register a la the %b format of the kernel's printf
225  */
226 static void
227 printreg(s, v, bits)
228 	char *s;
229 	register char *bits;
230 	register unsigned short v;
231 {
232 	register int i, any = 0;
233 	register char c;
234 
235 	if (bits && *bits == 8)
236 		printf("%s=%o", s, v);
237 	else
238 		printf("%s=%x", s, v);
239 	bits++;
240 	if (v && bits) {
241 		putchar('<');
242 		while (i = *bits++) {
243 			if (v & (1 << (i-1))) {
244 				if (any)
245 					putchar(',');
246 				any = 1;
247 				for (; (c = *bits) > 32; bits++)
248 					putchar(c);
249 			} else
250 				for (; *bits > 32; bits++)
251 					;
252 		}
253 		putchar('>');
254 	}
255 }
256 
257 static void
258 usage()
259 {
260 	fprintf(stderr, "usage: mt [ -f device ] command [ count ]\n");
261 	exit(1);
262 }
263