Home | History | Annotate | Line # | Download | only in mt
mt.c revision 1.24
      1 /*	$NetBSD: mt.c,v 1.24 1997/10/05 13:07:24 veego 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.24 1997/10/05 13:07:24 veego Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 /*
     51  * mt --
     52  *   magnetic tape manipulation program
     53  */
     54 #include <rmt.h>
     55 #include <sys/types.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/mtio.h>
     58 #include <sys/stat.h>
     59 
     60 #include <ctype.h>
     61 #include <err.h>
     62 #include <fcntl.h>
     63 #include <paths.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,  1 },
     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 	{ "sethpos",    MTIOCHLOCATE, 0,          1,  0 },
     99 	{ "setspos",    MTIOCSLOCATE, 0,          1,  0 },
    100 	{ "status",	MTIOCGET,     MTNOP,      1,  0 },
    101 	{ "weof",	MTIOCTOP,     MTWEOF,     0,  1 },
    102 	{ NULL }
    103 };
    104 
    105 void printreg __P((char *, u_int, char *));
    106 void status __P((struct mtget *));
    107 void usage __P((void));
    108 int  main __P((int, char *[]));
    109 
    110 int
    111 main(argc, argv)
    112 	int argc;
    113 	char *argv[];
    114 {
    115 	const struct commands *comp = (const struct commands *) NULL;
    116 	struct mtget mt_status;
    117 	struct mtop mt_com;
    118 	int ch, len, mtfd, flags;
    119 	char *p, *tape;
    120 	int count;
    121 
    122 	if ((tape = getenv("TAPE")) == NULL)
    123 		tape = _PATH_DEFTAPE;
    124 
    125 	while ((ch = getopt(argc, argv, "f:t:")) != -1)
    126 		switch (ch) {
    127 		case 'f':
    128 		case 't':
    129 			tape = optarg;
    130 			break;
    131 		case '?':
    132 		default:
    133 			usage();
    134 		}
    135 	argc -= optind;
    136 	argv += optind;
    137 
    138 	if (argc < 1 || argc > 2)
    139 		usage();
    140 
    141 	len = strlen(p = *argv++);
    142 	for (comp = com;; comp++) {
    143 		if (comp->c_name == NULL)
    144 			errx(1, "%s: unknown command", p);
    145 		if (strncmp(p, comp->c_name, len) == 0)
    146 			break;
    147 	}
    148 
    149 	if (*argv) {
    150 		count = strtol(*argv, &p, 10);
    151 		if (count < comp->c_mincount || *p)
    152 			errx(2, "%s: illegal count", *argv);
    153 	} else
    154 		count = 1;
    155 
    156 	flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
    157 
    158 	if ((mtfd = open(tape, flags)) < 0)
    159 		err(2, "%s", tape);
    160 
    161 	switch (comp->c_spcl) {
    162 	case MTIOCTOP:
    163 		if (comp->c_code == MTASF) {
    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 			mt_com.mt_op = MTFSF;
    175 			mt_com.mt_count = count;
    176 			if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    177 				err(2, "%s", tape);
    178 
    179 		} else {
    180 			mt_com.mt_op = comp->c_code;
    181 			mt_com.mt_count = count;
    182 
    183 			if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    184 				err(2, "%s: %s", tape, comp->c_name);
    185 
    186 		}
    187 		break;
    188 
    189 	case MTIOCGET:
    190 		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
    191 			err(2, "%s: %s", tape, comp->c_name);
    192 		status(&mt_status);
    193 		break;
    194 
    195 	case MTIOCRDSPOS:
    196 	case MTIOCRDHPOS:
    197 		if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
    198 			err(2, "%s", tape);
    199 		printf("%s: block location %u\n", tape, (unsigned int) count);
    200 		break;
    201 
    202 	case MTIOCSLOCATE:
    203 	case MTIOCHLOCATE:
    204 		if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
    205 			err(2, "%s", tape);
    206 		break;
    207 
    208 	default:
    209 		errx(1, "internal error: unknown request %d", comp->c_spcl);
    210 	}
    211 
    212 	exit(0);
    213 	/* NOTREACHED */
    214 }
    215 
    216 #ifdef sun
    217 #include <sundev/tmreg.h>
    218 #include <sundev/arreg.h>
    219 #endif
    220 
    221 #ifdef tahoe
    222 #include <tahoe/vba/cyreg.h>
    223 #endif
    224 
    225 struct tape_desc {
    226 	short	t_type;		/* type of magtape device */
    227 	char	*t_name;	/* printing name */
    228 	char	*t_dsbits;	/* "drive status" register */
    229 	char	*t_erbits;	/* "error" register */
    230 } tapes[] = {
    231 #ifdef sun
    232 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
    233 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
    234 #endif
    235 #ifdef tahoe
    236 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
    237 #endif
    238 	{ 0x7,		"SCSI",		"76543210",	"76543210" },
    239 	{ 0 }
    240 };
    241 
    242 /*
    243  * Interpret the status buffer returned
    244  */
    245 void
    246 status(bp)
    247 	struct mtget *bp;
    248 {
    249 	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 	u_int v;
    279 	char *bits;
    280 {
    281 	int i, any = 0;
    282 	char c;
    283 
    284 	if (bits && *bits == 8)
    285 		printf("%s=%o", s, v);
    286 	else
    287 		printf("%s=%x", s, v);
    288 	bits++;
    289 	if (v && bits) {
    290 		putchar('<');
    291 		while ((i = *bits++)) {
    292 			if (v & (1 << (i-1))) {
    293 				if (any)
    294 					putchar(',');
    295 				any = 1;
    296 				for (; (c = *bits) > 32; bits++)
    297 					putchar(c);
    298 			} else
    299 				for (; *bits > 32; bits++)
    300 					;
    301 		}
    302 		putchar('>');
    303 	}
    304 }
    305 
    306 void
    307 usage()
    308 {
    309 	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
    310 	exit(1);
    311 }
    312