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