mt.c revision 1.17 1 /* $NetBSD: mt.c,v 1.17 1996/08/09 04:28:43 jtc 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)mt.c 8.2 (Berkeley) 6/6/93";
45 #else
46 static char rcsid[] = "$NetBSD: mt.c,v 1.17 1996/08/09 04:28:43 jtc Exp $";
47 #endif
48 #endif /* not lint */
49
50 /*
51 * mt --
52 * magnetic tape manipulation program
53 */
54 #include <rmt.h>
55 #include <sys/types.h>
56 #include <sys/ioctl.h>
57 #include <sys/mtio.h>
58 #include <sys/stat.h>
59
60 #include <ctype.h>
61 #include <err.h>
62 #include <fcntl.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67
68 /* pseudo ioctl constants */
69 #define MTASF 100
70
71 struct commands {
72 const char *c_name;
73 int c_code;
74 int c_ronly;
75 };
76
77 const struct commands com[] = {
78 { "asf", MTASF, 1 },
79 { "blocksize", MTSETBSIZ, 1 },
80 { "bsf", MTBSF, 1 },
81 { "bsr", MTBSR, 1 },
82 { "density", MTSETDNSTY, 1 },
83 { "eof", MTWEOF, 0 },
84 { "eom", MTEOM, 1 },
85 { "erase", MTERASE, 0 },
86 { "fsf", MTFSF, 1 },
87 { "fsr", MTFSR, 1 },
88 { "offline", MTOFFL, 1 },
89 { "rewind", MTREW, 1 },
90 { "rewoffl", MTOFFL, 1 },
91 { "status", MTNOP, 1 },
92 { "retension", MTRETEN, 1 },
93 { "weof", MTWEOF, 0 },
94 { NULL }
95 };
96
97 void printreg __P((char *, u_int, char *));
98 void status __P((struct mtget *));
99 void usage __P((void));
100
101 int
102 main(argc, argv)
103 int argc;
104 char *argv[];
105 {
106 register const struct commands *comp;
107 struct mtget mt_status;
108 struct mtop mt_com;
109 int ch, len, mtfd, flags;
110 char *p, *tape;
111 int count;
112
113 if ((tape = getenv("TAPE")) == NULL)
114 tape = DEFTAPE;
115
116 while ((ch = getopt(argc, argv, "f:t:")) != -1)
117 switch (ch) {
118 case 'f':
119 case 't':
120 tape = optarg;
121 break;
122 case '?':
123 default:
124 usage();
125 }
126 argc -= optind;
127 argv += optind;
128
129 if (argc < 1 || argc > 2)
130 usage();
131
132 len = strlen(p = *argv++);
133 for (comp = com;; comp++) {
134 if (comp->c_name == NULL)
135 errx(1, "%s: unknown command", p);
136 if (strncmp(p, comp->c_name, len) == 0)
137 break;
138 }
139
140 flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
141 if ((mtfd = open(tape, flags)) < 0)
142 err(2, "%s", tape);
143
144 if (comp->c_code == MTASF) {
145 /* If mtget.mt_fileno was implemented, We could
146 compute the minimal seek needed to position
147 the tape. Until then, rewind and seek from
148 begining-of-tape */
149
150 /* zero is a valid count */
151 if (*argv) {
152 count = strtol(*argv, &p, 10);
153 if (count < 0 || *p)
154 errx(2, "%s: illegal count", *argv);
155 }
156 else
157 count = 1;
158
159 mt_com.mt_op = MTREW;
160 mt_com.mt_count = 1;
161 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
162 err(2, "%s", tape);
163
164 mt_com.mt_op = MTFSF;
165 mt_com.mt_count = count;
166 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
167 err(2, "%s", tape);
168
169 } else if (comp->c_code != MTNOP) {
170
171 /* zero is *not* a valid count */
172 if (*argv) {
173 count = strtol(*argv, &p, 10);
174 if (count <= 0 || *p)
175 errx(2, "%s: illegal count", *argv);
176 }
177 else
178 count = 1;
179
180 mt_com.mt_op = comp->c_code;
181 mt_com.mt_count = count;
182
183 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
184 err(2, "%s: %s", tape, comp->c_name);
185
186 } else {
187 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
188 err(2, "%s: %s", tape, comp->c_name);
189 status(&mt_status);
190 }
191
192 exit(0);
193 /* NOTREACHED */
194 }
195
196 #ifdef sun
197 #include <sundev/tmreg.h>
198 #include <sundev/arreg.h>
199 #endif
200
201 #ifdef tahoe
202 #include <tahoe/vba/cyreg.h>
203 #endif
204
205 struct tape_desc {
206 short t_type; /* type of magtape device */
207 char *t_name; /* printing name */
208 char *t_dsbits; /* "drive status" register */
209 char *t_erbits; /* "error" register */
210 } tapes[] = {
211 #ifdef sun
212 { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
213 { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
214 #endif
215 #ifdef tahoe
216 { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS },
217 #endif
218 { 0x7, "SCSI", "76543210", "76543210" },
219 { 0 }
220 };
221
222 /*
223 * Interpret the status buffer returned
224 */
225 void
226 status(bp)
227 register struct mtget *bp;
228 {
229 register struct tape_desc *mt;
230
231 for (mt = tapes;; mt++) {
232 if (mt->t_type == 0) {
233 (void)printf("%d: unknown tape drive type\n",
234 bp->mt_type);
235 return;
236 }
237 if (mt->t_type == bp->mt_type)
238 break;
239 }
240 (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
241 printreg("ds", bp->mt_dsreg, mt->t_dsbits);
242 printreg("\ner", bp->mt_erreg, mt->t_erbits);
243 (void)putchar('\n');
244 (void)printf("blocksize: %ld (%ld, %ld, %ld, %ld)\n",
245 bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
246 bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
247 (void)printf("density: %ld (%ld, %ld, %ld, %ld)\n",
248 bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
249 bp->mt_mdensity[2], bp->mt_mdensity[3]);
250 }
251
252 /*
253 * Print a register a la the %b format of the kernel's printf.
254 */
255 void
256 printreg(s, v, bits)
257 char *s;
258 register u_int v;
259 register char *bits;
260 {
261 register int i, any = 0;
262 register char c;
263
264 if (bits && *bits == 8)
265 printf("%s=%o", s, v);
266 else
267 printf("%s=%x", s, v);
268 bits++;
269 if (v && bits) {
270 putchar('<');
271 while ((i = *bits++)) {
272 if (v & (1 << (i-1))) {
273 if (any)
274 putchar(',');
275 any = 1;
276 for (; (c = *bits) > 32; bits++)
277 putchar(c);
278 } else
279 for (; *bits > 32; bits++)
280 ;
281 }
282 putchar('>');
283 }
284 }
285
286 void
287 usage()
288 {
289 (void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
290 exit(1);
291 }
292