mt.c revision 1.3 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.3 1994/03/30 01:50:43 jtc 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 { 0 }
79 };
80
81 int mtfd;
82 struct mtop mt_com;
83 struct mtget mt_status;
84 char *tape = NULL;
85
86 int
87 main(argc, argv)
88 int argc;
89 char **argv;
90 {
91 register char *cp;
92 register struct commands *comp;
93 int c;
94
95 while ((c = getopt(argc, argv, "f:t:")) != -1) {
96 switch (c) {
97 case 'f':
98 case 't':
99 tape = optarg;
100 break;
101 default:
102 usage();
103 /* NOTREACHED */
104 }
105 }
106 argc -= optind;
107 argv += optind;
108
109 if (argc < 1 || argc > 2) {
110 usage();
111 /* NOTREACHED */
112 }
113
114 cp = argv[0];
115 for (comp = com; comp->c_name != NULL; comp++)
116 if (strncmp(cp, comp->c_name, strlen(cp)) == 0)
117 break;
118 if (comp->c_name == NULL) {
119 errx(1, "don't grok \"%s\"", cp);
120 /* NOTREACHED */
121 }
122
123 if (tape == NULL) {
124 if ((tape = getenv("TAPE")) == NULL)
125 tape = DEFTAPE;
126 }
127
128 if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0) {
129 err(1, "%s", tape);
130 /* NOTREACHED */
131 }
132 if (comp->c_code != MTNOP) {
133 mt_com.mt_op = comp->c_code;
134 mt_com.mt_count = (argc == 2 ? atoi(argv[1]) : 1);
135 if (mt_com.mt_count < 0) {
136 errx(1, "negative repeat count");
137 /* NOTREACHED */
138 }
139 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
140 err(2, "%s %s %d failed",
141 tape, comp->c_name, mt_com.mt_count);
142 /* NOTREACHED */
143 }
144 } else {
145 if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
146 err(2, NULL);
147 /* NOTREACHED */
148 }
149 status(&mt_status);
150 }
151
152 exit(0);
153 }
154
155 #ifdef vax
156 #include <vaxmba/mtreg.h>
157 #include <vaxmba/htreg.h>
158
159 #include <vaxuba/utreg.h>
160 #include <vaxuba/tmreg.h>
161 #undef b_repcnt /* argh */
162 #include <vaxuba/tsreg.h>
163 #endif
164
165 #ifdef sun
166 #include <sundev/tmreg.h>
167 #include <sundev/arreg.h>
168 #endif
169
170 #ifdef tahoe
171 #include <tahoe/vba/cyreg.h>
172 #endif
173
174 struct tape_desc {
175 short t_type; /* type of magtape device */
176 char *t_name; /* printing name */
177 char *t_dsbits; /* "drive status" register */
178 char *t_erbits; /* "error" register */
179 } tapes[] = {
180 #ifdef vax
181 { MT_ISTS, "ts11", 0, TSXS0_BITS },
182 { MT_ISHT, "tm03", HTDS_BITS, HTER_BITS },
183 { MT_ISTM, "tm11", 0, TMER_BITS },
184 { MT_ISMT, "tu78", MTDS_BITS, 0 },
185 { MT_ISUT, "tu45", UTDS_BITS, UTER_BITS },
186 #endif
187 #ifdef sun
188 { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
189 { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
190 #endif
191 #ifdef tahoe
192 { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS },
193 #endif
194 { 0 }
195 };
196
197 /*
198 * Interpret the status buffer returned
199 */
200 static void
201 status(bp)
202 register struct mtget *bp;
203 {
204 register struct tape_desc *mt;
205
206 for (mt = tapes; mt->t_type; mt++)
207 if (mt->t_type == bp->mt_type)
208 break;
209 if (mt->t_type == 0) {
210 printf("unknown tape drive type (%d)\n", bp->mt_type);
211 return;
212 }
213 printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
214 printreg("ds", bp->mt_dsreg, mt->t_dsbits);
215 printreg("\ner", bp->mt_erreg, mt->t_erbits);
216 putchar('\n');
217 }
218
219 /*
220 * Print a register a la the %b format of the kernel's printf
221 */
222 static void
223 printreg(s, v, bits)
224 char *s;
225 register char *bits;
226 register unsigned short v;
227 {
228 register int i, any = 0;
229 register char c;
230
231 if (bits && *bits == 8)
232 printf("%s=%o", s, v);
233 else
234 printf("%s=%x", s, v);
235 bits++;
236 if (v && bits) {
237 putchar('<');
238 while (i = *bits++) {
239 if (v & (1 << (i-1))) {
240 if (any)
241 putchar(',');
242 any = 1;
243 for (; (c = *bits) > 32; bits++)
244 putchar(c);
245 } else
246 for (; *bits > 32; bits++)
247 ;
248 }
249 putchar('>');
250 }
251 }
252
253 static void
254 usage()
255 {
256 fprintf(stderr, "usage: mt [ -f device ] command [ count ]\n");
257 exit(1);
258 }
259