Home | History | Annotate | Line # | Download | only in mt
mt.c revision 1.36
      1 /* $NetBSD: mt.c,v 1.36 2001/09/16 21:57:34 wiz 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.36 2001/09/16 21:57:34 wiz 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 <rmt.h>
     64 #include <stdio.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 #include <unistd.h>
     68 
     69 /* pseudo ioctl constants */
     70 #define MTASF	100
     71 
     72 struct commands {
     73 	const char *c_name;		/* command */
     74 	int c_spcl;			/* ioctl request */
     75 	int c_code;			/* ioctl code for MTIOCTOP command */
     76 	int c_ronly;			/* open tape read-only */
     77 	int c_mincount;			/* min allowed count value */
     78 };
     79 
     80 const struct commands com[] = {
     81 	{ "asf",	MTIOCTOP,     MTASF,      1,  0 },
     82 	{ "blocksize",	MTIOCTOP,     MTSETBSIZ,  1,  0 },
     83 	{ "bsf",	MTIOCTOP,     MTBSF,      1,  1 },
     84 	{ "bsr",	MTIOCTOP,     MTBSR,      1,  1 },
     85 	{ "compress",	MTIOCTOP,     MTCMPRESS,  1,  0 },
     86 	{ "density",	MTIOCTOP,     MTSETDNSTY, 1,  0 },
     87 	{ "eof",	MTIOCTOP,     MTWEOF,     0,  1 },
     88 	{ "eom",	MTIOCTOP,     MTEOM,      1,  0 },
     89 	{ "erase",	MTIOCTOP,     MTERASE,    0,  0 },
     90 	{ "fsf",	MTIOCTOP,     MTFSF,      1,  1 },
     91 	{ "fsr",	MTIOCTOP,     MTFSR,      1,  1 },
     92 	{ "offline",	MTIOCTOP,     MTOFFL,     1,  0 },
     93 	{ "rdhpos",     MTIOCRDHPOS,  0,          1,  0 },
     94 	{ "rdspos",     MTIOCRDSPOS,  0,          1,  0 },
     95 	{ "retension",	MTIOCTOP,     MTRETEN,    1,  0 },
     96 	{ "rewind",	MTIOCTOP,     MTREW,      1,  0 },
     97 	{ "rewoffl",	MTIOCTOP,     MTOFFL,     1,  0 },
     98 	{ "setblk",	MTIOCTOP,     MTSETBSIZ,  1,  0 },
     99 	{ "setdensity",	MTIOCTOP,     MTSETDNSTY, 1,  0 },
    100 	{ "sethpos",    MTIOCHLOCATE, 0,          1,  0 },
    101 	{ "setspos",    MTIOCSLOCATE, 0,          1,  0 },
    102 	{ "status",	MTIOCGET,     MTNOP,      1,  0 },
    103 	{ "weof",	MTIOCTOP,     MTWEOF,     0,  1 },
    104 	{ "eew",	MTIOCTOP,     MTEWARN,    1,  0 },
    105 	{ NULL }
    106 };
    107 
    108 void printreg(const char *, u_int, const char *);
    109 void status(struct mtget *);
    110 void usage(void);
    111 int main(int, char *[]);
    112 
    113 int
    114 main(int argc, char *argv[])
    115 {
    116 	const struct commands *comp = (const struct commands *) NULL;
    117 	struct mtget mt_status;
    118 	struct mtop mt_com;
    119 	int ch, len, mtfd, flags;
    120 	char *p;
    121 	const char *tape;
    122 	int count;
    123 
    124 	setprogname(argv[0]);
    125 	if ((tape = getenv("TAPE")) == NULL)
    126 		tape = _PATH_DEFTAPE;
    127 
    128 	while ((ch = getopt(argc, argv, "f:t:")) != -1)
    129 		switch (ch) {
    130 		case 'f':
    131 		case 't':
    132 			tape = optarg;
    133 			break;
    134 		case '?':
    135 		default:
    136 			usage();
    137 		}
    138 	argc -= optind;
    139 	argv += optind;
    140 
    141 	if (argc < 1 || argc > 2)
    142 		usage();
    143 
    144 	len = strlen(p = *argv++);
    145 	for (comp = com;; comp++) {
    146 		if (comp->c_name == NULL)
    147 			errx(1, "%s: unknown command", p);
    148 		if (strncmp(p, comp->c_name, len) == 0)
    149 			break;
    150 	}
    151 
    152 	if (*argv) {
    153 		count = strtol(*argv, &p, 10);
    154 		if (count < comp->c_mincount || *p)
    155 			errx(2, "%s: illegal count", *argv);
    156 	} else
    157 		count = 1;
    158 
    159 	flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
    160 
    161 	if ((mtfd = open(tape, flags)) < 0)
    162 		err(2, "%s", tape);
    163 
    164 	switch (comp->c_spcl) {
    165 	case MTIOCTOP:
    166 		if (comp->c_code == MTASF) {
    167 
    168 			/* If mtget.mt_fileno was implemented, We could
    169 			   compute the minimal seek needed to position
    170 			   the tape.  Until then, rewind and seek from
    171 			   begining-of-tape */
    172 
    173 			mt_com.mt_op = MTREW;
    174 			mt_com.mt_count = 1;
    175 			if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    176 				err(2, "%s", tape);
    177 
    178 			if (count > 0) {
    179 				mt_com.mt_op = MTFSF;
    180 				    mt_com.mt_count = count;
    181 				if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    182 				    err(2, "%s", tape);
    183 			}
    184 
    185 		} else {
    186 			mt_com.mt_op = comp->c_code;
    187 			mt_com.mt_count = count;
    188 
    189 			if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    190 				err(2, "%s: %s", tape, comp->c_name);
    191 
    192 		}
    193 		break;
    194 
    195 	case MTIOCGET:
    196 		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
    197 			err(2, "%s: %s", tape, comp->c_name);
    198 		status(&mt_status);
    199 		break;
    200 
    201 	case MTIOCRDSPOS:
    202 	case MTIOCRDHPOS:
    203 		if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
    204 			err(2, "%s", tape);
    205 		printf("%s: block location %u\n", tape, (unsigned int) count);
    206 		break;
    207 
    208 	case MTIOCSLOCATE:
    209 	case MTIOCHLOCATE:
    210 		if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
    211 			err(2, "%s", tape);
    212 		break;
    213 
    214 	default:
    215 		errx(1, "internal error: unknown request %d", comp->c_spcl);
    216 	}
    217 
    218 	exit(0);
    219 	/* NOTREACHED */
    220 }
    221 
    222 #if defined(sun) && !defined(__SVR4)
    223 #include <sundev/tmreg.h>
    224 #include <sundev/arreg.h>
    225 #endif
    226 
    227 #ifdef tahoe
    228 #include <tahoe/vba/cyreg.h>
    229 #endif
    230 
    231 const struct tape_desc {
    232 	short	t_type;		/* type of magtape device */
    233 	const	char *t_name;	/* printing name */
    234 	const	char *t_dsbits;	/* "drive status" register */
    235 	const	char *t_erbits;	/* "error" register */
    236 } tapes[] = {
    237 #if defined(sun) && !defined(__SVR4)
    238 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
    239 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
    240 #endif
    241 #ifdef tahoe
    242 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
    243 #endif
    244 #define SCSI_DS_BITS	"\20\5WriteProtect\2Mounted"
    245 	{ 0x7,		"SCSI",		SCSI_DS_BITS,	"76543210" },
    246 	{ 0 }
    247 };
    248 
    249 
    250 /*
    251  * Interpret the status buffer returned
    252  */
    253 void
    254 status(struct mtget *bp)
    255 {
    256 	const struct tape_desc *mt;
    257 
    258 	for (mt = tapes;; mt++) {
    259 		if (mt->t_type == 0) {
    260 			(void)printf("%d: unknown tape drive type\n",
    261 			    bp->mt_type);
    262 			return;
    263 		}
    264 		if (mt->t_type == bp->mt_type)
    265 			break;
    266 	}
    267 	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
    268 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
    269 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
    270 	(void)putchar('\n');
    271 	(void)printf("blocksize: %d (%d, %d, %d, %d)\n",
    272 		bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
    273 		bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
    274 	(void)printf("density: %d (%d, %d, %d, %d)\n",
    275 		bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
    276 		bp->mt_mdensity[2], bp->mt_mdensity[3]);
    277 	(void)printf("current file number: %d\n", bp->mt_fileno);
    278 	(void)printf("current block number: %d\n", bp->mt_blkno);
    279 }
    280 
    281 /*
    282  * Print a register a la the %b format of the kernel's printf.
    283  */
    284 void
    285 printreg(const char *s, u_int v, const char *bits)
    286 {
    287 	int any, i;
    288 	char c;
    289 
    290 	any = 0;
    291 	if (bits && *bits == 8)
    292 		printf("%s=%o", s, v);
    293 	else
    294 		printf("%s=%x", s, v);
    295 	bits++;
    296 	if (v && *bits) {
    297 		putchar('<');
    298 		while ((i = *bits++)) {
    299 			if (v & (1 << (i-1))) {
    300 				if (any)
    301 					putchar(',');
    302 				any = 1;
    303 				for (; (c = *bits) > 32; bits++)
    304 					putchar(c);
    305 			} else
    306 				for (; *bits > 32; bits++)
    307 					;
    308 		}
    309 		putchar('>');
    310 	}
    311 }
    312 
    313 void
    314 usage(void)
    315 {
    316 	(void)fprintf(stderr, "usage: %s [-f device] command [ count ]\n", getprogname());
    317 	exit(1);
    318 	/* NOTREACHED */
    319 }
    320