Home | History | Annotate | Line # | Download | only in mt
mt.c revision 1.8
      1 /*	$NetBSD: mt.c,v 1.8 1995/09/28 07:18:20 tls 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.8 1995/09/28 07:18:20 tls 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 <fcntl.h>
     58 #include <err.h>
     59 #include <stdlib.h>
     60 #include <stdio.h>
     61 #include <ctype.h>
     62 #include <string.h>
     63 
     64 struct commands {
     65 	char *c_name;
     66 	int c_code;
     67 	int c_ronly;
     68 } com[] = {
     69 	{ "bsf",	MTBSF,	1 },
     70 	{ "bsr",	MTBSR,	1 },
     71 	{ "eof",	MTWEOF,	0 },
     72 	{ "eom",	MTEOM,	1 },
     73 	{ "erase",	MTERASE, 0 },
     74 	{ "fsf",	MTFSF,	1 },
     75 	{ "fsr",	MTFSR,	1 },
     76 	{ "offline",	MTOFFL,	1 },
     77 	{ "rewind",	MTREW,	1 },
     78 	{ "rewoffl",	MTOFFL,	1 },
     79 	{ "status",	MTNOP,	1 },
     80 	{ "retension",	MTRETEN, 1 },
     81 	{ "weof",	MTWEOF,	0 },
     82 	{ NULL }
     83 };
     84 
     85 void printreg __P((char *, u_int, char *));
     86 void status __P((struct mtget *));
     87 void usage __P((void));
     88 
     89 int
     90 main(argc, argv)
     91 	int argc;
     92 	char *argv[];
     93 {
     94 	register struct commands *comp;
     95 	struct mtget mt_status;
     96 	struct mtop mt_com;
     97 	int ch, len, mtfd;
     98 	char *p, *tape;
     99 
    100 	if ((tape = getenv("TAPE")) == NULL)
    101 		tape = DEFTAPE;
    102 
    103 	while ((ch = getopt(argc, argv, "f:t:")) != -1)
    104 		switch (ch) {
    105 		case 'f':
    106 		case 't':
    107 			tape = optarg;
    108 			break;
    109 		case '?':
    110 		default:
    111 			usage();
    112 		}
    113 	argc -= optind;
    114 	argv += optind;
    115 
    116 	if (argc < 1 || argc > 2)
    117 		usage();
    118 
    119 	len = strlen(p = *argv++);
    120 	for (comp = com;; comp++) {
    121 		if (comp->c_name == NULL)
    122 			errx(1, "%s: unknown command", p);
    123 		if (strncmp(p, comp->c_name, len) == 0)
    124 			break;
    125 	}
    126 	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
    127 		err(2, "%s", tape);
    128 	if (comp->c_code != MTNOP) {
    129 		mt_com.mt_op = comp->c_code;
    130 		if (*argv) {
    131 			mt_com.mt_count = strtol(*argv, &p, 10);
    132 			if (mt_com.mt_count <= 0 || *p)
    133 				errx(2, "%s: illegal count", *argv);
    134 		}
    135 		else
    136 			mt_com.mt_count = 1;
    137 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    138 			err(2, "%s: %s", tape, comp->c_name);
    139 	} else {
    140 		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
    141 			err(2, "ioctl MTIOCGET");
    142 		status(&mt_status);
    143 	}
    144 	exit (0);
    145 	/* NOTREACHED */
    146 }
    147 
    148 #ifdef sun
    149 #include <sundev/tmreg.h>
    150 #include <sundev/arreg.h>
    151 #endif
    152 
    153 #ifdef tahoe
    154 #include <tahoe/vba/cyreg.h>
    155 #endif
    156 
    157 struct tape_desc {
    158 	short	t_type;		/* type of magtape device */
    159 	char	*t_name;	/* printing name */
    160 	char	*t_dsbits;	/* "drive status" register */
    161 	char	*t_erbits;	/* "error" register */
    162 } tapes[] = {
    163 #ifdef sun
    164 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
    165 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
    166 #endif
    167 #ifdef tahoe
    168 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
    169 #endif
    170 	{ 0 }
    171 };
    172 
    173 /*
    174  * Interpret the status buffer returned
    175  */
    176 void
    177 status(bp)
    178 	register struct mtget *bp;
    179 {
    180 	register struct tape_desc *mt;
    181 
    182 	for (mt = tapes;; mt++) {
    183 		if (mt->t_type == 0) {
    184 			(void)printf("%d: unknown tape drive type\n",
    185 			    bp->mt_type);
    186 			return;
    187 		}
    188 		if (mt->t_type == bp->mt_type)
    189 			break;
    190 	}
    191 	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
    192 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
    193 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
    194 	(void)putchar('\n');
    195 }
    196 
    197 /*
    198  * Print a register a la the %b format of the kernel's printf.
    199  */
    200 void
    201 printreg(s, v, bits)
    202 	char *s;
    203 	register u_int v;
    204 	register char *bits;
    205 {
    206 	register int i, any = 0;
    207 	register char c;
    208 
    209 	if (bits && *bits == 8)
    210 		printf("%s=%o", s, v);
    211 	else
    212 		printf("%s=%x", s, v);
    213 	bits++;
    214 	if (v && bits) {
    215 		putchar('<');
    216 		while (i = *bits++) {
    217 			if (v & (1 << (i-1))) {
    218 				if (any)
    219 					putchar(',');
    220 				any = 1;
    221 				for (; (c = *bits) > 32; bits++)
    222 					putchar(c);
    223 			} else
    224 				for (; *bits > 32; bits++)
    225 					;
    226 		}
    227 		putchar('>');
    228 	}
    229 }
    230 
    231 void
    232 usage()
    233 {
    234 	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
    235 	exit(1);
    236 }
    237