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