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