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