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