Home | History | Annotate | Line # | Download | only in mt
mt.c revision 1.19
      1 /*	$NetBSD: mt.c,v 1.19 1997/04/15 06:53:51 lukem 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 #ifndef lint
     37 static char copyright[] =
     38 "@(#) 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 static char rcsid[] = "$NetBSD: mt.c,v 1.19 1997/04/15 06:53:51 lukem 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;
     74 	int c_code;
     75 	int c_ronly;
     76 };
     77 
     78 const struct commands com[] = {
     79 	{ "asf",	MTASF,  1 },
     80 	{ "blocksize",	MTSETBSIZ, 1 },
     81 	{ "bsf",	MTBSF,	1 },
     82 	{ "bsr",	MTBSR,	1 },
     83 	{ "density",	MTSETDNSTY, 1 },
     84 	{ "eof",	MTWEOF,	0 },
     85 	{ "eom",	MTEOM,	1 },
     86 	{ "erase",	MTERASE, 0 },
     87 	{ "fsf",	MTFSF,	1 },
     88 	{ "fsr",	MTFSR,	1 },
     89 	{ "offline",	MTOFFL,	1 },
     90 	{ "rewind",	MTREW,	1 },
     91 	{ "rewoffl",	MTOFFL,	1 },
     92 	{ "status",	MTNOP,	1 },
     93 	{ "retension",	MTRETEN, 1 },
     94 	{ "weof",	MTWEOF,	0 },
     95 	{ NULL }
     96 };
     97 
     98 void printreg __P((char *, u_int, char *));
     99 void status __P((struct mtget *));
    100 void usage __P((void));
    101 
    102 int
    103 main(argc, argv)
    104 	int argc;
    105 	char *argv[];
    106 {
    107 	const struct commands *comp;
    108 	struct mtget mt_status;
    109 	struct mtop mt_com;
    110 	int ch, len, mtfd, flags;
    111 	char *p, *tape;
    112 	int count;
    113 
    114 	if ((tape = getenv("TAPE")) == NULL)
    115 		tape = _PATH_DEFTAPE;
    116 
    117 	while ((ch = getopt(argc, argv, "f:t:")) != -1)
    118 		switch (ch) {
    119 		case 'f':
    120 		case 't':
    121 			tape = optarg;
    122 			break;
    123 		case '?':
    124 		default:
    125 			usage();
    126 		}
    127 	argc -= optind;
    128 	argv += optind;
    129 
    130 	if (argc < 1 || argc > 2)
    131 		usage();
    132 
    133 	len = strlen(p = *argv++);
    134 	for (comp = com;; comp++) {
    135 		if (comp->c_name == NULL)
    136 			errx(1, "%s: unknown command", p);
    137 		if (strncmp(p, comp->c_name, len) == 0)
    138 			break;
    139 	}
    140 
    141 	flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
    142 	if ((mtfd = open(tape, flags)) < 0)
    143 		err(2, "%s", tape);
    144 
    145 	if (comp->c_code == MTASF) {
    146 		/* If mtget.mt_fileno was implemented, We could
    147 		   compute the minimal seek needed to position
    148 		   the tape.  Until then, rewind and seek from
    149 		   begining-of-tape */
    150 
    151 		/* zero is a valid count */
    152 		if (*argv) {
    153 			count = strtol(*argv, &p, 10);
    154 			if (count < 0 || *p)
    155 				errx(2, "%s: illegal count", *argv);
    156 		}
    157 		else
    158 			count = 1;
    159 
    160 		mt_com.mt_op = MTREW;
    161 		mt_com.mt_count = 1;
    162 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    163 			err(2, "%s", tape);
    164 
    165 		mt_com.mt_op = MTFSF;
    166 		mt_com.mt_count = count;
    167 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    168 			err(2, "%s", tape);
    169 
    170 	} else if (comp->c_code != MTNOP) {
    171 
    172 		/* zero is *not* a valid count */
    173 		if (*argv) {
    174 			count = strtol(*argv, &p, 10);
    175 			if (count <= 0 || *p)
    176 				errx(2, "%s: illegal count", *argv);
    177 		}
    178 		else
    179 			count = 1;
    180 
    181 		mt_com.mt_op = comp->c_code;
    182 		mt_com.mt_count = count;
    183 
    184 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    185 			err(2, "%s: %s", tape, comp->c_name);
    186 
    187 	} else {
    188 		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
    189 			err(2, "%s: %s", tape, comp->c_name);
    190 		status(&mt_status);
    191 	}
    192 
    193 	exit(0);
    194 	/* NOTREACHED */
    195 }
    196 
    197 #ifdef sun
    198 #include <sundev/tmreg.h>
    199 #include <sundev/arreg.h>
    200 #endif
    201 
    202 #ifdef tahoe
    203 #include <tahoe/vba/cyreg.h>
    204 #endif
    205 
    206 struct tape_desc {
    207 	short	t_type;		/* type of magtape device */
    208 	char	*t_name;	/* printing name */
    209 	char	*t_dsbits;	/* "drive status" register */
    210 	char	*t_erbits;	/* "error" register */
    211 } tapes[] = {
    212 #ifdef sun
    213 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
    214 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
    215 #endif
    216 #ifdef tahoe
    217 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
    218 #endif
    219 	{ 0x7,		"SCSI",		"76543210",	"76543210" },
    220 	{ 0 }
    221 };
    222 
    223 /*
    224  * Interpret the status buffer returned
    225  */
    226 void
    227 status(bp)
    228 	struct mtget *bp;
    229 {
    230 	struct tape_desc *mt;
    231 
    232 	for (mt = tapes;; mt++) {
    233 		if (mt->t_type == 0) {
    234 			(void)printf("%d: unknown tape drive type\n",
    235 			    bp->mt_type);
    236 			return;
    237 		}
    238 		if (mt->t_type == bp->mt_type)
    239 			break;
    240 	}
    241 	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
    242 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
    243 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
    244 	(void)putchar('\n');
    245 	(void)printf("blocksize: %ld (%ld, %ld, %ld, %ld)\n",
    246 		bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
    247 		bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
    248 	(void)printf("density: %ld (%ld, %ld, %ld, %ld)\n",
    249 		bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
    250 		bp->mt_mdensity[2], bp->mt_mdensity[3]);
    251 }
    252 
    253 /*
    254  * Print a register a la the %b format of the kernel's printf.
    255  */
    256 void
    257 printreg(s, v, bits)
    258 	char *s;
    259 	u_int v;
    260 	char *bits;
    261 {
    262 	int i, any = 0;
    263 	char c;
    264 
    265 	if (bits && *bits == 8)
    266 		printf("%s=%o", s, v);
    267 	else
    268 		printf("%s=%x", s, v);
    269 	bits++;
    270 	if (v && bits) {
    271 		putchar('<');
    272 		while ((i = *bits++)) {
    273 			if (v & (1 << (i-1))) {
    274 				if (any)
    275 					putchar(',');
    276 				any = 1;
    277 				for (; (c = *bits) > 32; bits++)
    278 					putchar(c);
    279 			} else
    280 				for (; *bits > 32; bits++)
    281 					;
    282 		}
    283 		putchar('>');
    284 	}
    285 }
    286 
    287 void
    288 usage()
    289 {
    290 	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
    291 	exit(1);
    292 }
    293