Home | History | Annotate | Line # | Download | only in mt
mt.c revision 1.1.1.2
      1      1.1  cgd /*
      2  1.1.1.1  cgd  * Copyright (c) 1980, 1993
      3  1.1.1.1  cgd  *	The Regents of the University of California.  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.1.1  cgd static char copyright[] =
     36  1.1.1.1  cgd "@(#) Copyright (c) 1980, 1993\n\
     37  1.1.1.1  cgd 	The Regents of the University of California.  All rights reserved.\n";
     38      1.1  cgd #endif /* not lint */
     39      1.1  cgd 
     40      1.1  cgd #ifndef lint
     41  1.1.1.2  tls static char sccsid[] = "@(#)mt.c	8.2 (Berkeley) 5/4/95";
     42      1.1  cgd #endif /* not lint */
     43      1.1  cgd 
     44      1.1  cgd /*
     45      1.1  cgd  * mt --
     46      1.1  cgd  *   magnetic tape manipulation program
     47      1.1  cgd  */
     48      1.1  cgd #include <sys/types.h>
     49      1.1  cgd #include <sys/ioctl.h>
     50      1.1  cgd #include <sys/mtio.h>
     51  1.1.1.2  tls 
     52  1.1.1.2  tls #include <ctype.h>
     53  1.1.1.1  cgd #include <errno.h>
     54  1.1.1.2  tls #include <fcntl.h>
     55      1.1  cgd #include <stdio.h>
     56  1.1.1.2  tls #include <stdlib.h>
     57  1.1.1.1  cgd #include <string.h>
     58  1.1.1.2  tls #include <unistd.h>
     59      1.1  cgd 
     60      1.1  cgd struct commands {
     61      1.1  cgd 	char *c_name;
     62      1.1  cgd 	int c_code;
     63      1.1  cgd 	int c_ronly;
     64      1.1  cgd } com[] = {
     65  1.1.1.1  cgd 	{ "bsf",	MTBSF,	1 },
     66  1.1.1.1  cgd 	{ "bsr",	MTBSR,	1 },
     67      1.1  cgd 	{ "eof",	MTWEOF,	0 },
     68      1.1  cgd 	{ "fsf",	MTFSF,	1 },
     69      1.1  cgd 	{ "fsr",	MTFSR,	1 },
     70      1.1  cgd 	{ "offline",	MTOFFL,	1 },
     71  1.1.1.1  cgd 	{ "rewind",	MTREW,	1 },
     72      1.1  cgd 	{ "rewoffl",	MTOFFL,	1 },
     73      1.1  cgd 	{ "status",	MTNOP,	1 },
     74  1.1.1.1  cgd 	{ "weof",	MTWEOF,	0 },
     75  1.1.1.1  cgd 	{ NULL }
     76      1.1  cgd };
     77      1.1  cgd 
     78  1.1.1.1  cgd void err __P((const char *, ...));
     79  1.1.1.1  cgd void printreg __P((char *, u_int, char *));
     80  1.1.1.1  cgd void status __P((struct mtget *));
     81  1.1.1.1  cgd void usage __P((void));
     82      1.1  cgd 
     83  1.1.1.1  cgd int
     84      1.1  cgd main(argc, argv)
     85  1.1.1.1  cgd 	int argc;
     86  1.1.1.1  cgd 	char *argv[];
     87      1.1  cgd {
     88      1.1  cgd 	register struct commands *comp;
     89  1.1.1.1  cgd 	struct mtget mt_status;
     90  1.1.1.1  cgd 	struct mtop mt_com;
     91  1.1.1.1  cgd 	int ch, len, mtfd;
     92  1.1.1.1  cgd 	char *p, *tape;
     93  1.1.1.1  cgd 
     94  1.1.1.1  cgd 	if ((tape = getenv("TAPE")) == NULL)
     95  1.1.1.1  cgd 		tape = DEFTAPE;
     96  1.1.1.1  cgd 
     97  1.1.1.1  cgd 	while ((ch = getopt(argc, argv, "f:t:")) != EOF)
     98  1.1.1.1  cgd 		switch(ch) {
     99  1.1.1.1  cgd 		case 'f':
    100  1.1.1.1  cgd 		case 't':
    101  1.1.1.1  cgd 			tape = optarg;
    102  1.1.1.1  cgd 			break;
    103  1.1.1.1  cgd 		case '?':
    104  1.1.1.1  cgd 		default:
    105  1.1.1.1  cgd 			usage();
    106  1.1.1.1  cgd 		}
    107  1.1.1.1  cgd 	argc -= optind;
    108  1.1.1.1  cgd 	argv += optind;
    109      1.1  cgd 
    110  1.1.1.1  cgd 	if (argc < 1 || argc > 2)
    111  1.1.1.1  cgd 		usage();
    112  1.1.1.1  cgd 
    113  1.1.1.1  cgd 	len = strlen(p = *argv++);
    114  1.1.1.1  cgd 	for (comp = com;; comp++) {
    115  1.1.1.1  cgd 		if (comp->c_name == NULL)
    116  1.1.1.1  cgd 			err("%s: unknown command", p);
    117  1.1.1.1  cgd 		if (strncmp(p, comp->c_name, len) == 0)
    118      1.1  cgd 			break;
    119      1.1  cgd 	}
    120  1.1.1.1  cgd 	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
    121  1.1.1.1  cgd 		err("%s: %s", tape, strerror(errno));
    122      1.1  cgd 	if (comp->c_code != MTNOP) {
    123      1.1  cgd 		mt_com.mt_op = comp->c_code;
    124  1.1.1.1  cgd 		if (*argv) {
    125  1.1.1.1  cgd 			mt_com.mt_count = strtol(*argv, &p, 10);
    126  1.1.1.1  cgd 			if (mt_com.mt_count <= 0 || *p)
    127  1.1.1.1  cgd 				err("%s: illegal count", *argv);
    128      1.1  cgd 		}
    129  1.1.1.1  cgd 		else
    130  1.1.1.1  cgd 			mt_com.mt_count = 1;
    131  1.1.1.1  cgd 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    132  1.1.1.1  cgd 			err("%s: %s: %s", tape, comp->c_name, strerror(errno));
    133      1.1  cgd 	} else {
    134  1.1.1.1  cgd 		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
    135  1.1.1.1  cgd 			err("%s", strerror(errno));
    136      1.1  cgd 		status(&mt_status);
    137      1.1  cgd 	}
    138  1.1.1.1  cgd 	exit (0);
    139  1.1.1.1  cgd 	/* NOTREACHED */
    140      1.1  cgd }
    141      1.1  cgd 
    142      1.1  cgd #ifdef vax
    143  1.1.1.1  cgd #include <vax/mba/mtreg.h>
    144  1.1.1.1  cgd #include <vax/mba/htreg.h>
    145      1.1  cgd 
    146  1.1.1.1  cgd #include <vax/uba/utreg.h>
    147  1.1.1.1  cgd #include <vax/uba/tmreg.h>
    148      1.1  cgd #undef b_repcnt		/* argh */
    149  1.1.1.1  cgd #include <vax/uba/tsreg.h>
    150      1.1  cgd #endif
    151      1.1  cgd 
    152      1.1  cgd #ifdef sun
    153      1.1  cgd #include <sundev/tmreg.h>
    154      1.1  cgd #include <sundev/arreg.h>
    155      1.1  cgd #endif
    156      1.1  cgd 
    157      1.1  cgd #ifdef tahoe
    158      1.1  cgd #include <tahoe/vba/cyreg.h>
    159      1.1  cgd #endif
    160      1.1  cgd 
    161      1.1  cgd struct tape_desc {
    162      1.1  cgd 	short	t_type;		/* type of magtape device */
    163      1.1  cgd 	char	*t_name;	/* printing name */
    164      1.1  cgd 	char	*t_dsbits;	/* "drive status" register */
    165      1.1  cgd 	char	*t_erbits;	/* "error" register */
    166      1.1  cgd } tapes[] = {
    167      1.1  cgd #ifdef vax
    168      1.1  cgd 	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
    169      1.1  cgd 	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
    170      1.1  cgd 	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
    171      1.1  cgd 	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
    172      1.1  cgd 	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
    173      1.1  cgd #endif
    174      1.1  cgd #ifdef sun
    175      1.1  cgd 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
    176      1.1  cgd 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
    177      1.1  cgd #endif
    178      1.1  cgd #ifdef tahoe
    179      1.1  cgd 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
    180      1.1  cgd #endif
    181      1.1  cgd 	{ 0 }
    182      1.1  cgd };
    183      1.1  cgd 
    184      1.1  cgd /*
    185      1.1  cgd  * Interpret the status buffer returned
    186      1.1  cgd  */
    187  1.1.1.1  cgd void
    188      1.1  cgd status(bp)
    189      1.1  cgd 	register struct mtget *bp;
    190      1.1  cgd {
    191      1.1  cgd 	register struct tape_desc *mt;
    192      1.1  cgd 
    193  1.1.1.1  cgd 	for (mt = tapes;; mt++) {
    194  1.1.1.1  cgd 		if (mt->t_type == 0) {
    195  1.1.1.1  cgd 			(void)printf("%d: unknown tape drive type\n",
    196  1.1.1.1  cgd 			    bp->mt_type);
    197  1.1.1.1  cgd 			return;
    198  1.1.1.1  cgd 		}
    199      1.1  cgd 		if (mt->t_type == bp->mt_type)
    200      1.1  cgd 			break;
    201      1.1  cgd 	}
    202  1.1.1.1  cgd 	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
    203      1.1  cgd 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
    204      1.1  cgd 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
    205  1.1.1.1  cgd 	(void)putchar('\n');
    206      1.1  cgd }
    207      1.1  cgd 
    208      1.1  cgd /*
    209  1.1.1.1  cgd  * Print a register a la the %b format of the kernel's printf.
    210      1.1  cgd  */
    211  1.1.1.1  cgd void
    212      1.1  cgd printreg(s, v, bits)
    213      1.1  cgd 	char *s;
    214  1.1.1.1  cgd 	register u_int v;
    215      1.1  cgd 	register char *bits;
    216      1.1  cgd {
    217      1.1  cgd 	register int i, any = 0;
    218      1.1  cgd 	register char c;
    219      1.1  cgd 
    220      1.1  cgd 	if (bits && *bits == 8)
    221      1.1  cgd 		printf("%s=%o", s, v);
    222      1.1  cgd 	else
    223      1.1  cgd 		printf("%s=%x", s, v);
    224      1.1  cgd 	bits++;
    225      1.1  cgd 	if (v && bits) {
    226      1.1  cgd 		putchar('<');
    227      1.1  cgd 		while (i = *bits++) {
    228      1.1  cgd 			if (v & (1 << (i-1))) {
    229      1.1  cgd 				if (any)
    230      1.1  cgd 					putchar(',');
    231      1.1  cgd 				any = 1;
    232      1.1  cgd 				for (; (c = *bits) > 32; bits++)
    233      1.1  cgd 					putchar(c);
    234      1.1  cgd 			} else
    235      1.1  cgd 				for (; *bits > 32; bits++)
    236      1.1  cgd 					;
    237      1.1  cgd 		}
    238      1.1  cgd 		putchar('>');
    239      1.1  cgd 	}
    240  1.1.1.1  cgd }
    241  1.1.1.1  cgd 
    242  1.1.1.1  cgd void
    243  1.1.1.1  cgd usage()
    244  1.1.1.1  cgd {
    245  1.1.1.1  cgd 	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
    246  1.1.1.1  cgd 	exit(1);
    247  1.1.1.1  cgd }
    248  1.1.1.1  cgd 
    249  1.1.1.1  cgd #if __STDC__
    250  1.1.1.1  cgd #include <stdarg.h>
    251  1.1.1.1  cgd #else
    252  1.1.1.1  cgd #include <varargs.h>
    253  1.1.1.1  cgd #endif
    254  1.1.1.1  cgd 
    255  1.1.1.1  cgd void
    256  1.1.1.1  cgd #if __STDC__
    257  1.1.1.1  cgd err(const char *fmt, ...)
    258  1.1.1.1  cgd #else
    259  1.1.1.1  cgd err(fmt, va_alist)
    260  1.1.1.1  cgd 	char *fmt;
    261  1.1.1.1  cgd         va_dcl
    262  1.1.1.1  cgd #endif
    263  1.1.1.1  cgd {
    264  1.1.1.1  cgd 	va_list ap;
    265  1.1.1.1  cgd #if __STDC__
    266  1.1.1.1  cgd 	va_start(ap, fmt);
    267  1.1.1.1  cgd #else
    268  1.1.1.1  cgd 	va_start(ap);
    269  1.1.1.1  cgd #endif
    270  1.1.1.1  cgd 	(void)fprintf(stderr, "mt: ");
    271  1.1.1.1  cgd 	(void)vfprintf(stderr, fmt, ap);
    272  1.1.1.1  cgd 	va_end(ap);
    273  1.1.1.1  cgd 	(void)fprintf(stderr, "\n");
    274  1.1.1.1  cgd 	exit(1);
    275  1.1.1.1  cgd 	/* NOTREACHED */
    276      1.1  cgd }
    277