xref: /netbsd-src/bin/mt/mt.c (revision 5f7096188587a2c7c95fa3c69b78e1ec9c7923d0)
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.2 1993/08/01 18:59:20 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 <ctype.h>
55 
56 #define	equal(s1,s2)	(strcmp(s1, s2) == 0)
57 
58 struct commands {
59 	char *c_name;
60 	int c_code;
61 	int c_ronly;
62 } com[] = {
63 	{ "weof",	MTWEOF,	0 },
64 	{ "eof",	MTWEOF,	0 },
65 	{ "fsf",	MTFSF,	1 },
66 	{ "bsf",	MTBSF,	1 },
67 	{ "fsr",	MTFSR,	1 },
68 	{ "bsr",	MTBSR,	1 },
69 	{ "rewind",	MTREW,	1 },
70 	{ "offline",	MTOFFL,	1 },
71 	{ "rewoffl",	MTOFFL,	1 },
72 	{ "status",	MTNOP,	1 },
73 	{ 0 }
74 };
75 
76 int mtfd;
77 struct mtop mt_com;
78 struct mtget mt_status;
79 char *tape;
80 
81 main(argc, argv)
82 	char **argv;
83 {
84 	char line[80], *getenv();
85 	register char *cp;
86 	register struct commands *comp;
87 
88 	if (argc > 2 && (equal(argv[1], "-t") || equal(argv[1], "-f"))) {
89 		argc -= 2;
90 		tape = argv[2];
91 		argv += 2;
92 	} else
93 		if ((tape = getenv("TAPE")) == NULL)
94 			tape = DEFTAPE;
95 	if (argc < 2) {
96 		fprintf(stderr, "usage: mt [ -f device ] command [ count ]\n");
97 		exit(1);
98 	}
99 	cp = argv[1];
100 	for (comp = com; comp->c_name != NULL; comp++)
101 		if (strncmp(cp, comp->c_name, strlen(cp)) == 0)
102 			break;
103 	if (comp->c_name == NULL) {
104 		fprintf(stderr, "mt: don't grok \"%s\"\n", cp);
105 		exit(1);
106 	}
107 	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0) {
108 		perror(tape);
109 		exit(1);
110 	}
111 	if (comp->c_code != MTNOP) {
112 		mt_com.mt_op = comp->c_code;
113 		mt_com.mt_count = (argc > 2 ? atoi(argv[2]) : 1);
114 		if (mt_com.mt_count < 0) {
115 			fprintf(stderr, "mt: negative repeat count\n");
116 			exit(1);
117 		}
118 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
119 			fprintf(stderr, "%s %s %d ", tape, comp->c_name,
120 				mt_com.mt_count);
121 			perror("failed");
122 			exit(2);
123 		}
124 	} else {
125 		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
126 			perror("mt");
127 			exit(2);
128 		}
129 		status(&mt_status);
130 	}
131 }
132 
133 #ifdef vax
134 #include <vaxmba/mtreg.h>
135 #include <vaxmba/htreg.h>
136 
137 #include <vaxuba/utreg.h>
138 #include <vaxuba/tmreg.h>
139 #undef b_repcnt		/* argh */
140 #include <vaxuba/tsreg.h>
141 #endif
142 
143 #ifdef sun
144 #include <sundev/tmreg.h>
145 #include <sundev/arreg.h>
146 #endif
147 
148 #ifdef tahoe
149 #include <tahoe/vba/cyreg.h>
150 #endif
151 
152 struct tape_desc {
153 	short	t_type;		/* type of magtape device */
154 	char	*t_name;	/* printing name */
155 	char	*t_dsbits;	/* "drive status" register */
156 	char	*t_erbits;	/* "error" register */
157 } tapes[] = {
158 #ifdef vax
159 	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
160 	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
161 	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
162 	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
163 	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
164 #endif
165 #ifdef sun
166 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
167 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
168 #endif
169 #ifdef tahoe
170 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
171 #endif
172 	{ 0 }
173 };
174 
175 /*
176  * Interpret the status buffer returned
177  */
178 status(bp)
179 	register struct mtget *bp;
180 {
181 	register struct tape_desc *mt;
182 
183 	for (mt = tapes; mt->t_type; mt++)
184 		if (mt->t_type == bp->mt_type)
185 			break;
186 	if (mt->t_type == 0) {
187 		printf("unknown tape drive type (%d)\n", bp->mt_type);
188 		return;
189 	}
190 	printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
191 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
192 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
193 	putchar('\n');
194 }
195 
196 /*
197  * Print a register a la the %b format of the kernel's printf
198  */
199 printreg(s, v, bits)
200 	char *s;
201 	register char *bits;
202 	register unsigned short v;
203 {
204 	register int i, any = 0;
205 	register char c;
206 
207 	if (bits && *bits == 8)
208 		printf("%s=%o", s, v);
209 	else
210 		printf("%s=%x", s, v);
211 	bits++;
212 	if (v && bits) {
213 		putchar('<');
214 		while (i = *bits++) {
215 			if (v & (1 << (i-1))) {
216 				if (any)
217 					putchar(',');
218 				any = 1;
219 				for (; (c = *bits) > 32; bits++)
220 					putchar(c);
221 			} else
222 				for (; *bits > 32; bits++)
223 					;
224 		}
225 		putchar('>');
226 	}
227 }
228