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