mt.c revision 1.37 1 1.37 agc /* $NetBSD: mt.c,v 1.37 2003/08/07 09:05:17 agc Exp $ */
2 1.5 cgd
3 1.1 cgd /*
4 1.5 cgd * Copyright (c) 1980, 1993
5 1.5 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.37 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.21 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.21 christos __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
35 1.21 christos The Regents of the University of California. All rights reserved.\n");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.5 cgd #if 0
40 1.8 tls static char sccsid[] = "@(#)mt.c 8.2 (Berkeley) 6/6/93";
41 1.5 cgd #else
42 1.37 agc __RCSID("$NetBSD: mt.c,v 1.37 2003/08/07 09:05:17 agc Exp $");
43 1.5 cgd #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd /*
47 1.1 cgd * mt --
48 1.1 cgd * magnetic tape manipulation program
49 1.1 cgd */
50 1.1 cgd #include <sys/types.h>
51 1.1 cgd #include <sys/ioctl.h>
52 1.1 cgd #include <sys/mtio.h>
53 1.12 scottr #include <sys/stat.h>
54 1.12 scottr
55 1.12 scottr #include <ctype.h>
56 1.12 scottr #include <err.h>
57 1.1 cgd #include <fcntl.h>
58 1.19 lukem #include <paths.h>
59 1.36 wiz #include <rmt.h>
60 1.12 scottr #include <stdio.h>
61 1.5 cgd #include <stdlib.h>
62 1.3 jtc #include <string.h>
63 1.11 scottr #include <unistd.h>
64 1.25 thorpej
65 1.16 jtc /* pseudo ioctl constants */
66 1.16 jtc #define MTASF 100
67 1.16 jtc
68 1.1 cgd struct commands {
69 1.20 hannken const char *c_name; /* command */
70 1.23 hannken int c_spcl; /* ioctl request */
71 1.23 hannken int c_code; /* ioctl code for MTIOCTOP command */
72 1.20 hannken int c_ronly; /* open tape read-only */
73 1.20 hannken int c_mincount; /* min allowed count value */
74 1.16 jtc };
75 1.16 jtc
76 1.16 jtc const struct commands com[] = {
77 1.33 christos { "asf", MTIOCTOP, MTASF, 1, 0 },
78 1.23 hannken { "blocksize", MTIOCTOP, MTSETBSIZ, 1, 0 },
79 1.23 hannken { "bsf", MTIOCTOP, MTBSF, 1, 1 },
80 1.23 hannken { "bsr", MTIOCTOP, MTBSR, 1, 1 },
81 1.23 hannken { "compress", MTIOCTOP, MTCMPRESS, 1, 0 },
82 1.23 hannken { "density", MTIOCTOP, MTSETDNSTY, 1, 0 },
83 1.23 hannken { "eof", MTIOCTOP, MTWEOF, 0, 1 },
84 1.23 hannken { "eom", MTIOCTOP, MTEOM, 1, 0 },
85 1.23 hannken { "erase", MTIOCTOP, MTERASE, 0, 0 },
86 1.23 hannken { "fsf", MTIOCTOP, MTFSF, 1, 1 },
87 1.23 hannken { "fsr", MTIOCTOP, MTFSR, 1, 1 },
88 1.23 hannken { "offline", MTIOCTOP, MTOFFL, 1, 0 },
89 1.23 hannken { "rdhpos", MTIOCRDHPOS, 0, 1, 0 },
90 1.23 hannken { "rdspos", MTIOCRDSPOS, 0, 1, 0 },
91 1.23 hannken { "retension", MTIOCTOP, MTRETEN, 1, 0 },
92 1.23 hannken { "rewind", MTIOCTOP, MTREW, 1, 0 },
93 1.23 hannken { "rewoffl", MTIOCTOP, MTOFFL, 1, 0 },
94 1.31 tron { "setblk", MTIOCTOP, MTSETBSIZ, 1, 0 },
95 1.31 tron { "setdensity", MTIOCTOP, MTSETDNSTY, 1, 0 },
96 1.23 hannken { "sethpos", MTIOCHLOCATE, 0, 1, 0 },
97 1.23 hannken { "setspos", MTIOCSLOCATE, 0, 1, 0 },
98 1.23 hannken { "status", MTIOCGET, MTNOP, 1, 0 },
99 1.23 hannken { "weof", MTIOCTOP, MTWEOF, 0, 1 },
100 1.29 mjacob { "eew", MTIOCTOP, MTEWARN, 1, 0 },
101 1.5 cgd { NULL }
102 1.1 cgd };
103 1.1 cgd
104 1.36 wiz void printreg(const char *, u_int, const char *);
105 1.36 wiz void status(struct mtget *);
106 1.36 wiz void usage(void);
107 1.36 wiz int main(int, char *[]);
108 1.1 cgd
109 1.3 jtc int
110 1.36 wiz main(int argc, char *argv[])
111 1.1 cgd {
112 1.22 mjacob const struct commands *comp = (const struct commands *) NULL;
113 1.5 cgd struct mtget mt_status;
114 1.5 cgd struct mtop mt_com;
115 1.12 scottr int ch, len, mtfd, flags;
116 1.27 mycroft char *p;
117 1.27 mycroft const char *tape;
118 1.24 veego int count;
119 1.1 cgd
120 1.36 wiz setprogname(argv[0]);
121 1.5 cgd if ((tape = getenv("TAPE")) == NULL)
122 1.19 lukem tape = _PATH_DEFTAPE;
123 1.5 cgd
124 1.5 cgd while ((ch = getopt(argc, argv, "f:t:")) != -1)
125 1.5 cgd switch (ch) {
126 1.3 jtc case 'f':
127 1.3 jtc case 't':
128 1.3 jtc tape = optarg;
129 1.3 jtc break;
130 1.5 cgd case '?':
131 1.3 jtc default:
132 1.3 jtc usage();
133 1.3 jtc }
134 1.3 jtc argc -= optind;
135 1.3 jtc argv += optind;
136 1.3 jtc
137 1.5 cgd if (argc < 1 || argc > 2)
138 1.3 jtc usage();
139 1.3 jtc
140 1.23 hannken len = strlen(p = *argv++);
141 1.23 hannken for (comp = com;; comp++) {
142 1.23 hannken if (comp->c_name == NULL)
143 1.23 hannken errx(1, "%s: unknown command", p);
144 1.23 hannken if (strncmp(p, comp->c_name, len) == 0)
145 1.23 hannken break;
146 1.23 hannken }
147 1.22 mjacob
148 1.23 hannken if (*argv) {
149 1.23 hannken count = strtol(*argv, &p, 10);
150 1.23 hannken if (count < comp->c_mincount || *p)
151 1.23 hannken errx(2, "%s: illegal count", *argv);
152 1.23 hannken } else
153 1.23 hannken count = 1;
154 1.12 scottr
155 1.23 hannken flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
156 1.20 hannken
157 1.17 jtc if ((mtfd = open(tape, flags)) < 0)
158 1.7 pk err(2, "%s", tape);
159 1.16 jtc
160 1.23 hannken switch (comp->c_spcl) {
161 1.23 hannken case MTIOCTOP:
162 1.23 hannken if (comp->c_code == MTASF) {
163 1.35 mason
164 1.23 hannken /* If mtget.mt_fileno was implemented, We could
165 1.23 hannken compute the minimal seek needed to position
166 1.23 hannken the tape. Until then, rewind and seek from
167 1.23 hannken begining-of-tape */
168 1.23 hannken
169 1.23 hannken mt_com.mt_op = MTREW;
170 1.23 hannken mt_com.mt_count = 1;
171 1.23 hannken if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
172 1.23 hannken err(2, "%s", tape);
173 1.23 hannken
174 1.33 christos if (count > 0) {
175 1.33 christos mt_com.mt_op = MTFSF;
176 1.33 christos mt_com.mt_count = count;
177 1.33 christos if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
178 1.33 christos err(2, "%s", tape);
179 1.33 christos }
180 1.34 mason
181 1.22 mjacob } else {
182 1.23 hannken mt_com.mt_op = comp->c_code;
183 1.23 hannken mt_com.mt_count = count;
184 1.16 jtc
185 1.23 hannken if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
186 1.23 hannken err(2, "%s: %s", tape, comp->c_name);
187 1.16 jtc
188 1.23 hannken }
189 1.23 hannken break;
190 1.16 jtc
191 1.23 hannken case MTIOCGET:
192 1.17 jtc if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
193 1.17 jtc err(2, "%s: %s", tape, comp->c_name);
194 1.17 jtc status(&mt_status);
195 1.23 hannken break;
196 1.23 hannken
197 1.23 hannken case MTIOCRDSPOS:
198 1.23 hannken case MTIOCRDHPOS:
199 1.23 hannken if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
200 1.23 hannken err(2, "%s", tape);
201 1.23 hannken printf("%s: block location %u\n", tape, (unsigned int) count);
202 1.23 hannken break;
203 1.23 hannken
204 1.23 hannken case MTIOCSLOCATE:
205 1.23 hannken case MTIOCHLOCATE:
206 1.23 hannken if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
207 1.23 hannken err(2, "%s", tape);
208 1.23 hannken break;
209 1.23 hannken
210 1.23 hannken default:
211 1.23 hannken errx(1, "internal error: unknown request %d", comp->c_spcl);
212 1.1 cgd }
213 1.9 scottr
214 1.17 jtc exit(0);
215 1.5 cgd /* NOTREACHED */
216 1.1 cgd }
217 1.1 cgd
218 1.30 christos #if defined(sun) && !defined(__SVR4)
219 1.1 cgd #include <sundev/tmreg.h>
220 1.1 cgd #include <sundev/arreg.h>
221 1.1 cgd #endif
222 1.1 cgd
223 1.1 cgd #ifdef tahoe
224 1.1 cgd #include <tahoe/vba/cyreg.h>
225 1.1 cgd #endif
226 1.1 cgd
227 1.27 mycroft const struct tape_desc {
228 1.1 cgd short t_type; /* type of magtape device */
229 1.27 mycroft const char *t_name; /* printing name */
230 1.27 mycroft const char *t_dsbits; /* "drive status" register */
231 1.27 mycroft const char *t_erbits; /* "error" register */
232 1.1 cgd } tapes[] = {
233 1.30 christos #if defined(sun) && !defined(__SVR4)
234 1.1 cgd { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
235 1.1 cgd { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
236 1.1 cgd #endif
237 1.1 cgd #ifdef tahoe
238 1.1 cgd { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS },
239 1.1 cgd #endif
240 1.26 mjacob #define SCSI_DS_BITS "\20\5WriteProtect\2Mounted"
241 1.26 mjacob { 0x7, "SCSI", SCSI_DS_BITS, "76543210" },
242 1.1 cgd { 0 }
243 1.1 cgd };
244 1.1 cgd
245 1.26 mjacob
246 1.1 cgd /*
247 1.1 cgd * Interpret the status buffer returned
248 1.1 cgd */
249 1.5 cgd void
250 1.36 wiz status(struct mtget *bp)
251 1.1 cgd {
252 1.27 mycroft const struct tape_desc *mt;
253 1.1 cgd
254 1.5 cgd for (mt = tapes;; mt++) {
255 1.5 cgd if (mt->t_type == 0) {
256 1.5 cgd (void)printf("%d: unknown tape drive type\n",
257 1.5 cgd bp->mt_type);
258 1.5 cgd return;
259 1.5 cgd }
260 1.1 cgd if (mt->t_type == bp->mt_type)
261 1.1 cgd break;
262 1.1 cgd }
263 1.5 cgd (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
264 1.1 cgd printreg("ds", bp->mt_dsreg, mt->t_dsbits);
265 1.1 cgd printreg("\ner", bp->mt_erreg, mt->t_erbits);
266 1.5 cgd (void)putchar('\n');
267 1.21 christos (void)printf("blocksize: %d (%d, %d, %d, %d)\n",
268 1.14 mrg bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
269 1.14 mrg bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
270 1.21 christos (void)printf("density: %d (%d, %d, %d, %d)\n",
271 1.14 mrg bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
272 1.14 mrg bp->mt_mdensity[2], bp->mt_mdensity[3]);
273 1.32 simonb (void)printf("current file number: %d\n", bp->mt_fileno);
274 1.32 simonb (void)printf("current block number: %d\n", bp->mt_blkno);
275 1.1 cgd }
276 1.1 cgd
277 1.1 cgd /*
278 1.5 cgd * Print a register a la the %b format of the kernel's printf.
279 1.1 cgd */
280 1.5 cgd void
281 1.36 wiz printreg(const char *s, u_int v, const char *bits)
282 1.1 cgd {
283 1.36 wiz int any, i;
284 1.18 tls char c;
285 1.1 cgd
286 1.36 wiz any = 0;
287 1.1 cgd if (bits && *bits == 8)
288 1.1 cgd printf("%s=%o", s, v);
289 1.1 cgd else
290 1.1 cgd printf("%s=%x", s, v);
291 1.1 cgd bits++;
292 1.26 mjacob if (v && *bits) {
293 1.1 cgd putchar('<');
294 1.11 scottr while ((i = *bits++)) {
295 1.1 cgd if (v & (1 << (i-1))) {
296 1.1 cgd if (any)
297 1.1 cgd putchar(',');
298 1.1 cgd any = 1;
299 1.1 cgd for (; (c = *bits) > 32; bits++)
300 1.1 cgd putchar(c);
301 1.1 cgd } else
302 1.1 cgd for (; *bits > 32; bits++)
303 1.1 cgd ;
304 1.1 cgd }
305 1.1 cgd putchar('>');
306 1.1 cgd }
307 1.3 jtc }
308 1.3 jtc
309 1.5 cgd void
310 1.36 wiz usage(void)
311 1.3 jtc {
312 1.36 wiz (void)fprintf(stderr, "usage: %s [-f device] command [ count ]\n", getprogname());
313 1.17 jtc exit(1);
314 1.28 mycroft /* NOTREACHED */
315 1.1 cgd }
316