mt.c revision 7913
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 35static 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 41static 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/* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */ 59#if defined(__FreeBSD__) 60/* c_flags */ 61#define NEED_2ARGS 0x01 62#define ZERO_ALLOWED 0X02 63#endif /* defined(__FreeBSD__) */ 64 65struct commands { 66 char *c_name; 67 int c_code; 68 int c_ronly; 69#if defined(__FreeBSD__) 70 int c_flags; 71#endif /* defined(__FreeBSD__) */ 72} com[] = { 73 { "bsf", MTBSF, 1 }, 74 { "bsr", MTBSR, 1 }, 75 { "eof", MTWEOF, 0 }, 76 { "fsf", MTFSF, 1 }, 77 { "fsr", MTFSR, 1 }, 78 { "offline", MTOFFL, 1 }, 79 { "rewind", MTREW, 1 }, 80 { "rewoffl", MTOFFL, 1 }, 81 { "status", MTNOP, 1 }, 82 { "weof", MTWEOF, 0 }, 83#if defined(__FreeBSD__) 84 { "erase", MTERASE, 0 }, 85 { "blocksize", MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED }, 86 { "density", MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED }, 87 { "eom", MTEOD, 1 }, 88 { "comp", MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED }, 89#endif /* defined(__FreeBSD__) */ 90 { NULL } 91}; 92 93void err __P((const char *, ...)); 94void printreg __P((char *, u_int, char *)); 95void status __P((struct mtget *)); 96void usage __P((void)); 97#if defined (__FreeBSD__) 98void st_status (struct mtget *); 99#endif /* defined (__FreeBSD__) */ 100 101int 102main(argc, argv) 103 int argc; 104 char *argv[]; 105{ 106 register struct commands *comp; 107 struct mtget mt_status; 108 struct mtop mt_com; 109 int ch, len, mtfd; 110 char *p, *tape; 111 112 if ((tape = getenv("TAPE")) == NULL) 113 tape = DEFTAPE; 114 115 while ((ch = getopt(argc, argv, "f:t:")) != EOF) 116 switch(ch) { 117 case 'f': 118 case 't': 119 tape = optarg; 120 break; 121 case '?': 122 default: 123 usage(); 124 } 125 argc -= optind; 126 argv += optind; 127 128 if (argc < 1 || argc > 2) 129 usage(); 130 131 len = strlen(p = *argv++); 132 for (comp = com;; comp++) { 133 if (comp->c_name == NULL) 134 err("%s: unknown command", p); 135 if (strncmp(p, comp->c_name, len) == 0) 136 break; 137 } 138#if defined(__FreeBSD__) 139 if((comp->c_flags & NEED_2ARGS) && argc != 2) 140 usage(); 141#endif /* defined(__FreeBSD__) */ 142 if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0) 143 err("%s: %s", tape, strerror(errno)); 144 if (comp->c_code != MTNOP) { 145 mt_com.mt_op = comp->c_code; 146 if (*argv) { 147#if defined (__FreeBSD__) 148 /* allow for hex numbers; useful for density */ 149 mt_com.mt_count = strtol(*argv, &p, 0); 150#else 151 mt_com.mt_count = strtol(*argv, &p, 10); 152#endif /* defined(__FreeBSD__) */ 153 if (mt_com.mt_count <= 154#if defined (__FreeBSD__) 155 ((comp->c_flags & ZERO_ALLOWED)? -1: 0) 156#else 157 0 158#endif /* defined (__FreeBSD__) */ 159 || *p) 160 err("%s: illegal count", *argv); 161 } 162 else 163 mt_com.mt_count = 1; 164 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) 165 err("%s: %s: %s", tape, comp->c_name, strerror(errno)); 166 } else { 167 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0) 168 err("%s", strerror(errno)); 169 status(&mt_status); 170 } 171 exit (0); 172 /* NOTREACHED */ 173} 174 175#ifdef vax 176#include <vax/mba/mtreg.h> 177#include <vax/mba/htreg.h> 178 179#include <vax/uba/utreg.h> 180#include <vax/uba/tmreg.h> 181#undef b_repcnt /* argh */ 182#include <vax/uba/tsreg.h> 183#endif 184 185#ifdef sun 186#include <sundev/tmreg.h> 187#include <sundev/arreg.h> 188#endif 189 190#ifdef tahoe 191#include <tahoe/vba/cyreg.h> 192#endif 193 194struct tape_desc { 195 short t_type; /* type of magtape device */ 196 char *t_name; /* printing name */ 197 char *t_dsbits; /* "drive status" register */ 198 char *t_erbits; /* "error" register */ 199} tapes[] = { 200#ifdef vax 201 { MT_ISTS, "ts11", 0, TSXS0_BITS }, 202 { MT_ISHT, "tm03", HTDS_BITS, HTER_BITS }, 203 { MT_ISTM, "tm11", 0, TMER_BITS }, 204 { MT_ISMT, "tu78", MTDS_BITS, 0 }, 205 { MT_ISUT, "tu45", UTDS_BITS, UTER_BITS }, 206#endif 207#ifdef sun 208 { MT_ISCPC, "TapeMaster", TMS_BITS, 0 }, 209 { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS }, 210#endif 211#ifdef tahoe 212 { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS }, 213#endif 214#if defined (__FreeBSD__) 215 /* 216 * XXX This is terrific. The st driver reports the tape drive 217 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver 218 * either reports MT_ISVIPER1 for an Archive tape, or 0x11 219 * (MT_ISMFOUR) for other tapes. 220 * XXX for the wt driver, rely on it behaving like a "standard" 221 * magtape driver. 222 */ 223 { MT_ISAR, "SCSI tape drive", 0, 0 }, 224 { MT_ISVIPER1, "Archive Viper", 0, 0 }, 225 { MT_ISMFOUR, "Wangtek", 0, 0 }, 226#endif /* defined (__FreeBSD__) */ 227 { 0 } 228}; 229 230/* 231 * Interpret the status buffer returned 232 */ 233void 234status(bp) 235 register struct mtget *bp; 236{ 237 register struct tape_desc *mt; 238 239 for (mt = tapes;; mt++) { 240 if (mt->t_type == 0) { 241 (void)printf("%d: unknown tape drive type\n", 242 bp->mt_type); 243 return; 244 } 245 if (mt->t_type == bp->mt_type) 246 break; 247 } 248#if defined (__FreeBSD__) 249 if(mt->t_type == MT_ISAR) 250 st_status(bp); 251 else { 252#endif /* defined (__FreeBSD__) */ 253 (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid); 254 printreg("ds", bp->mt_dsreg, mt->t_dsbits); 255 printreg("\ner", bp->mt_erreg, mt->t_erbits); 256 (void)putchar('\n'); 257#if defined (__FreeBSD__) 258 } 259#endif /* defined (__FreeBSD__) */ 260} 261 262/* 263 * Print a register a la the %b format of the kernel's printf. 264 */ 265void 266printreg(s, v, bits) 267 char *s; 268 register u_int v; 269 register char *bits; 270{ 271 register int i, any = 0; 272 register char c; 273 274 if (bits && *bits == 8) 275 printf("%s=%o", s, v); 276 else 277 printf("%s=%x", s, v); 278 bits++; 279 if (v && bits) { 280 putchar('<'); 281 while (i = *bits++) { 282 if (v & (1 << (i-1))) { 283 if (any) 284 putchar(','); 285 any = 1; 286 for (; (c = *bits) > 32; bits++) 287 putchar(c); 288 } else 289 for (; *bits > 32; bits++) 290 ; 291 } 292 putchar('>'); 293 } 294} 295 296void 297usage() 298{ 299 (void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n"); 300 exit(1); 301} 302 303#if __STDC__ 304#include <stdarg.h> 305#else 306#include <varargs.h> 307#endif 308 309void 310#if __STDC__ 311err(const char *fmt, ...) 312#else 313err(fmt, va_alist) 314 char *fmt; 315 va_dcl 316#endif 317{ 318 va_list ap; 319#if __STDC__ 320 va_start(ap, fmt); 321#else 322 va_start(ap); 323#endif 324 (void)fprintf(stderr, "mt: "); 325 (void)vfprintf(stderr, fmt, ap); 326 va_end(ap); 327 (void)fprintf(stderr, "\n"); 328 exit(1); 329 /* NOTREACHED */ 330} 331 332#if defined (__FreeBSD__) 333 334struct densities { 335 int dens; 336 const char *name; 337} dens [] = { 338 { 0x1, "X3.22-1983 " }, 339 { 0x2, "X3.39-1986 " }, 340 { 0x3, "X3.54-1986 " }, 341 { 0x5, "X3.136-1986 " }, 342 { 0x6, "X3.157-1987 " }, 343 { 0x7, "X3.116-1986 " }, 344 { 0x8, "X3.158-1986 " }, 345 { 0x9, "X3B5/87-099 " }, 346 { 0xA, "X3B5/86-199 " }, 347 { 0xB, "X3.56-1986 " }, 348 { 0xC, "HI-TC1 " }, 349 { 0xD, "HI-TC2 " }, 350 { 0xF, "QIC-120 " }, 351 { 0x10, "QIC-150 " }, 352 { 0x11, "QIC-320 " }, 353 { 0x12, "QIC-1350 " }, 354 { 0x13, "X3B5/88-185A" }, 355 { 0x14, "X3.202-1991 " }, 356 { 0x15, "ECMA TC17 " }, 357 { 0x16, "X3.193-1990 " }, 358 { 0x17, "X3B5/91-174 " }, 359 { 0, 0 } 360}; 361 362const char * 363getdens(int d) 364{ 365 static char buf[20]; 366 struct densities *sd; 367 368 for (sd = dens; sd->dens; sd++) 369 if (sd->dens == d) 370 break; 371 if (sd->dens == 0) { 372 sprintf(buf, "0x%02x ", d); 373 return buf; 374 } 375 else 376 return sd->name; 377} 378 379const char * 380getblksiz(int bs) 381{ 382 static char buf[25]; 383 if (bs == 0) 384 return "variable"; 385 else { 386 sprintf(buf, "= %d bytes", bs); 387 return buf; 388 } 389} 390 391 392void 393st_status(struct mtget *bp) 394{ 395 printf("Present Mode: Density = %s Blocksize %s\n", 396 getdens(bp->mt_density), getblksiz(bp->mt_blksiz)); 397 printf("---------available modes---------\n"); 398 printf("Mode 0: Density = %s Blocksize %s\n", 399 getdens(bp->mt_density0), getblksiz(bp->mt_blksiz0)); 400 printf("Mode 1: Density = %s Blocksize %s\n", 401 getdens(bp->mt_density1), getblksiz(bp->mt_blksiz1)); 402 printf("Mode 2: Density = %s Blocksize %s\n", 403 getdens(bp->mt_density2), getblksiz(bp->mt_blksiz2)); 404 printf("Mode 3: Density = %s Blocksize %s\n", 405 getdens(bp->mt_density3), getblksiz(bp->mt_blksiz3)); 406} 407 408#endif /* defined (__FreeBSD__) */ 409