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