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