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