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