1 /* $OpenBSD: mt.c,v 1.18 1998/07/16 22:31:14 deraadt Exp $ */ 2 /* $NetBSD: mt.c,v 1.14.2.1 1996/05/27 15:12:11 mrg Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static char copyright[] = 39 "@(#) Copyright (c) 1980, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)mt.c 8.2 (Berkeley) 6/6/93"; 46 #else 47 static char rcsid[] = "$OpenBSD: mt.c,v 1.18 1998/07/16 22:31:14 deraadt Exp $"; 48 #endif 49 #endif /* not lint */ 50 51 /* 52 * mt -- 53 * magnetic tape manipulation program 54 */ 55 #include <sys/types.h> 56 #include <sys/ioctl.h> 57 #include <sys/mtio.h> 58 #include <sys/stat.h> 59 #include <sys/disklabel.h> 60 61 #include <ctype.h> 62 #include <err.h> 63 #include <errno.h> 64 #include <fcntl.h> 65 #include <paths.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <string.h> 69 #include <unistd.h> 70 #include <util.h> 71 72 #include "mt.h" 73 74 struct commands { 75 char *c_name; 76 int c_code; 77 int c_ronly; 78 int c_mincount; 79 } com[] = { 80 { "blocksize", MTSETBSIZ, 1, 0 }, 81 { "bsf", MTBSF, 1, 1 }, 82 { "bsr", MTBSR, 1, 1 }, 83 { "density", MTSETDNSTY, 1, 1 }, 84 { "eof", MTWEOF, 0, 1 }, 85 { "eom", MTEOM, 1, 1 }, 86 { "erase", MTERASE, 0, 1 }, 87 { "fsf", MTFSF, 1, 1 }, 88 { "fsr", MTFSR, 1, 1 }, 89 { "offline", MTOFFL, 1, 1 }, 90 { "rewind", MTREW, 1, 1 }, 91 { "rewoffl", MTOFFL, 1, 1 }, 92 { "status", MTNOP, 1, 1 }, 93 { "retension", MTRETEN, 1, 1 }, 94 { "weof", MTWEOF, 0, 1 }, 95 { NULL } 96 }; 97 #define COM_EJECT 9 /* element in the above array */ 98 99 void printreg __P((char *, u_int, char *)); 100 void status __P((struct mtget *)); 101 void usage __P((void)); 102 103 char *host = NULL; /* remote host (if any) */ 104 105 char *progname; 106 int eject = 0; 107 108 int 109 main(argc, argv) 110 int argc; 111 char *argv[]; 112 { 113 register struct commands *comp; 114 struct mtget mt_status; 115 struct mtop mt_com; 116 int ch, len, mtfd, flags; 117 char *p, *tape, *realtape; 118 119 if ((progname = strrchr(argv[0], '/'))) 120 progname++; 121 else 122 progname = argv[0]; 123 124 if (strcmp(progname, "eject") == 0) { 125 eject = 1; 126 tape = NULL; 127 } else { 128 if ((tape = getenv("TAPE")) == NULL) 129 tape = _PATH_DEFTAPE; 130 } 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 default: 139 usage(); 140 } 141 } 142 argc -= optind; 143 argv += optind; 144 145 if (eject) { 146 if (argc == 1) { 147 tape = *argv++; 148 argc--; 149 } 150 151 if (argc != 0) 152 usage(); 153 } else if (argc < 1 || argc > 2) 154 usage(); 155 156 if (tape == NULL) 157 usage(); 158 159 if (strchr(tape, ':')) { 160 host = tape; 161 tape = strchr(host, ':'); 162 *tape++ = '\0'; 163 if (rmthost(host) == 0) 164 exit(X_ABORT); 165 } 166 167 if (eject) 168 comp = &com[COM_EJECT]; 169 else { 170 len = strlen(p = *argv++); 171 for (comp = com;; comp++) { 172 if (comp->c_name == NULL) 173 errx(1, "%s: unknown command", p); 174 if (strncmp(p, comp->c_name, len) == 0) 175 break; 176 } 177 } 178 179 flags = comp->c_ronly ? O_RDONLY : O_WRONLY | O_CREAT; 180 if ((mtfd = host ? rmtopen(tape, flags) : opendev(tape, flags, 181 OPENDEV_PART | OPENDEV_DRCT, &realtape)) < 0) { 182 if (errno != 0) 183 warn("%s", host ? tape : realtape); 184 exit(2); 185 } 186 if (comp->c_code != MTNOP) { 187 mt_com.mt_op = comp->c_code; 188 if (*argv) { 189 mt_com.mt_count = strtol(*argv, &p, 10); 190 if (mt_com.mt_count < comp->c_mincount || *p) 191 errx(2, "%s: illegal count", *argv); 192 } 193 else 194 mt_com.mt_count = 1; 195 if ((host ? rmtioctl(mt_com.mt_op, mt_com.mt_count) : 196 ioctl(mtfd, MTIOCTOP, &mt_com)) < 0) 197 err(2, "%s: %s", tape, comp->c_name); 198 } else { 199 if (host) 200 status(rmtstatus()); 201 else { 202 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0) 203 err(2, "ioctl MTIOCGET"); 204 status(&mt_status); 205 } 206 } 207 208 if (host) 209 rmtclose(); 210 211 exit(X_FINOK); 212 /* NOTREACHED */ 213 } 214 215 #ifdef sun 216 #include <sundev/tmreg.h> 217 #include <sundev/arreg.h> 218 #endif 219 220 #ifdef tahoe 221 #include <tahoe/vba/cyreg.h> 222 #endif 223 224 struct tape_desc { 225 short t_type; /* type of magtape device */ 226 char *t_name; /* printing name */ 227 char *t_dsbits; /* "drive status" register */ 228 char *t_erbits; /* "error" register */ 229 } tapes[] = { 230 #ifdef sun 231 { MT_ISCPC, "TapeMaster", TMS_BITS, 0 }, 232 { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS }, 233 #endif 234 #ifdef tahoe 235 { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS }, 236 #endif 237 #define SCSI_DS_BITS "\20\5WriteProtect\2Mounted" 238 { 0x7, "SCSI", SCSI_DS_BITS, "76543210" }, 239 { 0 } 240 }; 241 242 /* 243 * Interpret the status buffer returned 244 */ 245 void 246 status(bp) 247 register struct mtget *bp; 248 { 249 register struct tape_desc *mt; 250 251 for (mt = tapes;; mt++) { 252 if (mt->t_type == 0) { 253 (void)printf("%d: unknown tape drive type\n", 254 bp->mt_type); 255 return; 256 } 257 if (mt->t_type == bp->mt_type) 258 break; 259 } 260 (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid); 261 printreg("ds", bp->mt_dsreg, mt->t_dsbits); 262 printreg("\ner", bp->mt_erreg, mt->t_erbits); 263 (void)putchar('\n'); 264 (void)printf("blocksize: %d (%d, %d, %d, %d)\n", 265 bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1], 266 bp->mt_mblksiz[2], bp->mt_mblksiz[3]); 267 (void)printf("density: %d (%d, %d, %d, %d)\n", 268 bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1], 269 bp->mt_mdensity[2], bp->mt_mdensity[3]); 270 } 271 272 /* 273 * Print a register a la the %b format of the kernel's printf. 274 */ 275 void 276 printreg(s, v, bits) 277 char *s; 278 register u_int v; 279 register char *bits; 280 { 281 register int i, any = 0; 282 register char c; 283 284 if (bits && *bits == 8) 285 printf("%s=%o", s, v); 286 else 287 printf("%s=%x", s, v); 288 if (!bits) 289 return; 290 bits++; 291 if (v && *bits) { 292 putchar('<'); 293 while ((i = *bits++)) { 294 if (v & (1 << (i-1))) { 295 if (any) 296 putchar(','); 297 any = 1; 298 for (; (c = *bits) > 32; bits++) 299 putchar(c); 300 } else 301 for (; *bits > 32; bits++) 302 ; 303 } 304 putchar('>'); 305 } 306 } 307 308 void 309 usage() 310 { 311 if (eject) 312 (void)fprintf(stderr, "usage: %s [-f] device\n", progname); 313 else 314 (void)fprintf(stderr, 315 "usage: %s [-f device] command [ count ]\n", progname); 316 exit(X_USAGE); 317 } 318