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 if (!bits) 306 return; 307 bits++; 308 if (v && bits) { 309 putchar('<'); 310 while (i = *bits++) { 311 if (v & (1 << (i-1))) { 312 if (any) 313 putchar(','); 314 any = 1; 315 for (; (c = *bits) > 32; bits++) 316 putchar(c); 317 } else 318 for (; *bits > 32; bits++) 319 ; 320 } 321 putchar('>'); 322 } 323 } 324 325 void 326 usage() 327 { 328 (void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n"); 329 exit(1); 330 } 331 332 #if __STDC__ 333 #include <stdarg.h> 334 #else 335 #include <varargs.h> 336 #endif 337 338 void 339 #if __STDC__ 340 err(const char *fmt, ...) 341 #else 342 err(fmt, va_alist) 343 char *fmt; 344 va_dcl 345 #endif 346 { 347 va_list ap; 348 #if __STDC__ 349 va_start(ap, fmt); 350 #else 351 va_start(ap); 352 #endif 353 (void)fprintf(stderr, "mt: "); 354 (void)vfprintf(stderr, fmt, ap); 355 va_end(ap); 356 (void)fprintf(stderr, "\n"); 357 exit(1); 358 /* NOTREACHED */ 359 } 360 361 #if defined (__FreeBSD__) 362 363 struct densities { 364 int dens; 365 const char *name; 366 } dens [] = { 367 { 0x1, "X3.22-1983" }, 368 { 0x2, "X3.39-1986" }, 369 { 0x3, "X3.54-1986" }, 370 { 0x5, "X3.136-1986" }, 371 { 0x6, "X3.157-1987" }, 372 { 0x7, "X3.116-1986" }, 373 { 0x8, "X3.158-1986" }, 374 { 0x9, "X3B5/87-099" }, 375 { 0xA, "X3B5/86-199" }, 376 { 0xB, "X3.56-1986" }, 377 { 0xC, "HI-TC1" }, 378 { 0xD, "HI-TC2" }, 379 { 0xF, "QIC-120" }, 380 { 0x10, "QIC-150" }, 381 { 0x11, "QIC-320" }, 382 { 0x12, "QIC-1350" }, 383 { 0x13, "X3B5/88-185A" }, 384 { 0x14, "X3.202-1991" }, 385 { 0x15, "ECMA TC17" }, 386 { 0x16, "X3.193-1990" }, 387 { 0x17, "X3B5/91-174" }, 388 { 0, 0 } 389 }; 390 391 const char * 392 denstostring(int d) 393 { 394 static char buf[20]; 395 struct densities *sd; 396 397 for (sd = dens; sd->dens; sd++) 398 if (sd->dens == d) 399 break; 400 if (sd->dens == 0) { 401 sprintf(buf, "0x%02x", d); 402 return buf; 403 } else 404 return sd->name; 405 } 406 407 int 408 stringtodens(const char *s) 409 { 410 struct densities *sd; 411 size_t l = strlen(s); 412 413 for (sd = dens; sd->dens; sd++) 414 if (strncasecmp(sd->name, s, l) == 0) 415 break; 416 return sd->dens; 417 } 418 419 420 const char * 421 getblksiz(int bs) 422 { 423 static char buf[25]; 424 if (bs == 0) 425 return "variable"; 426 else { 427 sprintf(buf, "= %d bytes", bs); 428 return buf; 429 } 430 } 431 432 433 void 434 st_status(struct mtget *bp) 435 { 436 printf("Present Mode: Density = %-12s Blocksize %s\n", 437 denstostring(bp->mt_density), getblksiz(bp->mt_blksiz)); 438 printf("---------available modes---------\n"); 439 printf("Mode 0: Density = %-12s Blocksize %s\n", 440 denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0)); 441 printf("Mode 1: Density = %-12s Blocksize %s\n", 442 denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1)); 443 printf("Mode 2: Density = %-12s Blocksize %s\n", 444 denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2)); 445 printf("Mode 3: Density = %-12s Blocksize %s\n", 446 denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3)); 447 } 448 449 void 450 warn_eof(void) 451 { 452 fprintf(stderr, 453 "The \"eof\" command has been disabled.\n" 454 "Use \"weof\" if you really want to write end-of-file marks,\n" 455 "or \"eom\" if you rather want to skip to the end of " 456 "recorded medium.\n"); 457 exit(1); 458 } 459 460 #endif /* defined (__FreeBSD__) */ 461