mt.c revision 1.25 1 /* $NetBSD: mt.c,v 1.25 1997/10/21 20:04:55 thorpej 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: mt.c,v 1.25 1997/10/21 20:04:55 thorpej Exp $");
47 #endif
48 #endif /* not lint */
49
50 /*
51 * mt --
52 * magnetic tape manipulation program
53 */
54 #include <sys/types.h>
55 #include <sys/ioctl.h>
56 #include <sys/mtio.h>
57 #include <sys/stat.h>
58
59 #include <ctype.h>
60 #include <err.h>
61 #include <fcntl.h>
62 #include <paths.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67
68 #include <rmt.h>
69
70 /* pseudo ioctl constants */
71 #define MTASF 100
72
73 struct commands {
74 const char *c_name; /* command */
75 int c_spcl; /* ioctl request */
76 int c_code; /* ioctl code for MTIOCTOP command */
77 int c_ronly; /* open tape read-only */
78 int c_mincount; /* min allowed count value */
79 };
80
81 const struct commands com[] = {
82 { "asf", MTIOCTOP, MTASF, 1, 1 },
83 { "blocksize", MTIOCTOP, MTSETBSIZ, 1, 0 },
84 { "bsf", MTIOCTOP, MTBSF, 1, 1 },
85 { "bsr", MTIOCTOP, MTBSR, 1, 1 },
86 { "compress", MTIOCTOP, MTCMPRESS, 1, 0 },
87 { "density", MTIOCTOP, MTSETDNSTY, 1, 0 },
88 { "eof", MTIOCTOP, MTWEOF, 0, 1 },
89 { "eom", MTIOCTOP, MTEOM, 1, 0 },
90 { "erase", MTIOCTOP, MTERASE, 0, 0 },
91 { "fsf", MTIOCTOP, MTFSF, 1, 1 },
92 { "fsr", MTIOCTOP, MTFSR, 1, 1 },
93 { "offline", MTIOCTOP, MTOFFL, 1, 0 },
94 { "rdhpos", MTIOCRDHPOS, 0, 1, 0 },
95 { "rdspos", MTIOCRDSPOS, 0, 1, 0 },
96 { "retension", MTIOCTOP, MTRETEN, 1, 0 },
97 { "rewind", MTIOCTOP, MTREW, 1, 0 },
98 { "rewoffl", MTIOCTOP, MTOFFL, 1, 0 },
99 { "sethpos", MTIOCHLOCATE, 0, 1, 0 },
100 { "setspos", MTIOCSLOCATE, 0, 1, 0 },
101 { "status", MTIOCGET, MTNOP, 1, 0 },
102 { "weof", MTIOCTOP, MTWEOF, 0, 1 },
103 { NULL }
104 };
105
106 void printreg __P((char *, u_int, char *));
107 void status __P((struct mtget *));
108 void usage __P((void));
109 int main __P((int, char *[]));
110
111 int
112 main(argc, argv)
113 int argc;
114 char *argv[];
115 {
116 const struct commands *comp = (const struct commands *) NULL;
117 struct mtget mt_status;
118 struct mtop mt_com;
119 int ch, len, mtfd, flags;
120 char *p, *tape;
121 int count;
122
123 if ((tape = getenv("TAPE")) == NULL)
124 tape = _PATH_DEFTAPE;
125
126 while ((ch = getopt(argc, argv, "f:t:")) != -1)
127 switch (ch) {
128 case 'f':
129 case 't':
130 tape = optarg;
131 break;
132 case '?':
133 default:
134 usage();
135 }
136 argc -= optind;
137 argv += optind;
138
139 if (argc < 1 || argc > 2)
140 usage();
141
142 len = strlen(p = *argv++);
143 for (comp = com;; comp++) {
144 if (comp->c_name == NULL)
145 errx(1, "%s: unknown command", p);
146 if (strncmp(p, comp->c_name, len) == 0)
147 break;
148 }
149
150 if (*argv) {
151 count = strtol(*argv, &p, 10);
152 if (count < comp->c_mincount || *p)
153 errx(2, "%s: illegal count", *argv);
154 } else
155 count = 1;
156
157 flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
158
159 if ((mtfd = open(tape, flags)) < 0)
160 err(2, "%s", tape);
161
162 switch (comp->c_spcl) {
163 case MTIOCTOP:
164 if (comp->c_code == MTASF) {
165 /* If mtget.mt_fileno was implemented, We could
166 compute the minimal seek needed to position
167 the tape. Until then, rewind and seek from
168 begining-of-tape */
169
170 mt_com.mt_op = MTREW;
171 mt_com.mt_count = 1;
172 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
173 err(2, "%s", tape);
174
175 mt_com.mt_op = MTFSF;
176 mt_com.mt_count = count;
177 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
178 err(2, "%s", tape);
179
180 } else {
181 mt_com.mt_op = comp->c_code;
182 mt_com.mt_count = count;
183
184 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
185 err(2, "%s: %s", tape, comp->c_name);
186
187 }
188 break;
189
190 case MTIOCGET:
191 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
192 err(2, "%s: %s", tape, comp->c_name);
193 status(&mt_status);
194 break;
195
196 case MTIOCRDSPOS:
197 case MTIOCRDHPOS:
198 if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
199 err(2, "%s", tape);
200 printf("%s: block location %u\n", tape, (unsigned int) count);
201 break;
202
203 case MTIOCSLOCATE:
204 case MTIOCHLOCATE:
205 if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
206 err(2, "%s", tape);
207 break;
208
209 default:
210 errx(1, "internal error: unknown request %d", comp->c_spcl);
211 }
212
213 exit(0);
214 /* NOTREACHED */
215 }
216
217 #ifdef sun
218 #include <sundev/tmreg.h>
219 #include <sundev/arreg.h>
220 #endif
221
222 #ifdef tahoe
223 #include <tahoe/vba/cyreg.h>
224 #endif
225
226 struct tape_desc {
227 short t_type; /* type of magtape device */
228 char *t_name; /* printing name */
229 char *t_dsbits; /* "drive status" register */
230 char *t_erbits; /* "error" register */
231 } tapes[] = {
232 #ifdef sun
233 { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
234 { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
235 #endif
236 #ifdef tahoe
237 { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS },
238 #endif
239 { 0x7, "SCSI", "76543210", "76543210" },
240 { 0 }
241 };
242
243 /*
244 * Interpret the status buffer returned
245 */
246 void
247 status(bp)
248 struct mtget *bp;
249 {
250 struct tape_desc *mt;
251
252 for (mt = tapes;; mt++) {
253 if (mt->t_type == 0) {
254 (void)printf("%d: unknown tape drive type\n",
255 bp->mt_type);
256 return;
257 }
258 if (mt->t_type == bp->mt_type)
259 break;
260 }
261 (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
262 printreg("ds", bp->mt_dsreg, mt->t_dsbits);
263 printreg("\ner", bp->mt_erreg, mt->t_erbits);
264 (void)putchar('\n');
265 (void)printf("blocksize: %d (%d, %d, %d, %d)\n",
266 bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
267 bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
268 (void)printf("density: %d (%d, %d, %d, %d)\n",
269 bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
270 bp->mt_mdensity[2], bp->mt_mdensity[3]);
271 }
272
273 /*
274 * Print a register a la the %b format of the kernel's printf.
275 */
276 void
277 printreg(s, v, bits)
278 char *s;
279 u_int v;
280 char *bits;
281 {
282 int i, any = 0;
283 char c;
284
285 if (bits && *bits == 8)
286 printf("%s=%o", s, v);
287 else
288 printf("%s=%x", s, v);
289 bits++;
290 if (v && bits) {
291 putchar('<');
292 while ((i = *bits++)) {
293 if (v & (1 << (i-1))) {
294 if (any)
295 putchar(',');
296 any = 1;
297 for (; (c = *bits) > 32; bits++)
298 putchar(c);
299 } else
300 for (; *bits > 32; bits++)
301 ;
302 }
303 putchar('>');
304 }
305 }
306
307 void
308 usage()
309 {
310 (void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
311 exit(1);
312 }
313