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