Home | History | Annotate | Line # | Download | only in mt
mt.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1980 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.2  mycroft /*static char sccsid[] = "from: @(#)mt.c	5.6 (Berkeley) 6/6/91";*/
     42  1.4  mycroft static char rcsid[] = "$Id: mt.c,v 1.4 1994/04/05 21:13:55 mycroft Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd /*
     46  1.1      cgd  * mt --
     47  1.1      cgd  *   magnetic tape manipulation program
     48  1.1      cgd  */
     49  1.1      cgd #include <sys/types.h>
     50  1.1      cgd #include <sys/ioctl.h>
     51  1.1      cgd #include <sys/mtio.h>
     52  1.1      cgd #include <fcntl.h>
     53  1.1      cgd #include <stdio.h>
     54  1.3      jtc #include <stdlib.h>
     55  1.3      jtc #include <string.h>
     56  1.1      cgd #include <ctype.h>
     57  1.3      jtc #include <err.h>
     58  1.1      cgd 
     59  1.3      jtc static void printreg();
     60  1.3      jtc static void status();
     61  1.3      jtc static void usage();
     62  1.1      cgd 
     63  1.1      cgd struct commands {
     64  1.1      cgd 	char *c_name;
     65  1.1      cgd 	int c_code;
     66  1.1      cgd 	int c_ronly;
     67  1.1      cgd } com[] = {
     68  1.4  mycroft 	{ "weof",	MTWEOF,	 0 },
     69  1.4  mycroft 	{ "eof",	MTWEOF,	 0 },
     70  1.4  mycroft 	{ "fsf",	MTFSF,	 1 },
     71  1.4  mycroft 	{ "bsf",	MTBSF,	 1 },
     72  1.4  mycroft 	{ "fsr",	MTFSR,	 1 },
     73  1.4  mycroft 	{ "bsr",	MTBSR,	 1 },
     74  1.4  mycroft 	{ "rewind",	MTREW,	 1 },
     75  1.4  mycroft 	{ "offline",	MTOFFL,	 1 },
     76  1.4  mycroft 	{ "rewoffl",	MTOFFL,	 1 },
     77  1.4  mycroft 	{ "status",	MTNOP,	 1 },
     78  1.4  mycroft 	{ "retension",	MTRETEN, 1 },
     79  1.4  mycroft 	{ "erase",	MTERASE, 0 },
     80  1.4  mycroft 	{ "eom",	MTEOM,	 1 },
     81  1.4  mycroft 	{ "nbsf",	MTNBSF,	 1 },
     82  1.1      cgd 	{ 0 }
     83  1.1      cgd };
     84  1.1      cgd 
     85  1.1      cgd int mtfd;
     86  1.1      cgd struct mtop mt_com;
     87  1.1      cgd struct mtget mt_status;
     88  1.3      jtc char *tape = NULL;
     89  1.1      cgd 
     90  1.3      jtc int
     91  1.1      cgd main(argc, argv)
     92  1.3      jtc 	int argc;
     93  1.1      cgd 	char **argv;
     94  1.1      cgd {
     95  1.1      cgd 	register char *cp;
     96  1.1      cgd 	register struct commands *comp;
     97  1.3      jtc 	int c;
     98  1.1      cgd 
     99  1.3      jtc 	while ((c = getopt(argc, argv, "f:t:")) != -1) {
    100  1.3      jtc 		switch (c) {
    101  1.3      jtc 		case 'f':
    102  1.3      jtc 		case 't':
    103  1.3      jtc 			tape = optarg;
    104  1.3      jtc 			break;
    105  1.3      jtc 		default:
    106  1.3      jtc 			usage();
    107  1.3      jtc 			/* NOTREACHED */
    108  1.3      jtc 		}
    109  1.3      jtc 	}
    110  1.3      jtc 	argc -= optind;
    111  1.3      jtc 	argv += optind;
    112  1.3      jtc 
    113  1.3      jtc 	if (argc < 1 || argc > 2) {
    114  1.3      jtc 		usage();
    115  1.3      jtc 		/* NOTREACHED */
    116  1.1      cgd 	}
    117  1.3      jtc 
    118  1.3      jtc 	cp = argv[0];
    119  1.1      cgd 	for (comp = com; comp->c_name != NULL; comp++)
    120  1.1      cgd 		if (strncmp(cp, comp->c_name, strlen(cp)) == 0)
    121  1.1      cgd 			break;
    122  1.1      cgd 	if (comp->c_name == NULL) {
    123  1.3      jtc 		errx(1, "don't grok \"%s\"", cp);
    124  1.3      jtc 		/* NOTREACHED */
    125  1.3      jtc 	}
    126  1.3      jtc 
    127  1.3      jtc 	if (tape == NULL) {
    128  1.3      jtc 		if ((tape = getenv("TAPE")) == NULL)
    129  1.3      jtc 			tape = DEFTAPE;
    130  1.1      cgd 	}
    131  1.3      jtc 
    132  1.1      cgd 	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0) {
    133  1.3      jtc 		err(1, "%s", tape);
    134  1.3      jtc 		/* NOTREACHED */
    135  1.1      cgd 	}
    136  1.1      cgd 	if (comp->c_code != MTNOP) {
    137  1.1      cgd 		mt_com.mt_op = comp->c_code;
    138  1.3      jtc 		mt_com.mt_count = (argc == 2 ? atoi(argv[1]) : 1);
    139  1.1      cgd 		if (mt_com.mt_count < 0) {
    140  1.3      jtc 			errx(1, "negative repeat count");
    141  1.3      jtc 			/* NOTREACHED */
    142  1.1      cgd 		}
    143  1.1      cgd 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
    144  1.3      jtc 			err(2, "%s %s %d failed",
    145  1.3      jtc 				tape, comp->c_name, mt_com.mt_count);
    146  1.3      jtc 			/* NOTREACHED */
    147  1.1      cgd 		}
    148  1.1      cgd 	} else {
    149  1.1      cgd 		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
    150  1.3      jtc 			err(2, NULL);
    151  1.3      jtc 			/* NOTREACHED */
    152  1.1      cgd 		}
    153  1.1      cgd 		status(&mt_status);
    154  1.1      cgd 	}
    155  1.3      jtc 
    156  1.3      jtc 	exit(0);
    157  1.1      cgd }
    158  1.1      cgd 
    159  1.1      cgd #ifdef vax
    160  1.1      cgd #include <vaxmba/mtreg.h>
    161  1.1      cgd #include <vaxmba/htreg.h>
    162  1.1      cgd 
    163  1.1      cgd #include <vaxuba/utreg.h>
    164  1.1      cgd #include <vaxuba/tmreg.h>
    165  1.1      cgd #undef b_repcnt		/* argh */
    166  1.1      cgd #include <vaxuba/tsreg.h>
    167  1.1      cgd #endif
    168  1.1      cgd 
    169  1.1      cgd #ifdef sun
    170  1.1      cgd #include <sundev/tmreg.h>
    171  1.1      cgd #include <sundev/arreg.h>
    172  1.1      cgd #endif
    173  1.1      cgd 
    174  1.1      cgd #ifdef tahoe
    175  1.1      cgd #include <tahoe/vba/cyreg.h>
    176  1.1      cgd #endif
    177  1.1      cgd 
    178  1.1      cgd struct tape_desc {
    179  1.1      cgd 	short	t_type;		/* type of magtape device */
    180  1.1      cgd 	char	*t_name;	/* printing name */
    181  1.1      cgd 	char	*t_dsbits;	/* "drive status" register */
    182  1.1      cgd 	char	*t_erbits;	/* "error" register */
    183  1.1      cgd } tapes[] = {
    184  1.1      cgd #ifdef vax
    185  1.1      cgd 	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
    186  1.1      cgd 	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
    187  1.1      cgd 	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
    188  1.1      cgd 	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
    189  1.1      cgd 	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
    190  1.1      cgd #endif
    191  1.1      cgd #ifdef sun
    192  1.1      cgd 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
    193  1.1      cgd 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
    194  1.1      cgd #endif
    195  1.1      cgd #ifdef tahoe
    196  1.1      cgd 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
    197  1.1      cgd #endif
    198  1.1      cgd 	{ 0 }
    199  1.1      cgd };
    200  1.1      cgd 
    201  1.1      cgd /*
    202  1.1      cgd  * Interpret the status buffer returned
    203  1.1      cgd  */
    204  1.3      jtc static void
    205  1.1      cgd status(bp)
    206  1.1      cgd 	register struct mtget *bp;
    207  1.1      cgd {
    208  1.1      cgd 	register struct tape_desc *mt;
    209  1.1      cgd 
    210  1.1      cgd 	for (mt = tapes; mt->t_type; mt++)
    211  1.1      cgd 		if (mt->t_type == bp->mt_type)
    212  1.1      cgd 			break;
    213  1.1      cgd 	if (mt->t_type == 0) {
    214  1.1      cgd 		printf("unknown tape drive type (%d)\n", bp->mt_type);
    215  1.1      cgd 		return;
    216  1.1      cgd 	}
    217  1.1      cgd 	printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
    218  1.1      cgd 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
    219  1.1      cgd 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
    220  1.1      cgd 	putchar('\n');
    221  1.1      cgd }
    222  1.1      cgd 
    223  1.1      cgd /*
    224  1.1      cgd  * Print a register a la the %b format of the kernel's printf
    225  1.1      cgd  */
    226  1.3      jtc static void
    227  1.1      cgd printreg(s, v, bits)
    228  1.1      cgd 	char *s;
    229  1.1      cgd 	register char *bits;
    230  1.1      cgd 	register unsigned short v;
    231  1.1      cgd {
    232  1.1      cgd 	register int i, any = 0;
    233  1.1      cgd 	register char c;
    234  1.1      cgd 
    235  1.1      cgd 	if (bits && *bits == 8)
    236  1.1      cgd 		printf("%s=%o", s, v);
    237  1.1      cgd 	else
    238  1.1      cgd 		printf("%s=%x", s, v);
    239  1.1      cgd 	bits++;
    240  1.1      cgd 	if (v && bits) {
    241  1.1      cgd 		putchar('<');
    242  1.1      cgd 		while (i = *bits++) {
    243  1.1      cgd 			if (v & (1 << (i-1))) {
    244  1.1      cgd 				if (any)
    245  1.1      cgd 					putchar(',');
    246  1.1      cgd 				any = 1;
    247  1.1      cgd 				for (; (c = *bits) > 32; bits++)
    248  1.1      cgd 					putchar(c);
    249  1.1      cgd 			} else
    250  1.1      cgd 				for (; *bits > 32; bits++)
    251  1.1      cgd 					;
    252  1.1      cgd 		}
    253  1.1      cgd 		putchar('>');
    254  1.1      cgd 	}
    255  1.3      jtc }
    256  1.3      jtc 
    257  1.3      jtc static void
    258  1.3      jtc usage()
    259  1.3      jtc {
    260  1.3      jtc 	fprintf(stderr, "usage: mt [ -f device ] command [ count ]\n");
    261  1.3      jtc 	exit(1);
    262  1.1      cgd }
    263